Browse Source

target_link_libraries: Fix static library private deps in other dirs

In commit a1ad0a699b (target_link_libraries: Allow use with targets in
other directories, 2018-09-07, v3.13.0-rc1~94^2) we accidentally broke
the logic that adds `$<LINK_ONLY:...>` to private dependencies of static
libraries in their `INTERFACE_LINK_LIBRARIES` in the case that the
dependency is added from outside the directory creating the library.
The check for a valid target name should apply to the original name
specified by the caller and not the encoded cross-directory reference.

Fixes: #19197
Brad King 6 years ago
parent
commit
3d3713121b

+ 2 - 2
Source/cmTargetLinkLibrariesCommand.cxx

@@ -452,8 +452,8 @@ bool cmTargetLinkLibrariesCommand::HandleLibrary(const std::string& lib,
     if (this->Target->GetType() == cmStateEnums::STATIC_LIBRARY) {
       std::string configLib =
         this->Target->GetDebugGeneratorExpressions(libRef, llt);
-      if (cmGeneratorExpression::IsValidTargetName(libRef) ||
-          cmGeneratorExpression::Find(libRef) != std::string::npos) {
+      if (cmGeneratorExpression::IsValidTargetName(lib) ||
+          cmGeneratorExpression::Find(lib) != std::string::npos) {
         configLib = "$<LINK_ONLY:" + configLib + ">";
       }
       this->Target->AppendProperty("INTERFACE_LINK_LIBRARIES",

+ 4 - 0
Tests/CMakeCommands/target_link_libraries/CMakeLists.txt

@@ -134,11 +134,15 @@ assert_property(newsignature1 LINK_LIBRARIES "depC;depB;subdirlib")
 #----------------------------------------------------------------------------
 # Test cross-directory linking.
 cmake_policy(PUSH)
+cmake_policy(SET CMP0022 NEW)
 cmake_policy(SET CMP0079 NEW)
 add_executable(TopDir TopDir.c)
 add_subdirectory(SubDirA)
 add_subdirectory(SubDirB)
 target_link_libraries(SubDirB TopDirImported)
+add_subdirectory(SubDirC)
+target_link_libraries(SubDirC PRIVATE SubDirC2)
+target_link_libraries(TopDir SubDirC)
 add_library(TopDirImported IMPORTED INTERFACE)
 target_compile_definitions(TopDirImported INTERFACE DEF_TopDirImported)
 cmake_policy(POP)

+ 9 - 0
Tests/CMakeCommands/target_link_libraries/SubDirC/CMakeLists.txt

@@ -0,0 +1,9 @@
+add_library(SubDirC STATIC SubDirC.c)
+
+add_library(SubDirC1 INTERFACE)
+target_compile_definitions(SubDirC1 INTERFACE DEF_SubDirC1)
+
+add_library(SubDirC2 INTERFACE)
+target_compile_definitions(SubDirC2 INTERFACE DEF_SubDirC2)
+
+target_link_libraries(SubDirC PRIVATE SubDirC1)

+ 11 - 0
Tests/CMakeCommands/target_link_libraries/SubDirC/SubDirC.c

@@ -0,0 +1,11 @@
+#ifndef DEF_SubDirC1
+#  error "DEF_SubDirC1 not defined"
+#endif
+#ifndef DEF_SubDirC2
+#  error "DEF_SubDirC2 not defined"
+#endif
+
+int SubDirC(void)
+{
+  return 0;
+}

+ 6 - 0
Tests/CMakeCommands/target_link_libraries/TopDir.c

@@ -7,6 +7,12 @@
 #ifdef DEF_TopDirImported
 #  error "DEF_TopDirImported is defined but should not be!"
 #endif
+#ifdef DEF_SubDirC1
+#  error "DEF_SubDirC1 defined but should not be"
+#endif
+#ifdef DEF_SubDirC2
+#  error "DEF_SubDirC2 defined but should not be"
+#endif
 
 int main(void)
 {