cmExportFileGenerator.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. void PopulateSourcesInterface(
  127. cmGeneratorTarget const* target,
  128. cmGeneratorExpression::PreprocessContext preprocessRule,
  129. ImportPropertyMap& properties);
  130. void PopulateLinkDirectoriesInterface(
  131. cmGeneratorTarget const* target,
  132. cmGeneratorExpression::PreprocessContext preprocessRule,
  133. ImportPropertyMap& properties);
  134. void PopulateLinkDependsInterface(
  135. cmGeneratorTarget const* target,
  136. cmGeneratorExpression::PreprocessContext preprocessRule,
  137. ImportPropertyMap& properties);
  138. void SetImportLinkInterface(
  139. const std::string& config, std::string const& suffix,
  140. cmGeneratorExpression::PreprocessContext preprocessRule,
  141. cmGeneratorTarget const* target, ImportPropertyMap& properties);
  142. enum FreeTargetsReplace
  143. {
  144. ReplaceFreeTargets,
  145. NoReplaceFreeTargets
  146. };
  147. void ResolveTargetsInGeneratorExpressions(
  148. std::string& input, cmGeneratorTarget const* target,
  149. FreeTargetsReplace replace = NoReplaceFreeTargets);
  150. virtual void GenerateRequiredCMakeVersion(std::ostream& os,
  151. const char* versionString);
  152. bool PopulateCxxModuleExportProperties(
  153. cmGeneratorTarget const* gte, ImportPropertyMap& properties,
  154. cmGeneratorExpression::PreprocessContext ctx, 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. // The namespace in which the exports are placed in the generated file.
  168. std::string Namespace;
  169. bool ExportOld;
  170. // The set of configurations to export.
  171. std::vector<std::string> Configurations;
  172. // The file to generate.
  173. std::string MainImportFile;
  174. std::string FileDir;
  175. std::string FileBase;
  176. std::string FileExt;
  177. bool AppendMode;
  178. // The set of targets included in the export.
  179. std::set<cmGeneratorTarget*> ExportedTargets;
  180. std::vector<std::string> MissingTargets;
  181. private:
  182. void PopulateInterfaceProperty(const std::string&, const std::string&,
  183. cmGeneratorTarget const* target,
  184. cmGeneratorExpression::PreprocessContext,
  185. ImportPropertyMap& properties);
  186. bool AddTargetNamespace(std::string& input, cmGeneratorTarget const* target,
  187. cmLocalGenerator const* lg);
  188. void ResolveTargetsInGeneratorExpression(std::string& input,
  189. cmGeneratorTarget const* target,
  190. cmLocalGenerator const* lg);
  191. virtual void ReplaceInstallPrefix(std::string& input);
  192. virtual std::string InstallNameDir(cmGeneratorTarget const* target,
  193. const std::string& config) = 0;
  194. virtual std::string GetCxxModulesDirectory() const = 0;
  195. virtual void GenerateCxxModuleConfigInformation(std::ostream& os) const = 0;
  196. };