Merge topic 'try_compile-project-platform-vars'

a6562ff579 try_compile: Add option to skip passing platform variables
4843a37676 try_compile: Propagate platform variables in project-mode too

Acked-by: Kitware Robot <kwrobot@kitware.com>
Tested-by: buildbot <buildbot@kitware.com>
Merge-request: !7265
diff --git a/Help/command/try_compile.rst b/Help/command/try_compile.rst
index 4b2a631..806a98d 100644
--- a/Help/command/try_compile.rst
+++ b/Help/command/try_compile.rst
@@ -7,6 +7,8 @@
 
 Try building some code.
 
+.. _`Try Compiling Whole Projects`:
+
 Try Compiling Whole Projects
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
@@ -25,6 +27,15 @@
 build a specific target instead of the ``all`` or ``ALL_BUILD`` target.  See
 below for the meaning of other options.
 
+.. versionchanged:: 3.24
+  CMake variables describing platform settings, and those listed by the
+  :variable:`CMAKE_TRY_COMPILE_PLATFORM_VARIABLES` variable, are propagated
+  into the project's build configuration.  See policy :policy:`CMP0137`.
+  Previously this was only done by the
+  :ref:`source file <Try Compiling Source Files>` signature.
+
+.. _`Try Compiling Source Files`:
+
 Try Compiling Source Files
 ^^^^^^^^^^^^^^^^^^^^^^^^^^
 
@@ -205,3 +216,7 @@
 .. versionchanged:: 3.14
   For the :generator:`Green Hills MULTI` generator the GHS toolset and target
   system customization cache variables are also propagated into the test project.
+
+.. versionadded:: 3.24
+  The :variable:`CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES` variable may be
+  set to disable passing platform variables into the test project.
diff --git a/Help/manual/cmake-policies.7.rst b/Help/manual/cmake-policies.7.rst
index 259cebb..788d086 100644
--- a/Help/manual/cmake-policies.7.rst
+++ b/Help/manual/cmake-policies.7.rst
@@ -58,6 +58,7 @@
 .. toctree::
    :maxdepth: 1
 
+   CMP0137: try_compile() passes platform variables in project mode. </policy/CMP0137>
    CMP0136: Watcom runtime library flags are selected by an abstraction. </policy/CMP0136>
    CMP0135: ExternalProject ignores timestamps in archives by default for the URL download method. </policy/CMP0135>
    CMP0134: Fallback to \"HOST\" Windows registry view when \"TARGET\" view is not usable. </policy/CMP0134>
diff --git a/Help/manual/cmake-variables.7.rst b/Help/manual/cmake-variables.7.rst
index 00ea0bc..ff431ad 100644
--- a/Help/manual/cmake-variables.7.rst
+++ b/Help/manual/cmake-variables.7.rst
@@ -503,6 +503,7 @@
    /variable/CMAKE_STATIC_LINKER_FLAGS_CONFIG_INIT
    /variable/CMAKE_STATIC_LINKER_FLAGS_INIT
    /variable/CMAKE_TRY_COMPILE_CONFIGURATION
+   /variable/CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES
    /variable/CMAKE_TRY_COMPILE_PLATFORM_VARIABLES
    /variable/CMAKE_TRY_COMPILE_TARGET_TYPE
    /variable/CMAKE_UNITY_BUILD
