From 9436ad35df17df89e1a74f91103b04baad085127 Mon Sep 17 00:00:00 2001 From: Marc Chevrier Date: Sat, 30 May 2020 11:45:53 +0200 Subject: target_link_libraries: self-link through ALIAS is an error Fixes: #19617 --- Help/manual/cmake-policies.7.rst | 1 + Help/policy/CMP0108.rst | 19 +++++++++++++++++++ Help/release/dev/self-link-through-alias.rst | 5 +++++ Source/cmGeneratorTarget.cxx | 7 +++++++ Source/cmPolicies.h | 5 ++++- Tests/RunCMake/TargetPolicies/PolicyList-stderr.txt | 1 + .../CMP0108-NEW-self-link-result.txt | 1 + .../CMP0108-NEW-self-link-stderr.txt | 5 +++++ .../target_link_libraries/CMP0108-NEW-self-link.cmake | 4 ++++ .../target_link_libraries/CMP0108-OLD-self-link.cmake | 4 ++++ .../target_link_libraries/CMP0108-self-link.cmake | 9 +++++++++ .../RunCMake/target_link_libraries/RunCMakeTest.cmake | 2 ++ 12 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 Help/policy/CMP0108.rst create mode 100644 Help/release/dev/self-link-through-alias.rst create mode 100644 Tests/RunCMake/target_link_libraries/CMP0108-NEW-self-link-result.txt create mode 100644 Tests/RunCMake/target_link_libraries/CMP0108-NEW-self-link-stderr.txt create mode 100644 Tests/RunCMake/target_link_libraries/CMP0108-NEW-self-link.cmake create mode 100644 Tests/RunCMake/target_link_libraries/CMP0108-OLD-self-link.cmake create mode 100644 Tests/RunCMake/target_link_libraries/CMP0108-self-link.cmake diff --git a/Help/manual/cmake-policies.7.rst b/Help/manual/cmake-policies.7.rst index 8d41ed8..e98038a 100644 --- a/Help/manual/cmake-policies.7.rst +++ b/Help/manual/cmake-policies.7.rst @@ -57,6 +57,7 @@ Policies Introduced by CMake 3.18 .. toctree:: :maxdepth: 1 + CMP0108: A target cannot link to itself through an alias. CMP0107: An ALIAS target cannot overwrite another target. CMP0106: The Documentation module is removed. CMP0105: Device link step uses the link options. diff --git a/Help/policy/CMP0108.rst b/Help/policy/CMP0108.rst new file mode 100644 index 0000000..0d54cfa --- /dev/null +++ b/Help/policy/CMP0108.rst @@ -0,0 +1,19 @@ +CMP0108 +------- + +A target is not allowed to link to itself even through an ``ALIAS`` target. + +In CMake 3.17 and below, a target can link to a target aliased to itself. + +The ``OLD`` behavior for this policy is to allow a target to link to a target +aliased to itself. + +The ``NEW`` behavior of this policy is to prevent a target to link to itself +through an ``ALIAS`` target. + +This policy was introduced in CMake version 3.17. Use the +:command:`cmake_policy` command to set it to ``OLD`` or ``NEW`` explicitly. +Unlike many policies, CMake version |release| does *not* warn +when this policy is not set and simply uses ``OLD`` behavior. + +.. include:: DEPRECATED.txt diff --git a/Help/release/dev/self-link-through-alias.rst b/Help/release/dev/self-link-through-alias.rst new file mode 100644 index 0000000..d91d7ae --- /dev/null +++ b/Help/release/dev/self-link-through-alias.rst @@ -0,0 +1,5 @@ +self-link-through-alias +----------------------- + +* Linking a target to itself through an alias now raise an error. + See policy :policy:`CMP0108`. diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index f2a51ab..24a1dde 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -7031,6 +7031,13 @@ void cmGeneratorTarget::ComputeLinkImplementationLibraries( // Skip entries that resolve to the target itself or are empty. std::string name = this->CheckCMP0004(lib); + if (this->GetPolicyStatusCMP0108() == cmPolicies::NEW) { + // resolve alias name + auto target = this->Makefile->FindTargetToUse(name); + if (target) { + name = target->GetName(); + } + } if (name == this->GetName() || name.empty()) { if (name == this->GetName()) { bool noMessage = false; diff --git a/Source/cmPolicies.h b/Source/cmPolicies.h index 4dff1d8..a82f421 100644 --- a/Source/cmPolicies.h +++ b/Source/cmPolicies.h @@ -318,6 +318,8 @@ class cmMakefile; SELECT(POLICY, CMP0106, "The Documentation module is removed.", 3, 18, 0, \ cmPolicies::WARN) \ SELECT(POLICY, CMP0107, "An ALIAS target cannot overwrite another target.", \ + 3, 18, 0, cmPolicies::WARN) \ + SELECT(POLICY, CMP0108, "A target cannot link to itself through an alias.", \ 3, 18, 0, cmPolicies::WARN) #define CM_SELECT_ID(F, A1, A2, A3, A4, A5, A6) F(A1) @@ -350,7 +352,8 @@ class cmMakefile; F(CMP0095) \ F(CMP0099) \ F(CMP0104) \ - F(CMP0105) + F(CMP0105) \ + F(CMP0108) /** \class cmPolicies * \brief Handles changes in CMake behavior and policies diff --git a/Tests/RunCMake/TargetPolicies/PolicyList-stderr.txt b/Tests/RunCMake/TargetPolicies/PolicyList-stderr.txt index 2d270c5..2454f25 100644 --- a/Tests/RunCMake/TargetPolicies/PolicyList-stderr.txt +++ b/Tests/RunCMake/TargetPolicies/PolicyList-stderr.txt @@ -31,6 +31,7 @@ \* CMP0099 \* CMP0104 \* CMP0105 + \* CMP0108 Call Stack \(most recent call first\): CMakeLists.txt:3 \(include\) diff --git a/Tests/RunCMake/target_link_libraries/CMP0108-NEW-self-link-result.txt b/Tests/RunCMake/target_link_libraries/CMP0108-NEW-self-link-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/target_link_libraries/CMP0108-NEW-self-link-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/target_link_libraries/CMP0108-NEW-self-link-stderr.txt b/Tests/RunCMake/target_link_libraries/CMP0108-NEW-self-link-stderr.txt new file mode 100644 index 0000000..7389eff --- /dev/null +++ b/Tests/RunCMake/target_link_libraries/CMP0108-NEW-self-link-stderr.txt @@ -0,0 +1,5 @@ +CMake Error at CMP0108-self-link.cmake:[0-9]+ \(add_library\): + Target "foo" links to itself. +Call Stack \(most recent call first\): + CMP0108-NEW-self-link.cmake:[0-9]+ \(include\) + CMakeLists.txt:[0-9]+ \(include\) diff --git a/Tests/RunCMake/target_link_libraries/CMP0108-NEW-self-link.cmake b/Tests/RunCMake/target_link_libraries/CMP0108-NEW-self-link.cmake new file mode 100644 index 0000000..4ee9621 --- /dev/null +++ b/Tests/RunCMake/target_link_libraries/CMP0108-NEW-self-link.cmake @@ -0,0 +1,4 @@ + +cmake_policy (SET CMP0108 NEW) + +include (CMP0108-self-link.cmake) diff --git a/Tests/RunCMake/target_link_libraries/CMP0108-OLD-self-link.cmake b/Tests/RunCMake/target_link_libraries/CMP0108-OLD-self-link.cmake new file mode 100644 index 0000000..813104a --- /dev/null +++ b/Tests/RunCMake/target_link_libraries/CMP0108-OLD-self-link.cmake @@ -0,0 +1,4 @@ + +cmake_policy (SET CMP0108 OLD) + +include (CMP0108-self-link.cmake) diff --git a/Tests/RunCMake/target_link_libraries/CMP0108-self-link.cmake b/Tests/RunCMake/target_link_libraries/CMP0108-self-link.cmake new file mode 100644 index 0000000..07a3490 --- /dev/null +++ b/Tests/RunCMake/target_link_libraries/CMP0108-self-link.cmake @@ -0,0 +1,9 @@ + +cmake_policy (SET CMP0038 NEW) +cmake_policy (SET CMP0042 NEW) + +enable_language(C) + +add_library(foo SHARED lib.c) +add_library(Bar::foo ALIAS foo) +target_link_libraries(foo PRIVATE Bar::foo) diff --git a/Tests/RunCMake/target_link_libraries/RunCMakeTest.cmake b/Tests/RunCMake/target_link_libraries/RunCMakeTest.cmake index fb223ab..dfa71dd 100644 --- a/Tests/RunCMake/target_link_libraries/RunCMakeTest.cmake +++ b/Tests/RunCMake/target_link_libraries/RunCMakeTest.cmake @@ -19,6 +19,8 @@ run_cmake(CMP0079-link-WARN) run_cmake(CMP0079-link-OLD) run_cmake(CMP0079-link-NEW) run_cmake(CMP0079-link-NEW-bogus) +run_cmake(CMP0108-OLD-self-link) +run_cmake(CMP0108-NEW-self-link) run_cmake(ImportedTarget) run_cmake(ImportedTargetFailure) run_cmake(MixedSignature) -- cgit v0.12