Explorar o código

target_link_libraries: self-link through ALIAS is an error

Fixes: #19617
Marc Chevrier %!s(int64=5) %!d(string=hai) anos
pai
achega
9436ad35df

+ 1 - 0
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. </policy/CMP0108>
    CMP0107: An ALIAS target cannot overwrite another target. </policy/CMP0107>
    CMP0106: The Documentation module is removed. </policy/CMP0106>
    CMP0105: Device link step uses the link options. </policy/CMP0105>

+ 19 - 0
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

+ 5 - 0
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`.

+ 7 - 0
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;

+ 4 - 1
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

+ 1 - 0
Tests/RunCMake/TargetPolicies/PolicyList-stderr.txt

@@ -31,6 +31,7 @@
    \* CMP0099
    \* CMP0104
    \* CMP0105
+   \* CMP0108
 
 Call Stack \(most recent call first\):
   CMakeLists.txt:3 \(include\)

+ 1 - 0
Tests/RunCMake/target_link_libraries/CMP0108-NEW-self-link-result.txt

@@ -0,0 +1 @@
+1

+ 5 - 0
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\)

+ 4 - 0
Tests/RunCMake/target_link_libraries/CMP0108-NEW-self-link.cmake

@@ -0,0 +1,4 @@
+
+cmake_policy (SET CMP0108 NEW)
+
+include (CMP0108-self-link.cmake)

+ 4 - 0
Tests/RunCMake/target_link_libraries/CMP0108-OLD-self-link.cmake

@@ -0,0 +1,4 @@
+
+cmake_policy (SET CMP0108 OLD)
+
+include (CMP0108-self-link.cmake)

+ 9 - 0
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)

+ 2 - 0
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)