From 8178fd43e9480384d914bfb2e4bca19991125801 Mon Sep 17 00:00:00 2001 From: Matthew Woehlke Date: Fri, 26 Jul 2024 16:40:03 -0400 Subject: [PATCH] export: Fix handling of import prefix Fix some additional places in export generation logic that were still hard-coding the import prefix. Change cmGeneratorExpression::Preprocess to take the desired prefix as an argument. (This replaces taking a boolean whether to resolve relative paths; if a non-empty prefix is given, that is used to resolve relative paths, otherwise relative paths are left alone.) This should ensure that import properties always spell the prefix according to the format being generated. --- Source/cmExportInstallFileGenerator.cxx | 41 +++++++++++++------------ Source/cmExportInstallFileGenerator.h | 3 ++ Source/cmGeneratorExpression.cxx | 14 +++++---- Source/cmGeneratorExpression.h | 4 ++- 4 files changed, 35 insertions(+), 27 deletions(-) diff --git a/Source/cmExportInstallFileGenerator.cxx b/Source/cmExportInstallFileGenerator.cxx index e61e8c1e89..7bd754a046 100644 --- a/Source/cmExportInstallFileGenerator.cxx +++ b/Source/cmExportInstallFileGenerator.cxx @@ -83,7 +83,8 @@ std::string cmExportInstallFileGenerator::GetImportXcFrameworkLocation( if (!importedXcFrameworkLocation.empty()) { importedXcFrameworkLocation = cmGeneratorExpression::Preprocess( importedXcFrameworkLocation, - cmGeneratorExpression::PreprocessContext::InstallInterface, true); + cmGeneratorExpression::PreprocessContext::InstallInterface, + this->GetImportPrefixWithSlash()); importedXcFrameworkLocation = cmGeneratorExpression::Evaluate( importedXcFrameworkLocation, targetExport->Target->GetLocalGenerator(), config, targetExport->Target, nullptr, targetExport->Target); @@ -156,7 +157,7 @@ void cmExportInstallFileGenerator::SetImportLocationProperty( std::string value; if (!cmSystemTools::FileIsFullPath(dest)) { // The target is installed relative to the installation prefix. - value = "${_IMPORT_PREFIX}/"; + value = std::string{ this->GetImportPrefixWithSlash() }; } value += dest; value += "/"; @@ -399,10 +400,11 @@ bool isSubDirectory(std::string const& a, std::string const& b) return (cmSystemTools::ComparePath(a, b) || cmSystemTools::IsSubDirectory(a, b)); } +} -bool checkInterfaceDirs(std::string const& prepro, - cmGeneratorTarget const* target, - std::string const& prop) +bool cmExportInstallFileGenerator::CheckInterfaceDirs( + std::string const& prepro, cmGeneratorTarget const* target, + std::string const& prop) const { std::string const& installDir = target->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX"); @@ -423,7 +425,7 @@ bool checkInterfaceDirs(std::string const& prepro, if (genexPos == 0) { continue; } - if (cmHasLiteralPrefix(li, "${_IMPORT_PREFIX}")) { + if (cmHasPrefix(li, this->GetImportPrefixWithSlash())) { continue; } MessageType messageType = MessageType::FATAL_ERROR; @@ -521,7 +523,6 @@ bool checkInterfaceDirs(std::string const& prepro, } return !hadFatalError; } -} void cmExportInstallFileGenerator::PopulateSourcesInterface( cmGeneratorTarget const* gt, @@ -542,12 +543,12 @@ void cmExportInstallFileGenerator::PopulateSourcesInterface( return; } - std::string prepro = - cmGeneratorExpression::Preprocess(*input, preprocessRule, true); + std::string prepro = cmGeneratorExpression::Preprocess( + *input, preprocessRule, this->GetImportPrefixWithSlash()); if (!prepro.empty()) { this->ResolveTargetsInGeneratorExpressions(prepro, gt); - if (!checkInterfaceDirs(prepro, gt, propName)) { + if (!this->CheckInterfaceDirs(prepro, gt, propName)) { return; } properties[propName] = prepro; @@ -571,7 +572,7 @@ void cmExportInstallFileGenerator::PopulateIncludeDirectoriesInterface( std::string dirs = cmGeneratorExpression::Preprocess( cmList::to_string(target->Target->GetInstallIncludeDirectoriesEntries(te)), - preprocessRule, true); + preprocessRule, this->GetImportPrefixWithSlash()); this->ReplaceInstallPrefix(dirs); std::unique_ptr cge = ge.Parse(dirs); std::string exportDirs = @@ -605,12 +606,12 @@ void cmExportInstallFileGenerator::PopulateIncludeDirectoriesInterface( std::string includes = (input ? *input : ""); char const* const sep = input ? ";" : ""; includes += sep + exportDirs; - std::string prepro = - cmGeneratorExpression::Preprocess(includes, preprocessRule, true); + std::string prepro = cmGeneratorExpression::Preprocess( + includes, preprocessRule, this->GetImportPrefixWithSlash()); if (!prepro.empty()) { this->ResolveTargetsInGeneratorExpressions(prepro, target); - if (!checkInterfaceDirs(prepro, target, propName)) { + if (!this->CheckInterfaceDirs(prepro, target, propName)) { return; } properties[propName] = prepro; @@ -636,12 +637,12 @@ void cmExportInstallFileGenerator::PopulateLinkDependsInterface( return; } - std::string prepro = - cmGeneratorExpression::Preprocess(*input, preprocessRule, true); + std::string prepro = cmGeneratorExpression::Preprocess( + *input, preprocessRule, this->GetImportPrefixWithSlash()); if (!prepro.empty()) { this->ResolveTargetsInGeneratorExpressions(prepro, gt); - if (!checkInterfaceDirs(prepro, gt, propName)) { + if (!this->CheckInterfaceDirs(prepro, gt, propName)) { return; } properties[propName] = prepro; @@ -667,12 +668,12 @@ void cmExportInstallFileGenerator::PopulateLinkDirectoriesInterface( return; } - std::string prepro = - cmGeneratorExpression::Preprocess(*input, preprocessRule, true); + std::string prepro = cmGeneratorExpression::Preprocess( + *input, preprocessRule, this->GetImportPrefixWithSlash()); if (!prepro.empty()) { this->ResolveTargetsInGeneratorExpressions(prepro, gt); - if (!checkInterfaceDirs(prepro, gt, propName)) { + if (!this->CheckInterfaceDirs(prepro, gt, propName)) { return; } properties[propName] = prepro; diff --git a/Source/cmExportInstallFileGenerator.h b/Source/cmExportInstallFileGenerator.h index 86c99495c4..f33ecc112b 100644 --- a/Source/cmExportInstallFileGenerator.h +++ b/Source/cmExportInstallFileGenerator.h @@ -145,6 +145,9 @@ protected: std::map> ConfigCxxModuleTargetFiles; private: + bool CheckInterfaceDirs(std::string const& prepro, + cmGeneratorTarget const* target, + std::string const& prop) const; void PopulateCompatibleInterfaceProperties(cmGeneratorTarget const* target, ImportPropertyMap& properties); void PopulateCustomTransitiveInterfaceProperties( diff --git a/Source/cmGeneratorExpression.cxx b/Source/cmGeneratorExpression.cxx index 8e590fa8c7..deb292dc0f 100644 --- a/Source/cmGeneratorExpression.cxx +++ b/Source/cmGeneratorExpression.cxx @@ -7,6 +7,8 @@ #include #include +#include + #include "cmsys/RegularExpression.hxx" #include "cmGeneratorExpressionContext.h" @@ -193,7 +195,7 @@ static std::string stripAllGeneratorExpressions(const std::string& input) } static void prefixItems(const std::string& content, std::string& result, - const std::string& prefix) + const cm::string_view& prefix) { std::vector entries; cmGeneratorExpression::Split(content, entries); @@ -211,7 +213,7 @@ static void prefixItems(const std::string& content, std::string& result, static std::string stripExportInterface( const std::string& input, cmGeneratorExpression::PreprocessContext context, - bool resolveRelative) + cm::string_view importPrefix) { std::string result; @@ -268,8 +270,8 @@ static std::string stripExportInterface( } else if (context == cmGeneratorExpression::InstallInterface && foundGenex == FoundGenex::InstallInterface) { const std::string content = input.substr(pos, c - cStart); - if (resolveRelative) { - prefixItems(content, result, "${_IMPORT_PREFIX}/"); + if (!importPrefix.empty()) { + prefixItems(content, result, importPrefix); } else { result += content; } @@ -359,13 +361,13 @@ void cmGeneratorExpression::Split(const std::string& input, std::string cmGeneratorExpression::Preprocess(const std::string& input, PreprocessContext context, - bool resolveRelative) + cm::string_view importPrefix) { if (context == StripAllGeneratorExpressions) { return stripAllGeneratorExpressions(input); } if (context == BuildInterface || context == InstallInterface) { - return stripExportInterface(input, context, resolveRelative); + return stripExportInterface(input, context, importPrefix); } assert(false && diff --git a/Source/cmGeneratorExpression.h b/Source/cmGeneratorExpression.h index 71855c9f6a..0abd968af6 100644 --- a/Source/cmGeneratorExpression.h +++ b/Source/cmGeneratorExpression.h @@ -11,6 +11,8 @@ #include #include +#include + #include "cmListFileCache.h" #include "cmLocalGenerator.h" @@ -59,7 +61,7 @@ public: static std::string Preprocess(const std::string& input, PreprocessContext context, - bool resolveRelative = false); + cm::string_view importPrefix = {}); static void Split(const std::string& input, std::vector& output);