ENH: Add UNKNOWN type for IMPORTED libraries

When creating an IMPORTED target for a library that has been found on
disk, it may not be known whether the library is STATIC or SHARED.
However, the library may still be linked using the file found from disk.
Use of an IMPORTED target is still important to allow per-configuration
files to be specified for the library.

This change creates an UNKNOWN type for IMPORTED library targets.  The
IMPORTED_LOCATION property (and its per-config equivalents) specifies
the location of the library.  CMake makes no assumptions about the
library that cannot be inferred from the file on disk.  This will help
projects and find-modules import targets found on disk or specified by
the user.
This commit is contained in:
Brad King 2008-08-18 11:39:22 -04:00
parent 0bf093fd17
commit 061d20be38
9 changed files with 70 additions and 12 deletions

View File

@ -68,6 +68,12 @@ bool cmAddLibraryCommand
type = cmTarget::MODULE_LIBRARY;
haveSpecifiedType = true;
}
else if(libType == "UNKNOWN")
{
++s;
type = cmTarget::UNKNOWN_LIBRARY;
haveSpecifiedType = true;
}
else if(*s == "EXCLUDE_FROM_ALL")
{
++s;
@ -127,6 +133,16 @@ bool cmAddLibraryCommand
return true;
}
// A non-imported target may not have UNKNOWN type.
if(type == cmTarget::UNKNOWN_LIBRARY)
{
this->Makefile->IssueMessage(
cmake::FATAL_ERROR,
"The UNKNOWN library type may be used only for IMPORTED libraries."
);
return true;
}
// Enforce name uniqueness.
{
std::string msg;

View File

@ -100,7 +100,7 @@ public:
"\n"
"The add_library command can also create IMPORTED library "
"targets using this signature:\n"
" add_library(<name> <SHARED|STATIC|MODULE> IMPORTED)\n"
" add_library(<name> <SHARED|STATIC|MODULE|UNKNOWN> IMPORTED)\n"
"An IMPORTED library target references a library file located "
"outside the project. "
"No rules are generated to build it. "

View File

@ -890,6 +890,14 @@ void cmComputeLinkDepends::CheckWrongConfigItem(std::string const& item)
}
}
//----------------------------------------------------------------------------
static bool cmComputeLinkDependsNotStatic(cmTarget* tgt)
{
return (tgt &&
tgt->GetType() != cmTarget::STATIC_LIBRARY &&
tgt->GetType() != cmTarget::UNKNOWN_LIBRARY);
}
//----------------------------------------------------------------------------
void cmComputeLinkDepends::PreserveOriginalEntries()
{
@ -901,7 +909,7 @@ void cmComputeLinkDepends::PreserveOriginalEntries()
out != this->FinalLinkOrder.end())
{
cmTarget* tgt = this->EntryList[*in].Target;
if(tgt && tgt->GetType() != cmTarget::STATIC_LIBRARY)
if(cmComputeLinkDependsNotStatic(tgt))
{
// Skip input items known to not be static libraries.
++in;
@ -924,7 +932,7 @@ void cmComputeLinkDepends::PreserveOriginalEntries()
while(in != this->OriginalEntries.end())
{
cmTarget* tgt = this->EntryList[*in].Target;
if(tgt && tgt->GetType() != cmTarget::STATIC_LIBRARY)
if(cmComputeLinkDependsNotStatic(tgt))
{
// Skip input items known to not be static libraries.
++in;

View File

@ -581,10 +581,7 @@ void cmComputeLinkInformation::AddItem(std::string const& item, cmTarget* tgt)
return;
}
if(tgt && (tgt->GetType() == cmTarget::STATIC_LIBRARY ||
tgt->GetType() == cmTarget::SHARED_LIBRARY ||
tgt->GetType() == cmTarget::MODULE_LIBRARY ||
impexe))
if(tgt && tgt->IsLinkable())
{
// This is a CMake target. Ask the target for its real name.
if(impexe && this->LoaderFlag)
@ -1555,6 +1552,14 @@ void
cmComputeLinkInformation::AddLibraryRuntimeInfo(std::string const& fullPath,
cmTarget* target)
{
// Libraries with unknown type must be handled using just the file
// on disk.
if(target->GetType() == cmTarget::UNKNOWN_LIBRARY)
{
this->AddLibraryRuntimeInfo(fullPath);
return;
}
// Skip targets that are not shared libraries (modules cannot be linked).
if(target->GetType() != cmTarget::SHARED_LIBRARY)
{

View File

@ -1706,6 +1706,7 @@ std::string cmLocalGenerator::GetRealDependency(const char* inName,
case cmTarget::STATIC_LIBRARY:
case cmTarget::SHARED_LIBRARY:
case cmTarget::MODULE_LIBRARY:
case cmTarget::UNKNOWN_LIBRARY:
{
// Get the location of the target's output file and depend on it.
if(const char* location = target->GetLocation(config))

View File

@ -30,7 +30,8 @@
const char* cmTarget::TargetTypeNames[] = {
"EXECUTABLE", "STATIC_LIBRARY",
"SHARED_LIBRARY", "MODULE_LIBRARY", "UTILITY", "GLOBAL_TARGET",
"INSTALL_FILES", "INSTALL_PROGRAMS", "INSTALL_DIRECTORY"
"INSTALL_FILES", "INSTALL_PROGRAMS", "INSTALL_DIRECTORY",
"UNKNOWN_LIBRARY"
};
//----------------------------------------------------------------------------
@ -277,6 +278,7 @@ void cmTarget::DefineProperties(cmake *cm)
"For frameworks on OS X this is the location of the library file "
"symlink just inside the framework folder. "
"For DLLs this is the location of the \".dll\" part of the library. "
"For UNKNOWN libraries this is the location of the file to be linked. "
"Ignored for non-imported targets.");
cm->DefineProperty
@ -786,6 +788,16 @@ bool cmTarget::IsExecutableWithExports()
this->GetPropertyAsBool("ENABLE_EXPORTS"));
}
//----------------------------------------------------------------------------
bool cmTarget::IsLinkable()
{
return (this->GetType() == cmTarget::STATIC_LIBRARY ||
this->GetType() == cmTarget::SHARED_LIBRARY ||
this->GetType() == cmTarget::MODULE_LIBRARY ||
this->GetType() == cmTarget::UNKNOWN_LIBRARY ||
this->IsExecutableWithExports());
}
//----------------------------------------------------------------------------
bool cmTarget::IsFrameworkOnApple()
{
@ -1860,7 +1872,8 @@ const char *cmTarget::GetProperty(const char* prop,
if(this->GetType() == cmTarget::EXECUTABLE ||
this->GetType() == cmTarget::STATIC_LIBRARY ||
this->GetType() == cmTarget::SHARED_LIBRARY ||
this->GetType() == cmTarget::MODULE_LIBRARY)
this->GetType() == cmTarget::MODULE_LIBRARY ||
this->GetType() == cmTarget::UNKNOWN_LIBRARY)
{
if(!this->IsImported() && strcmp(prop,"LOCATION") == 0)
{
@ -1957,6 +1970,9 @@ const char *cmTarget::GetProperty(const char* prop,
case cmTarget::INSTALL_DIRECTORY:
return "INSTALL_DIRECTORY";
// break; /* unreachable */
case cmTarget::UNKNOWN_LIBRARY:
return "UNKNOWN_LIBRARY";
// break; /* unreachable */
}
return 0;
}

View File

@ -80,7 +80,8 @@ public:
cmTarget();
enum TargetType { EXECUTABLE, STATIC_LIBRARY,
SHARED_LIBRARY, MODULE_LIBRARY, UTILITY, GLOBAL_TARGET,
INSTALL_FILES, INSTALL_PROGRAMS, INSTALL_DIRECTORY};
INSTALL_FILES, INSTALL_PROGRAMS, INSTALL_DIRECTORY,
UNKNOWN_LIBRARY};
static const char* TargetTypeNames[];
enum CustomCommandType { PRE_BUILD, PRE_LINK, POST_BUILD };
@ -393,6 +394,9 @@ public:
enabled. */
bool IsExecutableWithExports();
/** Return whether this target may be used to link another target. */
bool IsLinkable();
/** Return whether this target is a shared library Framework on
Apple. */
bool IsFrameworkOnApple();

View File

@ -66,9 +66,13 @@ IF(STAGE2)
PATHS ${LIBPATHS}
DOC "Fourth library")
# Test importing a library found on disk.
ADD_LIBRARY(lib_test4 UNKNOWN IMPORTED)
SET_PROPERTY(TARGET lib_test4 PROPERTY IMPORTED_LOCATION ${TEST4_LIBRARY})
INCLUDE_DIRECTORIES(${CMAKE_INSTALL_PREFIX}/MyTest/include)
ADD_EXECUTABLE (SimpleInstExeS2 inst2.cxx foo.c foo.h)
TARGET_LINK_LIBRARIES(SimpleInstExeS2 ${TEST1_LIBRARY} ${TEST2_LIBRARY} ${TEST4_LIBRARY})
TARGET_LINK_LIBRARIES(SimpleInstExeS2 ${TEST1_LIBRARY} ${TEST2_LIBRARY} lib_test4)
SET(install_target SimpleInstExeS2)
IF("${TEST1_LIBRARY}" MATCHES "static")

View File

@ -66,9 +66,13 @@ IF(STAGE2)
PATHS ${LIBPATHS}
DOC "Fourth library")
# Test importing a library found on disk.
ADD_LIBRARY(lib_test4 UNKNOWN IMPORTED)
SET_PROPERTY(TARGET lib_test4 PROPERTY IMPORTED_LOCATION ${TEST4_LIBRARY})
INCLUDE_DIRECTORIES(${CMAKE_INSTALL_PREFIX}/MyTest/include)
ADD_EXECUTABLE (SimpleInstExeS2 inst2.cxx foo.c foo.h)
TARGET_LINK_LIBRARIES(SimpleInstExeS2 ${TEST1_LIBRARY} ${TEST2_LIBRARY} ${TEST4_LIBRARY})
TARGET_LINK_LIBRARIES(SimpleInstExeS2 ${TEST1_LIBRARY} ${TEST2_LIBRARY} lib_test4)
SET(install_target SimpleInstExeS2)
IF("${TEST1_LIBRARY}" MATCHES "static")