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
This commit is contained in:
Brad King 2022-05-16 10:16:34 -04:00
parent 4843a37676
commit a6562ff579
10 changed files with 45 additions and 3 deletions

View File

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

View File

@ -502,6 +502,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

View File

@ -21,6 +21,10 @@ the :ref:`whole-project <Try Compiling Whole Projects>` signature.
The ``NEW`` behavior for this policy is to pass the same variables that the The ``NEW`` behavior for this policy is to pass the same variables that the
:ref:`source file <Try Compiling Source Files>` signature does. :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 This policy was introduced in CMake version 3.24. Use the
:command:`cmake_policy` command to set this policy to ``OLD`` or ``NEW`` :command:`cmake_policy` command to set this policy to ``OLD`` or ``NEW``
explicitly. Unlike many policies, CMake version |release| does *not* warn explicitly. Unlike many policies, CMake version |release| does *not* warn

View File

@ -4,3 +4,7 @@ try_compile-project-platform-vars
* The :command:`try_compile` command * The :command:`try_compile` command
:ref:`whole-project <Try Compiling Whole Projects>` signature :ref:`whole-project <Try Compiling Whole Projects>` signature
now propagates platform variables. See policy :policy:`CMP0137`. 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.

View File

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

View File

@ -31,3 +31,7 @@ command source file signature.
Listed variables are propagated to the :command:`try_compile` Listed variables are propagated to the :command:`try_compile`
:ref:`whole-project <Try Compiling Whole Projects>` signature too. :ref:`whole-project <Try Compiling Whole Projects>` signature too.
See :policy:`CMP0137`. 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.

View File

@ -872,9 +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) == this->Makefile->GetPolicyStatus(cmPolicies::CMP0137) ==
cmPolicies::NEW) { 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]);

View File

@ -6,3 +6,11 @@ try_compile(RESULT_VAR
${CMAKE_CURRENT_SOURCE_DIR}/CMP0137 ${CMAKE_CURRENT_SOURCE_DIR}/CMP0137
TestCMP0137) TestCMP0137)
include(${CMAKE_CURRENT_BINARY_DIR}/CMP0137-build/info.cmake OPTIONAL) 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)

View File

@ -1,2 +1,5 @@
-- CMAKE_ENABLE_EXPORTS='1' -- CMAKE_ENABLE_EXPORTS='1'
-- FOO='2' -- FOO='2'
-- Enabling CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES
-- CMAKE_ENABLE_EXPORTS=''
-- FOO=''

View File

@ -1,2 +1,5 @@
-- CMAKE_ENABLE_EXPORTS='' -- CMAKE_ENABLE_EXPORTS=''
-- FOO='' -- FOO=''
-- Enabling CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES
-- CMAKE_ENABLE_EXPORTS=''
-- FOO=''