CMP0022: Plain target_link_libraries must populate link interface

The CMP0022 NEW behavior is that the INTERFACE_LINK_LIBRARIES property
exactly defines the link interface.  The plain target_link_libraries
signature says linking is transitive by default, so it should populate
the property.

Teach the target_link_libraries plain signature to populate the
INTERFACE_LINK_LIBRARIES regardless of the CMP0022 setting.  Refactor
the cmTarget::ComputeLinkInterface checks that warn when the policy is
not set to compare the new property to either the explicitly set old
link interface properties or the link implementation fallback for all
linkable target types, not just static libraries.

This fixes a regression in 2.8.12.0 that caused target_link_libraries to
not implement transitive linking in the plain signature once the policy
CMP0022 is set to NEW.
This commit is contained in:
Brad King 2013-11-02 12:36:23 -04:00
parent 0e06788c0a
commit ef10b87cc1
11 changed files with 146 additions and 152 deletions

View File

@ -631,6 +631,7 @@ cmExportFileGenerator
if (iface->ImplementationIsInterface) if (iface->ImplementationIsInterface)
{ {
// Policy CMP0022 must not be NEW.
this->SetImportLinkProperty(suffix, target, this->SetImportLinkProperty(suffix, target,
"IMPORTED_LINK_INTERFACE_LIBRARIES", "IMPORTED_LINK_INTERFACE_LIBRARIES",
iface->Libraries, properties, missingTargets); iface->Libraries, properties, missingTargets);

View File

@ -2331,21 +2331,10 @@ void cmTarget::MergeLinkLibraries( cmMakefile& mf,
i += this->PrevLinkedLibraries.size(); i += this->PrevLinkedLibraries.size();
for( ; i != libs.end(); ++i ) for( ; i != libs.end(); ++i )
{ {
const char *lib = i->first.c_str(); // This is equivalent to the target_link_libraries plain signature.
// We call this so that the dependencies get written to the cache this->AddLinkLibrary( mf, selfname, i->first.c_str(), i->second );
this->AddLinkLibrary( mf, selfname, lib, i->second ); this->AppendProperty("INTERFACE_LINK_LIBRARIES",
this->GetDebugGeneratorExpressions(i->first.c_str(), i->second).c_str());
if (this->GetType() == cmTarget::STATIC_LIBRARY)
{
std::string configLib = this->GetDebugGeneratorExpressions(lib,
i->second);
if (cmGeneratorExpression::IsValidTargetName(lib)
|| cmGeneratorExpression::Find(lib) != std::string::npos)
{
configLib = "$<LINK_ONLY:" + configLib + ">";
}
this->AppendProperty("INTERFACE_LINK_LIBRARIES", configLib.c_str());
}
} }
this->PrevLinkedLibraries = libs; this->PrevLinkedLibraries = libs;
} }
@ -6417,12 +6406,20 @@ bool cmTarget::ComputeLinkInterface(const char* config, LinkInterface& iface,
// An explicit list of interface libraries may be set for shared // An explicit list of interface libraries may be set for shared
// libraries and executables that export symbols. // libraries and executables that export symbols.
const char* explicitLibraries = 0; const char* explicitLibraries = 0;
const char* newExplicitLibraries =
this->GetProperty("INTERFACE_LINK_LIBRARIES");
std::string linkIfaceProp; std::string linkIfaceProp;
if(this->GetType() == cmTarget::SHARED_LIBRARY || if(this->PolicyStatusCMP0022 != cmPolicies::OLD &&
this->PolicyStatusCMP0022 != cmPolicies::WARN)
{
// CMP0022 NEW behavior is to use INTERFACE_LINK_LIBRARIES.
linkIfaceProp = "INTERFACE_LINK_LIBRARIES";
explicitLibraries = this->GetProperty(linkIfaceProp.c_str());
}
else if(this->GetType() == cmTarget::SHARED_LIBRARY ||
this->IsExecutableWithExports()) this->IsExecutableWithExports())
{ {
// CMP0022 OLD behavior is to use LINK_INTERFACE_LIBRARIES if set on a
// shared lib or executable.
// Lookup the per-configuration property. // Lookup the per-configuration property.
linkIfaceProp = "LINK_INTERFACE_LIBRARIES"; linkIfaceProp = "LINK_INTERFACE_LIBRARIES";
linkIfaceProp += suffix; linkIfaceProp += suffix;
@ -6434,121 +6431,31 @@ bool cmTarget::ComputeLinkInterface(const char* config, LinkInterface& iface,
linkIfaceProp = "LINK_INTERFACE_LIBRARIES"; linkIfaceProp = "LINK_INTERFACE_LIBRARIES";
explicitLibraries = this->GetProperty(linkIfaceProp.c_str()); explicitLibraries = this->GetProperty(linkIfaceProp.c_str());
} }
if (newExplicitLibraries
&& (!explicitLibraries ||
(explicitLibraries
&& strcmp(newExplicitLibraries, explicitLibraries) != 0)))
{
switch(this->GetPolicyStatusCMP0022())
{
case cmPolicies::WARN:
{
cmOStringStream w;
w << (this->Makefile->GetPolicies()
->GetPolicyWarning(cmPolicies::CMP0022)) << "\n"
<< "Target \"" << this->GetName() << "\" has a "
"INTERFACE_LINK_LIBRARIES property which differs from its "
<< linkIfaceProp << " properties."
"\n"
"INTERFACE_LINK_LIBRARIES:\n "
<< newExplicitLibraries
<< "\n"
<< linkIfaceProp << ":\n "
<< (explicitLibraries ? explicitLibraries : "(empty)") << "\n";
this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
}
// Fall through
case cmPolicies::OLD:
break;
case cmPolicies::REQUIRED_IF_USED:
case cmPolicies::REQUIRED_ALWAYS:
case cmPolicies::NEW:
explicitLibraries = newExplicitLibraries;
linkIfaceProp = "INTERFACE_LINK_LIBRARIES";
break;
}
}
}
else if(this->GetType() == cmTarget::STATIC_LIBRARY)
{
if (newExplicitLibraries)
{
cmListFileBacktrace lfbt;
cmGeneratorExpression ge(lfbt);
cmGeneratorExpressionDAGChecker dagChecker(lfbt, this->GetName(),
"INTERFACE_LINK_LIBRARIES", 0, 0);
std::vector<std::string> ifaceLibs;
cmSystemTools::ExpandListArgument(
ge.Parse(newExplicitLibraries)->Evaluate(
this->Makefile,
config,
false,
headTarget,
this, &dagChecker), ifaceLibs);
LinkImplementation const* impl = this->GetLinkImplementation(config,
headTarget);
if (ifaceLibs != impl->Libraries)
{
switch(this->GetPolicyStatusCMP0022())
{
case cmPolicies::WARN:
{
std::string oldLibraries;
std::string newLibraries;
const char *sep = "";
for(std::vector<std::string>::const_iterator it
= impl->Libraries.begin(); it != impl->Libraries.end(); ++it)
{
oldLibraries += sep;
oldLibraries += *it;
sep = ";";
}
sep = "";
for(std::vector<std::string>::const_iterator it
= ifaceLibs.begin(); it != ifaceLibs.end(); ++it)
{
newLibraries += sep;
newLibraries += *it;
sep = ";";
} }
if(explicitLibraries && this->PolicyStatusCMP0022 == cmPolicies::WARN)
{
// Compare the explicitly set old link interface properties to the
// preferred new link interface property one and warn if different.
const char* newExplicitLibraries =
this->GetProperty("INTERFACE_LINK_LIBRARIES");
if (newExplicitLibraries
&& strcmp(newExplicitLibraries, explicitLibraries) != 0)
{
cmOStringStream w; cmOStringStream w;
w << (this->Makefile->GetPolicies() w <<
(this->Makefile->GetPolicies()
->GetPolicyWarning(cmPolicies::CMP0022)) << "\n" ->GetPolicyWarning(cmPolicies::CMP0022)) << "\n"
<< "Static library target \"" << this->GetName() << "\" has a " "Target \"" << this->GetName() << "\" has an "
"INTERFACE_LINK_LIBRARIES property. This should be preferred " "INTERFACE_LINK_LIBRARIES property which differs from its " <<
"as the source of the link interface for this library. " linkIfaceProp << " properties."
"Ignoring the property and using the link implementation "
"as the link interface instead."
"\n" "\n"
"INTERFACE_LINK_LIBRARIES:\n " "INTERFACE_LINK_LIBRARIES:\n"
<< newLibraries " " << newExplicitLibraries << "\n" <<
<< "\n" linkIfaceProp << ":\n"
<< "Link implementation:\n " " " << (explicitLibraries ? explicitLibraries : "(empty)") << "\n";
<< oldLibraries << "\n";
this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str()); this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
} }
// Fall through
case cmPolicies::OLD:
break;
case cmPolicies::REQUIRED_IF_USED:
case cmPolicies::REQUIRED_ALWAYS:
case cmPolicies::NEW:
explicitLibraries = newExplicitLibraries;
linkIfaceProp = "INTERFACE_LINK_LIBRARIES";
break;
}
}
else
{
iface.Libraries = impl->Libraries;
if(this->LinkLanguagePropagatesToDependents())
{
// Targets using this archive need its language runtime libraries.
iface.Languages = impl->Languages;
}
}
}
} }
// There is no implicit link interface for executables or modules // There is no implicit link interface for executables or modules
@ -6618,11 +6525,12 @@ bool cmTarget::ComputeLinkInterface(const char* config, LinkInterface& iface,
} }
} }
} }
else if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN else if (this->PolicyStatusCMP0022 == cmPolicies::WARN
|| this->GetPolicyStatusCMP0022() == cmPolicies::OLD) || this->PolicyStatusCMP0022 == cmPolicies::OLD)
// The implementation shouldn't be the interface if CMP0022 is NEW. That // If CMP0022 is NEW then the plain tll signature sets the
// way, the LINK_LIBRARIES property can be set directly without having to // INTERFACE_LINK_LIBRARIES, so if we get here then the project
// empty the INTERFACE_LINK_LIBRARIES // cleared the property explicitly and we should not fall back
// to the link implementation.
{ {
// The link implementation is the default link interface. // The link implementation is the default link interface.
LinkImplementation const* impl = this->GetLinkImplementation(config, LinkImplementation const* impl = this->GetLinkImplementation(config,
@ -6635,6 +6543,68 @@ bool cmTarget::ComputeLinkInterface(const char* config, LinkInterface& iface,
// Targets using this archive need its language runtime libraries. // Targets using this archive need its language runtime libraries.
iface.Languages = impl->Languages; iface.Languages = impl->Languages;
} }
if(this->PolicyStatusCMP0022 == cmPolicies::WARN)
{
// Compare the link implementation fallback link interface to the
// preferred new link interface property and warn if different.
cmListFileBacktrace lfbt;
cmGeneratorExpression ge(lfbt);
cmGeneratorExpressionDAGChecker dagChecker(lfbt, this->GetName(),
"INTERFACE_LINK_LIBRARIES", 0, 0);
std::vector<std::string> ifaceLibs;
const char* newExplicitLibraries =
this->GetProperty("INTERFACE_LINK_LIBRARIES");
cmSystemTools::ExpandListArgument(
ge.Parse(newExplicitLibraries)->Evaluate(this->Makefile,
config,
false,
headTarget,
this, &dagChecker),
ifaceLibs);
if (ifaceLibs != impl->Libraries)
{
std::string oldLibraries;
std::string newLibraries;
const char *sep = "";
for(std::vector<std::string>::const_iterator it
= impl->Libraries.begin(); it != impl->Libraries.end(); ++it)
{
oldLibraries += sep;
oldLibraries += *it;
sep = ";";
}
sep = "";
for(std::vector<std::string>::const_iterator it
= ifaceLibs.begin(); it != ifaceLibs.end(); ++it)
{
newLibraries += sep;
newLibraries += *it;
sep = ";";
}
if(oldLibraries.empty())
{ oldLibraries = "(empty)"; }
if(newLibraries.empty())
{ newLibraries = "(empty)"; }
cmOStringStream w;
w <<
(this->Makefile->GetPolicies()
->GetPolicyWarning(cmPolicies::CMP0022)) << "\n"
"Target \"" << this->GetName() << "\" has an "
"INTERFACE_LINK_LIBRARIES property. "
"This should be preferred as the source of the link interface "
"for this library but because CMP0022 is not set CMake is "
"ignoring the property and using the link implementation "
"as the link interface instead."
"\n"
"INTERFACE_LINK_LIBRARIES:\n"
" " << newLibraries << "\n"
"Link implementation:\n"
" " << oldLibraries << "\n";
this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
}
}
} }
if(this->GetType() == cmTarget::STATIC_LIBRARY) if(this->GetType() == cmTarget::STATIC_LIBRARY)

View File

@ -379,7 +379,13 @@ cmTargetLinkLibrariesCommand::HandleLibrary(const char* lib,
{ {
this->Makefile this->Makefile
->AddLinkLibraryForTarget(this->Target->GetName(), lib, llt); ->AddLinkLibraryForTarget(this->Target->GetName(), lib, llt);
if (this->CurrentProcessingState != ProcessingKeywordPublicInterface if(this->CurrentProcessingState == ProcessingLinkLibraries)
{
this->Target->AppendProperty("INTERFACE_LINK_LIBRARIES",
this->Target->GetDebugGeneratorExpressions(lib, llt).c_str());
return true;
}
else if(this->CurrentProcessingState != ProcessingKeywordPublicInterface
&& this->CurrentProcessingState != ProcessingPlainPublicInterface) && this->CurrentProcessingState != ProcessingPlainPublicInterface)
{ {
if (this->Target->GetType() == cmTarget::STATIC_LIBRARY) if (this->Target->GetType() == cmTarget::STATIC_LIBRARY)

View File

@ -88,14 +88,19 @@ public:
"See the IMPORTED mode of the add_library command for more " "See the IMPORTED mode of the add_library command for more "
"information. " "information. "
"\n" "\n"
"Library dependencies are transitive by default. " "Library dependencies are transitive by default with this signature. "
"When this target is linked into another target then the libraries " "When this target is linked into another target then the libraries "
"linked to this target will appear on the link line for the other " "linked to this target will appear on the link line for the other "
"target too. " "target too. "
"See the INTERFACE_LINK_LIBRARIES target property to override the " "This transitive \"link interface\" is stored in the "
"set of transitive link dependencies for a target. " "INTERFACE_LINK_LIBRARIES target property when policy CMP0022 is set "
"to NEW and may be overridden by setting the property directly. "
"("
"When CMP0022 is not set to NEW, transitive linking is builtin "
"but may be overridden by the LINK_INTERFACE_LIBRARIES property. "
"Calls to other signatures of this command may set the property " "Calls to other signatures of this command may set the property "
"making any libraries linked exclusively by this signature private." "making any libraries linked exclusively by this signature private."
")"
"\n" "\n"
"CMake will also propagate \"usage requirements\" from linked library " "CMake will also propagate \"usage requirements\" from linked library "
"targets. " "targets. "

View File

@ -0,0 +1 @@
^$

View File

@ -0,0 +1,8 @@
enable_language(CXX)
add_library(foo SHARED empty_vs6_1.cpp)
add_library(bar SHARED empty_vs6_2.cpp)
target_link_libraries(bar foo)
add_executable(zot empty.cpp)
target_link_libraries(zot bar)

View File

@ -3,14 +3,16 @@ CMake Warning \(dev\) in CMakeLists.txt:
interface. Run "cmake --help-policy CMP0022" for policy details. Use the interface. Run "cmake --help-policy CMP0022" for policy details. Use the
cmake_policy command to set the policy and suppress this warning. cmake_policy command to set the policy and suppress this warning.
Target "bar" has a INTERFACE_LINK_LIBRARIES property which differs from its Target "bar" has an INTERFACE_LINK_LIBRARIES property. This should be
LINK_INTERFACE_LIBRARIES properties. preferred as the source of the link interface for this library but because
CMP0022 is not set CMake is ignoring the property and using the link
implementation as the link interface instead.
INTERFACE_LINK_LIBRARIES: INTERFACE_LINK_LIBRARIES:
foo foo
LINK_INTERFACE_LIBRARIES: Link implementation:
\(empty\) \(empty\)

View File

@ -3,10 +3,10 @@ CMake Warning \(dev\) in CMakeLists.txt:
interface. Run "cmake --help-policy CMP0022" for policy details. Use the interface. Run "cmake --help-policy CMP0022" for policy details. Use the
cmake_policy command to set the policy and suppress this warning. cmake_policy command to set the policy and suppress this warning.
Static library target "bar" has a INTERFACE_LINK_LIBRARIES property. This Target "bar" has an INTERFACE_LINK_LIBRARIES property. This should be
should be preferred as the source of the link interface for this library. preferred as the source of the link interface for this library but because
Ignoring the property and using the link implementation as the link CMP0022 is not set CMake is ignoring the property and using the link
interface instead. implementation as the link interface instead.
INTERFACE_LINK_LIBRARIES: INTERFACE_LINK_LIBRARIES:

View File

@ -3,8 +3,8 @@ CMake Warning \(dev\) in CMakeLists.txt:
interface. Run "cmake --help-policy CMP0022" for policy details. Use the interface. Run "cmake --help-policy CMP0022" for policy details. Use the
cmake_policy command to set the policy and suppress this warning. cmake_policy command to set the policy and suppress this warning.
Target "bar" has a INTERFACE_LINK_LIBRARIES property which differs from its Target "bar" has an INTERFACE_LINK_LIBRARIES property which differs from
LINK_INTERFACE_LIBRARIES properties. its LINK_INTERFACE_LIBRARIES properties.
INTERFACE_LINK_LIBRARIES: INTERFACE_LINK_LIBRARIES:

View File

@ -3,8 +3,8 @@ CMake Warning \(dev\) in CMakeLists.txt:
interface. Run "cmake --help-policy CMP0022" for policy details. Use the interface. Run "cmake --help-policy CMP0022" for policy details. Use the
cmake_policy command to set the policy and suppress this warning. cmake_policy command to set the policy and suppress this warning.
Target "bar" has a INTERFACE_LINK_LIBRARIES property which differs from its Target "bar" has an INTERFACE_LINK_LIBRARIES property which differs from
LINK_INTERFACE_LIBRARIES properties. its LINK_INTERFACE_LIBRARIES properties.
INTERFACE_LINK_LIBRARIES: INTERFACE_LINK_LIBRARIES:

View File

@ -4,6 +4,7 @@ run_cmake(CMP0022-WARN)
run_cmake(CMP0022-WARN-tll) run_cmake(CMP0022-WARN-tll)
run_cmake(CMP0022-WARN-static) run_cmake(CMP0022-WARN-static)
run_cmake(CMP0022-WARN-empty-old) run_cmake(CMP0022-WARN-empty-old)
run_cmake(CMP0022-NOWARN-shared)
run_cmake(CMP0022-NOWARN-static) run_cmake(CMP0022-NOWARN-static)
run_cmake(CMP0022-NOWARN-static-link_libraries) run_cmake(CMP0022-NOWARN-static-link_libraries)
run_cmake(CMP0022-export) run_cmake(CMP0022-export)