cmExportFileGenerator.h 8.3 KB

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