From 6d34cc3cb8062647030df9bf7a890615844d3d8f Mon Sep 17 00:00:00 2001 From: Brad King Date: Fri, 1 Apr 2022 13:20:21 -0400 Subject: export: Fix TARGET_NAME lookup for out-of-dir linking Improve the logic added by commit f0e67da061 (target_link_libraries: Fix out-of-dir linking of a list of targets, 2020-01-14, v3.17.0-rc1~149^2) to, while exporting targets, look up referenced target names in the correct directory. --- Source/cmExportFileGenerator.cxx | 28 ++++++++++++---------- Source/cmExportFileGenerator.h | 4 +++- Source/cmGeneratorTarget.h | 6 ++--- .../ExportImport/Export/SubDirLinkA/CMakeLists.txt | 5 +++- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/Source/cmExportFileGenerator.cxx b/Source/cmExportFileGenerator.cxx index a21e614..dc94e76 100644 --- a/Source/cmExportFileGenerator.cxx +++ b/Source/cmExportFileGenerator.cxx @@ -16,7 +16,6 @@ #include "cmFileSet.h" #include "cmGeneratedFileStream.h" #include "cmGeneratorTarget.h" -#include "cmGlobalGenerator.h" #include "cmLinkItem.h" #include "cmLocalGenerator.h" #include "cmMakefile.h" @@ -607,10 +606,10 @@ void cmExportFileGenerator::GenerateInterfaceProperties( bool cmExportFileGenerator::AddTargetNamespace( std::string& input, cmGeneratorTarget const* target, - std::vector& missingTargets) + cmLocalGenerator const* lg, std::vector& missingTargets) { cmGeneratorTarget::TargetOrString resolved = - target->ResolveTargetReference(input); + target->ResolveTargetReference(input, lg); cmGeneratorTarget* tgt = resolved.Target; if (!tgt) { @@ -640,8 +639,10 @@ void cmExportFileGenerator::ResolveTargetsInGeneratorExpressions( std::string& input, cmGeneratorTarget const* target, std::vector& missingTargets, FreeTargetsReplace replace) { + cmLocalGenerator const* lg = target->GetLocalGenerator(); if (replace == NoReplaceFreeTargets) { - this->ResolveTargetsInGeneratorExpression(input, target, missingTargets); + this->ResolveTargetsInGeneratorExpression(input, target, lg, + missingTargets); return; } std::vector parts; @@ -650,13 +651,14 @@ void cmExportFileGenerator::ResolveTargetsInGeneratorExpressions( std::string sep; input.clear(); for (std::string& li : parts) { - if (cmHasLiteralPrefix(li, CMAKE_DIRECTORY_ID_SEP)) { + if (target->IsLinkLookupScope(li, lg)) { continue; } if (cmGeneratorExpression::Find(li) == std::string::npos) { - this->AddTargetNamespace(li, target, missingTargets); + this->AddTargetNamespace(li, target, lg, missingTargets); } else { - this->ResolveTargetsInGeneratorExpression(li, target, missingTargets); + this->ResolveTargetsInGeneratorExpression(li, target, lg, + missingTargets); } input += sep + li; sep = ";"; @@ -665,7 +667,7 @@ void cmExportFileGenerator::ResolveTargetsInGeneratorExpressions( void cmExportFileGenerator::ResolveTargetsInGeneratorExpression( std::string& input, cmGeneratorTarget const* target, - std::vector& missingTargets) + cmLocalGenerator const* lg, std::vector& missingTargets) { std::string::size_type pos = 0; std::string::size_type lastPos = pos; @@ -689,7 +691,7 @@ void cmExportFileGenerator::ResolveTargetsInGeneratorExpression( std::string targetName = input.substr(nameStartPos, commaPos - nameStartPos); - if (this->AddTargetNamespace(targetName, target, missingTargets)) { + if (this->AddTargetNamespace(targetName, target, lg, missingTargets)) { input.replace(nameStartPos, commaPos - nameStartPos, targetName); } lastPos = nameStartPos + targetName.size() + 1; @@ -711,7 +713,7 @@ void cmExportFileGenerator::ResolveTargetsInGeneratorExpression( "literal."; break; } - if (!this->AddTargetNamespace(targetName, target, missingTargets)) { + if (!this->AddTargetNamespace(targetName, target, lg, missingTargets)) { errorString = "$ requires its parameter to be a " "reachable target."; break; @@ -732,7 +734,7 @@ void cmExportFileGenerator::ResolveTargetsInGeneratorExpression( } std::string libName = input.substr(nameStartPos, endPos - nameStartPos); if (cmGeneratorExpression::IsValidTargetName(libName) && - this->AddTargetNamespace(libName, target, missingTargets)) { + this->AddTargetNamespace(libName, target, lg, missingTargets)) { input.replace(nameStartPos, endPos - nameStartPos, libName); } lastPos = nameStartPos + libName.size() + 1; @@ -900,6 +902,8 @@ void cmExportFileGenerator::SetImportLinkProperty( return; } + cmLocalGenerator const* lg = target->GetLocalGenerator(); + // Construct the property value. std::string link_entries; const char* sep = ""; @@ -910,7 +914,7 @@ void cmExportFileGenerator::SetImportLinkProperty( if (targetNames == ImportLinkPropertyTargetNames::Yes) { std::string temp = asString(l); - this->AddTargetNamespace(temp, target, missingTargets); + this->AddTargetNamespace(temp, target, lg, missingTargets); link_entries += temp; } else { link_entries += asString(l); diff --git a/Source/cmExportFileGenerator.h b/Source/cmExportFileGenerator.h index d50f7e8..bde6f1b 100644 --- a/Source/cmExportFileGenerator.h +++ b/Source/cmExportFileGenerator.h @@ -17,6 +17,7 @@ class cmFileSet; class cmGeneratorTarget; +class cmLocalGenerator; class cmTargetExport; #define STRINGIFY_HELPER(X) #X @@ -223,11 +224,12 @@ private: std::vector& missingTargets); bool AddTargetNamespace(std::string& input, cmGeneratorTarget const* target, + cmLocalGenerator const* lg, std::vector& missingTargets); void ResolveTargetsInGeneratorExpression( std::string& input, cmGeneratorTarget const* target, - std::vector& missingTargets); + cmLocalGenerator const* lg, std::vector& missingTargets); virtual void ReplaceInstallPrefix(std::string& input); diff --git a/Source/cmGeneratorTarget.h b/Source/cmGeneratorTarget.h index 53844ae..b927848 100644 --- a/Source/cmGeneratorTarget.h +++ b/Source/cmGeneratorTarget.h @@ -856,6 +856,9 @@ public: std::string GetFortranModuleDirectory(std::string const& working_dir) const; bool IsFortranBuildingInstrinsicModules() const; + bool IsLinkLookupScope(std::string const& n, + cmLocalGenerator const*& lg) const; + cmValue GetSourcesProperty() const; void AddISPCGeneratedHeader(std::string const& header, @@ -1073,9 +1076,6 @@ private: std::unordered_map> ISPCGeneratedObjects; - bool IsLinkLookupScope(std::string const& n, - cmLocalGenerator const*& lg) const; - enum class LinkInterfaceField { Libraries, diff --git a/Tests/ExportImport/Export/SubDirLinkA/CMakeLists.txt b/Tests/ExportImport/Export/SubDirLinkA/CMakeLists.txt index 1aa41d2..ccba264 100644 --- a/Tests/ExportImport/Export/SubDirLinkA/CMakeLists.txt +++ b/Tests/ExportImport/Export/SubDirLinkA/CMakeLists.txt @@ -1,6 +1,9 @@ add_library(SubDirLinkAImported IMPORTED INTERFACE) target_compile_definitions(SubDirLinkAImported INTERFACE DEF_SubDirLinkAImportedForExport) -target_link_libraries(TopDirLib PUBLIC debug "$<1:SubDirLinkAImported;SubDirLinkAImported>" optimized "$<1:SubDirLinkAImported;SubDirLinkAImported>") +target_link_libraries(TopDirLib PUBLIC + debug "$<1:$;$>" + optimized "$<1:$;$>" +) add_library(SubDirLinkA STATIC SubDirLinkA.c) -- cgit v0.12