cmExportFileGenerator.h 8.4 KB

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