Visual Studio 17 2022 Win64: Silently ignores toolchain file settings for CMAKE_C_COMPILER and CMAKE_CXX_COMPILER
I have a toolchain file, cmake\toolchains\windows_x86_64.cmake
In that toolchain file, among other settings, i set:
cmake_path(SET CMAKE_C_COMPILER NORMALIZE "${MY_MSVC_BINPATH}/cl.exe")
cmake_path(SET CMAKE_CXX_COMPILER NORMALIZE "${MY_MSVC_BINPATH}/cl.exe")
cmake_path(SET CMAKE_ASM_NASM_COMPILER NORMALIZE "${MY_NASM_BINPATH}/nasm.exe")
This works correctly with the Ninja generator.
When I use the Visual Studio 17 2022 Win64 generator, CMake silently ignores my settings for CMAKE_C_COMPILER
and CMAKE_ASM_NASM_COMPILER
, and uses instead C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.31.31103/bin/Hostx64/x64/cl.exe
What I expected was for CMake to do one of the following
- Error out and refuse to complete configuration, knowing that it would be ignoring an explicit setting from the toolchain file.
- Use the settings from the toolchain file as they were configured.
What I did not expect was for CMake to (nearly) silently choose to ignore my toolchain files C and CXX compiler setting, and use some other compiler that I did not specify.
This is important to my organization, because MSVC has a history of silently introducing subtle bugs in their code generator, so we capture a known-good snapshot of the visual studio toolchain for use by all developer and continuous integration systems.
With this bug, typical developers, who would likely be using the Visual Studio IDE, will silently encounter inexplicable issues with their local builds that aren't encountered with builds from the continuous integration system.
Toward that end, imagine a developer thinking that they can drop in place "linux_x86_64.cmake" as their toolchain file (which, for our project, exists and works as expected with the Ninja generator).
With CMake silently ignoring some of the settings, that developer will be building with MSVC and not their linux compiler of choice, without realizing it, assuming no other settings are interfering with the compile process like custom linkers or assemblers.