cmExportFileGenerator.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <iosfwd>
  6. #include <map>
  7. #include <set>
  8. #include <string>
  9. #include <vector>
  10. #include <cm/string_view>
  11. #include "cmGeneratorExpression.h"
  12. class cmExportSet;
  13. class cmGeneratorTarget;
  14. class cmLocalGenerator;
  15. /** \class cmExportFileGenerator
  16. * \brief Generate files exporting targets from a build or install tree.
  17. *
  18. * cmExportFileGenerator is the interface class for generating export files.
  19. */
  20. class cmExportFileGenerator
  21. {
  22. public:
  23. cmExportFileGenerator();
  24. virtual ~cmExportFileGenerator() = default;
  25. /** Set the full path to the export file to generate. */
  26. void SetExportFile(char const* mainFile);
  27. std::string const& GetMainExportFileName() const;
  28. /** Set the namespace in which to place exported target names. */
  29. void SetNamespace(std::string const& ns) { this->Namespace = ns; }
  30. std::string GetNamespace() const { return this->Namespace; }
  31. /** Add a configuration to be exported. */
  32. void AddConfiguration(std::string const& config);
  33. /** Create and actually generate the export file. Returns whether there was
  34. an error. */
  35. bool GenerateImportFile();
  36. protected:
  37. using ImportPropertyMap = std::map<std::string, std::string>;
  38. // Collect properties with detailed information about targets beyond
  39. // their location on disk.
  40. void SetImportDetailProperties(std::string const& config,
  41. std::string const& suffix,
  42. cmGeneratorTarget const* target,
  43. ImportPropertyMap& properties);
  44. enum class ImportLinkPropertyTargetNames
  45. {
  46. Yes,
  47. No,
  48. };
  49. template <typename T>
  50. void SetImportLinkProperty(std::string const& suffix,
  51. cmGeneratorTarget const* target,
  52. std::string const& propName,
  53. std::vector<T> const& entries,
  54. ImportPropertyMap& properties,
  55. ImportLinkPropertyTargetNames targetNames);
  56. /** Generate the export file to the given output stream. Returns whether
  57. there was an error. */
  58. virtual bool GenerateImportFile(std::ostream& os) = 0;
  59. /** Each subclass knows how to generate its kind of export file. */
  60. virtual bool GenerateMainFile(std::ostream& os) = 0;
  61. /** Generate per-configuration target information to the given output
  62. stream. */
  63. virtual void GenerateImportConfig(std::ostream& os,
  64. std::string const& config);
  65. /** Each subclass knows where the target files are located. */
  66. virtual void GenerateImportTargetsConfig(std::ostream& os,
  67. std::string const& config,
  68. std::string const& suffix) = 0;
  69. /** Record a target referenced by an exported target. */
  70. virtual bool NoteLinkedTarget(cmGeneratorTarget const* target,
  71. std::string const& linkedName,
  72. cmGeneratorTarget const* linkedTarget);
  73. /** Each subclass knows how to deal with a target that is missing from an
  74. * export set. */
  75. virtual void HandleMissingTarget(std::string& link_libs,
  76. cmGeneratorTarget const* depender,
  77. cmGeneratorTarget* dependee) = 0;
  78. /** Complain when a duplicate target is encountered. */
  79. virtual void ComplainAboutDuplicateTarget(
  80. std::string const& targetName) const = 0;
  81. virtual cm::string_view GetImportPrefixWithSlash() const = 0;
  82. void AddImportPrefix(std::string& exportDirs) const;
  83. void PopulateInterfaceProperty(std::string const& propName,
  84. cmGeneratorTarget const* target,
  85. ImportPropertyMap& properties) const;
  86. void PopulateInterfaceProperty(std::string const& propName,
  87. cmGeneratorTarget const* target,
  88. cmGeneratorExpression::PreprocessContext,
  89. ImportPropertyMap& properties);
  90. bool PopulateInterfaceLinkLibrariesProperty(
  91. cmGeneratorTarget const* target, cmGeneratorExpression::PreprocessContext,
  92. ImportPropertyMap& properties);
  93. bool PopulateInterfaceProperties(
  94. cmGeneratorTarget const* target,
  95. std::string const& includesDestinationDirs,
  96. cmGeneratorExpression::PreprocessContext preprocessRule,
  97. ImportPropertyMap& properties);
  98. virtual void ReportError(std::string const& errorMessage) const = 0;
  99. struct ExportInfo
  100. {
  101. std::vector<std::string> Files;
  102. std::set<std::string> Sets;
  103. std::set<std::string> Namespaces;
  104. };
  105. /** Find the set of export files and the unique namespace (if any) for a
  106. * target. */
  107. virtual ExportInfo FindExportInfo(cmGeneratorTarget const* target) const = 0;
  108. enum FreeTargetsReplace
  109. {
  110. ReplaceFreeTargets,
  111. NoReplaceFreeTargets
  112. };
  113. void ResolveTargetsInGeneratorExpressions(
  114. std::string& input, cmGeneratorTarget const* target,
  115. FreeTargetsReplace replace = NoReplaceFreeTargets);
  116. virtual cmExportSet* GetExportSet() const { return nullptr; }
  117. virtual void ReplaceInstallPrefix(std::string& input) const;
  118. virtual std::string InstallNameDir(cmGeneratorTarget const* target,
  119. std::string const& config) = 0;
  120. /** Get the temporary location of the config-agnostic C++ module file. */
  121. virtual std::string GetCxxModuleFile(std::string const& name) const = 0;
  122. virtual std::string GetCxxModulesDirectory() const = 0;
  123. virtual void GenerateCxxModuleConfigInformation(std::string const&,
  124. std::ostream& os) const = 0;
  125. bool AddTargetNamespace(std::string& input, cmGeneratorTarget const* target,
  126. cmLocalGenerator const* lg);
  127. static std::string PropertyConfigSuffix(std::string const& config);
  128. // The namespace in which the exports are placed in the generated file.
  129. std::string Namespace;
  130. // The set of configurations to export.
  131. std::vector<std::string> Configurations;
  132. // The file to generate.
  133. std::string MainImportFile;
  134. std::string FileDir;
  135. std::string FileBase;
  136. std::string FileExt;
  137. bool AppendMode = false;
  138. // The set of targets included in the export.
  139. std::set<cmGeneratorTarget const*> ExportedTargets;
  140. std::vector<std::string> MissingTargets;
  141. std::set<cmGeneratorTarget const*> ExternalTargets;
  142. private:
  143. void PopulateInterfaceProperty(std::string const& propName,
  144. std::string const& outputName,
  145. cmGeneratorTarget const* target,
  146. cmGeneratorExpression::PreprocessContext,
  147. ImportPropertyMap& properties);
  148. void PopulateCompatibleInterfaceProperties(
  149. cmGeneratorTarget const* target, ImportPropertyMap& properties) const;
  150. void PopulateCustomTransitiveInterfaceProperties(
  151. cmGeneratorTarget const* target,
  152. cmGeneratorExpression::PreprocessContext preprocessRule,
  153. ImportPropertyMap& properties);
  154. bool PopulateCxxModuleExportProperties(
  155. cmGeneratorTarget const* gte, ImportPropertyMap& properties,
  156. cmGeneratorExpression::PreprocessContext ctx,
  157. std::string const& includesDestinationDirs, std::string& errorMessage);
  158. bool PopulateExportProperties(cmGeneratorTarget const* gte,
  159. ImportPropertyMap& properties,
  160. std::string& errorMessage) const;
  161. void ResolveTargetsInGeneratorExpression(std::string& input,
  162. cmGeneratorTarget const* target,
  163. cmLocalGenerator const* lg);
  164. };
  165. extern template void cmExportFileGenerator::SetImportLinkProperty<std::string>(
  166. std::string const&, cmGeneratorTarget const*, std::string const&,
  167. std::vector<std::string> const&, ImportPropertyMap& properties,
  168. ImportLinkPropertyTargetNames);
  169. extern template void cmExportFileGenerator::SetImportLinkProperty<cmLinkItem>(
  170. std::string const&, cmGeneratorTarget const*, std::string const&,
  171. std::vector<cmLinkItem> const&, ImportPropertyMap& properties,
  172. ImportLinkPropertyTargetNames);