cmExportFileGenerator.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 cmFileSet;
  15. class cmGeneratorTarget;
  16. class cmLocalGenerator;
  17. class cmTargetExport;
  18. #define STRINGIFY_HELPER(X) #X
  19. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  20. #define DEVEL_CMAKE_VERSION(major, minor) \
  21. (CMake_VERSION_ENCODE(major, minor, 0) > \
  22. CMake_VERSION_ENCODE(CMake_VERSION_MAJOR, CMake_VERSION_MINOR, 0) \
  23. ? STRINGIFY(CMake_VERSION_MAJOR) "." STRINGIFY( \
  24. CMake_VERSION_MINOR) "." STRINGIFY(CMake_VERSION_PATCH) \
  25. : #major "." #minor ".0")
  26. /** \class cmExportFileGenerator
  27. * \brief Generate a file exporting targets from a build or install tree.
  28. *
  29. * cmExportFileGenerator is the superclass for
  30. * cmExportBuildFileGenerator and cmExportInstallFileGenerator. It
  31. * contains common code generation routines for the two kinds of
  32. * export implementations.
  33. */
  34. class cmExportFileGenerator
  35. {
  36. public:
  37. cmExportFileGenerator();
  38. virtual ~cmExportFileGenerator() = default;
  39. /** Set the full path to the export file to generate. */
  40. void SetExportFile(const char* mainFile);
  41. const std::string& GetMainExportFileName() const;
  42. /** Set the namespace in which to place exported target names. */
  43. void SetNamespace(const std::string& ns) { this->Namespace = ns; }
  44. std::string GetNamespace() const { return this->Namespace; }
  45. void SetExportOld(bool exportOld) { this->ExportOld = exportOld; }
  46. /** Add a configuration to be exported. */
  47. void AddConfiguration(const std::string& config);
  48. /** Actually generate the export file. Returns whether there was an
  49. error. */
  50. bool GenerateImportFile();
  51. protected:
  52. using ImportPropertyMap = std::map<std::string, std::string>;
  53. // Generate per-configuration target information to the given output
  54. // stream.
  55. void GenerateImportConfig(std::ostream& os, const std::string& config);
  56. // Methods to implement export file code generation.
  57. virtual void GeneratePolicyHeaderCode(std::ostream& os);
  58. virtual void GeneratePolicyFooterCode(std::ostream& os);
  59. virtual void GenerateImportHeaderCode(std::ostream& os,
  60. const std::string& config = "");
  61. virtual void GenerateImportFooterCode(std::ostream& os);
  62. void GenerateImportVersionCode(std::ostream& os);
  63. virtual void GenerateImportTargetCode(std::ostream& os,
  64. cmGeneratorTarget const* target,
  65. cmStateEnums::TargetType targetType);
  66. virtual void GenerateImportPropertyCode(std::ostream& os,
  67. const std::string& config,
  68. cmGeneratorTarget const* target,
  69. ImportPropertyMap const& properties);
  70. virtual void GenerateImportedFileChecksCode(
  71. std::ostream& os, cmGeneratorTarget* target,
  72. ImportPropertyMap const& properties,
  73. const std::set<std::string>& importedLocations);
  74. virtual void GenerateImportedFileCheckLoop(std::ostream& os);
  75. virtual void GenerateMissingTargetsCheckCode(std::ostream& os);
  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. 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. ImportLinkPropertyTargetNames targetNames);
  96. /** Each subclass knows how to generate its kind of export file. */
  97. virtual bool GenerateMainFile(std::ostream& os) = 0;
  98. /** Each subclass knows where the target files are located. */
  99. virtual void GenerateImportTargetsConfig(std::ostream& os,
  100. const std::string& config,
  101. std::string const& suffix) = 0;
  102. /** Each subclass knows how to deal with a target that is missing from an
  103. * export set. */
  104. virtual void HandleMissingTarget(std::string& link_libs,
  105. cmGeneratorTarget const* depender,
  106. cmGeneratorTarget* dependee) = 0;
  107. void PopulateInterfaceProperty(const std::string&,
  108. cmGeneratorTarget const* target,
  109. cmGeneratorExpression::PreprocessContext,
  110. ImportPropertyMap& properties);
  111. bool PopulateInterfaceLinkLibrariesProperty(
  112. cmGeneratorTarget const* target, cmGeneratorExpression::PreprocessContext,
  113. ImportPropertyMap& properties);
  114. void PopulateInterfaceProperty(const std::string& propName,
  115. cmGeneratorTarget const* target,
  116. ImportPropertyMap& properties);
  117. void PopulateCompatibleInterfaceProperties(cmGeneratorTarget const* target,
  118. ImportPropertyMap& properties);
  119. virtual void GenerateInterfaceProperties(
  120. cmGeneratorTarget const* target, std::ostream& os,
  121. const ImportPropertyMap& properties);
  122. void PopulateIncludeDirectoriesInterface(
  123. cmGeneratorTarget const* target,
  124. cmGeneratorExpression::PreprocessContext preprocessRule,
  125. ImportPropertyMap& properties, cmTargetExport const& te,
  126. std::string& includesDestinationDirs);
  127. void PopulateSourcesInterface(
  128. cmGeneratorTarget const* target,
  129. cmGeneratorExpression::PreprocessContext preprocessRule,
  130. ImportPropertyMap& properties);
  131. void PopulateLinkDirectoriesInterface(
  132. cmGeneratorTarget const* target,
  133. cmGeneratorExpression::PreprocessContext preprocessRule,
  134. ImportPropertyMap& properties);
  135. void PopulateLinkDependsInterface(
  136. cmGeneratorTarget const* target,
  137. cmGeneratorExpression::PreprocessContext preprocessRule,
  138. ImportPropertyMap& properties);
  139. void SetImportLinkInterface(
  140. const std::string& config, std::string const& suffix,
  141. cmGeneratorExpression::PreprocessContext preprocessRule,
  142. cmGeneratorTarget const* target, ImportPropertyMap& properties);
  143. enum FreeTargetsReplace
  144. {
  145. ReplaceFreeTargets,
  146. NoReplaceFreeTargets
  147. };
  148. void ResolveTargetsInGeneratorExpressions(
  149. std::string& input, cmGeneratorTarget const* target,
  150. FreeTargetsReplace replace = NoReplaceFreeTargets);
  151. bool PopulateCxxModuleExportProperties(
  152. cmGeneratorTarget const* gte, ImportPropertyMap& properties,
  153. cmGeneratorExpression::PreprocessContext ctx,
  154. std::string const& includesDestinationDirs, std::string& errorMessage);
  155. bool PopulateExportProperties(cmGeneratorTarget const* gte,
  156. ImportPropertyMap& properties,
  157. std::string& errorMessage);
  158. void GenerateTargetFileSets(cmGeneratorTarget* gte, std::ostream& os,
  159. cmTargetExport* te = nullptr);
  160. void GenerateCxxModuleInformation(std::ostream& os);
  161. virtual std::string GetFileSetDirectories(cmGeneratorTarget* gte,
  162. cmFileSet* fileSet,
  163. cmTargetExport* te) = 0;
  164. virtual std::string GetFileSetFiles(cmGeneratorTarget* gte,
  165. cmFileSet* fileSet,
  166. cmTargetExport* te) = 0;
  167. void SetRequiredCMakeVersion(unsigned int major, unsigned int minor,
  168. unsigned int patch);
  169. // The namespace in which the exports are placed in the generated file.
  170. std::string Namespace;
  171. bool ExportOld;
  172. // The set of configurations to export.
  173. std::vector<std::string> Configurations;
  174. // The file to generate.
  175. std::string MainImportFile;
  176. std::string FileDir;
  177. std::string FileBase;
  178. std::string FileExt;
  179. bool AppendMode;
  180. // The set of targets included in the export.
  181. std::set<cmGeneratorTarget*> ExportedTargets;
  182. std::vector<std::string> MissingTargets;
  183. unsigned int RequiredCMakeVersionMajor = 2;
  184. unsigned int RequiredCMakeVersionMinor = 8;
  185. unsigned int RequiredCMakeVersionPatch = 3;
  186. private:
  187. void PopulateInterfaceProperty(const std::string&, const std::string&,
  188. cmGeneratorTarget const* target,
  189. cmGeneratorExpression::PreprocessContext,
  190. ImportPropertyMap& properties);
  191. bool AddTargetNamespace(std::string& input, cmGeneratorTarget const* target,
  192. cmLocalGenerator const* lg);
  193. void ResolveTargetsInGeneratorExpression(std::string& input,
  194. cmGeneratorTarget const* target,
  195. cmLocalGenerator const* lg);
  196. virtual void ReplaceInstallPrefix(std::string& input);
  197. virtual std::string InstallNameDir(cmGeneratorTarget const* target,
  198. const std::string& config) = 0;
  199. virtual std::string GetCxxModulesDirectory() const = 0;
  200. virtual void GenerateCxxModuleConfigInformation(std::ostream& os) const = 0;
  201. };