cmExportFileGenerator.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 "cmCommand.h"
  13. #include "cmGeneratorExpression.h"
  14. #include "cmVersionMacros.h"
  15. #include "cmVersion.h"
  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(CMake_VERSION_MINOR) "." \
  22. STRINGIFY(CMake_VERSION_PATCH) \
  23. : #major "." #minor ".0" \
  24. )
  25. class cmTargetExport;
  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() {}
  39. /** Set the full path to the export file to generate. */
  40. void SetExportFile(const char* mainFile);
  41. const char *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. typedef std::map<std::string, std::string> ImportPropertyMap;
  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. void GenerateImportHeaderCode(std::ostream& os,
  59. const std::string& config = "");
  60. void GenerateImportFooterCode(std::ostream& os);
  61. void GenerateImportVersionCode(std::ostream& os);
  62. void GenerateImportTargetCode(std::ostream& os,
  63. cmGeneratorTarget const* target);
  64. void GenerateImportPropertyCode(std::ostream& os, const std::string& config,
  65. cmGeneratorTarget const* target,
  66. ImportPropertyMap const& properties);
  67. void GenerateImportedFileChecksCode(std::ostream& os,
  68. cmGeneratorTarget* target,
  69. ImportPropertyMap const& properties,
  70. const std::set<std::string>& importedLocations);
  71. void GenerateImportedFileCheckLoop(std::ostream& os);
  72. void GenerateMissingTargetsCheckCode(std::ostream& os,
  73. const std::vector<std::string>& missingTargets);
  74. void GenerateExpectedTargetsCode(std::ostream& os,
  75. const std::string &expectedTargets);
  76. // Collect properties with detailed information about targets beyond
  77. // their location on disk.
  78. void SetImportDetailProperties(const std::string& config,
  79. std::string const& suffix,
  80. cmGeneratorTarget* target,
  81. ImportPropertyMap& properties,
  82. std::vector<std::string>& missingTargets);
  83. template <typename T>
  84. void SetImportLinkProperty(std::string const& suffix,
  85. cmGeneratorTarget* target,
  86. const std::string& propName,
  87. std::vector<T> const& entries,
  88. ImportPropertyMap& properties,
  89. std::vector<std::string>& missingTargets);
  90. /** Each subclass knows how to generate its kind of export file. */
  91. virtual bool GenerateMainFile(std::ostream& os) = 0;
  92. /** Each subclass knows where the target files are located. */
  93. virtual void GenerateImportTargetsConfig(std::ostream& os,
  94. const std::string& config,
  95. 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&,
  104. cmGeneratorTarget *target,
  105. cmGeneratorExpression::PreprocessContext,
  106. ImportPropertyMap &properties,
  107. std::vector<std::string> &missingTargets);
  108. bool PopulateInterfaceLinkLibrariesProperty(cmGeneratorTarget* target,
  109. cmGeneratorExpression::PreprocessContext,
  110. ImportPropertyMap &properties,
  111. 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. void GenerateInterfaceProperties(cmGeneratorTarget const* target,
  118. std::ostream& os,
  119. const ImportPropertyMap &properties);
  120. void PopulateIncludeDirectoriesInterface(
  121. cmTargetExport *target,
  122. cmGeneratorExpression::PreprocessContext preprocessRule,
  123. ImportPropertyMap &properties,
  124. std::vector<std::string> &missingTargets);
  125. void PopulateSourcesInterface(
  126. cmTargetExport *target,
  127. cmGeneratorExpression::PreprocessContext preprocessRule,
  128. ImportPropertyMap &properties,
  129. std::vector<std::string> &missingTargets);
  130. void SetImportLinkInterface(const std::string& config,
  131. std::string const& suffix,
  132. cmGeneratorExpression::PreprocessContext preprocessRule,
  133. cmGeneratorTarget* target, ImportPropertyMap& properties,
  134. std::vector<std::string>& missingTargets);
  135. enum FreeTargetsReplace {
  136. ReplaceFreeTargets,
  137. NoReplaceFreeTargets
  138. };
  139. void ResolveTargetsInGeneratorExpressions(std::string &input,
  140. cmGeneratorTarget* target,
  141. std::vector<std::string> &missingTargets,
  142. FreeTargetsReplace replace = NoReplaceFreeTargets);
  143. void GenerateRequiredCMakeVersion(std::ostream& os,
  144. const char *versionString);
  145. // The namespace in which the exports are placed in the generated file.
  146. std::string Namespace;
  147. bool ExportOld;
  148. // The set of configurations to export.
  149. std::vector<std::string> Configurations;
  150. // The file to generate.
  151. std::string MainImportFile;
  152. std::string FileDir;
  153. std::string FileBase;
  154. std::string FileExt;
  155. bool AppendMode;
  156. // The set of targets included in the export.
  157. std::set<cmGeneratorTarget*> ExportedTargets;
  158. private:
  159. void PopulateInterfaceProperty(const std::string&, const std::string&,
  160. cmGeneratorTarget* target,
  161. cmGeneratorExpression::PreprocessContext,
  162. ImportPropertyMap &properties,
  163. std::vector<std::string> &missingTargets);
  164. bool AddTargetNamespace(std::string &input, cmGeneratorTarget* target,
  165. std::vector<std::string> &missingTargets);
  166. void ResolveTargetsInGeneratorExpression(std::string &input,
  167. cmGeneratorTarget* target,
  168. std::vector<std::string> &missingTargets);
  169. virtual void ReplaceInstallPrefix(std::string &input);
  170. virtual std::string InstallNameDir(cmGeneratorTarget* target,
  171. const std::string& config) = 0;
  172. };
  173. #endif