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 <Alexander.Neumann@hamburg.de>
Fixes: #23219
This commit is contained in:
Brad King
2022-05-16 10:04:48 -04:00
parent 364fde65ee
commit 4843a37676
14 changed files with 83 additions and 2 deletions

View File

@@ -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 ``<targetName>`` 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 <Try Compiling Source Files>` signature.
.. _`Try Compiling Source Files`:
Try Compiling Source Files
^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@@ -58,6 +58,7 @@ Policies Introduced by CMake 3.24
.. 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>

29
Help/policy/CMP0137.rst Normal file
View File

@@ -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 <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.
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

View File

@@ -0,0 +1,6 @@
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`.

View File

@@ -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 <Try Compiling Whole Projects>` signature too.
See :policy:`CMP0137`.

View File

@@ -872,7 +872,9 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> 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<std::string> vars;
vars.insert(&c_properties[lang_property_start],
&c_properties[lang_property_start + lang_property_size]);

View File

@@ -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) \

View File

@@ -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)

View File

@@ -0,0 +1,2 @@
-- CMAKE_ENABLE_EXPORTS='1'
-- FOO='2'

View File

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

View File

@@ -0,0 +1,2 @@
-- CMAKE_ENABLE_EXPORTS=''
-- FOO=''

View File

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

View File

@@ -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}'\")
")

View File

@@ -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.