try_compile: Restore platform-default link flags in pre-CMP0210 projects

The CMP0210 compatibility shims in the Darwin, AIX, HP-UX and ARMClang
modules read the policy with cmake_policy(GET CMP0210 ...) during
enable_language(), which runs inside project().  cmCoreTryCompile emitted
cmake_policy(SET CMP0210 ...) after project() in the generated test
project, so those shims always observed NEW regardless of the calling
project's setting.

On affected platforms this dropped the platform default link flags from
every try_compile, and left a toolchain-file value of
CMAKE_<LANG>_LINK_FLAGS unclobbered so that it reached the OLD code path.
For projects that are also not using CMP0181 NEW, that path uses the
variable as is and does not expand the LINKER: prefix.

Move the emission into the existing pre-project() policy block alongside
CMP0126, CMP0128 and CMP0197, which exist for exactly this reason.

Fixes: #28102
This commit is contained in:
Mickaël Germain
2026-09-21 11:35:38 -04:00
committed by Brad King
parent 4d4a0f16bb
commit 92320466dd
12 changed files with 27 additions and 9 deletions
+7 -9
View File
@@ -692,6 +692,13 @@ cm::optional<cmTryCompileResult> cmCoreTryCompile::TryCompileCode(
*cmp0197 == "NEW"_s ? "NEW" : "OLD");
}
/* Set per-language link flags policy to match outer project.
It affects platform modules. */
if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0210) !=
cmPolicies::NEW) {
fprintf(fout, "cmake_policy(SET CMP0210 OLD)\n");
}
std::string projectLangs;
for (std::string const& li : testLangs) {
projectLangs += cmStrCat(' ', li);
@@ -912,15 +919,6 @@ cm::optional<cmTryCompileResult> cmCoreTryCompile::TryCompileCode(
? "NEW"
: "OLD");
/* Set the appropriate policy information for passing
* CMAKE_<LANG>_LINK_FLAGS
*/
fprintf(fout, "cmake_policy(SET CMP0210 %s)\n",
this->Makefile->GetPolicyStatus(cmPolicies::CMP0210) ==
cmPolicies::NEW
? "NEW"
: "OLD");
// Workaround for -Wl,-headerpad_max_install_names issue until we can
// avoid adding that flag in the platform and compiler language files
fprintf(fout,
@@ -0,0 +1 @@
^try_compile CMP0210='NEW'
@@ -0,0 +1 @@
include("${CMAKE_CURRENT_LIST_DIR}/CMP0210-toolchain.cmake")
@@ -0,0 +1,2 @@
cmake_policy(SET CMP0210 NEW)
enable_language(C)
@@ -0,0 +1 @@
^try_compile CMP0210='OLD'
@@ -0,0 +1 @@
include("${CMAKE_CURRENT_LIST_DIR}/CMP0210-toolchain.cmake")
@@ -0,0 +1,2 @@
cmake_policy(SET CMP0210 OLD)
enable_language(C)
@@ -0,0 +1 @@
^try_compile CMP0210='OLD'
@@ -0,0 +1 @@
include("${CMAKE_CURRENT_LIST_DIR}/CMP0210-toolchain.cmake")
@@ -0,0 +1,2 @@
# leave CMP0210 unset
enable_language(C)
@@ -0,0 +1,5 @@
get_property(_IN_TC GLOBAL PROPERTY IN_TRY_COMPILE)
if(_IN_TC)
cmake_policy(GET CMP0210 cmp0210)
message("try_compile CMP0210='${cmp0210}'")
endif()
@@ -14,6 +14,9 @@ run_cmake_toolchain(LinkFlagsInit)
run_cmake_toolchain(CMP0126-NEW)
run_cmake_toolchain(CMP0126-OLD)
run_cmake_toolchain(CMP0126-WARN)
run_cmake_toolchain(CMP0210-NEW)
run_cmake_toolchain(CMP0210-OLD)
run_cmake_toolchain(CMP0210-WARN)
run_cmake_toolchain(SetCrossCompiling)
function(run_IncludeDirectories)