cmExportFileGenerator.h 8.8 KB

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