From 5b5869532145ff4425d7abfd09eaae6a638b6810 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Mon, 30 Jan 2023 16:19:14 -0500 Subject: cmTarget: store visibility as an `enum` rather than bools C++ modules are going to introduce a third concept of "synthesized" targets, so update logic where needed to avoid assuming "not imported? must be normal". Also add an accessor method to perform queries against the visibility. --- Source/cmGeneratorTarget.cxx | 5 ++++ Source/cmGeneratorTarget.h | 1 + Source/cmTarget.cxx | 59 ++++++++++++++++++++++++++++++++++---------- Source/cmTarget.h | 1 + 4 files changed, 53 insertions(+), 13 deletions(-) diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index f83be26..2202507 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -1237,6 +1237,11 @@ bool cmGeneratorTarget::IsInBuildSystem() const return false; } +bool cmGeneratorTarget::IsNormal() const +{ + return this->Target->IsNormal(); +} + bool cmGeneratorTarget::IsImported() const { return this->Target->IsImported(); diff --git a/Source/cmGeneratorTarget.h b/Source/cmGeneratorTarget.h index afd9da4..210022e 100644 --- a/Source/cmGeneratorTarget.h +++ b/Source/cmGeneratorTarget.h @@ -50,6 +50,7 @@ public: cmGlobalGenerator* GetGlobalGenerator() const; bool IsInBuildSystem() const; + bool IsNormal() const; bool IsImported() const; bool IsImportedGloballyVisible() const; bool CanCompileSources() const; diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx index e8d66cc..8c71295 100644 --- a/Source/cmTarget.cxx +++ b/Source/cmTarget.cxx @@ -629,10 +629,9 @@ public: bool IsDLLPlatform; bool IsAIX; bool IsAndroid; - bool IsImportedTarget; - bool ImportedGloballyVisible; bool BuildInterfaceIncludesAppended; bool PerConfig; + cmTarget::Visibility TargetVisibility; std::set>> Utilities; std::vector PreBuildCommands; std::vector PreLinkCommands; @@ -667,6 +666,8 @@ public: cmTargetInternals(); + bool IsImported() const; + bool CheckImportedLibName(std::string const& prop, std::string const& value) const; @@ -914,9 +915,7 @@ cmTarget::cmTarget(std::string const& name, cmStateEnums::TargetType type, this->impl->IsDLLPlatform = false; this->impl->IsAIX = false; this->impl->IsAndroid = false; - this->impl->IsImportedTarget = - (vis == VisibilityImported || vis == VisibilityImportedGlobally); - this->impl->ImportedGloballyVisible = vis == VisibilityImportedGlobally; + this->impl->TargetVisibility = vis; this->impl->BuildInterfaceIncludesAppended = false; this->impl->PerConfig = (perConfig == PerConfig::Yes); @@ -939,7 +938,7 @@ cmTarget::cmTarget(std::string const& name, cmStateEnums::TargetType type, // Save the backtrace of target construction. this->impl->Backtrace = this->impl->Makefile->GetBacktrace(); - if (!this->IsImported()) { + if (this->IsNormal()) { // Initialize the INCLUDE_DIRECTORIES property based on the current value // of the same directory property: this->impl->IncludeDirectories.CopyFromDirectory( @@ -988,7 +987,7 @@ cmTarget::cmTarget(std::string const& name, cmStateEnums::TargetType type, if (this->impl->TargetType != cmStateEnums::UTILITY && this->impl->TargetType != cmStateEnums::GLOBAL_TARGET) { metConditions.insert(TargetProperty::InitCondition::NormalTarget); - if (!this->IsImported()) { + if (this->IsNormal()) { metConditions.insert( TargetProperty::InitCondition::NormalNonImportedTarget); } @@ -1091,7 +1090,7 @@ cmTarget::cmTarget(std::string const& name, cmStateEnums::TargetType type, } } - if (this->IsImported() || mf->GetPropertyAsBool("SYSTEM")) { + if (!this->IsNormal() || mf->GetPropertyAsBool("SYSTEM")) { this->SetProperty("SYSTEM", "ON"); } @@ -1863,8 +1862,8 @@ void cmTarget::StoreProperty(const std::string& prop, ValueType value) return; } /* no need to change anything if value does not change */ - if (!this->impl->ImportedGloballyVisible) { - this->impl->ImportedGloballyVisible = true; + if (!this->IsImportedGloballyVisible()) { + this->impl->TargetVisibility = VisibilityImportedGlobally; this->GetGlobalGenerator()->IndexTarget(this); } } else if (cmHasLiteralPrefix(prop, "IMPORTED_LIBNAME") && @@ -2555,14 +2554,48 @@ bool cmTarget::IsAIX() const return this->impl->IsAIX; } +bool cmTarget::IsNormal() const +{ + switch (this->impl->TargetVisibility) { + case VisibilityNormal: + return true; + case VisibilityImported: + case VisibilityImportedGlobally: + return false; + } + assert(false && "unknown visibility (IsNormal)"); + return false; +} + +bool cmTargetInternals::IsImported() const +{ + switch (this->TargetVisibility) { + case cmTarget::VisibilityImported: + case cmTarget::VisibilityImportedGlobally: + return true; + case cmTarget::VisibilityNormal: + return false; + } + assert(false && "unknown visibility (IsImported)"); + return false; +} + bool cmTarget::IsImported() const { - return this->impl->IsImportedTarget; + return this->impl->IsImported(); } bool cmTarget::IsImportedGloballyVisible() const { - return this->impl->ImportedGloballyVisible; + switch (this->impl->TargetVisibility) { + case VisibilityImportedGlobally: + return true; + case VisibilityNormal: + case VisibilityImported: + return false; + } + assert(false && "unknown visibility (IsImportedGloballyVisible)"); + return false; } bool cmTarget::IsPerConfig() const @@ -2858,7 +2891,7 @@ bool cmTargetInternals::CheckImportedLibName(std::string const& prop, std::string const& value) const { if (this->TargetType != cmStateEnums::INTERFACE_LIBRARY || - !this->IsImportedTarget) { + !this->IsImported()) { this->Makefile->IssueMessage( MessageType::FATAL_ERROR, prop + diff --git a/Source/cmTarget.h b/Source/cmTarget.h index 38bd036..51bbd54 100644 --- a/Source/cmTarget.h +++ b/Source/cmTarget.h @@ -205,6 +205,7 @@ public: //! Return whether or not we are targeting AIX. bool IsAIX() const; + bool IsNormal() const; bool IsImported() const; bool IsImportedGloballyVisible() const; bool IsPerConfig() const; -- cgit v0.12 From 1d0426f6426ef88342f3a57fb555a2b2d8891712 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Tue, 14 Feb 2023 12:21:06 -0500 Subject: cmTarget: make Visibility an `enum class` --- Source/cmExportTryCompileFileGenerator.cxx | 2 +- Source/cmMakefile.cxx | 6 +++--- Source/cmTarget.cxx | 20 ++++++++++---------- Source/cmTarget.h | 8 ++++---- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Source/cmExportTryCompileFileGenerator.cxx b/Source/cmExportTryCompileFileGenerator.cxx index 33c057d..c6ebad5 100644 --- a/Source/cmExportTryCompileFileGenerator.cxx +++ b/Source/cmExportTryCompileFileGenerator.cxx @@ -85,7 +85,7 @@ std::string cmExportTryCompileFileGenerator::FindTargets( std::unique_ptr cge = ge.Parse(*prop); cmTarget dummyHead("try_compile_dummy_exe", cmStateEnums::EXECUTABLE, - cmTarget::VisibilityNormal, tgt->Target->GetMakefile(), + cmTarget::Visibility::Normal, tgt->Target->GetMakefile(), cmTarget::PerConfig::Yes); cmGeneratorTarget gDummyHead(&dummyHead, tgt->GetLocalGenerator()); diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index 0ad0e6e..457204a 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -2120,7 +2120,7 @@ std::pair cmMakefile::CreateNewTarget( cmTarget::PerConfig perConfig) { auto ib = this->Targets.emplace( - name, cmTarget(name, type, cmTarget::VisibilityNormal, this, perConfig)); + name, cmTarget(name, type, cmTarget::Visibility::Normal, this, perConfig)); auto it = ib.first; if (!ib.second) { return std::make_pair(std::ref(it->second), false); @@ -4203,8 +4203,8 @@ cmTarget* cmMakefile::AddImportedTarget(const std::string& name, // Create the target. std::unique_ptr target( new cmTarget(name, type, - global ? cmTarget::VisibilityImportedGlobally - : cmTarget::VisibilityImported, + global ? cmTarget::Visibility::ImportedGlobally + : cmTarget::Visibility::Imported, this, cmTarget::PerConfig::Yes)); // Add to the set of available imported targets. diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx index 8c71295..48f92b5 100644 --- a/Source/cmTarget.cxx +++ b/Source/cmTarget.cxx @@ -1863,7 +1863,7 @@ void cmTarget::StoreProperty(const std::string& prop, ValueType value) } /* no need to change anything if value does not change */ if (!this->IsImportedGloballyVisible()) { - this->impl->TargetVisibility = VisibilityImportedGlobally; + this->impl->TargetVisibility = Visibility::ImportedGlobally; this->GetGlobalGenerator()->IndexTarget(this); } } else if (cmHasLiteralPrefix(prop, "IMPORTED_LIBNAME") && @@ -2557,10 +2557,10 @@ bool cmTarget::IsAIX() const bool cmTarget::IsNormal() const { switch (this->impl->TargetVisibility) { - case VisibilityNormal: + case Visibility::Normal: return true; - case VisibilityImported: - case VisibilityImportedGlobally: + case Visibility::Imported: + case Visibility::ImportedGlobally: return false; } assert(false && "unknown visibility (IsNormal)"); @@ -2570,10 +2570,10 @@ bool cmTarget::IsNormal() const bool cmTargetInternals::IsImported() const { switch (this->TargetVisibility) { - case cmTarget::VisibilityImported: - case cmTarget::VisibilityImportedGlobally: + case cmTarget::Visibility::Imported: + case cmTarget::Visibility::ImportedGlobally: return true; - case cmTarget::VisibilityNormal: + case cmTarget::Visibility::Normal: return false; } assert(false && "unknown visibility (IsImported)"); @@ -2588,10 +2588,10 @@ bool cmTarget::IsImported() const bool cmTarget::IsImportedGloballyVisible() const { switch (this->impl->TargetVisibility) { - case VisibilityImportedGlobally: + case Visibility::ImportedGlobally: return true; - case VisibilityNormal: - case VisibilityImported: + case Visibility::Normal: + case Visibility::Imported: return false; } assert(false && "unknown visibility (IsImportedGloballyVisible)"); diff --git a/Source/cmTarget.h b/Source/cmTarget.h index 51bbd54..b96bdf2 100644 --- a/Source/cmTarget.h +++ b/Source/cmTarget.h @@ -46,11 +46,11 @@ class BTs; class cmTarget { public: - enum Visibility + enum class Visibility { - VisibilityNormal, - VisibilityImported, - VisibilityImportedGlobally + Normal, + Imported, + ImportedGlobally, }; enum class PerConfig -- cgit v0.12 From c97de1047f2fd08a74c42982012e8bb96a5e2f89 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Mon, 30 Jan 2023 10:36:44 -0500 Subject: cmMakefile: add support for a "synthesized" target It is a normal target, but will end up copying its internals from another target. Keep track of this state so that such copying can only occur when intended. --- Source/cmGeneratorTarget.cxx | 5 +++++ Source/cmGeneratorTarget.h | 1 + Source/cmMakefile.cxx | 15 ++++++++++++--- Source/cmMakefile.h | 5 ++++- Source/cmTarget.cxx | 17 +++++++++++++++++ Source/cmTarget.h | 2 ++ 6 files changed, 41 insertions(+), 4 deletions(-) diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index 2202507..b99892b 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -1242,6 +1242,11 @@ bool cmGeneratorTarget::IsNormal() const return this->Target->IsNormal(); } +bool cmGeneratorTarget::IsSynthetic() const +{ + return this->Target->IsSynthetic(); +} + bool cmGeneratorTarget::IsImported() const { return this->Target->IsImported(); diff --git a/Source/cmGeneratorTarget.h b/Source/cmGeneratorTarget.h index 210022e..e46c719 100644 --- a/Source/cmGeneratorTarget.h +++ b/Source/cmGeneratorTarget.h @@ -51,6 +51,7 @@ public: bool IsInBuildSystem() const; bool IsNormal() const; + bool IsSynthetic() const; bool IsImported() const; bool IsImportedGloballyVisible() const; bool CanCompileSources() const; diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index 457204a..d963a5a 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -2115,12 +2115,21 @@ cmTarget* cmMakefile::AddNewTarget(cmStateEnums::TargetType type, return &this->CreateNewTarget(name, type).first; } +cmTarget* cmMakefile::AddSynthesizedTarget(cmStateEnums::TargetType type, + const std::string& name) +{ + return &this + ->CreateNewTarget(name, type, cmTarget::PerConfig::Yes, + cmTarget::Visibility::Generated) + .first; +} + std::pair cmMakefile::CreateNewTarget( const std::string& name, cmStateEnums::TargetType type, - cmTarget::PerConfig perConfig) + cmTarget::PerConfig perConfig, cmTarget::Visibility vis) { - auto ib = this->Targets.emplace( - name, cmTarget(name, type, cmTarget::Visibility::Normal, this, perConfig)); + auto ib = + this->Targets.emplace(name, cmTarget(name, type, vis, this, perConfig)); auto it = ib.first; if (!ib.second) { return std::make_pair(std::ref(it->second), false); diff --git a/Source/cmMakefile.h b/Source/cmMakefile.h index 3866aca..7b19c97 100644 --- a/Source/cmMakefile.h +++ b/Source/cmMakefile.h @@ -241,10 +241,13 @@ public: std::pair CreateNewTarget( const std::string& name, cmStateEnums::TargetType type, - cmTarget::PerConfig perConfig = cmTarget::PerConfig::Yes); + cmTarget::PerConfig perConfig = cmTarget::PerConfig::Yes, + cmTarget::Visibility vis = cmTarget::Visibility::Normal); cmTarget* AddNewTarget(cmStateEnums::TargetType type, const std::string& name); + cmTarget* AddSynthesizedTarget(cmStateEnums::TargetType type, + const std::string& name); /** Create a target instance for the utility. */ cmTarget* AddNewUtilityTarget(const std::string& utilityName, diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx index 48f92b5..91d5de6 100644 --- a/Source/cmTarget.cxx +++ b/Source/cmTarget.cxx @@ -2559,6 +2559,7 @@ bool cmTarget::IsNormal() const switch (this->impl->TargetVisibility) { case Visibility::Normal: return true; + case Visibility::Generated: case Visibility::Imported: case Visibility::ImportedGlobally: return false; @@ -2567,6 +2568,20 @@ bool cmTarget::IsNormal() const return false; } +bool cmTarget::IsSynthetic() const +{ + switch (this->impl->TargetVisibility) { + case Visibility::Generated: + return true; + case Visibility::Normal: + case Visibility::Imported: + case Visibility::ImportedGlobally: + return false; + } + assert(false && "unknown visibility (IsSynthetic)"); + return false; +} + bool cmTargetInternals::IsImported() const { switch (this->TargetVisibility) { @@ -2574,6 +2589,7 @@ bool cmTargetInternals::IsImported() const case cmTarget::Visibility::ImportedGlobally: return true; case cmTarget::Visibility::Normal: + case cmTarget::Visibility::Generated: return false; } assert(false && "unknown visibility (IsImported)"); @@ -2591,6 +2607,7 @@ bool cmTarget::IsImportedGloballyVisible() const case Visibility::ImportedGlobally: return true; case Visibility::Normal: + case Visibility::Generated: case Visibility::Imported: return false; } diff --git a/Source/cmTarget.h b/Source/cmTarget.h index b96bdf2..95539fa 100644 --- a/Source/cmTarget.h +++ b/Source/cmTarget.h @@ -49,6 +49,7 @@ public: enum class Visibility { Normal, + Generated, Imported, ImportedGlobally, }; @@ -206,6 +207,7 @@ public: bool IsAIX() const; bool IsNormal() const; + bool IsSynthetic() const; bool IsImported() const; bool IsImportedGloballyVisible() const; bool IsPerConfig() const; -- cgit v0.12 From bde9d4ad014bd93181281fdfc2262d84ac9fd487 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Tue, 31 Jan 2023 22:46:25 -0500 Subject: cmCommonTargetGenerator: also consider synthetic targets --- Source/cmCommonTargetGenerator.cxx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Source/cmCommonTargetGenerator.cxx b/Source/cmCommonTargetGenerator.cxx index a63c162..1705763 100644 --- a/Source/cmCommonTargetGenerator.cxx +++ b/Source/cmCommonTargetGenerator.cxx @@ -176,7 +176,9 @@ std::vector cmCommonTargetGenerator::GetLinkedTargetDirectories( // We can ignore the INTERFACE_LIBRARY items because // Target->GetLinkInformation already processed their // link interface and they don't have any output themselves. - && linkee->GetType() != cmStateEnums::INTERFACE_LIBRARY && + && (linkee->GetType() != cmStateEnums::INTERFACE_LIBRARY + // Synthesized targets may have relevant rules. + || linkee->IsSynthetic()) && ((lang == "CXX"_s && linkee->HaveCxx20ModuleSources()) || (lang == "Fortran"_s && linkee->HaveFortranSources(config))) && emitted.insert(linkee).second) { -- cgit v0.12 From dadaa2863841fc704b73276630f4260fa7ad5850 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Tue, 31 Jan 2023 22:57:59 -0500 Subject: cmGeneratorTarget: synthetic targets don't have output either --- Source/cmGeneratorTarget.cxx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index b99892b..be6456b 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -7115,6 +7115,11 @@ cmGeneratorTarget::OutputInfo const* cmGeneratorTarget::GetOutputInfo( return nullptr; } + // Synthetic targets don't have output. + if (this->IsSynthetic()) { + return nullptr; + } + // Only libraries and executables have well-defined output files. if (!this->HaveWellDefinedOutputFiles()) { std::string msg = cmStrCat("cmGeneratorTarget::GetOutputInfo called for ", -- cgit v0.12