cmExportFileGenerator.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "cmGeneratorExpression.h"
  11. #include "cmStateTypes.h"
  12. #include "cmVersion.h"
  13. #include "cmVersionConfig.h"
  14. class cmGeneratorTarget;
  15. class cmTargetExport;
  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 a file exporting targets from a build or install tree.
  26. *
  27. * cmExportFileGenerator is the superclass for
  28. * cmExportBuildFileGenerator and cmExportInstallFileGenerator. It
  29. * contains common code generation routines for the two kinds of
  30. * export implementations.
  31. */
  32. class cmExportFileGenerator
  33. {
  34. public:
  35. cmExportFileGenerator();
  36. virtual ~cmExportFileGenerator() = default;
  37. /** Set the full path to the export file to generate. */
  38. void SetExportFile(const char* mainFile);
  39. const std::string& GetMainExportFileName() const;
  40. /** Set the namespace in which to place exported target names. */
  41. void SetNamespace(const std::string& ns) { this->Namespace = ns; }
  42. std::string GetNamespace() const { return this->Namespace; }
  43. void SetExportOld(bool exportOld) { this->ExportOld = exportOld; }
  44. /** Add a configuration to be exported. */
  45. void AddConfiguration(const std::string& config);
  46. /** Actually generate the export file. Returns whether there was an
  47. error. */
  48. bool GenerateImportFile();
  49. protected:
  50. using ImportPropertyMap = std::map<std::string, std::string>;
  51. // Generate per-configuration target information to the given output
  52. // stream.
  53. void GenerateImportConfig(std::ostream& os, const std::string& config,
  54. std::vector<std::string>& missingTargets);
  55. // Methods to implement export file code generation.
  56. virtual void GeneratePolicyHeaderCode(std::ostream& os);
  57. virtual void GeneratePolicyFooterCode(std::ostream& os);
  58. virtual void GenerateImportHeaderCode(std::ostream& os,
  59. const std::string& config = "");
  60. virtual void GenerateImportFooterCode(std::ostream& os);
  61. void GenerateImportVersionCode(std::ostream& os);
  62. virtual void GenerateImportTargetCode(std::ostream& os,
  63. cmGeneratorTarget const* target,
  64. cmStateEnums::TargetType targetType);
  65. virtual void GenerateImportPropertyCode(std::ostream& os,
  66. const std::string& config,
  67. cmGeneratorTarget const* target,
  68. ImportPropertyMap const& properties);
  69. virtual void GenerateImportedFileChecksCode(
  70. std::ostream& os, cmGeneratorTarget* target,
  71. ImportPropertyMap const& properties,
  72. const std::set<std::string>& importedLocations);
  73. virtual void GenerateImportedFileCheckLoop(std::ostream& os);
  74. virtual void GenerateMissingTargetsCheckCode(
  75. std::ostream& os, const std::vector<std::string>& missingTargets);
  76. virtual void GenerateExpectedTargetsCode(std::ostream& os,
  77. const std::string& expectedTargets);
  78. // Collect properties with detailed information about targets beyond
  79. // their location on disk.
  80. void SetImportDetailProperties(const std::string& config,
  81. std::string const& suffix,
  82. cmGeneratorTarget* target,
  83. ImportPropertyMap& properties,
  84. std::vector<std::string>& missingTargets);
  85. enum class ImportLinkPropertyTargetNames
  86. {
  87. Yes,
  88. No,
  89. };
  90. template <typename T>
  91. void SetImportLinkProperty(std::string const& suffix,
  92. cmGeneratorTarget const* target,
  93. const std::string& propName,
  94. std::vector<T> const& entries,
  95. ImportPropertyMap& properties,
  96. std::vector<std::string>& missingTargets,
  97. ImportLinkPropertyTargetNames targetNames);
  98. /** Each subclass knows how to generate its kind of export file. */
  99. virtual bool GenerateMainFile(std::ostream& os) = 0;
  100. /** Each subclass knows where the target files are located. */
  101. virtual void GenerateImportTargetsConfig(
  102. std::ostream& os, const std::string& config, std::string const& suffix,
  103. std::vector<std::string>& missingTargets) = 0;
  104. /** Each subclass knows how to deal with a target that is missing from an
  105. * export set. */
  106. virtual void HandleMissingTarget(std::string& link_libs,
  107. std::vector<std::string>& missingTargets,
  108. cmGeneratorTarget const* depender,
  109. cmGeneratorTarget* dependee) = 0;
  110. void PopulateInterfaceProperty(const std::string&,
  111. cmGeneratorTarget const* target,
  112. cmGeneratorExpression::PreprocessContext,
  113. ImportPropertyMap& properties,
  114. std::vector<std::string>& missingTargets);
  115. bool PopulateInterfaceLinkLibrariesProperty(
  116. cmGeneratorTarget const* target, cmGeneratorExpression::PreprocessContext,
  117. ImportPropertyMap& properties, std::vector<std::string>& missingTargets);
  118. void PopulateInterfaceProperty(const std::string& propName,
  119. cmGeneratorTarget const* target,
  120. ImportPropertyMap& properties);
  121. void PopulateCompatibleInterfaceProperties(cmGeneratorTarget const* target,
  122. ImportPropertyMap& properties);
  123. virtual void GenerateInterfaceProperties(
  124. cmGeneratorTarget const* target, std::ostream& os,
  125. const ImportPropertyMap& properties);
  126. void PopulateIncludeDirectoriesInterface(
  127. cmGeneratorTarget const* target,
  128. cmGeneratorExpression::PreprocessContext preprocessRule,
  129. ImportPropertyMap& properties, std::vector<std::string>& missingTargets,
  130. cmTargetExport const& te);
  131. void PopulateSourcesInterface(
  132. cmGeneratorTarget const* target,
  133. cmGeneratorExpression::PreprocessContext preprocessRule,
  134. ImportPropertyMap& properties, std::vector<std::string>& missingTargets);
  135. void PopulateLinkDirectoriesInterface(
  136. cmGeneratorTarget const* target,
  137. cmGeneratorExpression::PreprocessContext preprocessRule,
  138. ImportPropertyMap& properties, std::vector<std::string>& missingTargets);
  139. void PopulateLinkDependsInterface(
  140. cmGeneratorTarget const* target,
  141. cmGeneratorExpression::PreprocessContext preprocessRule,
  142. ImportPropertyMap& properties, std::vector<std::string>& missingTargets);
  143. void SetImportLinkInterface(
  144. const std::string& config, std::string const& suffix,
  145. cmGeneratorExpression::PreprocessContext preprocessRule,
  146. cmGeneratorTarget const* target, ImportPropertyMap& properties,
  147. std::vector<std::string>& missingTargets);
  148. enum FreeTargetsReplace
  149. {
  150. ReplaceFreeTargets,
  151. NoReplaceFreeTargets
  152. };
  153. void ResolveTargetsInGeneratorExpressions(
  154. std::string& input, cmGeneratorTarget const* target,
  155. std::vector<std::string>& missingTargets,
  156. FreeTargetsReplace replace = NoReplaceFreeTargets);
  157. virtual void GenerateRequiredCMakeVersion(std::ostream& os,
  158. const char* versionString);
  159. bool PopulateExportProperties(cmGeneratorTarget const* gte,
  160. ImportPropertyMap& properties,
  161. std::string& errorMessage);
  162. // The namespace in which the exports are placed in the generated file.
  163. std::string Namespace;
  164. bool ExportOld;
  165. // The set of configurations to export.
  166. std::vector<std::string> Configurations;
  167. // The file to generate.
  168. std::string MainImportFile;
  169. std::string FileDir;
  170. std::string FileBase;
  171. std::string FileExt;
  172. bool AppendMode;
  173. // The set of targets included in the export.
  174. std::set<cmGeneratorTarget*> ExportedTargets;
  175. private:
  176. void PopulateInterfaceProperty(const std::string&, const std::string&,
  177. cmGeneratorTarget const* target,
  178. cmGeneratorExpression::PreprocessContext,
  179. ImportPropertyMap& properties,
  180. std::vector<std::string>& missingTargets);
  181. bool AddTargetNamespace(std::string& input, cmGeneratorTarget const* target,
  182. std::vector<std::string>& missingTargets);
  183. void ResolveTargetsInGeneratorExpression(
  184. std::string& input, cmGeneratorTarget const* target,
  185. std::vector<std::string>& missingTargets);
  186. virtual void ReplaceInstallPrefix(std::string& input);
  187. virtual std::string InstallNameDir(cmGeneratorTarget const* target,
  188. const std::string& config) = 0;
  189. };