Cannot use MSVC_IDE before first project call?

I’m writing a toolchain file and want to use the MSVC_IDE variable. However it doesn’t work.

This will not work.

if (MSVC_IDE)
message(FATAL_ERROR "Foo")
endif()

This will work

if (CMAKE_GENERATOR MATCHES "Visual Studio")
message(FATAL_ERROR "Foo")
endif()

Is there a reason why?

Hmm. That is set in Modules/Platform/Windows-MSVC.cmake using the same conditional as you have here. Are you, perhaps, using clang-cl?

Cc: @brad.king

No I’m using MSVC.

This problem isn’t exclusive to toolchains it’s any logic executed before the first project call.

Oh! I missed the “toolchain” part, sorry :man_facepalming:. Yes, the file I linked to is only run at compiler detection time, so yes, it is unavailable to toolchain files. That said, I don’t know how much more work that variable is going to carry, so the CMAKE_GENERATOR-based detection is likely the way to go.

Hm gotcha. That’s fine for me. I’d just suggest adding some documentation to clarify this. In case someone runs into this again.

https://gitlab.kitware.com/cmake/cmake/-/merge_requests/5800

1 Like