From 4843a376767c2c4b46e29ed85ac823443317ca40 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 16 May 2022 10:04:48 -0400 Subject: try_compile: Propagate platform variables in project-mode too Add policy CMP0137 to propagate both our builtin variables and those listed by `CMAKE_TRY_COMPILE_PLATFORM_VARIABLES` to `try_compile` whole-project builds. Inspired-by: Alexander Neumann Fixes: #23219 --- Help/command/try_compile.rst | 11 ++++++++ Help/manual/cmake-policies.7.rst | 1 + Help/policy/CMP0137.rst | 29 ++++++++++++++++++++++ .../dev/try_compile-project-platform-vars.rst | 6 +++++ .../CMAKE_TRY_COMPILE_PLATFORM_VARIABLES.rst | 5 ++++ Source/cmCoreTryCompile.cxx | 4 ++- Source/cmPolicies.h | 5 +++- Tests/RunCMake/try_compile/CMP0137-Common.cmake | 8 ++++++ Tests/RunCMake/try_compile/CMP0137-NEW-stdout.txt | 2 ++ Tests/RunCMake/try_compile/CMP0137-NEW.cmake | 2 ++ Tests/RunCMake/try_compile/CMP0137-WARN-stdout.txt | 2 ++ Tests/RunCMake/try_compile/CMP0137-WARN.cmake | 2 ++ Tests/RunCMake/try_compile/CMP0137/CMakeLists.txt | 6 +++++ Tests/RunCMake/try_compile/RunCMakeTest.cmake | 2 ++ 14 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 Help/policy/CMP0137.rst create mode 100644 Help/release/dev/try_compile-project-platform-vars.rst create mode 100644 Tests/RunCMake/try_compile/CMP0137-Common.cmake create mode 100644 Tests/RunCMake/try_compile/CMP0137-NEW-stdout.txt create mode 100644 Tests/RunCMake/try_compile/CMP0137-NEW.cmake create mode 100644 Tests/RunCMake/try_compile/CMP0137-WARN-stdout.txt create mode 100644 Tests/RunCMake/try_compile/CMP0137-WARN.cmake create mode 100644 Tests/RunCMake/try_compile/CMP0137/CMakeLists.txt diff --git a/Help/command/try_compile.rst b/Help/command/try_compile.rst index 4b2a631..06b2362 100644 --- a/Help/command/try_compile.rst +++ b/Help/command/try_compile.rst @@ -7,6 +7,8 @@ try_compile Try building some code. +.. _`Try Compiling Whole Projects`: + Try Compiling Whole Projects ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -25,6 +27,15 @@ will not be deleted after this command is run. Specify ```` to 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 ` signature. + +.. _`Try Compiling Source Files`: + Try Compiling Source Files ^^^^^^^^^^^^^^^^^^^^^^^^^^ 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 @@ Policies Introduced by CMake 3.24 .. toctree:: :maxdepth: 1 + CMP0137: try_compile() passes platform variables in project mode. CMP0136: Watcom runtime library flags are selected by an abstraction. CMP0135: ExternalProject ignores timestamps in archives by default for the URL download method. CMP0134: Fallback to \"HOST\" Windows registry view when \"TARGET\" view is not usable. diff --git a/Help/policy/CMP0137.rst b/Help/policy/CMP0137.rst new file mode 100644 index 0000000..2efb906 --- /dev/null +++ b/Help/policy/CMP0137.rst @@ -0,0 +1,29 @@ +CMP0137 +------- + +.. versionadded:: 3.24 + +:command:`try_compile` passes platform variables in project mode. + +The :command:`try_compile` command :ref:`source file ` 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 ` signature does not +propagate platform variables automatically. CMake 3.24 and above prefer to +propagate platform variables in the :ref:`whole-project ` 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 ` signature. +The ``NEW`` behavior for this policy is to pass the same variables that the +:ref:`source file ` signature does. + +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..2f409a3 --- /dev/null +++ b/Help/release/dev/try_compile-project-platform-vars.rst @@ -0,0 +1,6 @@ +try_compile-project-platform-vars +--------------------------------- + +* The :command:`try_compile` command + :ref:`whole-project ` signature + now propagates platform variables. See policy :policy:`CMP0137`. diff --git a/Help/variable/CMAKE_TRY_COMPILE_PLATFORM_VARIABLES.rst b/Help/variable/CMAKE_TRY_COMPILE_PLATFORM_VARIABLES.rst index d178513..5e84fd0 100644 --- a/Help/variable/CMAKE_TRY_COMPILE_PLATFORM_VARIABLES.rst +++ b/Help/variable/CMAKE_TRY_COMPILE_PLATFORM_VARIABLES.rst @@ -26,3 +26,8 @@ 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 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 ` signature too. + See :policy:`CMP0137`. diff --git a/Source/cmCoreTryCompile.cxx b/Source/cmCoreTryCompile.cxx index 4909948..39c2b3a 100644 --- a/Source/cmCoreTryCompile.cxx +++ b/Source/cmCoreTryCompile.cxx @@ -872,7 +872,9 @@ int cmCoreTryCompile::TryCompileCode(std::vector const& argv, } // Forward a set of variables to the inner project cache. - if (this->SrcFileSignature) { + if (this->SrcFileSignature || + this->Makefile->GetPolicyStatus(cmPolicies::CMP0137) == + cmPolicies::NEW) { std::set 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 @@ class cmMakefile; 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..867475d --- /dev/null +++ b/Tests/RunCMake/try_compile/CMP0137-Common.cmake @@ -0,0 +1,8 @@ +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) 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..0a6bb38 --- /dev/null +++ b/Tests/RunCMake/try_compile/CMP0137-NEW-stdout.txt @@ -0,0 +1,2 @@ +-- CMAKE_ENABLE_EXPORTS='1' +-- FOO='2' 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..fda1e41 --- /dev/null +++ b/Tests/RunCMake/try_compile/CMP0137-WARN-stdout.txt @@ -0,0 +1,2 @@ +-- 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 @@ endif() 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. -- cgit v0.12 From a6562ff57908e6f03ef44eb0438ba9937f28174c Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 16 May 2022 10:16:34 -0400 Subject: try_compile: Add option to skip passing platform variables Add a `CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES` variable to tell `try_compile` not to pass platform variables to the test project. Issue: #23219 --- Help/command/try_compile.rst | 4 ++++ Help/manual/cmake-variables.7.rst | 1 + Help/policy/CMP0137.rst | 4 ++++ Help/release/dev/try_compile-project-platform-vars.rst | 4 ++++ Help/variable/CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES.rst | 10 ++++++++++ Help/variable/CMAKE_TRY_COMPILE_PLATFORM_VARIABLES.rst | 4 ++++ Source/cmCoreTryCompile.cxx | 7 ++++--- Tests/RunCMake/try_compile/CMP0137-Common.cmake | 8 ++++++++ Tests/RunCMake/try_compile/CMP0137-NEW-stdout.txt | 3 +++ Tests/RunCMake/try_compile/CMP0137-WARN-stdout.txt | 3 +++ 10 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 Help/variable/CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES.rst diff --git a/Help/command/try_compile.rst b/Help/command/try_compile.rst index 06b2362..806a98d 100644 --- a/Help/command/try_compile.rst +++ b/Help/command/try_compile.rst @@ -216,3 +216,7 @@ a build configuration. .. 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-variables.7.rst b/Help/manual/cmake-variables.7.rst index 80160b6..41f2f30 100644 --- a/Help/manual/cmake-variables.7.rst +++ b/Help/manual/cmake-variables.7.rst @@ -502,6 +502,7 @@ Variables that Control the Build /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 index 2efb906..ba3cb9c 100644 --- a/Help/policy/CMP0137.rst +++ b/Help/policy/CMP0137.rst @@ -21,6 +21,10 @@ the :ref:`whole-project ` signature. The ``NEW`` behavior for this policy is to pass the same variables that the :ref:`source file ` 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 diff --git a/Help/release/dev/try_compile-project-platform-vars.rst b/Help/release/dev/try_compile-project-platform-vars.rst index 2f409a3..06596d4 100644 --- a/Help/release/dev/try_compile-project-platform-vars.rst +++ b/Help/release/dev/try_compile-project-platform-vars.rst @@ -4,3 +4,7 @@ try_compile-project-platform-vars * The :command:`try_compile` command :ref:`whole-project ` 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 5e84fd0..6b26d14 100644 --- a/Help/variable/CMAKE_TRY_COMPILE_PLATFORM_VARIABLES.rst +++ b/Help/variable/CMAKE_TRY_COMPILE_PLATFORM_VARIABLES.rst @@ -31,3 +31,7 @@ command source file signature. Listed variables are propagated to the :command:`try_compile` :ref:`whole-project ` 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 39c2b3a..caa413b 100644 --- a/Source/cmCoreTryCompile.cxx +++ b/Source/cmCoreTryCompile.cxx @@ -872,9 +872,10 @@ int cmCoreTryCompile::TryCompileCode(std::vector const& argv, } // Forward a set of variables to the inner project cache. - if (this->SrcFileSignature || - this->Makefile->GetPolicyStatus(cmPolicies::CMP0137) == - cmPolicies::NEW) { + if ((this->SrcFileSignature || + this->Makefile->GetPolicyStatus(cmPolicies::CMP0137) == + cmPolicies::NEW) && + !this->Makefile->IsOn("CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES")) { std::set vars; vars.insert(&c_properties[lang_property_start], &c_properties[lang_property_start + lang_property_size]); diff --git a/Tests/RunCMake/try_compile/CMP0137-Common.cmake b/Tests/RunCMake/try_compile/CMP0137-Common.cmake index 867475d..2c1bc0d 100644 --- a/Tests/RunCMake/try_compile/CMP0137-Common.cmake +++ b/Tests/RunCMake/try_compile/CMP0137-Common.cmake @@ -6,3 +6,11 @@ try_compile(RESULT_VAR ${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 index 0a6bb38..abc61dc 100644 --- a/Tests/RunCMake/try_compile/CMP0137-NEW-stdout.txt +++ b/Tests/RunCMake/try_compile/CMP0137-NEW-stdout.txt @@ -1,2 +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-WARN-stdout.txt b/Tests/RunCMake/try_compile/CMP0137-WARN-stdout.txt index fda1e41..2e4bebe 100644 --- a/Tests/RunCMake/try_compile/CMP0137-WARN-stdout.txt +++ b/Tests/RunCMake/try_compile/CMP0137-WARN-stdout.txt @@ -1,2 +1,5 @@ -- CMAKE_ENABLE_EXPORTS='' -- FOO='' +-- Enabling CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES +-- CMAKE_ENABLE_EXPORTS='' +-- FOO='' -- cgit v0.12