ENH: Adding support to link specifically to an archive or a shared library based on the file name specified. This fixes the problem of having -lfoo linking to libfoo.so even when it came from libfoo.a being specified.

This commit is contained in:
Brad King 2006-09-15 14:09:10 -04:00
parent 429571bd1f
commit 1d0502927c
7 changed files with 286 additions and 44 deletions

View File

@ -72,10 +72,6 @@ IF(NOT CMAKE_SHARED_MODULE_CXX_FLAGS)
SET(CMAKE_SHARED_MODULE_CXX_FLAGS ${CMAKE_SHARED_MODULE_C_FLAGS})
ENDIF(NOT CMAKE_SHARED_MODULE_CXX_FLAGS)
IF(NOT CMAKE_SHARED_MODULE_LINK_CXX_FLAGS)
SET(CMAKE_SHARED_MODULE_LINK_CXX_FLAGS ${CMAKE_SHARED_MODULE_LINK_C_FLAGS})
ENDIF(NOT CMAKE_SHARED_MODULE_LINK_CXX_FLAGS)
IF(NOT CMAKE_SHARED_MODULE_RUNTIME_CXX_FLAG)
SET(CMAKE_SHARED_MODULE_RUNTIME_CXX_FLAG ${CMAKE_SHARED_MODULE_RUNTIME_FLAG})
ENDIF(NOT CMAKE_SHARED_MODULE_RUNTIME_CXX_FLAG)
@ -84,6 +80,18 @@ IF(NOT CMAKE_SHARED_MODULE_RUNTIME_CXX_FLAG_SEP)
SET(CMAKE_SHARED_MODULE_RUNTIME_CXX_FLAG_SEP ${CMAKE_SHARED_MODULE_RUNTIME_FLAG_SEP})
ENDIF(NOT CMAKE_SHARED_MODULE_RUNTIME_CXX_FLAG_SEP)
# Initialize CXX link type selection flags from C versions.
FOREACH(type SHARED_LIBRARY SHARED_MODULE EXE)
IF(NOT CMAKE_${type}_LINK_STATIC_CXX_FLAGS)
SET(CMAKE_${type}_LINK_STATIC_CXX_FLAGS
${CMAKE_${type}_LINK_STATIC_C_FLAGS})
ENDIF(NOT CMAKE_${type}_LINK_STATIC_CXX_FLAGS)
IF(NOT CMAKE_${type}_LINK_DYNAMIC_CXX_FLAGS)
SET(CMAKE_${type}_LINK_DYNAMIC_CXX_FLAGS
${CMAKE_${type}_LINK_DYNAMIC_C_FLAGS})
ENDIF(NOT CMAKE_${type}_LINK_DYNAMIC_CXX_FLAGS)
ENDFOREACH(type)
# add the flags to the cache based
# on the initial values computed in the platform/*.cmake files
# use _INIT variables so that this only happens the first time

View File

@ -30,7 +30,6 @@ ENDIF(EXISTS ${CMAKE_SYSTEM_INFO_FILE})
IF(NOT CMAKE_MODULE_EXISTS)
SET(CMAKE_SHARED_MODULE_PREFIX "${CMAKE_SHARED_LIBRARY_PREFIX}")
SET(CMAKE_SHARED_MODULE_SUFFIX "${CMAKE_SHARED_LIBRARY_SUFFIX}")
SET(CMAKE_SHARED_MODULE_LINK_C_FLAGS ${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS})
SET(CMAKE_SHARED_MODULE_RUNTIME_C_FLAG ${CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG})
SET(CMAKE_SHARED_MODULE_RUNTIME_C_FLAG_SEP ${CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG_SEP})
ENDIF(NOT CMAKE_MODULE_EXISTS)

View File

