cmQtAutoGenInitializer.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmQtAutoGenInitializer_h
  4. #define cmQtAutoGenInitializer_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include "cmGeneratedFileStream.h"
  7. #include "cmQtAutoGen.h"
  8. #include "cm_string_view.hxx"
  9. #include <map>
  10. #include <memory>
  11. #include <ostream>
  12. #include <set>
  13. #include <string>
  14. #include <unordered_map>
  15. #include <utility>
  16. #include <vector>
  17. class cmGeneratorTarget;
  18. class cmGlobalGenerator;
  19. class cmLocalGenerator;
  20. class cmMakefile;
  21. class cmQtAutoGenGlobalInitializer;
  22. class cmSourceFile;
  23. class cmTarget;
  24. /// @brief Initializes the QtAutoGen generators
  25. class cmQtAutoGenInitializer : public cmQtAutoGen
  26. {
  27. public:
  28. /// @brief Rcc job information
  29. class Qrc
  30. {
  31. public:
  32. std::string LockFile;
  33. std::string QrcFile;
  34. std::string QrcName;
  35. std::string PathChecksum;
  36. std::string InfoFile;
  37. std::string SettingsFile;
  38. std::map<std::string, std::string> ConfigSettingsFile;
  39. std::string RccFile;
  40. bool Generated = false;
  41. bool Unique = false;
  42. std::vector<std::string> Options;
  43. std::vector<std::string> Resources;
  44. };
  45. /// @brief Moc/Uic file
  46. struct MUFile
  47. {
  48. std::string FullPath;
  49. cmSourceFile* SF = nullptr;
  50. bool Generated = false;
  51. bool SkipMoc = false;
  52. bool SkipUic = false;
  53. bool MocIt = false;
  54. bool UicIt = false;
  55. };
  56. using MUFileHandle = std::unique_ptr<MUFile>;
  57. /// @brief Abstract moc/uic/rcc generator variables base class
  58. struct GenVarsT
  59. {
  60. bool Enabled = false;
  61. // Generator type/name
  62. GenT Gen;
  63. cm::string_view GenNameUpper;
  64. // Executable
  65. std::string ExecutableTargetName;
  66. cmGeneratorTarget* ExecutableTarget = nullptr;
  67. std::string Executable;
  68. CompilerFeaturesHandle ExecutableFeatures;
  69. /// @brief Constructor
  70. GenVarsT(GenT gen)
  71. : Gen(gen)
  72. , GenNameUpper(cmQtAutoGen::GeneratorNameUpper(gen)){};
  73. };
  74. /// @brief Writes a CMake info file
  75. class InfoWriter
  76. {
  77. public:
  78. /// @brief Open the given file
  79. InfoWriter(std::string const& filename);
  80. /// @return True if the file is open
  81. explicit operator bool() const { return static_cast<bool>(Ofs_); }
  82. void Write(cm::string_view text) { Ofs_ << text; }
  83. void Write(cm::string_view, std::string const& value);
  84. void WriteUInt(cm::string_view, unsigned int value);
  85. template <class C>
  86. void WriteStrings(cm::string_view, C const& container);
  87. void WriteConfig(cm::string_view,
  88. std::map<std::string, std::string> const& map);
  89. template <class C>
  90. void WriteConfigStrings(cm::string_view,
  91. std::map<std::string, C> const& map);
  92. void WriteNestedLists(cm::string_view,
  93. std::vector<std::vector<std::string>> const& lists);
  94. private:
  95. template <class IT>
  96. static std::string ListJoin(IT it_begin, IT it_end);
  97. static std::string ConfigKey(cm::string_view, std::string const& config);
  98. private:
  99. cmGeneratedFileStream Ofs_;
  100. };
  101. public:
  102. /// @return The detected Qt version and the required Qt major version
  103. static std::pair<IntegerVersion, unsigned int> GetQtVersion(
  104. cmGeneratorTarget const* genTarget);
  105. cmQtAutoGenInitializer(cmQtAutoGenGlobalInitializer* globalInitializer,
  106. cmGeneratorTarget* genTarget,
  107. IntegerVersion const& qtVersion, bool mocEnabled,
  108. bool uicEnabled, bool rccEnabled,
  109. bool globalAutogenTarget, bool globalAutoRccTarget);
  110. bool InitCustomTargets();
  111. bool SetupCustomTargets();
  112. private:
  113. /// @brief If moc or uic is enabled, the autogen target will be generated
  114. bool MocOrUicEnabled() const
  115. {
  116. return (this->Moc.Enabled || this->Uic.Enabled);
  117. }
  118. bool InitMoc();
  119. bool InitUic();
  120. bool InitRcc();
  121. bool InitScanFiles();
  122. bool InitAutogenTarget();
  123. bool InitRccTargets();
  124. bool SetupWriteAutogenInfo();
  125. bool SetupWriteRccInfo();
  126. void RegisterGeneratedSource(std::string const& filename);
  127. bool AddGeneratedSource(std::string const& filename, GenVarsT const& genVars,
  128. bool prepend = false);
  129. bool AddToSourceGroup(std::string const& fileName,
  130. cm::string_view genNameUpper);
  131. void AddCleanFile(std::string const& fileName);
  132. bool GetQtExecutable(GenVarsT& genVars, const std::string& executable,
  133. bool ignoreMissingTarget) const;
  134. private:
  135. cmQtAutoGenGlobalInitializer* GlobalInitializer = nullptr;
  136. cmGeneratorTarget* GenTarget = nullptr;
  137. cmGlobalGenerator* GlobalGen = nullptr;
  138. cmLocalGenerator* LocalGen = nullptr;
  139. cmMakefile* Makefile = nullptr;
  140. // Configuration
  141. IntegerVersion QtVersion;
  142. bool MultiConfig = false;
  143. std::string ConfigDefault;
  144. std::vector<std::string> ConfigsList;
  145. std::string Verbosity;
  146. std::string TargetsFolder;
  147. bool CMP0071Accept = false;
  148. bool CMP0071Warn = false;
  149. /// @brief Common directories
  150. struct
  151. {
  152. std::string Info;
  153. std::string Build;
  154. std::string Work;
  155. std::string Include;
  156. std::map<std::string, std::string> ConfigInclude;
  157. } Dir;
  158. /// @brief Autogen target variables
  159. struct
  160. {
  161. std::string Name;
  162. bool GlobalTarget = false;
  163. // Settings
  164. std::string Parallel;
  165. // Configuration files
  166. std::string InfoFile;
  167. std::string SettingsFile;
  168. std::string ParseCacheFile;
  169. std::map<std::string, std::string> ConfigSettingsFile;
  170. // Dependencies
  171. bool DependOrigin = false;
  172. std::set<std::string> DependFiles;
  173. std::set<cmTarget*> DependTargets;
  174. // Sources to process
  175. std::unordered_map<cmSourceFile*, MUFileHandle> Headers;
  176. std::unordered_map<cmSourceFile*, MUFileHandle> Sources;
  177. std::vector<MUFile*> FilesGenerated;
  178. } AutogenTarget;
  179. /// @brief Moc only variables
  180. struct MocT : public GenVarsT
  181. {
  182. std::string PredefsCmd;
  183. std::vector<std::string> Includes;
  184. std::map<std::string, std::vector<std::string>> ConfigIncludes;
  185. std::set<std::string> Defines;
  186. std::map<std::string, std::set<std::string>> ConfigDefines;
  187. std::string MocsCompilation;
  188. /// @brief Constructor
  189. MocT()
  190. : GenVarsT(GenT::MOC){};
  191. } Moc;
  192. /// @brief Uic only variables
  193. struct UicT : public GenVarsT
  194. {
  195. std::set<std::string> SkipUi;
  196. std::vector<std::string> SearchPaths;
  197. std::vector<std::string> Options;
  198. std::map<std::string, std::vector<std::string>> ConfigOptions;
  199. std::vector<std::string> FileFiles;
  200. std::vector<std::vector<std::string>> FileOptions;
  201. /// @brief Constructor
  202. UicT()
  203. : GenVarsT(GenT::UIC){};
  204. } Uic;
  205. /// @brief Rcc only variables
  206. struct RccT : public GenVarsT
  207. {
  208. bool GlobalTarget = false;
  209. std::vector<Qrc> Qrcs;
  210. /// @brief Constructor
  211. RccT()
  212. : GenVarsT(GenT::RCC){};
  213. } Rcc;
  214. };
  215. #endif