cmExportFileGenerator.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. cmTarget const* target,
  66. ImportPropertyMap const& properties);
  67. void GenerateImportedFileChecksCode(std::ostream& os, cmTarget* target,
  68. ImportPropertyMap const& properties,
  69. const std::set<std::string>& importedLocations);
  70. void GenerateImportedFileCheckLoop(std::ostream& os);
  71. void GenerateMissingTargetsCheckCode(std::ostream& os,
  72. const std::vector<std::string>& missingTargets);
  73. void GenerateExpectedTargetsCode(std::ostream& os,
  74. const std::string &expectedTargets);
  75. // Collect properties with detailed information about targets beyond
  76. // their location on disk.
  77. void SetImportDetailProperties(const std::string& config,
  78. std::string const& suffix,
  79. cmGeneratorTarget* target,
  80. ImportPropertyMap& properties,
  81. std::vector<std::string>& missingTargets);
  82. template <typename T>
  83. void SetImportLinkProperty(std::string const& suffix,
  84. cmGeneratorTarget* target,
  85. const std::string& propName,
  86. std::vector<T> const& entries,
  87. ImportPropertyMap& properties,
  88. std::vector<std::string>& missingTargets);
  89. /** Each subclass knows how to generate its kind of export file. */
  90. virtual bool GenerateMainFile(std::ostream& os) = 0;
  91. /** Each subclass knows where the target files are located. */
  92. virtual void GenerateImportTargetsConfig(std::ostream& os,
  93. const std::string& config,
  94. std::string const& suffix,
  95. std::vector<std::string> &missingTargets) = 0;
  96. /** Each subclass knows how to deal with a target that is missing from an
  97. * export set. */
  98. virtual void HandleMissingTarget(std::string& link_libs,
  99. std::vector<std::string>& missingTargets,
  100. cmGeneratorTarget* depender,
  101. cmGeneratorTarget* dependee) = 0;
  102. void PopulateInterfaceProperty(const std::string&,
  103. cmGeneratorTarget *target,
  104. cmGeneratorExpression::PreprocessContext,
  105. ImportPropertyMap &properties,
  106. std::vector<std::string> &missingTargets);
  107. bool PopulateInterfaceLinkLibrariesProperty(cmGeneratorTarget* target,
  108. cmGeneratorExpression::PreprocessContext,
  109. ImportPropertyMap &properties,
  110. std::vector<std::string> &missingTargets);
  111. void PopulateInterfaceProperty(const std::string& propName,
  112. cmGeneratorTarget* target,
  113. ImportPropertyMap &properties);
  114. void PopulateCompatibleInterfaceProperties(cmGeneratorTarget *target,
  115. ImportPropertyMap &properties);
  116. void GenerateInterfaceProperties(cmGeneratorTarget const* target,
  117. std::ostream& os,
  118. const ImportPropertyMap &properties);
  119. void PopulateIncludeDirectoriesInterface(
  120. cmTargetExport *target,
  121. cmGeneratorExpression::PreprocessContext preprocessRule,
  122. ImportPropertyMap &properties,
  123. std::vector<std::string> &missingTargets);
  124. void PopulateSourcesInterface(
  125. cmTargetExport *target,
  126. cmGeneratorExpression::PreprocessContext preprocessRule,
  127. ImportPropertyMap &properties,
  128. std::vector<std::string> &missingTargets);
  129. void SetImportLinkInterface(const std::string& config,
  130. std::string const& suffix,
  131. cmGeneratorExpression::PreprocessContext preprocessRule,
  132. cmGeneratorTarget* target, ImportPropertyMap& properties,
  133. std::vector<std::string>& missingTargets);
  134. enum FreeTargetsReplace {
  135. ReplaceFreeTargets,
  136. NoReplaceFreeTargets
  137. };
  138. void ResolveTargetsInGeneratorExpressions(std::string &input,
  139. cmGeneratorTarget* target,
  140. std::vector<std::string> &missingTargets,
  141. FreeTargetsReplace replace = NoReplaceFreeTargets);
  142. 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(std::string &input,
  166. 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