cmExportFileGenerator.h 8.4 KB

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