cmExportFileGenerator.h 10 KB

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