summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2020-04-17 13:44:30 (GMT)
committerBrad King <brad.king@kitware.com>2020-04-22 12:03:16 (GMT)
commite01990999a83fb08b0e46b3131501c01e3256944 (patch)
treecb89821b1aea1bbab3566897e12cb569aa0912dd
parent8bc757c8f68d653526dce3f723950ccf221a4c6c (diff)
downloadCMake-e01990999a83fb08b0e46b3131501c01e3256944.zip
CMake-e01990999a83fb08b0e46b3131501c01e3256944.tar.gz
CMake-e01990999a83fb08b0e46b3131501c01e3256944.tar.bz2
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
-rw-r--r--Help/release/dev/ninja-compiler-PATH-windows.rst7
-rw-r--r--Modules/CMakeDetermineCompiler.cmake14
-rw-r--r--Tests/RunCMake/Ninja/RunCMakeTest.cmake6
-rwxr-xr-xTests/RunCMake/Ninja/SelectCompiler/1/gcc2
-rwxr-xr-xTests/RunCMake/Ninja/SelectCompiler/2/cc2
-rw-r--r--Tests/RunCMake/Ninja/SelectCompilerUNIX-result.txt1
-rw-r--r--Tests/RunCMake/Ninja/SelectCompilerUNIX-stderr.txt6
-rw-r--r--Tests/RunCMake/Ninja/SelectCompilerUNIX.cmake3
-rw-r--r--Tests/RunCMake/Ninja/SelectCompilerWindows-result.txt1
-rw-r--r--Tests/RunCMake/Ninja/SelectCompilerWindows-stderr.txt6
-rw-r--r--Tests/RunCMake/Ninja/SelectCompilerWindows.cmake3
11 files changed, 51 insertions, 0 deletions
diff --git a/Help/release/dev/ninja-compiler-PATH-windows.rst b/Help/release/dev/ninja-compiler-PATH-windows.rst
new file mode 100644
index 0000000..cb33493
--- /dev/null
+++ b/Help/release/dev/ninja-compiler-PATH-windows.rst
@@ -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.
diff --git a/Modules/CMakeDetermineCompiler.cmake b/Modules/CMakeDetermineCompiler.cmake
index 7afae8a..da93b5c 100644
--- a/Modules/CMakeDetermineCompiler.cmake
+++ b/Modules/CMakeDetermineCompiler.cmake
@@ -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}")
diff --git a/Tests/RunCMake/Ninja/RunCMakeTest.cmake b/Tests/RunCMake/Ninja/RunCMakeTest.cmake
index a00d830..baa352c 100644
--- a/Tests/RunCMake/Ninja/RunCMakeTest.cmake
+++ b/Tests/RunCMake/Ninja/RunCMakeTest.cmake
@@ -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)
diff --git a/Tests/RunCMake/Ninja/SelectCompiler/1/gcc b/Tests/RunCMake/Ninja/SelectCompiler/1/gcc
new file mode 100755
index 0000000..151dfa6
--- /dev/null
+++ b/Tests/RunCMake/Ninja/SelectCompiler/1/gcc
@@ -0,0 +1,2 @@
+#!/bin/sh
+false
diff --git a/Tests/RunCMake/Ninja/SelectCompiler/2/cc b/Tests/RunCMake/Ninja/SelectCompiler/2/cc
new file mode 100755
index 0000000..151dfa6
--- /dev/null
+++ b/Tests/RunCMake/Ninja/SelectCompiler/2/cc
@@ -0,0 +1,2 @@
+#!/bin/sh
+false
diff --git a/Tests/RunCMake/Ninja/SelectCompilerUNIX-result.txt b/Tests/RunCMake/Ninja/SelectCompilerUNIX-result.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/Tests/RunCMake/Ninja/SelectCompilerUNIX-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/Ninja/SelectCompilerUNIX-stderr.txt b/Tests/RunCMake/Ninja/SelectCompilerUNIX-stderr.txt
new file mode 100644
index 0000000..a2baa48
--- /dev/null
+++ b/Tests/RunCMake/Ninja/SelectCompilerUNIX-stderr.txt
@@ -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.
diff --git a/Tests/RunCMake/Ninja/SelectCompilerUNIX.cmake b/Tests/RunCMake/Ninja/SelectCompilerUNIX.cmake
new file mode 100644
index 0000000..02c8439
--- /dev/null
+++ b/Tests/RunCMake/Ninja/SelectCompilerUNIX.cmake
@@ -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)
diff --git a/Tests/RunCMake/Ninja/SelectCompilerWindows-result.txt b/Tests/RunCMake/Ninja/SelectCompilerWindows-result.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/Tests/RunCMake/Ninja/SelectCompilerWindows-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/Ninja/SelectCompilerWindows-stderr.txt b/Tests/RunCMake/Ninja/SelectCompilerWindows-stderr.txt
new file mode 100644
index 0000000..e4b506e
--- /dev/null
+++ b/Tests/RunCMake/Ninja/SelectCompilerWindows-stderr.txt
@@ -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.
diff --git a/Tests/RunCMake/Ninja/SelectCompilerWindows.cmake b/Tests/RunCMake/Ninja/SelectCompilerWindows.cmake
new file mode 100644
index 0000000..f5a7e2b
--- /dev/null
+++ b/Tests/RunCMake/Ninja/SelectCompilerWindows.cmake
@@ -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)