cmExportFileGenerator.h 8.7 KB

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