Browse Source

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.
Matthew Woehlke 1 year ago
parent
commit
cd217739f7

+ 8 - 10
Source/cmExportBuildFileGenerator.cxx

@@ -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();
-    cmGlobalGenerator* gg =
-      dependee->GetLocalGenerator()->GetGlobalGenerator();
-    auto exportInfo = this->FindBuildExportInfo(gg, name);
-    std::vector<std::string> const& exportFiles = exportInfo.first;
+    auto const& exportInfo = this->FindExportInfo(dependee);
+    auto 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>
-cmExportBuildFileGenerator::FindBuildExportInfo(cmGlobalGenerator* gg,
-                                                std::string const& name)
+cmExportFileGenerator::ExportInfo cmExportBuildFileGenerator::FindExportInfo(
+  cmGeneratorTarget const* target) const
 {
 {
   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(

+ 2 - 4
Source/cmExportBuildFileGenerator.h

@@ -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(
-    cmGlobalGenerator* gg, std::string const& name);
+  ExportInfo FindExportInfo(cmGeneratorTarget const* target) const override;
 
 
   using cmExportFileGenerator::PopulateInterfaceProperties;
   using cmExportFileGenerator::PopulateInterfaceProperties;
   bool PopulateInterfaceProperties(cmGeneratorTarget const* target,
   bool PopulateInterfaceProperties(cmGeneratorTarget const* target,

+ 7 - 0
Source/cmExportFileGenerator.h

@@ -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,

+ 17 - 19
Source/cmExportInstallFileGenerator.cxx

@@ -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();
-  cmGlobalGenerator* gg = dependee->GetLocalGenerator()->GetGlobalGenerator();
-  auto exportInfo = this->FindNamespaces(gg, name);
-  std::vector<std::string> const& exportFiles = exportInfo.first;
+  auto const& exportInfo = this->FindExportInfo(dependee);
+  auto 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>
-cmExportInstallFileGenerator::FindNamespaces(cmGlobalGenerator* gg,
-                                             std::string const& name) const
+cmExportFileGenerator::ExportInfo cmExportInstallFileGenerator::FindExportInfo(
+  cmGeneratorTarget const* target) 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) {
-    cmExportSet const& exportSet = expIt.second;
+  auto const& name = target->GetName();
+  auto& exportSets =
+    target->GetLocalGenerator()->GetGlobalGenerator()->GetExportSets();
 
 
-    bool containsTarget = false;
-    for (auto const& target : exportSet.GetTargetExports()) {
-      if (name == target->TargetName) {
-        containsTarget = true;
-        break;
-      }
-    }
+  for (auto const& exp : exportSets) {
+    auto const& exportSet = exp.second;
+    auto const& targets = exportSet.GetTargetExports();
 
 
-    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(

+ 1 - 4
Source/cmExportInstallFileGenerator.h

@@ -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(
-    cmGlobalGenerator* gg, std::string const& name) const;
+  ExportInfo FindExportInfo(cmGeneratorTarget const* target) const override;
 
 
   void ReportError(std::string const& errorMessage) const override;
   void ReportError(std::string const& errorMessage) const override;
 
 

+ 5 - 0
Source/cmExportTryCompileFileGenerator.h

@@ -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);