cmExportFileGenerator.h 9.3 KB

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