target_link_libraries: Allow use with targets in other directories

Previously the command did not allow naming targets on the LHS that
were not created in the calling directory.  Lift this restriction to
enable more flexible use by projects.

Fixes: #17943
This commit is contained in:
Patrick Stotko
2018-05-11 18:06:53 +02:00
committed by Brad King
parent 743f24bac6
commit c9349cc1b9
19 changed files with 110 additions and 10 deletions

View File

@@ -78,6 +78,7 @@ The following individuals and institutions are among the Contributors:
* Nikita Krupen'ko <krnekit@gmail.com>
* NVIDIA Corporation <www.nvidia.com>
* OpenGamma Ltd. <opengamma.com>
* Patrick Stotko <stotko@cs.uni-bonn.de>
* Per Øyvind Karlsen <peroyvind@mandriva.org>
* Peter Collingbourne <peter@pcc.me.uk>
* Petr Gotthard <gotthard@honeywell.com>

View File

@@ -18,7 +18,7 @@ All of them have the general form::
target_link_libraries(<target> ... <item>... ...)
The named ``<target>`` must have been created in the current directory by
The named ``<target>`` must have been created by
a command such as :command:`add_executable` or :command:`add_library` and
must not be an :ref:`ALIAS target <Alias Targets>`.
Repeated calls for the same ``<target>`` append items in the order called.

View File

@@ -0,0 +1,5 @@
subdirectory-linking
--------------------
* The :command:`target_link_libraries` command may now be called
to modify targets created outside the current directory.

View File