@ -8,4 +8,14 @@ SET(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG_SEP ":")
SET(CMAKE_SHARED_LIBRARY_SONAME_C_FLAG "-Wl,-soname,")
SET(CMAKE_SHARED_LIBRARY_SONAME_CXX_FLAG "-Wl,-soname,")
SET(CMAKE_SHARED_LIBRARY_SONAME_Fortran_FLAG "-Wl,-soname,")
# Initialize C link type selection flags. These flags are used when
# building a shared library, shared module, or executable that links
# to other libraries to select whether to use the static or shared
# versions of the libraries.
FOREACH(type SHARED_LIBRARY SHARED_MODULE EXE)
SET(CMAKE_${type}_LINK_STATIC_C_FLAGS "-Wl,-Bstatic")
SET(CMAKE_${type}_LINK_DYNAMIC_C_FLAGS "-Wl,-Bdynamic")
ENDFOREACH(type)
INCLUDE(Platform/UnixPaths)

View File

@ -1639,14 +1639,60 @@ void cmLocalGenerator
const std::vector<std::string>&
linkDirectories = target.GetLinkDirectories();
// Get the language used for linking.
const char* linkLanguage =
target.GetLinkerLanguage(this->GetGlobalGenerator());
if(!linkLanguage)
{
cmSystemTools::
Error("CMake can not determine linker language for target:",
target.GetName());
return;
}
// Lookup link type selection flags.
const char* static_link_type_flag = 0;
const char* shared_link_type_flag = 0;
const char* target_type_str = 0;
switch(target.GetType())
{
case cmTarget::EXECUTABLE: target_type_str = "EXE"; break;
case cmTarget::SHARED_LIBRARY: target_type_str = "SHARED_LIBRARY"; break;
case cmTarget::MODULE_LIBRARY: target_type_str = "SHARED_MODULE"; break;
default: break;
}
if(target_type_str)
{
std::string static_link_type_flag_var = "CMAKE_";
static_link_type_flag_var += target_type_str;
static_link_type_flag_var += "_LINK_STATIC_";
static_link_type_flag_var += linkLanguage;
static_link_type_flag_var += "_FLAGS";
static_link_type_flag =
this->Makefile->GetDefinition(static_link_type_flag_var.c_str());
std::string shared_link_type_flag_var = "CMAKE_";
shared_link_type_flag_var += target_type_str;
shared_link_type_flag_var += "_LINK_DYNAMIC_";
shared_link_type_flag_var += linkLanguage;
shared_link_type_flag_var += "_FLAGS";
shared_link_type_flag =
this->Makefile->GetDefinition(shared_link_type_flag_var.c_str());
}
// Compute the link directory order needed to link the libraries.
cmOrderLinkDirectories orderLibs;
orderLibs.SetLinkTypeInformation(cmOrderLinkDirectories::LinkShared,
static_link_type_flag,
shared_link_type_flag);
orderLibs.SetLinkPrefix(
this->Makefile->GetDefinition("CMAKE_STATIC_LIBRARY_PREFIX"));
orderLibs.AddLinkExtension(
this->Makefile->GetDefinition("CMAKE_STATIC_LIBRARY_SUFFIX"));
this->Makefile->GetDefinition("CMAKE_STATIC_LIBRARY_SUFFIX"),
cmOrderLinkDirectories::LinkStatic);
orderLibs.AddLinkExtension(
this->Makefile->GetDefinition("CMAKE_SHARED_LIBRARY_SUFFIX"));
this->Makefile->GetDefinition("CMAKE_SHARED_LIBRARY_SUFFIX"),
cmOrderLinkDirectories::LinkShared);
orderLibs.AddLinkExtension(
this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"));
orderLibs.AddLinkExtension(

View File

@ -3,13 +3,62 @@
#include "cmsys/RegularExpression.hxx"
#include <ctype.h>
//#define CM_ORDER_LINK_DIRECTORIES_DEBUG
//-------------------------------------------------------------------
cmOrderLinkDirectories::cmOrderLinkDirectories()
{
this->StartLinkType = LinkUnknown;
this->LinkTypeEnabled = false;
this->Debug = false;
}
//-------------------------------------------------------------------
void
cmOrderLinkDirectories
::SetLinkTypeInformation(LinkType start_link_type,
const char* static_link_type_flag,
const char* shared_link_type_flag)
{
// We can support link type switching only if all needed flags are
// known.
this->StartLinkType = start_link_type;
if(static_link_type_flag && *static_link_type_flag &&
shared_link_type_flag && *shared_link_type_flag)
{
this->LinkTypeEnabled = true;
this->StaticLinkTypeFlag = static_link_type_flag;
this->SharedLinkTypeFlag = shared_link_type_flag;
}
else
{
this->LinkTypeEnabled = false;
this->StaticLinkTypeFlag = "";
this->SharedLinkTypeFlag = "";
}
}
//-------------------------------------------------------------------
void cmOrderLinkDirectories::SetCurrentLinkType(LinkType lt)
{
if(this->CurrentLinkType != lt)
{
this->CurrentLinkType = lt;
if(this->LinkTypeEnabled)
{
switch(this->CurrentLinkType)
{
case LinkStatic:
this->LinkItems.push_back(this->StaticLinkTypeFlag); break;
case LinkShared:
this->LinkItems.push_back(this->SharedLinkTypeFlag); break;
default: break;
}
}
}
}
//-------------------------------------------------------------------
bool cmOrderLinkDirectories::LibraryInDirectory(const char* desiredLib,
const char* dir,
@ -126,16 +175,75 @@ std::string cmOrderLinkDirectories::NoCaseExpression(const char* str)
void cmOrderLinkDirectories::CreateRegularExpressions()
{
this->SplitFramework.compile("(.*)/(.*)\\.framework$");
cmStdString libext = "(";
bool first = true;
for(std::vector<cmStdString>::iterator i = this->LinkExtensions.begin();
i != this->LinkExtensions.end(); ++i)
// Compute a regex to match link extensions.
cmStdString libext = this->CreateExtensionRegex(this->LinkExtensions);
// Create regex to remove any library extension.
cmStdString reg("(.*)");
reg += libext;
this->RemoveLibraryExtension.compile(reg.c_str());
// Create a regex to match a library name. Match index 1 will be
// the prefix if it exists and empty otherwise. Match index 2 will
// be the library name. Match index 3 will be the library
// extension.
reg = "^(";
if(!this->LinkPrefix.empty())
{
if(!first)
{
libext += "|";
}
first = false;
reg += this->LinkPrefix;
reg += "|";
}
reg += ")";
reg += "([^/]*)";
// Create a regex to match any library name.
cmStdString reg_any = reg;
reg_any += libext;
#ifdef CM_ORDER_LINK_DIRECTORIES_DEBUG
fprintf(stderr, "any regex [%s]\n", reg_any.c_str());
#endif
this->ExtractAnyLibraryName.compile(reg_any.c_str());
// Create a regex to match static library names.
if(!this->StaticLinkExtensions.empty())
{
cmStdString reg_static = reg;
reg_static += this->CreateExtensionRegex(this->StaticLinkExtensions);
#ifdef CM_ORDER_LINK_DIRECTORIES_DEBUG
fprintf(stderr, "static regex [%s]\n", reg_static.c_str());
#endif
this->ExtractStaticLibraryName.compile(reg_static.c_str());
}
// Create a regex to match shared library names.
if(!this->SharedLinkExtensions.empty())
{
cmStdString reg_shared = reg;
reg_shared += this->CreateExtensionRegex(this->SharedLinkExtensions);
#ifdef CM_ORDER_LINK_DIRECTORIES_DEBUG
fprintf(stderr, "shared regex [%s]\n", reg_shared.c_str());
#endif
this->ExtractSharedLibraryName.compile(reg_shared.c_str());
}
}
//-------------------------------------------------------------------
std::string
cmOrderLinkDirectories::CreateExtensionRegex(
std::vector<cmStdString> const& exts)
{
// Build a list of extension choices.
cmStdString libext = "(";
const char* sep = "";
for(std::vector<cmStdString>::const_iterator i = exts.begin();
i != exts.end(); ++i)
{
// Separate this choice from the previous one.
libext += sep;
sep = "|";
// Store this extension choice with the "." escaped.
libext += "\\";
#if defined(_WIN32) && !defined(__CYGWIN__)
libext += this->NoCaseExpression(i->c_str());
@ -143,41 +251,65 @@ void cmOrderLinkDirectories::CreateRegularExpressions()
libext += *i;
#endif
}
libext += ").*";
cmStdString reg("(.*)");
reg += libext;
this->RemoveLibraryExtension.compile(reg.c_str());
reg = "";
if(this->LinkPrefix.size())
{
reg = "^";
reg += this->LinkPrefix;
}
reg += "([^/]*)";
reg += libext;
this->ExtractBaseLibraryName.compile(reg.c_str());
reg = "([^/]*)";
reg += libext;
this->ExtractBaseLibraryNameNoPrefix.compile(reg.c_str());
}
// Finish the list.
libext += ").*";
return libext;
}
//-------------------------------------------------------------------
void cmOrderLinkDirectories::PrepareLinkTargets()
{
for(std::vector<cmStdString>::iterator i = this->LinkItems.begin();
i != this->LinkItems.end(); ++i)
std::vector<cmStdString> originalLinkItems = this->LinkItems;
this->LinkItems.clear();
this->CurrentLinkType = this->StartLinkType;
for(std::vector<cmStdString>::iterator i = originalLinkItems.begin();
i != originalLinkItems.end(); ++i)
{
// separate the library name from libfoo.a or foo.a
if(this->ExtractBaseLibraryName.find(*i))
if(this->ExtractStaticLibraryName.find(*i))
{
*i = this->ExtractBaseLibraryName.match(1);
#ifdef CM_ORDER_LINK_DIRECTORIES_DEBUG
fprintf(stderr, "static regex matched [%s] [%s] [%s]\n",
this->ExtractStaticLibraryName.match(1).c_str(),
this->ExtractStaticLibraryName.match(2).c_str(),
this->ExtractStaticLibraryName.match(3).c_str());
#endif
this->SetCurrentLinkType(LinkStatic);
this->LinkItems.push_back(this->ExtractStaticLibraryName.match(2));
}
else if(this->ExtractBaseLibraryNameNoPrefix.find(*i))
else if(this->ExtractSharedLibraryName.find(*i))
{
*i = this->ExtractBaseLibraryNameNoPrefix.match(1);
#ifdef CM_ORDER_LINK_DIRECTORIES_DEBUG
fprintf(stderr, "shared regex matched [%s] [%s] [%s]\n",
this->ExtractSharedLibraryName.match(1).c_str(),
this->ExtractSharedLibraryName.match(2).c_str(),
this->ExtractSharedLibraryName.match(3).c_str());
#endif
this->SetCurrentLinkType(LinkShared);
this->LinkItems.push_back(this->ExtractSharedLibraryName.match(2));
}
else if(this->ExtractAnyLibraryName.find(*i))
{
#ifdef CM_ORDER_LINK_DIRECTORIES_DEBUG
fprintf(stderr, "any regex matched [%s] [%s] [%s]\n",
this->ExtractAnyLibraryName.match(1).c_str(),
this->ExtractAnyLibraryName.match(2).c_str(),
this->ExtractAnyLibraryName.match(3).c_str());
#endif
this->SetCurrentLinkType(this->StartLinkType);
this->LinkItems.push_back(this->ExtractAnyLibraryName.match(2));
}
else
{
this->SetCurrentLinkType(this->StartLinkType);
this->LinkItems.push_back(*i);
}
}
// Restore the original linking type so system runtime libraries are
// linked properly.
this->SetCurrentLinkType(this->StartLinkType);
}
//-------------------------------------------------------------------
@ -318,6 +450,9 @@ bool cmOrderLinkDirectories::DetermineLibraryPathOrder()
for(unsigned int i=0; i < this->RawLinkItems.size(); ++i)
{
bool framework = false;
#ifdef CM_ORDER_LINK_DIRECTORIES_DEBUG
fprintf(stderr, "Raw link item [%s]\n", this->RawLinkItems[i].c_str());
#endif
if(cmSystemTools::FileIsFullPath(this->RawLinkItems[i].c_str()))
{
if(cmSystemTools::FileIsDirectory(this->RawLinkItems[i].c_str()))
@ -378,6 +513,9 @@ bool cmOrderLinkDirectories::DetermineLibraryPathOrder()
aLib.File = file;
aLib.Path = dir;
this->FullPathLibraries[aLib.FullPath] = aLib;
#ifdef CM_ORDER_LINK_DIRECTORIES_DEBUG
fprintf(stderr, "Storing item [%s]\n", file.c_str());
#endif
this->LinkItems.push_back(file);
}
}

View File

@ -66,10 +66,19 @@ public:
// should be set from CMAKE_STATIC_LIBRARY_SUFFIX,
// CMAKE_SHARED_LIBRARY_SUFFIX
// CMAKE_LINK_LIBRARY_SUFFIX
void AddLinkExtension(const char* e)
enum LinkType { LinkUnknown, LinkStatic, LinkShared };
void AddLinkExtension(const char* e, LinkType type = LinkUnknown)
{
if(e && *e)
{
if(type == LinkStatic)
{
this->StaticLinkExtensions.push_back(e);
}
if(type == LinkShared)
{
this->SharedLinkExtensions.push_back(e);
}
this->LinkExtensions.push_back(e);
}
}
@ -86,6 +95,11 @@ public:
// return a list of all full path libraries
void GetFullPathLibraries(std::vector<cmStdString>& libs);
// Provide flags for switching library link type.
void SetLinkTypeInformation(LinkType start_link_type,
const char* static_link_type_flag,
const char* shared_link_type_flag);
// structure to hold a full path library link item
struct Library
{
@ -101,6 +115,7 @@ public:
private:
void CreateRegularExpressions();
std::string CreateExtensionRegex(std::vector<cmStdString> const& exts);
void DetermineLibraryPathOrder(std::vector<cmStdString>& searchPaths,
std::vector<cmStdString>& libs,
std::vector<cmStdString>& sortedPaths);
@ -145,6 +160,8 @@ private:
// This is the set of -L paths unsorted, but unique
std::set<cmStdString> LinkPathSet;
// the names of link extensions
std::vector<cmStdString> StaticLinkExtensions;
std::vector<cmStdString> SharedLinkExtensions;
std::vector<cmStdString> LinkExtensions;
// the names of link prefixes
cmStdString LinkPrefix;
@ -154,10 +171,20 @@ private:
cmStdString TargetName;
// Subdirectory used for this configuration if any.
cmStdString ConfigSubdir;
// Link type adjustment.
LinkType StartLinkType;
LinkType CurrentLinkType;
cmStdString StaticLinkTypeFlag;
cmStdString SharedLinkTypeFlag;
bool LinkTypeEnabled;
void SetCurrentLinkType(LinkType lt);
// library regular expressions
cmsys::RegularExpression RemoveLibraryExtension;
cmsys::RegularExpression ExtractBaseLibraryName;
cmsys::RegularExpression ExtractBaseLibraryNameNoPrefix;
cmsys::RegularExpression ExtractStaticLibraryName;
cmsys::RegularExpression ExtractSharedLibraryName;
cmsys::RegularExpression ExtractAnyLibraryName;
cmsys::RegularExpression SplitFramework;
bool Debug;
};

View File

@ -24,13 +24,20 @@ CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS == "${CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS}"
CMAKE_SHARED_LIBRARY_LINK_FLAGS == "${CMAKE_SHARED_LIBRARY_LINK_FLAGS}"
CMAKE_SHARED_LIBRARY_RUNTIME_FLAG == "${CMAKE_SHARED_LIBRARY_RUNTIME_FLAG}"
CMAKE_SHARED_LIBRARY_RUNTIME_FLAG_SEP == "${CMAKE_SHARED_LIBRARY_RUNTIME_FLAG_SEP}"
CMAKE_SHARED_LIBRARY_LINK_STATIC_C_FLAGS == "${CMAKE_SHARED_LIBRARY_LINK_STATIC_C_FLAGS}"
CMAKE_SHARED_LIBRARY_LINK_DYNAMIC_C_FLAGS == "${CMAKE_SHARED_LIBRARY_LINK_DYNAMIC_C_FLAGS}"
// C shared module flags
CMAKE_SHARED_MODULE_C_FLAGS == "${CMAKE_SHARED_MODULE_C_FLAGS}"
CMAKE_SHARED_MODULE_CREATE_C_FLAGS == "${CMAKE_SHARED_MODULE_CREATE_C_FLAGS}"
CMAKE_SHARED_MODULE_LINK_FLAGS == "${CMAKE_SHARED_MODULE_LINK_FLAGS}"
CMAKE_SHARED_MODULE_RUNTIME_FLAG == "${CMAKE_SHARED_MODULE_RUNTIME_FLAG}"
CMAKE_SHARED_MODULE_RUNTIME_FLAG_SEP == "${CMAKE_SHARED_MODULE_RUNTIME_FLAG_SEP}"
CMAKE_SHARED_MODULE_LINK_STATIC_C_FLAGS == "${CMAKE_SHARED_MODULE_LINK_STATIC_C_FLAGS}"
CMAKE_SHARED_MODULE_LINK_DYNAMIC_C_FLAGS == "${CMAKE_SHARED_MODULE_LINK_DYNAMIC_C_FLAGS}"
// C exe flags
CMAKE_EXE_LINK_STATIC_C_FLAGS == "${CMAKE_EXE_LINK_STATIC_C_FLAGS}"
CMAKE_EXE_LINK_DYNAMIC_C_FLAGS == "${CMAKE_EXE_LINK_DYNAMIC_C_FLAGS}"
// CXX shared library flags
CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS == "${CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS}"
@ -38,13 +45,20 @@ CMAKE_SHARED_LIBRARY_CXX_FLAGS == "${CMAKE_SHARED_LIBRARY_CXX_FLAGS}"
CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS == "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS}"
CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG == "${CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG}"
CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG_SEP == "${CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG_SEP}"
CMAKE_SHARED_LIBRARY_LINK_STATIC_CXX_FLAGS == "${CMAKE_SHARED_LIBRARY_LINK_STATIC_CXX_FLAGS}"
CMAKE_SHARED_LIBRARY_LINK_DYNAMIC_CXX_FLAGS == "${CMAKE_SHARED_LIBRARY_LINK_DYNAMIC_CXX_FLAGS}"
// CXX shared module flags
CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS == "${CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS}"
CMAKE_SHARED_MODULE_CXX_FLAGS == "${CMAKE_SHARED_MODULE_CXX_FLAGS}"
CMAKE_SHARED_MODULE_LINK_CXX_FLAGS == "${CMAKE_SHARED_MODULE_LINK_CXX_FLAGS}"
CMAKE_SHARED_MODULE_RUNTIME_CXX_FLAG == "${CMAKE_SHARED_MODULE_RUNTIME_CXX_FLAG}"
CMAKE_SHARED_MODULE_RUNTIME_CXX_FLAG_SEP == "${CMAKE_SHARED_MODULE_RUNTIME_CXX_FLAG_SEP}"
CMAKE_SHARED_MODULE_LINK_STATIC_CXX_FLAGS == "${CMAKE_SHARED_MODULE_LINK_STATIC_CXX_FLAGS}"
CMAKE_SHARED_MODULE_LINK_DYNAMIC_CXX_FLAGS == "${CMAKE_SHARED_MODULE_LINK_DYNAMIC_CXX_FLAGS}"
// CXX exe flags
CMAKE_EXE_LINK_STATIC_CXX_FLAGS == "${CMAKE_EXE_LINK_STATIC_CXX_FLAGS}"
CMAKE_EXE_LINK_DYNAMIC_CXX_FLAGS == "${CMAKE_EXE_LINK_DYNAMIC_CXX_FLAGS}"
CMAKE_USER_MAKE_RULES_OVERRIDE == "${CMAKE_USER_MAKE_RULES_OVERRIDE}"
CMAKE_VERBOSE_MAKEFILE == "${CMAKE_VERBOSE_MAKEFILE}"