Add LINK_LIBRARIES property for direct target link dependencies

Previously we kept direct link dependencies in OriginalLinkLibraries.
The property exposes the information in the CMake language through the
get/set_property commands.  We preserve the OriginalLinkLibraries value
internally to support old APIs like that for CMP0003's OLD behavior, but
the property is now authoritative.  This follows up from commit d5cf644a
(Split link information processing into two steps, 2012-11-01).

This will be used later to populate the link interface properties when
exporting targets, and will later allow use of generator expressions
when linking to libraries with target_link_libraries.

Also make targets depend on the (config-specific) union of dependencies.
CMake now allows linking to dependencies or not depending on the config.
However, generated build systems are not all capable of processing
config-specific dependencies, so the targets depend on the union of
dependencies for all configs.
This commit is contained in:
Stephen Kelly 2012-12-06 12:14:03 +01:00 committed by Brad King
parent 40cf3fb95b
commit 7653862798
7 changed files with 164 additions and 17 deletions

View File

@ -200,25 +200,51 @@ void cmComputeTargetDepends::CollectTargetDepends(int depender_index)
// Get the depender.
cmTarget* depender = this->Targets[depender_index];
// Loop over all targets linked directly.
// Loop over all targets linked directly in all configs.
// We need to make targets depend on the union of all config-specific
// dependencies in all targets, because the generated build-systems can't
// deal with config-specific dependencies.
{
cmTarget::LinkLibraryVectorType const& tlibs =
depender->GetOriginalLinkLibraries();
std::set<cmStdString> emitted;
{
std::vector<std::string> tlibs;
depender->GetDirectLinkLibraries(0, tlibs, depender);
// A target should not depend on itself.
emitted.insert(depender->GetName());
for(cmTarget::LinkLibraryVectorType::const_iterator lib = tlibs.begin();
for(std::vector<std::string>::const_iterator lib = tlibs.begin();
lib != tlibs.end(); ++lib)
{
// Don't emit the same library twice for this target.
if(emitted.insert(lib->first).second)
if(emitted.insert(*lib).second)
{
this->AddTargetDepend(depender_index, lib->first.c_str(), true);
this->AddInterfaceDepends(depender_index, lib->first.c_str(),
this->AddTargetDepend(depender_index, lib->c_str(), true);
this->AddInterfaceDepends(depender_index, lib->c_str(),
true, emitted);
}
}
}
std::vector<std::string> configs;
depender->GetMakefile()->GetConfigurations(configs);
for (std::vector<std::string>::const_iterator it = configs.begin();
it != configs.end(); ++it)
{
std::vector<std::string> tlibs;
depender->GetDirectLinkLibraries(it->c_str(), tlibs, depender);
// A target should not depend on itself.
emitted.insert(depender->GetName());
for(std::vector<std::string>::const_iterator lib = tlibs.begin();
lib != tlibs.end(); ++lib)
{
// Don't emit the same library twice for this target.
if(emitted.insert(*lib).second)
{
this->AddTargetDepend(depender_index, lib->c_str(), true);
this->AddInterfaceDepends(depender_index, lib->c_str(),
true, emitted);
}
}
}
}
// Loop over all utility dependencies.
{

View File

@ -519,6 +519,22 @@ void cmTarget::DefineProperties(cmake *cm)
"Installing a target with EXCLUDE_FROM_ALL set to true has "
"undefined behavior.");
cm->DefineProperty
("LINK_LIBRARIES", cmProperty::TARGET,
"List of direct link dependencies.",
"This property specifies the list of libraries or targets which will be "
"used for linking. "
"In addition to accepting values from the target_link_libraries "
"command, values may be set directly on any target using the "
"set_property command. "
"\n"
"The target property values are used by the generators to set "
"the link libraries for the compiler. "
"See also the target_link_libraries command.\n"
"Contents of LINK_LIBRARIES may use \"generator expressions\" with "
"the syntax \"$<...>\". "
CM_DOCUMENT_COMMAND_GENERATOR_EXPRESSIONS);
cm->DefineProperty
("INCLUDE_DIRECTORIES", cmProperty::TARGET,
"List of preprocessor include file search directories.",
@ -2139,6 +2155,66 @@ bool cmTarget::NameResolvesToFramework(const std::string& libname)
NameResolvesToFramework(libname);
}
//----------------------------------------------------------------------------
void cmTarget::GetDirectLinkLibraries(const char *config,
std::vector<std::string> &libs, cmTarget *head)
{
const char *prop = this->GetProperty("LINK_LIBRARIES");
if (prop)
{
cmListFileBacktrace lfbt;
cmGeneratorExpression ge(lfbt);
cmGeneratorExpressionDAGChecker dagChecker(lfbt,
this->GetName(),
"LINK_LIBRARIES", 0, 0);
cmSystemTools::ExpandListArgument(ge.Parse(prop)->Evaluate(this->Makefile,
config,
false,
head,
&dagChecker),
libs);
}
}
//----------------------------------------------------------------------------
std::string cmTarget::GetDebugGeneratorExpressions(const std::string &value,
cmTarget::LinkLibraryType llt)
{
if (llt == GENERAL)
{
return value;
}
// Get the list of configurations considered to be DEBUG.
std::vector<std::string> const& debugConfigs =
this->Makefile->GetCMakeInstance()->GetDebugConfigs();
std::string configString = "$<CONFIG:" + debugConfigs[0] + ">";
if (debugConfigs.size() > 1)
{
for(std::vector<std::string>::const_iterator
li = debugConfigs.begin() + 1; li != debugConfigs.end(); ++li)
{
configString += ",$<CONFIG:" + *li + ">";
}
configString = "$<OR:" + configString + ">";
}
if (llt == OPTIMIZED)
{
configString = "$<NOT:" + configString + ">";
}
return "$<" + configString + ":" + value + ">";
}
//----------------------------------------------------------------------------
static std::string targetNameGenex(const char *lib)
{
return std::string("$<TARGET_NAME:") + lib + ">";
}
//----------------------------------------------------------------------------
void cmTarget::AddLinkLibrary(cmMakefile& mf,
const char *target, const char* lib,
@ -2149,6 +2225,18 @@ void cmTarget::AddLinkLibrary(cmMakefile& mf,
{
return;
}
{
cmTarget *tgt = this->Makefile->FindTargetToUse(lib);
const bool isNonImportedTarget = tgt && !tgt->IsImported();
std::string libName = isNonImportedTarget ? targetNameGenex(lib)
: std::string(lib);
this->AppendProperty("LINK_LIBRARIES",
this->GetDebugGeneratorExpressions(libName,
llt).c_str());
}
cmTarget::LibraryID tmp;
tmp.first = lib;
tmp.second = llt;
@ -4884,26 +4972,23 @@ void cmTarget::ComputeLinkImplementation(const char* config,
LinkImplementation& impl,
cmTarget *head)
{
(void)head;
// Compute which library configuration to link.
cmTarget::LinkLibraryType linkType = this->ComputeLinkType(config);
// Collect libraries directly linked in this configuration.
LinkLibraryVectorType const& llibs = this->GetOriginalLinkLibraries();
for(cmTarget::LinkLibraryVectorType::const_iterator li = llibs.begin();
std::vector<std::string> llibs;
this->GetDirectLinkLibraries(config, llibs, head);
for(std::vector<std::string>::const_iterator li = llibs.begin();
li != llibs.end(); ++li)
{
// Skip entries that resolve to the target itself or are empty.
std::string item = this->CheckCMP0004(li->first);
std::string item = this->CheckCMP0004(*li);
if(item == this->GetName() || item.empty())
{
continue;
}
if(li->second == cmTarget::GENERAL || li->second == linkType)
{
// The entry is meant for this configuration.
impl.Libraries.push_back(item);
}
// The entry is meant for this configuration.
impl.Libraries.push_back(item);
}
LinkLibraryVectorType const& oldllibs = this->GetOriginalLinkLibraries();

View File

@ -168,6 +168,9 @@ public:
return this->LinkLibraries;}
const LinkLibraryVectorType &GetOriginalLinkLibraries() const
{return this->OriginalLinkLibraries;}
void GetDirectLinkLibraries(const char *config,
std::vector<std::string> &,
cmTarget *head);
/** Compute the link type to use for the given configuration. */
LinkLibraryType ComputeLinkType(const char* config);
@ -622,6 +625,9 @@ private:
void ProcessSourceExpression(std::string const& expr);
std::string GetDebugGeneratorExpressions(const std::string &value,
cmTarget::LinkLibraryType llt);
// The cmMakefile instance that owns this target. This should
// always be set.
cmMakefile* Makefile;

View File

@ -34,6 +34,13 @@ generate_export_header(depB)
target_link_libraries(depB LINK_PRIVATE depA)
add_library(libgenex SHARED libgenex.cpp)
generate_export_header(libgenex)
set_property(TARGET depB APPEND PROPERTY
LINK_LIBRARIES $<1:libgenex>
)
add_library(depC SHARED depC.cpp)
generate_export_header(depC)

View File

@ -3,9 +3,13 @@
#include "depA.h"
#include "libgenex.h"
int DepB::foo()
{
DepA a;
return a.foo();
LibGenex lg;
return a.foo() + lg.foo();
}

View File

@ -0,0 +1,7 @@
#include "libgenex.h"
int LibGenex::foo()
{
return 0;
}

View File

@ -0,0 +1,12 @@
#include "libgenex_export.h"
#ifndef LIBGENEX_H
#define LIBGENEX_H
struct LIBGENEX_EXPORT LibGenex
{
int foo();
};
#endif