cmQtAutoMocUic.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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 cmQtAutoMocUic_h
  4. #define cmQtAutoMocUic_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include "cmFileTime.h"
  7. #include "cmQtAutoGen.h"
  8. #include "cmQtAutoGenerator.h"
  9. #include "cmWorkerPool.h"
  10. #include "cm_string_view.hxx"
  11. #include "cmsys/RegularExpression.hxx"
  12. #include <atomic>
  13. #include <cstddef>
  14. #include <map>
  15. #include <memory>
  16. #include <set>
  17. #include <string>
  18. #include <unordered_map>
  19. #include <unordered_set>
  20. #include <utility>
  21. #include <vector>
  22. class cmMakefile;
  23. /** \class cmQtAutoMocUic
  24. * \brief AUTOMOC and AUTOUIC generator
  25. */
  26. class cmQtAutoMocUic : public cmQtAutoGenerator
  27. {
  28. public:
  29. cmQtAutoMocUic();
  30. ~cmQtAutoMocUic() override;
  31. cmQtAutoMocUic(cmQtAutoMocUic const&) = delete;
  32. cmQtAutoMocUic& operator=(cmQtAutoMocUic const&) = delete;
  33. public:
  34. // -- Types
  35. /**
  36. * Search key plus regular expression pair
  37. */
  38. struct KeyExpT
  39. {
  40. KeyExpT() = default;
  41. KeyExpT(const char* key, const char* exp)
  42. : Key(key)
  43. , Exp(exp)
  44. {
  45. }
  46. KeyExpT(std::string key, std::string const& exp)
  47. : Key(std::move(key))
  48. , Exp(exp)
  49. {
  50. }
  51. std::string Key;
  52. cmsys::RegularExpression Exp;
  53. };
  54. /**
  55. * Include string with sub parts
  56. */
  57. struct IncludeKeyT
  58. {
  59. IncludeKeyT(std::string const& key, std::size_t basePrefixLength);
  60. std::string Key; // Full include string
  61. std::string Dir; // Include directory
  62. std::string Base; // Base part of the include file name
  63. };
  64. /**
  65. * Source file parsing cache
  66. */
  67. class ParseCacheT
  68. {
  69. public:
  70. // -- Types
  71. /**
  72. * Entry of the file parsing cache
  73. */
  74. struct FileT
  75. {
  76. void Clear();
  77. struct MocT
  78. {
  79. std::string Macro;
  80. struct IncludeT
  81. {
  82. std::vector<IncludeKeyT> Underscore;
  83. std::vector<IncludeKeyT> Dot;
  84. } Include;
  85. std::vector<std::string> Depends;
  86. } Moc;
  87. struct UicT
  88. {
  89. std::vector<IncludeKeyT> Include;
  90. std::vector<std::string> Depends;
  91. } Uic;
  92. };
  93. using FileHandleT = std::shared_ptr<FileT>;
  94. using GetOrInsertT = std::pair<FileHandleT, bool>;
  95. public:
  96. ParseCacheT();
  97. ~ParseCacheT();
  98. void Clear();
  99. bool ReadFromFile(std::string const& fileName);
  100. bool WriteToFile(std::string const& fileName);
  101. //! Might return an invalid handle
  102. FileHandleT Get(std::string const& fileName) const;
  103. //! Always returns a valid handle
  104. GetOrInsertT GetOrInsert(std::string const& fileName);
  105. private:
  106. std::unordered_map<std::string, FileHandleT> Map_;
  107. };
  108. /**
  109. * Source file data
  110. */
  111. class SourceFileT
  112. {
  113. public:
  114. SourceFileT(std::string fileName)
  115. : FileName(std::move(fileName))
  116. {
  117. }
  118. public:
  119. std::string FileName;
  120. cmFileTime FileTime;
  121. ParseCacheT::FileHandleT ParseData;
  122. std::string BuildPath;
  123. bool Moc = false;
  124. bool Uic = false;
  125. };
  126. using SourceFileHandleT = std::shared_ptr<SourceFileT>;
  127. using SourceFileMapT = std::map<std::string, SourceFileHandleT>;
  128. /**
  129. * Meta compiler file mapping information
  130. */
  131. struct MappingT
  132. {
  133. SourceFileHandleT SourceFile;
  134. std::string OutputFile;
  135. std::string IncludeString;
  136. std::vector<SourceFileHandleT> IncluderFiles;
  137. };
  138. using MappingHandleT = std::shared_ptr<MappingT>;
  139. using MappingMapT = std::map<std::string, MappingHandleT>;
  140. /**
  141. * Common settings
  142. */
  143. class BaseSettingsT
  144. {
  145. public:
  146. // -- Constructors
  147. BaseSettingsT();
  148. ~BaseSettingsT();
  149. BaseSettingsT(BaseSettingsT const&) = delete;
  150. BaseSettingsT& operator=(BaseSettingsT const&) = delete;
  151. // -- Attributes
  152. // - Config
  153. bool MultiConfig = false;
  154. bool IncludeProjectDirsBefore = false;
  155. unsigned int QtVersionMajor = 4;
  156. // - Directories
  157. std::string ProjectSourceDir;
  158. std::string ProjectBinaryDir;
  159. std::string CurrentSourceDir;
  160. std::string CurrentBinaryDir;
  161. std::string AutogenBuildDir;
  162. std::string AutogenIncludeDir;
  163. // - Files
  164. std::string CMakeExecutable;
  165. cmFileTime CMakeExecutableTime;
  166. std::string ParseCacheFile;
  167. std::vector<std::string> HeaderExtensions;
  168. };
  169. /**
  170. * Shared common variables
  171. */
  172. class BaseEvalT
  173. {
  174. public:
  175. // -- Parse Cache
  176. bool ParseCacheChanged = false;
  177. cmFileTime ParseCacheTime;
  178. ParseCacheT ParseCache;
  179. // -- Sources
  180. SourceFileMapT Headers;
  181. SourceFileMapT Sources;
  182. };
  183. /**
  184. * Moc settings
  185. */
  186. class MocSettingsT
  187. {
  188. public:
  189. // -- Constructors
  190. MocSettingsT();
  191. ~MocSettingsT();
  192. MocSettingsT(MocSettingsT const&) = delete;
  193. MocSettingsT& operator=(MocSettingsT const&) = delete;
  194. // -- Const methods
  195. bool skipped(std::string const& fileName) const;
  196. std::string MacrosString() const;
  197. // -- Attributes
  198. bool Enabled = false;
  199. bool SettingsChanged = false;
  200. bool RelaxedMode = false;
  201. cmFileTime ExecutableTime;
  202. std::string Executable;
  203. std::string CompFileAbs;
  204. std::string PredefsFileRel;
  205. std::string PredefsFileAbs;
  206. std::unordered_set<std::string> SkipList;
  207. std::vector<std::string> IncludePaths;
  208. std::vector<std::string> Includes;
  209. std::vector<std::string> Definitions;
  210. std::vector<std::string> Options;
  211. std::vector<std::string> AllOptions;
  212. std::vector<std::string> PredefsCmd;
  213. std::vector<KeyExpT> DependFilters;
  214. std::vector<KeyExpT> MacroFilters;
  215. cmsys::RegularExpression RegExpInclude;
  216. };
  217. /**
  218. * Moc shared variables
  219. */
  220. class MocEvalT
  221. {
  222. public:
  223. // -- predefines file
  224. cmFileTime PredefsTime;
  225. // -- Mappings
  226. MappingMapT HeaderMappings;
  227. MappingMapT SourceMappings;
  228. MappingMapT Includes;
  229. // -- Discovered files
  230. SourceFileMapT HeadersDiscovered;
  231. // -- Mocs compilation
  232. bool CompUpdated = false;
  233. std::vector<std::string> CompFiles;
  234. };
  235. /**
  236. * Uic settings
  237. */
  238. class UicSettingsT
  239. {
  240. public:
  241. UicSettingsT();
  242. ~UicSettingsT();
  243. UicSettingsT(UicSettingsT const&) = delete;
  244. UicSettingsT& operator=(UicSettingsT const&) = delete;
  245. // -- Const methods
  246. bool skipped(std::string const& fileName) const;
  247. // -- Attributes
  248. bool Enabled = false;
  249. bool SettingsChanged = false;
  250. cmFileTime ExecutableTime;
  251. std::string Executable;
  252. std::unordered_set<std::string> SkipList;
  253. std::vector<std::string> TargetOptions;
  254. std::map<std::string, std::vector<std::string>> Options;
  255. std::vector<std::string> SearchPaths;
  256. cmsys::RegularExpression RegExpInclude;
  257. };
  258. /**
  259. * Uic shared variables
  260. */
  261. class UicEvalT
  262. {
  263. public:
  264. SourceFileMapT UiFiles;
  265. MappingMapT Includes;
  266. };
  267. /**
  268. * Abstract job class for concurrent job processing
  269. */
  270. class JobT : public cmWorkerPool::JobT
  271. {
  272. protected:
  273. /**
  274. * @brief Protected default constructor
  275. */
  276. JobT(bool fence = false)
  277. : cmWorkerPool::JobT(fence)
  278. {
  279. }
  280. //! Get the generator. Only valid during Process() call!
  281. cmQtAutoMocUic* Gen() const
  282. {
  283. return static_cast<cmQtAutoMocUic*>(UserData());
  284. };
  285. // -- Accessors. Only valid during Process() call!
  286. Logger const& Log() const { return Gen()->Log(); }
  287. BaseSettingsT const& BaseConst() const { return Gen()->BaseConst(); }
  288. BaseEvalT& BaseEval() const { return Gen()->BaseEval(); }
  289. MocSettingsT const& MocConst() const { return Gen()->MocConst(); }
  290. MocEvalT& MocEval() const { return Gen()->MocEval(); }
  291. UicSettingsT const& UicConst() const { return Gen()->UicConst(); }
  292. UicEvalT& UicEval() const { return Gen()->UicEval(); }
  293. // -- Error logging with automatic abort
  294. void LogError(GenT genType, cm::string_view message) const;
  295. void LogFileError(GenT genType, cm::string_view filename,
  296. cm::string_view message) const;
  297. void LogCommandError(GenT genType, cm::string_view message,
  298. std::vector<std::string> const& command,
  299. std::string const& output) const;
  300. /**
  301. * @brief Run an external process. Use only during Process() call!
  302. */
  303. bool RunProcess(GenT genType, cmWorkerPool::ProcessResultT& result,
  304. std::vector<std::string> const& command,
  305. std::string* infoMessage = nullptr);
  306. };
  307. /**
  308. * Fence job utility class
  309. */
  310. class JobFenceT : public JobT
  311. {
  312. public:
  313. JobFenceT()
  314. : JobT(true)
  315. {
  316. }
  317. void Process() override{};
  318. };
  319. /**
  320. * Generate moc_predefs.h
  321. */
  322. class JobMocPredefsT : public JobFenceT
  323. {
  324. void Process() override;
  325. bool Update(std::string* reason) const;
  326. };
  327. /**
  328. * File parse job base class
  329. */
  330. class JobParseT : public JobT
  331. {
  332. public:
  333. JobParseT(SourceFileHandleT fileHandle)
  334. : FileHandle(std::move(fileHandle))
  335. {
  336. }
  337. protected:
  338. bool ReadFile();
  339. void CreateKeys(std::vector<IncludeKeyT>& container,
  340. std::set<std::string> const& source,
  341. std::size_t basePrefixLength);
  342. void MocMacro();
  343. void MocDependecies();
  344. void MocIncludes();
  345. void UicIncludes();
  346. protected:
  347. SourceFileHandleT FileHandle;
  348. std::string Content;
  349. };
  350. /**
  351. * Header file parse job
  352. */
  353. class JobParseHeaderT : public JobParseT
  354. {
  355. public:
  356. using JobParseT::JobParseT;
  357. void Process() override;
  358. };
  359. /**
  360. * Source file parse job
  361. */
  362. class JobParseSourceT : public JobParseT
  363. {
  364. public:
  365. using JobParseT::JobParseT;
  366. void Process() override;
  367. };
  368. /**
  369. * Evaluate parsed files
  370. */
  371. class JobEvaluateT : public JobFenceT
  372. {
  373. void Process() override;
  374. // -- Moc
  375. bool MocEvalHeader(SourceFileHandleT source);
  376. bool MocEvalSource(SourceFileHandleT const& source);
  377. SourceFileHandleT MocFindIncludedHeader(
  378. std::string const& includerDir, std::string const& includeBase) const;
  379. SourceFileHandleT MocFindHeader(std::string const& basePath) const;
  380. std::string MocMessageTestHeaders(cm::string_view fileBase) const;
  381. bool MocRegisterIncluded(std::string const& includeString,
  382. SourceFileHandleT includerFileHandle,
  383. SourceFileHandleT sourceFileHandle,
  384. bool sourceIsHeader) const;
  385. void MocRegisterMapping(MappingHandleT mappingHandle,
  386. bool sourceIsHeader) const;
  387. // -- Uic
  388. bool UicEval(SourceFileMapT const& fileMap);
  389. bool UicEvalFile(SourceFileHandleT const& sourceFileHandle);
  390. SourceFileHandleT UicFindIncludedUi(std::string const& sourceFile,
  391. std::string const& sourceDir,
  392. IncludeKeyT const& incKey) const;
  393. bool UicRegisterMapping(std::string const& includeString,
  394. SourceFileHandleT uiFileHandle,
  395. SourceFileHandleT includerFileHandle);
  396. };
  397. /**
  398. * Generates moc/uic jobs
  399. */
  400. class JobGenerateT : public JobFenceT
  401. {
  402. void Process() override;
  403. // -- Moc
  404. bool MocGenerate(MappingHandleT const& mapping, bool compFile) const;
  405. bool MocUpdate(MappingT const& mapping, std::string* reason) const;
  406. std::pair<std::string, cmFileTime> MocFindDependency(
  407. std::string const& sourceDir, std::string const& includeString) const;
  408. // -- Uic
  409. bool UicGenerate(MappingHandleT const& mapping) const;
  410. bool UicUpdate(MappingT const& mapping, std::string* reason) const;
  411. };
  412. /**
  413. * File compiling base job
  414. */
  415. class JobCompileT : public JobT
  416. {
  417. public:
  418. JobCompileT(MappingHandleT uicMapping, std::unique_ptr<std::string> reason)
  419. : Mapping(std::move(uicMapping))
  420. , Reason(std::move(reason))
  421. {
  422. }
  423. protected:
  424. MappingHandleT Mapping;
  425. std::unique_ptr<std::string> Reason;
  426. };
  427. /**
  428. * moc compiles a file
  429. */
  430. class JobMocT : public JobCompileT
  431. {
  432. public:
  433. using JobCompileT::JobCompileT;
  434. void Process() override;
  435. };
  436. /**
  437. * uic compiles a file
  438. */
  439. class JobUicT : public JobCompileT
  440. {
  441. public:
  442. using JobCompileT::JobCompileT;
  443. void Process() override;
  444. };
  445. /// @brief Generate mocs_compilation.cpp
  446. ///
  447. class JobMocsCompilationT : public JobFenceT
  448. {
  449. private:
  450. void Process() override;
  451. };
  452. /// @brief The last job
  453. ///
  454. class JobFinishT : public JobFenceT
  455. {
  456. private:
  457. void Process() override;
  458. };
  459. // -- Const settings interface
  460. BaseSettingsT const& BaseConst() const { return this->BaseConst_; }
  461. BaseEvalT& BaseEval() { return this->BaseEval_; }
  462. MocSettingsT const& MocConst() const { return this->MocConst_; }
  463. MocEvalT& MocEval() { return this->MocEval_; }
  464. UicSettingsT const& UicConst() const { return this->UicConst_; }
  465. UicEvalT& UicEval() { return this->UicEval_; }
  466. // -- Parallel job processing interface
  467. cmWorkerPool& WorkerPool() { return WorkerPool_; }
  468. void AbortError() { Abort(true); }
  469. void AbortSuccess() { Abort(false); }
  470. // -- Utility
  471. std::string AbsoluteBuildPath(cm::string_view relativePath) const;
  472. std::string AbsoluteIncludePath(cm::string_view relativePath) const;
  473. template <class JOBTYPE>
  474. void CreateParseJobs(SourceFileMapT const& sourceMap);
  475. private:
  476. // -- Utility accessors
  477. Logger const& Log() const { return Logger_; }
  478. // -- Abstract processing interface
  479. bool Init(cmMakefile* makefile) override;
  480. void InitJobs();
  481. bool Process() override;
  482. // -- Settings file
  483. void SettingsFileRead();
  484. bool SettingsFileWrite();
  485. // -- Parse cache
  486. void ParseCacheRead();
  487. bool ParseCacheWrite();
  488. // -- Thread processing
  489. void Abort(bool error);
  490. // -- Generation
  491. bool CreateDirectories();
  492. private:
  493. // -- Utility
  494. Logger Logger_;
  495. // -- Settings
  496. BaseSettingsT BaseConst_;
  497. BaseEvalT BaseEval_;
  498. MocSettingsT MocConst_;
  499. MocEvalT MocEval_;
  500. UicSettingsT UicConst_;
  501. UicEvalT UicEval_;
  502. // -- Settings file
  503. std::string SettingsFile_;
  504. std::string SettingsStringMoc_;
  505. std::string SettingsStringUic_;
  506. // -- Worker thread pool
  507. std::atomic<bool> JobError_ = ATOMIC_VAR_INIT(false);
  508. cmWorkerPool WorkerPool_;
  509. };
  510. #endif