Install: Refactor ResolveTargetsInGeneratorExpression

This commit is contained in:
Tom Osika
2026-05-24 18:28:07 -04:00
parent 7c31cbf9ae
commit 9669829573
2 changed files with 40 additions and 11 deletions

View File

@@ -4,10 +4,12 @@
#include <array>
#include <cstddef>
#include <functional>
#include <sstream>
#include <utility>
#include <cm/memory>
#include <cm/optional>
#include <cm/string_view>
#include <cmext/string_view>
@@ -449,9 +451,9 @@ void cmExportFileGenerator::ResolveTargetsInGeneratorExpressions(
}
}
void cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
std::string& input, cmGeneratorTarget const* target,
cmLocalGenerator const* lg)
cm::optional<std::string> cmResolveTargetsInGeneratorExpression(
std::string& input,
std::function<bool(std::string& name)> const& addTargetNamespace)
{
std::string::size_type pos = 0;
std::string::size_type lastPos = pos;
@@ -474,7 +476,7 @@ void cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
std::string targetName =
input.substr(nameStartPos, commaPos - nameStartPos);
if (this->AddTargetNamespace(targetName, target, lg)) {
if (addTargetNamespace(targetName)) {
input.replace(nameStartPos, commaPos - nameStartPos, targetName);
}
lastPos = nameStartPos + targetName.size() + 1;
@@ -496,7 +498,7 @@ void cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
"literal.";
break;
}
if (!this->AddTargetNamespace(targetName, target, lg)) {
if (!addTargetNamespace(targetName)) {
errorString = "$<TARGET_NAME:...> requires its parameter to be a "
"reachable target.";
break;
@@ -517,7 +519,7 @@ void cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
}
std::string libName = input.substr(nameStartPos, endPos - nameStartPos);
if (cmGeneratorExpression::IsValidTargetName(libName) &&
this->AddTargetNamespace(libName, target, lg)) {
addTargetNamespace(libName)) {
input.replace(nameStartPos, endPos - nameStartPos, libName);
}
lastPos = nameStartPos + libName.size() + 1;
@@ -533,17 +535,29 @@ void cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
}
std::string libName = input.substr(nameStartPos, endPos - nameStartPos);
if (cmGeneratorExpression::IsValidTargetName(libName) &&
this->AddTargetNamespace(libName, target, lg)) {
addTargetNamespace(libName)) {
input.replace(nameStartPos, endPos - nameStartPos, libName);
}
lastPos = nameStartPos + libName.size() + 1;
}
this->ReplaceInstallPrefix(input);
if (!errorString.empty()) {
target->GetLocalGenerator()->IssueMessage(MessageType::FATAL_ERROR,
errorString);
return errorString;
}
return cm::nullopt;
}
void cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
std::string& input, cmGeneratorTarget const* target,
cmLocalGenerator const* lg)
{
auto err = cmResolveTargetsInGeneratorExpression(
input, [this, target, lg](std::string& name) {
return this->AddTargetNamespace(name, target, lg);
});
this->ReplaceInstallPrefix(input);
if (err) {
target->GetLocalGenerator()->IssueMessage(MessageType::FATAL_ERROR, *err);
}
}

View File

@@ -4,12 +4,14 @@
#include "cmConfigure.h" // IWYU pragma: keep
#include <functional>
#include <iosfwd>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cm/optional>
#include <cm/string_view>
#include "cmDiagnostics.h"
@@ -238,3 +240,16 @@ extern template void cmExportFileGenerator::SetImportLinkProperty<cmLinkItem>(
std::string const&, cmGeneratorTarget const*, std::string const&,
std::vector<cmLinkItem> const&, ImportPropertyMap& properties,
ImportLinkPropertyTargetNames);
/** Walk a generator expression and rewrite the target-name slot of each
`$<TARGET_PROPERTY:>`, `$<TARGET_NAME:>`, `$<LINK_ONLY:>`, and
`$<COMPILE_ONLY:>` construct via the provided callback. The callback
receives the bare target name; if it mutates the name and returns true,
the helper splices the new name back into the genex. Returns the
parse-level error message, if an error was encountered. Callers handle
install-prefix substitution and error reporting themselves. This is used
by the cmExportFileGenerator hierarchy, as well as the cmSbomBuilder
hierarchy. */
cm::optional<std::string> cmResolveTargetsInGeneratorExpression(
std::string& input,
std::function<bool(std::string& name)> const& addTargetNamespace);