mirror of
https://github.com/Kitware/CMake.git
synced 2025-12-31 10:50:16 -06:00
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:
7
Help/release/dev/ninja-compiler-PATH-windows.rst
Normal file
7
Help/release/dev/ninja-compiler-PATH-windows.rst
Normal 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.
|
||||
@@ -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}")
|
||||
|
||||
@@ -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)
|
||||
|
||||
2
Tests/RunCMake/Ninja/SelectCompiler/1/gcc
Executable file
2
Tests/RunCMake/Ninja/SelectCompiler/1/gcc
Executable file
@@ -0,0 +1,2 @@
|
||||
#!/bin/sh
|
||||
false
|
||||
2
Tests/RunCMake/Ninja/SelectCompiler/2/cc
Executable file
2
Tests/RunCMake/Ninja/SelectCompiler/2/cc
Executable file
@@ -0,0 +1,2 @@
|
||||
#!/bin/sh
|
||||
false
|
||||
1
Tests/RunCMake/Ninja/SelectCompilerUNIX-result.txt
Normal file
1
Tests/RunCMake/Ninja/SelectCompilerUNIX-result.txt
Normal file
@@ -0,0 +1 @@
|
||||
1
|
||||
6
Tests/RunCMake/Ninja/SelectCompilerUNIX-stderr.txt
Normal file
6
Tests/RunCMake/Ninja/SelectCompilerUNIX-stderr.txt
Normal 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.
|
||||
3
Tests/RunCMake/Ninja/SelectCompilerUNIX.cmake
Normal file
3
Tests/RunCMake/Ninja/SelectCompilerUNIX.cmake
Normal 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)
|
||||
1
Tests/RunCMake/Ninja/SelectCompilerWindows-result.txt
Normal file
1
Tests/RunCMake/Ninja/SelectCompilerWindows-result.txt
Normal file
@@ -0,0 +1 @@
|
||||
1
|
||||
6
Tests/RunCMake/Ninja/SelectCompilerWindows-stderr.txt
Normal file
6
Tests/RunCMake/Ninja/SelectCompilerWindows-stderr.txt
Normal 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.
|
||||
3
Tests/RunCMake/Ninja/SelectCompilerWindows.cmake
Normal file
3
Tests/RunCMake/Ninja/SelectCompilerWindows.cmake
Normal 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)
|
||||
Reference in New Issue
Block a user