cmExportFileGenerator.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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, cmTarget* target,
  78. ImportPropertyMap& properties,
  79. std::vector<std::string>& missingTargets);
  80. template <typename T>
  81. void SetImportLinkProperty(std::string const& suffix,
  82. cmTarget* target, const std::string& propName,
  83. std::vector<T> const& entries,
  84. ImportPropertyMap& properties,
  85. std::vector<std::string>& missingTargets);
  86. /** Each subclass knows how to generate its kind of export file. */
  87. virtual bool GenerateMainFile(std::ostream& os) = 0;
  88. /** Each subclass knows where the target files are located. */
  89. virtual void GenerateImportTargetsConfig(std::ostream& os,
  90. const std::string& config,
  91. std::string const& suffix,
  92. std::vector<std::string> &missingTargets) = 0;
  93. /** Each subclass knows how to deal with a target that is missing from an
  94. * export set. */
  95. virtual void HandleMissingTarget(std::string& link_libs,
  96. std::vector<std::string>& missingTargets,
  97. cmMakefile* mf,
  98. cmTarget* depender,
  99. cmTarget* dependee) = 0;
  100. void PopulateInterfaceProperty(const std::string&,
  101. cmTarget *target,
  102. cmGeneratorExpression::PreprocessContext,
  103. ImportPropertyMap &properties,
  104. std::vector<std::string> &missingTargets);
  105. bool PopulateInterfaceLinkLibrariesProperty(cmTarget *target,
  106. cmGeneratorExpression::PreprocessContext,
  107. ImportPropertyMap &properties,
  108. std::vector<std::string> &missingTargets);
  109. void PopulateInterfaceProperty(const std::string& propName, cmTarget *target,
  110. ImportPropertyMap &properties);
  111. void PopulateCompatibleInterfaceProperties(cmTarget *target,
  112. ImportPropertyMap &properties);
  113. void GenerateInterfaceProperties(cmTarget const* target, std::ostream& os,
  114. const ImportPropertyMap &properties);
  115. void PopulateIncludeDirectoriesInterface(
  116. cmTargetExport *target,
  117. cmGeneratorExpression::PreprocessContext preprocessRule,
  118. ImportPropertyMap &properties,
  119. std::vector<std::string> &missingTargets);
  120. void SetImportLinkInterface(const std::string& config,
  121. std::string const& suffix,
  122. cmGeneratorExpression::PreprocessContext preprocessRule,
  123. cmTarget* target, ImportPropertyMap& properties,
  124. std::vector<std::string>& missingTargets);
  125. enum FreeTargetsReplace {
  126. ReplaceFreeTargets,
  127. NoReplaceFreeTargets
  128. };
  129. void ResolveTargetsInGeneratorExpressions(std::string &input,
  130. cmTarget* target,
  131. std::vector<std::string> &missingTargets,
  132. FreeTargetsReplace replace = NoReplaceFreeTargets);
  133. void GenerateRequiredCMakeVersion(std::ostream& os,
  134. const char *versionString);
  135. // The namespace in which the exports are placed in the generated file.
  136. std::string Namespace;
  137. bool ExportOld;
  138. // The set of configurations to export.
  139. std::vector<std::string> Configurations;
  140. // The file to generate.
  141. std::string MainImportFile;
  142. std::string FileDir;
  143. std::string FileBase;
  144. std::string FileExt;
  145. bool AppendMode;
  146. // The set of targets included in the export.
  147. std::set<cmTarget*> ExportedTargets;
  148. private:
  149. void PopulateInterfaceProperty(const std::string&, const std::string&,
  150. cmTarget *target,
  151. cmGeneratorExpression::PreprocessContext,
  152. ImportPropertyMap &properties,
  153. std::vector<std::string> &missingTargets);
  154. bool AddTargetNamespace(std::string &input, cmTarget* target,
  155. std::vector<std::string> &missingTargets);
  156. void ResolveTargetsInGeneratorExpression(std::string &input,
  157. cmTarget* target,
  158. std::vector<std::string> &missingTargets);
  159. virtual void ReplaceInstallPrefix(std::string &input);
  160. virtual std::string InstallNameDir(cmTarget* target,
  161. const std::string& config) = 0;
  162. };
  163. #endif