diff --git a/Help/policy/CMP0137.rst b/Help/policy/CMP0137.rst
new file mode 100644
index 0000000..ba3cb9c
--- /dev/null
+++ b/Help/policy/CMP0137.rst
@@ -0,0 +1,33 @@
+CMP0137
+-------
+
+.. versionadded:: 3.24
+
+:command:`try_compile` passes platform variables in project mode.
+
+The :command:`try_compile` command :ref:`source file <Try Compiling Source
+Files>` signature propagates CMake variables containing platform settings,
+and those specified by the :variable:`CMAKE_TRY_COMPILE_PLATFORM_VARIABLES`
+variable, into the generated test project.  This helps the test project drive
+the toolchain the same way the calling project will.  In CMake 3.23 and below,
+the :ref:`whole-project <Try Compiling Whole Projects>` signature does not
+propagate platform variables automatically.  CMake 3.24 and above prefer to
+propagate platform variables in the :ref:`whole-project <Try Compiling Whole
+Projects>` signature.  This policy provides compatibility with projects that
+have not been updated to expect the behavior.
+
+The ``OLD`` behavior for this policy is to not pass any additional variables to
+the :ref:`whole-project <Try Compiling Whole Projects>` signature.
+The ``NEW`` behavior for this policy is to pass the same variables that the
+:ref:`source file <Try Compiling Source Files>` signature does.
+
+Regardless of the policy setting, the
+:variable:`CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES` variable may be set
+to suppress passing the platform variables through either signature.
+
+This policy was introduced in CMake version 3.24.  Use the
+:command:`cmake_policy` command to set this policy to ``OLD`` or ``NEW``
+explicitly. Unlike many policies, CMake version |release| does *not* warn
+by default when this policy is not set and simply uses ``OLD`` behavior.
+
+.. include:: DEPRECATED.txt
diff --git a/Help/release/dev/try_compile-project-platform-vars.rst b/Help/release/dev/try_compile-project-platform-vars.rst
new file mode 100644
index 0000000..06596d4
--- /dev/null
+++ b/Help/release/dev/try_compile-project-platform-vars.rst
@@ -0,0 +1,10 @@
+try_compile-project-platform-vars
+---------------------------------
+
+* The :command:`try_compile` command
+  :ref:`whole-project <Try Compiling Whole Projects>` signature
+  now propagates platform variables.  See policy :policy:`CMP0137`.
+
+* The :variable:`CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES` variable
+  was added to tell the :command:`try_compile` command not to
+  pass any platform variables to the test project.
diff --git a/Help/variable/CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES.rst b/Help/variable/CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES.rst
new file mode 100644
index 0000000..944caa5
--- /dev/null
+++ b/Help/variable/CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES.rst
@@ -0,0 +1,10 @@
+CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES
+---------------------------------------
+
+Set to a true value to tell the :command:`try_compile` command not
+to propagate any platform variables into the test project.
+
+The :command:`try_compile` command normally passes some CMake variables
+that configure the platform and toolchain behavior into test projects.
+See policy :policy:`CMP0137`.  This variable may be set to disable
+that behavior.
diff --git a/Help/variable/CMAKE_TRY_COMPILE_PLATFORM_VARIABLES.rst b/Help/variable/CMAKE_TRY_COMPILE_PLATFORM_VARIABLES.rst
index d178513..6b26d14 100644
--- a/Help/variable/CMAKE_TRY_COMPILE_PLATFORM_VARIABLES.rst
+++ b/Help/variable/CMAKE_TRY_COMPILE_PLATFORM_VARIABLES.rst
@@ -26,3 +26,12 @@
 setting will be made visible to the toolchain file both for the main
 project and for test projects generated by the :command:`try_compile`
 command source file signature.
+
+.. versionchanged:: 3.24
+  Listed variables are propagated to the :command:`try_compile`
+  :ref:`whole-project <Try Compiling Whole Projects>` signature too.
+  See :policy:`CMP0137`.
+
+.. versionadded:: 3.24
+  The :variable:`CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES` variable may be
+  set to disable passing platform variables into the test project.
diff --git a/Source/cmCoreTryCompile.cxx b/Source/cmCoreTryCompile.cxx
index 4909948..caa413b 100644
--- a/Source/cmCoreTryCompile.cxx
+++ b/Source/cmCoreTryCompile.cxx
@@ -872,7 +872,10 @@
   }
 
   // Forward a set of variables to the inner project cache.
