1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-15 20:46:37 +08:00

target_link_libraries: self-link through ALIAS is an error

Fixes: #19617
This commit is contained in:
Marc Chevrier
2020-05-30 11:45:53 +02:00
parent 67dd687657
commit 9436ad35df
12 changed files with 62 additions and 1 deletions

View File

@@ -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
Help/policy/CMP0108.rst Normal file
View File

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

View File

@@ -0,0 +1,5 @@
self-link-through-alias
-----------------------
* Linking a target to itself through an alias now raise an error.
See policy :policy:`CMP0108`.

View File

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

View File

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

View File

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

View File

@@ -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\)

View File

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

View File

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

View File

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

View File

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