cmExportFileGenerator.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmExportFileGenerator_h
  4. #define cmExportFileGenerator_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include "cmGeneratorExpression.h"
  7. #include "cmStateTypes.h"
  8. #include "cmVersion.h"
  9. #include "cmVersionConfig.h"
  10. #include <iosfwd>
  11. #include <map>
  12. #include <set>
  13. #include <string>
  14. #include <vector>
  15. class cmGeneratorTarget;
  16. #define STRINGIFY_HELPER(X) #X
  17. #define STRINGIFY(X) STRINGIFY_HELPER(X)
  18. #define DEVEL_CMAKE_VERSION(major, minor) \
  19. (CMake_VERSION_ENCODE(major, minor, 0) > \
  20. CMake_VERSION_ENCODE(CMake_VERSION_MAJOR, CMake_VERSION_MINOR, 0) \
  21. ? STRINGIFY(CMake_VERSION_MAJOR) "." STRINGIFY( \
  22. CMake_VERSION_MINOR) "." STRINGIFY(CMake_VERSION_PATCH) \
  23. : #major "." #minor ".0")
  24. class cmTargetExport;
  25. /** \class cmExportFileGenerator
  26. * \brief Generate a file exporting targets from a build or install tree.
  27. *
  28. * cmExportFileGenerator is the superclass for
  29. * cmExportBuildFileGenerator and cmExportInstallFileGenerator. It
  30. * contains common code generation routines for the two kinds of
  31. * export implementations.
  32. */
  33. class cmExportFileGenerator
  34. {
  35. public:
  36. cmExportFileGenerator();
  37. virtual ~cmExportFileGenerator() {}
  38. /** Set the full path to the export file to generate. */
  39. void SetExportFile(const char* mainFile);
  40. const char* GetMainExportFileName() const;
  41. /** Set the namespace in which to place exported target names. */
  42. void SetNamespace(const std::string& ns) { this->Namespace = ns; }
  43. std::string GetNamespace() const { return this->Namespace; }
  44. void SetExportOld(bool exportOld) { this->ExportOld = exportOld; }
  45. /** Add a configuration to be exported. */
  46. void AddConfiguration(const std::string& config);
  47. /** Actually generate the export file. Returns whether there was an
  48. error. */
  49. bool GenerateImportFile();
  50. protected:
  51. typedef std::map<std::string, std::string> ImportPropertyMap;
  52. // Generate per-configuration target information to the given output
  53. // stream.
  54. void GenerateImportConfig(std::ostream& os, const std::string& config,
  55. std::vector<std::string>& missingTargets);
  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(
  76. std::ostream& os, const std::vector<std::string>& missingTargets);
  77. virtual void GenerateExpectedTargetsCode(std::ostream& os,
  78. const std::string& expectedTargets);
  79. // Collect properties with detailed information about targets beyond
  80. // their location on disk.
  81. void SetImportDetailProperties(const std::string& config,
  82. std::string const& suffix,
  83. cmGeneratorTarget* target,
  84. ImportPropertyMap& properties,
  85. std::vector<std::string>& missingTargets);
  86. template <typename T>
  87. void SetImportLinkProperty(std::string const& suffix,
  88. cmGeneratorTarget* target,
  89. const std::string& propName,
  90. std::vector<T> const& entries,
  91. ImportPropertyMap& properties,
  92. std::vector<std::string>& missingTargets);
  93. /** Each subclass knows how to generate its kind of export file. */
  94. virtual bool GenerateMainFile(std::ostream& os) = 0;
  95. /** Each subclass knows where the target files are located. */
  96. virtual void GenerateImportTargetsConfig(
  97. std::ostream& os, const std::string& config, std::string const& suffix,
  98. std::vector<std::string>& missingTargets) = 0;
  99. /** Each subclass knows how to deal with a target that is missing from an
  100. * export set. */
  101. virtual void HandleMissingTarget(std::string& link_libs,
  102. std::vector<std::string>& missingTargets,
  103. cmGeneratorTarget* depender,
  104. cmGeneratorTarget* dependee) = 0;
  105. void PopulateInterfaceProperty(const std::string&, cmGeneratorTarget* target,
  106. cmGeneratorExpression::PreprocessContext,
  107. ImportPropertyMap& properties,
  108. std::vector<std::string>& missingTargets);
  109. bool PopulateInterfaceLinkLibrariesProperty(
  110. cmGeneratorTarget* target, cmGeneratorExpression::PreprocessContext,
  111. ImportPropertyMap& properties, std::vector<std::string>& missingTargets);
  112. void PopulateInterfaceProperty(const std::string& propName,
  113. cmGeneratorTarget* target,
  114. ImportPropertyMap& properties);
  115. void PopulateCompatibleInterfaceProperties(cmGeneratorTarget* target,
  116. ImportPropertyMap& properties);
  117. virtual void GenerateInterfaceProperties(
  118. cmGeneratorTarget const* target, std::ostream& os,
  119. const ImportPropertyMap& properties);
  120. void PopulateIncludeDirectoriesInterface(
  121. cmTargetExport* target,
  122. cmGeneratorExpression::PreprocessContext preprocessRule,
  123. ImportPropertyMap& properties, std::vector<std::string>& missingTargets);
  124. void PopulateSourcesInterface(
  125. cmTargetExport* target,
  126. cmGeneratorExpression::PreprocessContext preprocessRule,
  127. ImportPropertyMap& properties, std::vector<std::string>& missingTargets);
  128. void SetImportLinkInterface(
  129. const std::string& config, std::string const& suffix,
  130. cmGeneratorExpression::PreprocessContext preprocessRule,
  131. cmGeneratorTarget* target, ImportPropertyMap& properties,
  132. std::vector<std::string>& missingTargets);
  133. enum FreeTargetsReplace
  134. {
  135. ReplaceFreeTargets,
  136. NoReplaceFreeTargets
  137. };
  138. void ResolveTargetsInGeneratorExpressions(
  139. std::string& input, cmGeneratorTarget* target,
  140. std::vector<std::string>& missingTargets,
  141. FreeTargetsReplace replace = NoReplaceFreeTargets);
  142. virtual void GenerateRequiredCMakeVersion(std::ostream& os,
  143. const char* versionString);
  144. // The namespace in which the exports are placed in the generated file.
  145. std::string Namespace;
  146. bool ExportOld;
  147. // The set of configurations to export.
  148. std::vector<std::string> Configurations;
  149. // The file to generate.
  150. std::string MainImportFile;
  151. std::string FileDir;
  152. std::string FileBase;
  153. std::string FileExt;
  154. bool AppendMode;
  155. // The set of targets included in the export.
  156. std::set<cmGeneratorTarget*> ExportedTargets;
  157. private:
  158. void PopulateInterfaceProperty(const std::string&, const std::string&,
  159. cmGeneratorTarget* target,
  160. cmGeneratorExpression::PreprocessContext,
  161. ImportPropertyMap& properties,
  162. std::vector<std::string>& missingTargets);
  163. bool AddTargetNamespace(std::string& input, cmGeneratorTarget* target,
  164. std::vector<std::string>& missingTargets);
  165. void ResolveTargetsInGeneratorExpression(
  166. std::string& input, cmGeneratorTarget* target,
  167. std::vector<std::string>& missingTargets);
  168. virtual void ReplaceInstallPrefix(std::string& input);
  169. virtual std::string InstallNameDir(cmGeneratorTarget* target,
  170. const std::string& config) = 0;
  171. };
  172. #endif