Browse Source

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 <[email protected]>
Tested-by: buildbot <[email protected]>
Merge-request: !7265
Brad King 3 years ago
parent
commit
a7cd15a61c

+ 15 - 0
Help/command/try_compile.rst

@@ -7,6 +7,8 @@ try_compile
 
 
 Try building some code.
 Try building some code.
 
 
+.. _`Try Compiling Whole Projects`:
+
 Try Compiling Whole Projects
 Try Compiling Whole Projects
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 
@@ -25,6 +27,15 @@ will not be deleted after this command is run.  Specify ``<targetName>`` to
 build a specific target instead of the ``all`` or ``ALL_BUILD`` target.  See
 build a specific target instead of the ``all`` or ``ALL_BUILD`` target.  See
 below for the meaning of other options.
 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
 Try Compiling Source Files
 ^^^^^^^^^^^^^^^^^^^^^^^^^^
 ^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 
@@ -205,3 +216,7 @@ a build configuration.
 .. versionchanged:: 3.14
 .. versionchanged:: 3.14
   For the :generator:`Green Hills MULTI` generator the GHS toolset and target
   For the :generator:`Green Hills MULTI` generator the GHS toolset and target
   system customization cache variables are also propagated into the test project.
   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.

+ 1 - 0
Help/manual/cmake-policies.7.rst

@@ -58,6 +58,7 @@ Policies Introduced by CMake 3.24
 .. toctree::
 .. toctree::
    :maxdepth: 1
    :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>
    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>
    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>
    CMP0134: Fallback to \"HOST\" Windows registry view when \"TARGET\" view is not usable. </policy/CMP0134>

+ 1 - 0
Help/manual/cmake-variables.7.rst

@@ -503,6 +503,7 @@ Variables that Control the Build
    /variable/CMAKE_STATIC_LINKER_FLAGS_CONFIG_INIT
    /variable/CMAKE_STATIC_LINKER_FLAGS_CONFIG_INIT
    /variable/CMAKE_STATIC_LINKER_FLAGS_INIT
    /variable/CMAKE_STATIC_LINKER_FLAGS_INIT
    /variable/CMAKE_TRY_COMPILE_CONFIGURATION
    /variable/CMAKE_TRY_COMPILE_CONFIGURATION
+   /variable/CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES
    /variable/CMAKE_TRY_COMPILE_PLATFORM_VARIABLES
    /variable/CMAKE_TRY_COMPILE_PLATFORM_VARIABLES
    /variable/CMAKE_TRY_COMPILE_TARGET_TYPE
    /variable/CMAKE_TRY_COMPILE_TARGET_TYPE
    /variable/CMAKE_UNITY_BUILD
    /variable/CMAKE_UNITY_BUILD

+ 33 - 0
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

+ 10 - 0
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.

+ 10 - 0
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.

+ 9 - 0
Help/variable/CMAKE_TRY_COMPILE_PLATFORM_VARIABLES.rst

@@ -26,3 +26,12 @@ If a user passes ``-DMY_CUSTOM_VARIABLE=SomeValue`` to CMake then this
 setting will be made visible to the toolchain file both for the main
 setting will be made visible to the toolchain file both for the main
 project and for test projects generated by the :command:`try_compile`
 project and for test projects generated by the :command:`try_compile`
 command source file signature.
 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.

+ 4 - 1
Source/cmCoreTryCompile.cxx

@@ -872,7 +872,10 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
   }
   }
 
 
   // Forward a set of variables to the inner project cache.
   // 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;
     std::set<std::string> vars;
     vars.insert(&c_properties[lang_property_start],
     vars.insert(&c_properties[lang_property_start],
                 &c_properties[lang_property_start + lang_property_size]);
                 &c_properties[lang_property_start + lang_property_size]);

+ 4 - 1
Source/cmPolicies.h

@@ -411,7 +411,10 @@ class cmMakefile;
          3, 24, 0, cmPolicies::WARN)                                          \
          3, 24, 0, cmPolicies::WARN)                                          \
   SELECT(POLICY, CMP0136,                                                     \
   SELECT(POLICY, CMP0136,                                                     \
          "Watcom runtime library flags are selected by an abstraction.", 3,   \
          "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_SELECT_ID(F, A1, A2, A3, A4, A5, A6) F(A1)
 #define CM_FOR_EACH_POLICY_ID(POLICY)                                         \
 #define CM_FOR_EACH_POLICY_ID(POLICY)                                         \

+ 16 - 0
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)

+ 5 - 0
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=''

+ 2 - 0
Tests/RunCMake/try_compile/CMP0137-NEW.cmake

@@ -0,0 +1,2 @@
+cmake_policy(SET CMP0137 NEW)
+include(CMP0137-Common.cmake)

+ 5 - 0
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=''

+ 2 - 0
Tests/RunCMake/try_compile/CMP0137-WARN.cmake

@@ -0,0 +1,2 @@
+# policy CMP0137 not set
+include(CMP0137-Common.cmake)

+ 6 - 0
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}'\")
+")

+ 2 - 0
Tests/RunCMake/try_compile/RunCMakeTest.cmake

@@ -72,6 +72,8 @@ endif()
 run_cmake(CMP0056)
 run_cmake(CMP0056)
 run_cmake(CMP0066)
 run_cmake(CMP0066)
 run_cmake(CMP0067)
 run_cmake(CMP0067)
+run_cmake(CMP0137-WARN)
+run_cmake(CMP0137-NEW)
 
 
 if(RunCMake_GENERATOR MATCHES "Make|Ninja")
 if(RunCMake_GENERATOR MATCHES "Make|Ninja")
   # Use a single build tree for a few tests without cleaning.
   # Use a single build tree for a few tests without cleaning.