TargetTypes: Improve use of imported target scope type

Move `ImportedTargetScope` to `cmTargetTypes.h`, enabling it to be used
without dragging in all of `cmMakefile.h`. Use this in more places
instead of `bool global`.
This commit is contained in:
Matthew Woehlke
2026-07-01 14:44:11 -04:00
parent a5a9ec9113
commit f1d7bedc4b
10 changed files with 49 additions and 28 deletions
+3 -1
View File
@@ -140,7 +140,9 @@ bool cmAddExecutableCommand(std::vector<std::string> const& args,
}
// Create the imported target.
mf.AddImportedTarget(exename, cm::TargetType::EXECUTABLE, importGlobal);
mf.AddImportedTarget(exename, cm::TargetType::EXECUTABLE,
importGlobal ? cm::ImportedTargetScope::Global
: cm::ImportedTargetScope::Local);
return true;
}
+4 -1
View File
@@ -287,7 +287,10 @@ bool cmAddLibraryCommand(std::vector<std::string> const& args,
}
// Create the imported target.
cmTarget* target = mf.AddImportedTarget(libName, type, importGlobal);
cmTarget* target =
mf.AddImportedTarget(libName, type,
importGlobal ? cm::ImportedTargetScope::Global
: cm::ImportedTargetScope::Local);
target->SetSymbolic(symbolicTarget);
return true;
}
+2 -1
View File
@@ -1033,7 +1033,8 @@ bool HandleImportCommand(std::vector<std::string> const& args,
mf.AddDefinition(found_var, "TRUE");
auto* tgt =
mf.AddImportedTarget(local_name, cm::TargetType::INTERFACE_LIBRARY, false);
mf.AddImportedTarget(local_name, cm::TargetType::INTERFACE_LIBRARY,
cm::ImportedTargetScope::Local);
tgt->AppendProperty("INTERFACE_LINK_LIBRARIES", foreign_name);
return true;
}
+5 -2
View File
@@ -2079,7 +2079,7 @@ bool cmFindPackageCommand::ReadListFile(std::string const& f,
ps = cm::PolicyScope::None;
}
using ITScope = cmMakefile::ImportedTargetScope;
using ITScope = cm::ImportedTargetScope;
ITScope scope = this->GlobalScope ? ITScope::Global : ITScope::Local;
cmMakefile::SetGlobalTargetImportScope globScope(this->Makefile, scope);
@@ -2242,7 +2242,10 @@ bool cmFindPackageCommand::ImportPackageTargets(cmPackageState& packageState,
}
// Import base file.
if (!reader.ImportTargets(this->Makefile, this->Status, this->GlobalScope)) {
if (!reader.ImportTargets(this->Makefile, this->Status,
this->GlobalScope
? cm::ImportedTargetScope::Global
: cm::ImportedTargetScope::Local)) {
return false;
}
+4 -5
View File
@@ -658,7 +658,7 @@ bool cmMakefile::ExecuteCommand(cmListFileFunction const& lff,
bool cmMakefile::IsImportedTargetGlobalScope() const
{
return this->CurrentImportedTargetScope == ImportedTargetScope::Global;
return this->CurrentImportedTargetScope == cm::ImportedTargetScope::Global;
}
class cmMakefile::IncludeScope : public FileScopeBase
@@ -3986,13 +3986,12 @@ void cmMakefile::RaiseScope(std::vector<std::string> const& variables)
}
cmTarget* cmMakefile::AddImportedTarget(std::string const& name,
cm::TargetType type, bool global)
cm::TargetType type,
cm::ImportedTargetScope scope)
{
// Create the target.
auto target =
cm::make_unique<cmTarget>(name, type,
global ? cmTarget::Visibility::ImportedGlobally
: cmTarget::Visibility::Imported,
cm::make_unique<cmTarget>(name, type, cmTarget::ImportedVisibility(scope),
this, cmTarget::PerConfig::Yes);
// Add to the set of available imported targets.
+7 -11
View File
@@ -272,7 +272,7 @@ public:
/** Create a new imported target with the name and type given. */
cmTarget* AddImportedTarget(std::string const& name, cm::TargetType type,
bool global);
cm::ImportedTargetScope scope);
cmTarget* AddForeignTarget(std::string const& origin,
std::string const& name);
@@ -979,22 +979,17 @@ public:
bool IsImportedTargetGlobalScope() const;
enum class ImportedTargetScope
{
Local,
Global,
};
/** Helper class to manage whether imported packages
* should be globally scoped based off the find package command
*/
class SetGlobalTargetImportScope
{
public:
SetGlobalTargetImportScope(cmMakefile* mk, ImportedTargetScope const scope)
SetGlobalTargetImportScope(cmMakefile* mk,
cm::ImportedTargetScope const scope)
: Makefile(mk)
{
if (scope == ImportedTargetScope::Global &&
if (scope == cm::ImportedTargetScope::Global &&
!this->Makefile->IsImportedTargetGlobalScope()) {
this->Makefile->CurrentImportedTargetScope = scope;
this->Set = true;
@@ -1006,7 +1001,7 @@ public:
{
if (this->Set) {
this->Makefile->CurrentImportedTargetScope =
ImportedTargetScope::Local;
cm::ImportedTargetScope::Local;
}
}
@@ -1371,5 +1366,6 @@ private:
std::set<std::string> WarnedCMP0144;
std::set<std::string> WarnedCMP0219;
bool IsSourceFileTryCompile;
ImportedTargetScope CurrentImportedTargetScope = ImportedTargetScope::Local;
cm::ImportedTargetScope CurrentImportedTargetScope =
cm::ImportedTargetScope::Local;
};
+7 -5
View File
@@ -855,10 +855,11 @@ void cmPackageInfoReader::ReadCxxModulesMetadata(
cmTarget* cmPackageInfoReader::AddLibraryComponent(
cmMakefile* makefile, cm::TargetType type, std::string const& name,
Json::Value const& data, std::string const& package, bool global) const
Json::Value const& data, std::string const& package,
cm::ImportedTargetScope scope) const
{
// Create the imported target.
cmTarget* const target = makefile->AddImportedTarget(name, type, global);
cmTarget* const target = makefile->AddImportedTarget(name, type, scope);
target->SetOrigin(cmTarget::Origin::Cps);
// Set target properties.
@@ -872,7 +873,8 @@ cmTarget* cmPackageInfoReader::AddLibraryComponent(
}
bool cmPackageInfoReader::ImportTargets(cmMakefile* makefile,
cmExecutionStatus& status, bool global)
cmExecutionStatus& status,
cm::ImportedTargetScope scope)
{
std::string const& package = this->GetName();
@@ -896,7 +898,7 @@ bool cmPackageInfoReader::ImportTargets(cmMakefile* makefile,
auto createTarget = [&](cm::TargetType typeEnum) {
return this->AddLibraryComponent(makefile, typeEnum, fullName, *ci,
package, global);
package, scope);
};
cmTarget* target = nullptr;
@@ -934,7 +936,7 @@ bool cmPackageInfoReader::ImportTargets(cmMakefile* makefile,
}
cmTarget* const target = makefile->AddImportedTarget(
package, cm::TargetType::INTERFACE_LIBRARY, global);
package, cm::TargetType::INTERFACE_LIBRARY, scope);
for (std::string const& name : defaultComponents) {
std::string const& fullName = cmStrCat(package, "::"_s, name);
AppendProperty(makefile, target, "LINK_LIBRARIES"_s, {}, fullName);
+4 -2
View File
@@ -19,6 +19,7 @@ class cmMakefile;
class cmTarget;
namespace cm {
enum class ImportedTargetScope;
enum class TargetType;
} // namespace cm
@@ -80,7 +81,7 @@ public:
/// Create targets for components specified in the CPS file.
bool ImportTargets(cmMakefile* makefile, cmExecutionStatus& status,
bool global);
cm::ImportedTargetScope scope);
/// Add configuration-specific properties for targets.
bool ImportTargetConfigurations(cmMakefile* makefile,
@@ -92,7 +93,8 @@ private:
cmTarget* AddLibraryComponent(cmMakefile* makefile, cm::TargetType type,
std::string const& name,
Json::Value const& data,
std::string const& package, bool global) const;
std::string const& package,
cm::ImportedTargetScope scope) const;
void AddTargetConfiguration(cmTarget* target,
cm::string_view configuration) const;
+7
View File
@@ -71,6 +71,13 @@ public:
No
};
static Visibility ImportedVisibility(cm::ImportedTargetScope scope)
{
return (scope == cm::ImportedTargetScope::Global
? Visibility::ImportedGlobally
: Visibility::Imported);
}
cmTarget(std::string name, cm::TargetType type, Visibility vis,
cmMakefile* mf, PerConfig perConfig);
+6
View File
@@ -40,4 +40,10 @@ using TargetDomainSet = cm::enum_set<TargetDomain>;
static TargetDomainSet const AllTargetDomains{ TargetDomain::NATIVE,
TargetDomain::ALIAS,
TargetDomain::FOREIGN };
enum class ImportedTargetScope
{
Local,
Global,
};
};