-  if (this->SrcFileSignature) {
+  if ((this->SrcFileSignature ||
+       this->Makefile->GetPolicyStatus(cmPolicies::CMP0137) ==
+         cmPolicies::NEW) &&
+      !this->Makefile->IsOn("CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES")) {
     std::set<std::string> vars;
     vars.insert(&c_properties[lang_property_start],
                 &c_properties[lang_property_start + lang_property_size]);
diff --git a/Source/cmPolicies.h b/Source/cmPolicies.h
index 4977083..c8b037e 100644
--- a/Source/cmPolicies.h
+++ b/Source/cmPolicies.h
@@ -411,7 +411,10 @@
          3, 24, 0, cmPolicies::WARN)                                          \
   SELECT(POLICY, CMP0136,                                                     \
          "Watcom runtime library flags are selected by an abstraction.", 3,   \
-         24, 0, cmPolicies::WARN)
+         24, 0, cmPolicies::WARN)                                             \
+  SELECT(POLICY, CMP0137,                                                     \
+         "try_compile() passes platform variables in project mode", 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/try_compile/CMP0137-Common.cmake b/Tests/RunCMake/try_compile/CMP0137-Common.cmake
new file mode 100644
index 0000000..2c1bc0d
--- /dev/null
+++ b/Tests/RunCMake/try_compile/CMP0137-Common.cmake
@@ -0,0 +1,16 @@
+set(CMAKE_ENABLE_EXPORTS 1)
+set(FOO 2)
+set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES FOO)
+try_compile(RESULT_VAR
+  ${CMAKE_CURRENT_BINARY_DIR}/CMP0137-build
+  ${CMAKE_CURRENT_SOURCE_DIR}/CMP0137
+  TestCMP0137)
+include(${CMAKE_CURRENT_BINARY_DIR}/CMP0137-build/info.cmake OPTIONAL)
+
+message(STATUS "Enabling CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES")
+set(CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES 1)
+try_compile(RESULT_VAR
+  ${CMAKE_CURRENT_BINARY_DIR}/CMP0137-build2
+  ${CMAKE_CURRENT_SOURCE_DIR}/CMP0137
+  TestCMP0137)
+include(${CMAKE_CURRENT_BINARY_DIR}/CMP0137-build2/info.cmake OPTIONAL)
diff --git a/Tests/RunCMake/try_compile/CMP0137-NEW-stdout.txt b/Tests/RunCMake/try_compile/CMP0137-NEW-stdout.txt
new file mode 100644
index 0000000..abc61dc
--- /dev/null
+++ b/Tests/RunCMake/try_compile/CMP0137-NEW-stdout.txt
@@ -0,0 +1,5 @@
+-- CMAKE_ENABLE_EXPORTS='1'
+-- FOO='2'
+-- Enabling CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES
+-- CMAKE_ENABLE_EXPORTS=''
+-- FOO=''
diff --git a/Tests/RunCMake/try_compile/CMP0137-NEW.cmake b/Tests/RunCMake/try_compile/CMP0137-NEW.cmake
new file mode 100644
index 0000000..f7caa50
--- /dev/null
+++ b/Tests/RunCMake/try_compile/CMP0137-NEW.cmake
@@ -0,0 +1,2 @@
+cmake_policy(SET CMP0137 NEW)
+include(CMP0137-Common.cmake)
diff --git a/Tests/RunCMake/try_compile/CMP0137-WARN-stdout.txt b/Tests/RunCMake/try_compile/CMP0137-WARN-stdout.txt
new file mode 100644
index 0000000..2e4bebe
--- /dev/null
+++ b/Tests/RunCMake/try_compile/CMP0137-WARN-stdout.txt
@@ -0,0 +1,5 @@
+-- CMAKE_ENABLE_EXPORTS=''
+-- FOO=''
+-- Enabling CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES
+-- CMAKE_ENABLE_EXPORTS=''
+-- FOO=''
diff --git a/Tests/RunCMake/try_compile/CMP0137-WARN.cmake b/Tests/RunCMake/try_compile/CMP0137-WARN.cmake
new file mode 100644
index 0000000..61b5641
--- /dev/null
+++ b/Tests/RunCMake/try_compile/CMP0137-WARN.cmake
@@ -0,0 +1,2 @@
+# policy CMP0137 not set
+include(CMP0137-Common.cmake)
diff --git a/Tests/RunCMake/try_compile/CMP0137/CMakeLists.txt b/Tests/RunCMake/try_compile/CMP0137/CMakeLists.txt
new file mode 100644
index 0000000..a3b9fda
--- /dev/null
+++ b/Tests/RunCMake/try_compile/CMP0137/CMakeLists.txt
@@ -0,0 +1,6 @@
+cmake_minimum_required(VERSION 3.23)
+project(TestCMP0137 NONE)
+file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/info.cmake" "
+message(STATUS \"CMAKE_ENABLE_EXPORTS='${CMAKE_ENABLE_EXPORTS}'\")
+message(STATUS \"FOO='${FOO}'\")
+")
diff --git a/Tests/RunCMake/try_compile/RunCMakeTest.cmake b/Tests/RunCMake/try_compile/RunCMakeTest.cmake
index dcd3799..eca7bf4 100644
--- a/Tests/RunCMake/try_compile/RunCMakeTest.cmake
+++ b/Tests/RunCMake/try_compile/RunCMakeTest.cmake
@@ -72,6 +72,8 @@
 run_cmake(CMP0056)
 run_cmake(CMP0066)
 run_cmake(CMP0067)
+run_cmake(CMP0137-WARN)
+run_cmake(CMP0137-NEW)
 
 if(RunCMake_GENERATOR MATCHES "Make|Ninja")
   # Use a single build tree for a few tests without cleaning.