cmake: Add per-language link flags for all target types

Previously, `CMAKE_<LANG>_LINK_FLAGS` was an undocumented variable used
for linking executables only. Re-spell that variable mirroring the
existing spellings for shared and module libraries, and add policy
CMP0210 to preserve compatibility.

Then, repurpose `CMAKE_<LANG>_LINK_FLAGS` to provide a variable to be
used for per-language link flags for all target types, along with a
per-configuration variant. These are added to the `<LINK_FLAGS>` rule
placeholder in the generators.

Fixes: #21934
Relates: #25620

Co-authored-by: Brad King <brad.king@kitware.com>
This commit is contained in:
Tyler Yankee
2026-01-31 19:37:09 -05:00
co-authored by Brad King
parent 63858fc3e0
commit 98f9874703
78 changed files with 508 additions and 13 deletions
+8
View File
@@ -324,6 +324,8 @@ Other Behavior Settings
* :variable:`CMAKE_SHARED_LIBRARY_ENABLE_EXPORTS`
* :variable:`CMAKE_EXE_LINKER_FLAGS`, unless using CMake versions
prior to 4.0 without policy :policy:`CMP0056` set to ``NEW``
* :variable:`CMAKE_<LANG>_LINK_FLAGS`, unless using CMake versions
prior to 4.3 without policy :policy:`CMP0210` set to ``NEW``
* :variable:`CMAKE_LINK_SEARCH_START_STATIC`
* :variable:`CMAKE_LINK_SEARCH_END_STATIC`
* :variable:`CMAKE_MSVC_RUNTIME_LIBRARY`
@@ -352,6 +354,7 @@ as needed to honor the state of the calling project:
* :policy:`CMP0157`
* :policy:`CMP0181`
* :policy:`CMP0184`
* :policy:`CMP0210`
Set variable :variable:`CMAKE_TRY_COMPILE_CONFIGURATION` to choose a build
configuration:
@@ -421,6 +424,11 @@ configuration:
:variable:`CMAKE_MSVC_RUNTIME_CHECKS` to specify the enabled MSVC runtime
checks.
.. versionadded:: 4.3
If :policy:`CMP0210` is set to ``NEW``, :variable:`CMAKE_<LANG>_LINK_FLAGS`
and :variable:`CMAKE_<LANG>_LINK_FLAGS_<CONFIG>` are propagated into the
test project's build configuration.
See Also
^^^^^^^^
+1
View File
@@ -100,6 +100,7 @@ Policies Introduced by CMake 4.3
.. toctree::
:maxdepth: 1
CMP0210: CMAKE_<LANG>_LINK_FLAGS adds link flags to all target types. </policy/CMP0210>
CMP0209: Verify interface header sets checks executables without exports. </policy/CMP0209>
CMP0208: export(EXPORT) does not allow empty arguments. </policy/CMP0208>
CMP0207: file(GET_RUNTIME_DEPENDENCIES) normalizes paths before matching. </policy/CMP0207>
+2
View File
@@ -97,6 +97,8 @@ Variables that Provide Information
/variable/CMAKE_LANG_COMPILER_LINKER_VERSION
/variable/CMAKE_LANG_COMPILER_RANLIB
/variable/CMAKE_LANG_DEVICE_LINK_MODE
/variable/CMAKE_LANG_LINK_FLAGS
/variable/CMAKE_LANG_LINK_FLAGS_CONFIG
/variable/CMAKE_LANG_LINK_LIBRARY_SUFFIX
/variable/CMAKE_LANG_LINK_MODE
/variable/CMAKE_LINK_LIBRARY_SUFFIX
+30
View File
@@ -0,0 +1,30 @@
CMP0210
-------
.. versionadded:: 4.3
:variable:`CMAKE_<LANG>_LINK_FLAGS` adds link flags to all target types.
In CMake 4.2 and below, :variable:`CMAKE_<LANG>_LINK_FLAGS` held flags
relevant to target creation and applied when linking executables only. It was
undocumented and originally intended for internal use only, but still
available for projects and users to set in order to control linking behavior.
CMake 4.3 and above repurpose this variable to add per-language link flags for
all target types that link (executables, shared libraries, and module
libraries).
This policy provides compatibility with projects that relied on this variable
and have not been updated to be aware of the change.
The ``OLD`` behavior for this policy is to apply
:variable:`CMAKE_<LANG>_LINK_FLAGS` to invocations of the compiler which drive
linking for executables only. The ``NEW`` behavior is to apply the flags to
all target types.
.. |INTRODUCED_IN_CMAKE_VERSION| replace:: 4.3
.. |WARNS_OR_DOES_NOT_WARN| replace::
warns when :variable:`CMAKE_<LANG>_LINK_FLAGS` is set
.. include:: include/STANDARD_ADVICE.rst
.. include:: include/DEPRECATED.rst
@@ -0,0 +1,6 @@
CMAKE_<LANG>_LINK_FLAGS
-----------------------
* The variables :variable:`CMAKE_<LANG>_LINK_FLAGS` and
:variable:`CMAKE_<LANG>_LINK_FLAGS_<CONFIG>` were added to support
per-language link flags for all target types. See policy :policy:`CMP0210`.
+38
View File
@@ -0,0 +1,38 @@
CMAKE_<LANG>_LINK_FLAGS
-----------------------
.. versionadded:: 4.3
Language-wide flags for language ``<LANG>`` used when linking for all
configurations. These flags will be passed to all invocations of the compiler
which drive linking.
The flags in this variable will obey the following behavior with respect to
ordering of flags from other variables.
* They will be passed after those added by :variable:`CMAKE_<LANG>_FLAGS` and
:variable:`CMAKE_<LANG>_FLAGS_<CONFIG>`.
* They will be passed after those added by :variable:`CMAKE_EXE_LINKER_FLAGS`,
:variable:`CMAKE_EXE_LINKER_FLAGS_<CONFIG>`,
:variable:`CMAKE_SHARED_LINKER_FLAGS`,
:variable:`CMAKE_SHARED_LINKER_FLAGS_<CONFIG>`,
:variable:`CMAKE_MODULE_LINKER_FLAGS`,
and :variable:`CMAKE_MODULE_LINKER_FLAGS_<CONFIG>` depending on the given
target type.
* They will be passed before those added by
:variable:`CMAKE_<LANG>_LINK_FLAGS_<CONFIG>`.
* They will be passed before those added by commands such
as :command:`add_link_options` and :command:`target_link_options`.
Use of this variable is enabled when policy :policy:`CMP0210` is ``NEW``.
.. include:: ../command/include/LINK_LIBRARIES_LINKER.rst
This support implies to parse and re-quote the content of the variable.
See Also
^^^^^^^^
* :variable:`CMAKE_<LANG>_LINK_FLAGS_<CONFIG>`
* :variable:`CMAKE_<LANG>_FLAGS`
* :policy:`CMP0210`
@@ -0,0 +1,23 @@
CMAKE_<LANG>_LINK_FLAGS_<CONFIG>
--------------------------------
.. versionadded:: 4.3
Language-wide flags for language ``<LANG>`` used when linking for the
``<CONFIG>`` configuration. These flags will be passed to all invocations of
the compiler which drive linking.
See :variable:`CMAKE_<LANG>_LINK_FLAGS` for the ordering of these flags with
respect to other variables. Notably, flags in
``CMAKE_<LANG>_LINK_FLAGS_<CONFIG>`` are passed after those in
:variable:`CMAKE_<LANG>_LINK_FLAGS`.
.. include:: ../command/include/LINK_LIBRARIES_LINKER.rst
This support implies to parse and re-quote the content of the variable.
See Also
^^^^^^^^
* :variable:`CMAKE_<LANG>_LINK_FLAGS`
* :variable:`CMAKE_<LANG>_FLAGS_<CONFIG>`
+6 -1
View File
@@ -121,7 +121,12 @@ macro(__compiler_armclang lang)
list(TRANSFORM CMAKE_LINKER_CPU_LIST TOLOWER)
__armclang_check_processor("${CMAKE_SYSTEM_PROCESSOR}" "${CMAKE_LINKER_CPU_LIST}" _CMAKE_CHECK_LINK_CPU_RESULT)
if(_CMAKE_CHECK_LINK_CPU_RESULT)
string(APPEND CMAKE_${lang}_LINK_FLAGS " --cpu=${CMAKE_SYSTEM_PROCESSOR}")
string(APPEND CMAKE_EXECUTABLE_CREATE_${lang}_FLAGS " --cpu=${CMAKE_SYSTEM_PROCESSOR}")
cmake_policy(GET CMP0210 _CMP0210)
if (NOT _CMP0210 STREQUAL "NEW")
string(APPEND CMAKE_${lang}_LINK_FLAGS " --cpu=${CMAKE_SYSTEM_PROCESSOR}")
endif()
unset(_CMP0210)
endif()
endif()
@@ -48,6 +48,7 @@ function(__cmake_internal_workaround_headerpad_flag_conflict _LANG)
if(remove_headerpad)
foreach(flag IN ITEMS
CMAKE_${_LANG}_LINK_FLAGS
CMAKE_EXECUTABLE_CREATE_${_LANG}_FLAGS
CMAKE_SHARED_LIBRARY_CREATE_${_LANG}_FLAGS
CMAKE_SHARED_MODULE_CREATE_${_LANG}_FLAGS)
string(REPLACE "-Wl,-headerpad_max_install_names" "" ${flag} "${${flag}}")
+6 -1
View File
@@ -16,7 +16,12 @@ macro(__aix_compiler_gnu lang)
set(CMAKE_${lang}_USE_IMPLICIT_LINK_DIRECTORIES_IN_RUNTIME_PATH 1)
set(CMAKE_${lang}_VERBOSE_LINK_FLAG "-Wl,-v")
set(CMAKE_${lang}_LINK_FLAGS "-Wl,-bnoipath")
set(CMAKE_EXECUTABLE_CREATE_${lang}_FLAGS "-Wl,-bnoipath")
cmake_policy(GET CMP0210 _CMP0210)
if (NOT _CMP0210 STREQUAL "NEW")
set(CMAKE_${lang}_LINK_FLAGS "${CMAKE_EXECUTABLE_CREATE_${lang}_FLAGS}")
endif()
unset(_CMP0210)
if(CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 7 OR CMAKE_SYSTEM_VERSION VERSION_LESS 7.1)
unset(CMAKE_${lang}_COMPILE_OPTIONS_VISIBILITY)
+6 -1
View File
@@ -16,7 +16,12 @@ macro(__aix_compiler_xl lang)
set(CMAKE_SHARED_LIBRARY_${lang}_FLAGS " ")
set(CMAKE_SHARED_MODULE_${lang}_FLAGS " ")
set(CMAKE_${lang}_LINK_FLAGS "-Wl,-bnoipath")
set(CMAKE_EXECUTABLE_CREATE_${lang}_FLAGS "-Wl,-bnoipath")
cmake_policy(GET CMP0210 _CMP0210)
if (NOT _CMP0210 STREQUAL "NEW")
set(CMAKE_${lang}_LINK_FLAGS "${CMAKE_EXECUTABLE_CREATE_${lang}_FLAGS}")
endif()
unset(_CMP0210)
set(_OBJECTS " <OBJECTS>")
if(DEFINED CMAKE_XL_CreateExportList AND CMAKE_XL_CreateExportList STREQUAL "")
+7 -1
View File
@@ -39,15 +39,21 @@ if(NOT (DEFINED _CMAKE_HOST_OSX_VERSION AND _CMAKE_HOST_OSX_VERSION VERSION_LESS
set(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG "-Wl,-rpath,")
endif()
cmake_policy(GET CMP0210 _CMP0210)
foreach(lang C CXX OBJC OBJCXX)
set(CMAKE_${lang}_OSX_COMPATIBILITY_VERSION_FLAG "-compatibility_version ")
set(CMAKE_${lang}_OSX_CURRENT_VERSION_FLAG "-current_version ")
set(CMAKE_${lang}_LINK_FLAGS "-Wl,-search_paths_first -Wl,-headerpad_max_install_names")
set(CMAKE_EXECUTABLE_CREATE_${lang}_FLAGS "-Wl,-search_paths_first -Wl,-headerpad_max_install_names")
if (NOT _CMP0210 STREQUAL "NEW")
set(CMAKE_${lang}_LINK_FLAGS "${CMAKE_EXECUTABLE_CREATE_${lang}_FLAGS}")
endif()
set(CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS "-dynamiclib -Wl,-headerpad_max_install_names")
set(CMAKE_SHARED_MODULE_CREATE_${lang}_FLAGS "-bundle -Wl,-headerpad_max_install_names")
set(CMAKE_SHARED_MODULE_LOADER_${lang}_FLAG "-Wl,-bundle_loader,")
endforeach()
unset(_CMP0210)
set(CMAKE_PLATFORM_HAS_INSTALLNAME 1)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".tbd" ".dylib" ".so" ".a")
+7 -1
View File
@@ -15,6 +15,12 @@ macro(__hpux_compiler_gnu lang)
set(CMAKE_SHARED_LIBRARY_RUNTIME_${lang}_FLAG_SEP ":")
set(CMAKE_SHARED_LIBRARY_SONAME_${lang}_FLAG "-Wl,+h")
set(CMAKE_${lang}_LINK_FLAGS "-Wl,+s,+nodefaultrpath")
set(CMAKE_EXECUTABLE_CREATE_${lang}_FLAGS "-Wl,+s,+nodefaultrpath")
cmake_policy(GET CMP0210 _CMP0210)
if (NOT _CMP0210 STREQUAL "NEW")
set(CMAKE_${lang}_LINK_FLAGS "${CMAKE_EXECUTABLE_CREATE_${lang}_FLAGS}")
endif()
unset(_CMP0210)
unset(CMAKE_${lang}_COMPILE_OPTIONS_VISIBILITY)
endmacro()
+6 -1
View File
@@ -19,5 +19,10 @@ macro(__hpux_compiler_hp lang)
string(APPEND CMAKE_${lang}_FLAGS_INIT " ")
set(CMAKE_${lang}_LINK_FLAGS "-Wl,+s,+nodefaultrpath")
set(CMAKE_EXECUTABLE_CREATE_${lang}_FLAGS "-Wl,+s,+nodefaultrpath")
cmake_policy(GET CMP0210 _CMP0210)
if (NOT _CMP0210 STREQUAL "NEW")
set(CMAKE_${lang}_LINK_FLAGS "${CMAKE_EXECUTABLE_CREATE_${lang}_FLAGS}")
endif()
unset(_CMP0210)
endmacro()
+46
View File
@@ -794,6 +794,43 @@ cm::optional<cmTryCompileResult> cmCoreTryCompile::TryCompileCode(
fprintf(fout,
"set(CMAKE_EXE_LINKER_FLAGS \"${CMAKE_EXE_LINKER_FLAGS}"
" ${EXE_LINKER_FLAGS}\")\n");
switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0210)) {
case cmPolicies::WARN:
// This policy does WARN, but not during try_compile.
CM_FALLTHROUGH;
case cmPolicies::OLD:
// OLD behavior is to do nothing here. CMAKE_<LANG>_LINK_FLAGS was
// previously used internally by executables only, and not during
// try_compile.
break;
case cmPolicies::NEW:
// NEW behavior is to propagate language-specific link flags (stored
// in both the default and per-configuration variables, similar to the
// NEW behavior of CMP0066) to the test project.
for (std::string const& li : testLangs) {
std::string langLinkFlags = cmStrCat("CMAKE_", li, "_LINK_FLAGS");
cmValue flags = this->Makefile->GetDefinition(langLinkFlags);
fprintf(fout, "set(CMAKE_%s_LINK_FLAGS %s)\n", li.c_str(),
cmOutputConverter::EscapeForCMake(*flags).c_str());
std::string langLinkFlagsConfig =
cmStrCat("CMAKE_", li, "_LINK_FLAGS_", tcConfig);
cmValue flagsConfig =
this->Makefile->GetDefinition(langLinkFlagsConfig);
fprintf(fout, "set(CMAKE_%s_LINK_FLAGS_%s %s)\n", li.c_str(),
tcConfig.c_str(),
cmOutputConverter::EscapeForCMake(*flagsConfig).c_str());
if (flags) {
cmakeVariables.emplace(langLinkFlags, *flags);
}
if (flagsConfig) {
cmakeVariables.emplace(langLinkFlagsConfig, *flagsConfig);
}
}
break;
}
fprintf(fout, "include_directories(${INCLUDE_DIRECTORIES})\n");
fprintf(fout, "set(CMAKE_SUPPRESS_REGENERATION 1)\n");
fprintf(fout, "link_directories(${LINK_DIRECTORIES})\n");
@@ -875,6 +912,15 @@ 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,
+2
View File
@@ -2700,6 +2700,8 @@ void cmGlobalXCodeGenerator::CreateBuildSettings(cmGeneratorTarget* gtgt,
} else {
this->CurrentLocalGenerator->AddTargetTypeLinkerFlags(
extraLinkOptions, gtgt, llang, configName);
this->CurrentLocalGenerator->AddPerLanguageLinkFlags(
extraLinkOptions, gtgt, llang, configName);
this->CurrentLocalGenerator->AppendLinkerTypeFlags(extraLinkOptions, gtgt,
configName, llang);
this->CurrentLocalGenerator->AppendWarningAsErrorLinkerFlags(
+75 -1
View File
@@ -1525,6 +1525,13 @@ void cmLocalGenerator::GetTargetFlags(
}
}
std::string langLinkFlags;
this->AddPerLanguageLinkFlags(langLinkFlags, target, linkLanguage,
config);
if (!langLinkFlags.empty()) {
linkFlags.emplace_back(std::move(langLinkFlags));
}
std::string sharedLibFlags;
this->AddTargetPropertyLinkFlags(sharedLibFlags, target, config);
if (!sharedLibFlags.empty()) {
@@ -1553,6 +1560,13 @@ void cmLocalGenerator::GetTargetFlags(
}
}
std::string langLinkFlags;
this->AddPerLanguageLinkFlags(langLinkFlags, target, linkLanguage,
config);
if (!langLinkFlags.empty()) {
linkFlags.emplace_back(std::move(langLinkFlags));
}
{
auto exeType = cmStrCat(
"CMAKE_", linkLanguage, "_CREATE_",
@@ -3342,6 +3356,64 @@ void cmLocalGenerator::AddUnityBuild(cmGeneratorTarget* target)
}
}
void cmLocalGenerator::AddPerLanguageLinkFlags(std::string& flags,
cmGeneratorTarget const* target,
std::string const& lang,
std::string const& config)
{
switch (target->GetType()) {
case cmStateEnums::MODULE_LIBRARY:
case cmStateEnums::SHARED_LIBRARY:
case cmStateEnums::EXECUTABLE:
break;
default:
return;
}
std::string langLinkFlags =
this->Makefile->GetSafeDefinition(cmStrCat("CMAKE_", lang, "_LINK_FLAGS"));
switch (target->GetPolicyStatusCMP0210()) {
case cmPolicies::WARN:
// WARN only when CMAKE_<LANG>_LINK_FLAGS is set, and when the current
// target is not an executable, and CMAKE_<LANG>_LINK_FLAGS is not equal
// to CMAKE_EXECUTABLE_CREATE_<LANG>_FLAGS. This warns users trying to
// use the NEW behavior on old projects (since CMake will be ignoring
// their wishes), while also exempting cases when the latter variable
// (substituted for the former spelling under the NEW behavior) is being
// used legitimately by CMake.
if (!langLinkFlags.empty() &&
target->GetType() != cmStateEnums::EXECUTABLE &&
langLinkFlags !=
this->Makefile->GetSafeDefinition(
cmStrCat("CMAKE_EXECUTABLE_CREATE_", lang, "_FLAGS"))) {
this->IssueMessage(
MessageType::AUTHOR_WARNING,
cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0210), "\n",
"For compatibility with older versions of CMake, ",
"CMAKE_", lang, "_LINK_FLAGS will be ignored for target '",
target->GetName(), "'."));
}
CM_FALLTHROUGH;
case cmPolicies::OLD:
// OLD behavior is to do nothing here, since the use of
// CMAKE_<LANG>_LINK_FLAGS for EXECUTABLEs is handled elsewhere.
break;
case cmPolicies::NEW:
// NEW behavior is to support per-language link flags for all target
// types.
this->AppendLinkFlagsWithParsing(flags, langLinkFlags, target, lang);
if (!config.empty()) {
std::string lankLinkFlagsConfig =
this->Makefile->GetSafeDefinition(cmStrCat(
"CMAKE_", lang, "_LINK_FLAGS_", cmSystemTools::UpperCase(config)));
this->AppendLinkFlagsWithParsing(flags, lankLinkFlagsConfig, target,
lang);
}
break;
}
}
void cmLocalGenerator::AppendTargetCreationLinkFlags(
std::string& flags, cmGeneratorTarget const* target,
std::string const& linkLanguage)
@@ -3365,7 +3437,9 @@ void cmLocalGenerator::AppendTargetCreationLinkFlags(
}
break;
case cmStateEnums::EXECUTABLE:
createFlagsVar = cmStrCat("CMAKE_", linkLanguage, "_LINK_FLAGS");
createFlagsVar = target->GetPolicyStatusCMP0210() == cmPolicies::NEW
? cmStrCat("CMAKE_EXECUTABLE_CREATE_", linkLanguage, "_FLAGS")
: cmStrCat("CMAKE_", linkLanguage, "_LINK_FLAGS");
createFlagsVal = this->Makefile->GetDefinition(createFlagsVar);
break;
default:
+4
View File
@@ -181,6 +181,10 @@ public:
void AddPchDependencies(cmGeneratorTarget* target);
void AddUnityBuild(cmGeneratorTarget* target);
virtual void AddXCConfigSources(cmGeneratorTarget* /* target */) {}
void AddPerLanguageLinkFlags(std::string& flags,
cmGeneratorTarget const* target,
std::string const& lang,
std::string const& config);
void AppendTargetCreationLinkFlags(std::string& flags,
cmGeneratorTarget const* target,
std::string const& linkLanguage);
+2
View File
@@ -969,6 +969,8 @@ void cmLocalVisualStudio7Generator::OutputBuildTool(
std::string extraLinkOptions;
this->AddTargetTypeLinkerFlags(extraLinkOptions, target, linkLanguage,
configName);
this->AddPerLanguageLinkFlags(extraLinkOptions, target, linkLanguage,
configName);
this->AddTargetPropertyLinkFlags(extraLinkOptions, target, configName);
@@ -381,6 +381,8 @@ void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink)
linkFlags, this->GeneratorTarget, linkLanguage);
this->LocalGenerator->AddTargetTypeLinkerFlags(
linkFlags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
this->LocalGenerator->AddPerLanguageLinkFlags(
linkFlags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
{
auto exeType =
@@ -180,6 +180,8 @@ void cmMakefileLibraryTargetGenerator::WriteSharedLibraryRules(bool relink)
extraFlags, this->GeneratorTarget, linkLanguage);
this->LocalGenerator->AddTargetTypeLinkerFlags(
extraFlags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
this->LocalGenerator->AddPerLanguageLinkFlags(
extraFlags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
std::unique_ptr<cmLinkLineComputer> linkLineComputer =
this->CreateLinkLineComputer(
@@ -218,6 +220,8 @@ void cmMakefileLibraryTargetGenerator::WriteModuleLibraryRules(bool relink)
extraFlags, this->GeneratorTarget, linkLanguage);
this->LocalGenerator->AddTargetTypeLinkerFlags(
extraFlags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
this->LocalGenerator->AddPerLanguageLinkFlags(
extraFlags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
std::unique_ptr<cmLinkLineComputer> linkLineComputer =
this->CreateLinkLineComputer(
+6 -2
View File
@@ -626,7 +626,10 @@ class cmMakefile;
4, 3, 0, WARN) \
SELECT(POLICY, CMP0209, \
"Verify interface header sets checks executables without exports.", \
4, 3, 0, WARN)
4, 3, 0, WARN) \
SELECT(POLICY, CMP0210, \
"CMAKE_<LANG>_LINK_FLAGS adds link flags to all target types.", 4, \
3, 0, WARN)
#define CM_SELECT_ID(F, A1, A2, A3, A4, A5, A6) F(A1)
#define CM_FOR_EACH_POLICY_ID(POLICY) \
@@ -680,7 +683,8 @@ class cmMakefile;
F(CMP0202) \
F(CMP0203) \
F(CMP0204) \
F(CMP0209)
F(CMP0209) \
F(CMP0210)
#define CM_FOR_EACH_CUSTOM_COMMAND_POLICY(F) \
F(CMP0116) \
@@ -4545,6 +4545,9 @@ bool cmVisualStudio10TargetGenerator::ComputeLinkOptions(
this->LocalGenerator->AddTargetTypeLinkerFlags(flags, this->GeneratorTarget,
linkLanguage, config);
this->LocalGenerator->AddPerLanguageLinkFlags(flags, this->GeneratorTarget,
linkLanguage, config);
this->LocalGenerator->AddTargetPropertyLinkFlags(
flags, this->GeneratorTarget, config);
+2 -1
View File
@@ -790,6 +790,7 @@ function(add_RunCMake_test_try_compile)
foreach(
var
IN ITEMS
CMAKE_C_COMPILER_ID
CMAKE_SYSTEM_NAME
CMake_TEST_CUDA
CMake_TEST_ISPC
@@ -988,7 +989,7 @@ endif()
add_RunCMake_test(LinkLibrariesProcessing)
add_RunCMake_test(LinkLibrariesStrategy)
add_RunCMake_test(LinkFlags)
add_RunCMake_test(LinkFlags -DCMAKE_C_COMPILER_ID=${CMAKE_C_COMPILER_ID})
add_RunCMake_test(File_Archive -DPython_EXECUTABLE=${Python_EXECUTABLE})
add_RunCMake_test(File_Configure)
add_RunCMake_test(File_Generate)
@@ -0,0 +1 @@
.*BADFLAG_C.*
@@ -0,0 +1 @@
.*BADFLAG_CXX.*
@@ -0,0 +1 @@
.*BADFLAG_C.*
@@ -0,0 +1 @@
.*BADFLAG_CXX.*
@@ -0,0 +1 @@
.*BADFLAG_C.*
@@ -0,0 +1 @@
.*BADFLAG_CXX.*
@@ -0,0 +1,25 @@
cmake_policy(SET CMP0210 NEW)
set(CMAKE_VERBOSE_MAKEFILE TRUE)
if (CMAKE_GENERATOR MATCHES "Borland|NMake")
string(REPLACE "${CMAKE_START_TEMP_FILE}" "" CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_LINK_EXECUTABLE}")
string(REPLACE "${CMAKE_END_TEMP_FILE}" "" CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_LINK_EXECUTABLE}")
string(REPLACE "${CMAKE_START_TEMP_FILE}" "" CMAKE_C_CREATE_SHARED_LIBRARY "${CMAKE_C_CREATE_SHARED_LIBRARY}")
string(REPLACE "${CMAKE_END_TEMP_FILE}" "" CMAKE_C_CREATE_SHARED_LIBRARY "${CMAKE_C_CREATE_SHARED_LIBRARY}")
string(REPLACE "${CMAKE_START_TEMP_FILE}" "" CMAKE_C_CREATE_SHARED_MODULE "${CMAKE_C_CREATE_SHARED_MODULE}")
string(REPLACE "${CMAKE_END_TEMP_FILE}" "" CMAKE_C_CREATE_SHARED_MODULE "${CMAKE_C_CREATE_SHARED_MODULE}")
endif()
set(CMAKE_C_LINK_FLAGS ${pre}BADFLAG_C${obj})
set(CMAKE_CXX_LINK_FLAGS ${pre}BADFLAG_CXX${obj})
add_library(LinkFlags_shared_C SHARED LinkFlagsLib.c)
add_library(LinkFlags_mod_C MODULE LinkFlagsLib.c)
add_executable(LinkFlags_exe_C LinkFlagsExe.c)
add_library(LinkFlags_shared_CXX SHARED LinkFlagsLib.cxx)
add_library(LinkFlags_mod_CXX MODULE LinkFlagsLib.cxx)
add_executable(LinkFlags_exe_CXX LinkFlagsExe.cxx)
@@ -0,0 +1 @@
.*BADFLAG.*
@@ -0,0 +1,7 @@
cmake_policy(SET CMP0210 OLD)
set(CMAKE_C_LINK_FLAGS ${pre}BADFLAG${obj})
add_library(LinkFlags_shared SHARED LinkFlagsLib.c)
add_library(LinkFlags_mod MODULE LinkFlagsLib.c)
add_executable(LinkFlags_exe LinkFlagsExe.c)
@@ -0,0 +1 @@
.*BADFLAG.*
@@ -0,0 +1,8 @@
CMake Warning \(dev\) in CMakeLists\.txt:
Policy CMP0210 is not set: CMAKE_<LANG>_LINK_FLAGS adds link flags to all
target types\. Run "cmake --help-policy CMP0210" for policy details\. Use
the cmake_policy command to set the policy and suppress this warning\.
For compatibility with older versions of CMake, CMAKE_C_LINK_FLAGS will be
ignored for target 'LinkFlags_shared'\.
This warning is for project developers\. Use -Wno-dev to suppress it\.
@@ -0,0 +1,3 @@
set(CMAKE_C_LINK_FLAGS ${pre}BADFLAG${obj})
add_library(LinkFlags_shared SHARED LinkFlagsLib.c)
add_executable(LinkFlags_exe LinkFlagsExe.c)
@@ -0,0 +1 @@
.*BADFLAG_C_RELEASE.*
@@ -0,0 +1 @@
.*BADFLAG_CXX_RELEASE.*
@@ -0,0 +1 @@
.*BADFLAG_C_RELEASE.*
@@ -0,0 +1 @@
.*BADFLAG_CXX_RELEASE.*
@@ -0,0 +1 @@
.*BADFLAG_C_RELEASE.*
@@ -0,0 +1 @@
.*BADFLAG_CXX_RELEASE.*
@@ -0,0 +1,25 @@
cmake_policy(SET CMP0210 NEW)
set(CMAKE_VERBOSE_MAKEFILE TRUE)
if (CMAKE_GENERATOR MATCHES "Borland|NMake")
string(REPLACE "${CMAKE_START_TEMP_FILE}" "" CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_LINK_EXECUTABLE}")
string(REPLACE "${CMAKE_END_TEMP_FILE}" "" CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_LINK_EXECUTABLE}")
string(REPLACE "${CMAKE_START_TEMP_FILE}" "" CMAKE_C_CREATE_SHARED_LIBRARY "${CMAKE_C_CREATE_SHARED_LIBRARY}")
string(REPLACE "${CMAKE_END_TEMP_FILE}" "" CMAKE_C_CREATE_SHARED_LIBRARY "${CMAKE_C_CREATE_SHARED_LIBRARY}")
string(REPLACE "${CMAKE_START_TEMP_FILE}" "" CMAKE_C_CREATE_SHARED_MODULE "${CMAKE_C_CREATE_SHARED_MODULE}")
string(REPLACE "${CMAKE_END_TEMP_FILE}" "" CMAKE_C_CREATE_SHARED_MODULE "${CMAKE_C_CREATE_SHARED_MODULE}")
endif()
set(CMAKE_C_LINK_FLAGS_RELEASE ${pre}BADFLAG_C_RELEASE${obj})
set(CMAKE_CXX_LINK_FLAGS_RELEASE ${pre}BADFLAG_CXX_RELEASE${obj})
add_library(LinkFlags_shared_C SHARED LinkFlagsLib.c)
add_library(LinkFlags_mod_C MODULE LinkFlagsLib.c)
add_executable(LinkFlags_exe_C LinkFlagsExe.c)
add_library(LinkFlags_shared_CXX SHARED LinkFlagsLib.cxx)
add_library(LinkFlags_mod_CXX MODULE LinkFlagsLib.cxx)
add_executable(LinkFlags_exe_CXX LinkFlagsExe.cxx)
+2 -1
View File
@@ -1,7 +1,8 @@
cmake_minimum_required(VERSION 3.10)
project(${RunCMake_TEST} C)
project(${RunCMake_TEST} C CXX)
set(obj "${CMAKE_C_OUTPUT_EXTENSION}")
set(obj_cxx "${CMAKE_CXX_OUTPUT_EXTENSION}")
if(BORLAND)
set(pre -)
endif()
@@ -0,0 +1,4 @@
int main(void)
{
return 0;
}
@@ -0,0 +1,4 @@
int flags_lib(void)
{
return 0;
}
@@ -6,6 +6,7 @@ macro(run_cmake_target test subtest)
run_cmake_command(${test}-${subtest}
${CMAKE_COMMAND} --build .
--target LinkFlags_${subtest}
--verbose
${ARGN}
)
@@ -40,6 +41,34 @@ if (NOT CMAKE_C_COMPILER_ID STREQUAL "Intel")
run_cmake_target(CMAKE_LINKER_FLAGS_CONFIG mod --config Release)
run_cmake_target(CMAKE_LINKER_FLAGS_CONFIG exe --config Release)
run_cmake(CMAKE_LANG_LINK_FLAGS-CMP0210-NEW)
run_cmake_target(CMAKE_LANG_LINK_FLAGS-CMP0210-NEW shared_C)
run_cmake_target(CMAKE_LANG_LINK_FLAGS-CMP0210-NEW mod_C)
run_cmake_target(CMAKE_LANG_LINK_FLAGS-CMP0210-NEW exe_C)
run_cmake_target(CMAKE_LANG_LINK_FLAGS-CMP0210-NEW shared_CXX)
run_cmake_target(CMAKE_LANG_LINK_FLAGS-CMP0210-NEW mod_CXX)
run_cmake_target(CMAKE_LANG_LINK_FLAGS-CMP0210-NEW exe_CXX)
run_cmake(CMAKE_LANG_LINK_FLAGS_CONFIG-CMP0210-NEW)
run_cmake_target(CMAKE_LANG_LINK_FLAGS_CONFIG-CMP0210-NEW shared_C --config Release)
run_cmake_target(CMAKE_LANG_LINK_FLAGS_CONFIG-CMP0210-NEW mod_C --config Release)
run_cmake_target(CMAKE_LANG_LINK_FLAGS_CONFIG-CMP0210-NEW exe_C --config Release)
run_cmake_target(CMAKE_LANG_LINK_FLAGS_CONFIG-CMP0210-NEW shared_CXX --config Release)
run_cmake_target(CMAKE_LANG_LINK_FLAGS_CONFIG-CMP0210-NEW mod_CXX --config Release)
run_cmake_target(CMAKE_LANG_LINK_FLAGS_CONFIG-CMP0210-NEW exe_CXX --config Release)
if (NOT RunCMake_GENERATOR MATCHES "Visual Studio")
# CMP0210's OLD behavior never applied to the Visual Studio generators.
run_cmake(CMAKE_LANG_LINK_FLAGS-CMP0210-OLD)
run_cmake_target(CMAKE_LANG_LINK_FLAGS-CMP0210-OLD shared)
run_cmake_target(CMAKE_LANG_LINK_FLAGS-CMP0210-OLD mod)
run_cmake_target(CMAKE_LANG_LINK_FLAGS-CMP0210-OLD exe)
run_cmake(CMAKE_LANG_LINK_FLAGS-CMP0210-WARN)
run_cmake_target(CMAKE_LANG_LINK_FLAGS-CMP0210-WARN shared)
run_cmake_target(CMAKE_LANG_LINK_FLAGS-CMP0210-WARN exe)
endif()
unset(RunCMake_TEST_OPTIONS)
unset(RunCMake_TEST_OUTPUT_MERGE)
endif()
@@ -53,6 +53,7 @@
\* CMP0203
\* CMP0204
\* CMP0209
\* CMP0210
Call Stack \(most recent call first\):
CMakeLists\.txt:3 \(include\)
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.30...4.0)
cmake_minimum_required(VERSION 3.30)
project(${RunCMake_TEST} LANGUAGES NONE)
@@ -3,6 +3,9 @@ enable_language(C)
cmake_policy(SET CMP0181 ${CMP0181})
# Prefer CMAKE_EXECUTABLE_C_CREATE_FLAGS per policy CMP0210.
cmake_policy(SET CMP0210 NEW)
# ensure command line is always displayed and do not use any response file
set(CMAKE_VERBOSE_MAKEFILE TRUE)
@@ -18,7 +21,7 @@ if (CMAKE_GENERATOR MATCHES "Borland|NMake")
endif()
set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} LINKER:-foo,bar")
set(CMAKE_EXECUTABLE_CREATE_C_FLAGS "${CMAKE_EXECUTABLE_CREATE_C_FLAGS} LINKER:-foo,bar")
add_executable(c_exe_create_link_flags main.c)
set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "${CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS} LINKER:-foo,bar")
@@ -0,0 +1,3 @@
set(reference_file "LINKER.txt")
include ("${CMAKE_CURRENT_LIST_DIR}/LINKER_expansion-validation.cmake")
@@ -0,0 +1,3 @@
set(reference_file "LINKER.txt")
include ("${CMAKE_CURRENT_LIST_DIR}/LINKER_expansion-validation.cmake")
@@ -0,0 +1,3 @@
set(reference_file "LINKER.txt")
include ("${CMAKE_CURRENT_LIST_DIR}/LINKER_expansion-validation.cmake")
@@ -0,0 +1,25 @@
enable_language(C)
cmake_policy(SET CMP0210 NEW)
# ensure command line is always displayed and do not use any response file
set(CMAKE_VERBOSE_MAKEFILE TRUE)
if (CMAKE_GENERATOR MATCHES "Borland|NMake")
string(REPLACE "${CMAKE_START_TEMP_FILE}" "" CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_LINK_EXECUTABLE}")
string(REPLACE "${CMAKE_END_TEMP_FILE}" "" CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_LINK_EXECUTABLE}")
string(REPLACE "${CMAKE_START_TEMP_FILE}" "" CMAKE_C_CREATE_SHARED_LIBRARY "${CMAKE_C_CREATE_SHARED_LIBRARY}")
string(REPLACE "${CMAKE_END_TEMP_FILE}" "" CMAKE_C_CREATE_SHARED_LIBRARY "${CMAKE_C_CREATE_SHARED_LIBRARY}")
string(REPLACE "${CMAKE_START_TEMP_FILE}" "" CMAKE_C_CREATE_SHARED_MODULE "${CMAKE_C_CREATE_SHARED_MODULE}")
string(REPLACE "${CMAKE_END_TEMP_FILE}" "" CMAKE_C_CREATE_SHARED_MODULE "${CMAKE_C_CREATE_SHARED_MODULE}")
endif()
set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} LINKER:-foo,bar")
add_executable (exe_c_link_flags main.c)
add_library(shared_c_link_flags SHARED LinkOptionsLib.c)
add_library(module_c_link_flags MODULE LinkOptionsLib.c)
include(generate_linker_flag_reference.cmake)
@@ -56,6 +56,13 @@ if (RunCMake_GENERATOR MATCHES "Makefiles|Ninja|Xcode|Visual Studio"
endif()
endif()
endforeach()
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/LINKER_expansion5-build)
run_cmake(LINKER_expansion5)
run_cmake_target(LINKER_expansion5 EXE_C_LINK_FLAGS exe_c_link_flags --verbose)
run_cmake_target(LINKER_expansion5 SHARED_C_LINK_FLAGS shared_c_link_flags --verbose)
run_cmake_target(LINKER_expansion5 MODULE_C_LINK_FLAGS module_c_link_flags --verbose)
unset(RunCMake_TEST_BINARY_DIR)
endif()
if(RunCMake_GENERATOR MATCHES "Makefiles|Ninja" AND
@@ -0,0 +1,17 @@
enable_language(C)
set(obj "${CMAKE_C_OUTPUT_EXTENSION}")
if(BORLAND)
set(pre -)
endif()
set(CMAKE_C_LINK_FLAGS ${pre}BADFLAG${obj})
try_compile(RESULT ${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/src.c
OUTPUT_VARIABLE out
)
if (RESULT)
message(STATUS "try_compile output: ${out}")
else()
message(FATAL_ERROR "try_compile output: ${out}")
endif()
@@ -0,0 +1 @@
1
@@ -0,0 +1 @@
.*BADFLAG.*
@@ -0,0 +1,2 @@
cmake_policy(SET CMP0210 NEW)
include(CMP0210-Common.cmake)
@@ -0,0 +1,2 @@
include(CMP0210-Common.cmake)
@@ -67,6 +67,13 @@ run_cmake(CMP0066)
run_cmake(CMP0067)
run_cmake(CMP0137-WARN)
run_cmake(CMP0137-NEW)
run_cmake(CMP0210-WARN)
if(NOT CMAKE_C_COMPILER_ID STREQUAL "Intel")
# Intel compiler does not reject bad flags or objects!
set(RunCMake_TEST_OUTPUT_MERGE 1)
run_cmake(CMP0210-NEW)
unset(RunCMake_TEST_OUTPUT_MERGE)
endif()
if(RunCMake_GENERATOR MATCHES "Make|Ninja")
# Use a single build tree for a few tests without cleaning.