summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCraig Scott <craig.scott@crascit.com>2022-03-23 14:31:04 +1100
committerBrad King <brad.king@kitware.com>2022-03-29 09:42:59 -0400
commit05e510bf0bf8d730b03e8b389d088919825b4b5a (patch)
tree12fc8e73e25bf76f2e6b243ce4469dae64c84370
parent774a9eb210c09da97135cb733828752e627c96c7 (diff)
downloadcmake-05e510bf0bf8d730b03e8b389d088919825b4b5a.tar.gz
CMP0132: Don't set compiler environment variables on first run
When running CMake for the first time in a build tree, for some generators CMake would set compiler environment variables like CC, CXX, etc. when the corresponding language is enabled. That behavior was never documented and can result in different behavior between the first and subsequent runs. Add a policy to no longer set those environment variables. Fixes: #21378
-rw-r--r--Help/manual/cmake-policies.7.rst1
-rw-r--r--Help/policy/CMP0132.rst26
-rw-r--r--Help/release/dev/set-env-var-first-run.rst6
-rw-r--r--Source/cmGlobalGenerator.cxx4
-rw-r--r--Source/cmPolicies.h5
-rw-r--r--Tests/RunCMake/CMP0132/CMP0132-Common.cmake15
-rw-r--r--Tests/RunCMake/CMP0132/CMP0132-NEW-stdout.txt1
-rw-r--r--Tests/RunCMake/CMP0132/CMP0132-NEW.cmake2
-rw-r--r--Tests/RunCMake/CMP0132/CMP0132-OLD-stdout.txt1
-rw-r--r--Tests/RunCMake/CMP0132/CMP0132-OLD.cmake2
-rw-r--r--Tests/RunCMake/CMP0132/CMP0132-WARN-stdout.txt1
-rw-r--r--Tests/RunCMake/CMP0132/CMP0132-WARN.cmake2
-rw-r--r--Tests/RunCMake/CMP0132/CMakeLists.txt3
-rw-r--r--Tests/RunCMake/CMP0132/RunCMakeTest.cmake7
-rw-r--r--Tests/RunCMake/CMakeLists.txt2
15 files changed, 76 insertions, 2 deletions
diff --git a/Help/manual/cmake-policies.7.rst b/Help/manual/cmake-policies.7.rst
index 9bf2913c55..17a3764a2b 100644
--- a/Help/manual/cmake-policies.7.rst
+++ b/Help/manual/cmake-policies.7.rst
@@ -58,6 +58,7 @@ Policies Introduced by CMake 3.24
.. toctree::
:maxdepth: 1
+ CMP0132: Do not set compiler environment variables on first run. </policy/CMP0132>
CMP0131: LINK_LIBRARIES supports the LINK_ONLY generator expression. </policy/CMP0131>
CMP0130: while() diagnoses condition evaluation errors. </policy/CMP0130>
diff --git a/Help/policy/CMP0132.rst b/Help/policy/CMP0132.rst
new file mode 100644
index 0000000000..fadbbdcda2
--- /dev/null
+++ b/Help/policy/CMP0132.rst
@@ -0,0 +1,26 @@
+CMP0132
+-------
+
+.. versionadded:: 3.24
+
+Apart from when using the Xcode generator and some Visual Studio generators,
+CMake 3.23 and below will set environment variables like :envvar:`CC`,
+:envvar:`CXX`, etc. when the corresponding language is enabled.
+This only occurs on the very first time CMake is run in a build directory,
+and the environment variables are only defined at configure time, not build
+time. On subsequent CMake runs, these environment variables are not set,
+opening up the opportunity for different behavior between the first and
+subsequent CMake runs. CMake 3.24 and above prefer to not set these
+environment variables when a language is enabled, even on the first run in
+a build directory.
+
+The ``OLD`` behavior for this policy sets the relevant environment variable
+on the first run when a language is enabled. The ``NEW`` behavior for this
+policy does not set any such environment variables.
+
+This policy was introduced in CMake version 3.24. Use the
+:command:`cmake_policy` command to set it to ``OLD`` or ``NEW`` explicitly.
+Unlike many policies, CMake version |release| does *not* warn
+when this policy is not set and simply uses ``OLD`` behavior.
+
+.. include:: DEPRECATED.txt
diff --git a/Help/release/dev/set-env-var-first-run.rst b/Help/release/dev/set-env-var-first-run.rst
new file mode 100644
index 0000000000..c3f7d9f0ad
--- /dev/null
+++ b/Help/release/dev/set-env-var-first-run.rst
@@ -0,0 +1,6 @@
+set-env-var-first-run
+---------------------
+
+* CMake no longer sets environment variables like :envvar:`CC`, :envvar:`CXX`,
+ etc. when enabling the corresponding language during the first CMake run in
+ a build directory. See policy :policy:`CMP0132`.
diff --git a/Source/cmGlobalGenerator.cxx b/Source/cmGlobalGenerator.cxx
index baa54e58a0..fb84cd1646 100644
--- a/Source/cmGlobalGenerator.cxx
+++ b/Source/cmGlobalGenerator.cxx
@@ -759,7 +759,9 @@ void cmGlobalGenerator::EnableLanguage(
needTestLanguage[lang] = true;
// Some generators like visual studio should not use the env variables
// So the global generator can specify that in this variable
- if (!mf->GetDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV")) {
+ if ((mf->GetPolicyStatus(cmPolicies::CMP0132) == cmPolicies::OLD ||
+ mf->GetPolicyStatus(cmPolicies::CMP0132) == cmPolicies::WARN) &&
+ !mf->GetDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV")) {
// put ${CMake_(LANG)_COMPILER_ENV_VAR}=${CMAKE_(LANG)_COMPILER
// into the environment, in case user scripts want to run
// configure, or sub cmakes
diff --git a/Source/cmPolicies.h b/Source/cmPolicies.h
index 3b9d067595..434c51c237 100644
--- a/Source/cmPolicies.h
+++ b/Source/cmPolicies.h
@@ -393,7 +393,10 @@ class cmMakefile;
3, 24, 0, cmPolicies::WARN) \
SELECT(POLICY, CMP0131, \
"LINK_LIBRARIES supports the LINK_ONLY generator expression.", 3, \
- 24, 0, cmPolicies::WARN)
+ 24, 0, cmPolicies::WARN) \
+ SELECT(POLICY, CMP0132, \
+ "Do not set compiler environment variables on first run", 3, 24, 0, \
+ cmPolicies::WARN)
#define CM_SELECT_ID(F, A1, A2, A3, A4, A5, A6) F(A1)
#define CM_FOR_EACH_POLICY_ID(POLICY) \
diff --git a/Tests/RunCMake/CMP0132/CMP0132-Common.cmake b/Tests/RunCMake/CMP0132/CMP0132-Common.cmake
new file mode 100644
index 0000000000..796f61c689
--- /dev/null
+++ b/Tests/RunCMake/CMP0132/CMP0132-Common.cmake
@@ -0,0 +1,15 @@
+if(NOT "$ENV{CC}" STREQUAL "")
+ message(STATUS "Test environment already sets CC, test being SKIPPED")
+ return()
+elseif(CMAKE_GENERATOR MATCHES "Xcode|Visual Studio")
+ message(STATUS "This generator never sets CC, test being SKIPPED")
+ return()
+endif()
+
+enable_language(C)
+
+if("$ENV{CC}" STREQUAL "")
+ message(STATUS "CC was left unset")
+else()
+ message(STATUS "CC was set to $ENV{CC}")
+endif()
diff --git a/Tests/RunCMake/CMP0132/CMP0132-NEW-stdout.txt b/Tests/RunCMake/CMP0132/CMP0132-NEW-stdout.txt
new file mode 100644
index 0000000000..8056c2ca9d
--- /dev/null
+++ b/Tests/RunCMake/CMP0132/CMP0132-NEW-stdout.txt
@@ -0,0 +1 @@
+SKIPPED|CC was left unset
diff --git a/Tests/RunCMake/CMP0132/CMP0132-NEW.cmake b/Tests/RunCMake/CMP0132/CMP0132-NEW.cmake
new file mode 100644
index 0000000000..fabb4196dc
--- /dev/null
+++ b/Tests/RunCMake/CMP0132/CMP0132-NEW.cmake
@@ -0,0 +1,2 @@
+cmake_policy(SET CMP0132 NEW)
+include(CMP0132-Common.cmake)
diff --git a/Tests/RunCMake/CMP0132/CMP0132-OLD-stdout.txt b/Tests/RunCMake/CMP0132/CMP0132-OLD-stdout.txt
new file mode 100644
index 0000000000..c13142893a
--- /dev/null
+++ b/Tests/RunCMake/CMP0132/CMP0132-OLD-stdout.txt
@@ -0,0 +1 @@
+SKIPPED|CC was set
diff --git a/Tests/RunCMake/CMP0132/CMP0132-OLD.cmake b/Tests/RunCMake/CMP0132/CMP0132-OLD.cmake
new file mode 100644
index 0000000000..aae4f2a87c
--- /dev/null
+++ b/Tests/RunCMake/CMP0132/CMP0132-OLD.cmake
@@ -0,0 +1,2 @@
+cmake_policy(SET CMP0132 OLD)
+include(CMP0132-Common.cmake)
diff --git a/Tests/RunCMake/CMP0132/CMP0132-WARN-stdout.txt b/Tests/RunCMake/CMP0132/CMP0132-WARN-stdout.txt
new file mode 100644
index 0000000000..c13142893a
--- /dev/null
+++ b/Tests/RunCMake/CMP0132/CMP0132-WARN-stdout.txt
@@ -0,0 +1 @@
+SKIPPED|CC was set
diff --git a/Tests/RunCMake/CMP0132/CMP0132-WARN.cmake b/Tests/RunCMake/CMP0132/CMP0132-WARN.cmake
new file mode 100644
index 0000000000..c07e5db031
--- /dev/null
+++ b/Tests/RunCMake/CMP0132/CMP0132-WARN.cmake
@@ -0,0 +1,2 @@
+
+include(CMP0132-Common.cmake)
diff --git a/Tests/RunCMake/CMP0132/CMakeLists.txt b/Tests/RunCMake/CMP0132/CMakeLists.txt
new file mode 100644
index 0000000000..5ff8d3e0ff
--- /dev/null
+++ b/Tests/RunCMake/CMP0132/CMakeLists.txt
@@ -0,0 +1,3 @@
+cmake_minimum_required(VERSION 3.23)
+project(${RunCMake_TEST} NONE)
+include(${RunCMake_TEST}.cmake)
diff --git a/Tests/RunCMake/CMP0132/RunCMakeTest.cmake b/Tests/RunCMake/CMP0132/RunCMakeTest.cmake
new file mode 100644
index 0000000000..db599ce706
--- /dev/null
+++ b/Tests/RunCMake/CMP0132/RunCMakeTest.cmake
@@ -0,0 +1,7 @@
+include(RunCMake)
+
+if(NOT CMAKE_GENERATOR_NO_COMPILER_ENV)
+ run_cmake(CMP0132-WARN)
+ run_cmake(CMP0132-OLD)
+ run_cmake(CMP0132-NEW)
+endif()
diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt
index fffbc6be6a..8ea5a5043f 100644
--- a/Tests/RunCMake/CMakeLists.txt
+++ b/Tests/RunCMake/CMakeLists.txt
@@ -148,6 +148,8 @@ if("${CMAKE_C_COMPILER_ID}" STREQUAL "LCC" OR
add_RunCMake_test("CMP0129")
endif()
+add_RunCMake_test(CMP0132)
+
# The test for Policy 65 requires the use of the
# CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS variable, which both the VS and Xcode
# generators ignore. The policy will have no effect on those generators.