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