cmExportFileGenerator.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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(
  72. std::ostream& os, const std::string& config, const std::string& suffix,
  73. cmGeneratorTarget const* target, ImportPropertyMap const& properties,
  74. const std::string& importedXcFrameworkLocation);
  75. virtual void GenerateImportedFileChecksCode(
  76. std::ostream& os, cmGeneratorTarget* target,
  77. ImportPropertyMap const& properties,
  78. const std::set<std::string>& importedLocations,
  79. const std::string& importedXcFrameworkLocation);
  80. virtual void GenerateImportedFileCheckLoop(std::ostream& os);
  81. virtual void GenerateMissingTargetsCheckCode(std::ostream& os);
  82. virtual void GenerateFindDependencyCalls(std::ostream& os);
  83. virtual void GenerateExpectedTargetsCode(std::ostream& os,
  84. const std::string& expectedTargets);
  85. // Collect properties with detailed information about targets beyond
  86. // their location on disk.
  87. void SetImportDetailProperties(const std::string& config,
  88. std::string const& suffix,
  89. cmGeneratorTarget* target,
  90. ImportPropertyMap& properties);
  91. enum class ImportLinkPropertyTargetNames
  92. {
  93. Yes,
  94. No,
  95. };
  96. template <typename T>
  97. void SetImportLinkProperty(std::string const& suffix,
  98. cmGeneratorTarget const* target,
  99. const std::string& propName,
  100. std::vector<T> const& entries,
  101. ImportPropertyMap& properties,
  102. ImportLinkPropertyTargetNames targetNames);
  103. /** Each subclass knows how to generate its kind of export file. */
  104. virtual bool GenerateMainFile(std::ostream& os) = 0;
  105. /** Each subclass knows where the target files are located. */
  106. virtual void GenerateImportTargetsConfig(std::ostream& os,
  107. const std::string& config,
  108. std::string const& suffix) = 0;
  109. /** Each subclass knows how to deal with a target that is missing from an
  110. * export set. */
  111. virtual void HandleMissingTarget(std::string& link_libs,
  112. cmGeneratorTarget const* depender,
  113. cmGeneratorTarget* dependee) = 0;
  114. void PopulateInterfaceProperty(const std::string&,
  115. cmGeneratorTarget const* target,
  116. cmGeneratorExpression::PreprocessContext,
  117. ImportPropertyMap& properties);
  118. bool PopulateInterfaceLinkLibrariesProperty(
  119. cmGeneratorTarget const* target, cmGeneratorExpression::PreprocessContext,
  120. ImportPropertyMap& properties);
  121. void PopulateInterfaceProperty(const std::string& propName,
  122. cmGeneratorTarget const* target,
  123. ImportPropertyMap& properties);
  124. void PopulateCompatibleInterfaceProperties(cmGeneratorTarget const* target,
  125. ImportPropertyMap& properties);
  126. virtual void GenerateInterfaceProperties(
  127. cmGeneratorTarget const* target, std::ostream& os,
  128. const ImportPropertyMap& properties);
  129. void PopulateIncludeDirectoriesInterface(
  130. cmGeneratorTarget const* target,
  131. cmGeneratorExpression::PreprocessContext preprocessRule,
  132. ImportPropertyMap& properties, cmTargetExport const& te,
  133. std::string& includesDestinationDirs);
  134. void PopulateSourcesInterface(
  135. cmGeneratorTarget const* target,
  136. cmGeneratorExpression::PreprocessContext preprocessRule,
  137. ImportPropertyMap& properties);
  138. void PopulateLinkDirectoriesInterface(
  139. cmGeneratorTarget const* target,
  140. cmGeneratorExpression::PreprocessContext preprocessRule,
  141. ImportPropertyMap& properties);
  142. void PopulateLinkDependsInterface(
  143. cmGeneratorTarget const* target,
  144. cmGeneratorExpression::PreprocessContext preprocessRule,
  145. ImportPropertyMap& properties);
  146. void SetImportLinkInterface(
  147. const std::string& config, std::string const& suffix,
  148. cmGeneratorExpression::PreprocessContext preprocessRule,
  149. cmGeneratorTarget const* target, ImportPropertyMap& properties);
  150. enum FreeTargetsReplace
  151. {
  152. ReplaceFreeTargets,
  153. NoReplaceFreeTargets
  154. };
  155. void ResolveTargetsInGeneratorExpressions(
  156. std::string& input, cmGeneratorTarget const* target,
  157. FreeTargetsReplace replace = NoReplaceFreeTargets);
  158. bool PopulateCxxModuleExportProperties(
  159. cmGeneratorTarget const* gte, ImportPropertyMap& properties,
  160. cmGeneratorExpression::PreprocessContext ctx,
  161. std::string const& includesDestinationDirs, std::string& errorMessage);
  162. bool PopulateExportProperties(cmGeneratorTarget const* gte,
  163. ImportPropertyMap& properties,
  164. std::string& errorMessage);
  165. void GenerateTargetFileSets(cmGeneratorTarget* gte, std::ostream& os,
  166. cmTargetExport* te = nullptr);
  167. void GenerateCxxModuleInformation(std::ostream& os);
  168. virtual std::string GetFileSetDirectories(cmGeneratorTarget* gte,
  169. cmFileSet* fileSet,
  170. cmTargetExport* te) = 0;
  171. virtual std::string GetFileSetFiles(cmGeneratorTarget* gte,
  172. cmFileSet* fileSet,
  173. cmTargetExport* te) = 0;
  174. virtual cmExportSet* GetExportSet() const { return nullptr; }
  175. void SetRequiredCMakeVersion(unsigned int major, unsigned int minor,
  176. unsigned int patch);
  177. // The namespace in which the exports are placed in the generated file.
  178. std::string Namespace;
  179. bool ExportOld;
  180. // The set of configurations to export.
  181. std::vector<std::string> Configurations;
  182. // The file to generate.
  183. std::string MainImportFile;
  184. std::string FileDir;
  185. std::string FileBase;
  186. std::string FileExt;
  187. bool AppendMode;
  188. // The set of targets included in the export.
  189. std::set<cmGeneratorTarget*> ExportedTargets;
  190. std::vector<std::string> MissingTargets;
  191. std::set<cmGeneratorTarget const*> ExternalTargets;
  192. unsigned int RequiredCMakeVersionMajor = 2;
  193. unsigned int RequiredCMakeVersionMinor = 8;
  194. unsigned int RequiredCMakeVersionPatch = 3;
  195. bool ExportPackageDependencies = false;
  196. private:
  197. void PopulateInterfaceProperty(const std::string&, const std::string&,
  198. cmGeneratorTarget const* target,
  199. cmGeneratorExpression::PreprocessContext,
  200. ImportPropertyMap& properties);
  201. bool AddTargetNamespace(std::string& input, cmGeneratorTarget const* target,
  202. cmLocalGenerator const* lg);
  203. void ResolveTargetsInGeneratorExpression(std::string& input,
  204. cmGeneratorTarget const* target,
  205. cmLocalGenerator const* lg);
  206. virtual void ReplaceInstallPrefix(std::string& input);
  207. virtual std::string InstallNameDir(cmGeneratorTarget const* target,
  208. const std::string& config) = 0;
  209. virtual std::string GetCxxModulesDirectory() const = 0;
  210. virtual void GenerateCxxModuleConfigInformation(std::ostream& os) const = 0;
  211. };