Ninja: On Windows, select the compiler occurring first in PATH

On Windows, toolchains like MSVC require a set of environment variables
to be configured for the compiler to work correctly.  The scripts that
prepare these environments typically put the compiler's directory ahead
of others in the `PATH`.  Teach the Ninja generators to use this as a
heuristic to select the compiler when none is explicitly specified.

This is not necessary with Makefile generators because each toolchain's
environment comes with its own make tool variant, and the corresponding
Makefile generator (e.g. "NMake Makefiles") automatically implies the
matching compiler.

Fixes: #20585
This commit is contained in:
Brad King
2020-04-17 09:44:30 -04:00
parent 8bc757c8f6
commit e01990999a
11 changed files with 51 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
ninja-compiler-PATH-windows
---------------------------
* On Windows, the :generator:`Ninja` and :generator:`Ninja Multi-Config`
generators, when a compiler is not explicitly specified, now select
the first compiler (of any name) found in directories listed by the
``PATH`` environment variable.

View File

@@ -53,6 +53,20 @@ macro(_cmake_find_compiler lang)
NO_DEFAULT_PATH
DOC "${lang} compiler")
endif()
if(CMAKE_HOST_WIN32 AND CMAKE_GENERATOR MATCHES "Ninja")
# On Windows command-line builds, the Makefile generators each imply
# a preferred compiler tool. The Ninja generator does not imply a
# compiler tool, so use the compiler that occurs first in PATH.
find_program(CMAKE_${lang}_COMPILER
NAMES ${CMAKE_${lang}_COMPILER_LIST}
NAMES_PER_DIR
DOC "${lang} compiler"
NO_PACKAGE_ROOT_PATH
NO_CMAKE_PATH
NO_CMAKE_ENVIRONMENT_PATH
NO_CMAKE_SYSTEM_PATH
)
endif()
find_program(CMAKE_${lang}_COMPILER NAMES ${CMAKE_${lang}_COMPILER_LIST} DOC "${lang} compiler")
if(CMAKE_${lang}_COMPILER_INIT AND NOT CMAKE_${lang}_COMPILER)
set_property(CACHE CMAKE_${lang}_COMPILER PROPERTY VALUE "${CMAKE_${lang}_COMPILER_INIT}")

View File

@@ -18,6 +18,12 @@ else()
message(FATAL_ERROR "'ninja --version' reported:\n${ninja_out}")
endif()
if(CMAKE_HOST_WIN32)
run_cmake(SelectCompilerWindows)
else()
run_cmake(SelectCompilerUNIX)
endif()
function(run_NinjaToolMissing)
set(RunCMake_MAKE_PROGRAM ninja-tool-missing)
run_cmake(NinjaToolMissing)

View File

@@ -0,0 +1,2 @@
#!/bin/sh
false

View File

@@ -0,0 +1,2 @@
#!/bin/sh
false

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,6 @@
^CMake Error at .*/Modules/CMakeTestCCompiler.cmake:[0-9]+ \(message\):
The C compiler
"[^"]*/Tests/RunCMake/Ninja/SelectCompiler/2/cc"
is not able to compile a simple test program.

View File

@@ -0,0 +1,3 @@
set(ENV{PATH} "${CMAKE_CURRENT_SOURCE_DIR}/SelectCompiler/1:${CMAKE_CURRENT_SOURCE_DIR}/SelectCompiler/2:$ENV{PATH}")
unset(ENV{CC})
enable_language(C)

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,6 @@
^CMake Error at .*/Modules/CMakeTestCCompiler.cmake:[0-9]+ \(message\):
The C compiler
"[^"]*/Tests/RunCMake/Ninja/SelectCompiler/1/gcc"
is not able to compile a simple test program.

View File

@@ -0,0 +1,3 @@
set(ENV{PATH} "${CMAKE_CURRENT_SOURCE_DIR}/SelectCompiler/1;${CMAKE_CURRENT_SOURCE_DIR}/SelectCompiler/2;$ENV{PATH}")
unset(ENV{CC})
enable_language(C)