Ninja: On Windows, select the compiler occurring first in PATH
Hello,
In Modules/CMakeDetermineCXXCompiler.cmake
the variable CMAKE_CXX_COMPILER_LIST
is set, which is then used by _cmake_find_compiler
(https://gitlab.kitware.com/cmake/cmake/-/blob/v3.17.1/Modules/CMakeDetermineCompiler.cmake) to find the compiler to use. From the comments I can see there is supposed to be some kind of 'preferred' order, but it's not entirely clear how that works.
On my machine I have gcc
and ld
in the system PATH (because of an unrelated Haskell Platform installation), but when running from a Visual Studio command prompt (which puts cl.exe
and link.exe
first in the PATH) CMake still prefers cc
, c++
and ld
and this behavior is very surprising (I imagine even more so for newer users who might have a bunch of compilers installed when learning).
I just patched the following line in my CMake installation to get the search order I want, but I was wondering why CMake doesn't simply use the PATH to determine the user's preference.
Line I patched:
set(CMAKE_CXX_COMPILER_LIST CC ${_CMAKE_TOOLCHAIN_PREFIX}c++ ${_CMAKE_TOOLCHAIN_PREFIX}g++ aCC cl bcc xlC clang++)
I know (now) you can specify -DCMAKE_CXX_COMPILER=cl.exe
or set the CC
and CXX
variables, but I think using the PATH as the search order would make a lot more sense since it would have fewer surprises and initialization scripts always put the desired compiler in the PATH first.
I cannot find any documentation mentioning this as the 'official' search order (although admittedly I didn't look very hard) so it wouldn't break anything users couldn't depend on already.
Please let me know what you think,
Duncan