cmExportFileGenerator.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. void GenerateImportConfig(std::ostream& os, std::string const& config);
  72. /** Each subclass knows where the target files are located. */
  73. virtual void GenerateImportTargetsConfig(std::ostream& os,
  74. std::string const& config,
  75. std::string const& suffix) = 0;
  76. /** Each subclass knows how to deal with a target that is missing from an
  77. * export set. */
  78. virtual void HandleMissingTarget(std::string& link_libs,
  79. cmGeneratorTarget const* depender,
  80. cmGeneratorTarget* dependee) = 0;
  81. /** Complain when a duplicate target is encountered. */
  82. virtual void ComplainAboutDuplicateTarget(
  83. std::string const& targetName) const = 0;
  84. virtual cm::string_view GetImportPrefixWithSlash() const = 0;
  85. void AddImportPrefix(std::string& exportDirs) const;
  86. void PopulateInterfaceProperty(std::string const& propName,
  87. cmGeneratorTarget const* target,
  88. ImportPropertyMap& properties) const;
  89. void PopulateInterfaceProperty(std::string const& propName,
  90. cmGeneratorTarget const* target,
  91. cmGeneratorExpression::PreprocessContext,
  92. ImportPropertyMap& properties);
  93. bool PopulateInterfaceLinkLibrariesProperty(
  94. cmGeneratorTarget const* target, cmGeneratorExpression::PreprocessContext,
  95. ImportPropertyMap& properties);
  96. bool PopulateInterfaceProperties(
  97. cmGeneratorTarget const* target,
  98. std::string const& includesDestinationDirs,
  99. cmGeneratorExpression::PreprocessContext preprocessRule,
  100. ImportPropertyMap& properties);
  101. virtual void ReportError(std::string const& errorMessage) const = 0;
  102. enum FreeTargetsReplace
  103. {
  104. ReplaceFreeTargets,
  105. NoReplaceFreeTargets
  106. };
  107. void ResolveTargetsInGeneratorExpressions(
  108. std::string& input, cmGeneratorTarget const* target,
  109. FreeTargetsReplace replace = NoReplaceFreeTargets);
  110. virtual cmExportSet* GetExportSet() const { return nullptr; }
  111. virtual void ReplaceInstallPrefix(std::string& input) const;
  112. virtual std::string InstallNameDir(cmGeneratorTarget const* target,
  113. std::string const& config) = 0;
  114. /** Get the temporary location of the config-agnostic C++ module file. */
  115. virtual std::string GetCxxModuleFile(std::string const& name) const = 0;
  116. virtual std::string GetCxxModulesDirectory() const = 0;
  117. virtual void GenerateCxxModuleConfigInformation(std::string const&,
  118. std::ostream& os) const = 0;
  119. bool AddTargetNamespace(std::string& input, cmGeneratorTarget const* target,
  120. cmLocalGenerator const* lg);
  121. // The namespace in which the exports are placed in the generated file.
  122. std::string Namespace;
  123. // The set of configurations to export.
  124. std::vector<std::string> Configurations;
  125. // The file to generate.
  126. std::string MainImportFile;
  127. std::string FileDir;
  128. std::string FileBase;
  129. std::string FileExt;
  130. bool AppendMode = false;
  131. // The set of targets included in the export.
  132. std::set<cmGeneratorTarget const*> ExportedTargets;
  133. std::vector<std::string> MissingTargets;
  134. std::set<cmGeneratorTarget const*> ExternalTargets;
  135. private:
  136. void PopulateInterfaceProperty(std::string const& propName,
  137. std::string const& outputName,
  138. cmGeneratorTarget const* target,
  139. cmGeneratorExpression::PreprocessContext,
  140. ImportPropertyMap& properties);
  141. void PopulateCompatibleInterfaceProperties(
  142. cmGeneratorTarget const* target, ImportPropertyMap& properties) const;
  143. void PopulateCustomTransitiveInterfaceProperties(
  144. cmGeneratorTarget const* target,
  145. cmGeneratorExpression::PreprocessContext preprocessRule,
  146. ImportPropertyMap& properties);
  147. bool PopulateCxxModuleExportProperties(
  148. cmGeneratorTarget const* gte, ImportPropertyMap& properties,
  149. cmGeneratorExpression::PreprocessContext ctx,
  150. std::string const& includesDestinationDirs, std::string& errorMessage);
  151. bool PopulateExportProperties(cmGeneratorTarget const* gte,
  152. ImportPropertyMap& properties,
  153. std::string& errorMessage) const;
  154. void ResolveTargetsInGeneratorExpression(std::string& input,
  155. cmGeneratorTarget const* target,
  156. cmLocalGenerator const* lg);
  157. };
  158. extern template void cmExportFileGenerator::SetImportLinkProperty<std::string>(
  159. std::string const&, cmGeneratorTarget const*, std::string const&,
  160. std::vector<std::string> const&, ImportPropertyMap& properties,
  161. ImportLinkPropertyTargetNames);
  162. extern template void cmExportFileGenerator::SetImportLinkProperty<cmLinkItem>(
  163. std::string const&, cmGeneratorTarget const*, std::string const&,
  164. std::vector<cmLinkItem> const&, ImportPropertyMap& properties,
  165. ImportLinkPropertyTargetNames);