export: Refactor obtaining export information

Refactor methods in the build and install export file generators to have
the same, simplified API. Expose the resulting method as an abstract
method on the base class, so that it can be called from mode-agnostic
generators. While we're at it, refactor cmExportInstallFileGenerator's
version to use std::any_of.
This commit is contained in:
Matthew Woehlke
2024-07-25 13:03:42 -04:00
parent 262a89c92b
commit cd217739f7
6 changed files with 40 additions and 37 deletions

View File

@@ -138,11 +138,8 @@ void cmExportBuildFileGenerator::HandleMissingTarget(
{ {
// The target is not in the export. // The target is not in the export.
if (!this->AppendMode) { if (!this->AppendMode) {
std::string const& name = dependee->GetName(); auto const& exportInfo = this->FindExportInfo(dependee);
cmGlobalGenerator* gg = auto const& exportFiles = exportInfo.first;
dependee->GetLocalGenerator()->GetGlobalGenerator();
auto exportInfo = this->FindBuildExportInfo(gg, name);
std::vector<std::string> const& exportFiles = exportInfo.first;
if (exportFiles.size() == 1) { if (exportFiles.size() == 1) {
std::string missingTarget = exportInfo.second; std::string missingTarget = exportInfo.second;
@@ -178,14 +175,15 @@ void cmExportBuildFileGenerator::GetTargets(
targets = this->Targets; targets = this->Targets;
} }
std::pair<std::vector<std::string>, std::string> cmExportFileGenerator::ExportInfo cmExportBuildFileGenerator::FindExportInfo(
cmExportBuildFileGenerator::FindBuildExportInfo(cmGlobalGenerator* gg, cmGeneratorTarget const* target) const
std::string const& name)
{ {
std::vector<std::string> exportFiles; std::vector<std::string> exportFiles;
std::string ns; std::string ns;
auto& exportSets = gg->GetBuildExportSets(); auto const& name = target->GetName();
auto& exportSets =
target->GetLocalGenerator()->GetGlobalGenerator()->GetBuildExportSets();
for (auto const& exp : exportSets) { for (auto const& exp : exportSets) {
auto const& exportSet = exp.second; auto const& exportSet = exp.second;
@@ -199,7 +197,7 @@ cmExportBuildFileGenerator::FindBuildExportInfo(cmGlobalGenerator* gg,
} }
} }
return { exportFiles, ns }; return { exportFiles, exportFiles.size() == 1 ? ns : std::string{} };
} }
void cmExportBuildFileGenerator::ComplainAboutMissingTarget( void cmExportBuildFileGenerator::ComplainAboutMissingTarget(

View File

@@ -16,7 +16,6 @@
class cmExportSet; class cmExportSet;
class cmGeneratorTarget; class cmGeneratorTarget;
class cmGlobalGenerator;
class cmLocalGenerator; class cmLocalGenerator;
/** \class cmExportBuildCMakeConfigGenerator /** \class cmExportBuildCMakeConfigGenerator
@@ -82,7 +81,7 @@ protected:
void ComplainAboutMissingTarget( void ComplainAboutMissingTarget(
cmGeneratorTarget const* depender, cmGeneratorTarget const* dependee, cmGeneratorTarget const* depender, cmGeneratorTarget const* dependee,
std::vector<std::string> const& namespaces) const; std::vector<std::string> const& exportFiles) const;
void ComplainAboutDuplicateTarget( void ComplainAboutDuplicateTarget(
std::string const& targetName) const override; std::string const& targetName) const override;
@@ -105,8 +104,7 @@ protected:
return this->CxxModulesDirectory; return this->CxxModulesDirectory;
} }
std::pair<std::vector<std::string>, std::string> FindBuildExportInfo( ExportInfo FindExportInfo(cmGeneratorTarget const* target) const override;
cmGlobalGenerator* gg, std::string const& name);
using cmExportFileGenerator::PopulateInterfaceProperties; using cmExportFileGenerator::PopulateInterfaceProperties;
bool PopulateInterfaceProperties(cmGeneratorTarget const* target, bool PopulateInterfaceProperties(cmGeneratorTarget const* target,

View File

@@ -8,6 +8,7 @@
#include <map> #include <map>
#include <set> #include <set>
#include <string> #include <string>
#include <utility>
#include <vector> #include <vector>
#include <cm/string_view> #include <cm/string_view>
@@ -126,6 +127,12 @@ protected:
virtual void ReportError(std::string const& errorMessage) const = 0; virtual void ReportError(std::string const& errorMessage) const = 0;
using ExportInfo = std::pair<std::vector<std::string>, std::string>;
/** Find the set of export files and the unique namespace (if any) for a
* target. */
virtual ExportInfo FindExportInfo(cmGeneratorTarget const* target) const = 0;
enum FreeTargetsReplace enum FreeTargetsReplace
{ {
ReplaceFreeTargets, ReplaceFreeTargets,

View File

@@ -8,6 +8,7 @@
#include <memory> #include <memory>
#include <set> #include <set>
#include <sstream> #include <sstream>
#include <utility>
#include "cmExportSet.h" #include "cmExportSet.h"
#include "cmGeneratorTarget.h" #include "cmGeneratorTarget.h"
@@ -204,10 +205,9 @@ void cmExportInstallFileGenerator::HandleMissingTarget(
std::string& link_libs, cmGeneratorTarget const* depender, std::string& link_libs, cmGeneratorTarget const* depender,
cmGeneratorTarget* dependee) cmGeneratorTarget* dependee)
{ {
std::string const& name = dependee->GetName(); auto const& exportInfo = this->FindExportInfo(dependee);
cmGlobalGenerator* gg = dependee->GetLocalGenerator()->GetGlobalGenerator(); auto const& exportFiles = exportInfo.first;
auto exportInfo = this->FindNamespaces(gg, name);
std::vector<std::string> const& exportFiles = exportInfo.first;
if (exportFiles.size() == 1) { if (exportFiles.size() == 1) {
std::string missingTarget = exportInfo.second; std::string missingTarget = exportInfo.second;
@@ -221,26 +221,24 @@ void cmExportInstallFileGenerator::HandleMissingTarget(
} }
} }
std::pair<std::vector<std::string>, std::string> cmExportFileGenerator::ExportInfo cmExportInstallFileGenerator::FindExportInfo(
cmExportInstallFileGenerator::FindNamespaces(cmGlobalGenerator* gg, cmGeneratorTarget const* target) const
std::string const& name) const
{ {
std::vector<std::string> exportFiles; std::vector<std::string> exportFiles;
std::string ns; std::string ns;
cmExportSetMap const& exportSets = gg->GetExportSets();
for (auto const& expIt : exportSets) { auto const& name = target->GetName();
cmExportSet const& exportSet = expIt.second; auto& exportSets =
target->GetLocalGenerator()->GetGlobalGenerator()->GetExportSets();
bool containsTarget = false; for (auto const& exp : exportSets) {
for (auto const& target : exportSet.GetTargetExports()) { auto const& exportSet = exp.second;
if (name == target->TargetName) { auto const& targets = exportSet.GetTargetExports();
containsTarget = true;
break;
}
}
if (containsTarget) { if (std::any_of(targets.begin(), targets.end(),
[&name](std::unique_ptr<cmTargetExport> const& te) {
return te->TargetName == name;
})) {
std::vector<cmInstallExportGenerator const*> const* installs = std::vector<cmInstallExportGenerator const*> const* installs =
exportSet.GetInstallations(); exportSet.GetInstallations();
for (cmInstallExportGenerator const* install : *installs) { for (cmInstallExportGenerator const* install : *installs) {
@@ -250,7 +248,7 @@ cmExportInstallFileGenerator::FindNamespaces(cmGlobalGenerator* gg,
} }
} }
return { exportFiles, ns }; return { exportFiles, exportFiles.size() == 1 ? ns : std::string{} };
} }
void cmExportInstallFileGenerator::ComplainAboutMissingTarget( void cmExportInstallFileGenerator::ComplainAboutMissingTarget(

View File

@@ -8,7 +8,6 @@
#include <map> #include <map>
#include <set> #include <set>
#include <string> #include <string>
#include <utility>
#include <vector> #include <vector>
#include <cm/string_view> #include <cm/string_view>
@@ -20,7 +19,6 @@
class cmExportSet; class cmExportSet;
class cmGeneratorTarget; class cmGeneratorTarget;
class cmGlobalGenerator;
class cmInstallTargetGenerator; class cmInstallTargetGenerator;
class cmTargetExport; class cmTargetExport;
@@ -94,8 +92,7 @@ protected:
void ComplainAboutDuplicateTarget( void ComplainAboutDuplicateTarget(
std::string const& targetName) const override; std::string const& targetName) const override;
std::pair<std::vector<std::string>, std::string> FindNamespaces( ExportInfo FindExportInfo(cmGeneratorTarget const* target) const override;
cmGlobalGenerator* gg, std::string const& name) const;
void ReportError(std::string const& errorMessage) const override; void ReportError(std::string const& errorMessage) const override;

View File

@@ -45,6 +45,11 @@ protected:
{ {
} }
ExportInfo FindExportInfo(cmGeneratorTarget const* /*target*/) const override
{
return { {}, {} };
}
void PopulateProperties(cmGeneratorTarget const* target, void PopulateProperties(cmGeneratorTarget const* target,
ImportPropertyMap& properties, ImportPropertyMap& properties,
std::set<cmGeneratorTarget const*>& emitted); std::set<cmGeneratorTarget const*>& emitted);