@@ -364,7 +364,7 @@ bool cmTargetLinkLibrariesCommand::HandleLibrary(const std::string& lib,
if (this->CurrentProcessingState != ProcessingKeywordLinkInterface &&
this->CurrentProcessingState != ProcessingPlainLinkInterface) {
// Assure that the target on the LHS was created in the current directory.
// Find target on the LHS locally
cmTarget* t =
this->Makefile->FindLocalNonAliasTarget(this->Target->GetName());
if (!t) {
@@ -377,11 +377,18 @@ bool cmTargetLinkLibrariesCommand::HandleLibrary(const std::string& lib,
}
}
}
// If no local target has been found, find it in the global scope
if (!t) {
t = this->Makefile->GetGlobalGenerator()->FindTarget(
this->Target->GetName(), true);
}
if (!t) {
std::ostringstream e;
e << "Attempt to add link library \"" << lib << "\" to target \""
<< this->Target->GetName()
<< "\" which is not built in this directory.";
<< "\" which does not exist or is an alias target.";
this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
return false;
}

View File

@@ -8,7 +8,10 @@ run_cmake(ImportedTarget)
run_cmake(ImportedTargetFailure)
run_cmake(MixedSignature)
run_cmake(Separate-PRIVATE-LINK_PRIVATE-uses)
run_cmake(SubDirImportedTarget)
run_cmake(SubDirTarget)
run_cmake(SubDirTarget-UNKNOWN-IMPORTED)
run_cmake(SubDirTarget-UNKNOWN-IMPORTED-GLOBAL)
run_cmake(SharedDepNotTarget)
run_cmake(StaticPrivateDepNotExported)
run_cmake(StaticPrivateDepNotTarget)

View File

@@ -0,0 +1,4 @@
-- mainexeUnknownImportedGlobal: mainlib;sublib
-- mainlibUnknownImportedGlobal: mainlib;sublib
-- subexeUnknownImportedGlobal: mainlib;sublib
-- sublibUnknownImportedGlobal: mainlib;sublib

View File

@@ -0,0 +1,17 @@
enable_language(C)
add_executable(mainexeUnknownImportedGlobal IMPORTED GLOBAL)
add_library(mainlibUnknownImportedGlobal UNKNOWN IMPORTED GLOBAL)
add_library(mainlib empty.c)
add_subdirectory(SubDirImportedTarget)
target_link_libraries(subexeUnknownImportedGlobal INTERFACE mainlib)
target_link_libraries(subexeUnknownImportedGlobal INTERFACE sublib)
get_property(subexeUnknownImportedGlobal_libs TARGET subexeUnknownImportedGlobal PROPERTY INTERFACE_LINK_LIBRARIES)
message(STATUS "subexeUnknownImportedGlobal: ${subexeUnknownImportedGlobal_libs}")
target_link_libraries(sublibUnknownImportedGlobal INTERFACE mainlib)
target_link_libraries(sublibUnknownImportedGlobal INTERFACE sublib)
get_property(sublibUnknownImportedGlobal_libs TARGET sublibUnknownImportedGlobal PROPERTY INTERFACE_LINK_LIBRARIES)
message(STATUS "sublibUnknownImportedGlobal: ${sublibUnknownImportedGlobal_libs}")

View File

@@ -0,0 +1,13 @@
add_executable(subexeUnknownImportedGlobal IMPORTED GLOBAL)
add_library(sublibUnknownImportedGlobal UNKNOWN IMPORTED GLOBAL)
add_library(sublib ../empty.c)
target_link_libraries(mainexeUnknownImportedGlobal INTERFACE mainlib)
target_link_libraries(mainexeUnknownImportedGlobal INTERFACE sublib)
get_property(mainexeUnknownImportedGlobal_libs TARGET mainexeUnknownImportedGlobal PROPERTY INTERFACE_LINK_LIBRARIES)
message(STATUS "mainexeUnknownImportedGlobal: ${mainexeUnknownImportedGlobal_libs}")
target_link_libraries(mainlibUnknownImportedGlobal INTERFACE mainlib)
target_link_libraries(mainlibUnknownImportedGlobal INTERFACE sublib)
get_property(mainlibUnknownImportedGlobal_libs TARGET mainlibUnknownImportedGlobal PROPERTY INTERFACE_LINK_LIBRARIES)
message(STATUS "mainlibUnknownImportedGlobal: ${mainlibUnknownImportedGlobal_libs}")

View File

@@ -0,0 +1,2 @@
-- mainexe: mainlibUnknownImportedGlobal;sublibUnknownImportedGlobal
-- subexe: mainlibUnknownImportedGlobal;sublibUnknownImportedGlobal

View File

@@ -0,0 +1,11 @@
enable_language(C)
add_executable(mainexe empty.c)
add_library(mainlibUnknownImportedGlobal UNKNOWN IMPORTED GLOBAL)
add_subdirectory(SubDirTarget-UNKNOWN-IMPORTED-GLOBAL)
target_link_libraries(subexe mainlibUnknownImportedGlobal)
target_link_libraries(subexe sublibUnknownImportedGlobal)
get_property(subexe_libs TARGET subexe PROPERTY INTERFACE_LINK_LIBRARIES)
message(STATUS "subexe: ${subexe_libs}")

View File

@@ -0,0 +1,7 @@
add_executable(subexe ../empty.c)
add_library(sublibUnknownImportedGlobal UNKNOWN IMPORTED GLOBAL)
target_link_libraries(mainexe mainlibUnknownImportedGlobal)
target_link_libraries(mainexe sublibUnknownImportedGlobal)
get_property(mainexe_libs TARGET mainexe PROPERTY INTERFACE_LINK_LIBRARIES)
message(STATUS "mainexe: ${mainexe_libs}")

View File

@@ -0,0 +1,2 @@
-- mainexe: mainlibUnknownImported;sublibUnknownImported
-- subexe: mainlibUnknownImported;sublibUnknownImported

View File

@@ -0,0 +1,11 @@
enable_language(C)
add_executable(mainexe empty.c)
add_library(mainlibUnknownImported UNKNOWN IMPORTED)
add_subdirectory(SubDirTarget-UNKNOWN-IMPORTED)
target_link_libraries(subexe mainlibUnknownImported)
target_link_libraries(subexe sublibUnknownImported)
get_property(subexe_libs TARGET subexe PROPERTY INTERFACE_LINK_LIBRARIES)
message(STATUS "subexe: ${subexe_libs}")

View File

@@ -0,0 +1,7 @@
add_executable(subexe ../empty.c)
add_library(sublibUnknownImported UNKNOWN IMPORTED)
target_link_libraries(mainexe mainlibUnknownImported)
target_link_libraries(mainexe sublibUnknownImported)
get_property(mainexe_libs TARGET mainexe PROPERTY INTERFACE_LINK_LIBRARIES)
message(STATUS "mainexe: ${mainexe_libs}")

View File

@@ -1,5 +0,0 @@
^CMake Error at SubDirTarget.cmake:[0-9]+ \(target_link_libraries\):
Attempt to add link library "m" to target "subexe" which is not built in
this directory.
Call Stack \(most recent call first\):
CMakeLists.txt:[0-9]+ \(include\)$

View File

@@ -0,0 +1,2 @@
-- mainexe: mainlib;sublib
-- subexe: mainlib;sublib

View File

@@ -1,3 +1,11 @@
enable_language(C)
add_executable(mainexe empty.c)
add_library(mainlib empty.c)
add_subdirectory(SubDirTarget)
target_link_libraries(subexe m)
target_link_libraries(subexe mainlib)
target_link_libraries(subexe sublib)
get_property(subexe_libs TARGET subexe PROPERTY INTERFACE_LINK_LIBRARIES)
message(STATUS "subexe: ${subexe_libs}")

View File

@@ -1 +1,7 @@
add_executable(subexe ../empty.c)
add_library(sublib ../empty.c)
target_link_libraries(mainexe mainlib)
target_link_libraries(mainexe sublib)
get_property(mainexe_libs TARGET mainexe PROPERTY INTERFACE_LINK_LIBRARIES)
message(STATUS "mainexe: ${mainexe_libs}")