浏览代码

export: Generalize GenerateImportFileConfig

Move some logic that is specific to CMake-format exports from
GenerateImportFileConfig to an overload of GenerateImportConfig, so that
the former can be moved (back) to the generic base class. This will
allow it to also be used for Common Package Specification exports. To
facilitate this, also add a method to get the format-specific character
used to separate the export file base name from the config suffix, so
that the rest of the logic to determine the file name can be shared.
Matthew Woehlke 1 年之前
父节点
当前提交
e5b73b60e3

+ 2 - 1
Source/cmExportFileGenerator.h

@@ -87,7 +87,8 @@ protected:
 
   /** Generate per-configuration target information to the given output
       stream.  */
-  void GenerateImportConfig(std::ostream& os, std::string const& config);
+  virtual void GenerateImportConfig(std::ostream& os,
+                                    std::string const& config);
 
   /** Each subclass knows where the target files are located.  */
   virtual void GenerateImportTargetsConfig(std::ostream& os,

+ 2 - 0
Source/cmExportInstallAndroidMKGenerator.h

@@ -40,6 +40,8 @@ public:
 protected:
   GenerateType GetGenerateType() const override { return INSTALL; }
 
+  char GetConfigFileNameSeparator() const override { return '-'; }
+
   // Implement virtual methods from the superclass.
   void ReportDuplicateTarget(std::string const& targetName) const;
   bool GenerateMainFile(std::ostream& os) override;

+ 4 - 34
Source/cmExportInstallCMakeConfigGenerator.cxx

@@ -13,6 +13,7 @@
 #include <cm/string_view>
 #include <cmext/string_view>
 
+#include "cmExportFileGenerator.h"
 #include "cmExportSet.h"
 #include "cmFileSet.h"
 #include "cmGeneratedFileStream.h"
@@ -224,48 +225,17 @@ void cmExportInstallCMakeConfigGenerator::LoadConfigFiles(std::ostream& os)
   /* clang-format on */
 }
 
-bool cmExportInstallCMakeConfigGenerator::GenerateImportFileConfig(
-  std::string const& config)
+void cmExportInstallCMakeConfigGenerator::GenerateImportConfig(
+  std::ostream& os, std::string const& config)
 {
-  // Skip configurations not enabled for this export.
-  if (!this->IEGen->InstallsForConfig(config)) {
-    return true;
-  }
-
-  // Construct the name of the file to generate.
-  std::string fileName = cmStrCat(this->FileDir, '/', this->FileBase, '-');
-  if (!config.empty()) {
-    fileName += cmSystemTools::LowerCase(config);
-  } else {
-    fileName += "noconfig";
-  }
-  fileName += this->FileExt;
-
-  // Open the output file to generate it.
-  cmGeneratedFileStream exportFileStream(fileName, true);
-  if (!exportFileStream) {
-    std::string se = cmSystemTools::GetLastSystemError();
-    std::ostringstream e;
-    e << "cannot write to file \"" << fileName << "\": " << se;
-    cmSystemTools::Error(e.str());
-    return false;
-  }
-  exportFileStream.SetCopyIfDifferent(true);
-  std::ostream& os = exportFileStream;
-
   // Start with the import file header.
   this->GenerateImportHeaderCode(os, config);
 
   // Generate the per-config target information.
-  this->GenerateImportConfig(os, config);
+  this->cmExportFileGenerator::GenerateImportConfig(os, config);
 
   // End with the import file footer.
   this->GenerateImportFooterCode(os);
-
-  // Record this per-config import file.
-  this->ConfigImportFiles[config] = fileName;
-
-  return true;
 }
 
 void cmExportInstallCMakeConfigGenerator::GenerateImportTargetsConfig(

+ 4 - 3
Source/cmExportInstallCMakeConfigGenerator.h

@@ -49,6 +49,10 @@ protected:
   bool GenerateMainFile(std::ostream& os) override;
   void GenerateImportTargetsConfig(std::ostream& os, std::string const& config,
                                    std::string const& suffix) override;
+  void GenerateImportConfig(std::ostream& os,
+                            std::string const& config) override;
+
+  char GetConfigFileNameSeparator() const override { return '-'; }
 
   /** Generate the relative import prefix.  */
   virtual void GenerateImportPrefix(std::ostream&);
@@ -58,9 +62,6 @@ protected:
 
   virtual void CleanupTemporaryVariables(std::ostream&);
 
-  /** Generate a per-configuration file for the targets.  */
-  virtual bool GenerateImportFileConfig(std::string const& config);
-
   std::string GetFileSetDirectories(cmGeneratorTarget* gte, cmFileSet* fileSet,
                                     cmTargetExport const* te) override;
   std::string GetFileSetFiles(cmGeneratorTarget* gte, cmFileSet* fileSet,

+ 40 - 0
Source/cmExportInstallFileGenerator.cxx

@@ -11,6 +11,7 @@
 #include <utility>
 
 #include "cmExportSet.h"
+#include "cmGeneratedFileStream.h"
 #include "cmGeneratorTarget.h"
 #include "cmGlobalGenerator.h"
 #include "cmInstallTargetGenerator.h"
@@ -98,6 +99,45 @@ std::string cmExportInstallFileGenerator::GetImportXcFrameworkLocation(
   return importedXcFrameworkLocation;
 }
 
+bool cmExportInstallFileGenerator::GenerateImportFileConfig(
+  std::string const& config)
+{
+  // Skip configurations not enabled for this export.
+  if (!this->IEGen->InstallsForConfig(config)) {
+    return true;
+  }
+
+  // Construct the name of the file to generate.
+  std::string fileName = cmStrCat(this->FileDir, '/', this->FileBase,
+                                  this->GetConfigFileNameSeparator());
+  if (!config.empty()) {
+    fileName += cmSystemTools::LowerCase(config);
+  } else {
+    fileName += "noconfig";
+  }
+  fileName += this->FileExt;
+
+  // Open the output file to generate it.
+  cmGeneratedFileStream exportFileStream(fileName, true);
+  if (!exportFileStream) {
+    std::string se = cmSystemTools::GetLastSystemError();
+    std::ostringstream e;
+    e << "cannot write to file \"" << fileName << "\": " << se;
+    cmSystemTools::Error(e.str());
+    return false;
+  }
+  exportFileStream.SetCopyIfDifferent(true);
+  std::ostream& os = exportFileStream;
+
+  // Generate the per-config target information.
+  this->GenerateImportConfig(os, config);
+
+  // Record this per-config import file.
+  this->ConfigImportFiles[config] = fileName;
+
+  return true;
+}
+
 void cmExportInstallFileGenerator::SetImportLocationProperty(
   std::string const& config, std::string const& suffix,
   cmInstallTargetGenerator* itgen, ImportPropertyMap& properties,

+ 4 - 0
Source/cmExportInstallFileGenerator.h

@@ -78,6 +78,7 @@ protected:
     cm::string_view const& prefixWithSlash = this->GetImportPrefixWithSlash();
     return std::string(prefixWithSlash.data(), prefixWithSlash.length() - 1);
   }
+  virtual char GetConfigFileNameSeparator() const = 0;
 
   void HandleMissingTarget(std::string& link_libs,
                            cmGeneratorTarget const* depender,
@@ -96,6 +97,9 @@ protected:
 
   void ReportError(std::string const& errorMessage) const override;
 
+  /** Generate a per-configuration file for the targets.  */
+  virtual bool GenerateImportFileConfig(std::string const& config);
+
   /** Fill in properties indicating installed file locations.  */
   void SetImportLocationProperty(std::string const& config,
                                  std::string const& suffix,