cmExportFileGenerator.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. std::vector<std::string>& missingTargets);
  57. // Methods to implement export file code generation.
  58. virtual void GeneratePolicyHeaderCode(std::ostream& os);
  59. virtual void GeneratePolicyFooterCode(std::ostream& os);
  60. virtual void GenerateImportHeaderCode(std::ostream& os,
  61. const std::string& config = "");
  62. virtual void GenerateImportFooterCode(std::ostream& os);
  63. void GenerateImportVersionCode(std::ostream& os);
  64. virtual void GenerateImportTargetCode(std::ostream& os,
  65. cmGeneratorTarget const* target,
  66. cmStateEnums::TargetType targetType);
  67. virtual void GenerateImportPropertyCode(std::ostream& os,
  68. const std::string& config,
  69. cmGeneratorTarget const* target,
  70. ImportPropertyMap const& properties);
  71. virtual void GenerateImportedFileChecksCode(
  72. std::ostream& os, cmGeneratorTarget* target,
  73. ImportPropertyMap const& properties,
  74. const std::set<std::string>& importedLocations);
  75. virtual void GenerateImportedFileCheckLoop(std::ostream& os);
  76. virtual void GenerateMissingTargetsCheckCode(
  77. std::ostream& os, const std::vector<std::string>& missingTargets);
  78. virtual void GenerateExpectedTargetsCode(std::ostream& os,
  79. const std::string& expectedTargets);
  80. // Collect properties with detailed information about targets beyond
  81. // their location on disk.
  82. void SetImportDetailProperties(const std::string& config,
  83. std::string const& suffix,
  84. cmGeneratorTarget* target,
  85. ImportPropertyMap& properties,
  86. std::vector<std::string>& missingTargets);
  87. enum class ImportLinkPropertyTargetNames
  88. {
  89. Yes,
  90. No,
  91. };
  92. template <typename T>
  93. void SetImportLinkProperty(std::string const& suffix,
  94. cmGeneratorTarget const* target,
  95. const std::string& propName,
  96. std::vector<T> const& entries,
  97. ImportPropertyMap& properties,
  98. std::vector<std::string>& missingTargets,
  99. ImportLinkPropertyTargetNames targetNames);
  100. /** Each subclass knows how to generate its kind of export file. */
  101. virtual bool GenerateMainFile(std::ostream& os) = 0;
  102. /** Each subclass knows where the target files are located. */
  103. virtual void GenerateImportTargetsConfig(
  104. std::ostream& os, const std::string& config, std::string const& suffix,
  105. std::vector<std::string>& missingTargets) = 0;
  106. /** Each subclass knows how to deal with a target that is missing from an
  107. * export set. */
  108. virtual void HandleMissingTarget(std::string& link_libs,
  109. std::vector<std::string>& missingTargets,
  110. cmGeneratorTarget const* depender,
  111. cmGeneratorTarget* dependee) = 0;
  112. void PopulateInterfaceProperty(const std::string&,
  113. cmGeneratorTarget const* target,
  114. cmGeneratorExpression::PreprocessContext,
  115. ImportPropertyMap& properties,
  116. std::vector<std::string>& missingTargets);
  117. bool PopulateInterfaceLinkLibrariesProperty(
  118. cmGeneratorTarget const* target, cmGeneratorExpression::PreprocessContext,
  119. ImportPropertyMap& properties, std::vector<std::string>& missingTargets);
  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, std::vector<std::string>& missingTargets,
  132. cmTargetExport const& te);
  133. void PopulateSourcesInterface(
  134. cmGeneratorTarget const* target,
  135. cmGeneratorExpression::PreprocessContext preprocessRule,
  136. ImportPropertyMap& properties, std::vector<std::string>& missingTargets);
  137. void PopulateLinkDirectoriesInterface(
  138. cmGeneratorTarget const* target,
  139. cmGeneratorExpression::PreprocessContext preprocessRule,
  140. ImportPropertyMap& properties, std::vector<std::string>& missingTargets);
  141. void PopulateLinkDependsInterface(
  142. cmGeneratorTarget const* target,
  143. cmGeneratorExpression::PreprocessContext preprocessRule,
  144. ImportPropertyMap& properties, std::vector<std::string>& missingTargets);
  145. void SetImportLinkInterface(
  146. const std::string& config, std::string const& suffix,
  147. cmGeneratorExpression::PreprocessContext preprocessRule,
  148. cmGeneratorTarget const* target, ImportPropertyMap& properties,
  149. std::vector<std::string>& missingTargets);
  150. enum FreeTargetsReplace
  151. {
  152. ReplaceFreeTargets,
  153. NoReplaceFreeTargets
  154. };
  155. void ResolveTargetsInGeneratorExpressions(
  156. std::string& input, cmGeneratorTarget const* target,
  157. std::vector<std::string>& missingTargets,
  158. FreeTargetsReplace replace = NoReplaceFreeTargets);
  159. virtual void GenerateRequiredCMakeVersion(std::ostream& os,
  160. const char* versionString);
  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. virtual std::string GetFileSetDirectories(cmGeneratorTarget* gte,
  167. cmFileSet* fileSet,
  168. cmTargetExport* te) = 0;
  169. virtual std::string GetFileSetFiles(cmGeneratorTarget* gte,
  170. cmFileSet* fileSet,
  171. cmTargetExport* te) = 0;
  172. // The namespace in which the exports are placed in the generated file.
  173. std::string Namespace;
  174. bool ExportOld;
  175. // The set of configurations to export.
  176. std::vector<std::string> Configurations;
  177. // The file to generate.
  178. std::string MainImportFile;
  179. std::string FileDir;
  180. std::string FileBase;
  181. std::string FileExt;
  182. bool AppendMode;
  183. // The set of targets included in the export.
  184. std::set<cmGeneratorTarget*> ExportedTargets;
  185. private:
  186. void PopulateInterfaceProperty(const std::string&, const std::string&,
  187. cmGeneratorTarget const* target,
  188. cmGeneratorExpression::PreprocessContext,
  189. ImportPropertyMap& properties,
  190. std::vector<std::string>& missingTargets);
  191. bool AddTargetNamespace(std::string& input, cmGeneratorTarget const* target,
  192. cmLocalGenerator const* lg,
  193. std::vector<std::string>& missingTargets);
  194. void ResolveTargetsInGeneratorExpression(
  195. std::string& input, cmGeneratorTarget const* target,
  196. cmLocalGenerator const* lg, std::vector<std::string>& missingTargets);
  197. virtual void ReplaceInstallPrefix(std::string& input);
  198. virtual std::string InstallNameDir(cmGeneratorTarget const* target,
  199. const std::string& config) = 0;
  200. };