cmExportFileGenerator.h 8.7 KB

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