cmQtAutoMocUic.cxx 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmQtAutoMocUic.h"
  4. #include <algorithm>
  5. #include <atomic>
  6. #include <cstddef>
  7. #include <map>
  8. #include <mutex>
  9. #include <set>
  10. #include <string>
  11. #include <unordered_map>
  12. #include <unordered_set>
  13. #include <utility>
  14. #include <vector>
  15. #include <cm/memory>
  16. #include <cm/optional>
  17. #include <cm/string_view>
  18. #include <cmext/algorithm>
  19. #include <cm3p/json/value.h>
  20. #include "cmsys/FStream.hxx"
  21. #include "cmsys/RegularExpression.hxx"
  22. #include "cmCryptoHash.h"
  23. #include "cmFileTime.h"
  24. #include "cmGccDepfileReader.h"
  25. #include "cmGeneratedFileStream.h"
  26. #include "cmQtAutoGen.h"
  27. #include "cmQtAutoGenerator.h"
  28. #include "cmStringAlgorithms.h"
  29. #include "cmSystemTools.h"
  30. #include "cmWorkerPool.h"
  31. #if defined(__APPLE__)
  32. # include <unistd.h>
  33. #endif
  34. namespace {
  35. constexpr std::size_t MocUnderscoreLength = 4; // Length of "moc_"
  36. constexpr std::size_t UiUnderscoreLength = 3; // Length of "ui_"
  37. /** \class cmQtAutoMocUicT
  38. * \brief AUTOMOC and AUTOUIC generator
  39. */
  40. class cmQtAutoMocUicT : public cmQtAutoGenerator
  41. {
  42. public:
  43. cmQtAutoMocUicT();
  44. ~cmQtAutoMocUicT() override;
  45. cmQtAutoMocUicT(cmQtAutoMocUicT const&) = delete;
  46. cmQtAutoMocUicT& operator=(cmQtAutoMocUicT const&) = delete;
  47. public:
  48. // -- Types
  49. /** Include string with sub parts. */
  50. struct IncludeKeyT
  51. {
  52. IncludeKeyT(std::string const& key, std::size_t basePrefixLength);
  53. std::string Key; // Full include string
  54. std::string Dir; // Include directory
  55. std::string Base; // Base part of the include file name
  56. };
  57. /** Search key plus regular expression pair. */
  58. struct KeyExpT
  59. {
  60. KeyExpT(std::string key, std::string const& exp)
  61. : Key(std::move(key))
  62. , Exp(exp)
  63. {
  64. }
  65. std::string Key;
  66. cmsys::RegularExpression Exp;
  67. };
  68. /** Source file parsing cache. */
  69. class ParseCacheT
  70. {
  71. public:
  72. // -- Types
  73. /** Entry of the file parsing cache. */
  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. bool ReadFromFile(std::string const& fileName);
  99. bool WriteToFile(std::string const& fileName);
  100. //! Always returns a valid handle
  101. GetOrInsertT GetOrInsert(std::string const& fileName);
  102. private:
  103. std::unordered_map<std::string, FileHandleT> Map_;
  104. };
  105. /** Source file data. */
  106. class SourceFileT
  107. {
  108. public:
  109. SourceFileT(std::string fileName)
  110. : FileName(std::move(fileName))
  111. {
  112. }
  113. public:
  114. std::string FileName;
  115. cmFileTime FileTime;
  116. ParseCacheT::FileHandleT ParseData;
  117. std::string BuildPath;
  118. bool IsHeader = false;
  119. bool Moc = false;
  120. bool Uic = false;
  121. };
  122. using SourceFileHandleT = std::shared_ptr<SourceFileT>;
  123. using SourceFileMapT = std::map<std::string, SourceFileHandleT>;
  124. /** Meta compiler file mapping information. */
  125. struct MappingT
  126. {
  127. SourceFileHandleT SourceFile;
  128. std::string OutputFile;
  129. std::string IncludeString;
  130. std::vector<SourceFileHandleT> IncluderFiles;
  131. };
  132. using MappingHandleT = std::shared_ptr<MappingT>;
  133. using MappingMapT = std::map<std::string, MappingHandleT>;
  134. /** Common settings. */
  135. class BaseSettingsT
  136. {
  137. public:
  138. // -- Constructors
  139. BaseSettingsT();
  140. ~BaseSettingsT();
  141. BaseSettingsT(BaseSettingsT const&) = delete;
  142. BaseSettingsT& operator=(BaseSettingsT const&) = delete;
  143. // -- Attributes
  144. // - Config
  145. bool MultiConfig = false;
  146. IntegerVersion QtVersion = { 4, 0 };
  147. unsigned int ThreadCount = 0;
  148. // - Directories
  149. std::string AutogenBuildDir;
  150. std::string AutogenIncludeDir;
  151. // - Files
  152. std::string CMakeExecutable;
  153. cmFileTime CMakeExecutableTime;
  154. std::string ParseCacheFile;
  155. std::string DepFile;
  156. std::string DepFileRuleName;
  157. std::vector<std::string> HeaderExtensions;
  158. std::vector<std::string> ListFiles;
  159. };
  160. /** Shared common variables. */
  161. class BaseEvalT
  162. {
  163. public:
  164. // -- Parse Cache
  165. std::atomic<bool> ParseCacheChanged = ATOMIC_VAR_INIT(false);
  166. cmFileTime ParseCacheTime;
  167. ParseCacheT ParseCache;
  168. // -- Sources
  169. SourceFileMapT Headers;
  170. SourceFileMapT Sources;
  171. };
  172. /** Moc settings. */
  173. class MocSettingsT
  174. {
  175. public:
  176. // -- Constructors
  177. MocSettingsT();
  178. ~MocSettingsT();
  179. MocSettingsT(MocSettingsT const&) = delete;
  180. MocSettingsT& operator=(MocSettingsT const&) = delete;
  181. // -- Const methods
  182. bool skipped(std::string const& fileName) const;
  183. std::string MacrosString() const;
  184. // -- Attributes
  185. bool Enabled = false;
  186. bool SettingsChanged = false;
  187. bool RelaxedMode = false;
  188. bool PathPrefix = false;
  189. bool CanOutputDependencies = false;
  190. cmFileTime ExecutableTime;
  191. std::string Executable;
  192. std::string CompFileAbs;
  193. std::string PredefsFileAbs;
  194. std::unordered_set<std::string> SkipList;
  195. std::vector<std::string> IncludePaths;
  196. std::vector<std::string> Definitions;
  197. std::vector<std::string> OptionsIncludes;
  198. std::vector<std::string> OptionsDefinitions;
  199. std::vector<std::string> OptionsExtra;
  200. std::vector<std::string> PredefsCmd;
  201. std::vector<KeyExpT> DependFilters;
  202. std::vector<KeyExpT> MacroFilters;
  203. cmsys::RegularExpression RegExpInclude;
  204. };
  205. /** Moc shared variables. */
  206. class MocEvalT
  207. {
  208. public:
  209. // -- predefines file
  210. cmFileTime PredefsTime;
  211. // -- Mappings
  212. MappingMapT HeaderMappings;
  213. MappingMapT SourceMappings;
  214. MappingMapT Includes;
  215. // -- Discovered files
  216. SourceFileMapT HeadersDiscovered;
  217. // -- Output directories
  218. std::unordered_set<std::string> OutputDirs;
  219. // -- Mocs compilation
  220. bool CompUpdated = false;
  221. std::vector<std::string> CompFiles;
  222. };
  223. /** Uic settings. */
  224. class UicSettingsT
  225. {
  226. public:
  227. struct UiFile
  228. {
  229. std::vector<std::string> Options;
  230. };
  231. public:
  232. UicSettingsT();
  233. ~UicSettingsT();
  234. UicSettingsT(UicSettingsT const&) = delete;
  235. UicSettingsT& operator=(UicSettingsT const&) = delete;
  236. // -- Const methods
  237. bool skipped(std::string const& fileName) const;
  238. // -- Attributes
  239. bool Enabled = false;
  240. bool SettingsChanged = false;
  241. cmFileTime ExecutableTime;
  242. std::string Executable;
  243. std::unordered_set<std::string> SkipList;
  244. std::vector<std::string> Options;
  245. std::unordered_map<std::string, UiFile> UiFiles;
  246. std::vector<std::string> SearchPaths;
  247. cmsys::RegularExpression RegExpInclude;
  248. };
  249. /** Uic shared variables. */
  250. class UicEvalT
  251. {
  252. public:
  253. // -- Discovered files
  254. SourceFileMapT UiFiles;
  255. // -- Mappings
  256. MappingMapT Includes;
  257. // -- Output directories
  258. std::unordered_set<std::string> OutputDirs;
  259. };
  260. /** Abstract job class for concurrent job processing. */
  261. class JobT : public cmWorkerPool::JobT
  262. {
  263. protected:
  264. /** Protected default constructor. */
  265. JobT(bool fence = false)
  266. : cmWorkerPool::JobT(fence)
  267. {
  268. }
  269. //! Get the generator. Only valid during Process() call!
  270. cmQtAutoMocUicT* Gen() const
  271. {
  272. return static_cast<cmQtAutoMocUicT*>(this->UserData());
  273. };
  274. // -- Accessors. Only valid during Process() call!
  275. Logger const& Log() const { return this->Gen()->Log(); }
  276. BaseSettingsT const& BaseConst() const { return this->Gen()->BaseConst(); }
  277. BaseEvalT& BaseEval() const { return this->Gen()->BaseEval(); }
  278. MocSettingsT const& MocConst() const { return this->Gen()->MocConst(); }
  279. MocEvalT& MocEval() const { return this->Gen()->MocEval(); }
  280. UicSettingsT const& UicConst() const { return this->Gen()->UicConst(); }
  281. UicEvalT& UicEval() const { return this->Gen()->UicEval(); }
  282. // -- Logging
  283. std::string MessagePath(cm::string_view path) const
  284. {
  285. return this->Gen()->MessagePath(path);
  286. }
  287. // - Error logging with automatic abort
  288. void LogError(GenT genType, cm::string_view message) const;
  289. void LogCommandError(GenT genType, cm::string_view message,
  290. std::vector<std::string> const& command,
  291. std::string const& output) const;
  292. /** @brief Run an external process. Use only during Process() call! */
  293. bool RunProcess(GenT genType, cmWorkerPool::ProcessResultT& result,
  294. std::vector<std::string> const& command,
  295. std::string* infoMessage = nullptr);
  296. };
  297. /** Fence job utility class. */
  298. class JobFenceT : public JobT
  299. {
  300. public:
  301. JobFenceT()
  302. : JobT(true)
  303. {
  304. }
  305. void Process() override{};
  306. };
  307. /** Generate moc_predefs.h. */
  308. class JobMocPredefsT : public JobFenceT
  309. {
  310. void Process() override;
  311. bool Update(std::string* reason) const;
  312. };
  313. /** File parse job base class. */
  314. class JobParseT : public JobT
  315. {
  316. public:
  317. JobParseT(SourceFileHandleT fileHandle)
  318. : FileHandle(std::move(fileHandle))
  319. {
  320. }
  321. protected:
  322. bool ReadFile();
  323. void CreateKeys(std::vector<IncludeKeyT>& container,
  324. std::set<std::string> const& source,
  325. std::size_t basePrefixLength);
  326. void MocMacro();
  327. void MocDependecies();
  328. void MocIncludes();
  329. void UicIncludes();
  330. protected:
  331. SourceFileHandleT FileHandle;
  332. std::string Content;
  333. };
  334. /** Header file parse job. */
  335. class JobParseHeaderT : public JobParseT
  336. {
  337. public:
  338. using JobParseT::JobParseT;
  339. void Process() override;
  340. };
  341. /** Source file parse job. */
  342. class JobParseSourceT : public JobParseT
  343. {
  344. public:
  345. using JobParseT::JobParseT;
  346. void Process() override;
  347. };
  348. /** Evaluate cached file parse data - moc. */
  349. class JobEvalCacheT : public JobT
  350. {
  351. protected:
  352. std::string MessageSearchLocations() const;
  353. std::vector<std::string> SearchLocations;
  354. };
  355. /** Evaluate cached file parse data - moc. */
  356. class JobEvalCacheMocT : public JobEvalCacheT
  357. {
  358. void Process() override;
  359. bool EvalHeader(SourceFileHandleT source);
  360. bool EvalSource(SourceFileHandleT const& source);
  361. bool FindIncludedHeader(SourceFileHandleT& headerHandle,
  362. cm::string_view includerDir,
  363. cm::string_view includeBase);
  364. bool RegisterIncluded(std::string const& includeString,
  365. SourceFileHandleT includerFileHandle,
  366. SourceFileHandleT sourceFileHandle) const;
  367. void RegisterMapping(MappingHandleT mappingHandle) const;
  368. std::string MessageHeader(cm::string_view headerBase) const;
  369. };
  370. /** Evaluate cached file parse data - uic. */
  371. class JobEvalCacheUicT : public JobEvalCacheT
  372. {
  373. void Process() override;
  374. bool EvalFile(SourceFileHandleT const& sourceFileHandle);
  375. bool FindIncludedUi(cm::string_view sourceDirPrefix,
  376. cm::string_view includePrefix);
  377. bool RegisterMapping(std::string const& includeString,
  378. SourceFileHandleT includerFileHandle);
  379. std::string UiName;
  380. SourceFileHandleT UiFileHandle;
  381. };
  382. /** Evaluate cached file parse data - finish */
  383. class JobEvalCacheFinishT : public JobFenceT
  384. {
  385. void Process() override;
  386. };
  387. /** Dependency probing base job. */
  388. class JobProbeDepsT : public JobT
  389. {
  390. };
  391. /** Probes file dependencies and generates moc compile jobs. */
  392. class JobProbeDepsMocT : public JobProbeDepsT
  393. {
  394. void Process() override;
  395. bool Generate(MappingHandleT const& mapping, bool compFile) const;
  396. bool Probe(MappingT const& mapping, std::string* reason) const;
  397. std::pair<std::string, cmFileTime> FindDependency(
  398. std::string const& sourceDir, std::string const& includeString) const;
  399. };
  400. /** Probes file dependencies and generates uic compile jobs. */
  401. class JobProbeDepsUicT : public JobProbeDepsT
  402. {
  403. void Process() override;
  404. bool Probe(MappingT const& mapping, std::string* reason) const;
  405. };
  406. /** Dependency probing finish job. */
  407. class JobProbeDepsFinishT : public JobFenceT
  408. {
  409. void Process() override;
  410. };
  411. /** Meta compiler base job. */
  412. class JobCompileT : public JobT
  413. {
  414. public:
  415. JobCompileT(MappingHandleT uicMapping, std::unique_ptr<std::string> reason)
  416. : Mapping(std::move(uicMapping))
  417. , Reason(std::move(reason))
  418. {
  419. }
  420. protected:
  421. MappingHandleT Mapping;
  422. std::unique_ptr<std::string> Reason;
  423. };
  424. /** moc compiles a file. */
  425. class JobCompileMocT : public JobCompileT
  426. {
  427. public:
  428. JobCompileMocT(MappingHandleT uicMapping,
  429. std::unique_ptr<std::string> reason,
  430. ParseCacheT::FileHandleT cacheEntry)
  431. : JobCompileT(std::move(uicMapping), std::move(reason))
  432. , CacheEntry(std::move(cacheEntry))
  433. {
  434. }
  435. void Process() override;
  436. protected:
  437. ParseCacheT::FileHandleT CacheEntry;
  438. };
  439. /** uic compiles a file. */
  440. class JobCompileUicT : public JobCompileT
  441. {
  442. public:
  443. using JobCompileT::JobCompileT;
  444. void Process() override;
  445. };
  446. /** Generate mocs_compilation.cpp. */
  447. class JobMocsCompilationT : public JobFenceT
  448. {
  449. private:
  450. void Process() override;
  451. };
  452. class JobDepFilesMergeT : public JobFenceT
  453. {
  454. private:
  455. std::vector<std::string> initialDependencies() const;
  456. void Process() override;
  457. };
  458. /** @brief The last job. */
  459. class JobFinishT : public JobFenceT
  460. {
  461. private:
  462. void Process() override;
  463. };
  464. // -- Const settings interface
  465. BaseSettingsT const& BaseConst() const { return this->BaseConst_; }
  466. BaseEvalT& BaseEval() { return this->BaseEval_; }
  467. MocSettingsT const& MocConst() const { return this->MocConst_; }
  468. MocEvalT& MocEval() { return this->MocEval_; }
  469. UicSettingsT const& UicConst() const { return this->UicConst_; }
  470. UicEvalT& UicEval() { return this->UicEval_; }
  471. // -- Parallel job processing interface
  472. cmWorkerPool& WorkerPool() { return this->WorkerPool_; }
  473. void AbortError() { this->Abort(true); }
  474. void AbortSuccess() { this->Abort(false); }
  475. // -- Utility
  476. std::string AbsoluteBuildPath(cm::string_view relativePath) const;
  477. std::string AbsoluteIncludePath(cm::string_view relativePath) const;
  478. template <class JOBTYPE>
  479. void CreateParseJobs(SourceFileMapT const& sourceMap);
  480. std::string CollapseFullPathTS(std::string const& path) const;
  481. private:
  482. // -- Abstract processing interface
  483. bool InitFromInfo(InfoT const& info) override;
  484. void InitJobs();
  485. bool Process() override;
  486. // -- Settings file
  487. void SettingsFileRead();
  488. bool SettingsFileWrite();
  489. // -- Parse cache
  490. void ParseCacheRead();
  491. bool ParseCacheWrite();
  492. // -- Thread processing
  493. void Abort(bool error);
  494. // -- Generation
  495. bool CreateDirectories();
  496. // -- Support for depfiles
  497. static std::vector<std::string> dependenciesFromDepFile(
  498. const char* filePath);
  499. private:
  500. // -- Settings
  501. BaseSettingsT BaseConst_;
  502. BaseEvalT BaseEval_;
  503. MocSettingsT MocConst_;
  504. MocEvalT MocEval_;
  505. UicSettingsT UicConst_;
  506. UicEvalT UicEval_;
  507. // -- Settings file
  508. std::string SettingsFile_;
  509. std::string SettingsStringMoc_;
  510. std::string SettingsStringUic_;
  511. // -- Worker thread pool
  512. std::atomic<bool> JobError_ = ATOMIC_VAR_INIT(false);
  513. cmWorkerPool WorkerPool_;
  514. // -- Concurrent processing
  515. mutable std::mutex CMakeLibMutex_;
  516. };
  517. cmQtAutoMocUicT::IncludeKeyT::IncludeKeyT(std::string const& key,
  518. std::size_t basePrefixLength)
  519. : Key(key)
  520. , Dir(SubDirPrefix(key))
  521. , Base(cmSystemTools::GetFilenameWithoutLastExtension(key))
  522. {
  523. if (basePrefixLength != 0) {
  524. this->Base = this->Base.substr(basePrefixLength);
  525. }
  526. }
  527. void cmQtAutoMocUicT::ParseCacheT::FileT::Clear()
  528. {
  529. this->Moc.Macro.clear();
  530. this->Moc.Include.Underscore.clear();
  531. this->Moc.Include.Dot.clear();
  532. this->Moc.Depends.clear();
  533. this->Uic.Include.clear();
  534. this->Uic.Depends.clear();
  535. }
  536. cmQtAutoMocUicT::ParseCacheT::GetOrInsertT
  537. cmQtAutoMocUicT::ParseCacheT::GetOrInsert(std::string const& fileName)
  538. {
  539. // Find existing entry
  540. {
  541. auto it = this->Map_.find(fileName);
  542. if (it != this->Map_.end()) {
  543. return GetOrInsertT{ it->second, false };
  544. }
  545. }
  546. // Insert new entry
  547. return GetOrInsertT{
  548. this->Map_.emplace(fileName, std::make_shared<FileT>()).first->second, true
  549. };
  550. }
  551. cmQtAutoMocUicT::ParseCacheT::ParseCacheT() = default;
  552. cmQtAutoMocUicT::ParseCacheT::~ParseCacheT() = default;
  553. bool cmQtAutoMocUicT::ParseCacheT::ReadFromFile(std::string const& fileName)
  554. {
  555. cmsys::ifstream fin(fileName.c_str());
  556. if (!fin) {
  557. return false;
  558. }
  559. FileHandleT fileHandle;
  560. std::string line;
  561. while (std::getline(fin, line)) {
  562. // Check if this an empty or a comment line
  563. if (line.empty() || line.front() == '#') {
  564. continue;
  565. }
  566. // Drop carriage return character at the end
  567. if (line.back() == '\r') {
  568. line.pop_back();
  569. if (line.empty()) {
  570. continue;
  571. }
  572. }
  573. // Check if this a file name line
  574. if (line.front() != ' ') {
  575. fileHandle = this->GetOrInsert(line).first;
  576. continue;
  577. }
  578. // Bad line or bad file handle
  579. if (!fileHandle || (line.size() < 6)) {
  580. continue;
  581. }
  582. constexpr std::size_t offset = 5;
  583. if (cmHasLiteralPrefix(line, " mmc:")) {
  584. fileHandle->Moc.Macro = line.substr(offset);
  585. continue;
  586. }
  587. if (cmHasLiteralPrefix(line, " miu:")) {
  588. fileHandle->Moc.Include.Underscore.emplace_back(line.substr(offset),
  589. MocUnderscoreLength);
  590. continue;
  591. }
  592. if (cmHasLiteralPrefix(line, " mid:")) {
  593. fileHandle->Moc.Include.Dot.emplace_back(line.substr(offset), 0);
  594. continue;
  595. }
  596. if (cmHasLiteralPrefix(line, " mdp:")) {
  597. fileHandle->Moc.Depends.emplace_back(line.substr(offset));
  598. continue;
  599. }
  600. if (cmHasLiteralPrefix(line, " uic:")) {
  601. fileHandle->Uic.Include.emplace_back(line.substr(offset),
  602. UiUnderscoreLength);
  603. continue;
  604. }
  605. if (cmHasLiteralPrefix(line, " udp:")) {
  606. fileHandle->Uic.Depends.emplace_back(line.substr(offset));
  607. continue;
  608. }
  609. }
  610. return true;
  611. }
  612. bool cmQtAutoMocUicT::ParseCacheT::WriteToFile(std::string const& fileName)
  613. {
  614. cmGeneratedFileStream ofs(fileName);
  615. if (!ofs) {
  616. return false;
  617. }
  618. ofs << "# Generated by CMake. Changes will be overwritten.\n";
  619. for (auto const& pair : this->Map_) {
  620. ofs << pair.first << '\n';
  621. FileT const& file = *pair.second;
  622. if (!file.Moc.Macro.empty()) {
  623. ofs << " mmc:" << file.Moc.Macro << '\n';
  624. }
  625. for (IncludeKeyT const& item : file.Moc.Include.Underscore) {
  626. ofs << " miu:" << item.Key << '\n';
  627. }
  628. for (IncludeKeyT const& item : file.Moc.Include.Dot) {
  629. ofs << " mid:" << item.Key << '\n';
  630. }
  631. for (std::string const& item : file.Moc.Depends) {
  632. ofs << " mdp:" << item << '\n';
  633. }
  634. for (IncludeKeyT const& item : file.Uic.Include) {
  635. ofs << " uic:" << item.Key << '\n';
  636. }
  637. for (std::string const& item : file.Uic.Depends) {
  638. ofs << " udp:" << item << '\n';
  639. }
  640. }
  641. return ofs.Close();
  642. }
  643. cmQtAutoMocUicT::BaseSettingsT::BaseSettingsT() = default;
  644. cmQtAutoMocUicT::BaseSettingsT::~BaseSettingsT() = default;
  645. cmQtAutoMocUicT::MocSettingsT::MocSettingsT()
  646. {
  647. this->RegExpInclude.compile(
  648. "(^|\n)[ \t]*#[ \t]*include[ \t]+"
  649. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  650. }
  651. cmQtAutoMocUicT::MocSettingsT::~MocSettingsT() = default;
  652. bool cmQtAutoMocUicT::MocSettingsT::skipped(std::string const& fileName) const
  653. {
  654. return (!this->Enabled ||
  655. (this->SkipList.find(fileName) != this->SkipList.end()));
  656. }
  657. std::string cmQtAutoMocUicT::MocSettingsT::MacrosString() const
  658. {
  659. std::string res;
  660. const auto itB = this->MacroFilters.cbegin();
  661. const auto itE = this->MacroFilters.cend();
  662. const auto itL = itE - 1;
  663. auto itC = itB;
  664. for (; itC != itE; ++itC) {
  665. // Separator
  666. if (itC != itB) {
  667. if (itC != itL) {
  668. res += ", ";
  669. } else {
  670. res += " or ";
  671. }
  672. }
  673. // Key
  674. res += itC->Key;
  675. }
  676. return res;
  677. }
  678. cmQtAutoMocUicT::UicSettingsT::UicSettingsT()
  679. {
  680. this->RegExpInclude.compile("(^|\n)[ \t]*#[ \t]*include[ \t]+"
  681. "[\"<](([^ \">]+/)?ui_[^ \">/]+\\.h)[\">]");
  682. }
  683. cmQtAutoMocUicT::UicSettingsT::~UicSettingsT() = default;
  684. bool cmQtAutoMocUicT::UicSettingsT::skipped(std::string const& fileName) const
  685. {
  686. return (!this->Enabled ||
  687. (this->SkipList.find(fileName) != this->SkipList.end()));
  688. }
  689. void cmQtAutoMocUicT::JobT::LogError(GenT genType,
  690. cm::string_view message) const
  691. {
  692. this->Gen()->AbortError();
  693. this->Gen()->Log().Error(genType, message);
  694. }
  695. void cmQtAutoMocUicT::JobT::LogCommandError(
  696. GenT genType, cm::string_view message,
  697. std::vector<std::string> const& command, std::string const& output) const
  698. {
  699. this->Gen()->AbortError();
  700. this->Gen()->Log().ErrorCommand(genType, message, command, output);
  701. }
  702. bool cmQtAutoMocUicT::JobT::RunProcess(GenT genType,
  703. cmWorkerPool::ProcessResultT& result,
  704. std::vector<std::string> const& command,
  705. std::string* infoMessage)
  706. {
  707. // Log command
  708. if (this->Log().Verbose()) {
  709. cm::string_view info;
  710. if (infoMessage != nullptr) {
  711. info = *infoMessage;
  712. }
  713. this->Log().Info(
  714. genType,
  715. cmStrCat(info, info.empty() || cmHasSuffix(info, '\n') ? "" : "\n",
  716. QuotedCommand(command), '\n'));
  717. }
  718. // Run command
  719. return this->cmWorkerPool::JobT::RunProcess(
  720. result, command, this->BaseConst().AutogenBuildDir);
  721. }
  722. void cmQtAutoMocUicT::JobMocPredefsT::Process()
  723. {
  724. // (Re)generate moc_predefs.h on demand
  725. std::unique_ptr<std::string> reason;
  726. if (this->Log().Verbose()) {
  727. reason = cm::make_unique<std::string>();
  728. }
  729. if (!this->Update(reason.get())) {
  730. return;
  731. }
  732. std::string const& predefsFileAbs = this->MocConst().PredefsFileAbs;
  733. {
  734. cmWorkerPool::ProcessResultT result;
  735. {
  736. // Compose command
  737. std::vector<std::string> cmd = this->MocConst().PredefsCmd;
  738. // Add definitions
  739. cm::append(cmd, this->MocConst().OptionsDefinitions);
  740. // Add includes
  741. cm::append(cmd, this->MocConst().OptionsIncludes);
  742. // Execute command
  743. if (!this->RunProcess(GenT::MOC, result, cmd, reason.get())) {
  744. this->LogCommandError(GenT::MOC,
  745. cmStrCat("The content generation command for ",
  746. this->MessagePath(predefsFileAbs),
  747. " failed.\n", result.ErrorMessage),
  748. cmd, result.StdOut);
  749. return;
  750. }
  751. }
  752. // (Re)write predefs file only on demand
  753. if (cmQtAutoGenerator::FileDiffers(predefsFileAbs, result.StdOut)) {
  754. if (!cmQtAutoGenerator::FileWrite(predefsFileAbs, result.StdOut)) {
  755. this->LogError(
  756. GenT::MOC,
  757. cmStrCat("Writing ", this->MessagePath(predefsFileAbs), " failed."));
  758. return;
  759. }
  760. } else {
  761. // Touch to update the time stamp
  762. if (this->Log().Verbose()) {
  763. this->Log().Info(GenT::MOC,
  764. "Touching " + this->MessagePath(predefsFileAbs));
  765. }
  766. if (!cmSystemTools::Touch(predefsFileAbs, false)) {
  767. this->LogError(GenT::MOC,
  768. cmStrCat("Touching ", this->MessagePath(predefsFileAbs),
  769. " failed."));
  770. return;
  771. }
  772. }
  773. }
  774. // Read file time afterwards
  775. if (!this->MocEval().PredefsTime.Load(predefsFileAbs)) {
  776. this->LogError(GenT::MOC,
  777. cmStrCat("Reading the file time of ",
  778. this->MessagePath(predefsFileAbs), " failed."));
  779. return;
  780. }
  781. }
  782. bool cmQtAutoMocUicT::JobMocPredefsT::Update(std::string* reason) const
  783. {
  784. // Test if the file exists
  785. if (!this->MocEval().PredefsTime.Load(this->MocConst().PredefsFileAbs)) {
  786. if (reason != nullptr) {
  787. *reason = cmStrCat("Generating ",
  788. this->MessagePath(this->MocConst().PredefsFileAbs),
  789. ", because it doesn't exist.");
  790. }
  791. return true;
  792. }
  793. // Test if the settings changed
  794. if (this->MocConst().SettingsChanged) {
  795. if (reason != nullptr) {
  796. *reason = cmStrCat("Generating ",
  797. this->MessagePath(this->MocConst().PredefsFileAbs),
  798. ", because the moc settings changed.");
  799. }
  800. return true;
  801. }
  802. // Test if the executable is newer
  803. {
  804. std::string const& exec = this->MocConst().PredefsCmd.at(0);
  805. cmFileTime execTime;
  806. if (execTime.Load(exec)) {
  807. if (this->MocEval().PredefsTime.Older(execTime)) {
  808. if (reason != nullptr) {
  809. *reason = cmStrCat(
  810. "Generating ", this->MessagePath(this->MocConst().PredefsFileAbs),
  811. " because it is older than ", this->MessagePath(exec), '.');
  812. }
  813. return true;
  814. }
  815. }
  816. }
  817. return false;
  818. }
  819. bool cmQtAutoMocUicT::JobParseT::ReadFile()
  820. {
  821. // Clear old parse information
  822. this->FileHandle->ParseData->Clear();
  823. std::string const& fileName = this->FileHandle->FileName;
  824. // Write info
  825. if (this->Log().Verbose()) {
  826. this->Log().Info(GenT::GEN,
  827. cmStrCat("Parsing ", this->MessagePath(fileName)));
  828. }
  829. // Read file content
  830. {
  831. std::string error;
  832. if (!cmQtAutoGenerator::FileRead(this->Content, fileName, &error)) {
  833. this->LogError(GenT::GEN,
  834. cmStrCat("Could not read ", this->MessagePath(fileName),
  835. ".\n", error));
  836. return false;
  837. }
  838. }
  839. // Warn if empty
  840. if (this->Content.empty()) {
  841. this->Log().Warning(GenT::GEN,
  842. cmStrCat(this->MessagePath(fileName), " is empty."));
  843. return false;
  844. }
  845. return true;
  846. }
  847. void cmQtAutoMocUicT::JobParseT::CreateKeys(
  848. std::vector<IncludeKeyT>& container, std::set<std::string> const& source,
  849. std::size_t basePrefixLength)
  850. {
  851. if (source.empty()) {
  852. return;
  853. }
  854. container.reserve(source.size());
  855. for (std::string const& src : source) {
  856. container.emplace_back(src, basePrefixLength);
  857. }
  858. }
  859. void cmQtAutoMocUicT::JobParseT::MocMacro()
  860. {
  861. for (KeyExpT const& filter : this->MocConst().MacroFilters) {
  862. // Run a simple find string check
  863. if (this->Content.find(filter.Key) == std::string::npos) {
  864. continue;
  865. }
  866. // Run the expensive regular expression check loop
  867. cmsys::RegularExpressionMatch match;
  868. if (filter.Exp.find(this->Content.c_str(), match)) {
  869. // Keep detected macro name
  870. this->FileHandle->ParseData->Moc.Macro = filter.Key;
  871. return;
  872. }
  873. }
  874. }
  875. void cmQtAutoMocUicT::JobParseT::MocDependecies()
  876. {
  877. if (this->MocConst().DependFilters.empty() ||
  878. this->MocConst().CanOutputDependencies) {
  879. return;
  880. }
  881. // Find dependency strings
  882. std::set<std::string> parseDepends;
  883. for (KeyExpT const& filter : this->MocConst().DependFilters) {
  884. // Run a simple find string check
  885. if (this->Content.find(filter.Key) == std::string::npos) {
  886. continue;
  887. }
  888. // Run the expensive regular expression check loop
  889. const char* contentChars = this->Content.c_str();
  890. cmsys::RegularExpressionMatch match;
  891. while (filter.Exp.find(contentChars, match)) {
  892. {
  893. std::string dep = match.match(1);
  894. if (!dep.empty()) {
  895. parseDepends.emplace(std::move(dep));
  896. }
  897. }
  898. contentChars += match.end();
  899. }
  900. }
  901. // Store dependency strings
  902. {
  903. auto& Depends = this->FileHandle->ParseData->Moc.Depends;
  904. Depends.reserve(parseDepends.size());
  905. for (std::string const& item : parseDepends) {
  906. Depends.emplace_back(item);
  907. // Replace end of line characters in filenames
  908. std::string& path = Depends.back();
  909. std::replace(path.begin(), path.end(), '\n', ' ');
  910. std::replace(path.begin(), path.end(), '\r', ' ');
  911. }
  912. }
  913. }
  914. void cmQtAutoMocUicT::JobParseT::MocIncludes()
  915. {
  916. if (this->Content.find("moc") == std::string::npos) {
  917. return;
  918. }
  919. std::set<std::string> underscore;
  920. std::set<std::string> dot;
  921. {
  922. const char* contentChars = this->Content.c_str();
  923. cmsys::RegularExpression const& regExp = this->MocConst().RegExpInclude;
  924. cmsys::RegularExpressionMatch match;
  925. while (regExp.find(contentChars, match)) {
  926. std::string incString = match.match(2);
  927. std::string const incBase =
  928. cmSystemTools::GetFilenameWithoutLastExtension(incString);
  929. if (cmHasLiteralPrefix(incBase, "moc_")) {
  930. // moc_<BASE>.cpp
  931. // Remove the moc_ part from the base name
  932. underscore.emplace(std::move(incString));
  933. } else {
  934. // <BASE>.moc
  935. dot.emplace(std::move(incString));
  936. }
  937. // Forward content pointer
  938. contentChars += match.end();
  939. }
  940. }
  941. auto& Include = this->FileHandle->ParseData->Moc.Include;
  942. this->CreateKeys(Include.Underscore, underscore, MocUnderscoreLength);
  943. this->CreateKeys(Include.Dot, dot, 0);
  944. }
  945. void cmQtAutoMocUicT::JobParseT::UicIncludes()
  946. {
  947. if (this->Content.find("ui_") == std::string::npos) {
  948. return;
  949. }
  950. std::set<std::string> includes;
  951. {
  952. const char* contentChars = this->Content.c_str();
  953. cmsys::RegularExpression const& regExp = this->UicConst().RegExpInclude;
  954. cmsys::RegularExpressionMatch match;
  955. while (regExp.find(contentChars, match)) {
  956. includes.emplace(match.match(2));
  957. // Forward content pointer
  958. contentChars += match.end();
  959. }
  960. }
  961. this->CreateKeys(this->FileHandle->ParseData->Uic.Include, includes,
  962. UiUnderscoreLength);
  963. }
  964. void cmQtAutoMocUicT::JobParseHeaderT::Process()
  965. {
  966. if (!this->ReadFile()) {
  967. return;
  968. }
  969. // Moc parsing
  970. if (this->FileHandle->Moc) {
  971. this->MocMacro();
  972. this->MocDependecies();
  973. }
  974. // Uic parsing
  975. if (this->FileHandle->Uic) {
  976. this->UicIncludes();
  977. }
  978. }
  979. void cmQtAutoMocUicT::JobParseSourceT::Process()
  980. {
  981. if (!this->ReadFile()) {
  982. return;
  983. }
  984. // Moc parsing
  985. if (this->FileHandle->Moc) {
  986. this->MocMacro();
  987. this->MocDependecies();
  988. this->MocIncludes();
  989. }
  990. // Uic parsing
  991. if (this->FileHandle->Uic) {
  992. this->UicIncludes();
  993. }
  994. }
  995. std::string cmQtAutoMocUicT::JobEvalCacheT::MessageSearchLocations() const
  996. {
  997. std::string res;
  998. res.reserve(512);
  999. for (std::string const& path : this->SearchLocations) {
  1000. res += " ";
  1001. res += this->MessagePath(path);
  1002. res += '\n';
  1003. }
  1004. return res;
  1005. }
  1006. void cmQtAutoMocUicT::JobEvalCacheMocT::Process()
  1007. {
  1008. // Evaluate headers
  1009. for (auto const& pair : this->BaseEval().Headers) {
  1010. if (!this->EvalHeader(pair.second)) {
  1011. return;
  1012. }
  1013. }
  1014. // Evaluate sources
  1015. for (auto const& pair : this->BaseEval().Sources) {
  1016. if (!this->EvalSource(pair.second)) {
  1017. return;
  1018. }
  1019. }
  1020. }
  1021. bool cmQtAutoMocUicT::JobEvalCacheMocT::EvalHeader(SourceFileHandleT source)
  1022. {
  1023. SourceFileT const& sourceFile = *source;
  1024. auto const& parseData = sourceFile.ParseData->Moc;
  1025. if (!source->Moc) {
  1026. return true;
  1027. }
  1028. if (!parseData.Macro.empty()) {
  1029. // Create a new mapping
  1030. MappingHandleT handle = std::make_shared<MappingT>();
  1031. handle->SourceFile = std::move(source);
  1032. // Absolute build path
  1033. if (this->BaseConst().MultiConfig) {
  1034. handle->OutputFile =
  1035. this->Gen()->AbsoluteIncludePath(sourceFile.BuildPath);
  1036. } else {
  1037. handle->OutputFile =
  1038. this->Gen()->AbsoluteBuildPath(sourceFile.BuildPath);
  1039. }
  1040. // Register mapping in headers map
  1041. this->RegisterMapping(handle);
  1042. }
  1043. return true;
  1044. }
  1045. bool cmQtAutoMocUicT::JobEvalCacheMocT::EvalSource(
  1046. SourceFileHandleT const& source)
  1047. {
  1048. SourceFileT const& sourceFile = *source;
  1049. auto const& parseData = sourceFile.ParseData->Moc;
  1050. if (!sourceFile.Moc ||
  1051. (parseData.Macro.empty() && parseData.Include.Underscore.empty() &&
  1052. parseData.Include.Dot.empty())) {
  1053. return true;
  1054. }
  1055. std::string const sourceDirPrefix = SubDirPrefix(sourceFile.FileName);
  1056. std::string const sourceBase =
  1057. cmSystemTools::GetFilenameWithoutLastExtension(sourceFile.FileName);
  1058. // For relaxed mode check if the own "moc_" or ".moc" file is included
  1059. bool const relaxedMode = this->MocConst().RelaxedMode;
  1060. bool sourceIncludesMocUnderscore = false;
  1061. bool sourceIncludesDotMoc = false;
  1062. // Check if the sources own "moc_" or ".moc" file is included
  1063. if (relaxedMode) {
  1064. for (IncludeKeyT const& incKey : parseData.Include.Underscore) {
  1065. if (incKey.Base == sourceBase) {
  1066. sourceIncludesMocUnderscore = true;
  1067. break;
  1068. }
  1069. }
  1070. }
  1071. for (IncludeKeyT const& incKey : parseData.Include.Dot) {
  1072. if (incKey.Base == sourceBase) {
  1073. sourceIncludesDotMoc = true;
  1074. break;
  1075. }
  1076. }
  1077. // Check if this source needs to be moc processed but doesn't.
  1078. if (!sourceIncludesDotMoc && !parseData.Macro.empty() &&
  1079. !(relaxedMode && sourceIncludesMocUnderscore)) {
  1080. this->LogError(GenT::MOC,
  1081. cmStrCat(this->MessagePath(sourceFile.FileName),
  1082. "\ncontains a ", Quoted(parseData.Macro),
  1083. " macro, but does not include ",
  1084. this->MessagePath(sourceBase + ".moc"),
  1085. "!\nConsider to\n - add #include \"", sourceBase,
  1086. ".moc\"\n - enable SKIP_AUTOMOC for this file"));
  1087. return false;
  1088. }
  1089. // Evaluate "moc_" includes
  1090. for (IncludeKeyT const& incKey : parseData.Include.Underscore) {
  1091. SourceFileHandleT headerHandle;
  1092. {
  1093. std::string const headerBase = cmStrCat(incKey.Dir, incKey.Base);
  1094. if (!this->FindIncludedHeader(headerHandle, sourceDirPrefix,
  1095. headerBase)) {
  1096. this->LogError(
  1097. GenT::MOC,
  1098. cmStrCat(this->MessagePath(sourceFile.FileName),
  1099. "\nincludes the moc file ", this->MessagePath(incKey.Key),
  1100. ",\nbut a header ", this->MessageHeader(headerBase),
  1101. "\ncould not be found "
  1102. "in the following directories\n",
  1103. this->MessageSearchLocations()));
  1104. return false;
  1105. }
  1106. }
  1107. // The include might be handled differently in relaxed mode
  1108. if (relaxedMode && !sourceIncludesDotMoc && !parseData.Macro.empty() &&
  1109. (incKey.Base == sourceBase)) {
  1110. // The <BASE>.cpp file includes a Qt macro but does not include the
  1111. // <BASE>.moc file. In this case, the moc_<BASE>.cpp should probably
  1112. // be generated from <BASE>.cpp instead of <BASE>.h, because otherwise
  1113. // it won't build. But warn, since this is not how it is supposed to be
  1114. // used. This is for KDE4 compatibility.
  1115. // Issue a warning
  1116. this->Log().Warning(
  1117. GenT::MOC,
  1118. cmStrCat(this->MessagePath(sourceFile.FileName), "\ncontains a ",
  1119. Quoted(parseData.Macro), " macro, but does not include ",
  1120. this->MessagePath(sourceBase + ".moc"),
  1121. ".\nInstead it includes ", this->MessagePath(incKey.Key),
  1122. ".\nRunning moc on the source\n ",
  1123. this->MessagePath(sourceFile.FileName), "!\nBetter include ",
  1124. this->MessagePath(sourceBase + ".moc"),
  1125. " for compatibility with regular mode.\n",
  1126. "This is a CMAKE_AUTOMOC_RELAXED_MODE warning.\n"));
  1127. // Create mapping
  1128. if (!this->RegisterIncluded(incKey.Key, source, source)) {
  1129. return false;
  1130. }
  1131. continue;
  1132. }
  1133. // Check if header is skipped
  1134. if (this->MocConst().skipped(headerHandle->FileName)) {
  1135. continue;
  1136. }
  1137. // Create mapping
  1138. if (!this->RegisterIncluded(incKey.Key, source, std::move(headerHandle))) {
  1139. return false;
  1140. }
  1141. }
  1142. // Evaluate ".moc" includes
  1143. if (relaxedMode) {
  1144. // Relaxed mode
  1145. for (IncludeKeyT const& incKey : parseData.Include.Dot) {
  1146. // Check if this is the sources own .moc file
  1147. bool const ownMoc = (incKey.Base == sourceBase);
  1148. if (ownMoc && !parseData.Macro.empty()) {
  1149. // Create mapping for the regular use case
  1150. if (!this->RegisterIncluded(incKey.Key, source, source)) {
  1151. return false;
  1152. }
  1153. continue;
  1154. }
  1155. // Try to find a header instead but issue a warning.
  1156. // This is for KDE4 compatibility.
  1157. SourceFileHandleT headerHandle;
  1158. {
  1159. std::string const headerBase = cmStrCat(incKey.Dir, incKey.Base);
  1160. if (!this->FindIncludedHeader(headerHandle, sourceDirPrefix,
  1161. headerBase)) {
  1162. this->LogError(
  1163. GenT::MOC,
  1164. cmStrCat(
  1165. this->MessagePath(sourceFile.FileName),
  1166. "\nincludes the moc file ", this->MessagePath(incKey.Key),
  1167. ",\nwhich seems to be the moc file from a different source "
  1168. "file.\nCMAKE_AUTOMOC_RELAXED_MODE:\nAlso a matching header ",
  1169. this->MessageHeader(headerBase),
  1170. "\ncould not be found in the following directories\n",
  1171. this->MessageSearchLocations()));
  1172. return false;
  1173. }
  1174. }
  1175. // Check if header is skipped
  1176. if (this->MocConst().skipped(headerHandle->FileName)) {
  1177. continue;
  1178. }
  1179. // Issue a warning
  1180. if (ownMoc && parseData.Macro.empty()) {
  1181. this->Log().Warning(
  1182. GenT::MOC,
  1183. cmStrCat(
  1184. this->MessagePath(sourceFile.FileName), "\nincludes the moc file ",
  1185. this->MessagePath(incKey.Key), ", but does not contain a\n",
  1186. this->MocConst().MacrosString(),
  1187. " macro.\nRunning moc on the header\n ",
  1188. this->MessagePath(headerHandle->FileName), "!\nBetter include ",
  1189. this->MessagePath("moc_" + incKey.Base + ".cpp"),
  1190. " for a compatibility with regular mode.\n",
  1191. "This is a CMAKE_AUTOMOC_RELAXED_MODE warning.\n"));
  1192. } else {
  1193. this->Log().Warning(
  1194. GenT::MOC,
  1195. cmStrCat(
  1196. this->MessagePath(sourceFile.FileName), "\nincludes the moc file ",
  1197. this->MessagePath(incKey.Key), " instead of ",
  1198. this->MessagePath("moc_" + incKey.Base + ".cpp"),
  1199. ".\nRunning moc on the header\n ",
  1200. this->MessagePath(headerHandle->FileName), "!\nBetter include ",
  1201. this->MessagePath("moc_" + incKey.Base + ".cpp"),
  1202. " for compatibility with regular mode.\n",
  1203. "This is a CMAKE_AUTOMOC_RELAXED_MODE warning.\n"));
  1204. }
  1205. // Create mapping
  1206. if (!this->RegisterIncluded(incKey.Key, source,
  1207. std::move(headerHandle))) {
  1208. return false;
  1209. }
  1210. }
  1211. } else {
  1212. // Strict mode
  1213. for (IncludeKeyT const& incKey : parseData.Include.Dot) {
  1214. // Check if this is the sources own .moc file
  1215. bool const ownMoc = (incKey.Base == sourceBase);
  1216. if (!ownMoc) {
  1217. // Don't allow <BASE>.moc include other than own in regular mode
  1218. this->LogError(
  1219. GenT::MOC,
  1220. cmStrCat(this->MessagePath(sourceFile.FileName),
  1221. "\nincludes the moc file ", this->MessagePath(incKey.Key),
  1222. ",\nwhich seems to be the moc file from a different "
  1223. "source file.\nThis is not supported. Include ",
  1224. this->MessagePath(sourceBase + ".moc"),
  1225. " to run moc on this source file."));
  1226. return false;
  1227. }
  1228. // Accept but issue a warning if moc isn't required
  1229. if (parseData.Macro.empty()) {
  1230. this->Log().Warning(
  1231. GenT::MOC,
  1232. cmStrCat(this->MessagePath(sourceFile.FileName),
  1233. "\nincludes the moc file ", this->MessagePath(incKey.Key),
  1234. ", but does not contain a ",
  1235. this->MocConst().MacrosString(), " macro."));
  1236. }
  1237. // Create mapping
  1238. if (!this->RegisterIncluded(incKey.Key, source, source)) {
  1239. return false;
  1240. }
  1241. }
  1242. }
  1243. return true;
  1244. }
  1245. bool cmQtAutoMocUicT::JobEvalCacheMocT::FindIncludedHeader(
  1246. SourceFileHandleT& headerHandle, cm::string_view includerDir,
  1247. cm::string_view includeBase)
  1248. {
  1249. // Clear search locations
  1250. this->SearchLocations.clear();
  1251. auto findHeader = [this,
  1252. &headerHandle](std::string const& basePath) -> bool {
  1253. bool found = false;
  1254. for (std::string const& ext : this->BaseConst().HeaderExtensions) {
  1255. std::string const testPath =
  1256. this->Gen()->CollapseFullPathTS(cmStrCat(basePath, '.', ext));
  1257. cmFileTime fileTime;
  1258. if (!fileTime.Load(testPath)) {
  1259. // File not found
  1260. continue;
  1261. }
  1262. // Return a known file if it exists already
  1263. {
  1264. auto it = this->BaseEval().Headers.find(testPath);
  1265. if (it != this->BaseEval().Headers.end()) {
  1266. headerHandle = it->second;
  1267. found = true;
  1268. break;
  1269. }
  1270. }
  1271. // Created and return discovered file entry
  1272. {
  1273. SourceFileHandleT& handle =
  1274. this->MocEval().HeadersDiscovered[testPath];
  1275. if (!handle) {
  1276. handle = std::make_shared<SourceFileT>(testPath);
  1277. handle->FileTime = fileTime;
  1278. handle->IsHeader = true;
  1279. handle->Moc = true;
  1280. }
  1281. headerHandle = handle;
  1282. found = true;
  1283. break;
  1284. }
  1285. }
  1286. if (!found) {
  1287. this->SearchLocations.emplace_back(cmQtAutoGen::ParentDir(basePath));
  1288. }
  1289. return found;
  1290. };
  1291. // Search in vicinity of the source
  1292. if (findHeader(cmStrCat(includerDir, includeBase))) {
  1293. return true;
  1294. }
  1295. // Search in include directories
  1296. for (std::string const& path : this->MocConst().IncludePaths) {
  1297. if (findHeader(cmStrCat(path, '/', includeBase))) {
  1298. return true;
  1299. }
  1300. }
  1301. // Return without success
  1302. return false;
  1303. }
  1304. bool cmQtAutoMocUicT::JobEvalCacheMocT::RegisterIncluded(
  1305. std::string const& includeString, SourceFileHandleT includerFileHandle,
  1306. SourceFileHandleT sourceFileHandle) const
  1307. {
  1308. // Check if this file is already included
  1309. MappingHandleT& handle = this->MocEval().Includes[includeString];
  1310. if (handle) {
  1311. // Check if the output file would be generated from different source files
  1312. if (handle->SourceFile != sourceFileHandle) {
  1313. std::string files =
  1314. cmStrCat(" ", this->MessagePath(includerFileHandle->FileName), '\n');
  1315. for (auto const& item : handle->IncluderFiles) {
  1316. files += cmStrCat(" ", this->MessagePath(item->FileName), '\n');
  1317. }
  1318. this->LogError(
  1319. GenT::MOC,
  1320. cmStrCat("The source files\n", files,
  1321. "contain the same include string ",
  1322. this->MessagePath(includeString),
  1323. ", but\nthe moc file would be generated from different "
  1324. "source files\n ",
  1325. this->MessagePath(sourceFileHandle->FileName), " and\n ",
  1326. this->MessagePath(handle->SourceFile->FileName),
  1327. ".\nConsider to\n"
  1328. " - not include the \"moc_<NAME>.cpp\" file\n"
  1329. " - add a directory prefix to a \"<NAME>.moc\" include "
  1330. "(e.g \"sub/<NAME>.moc\")\n"
  1331. " - rename the source file(s)\n"));
  1332. return false;
  1333. }
  1334. // The same mapping already exists. Just add to the includers list.
  1335. handle->IncluderFiles.emplace_back(std::move(includerFileHandle));
  1336. return true;
  1337. }
  1338. // Create a new mapping
  1339. handle = std::make_shared<MappingT>();
  1340. handle->IncludeString = includeString;
  1341. handle->IncluderFiles.emplace_back(std::move(includerFileHandle));
  1342. handle->SourceFile = std::move(sourceFileHandle);
  1343. handle->OutputFile = this->Gen()->AbsoluteIncludePath(includeString);
  1344. // Register mapping in sources/headers map
  1345. this->RegisterMapping(handle);
  1346. return true;
  1347. }
  1348. void cmQtAutoMocUicT::JobEvalCacheMocT::RegisterMapping(
  1349. MappingHandleT mappingHandle) const
  1350. {
  1351. auto& regMap = mappingHandle->SourceFile->IsHeader
  1352. ? this->MocEval().HeaderMappings
  1353. : this->MocEval().SourceMappings;
  1354. // Check if source file already gets mapped
  1355. auto& regHandle = regMap[mappingHandle->SourceFile->FileName];
  1356. if (!regHandle) {
  1357. // Yet unknown mapping
  1358. regHandle = std::move(mappingHandle);
  1359. } else {
  1360. // Mappings with include string override those without
  1361. if (!mappingHandle->IncludeString.empty()) {
  1362. regHandle = std::move(mappingHandle);
  1363. }
  1364. }
  1365. }
  1366. std::string cmQtAutoMocUicT::JobEvalCacheMocT::MessageHeader(
  1367. cm::string_view headerBase) const
  1368. {
  1369. return this->MessagePath(cmStrCat(
  1370. headerBase, ".{", cmJoin(this->BaseConst().HeaderExtensions, ","), '}'));
  1371. }
  1372. void cmQtAutoMocUicT::JobEvalCacheUicT::Process()
  1373. {
  1374. // Prepare buffers
  1375. this->SearchLocations.reserve((this->UicConst().SearchPaths.size() + 1) * 2);
  1376. // Evaluate headers
  1377. for (auto const& pair : this->BaseEval().Headers) {
  1378. if (!this->EvalFile(pair.second)) {
  1379. return;
  1380. }
  1381. }
  1382. // Evaluate sources
  1383. for (auto const& pair : this->BaseEval().Sources) {
  1384. if (!this->EvalFile(pair.second)) {
  1385. return;
  1386. }
  1387. }
  1388. }
  1389. bool cmQtAutoMocUicT::JobEvalCacheUicT::EvalFile(
  1390. SourceFileHandleT const& sourceFileHandle)
  1391. {
  1392. SourceFileT const& sourceFile = *sourceFileHandle;
  1393. auto const& Include = sourceFile.ParseData->Uic.Include;
  1394. if (!sourceFile.Uic || Include.empty()) {
  1395. return true;
  1396. }
  1397. std::string const sourceDirPrefix = SubDirPrefix(sourceFile.FileName);
  1398. for (IncludeKeyT const& incKey : Include) {
  1399. // Find .ui file
  1400. this->UiName = cmStrCat(incKey.Base, ".ui");
  1401. if (!this->FindIncludedUi(sourceDirPrefix, incKey.Dir)) {
  1402. this->LogError(
  1403. GenT::UIC,
  1404. cmStrCat(this->MessagePath(sourceFile.FileName),
  1405. "\nincludes the uic file ", this->MessagePath(incKey.Key),
  1406. ",\nbut the user interface file ",
  1407. this->MessagePath(this->UiName),
  1408. "\ncould not be found in the following directories\n",
  1409. this->MessageSearchLocations()));
  1410. return false;
  1411. }
  1412. // Check if the file is skipped
  1413. if (this->UicConst().skipped(this->UiFileHandle->FileName)) {
  1414. continue;
  1415. }
  1416. // Register mapping
  1417. if (!this->RegisterMapping(incKey.Key, sourceFileHandle)) {
  1418. return false;
  1419. }
  1420. }
  1421. return true;
  1422. }
  1423. bool cmQtAutoMocUicT::JobEvalCacheUicT::FindIncludedUi(
  1424. cm::string_view sourceDirPrefix, cm::string_view includePrefix)
  1425. {
  1426. // Clear locations buffer
  1427. this->SearchLocations.clear();
  1428. auto findUi = [this](std::string const& testPath) -> bool {
  1429. std::string const fullPath = this->Gen()->CollapseFullPathTS(testPath);
  1430. cmFileTime fileTime;
  1431. if (!fileTime.Load(fullPath)) {
  1432. this->SearchLocations.emplace_back(cmQtAutoGen::ParentDir(fullPath));
  1433. return false;
  1434. }
  1435. // .ui file found in files system!
  1436. // Get or create .ui file handle
  1437. SourceFileHandleT& handle = this->UicEval().UiFiles[fullPath];
  1438. if (!handle) {
  1439. // The file wasn't registered, yet
  1440. handle = std::make_shared<SourceFileT>(fullPath);
  1441. handle->FileTime = fileTime;
  1442. }
  1443. this->UiFileHandle = handle;
  1444. return true;
  1445. };
  1446. // Vicinity of the source
  1447. if (findUi(cmStrCat(sourceDirPrefix, this->UiName))) {
  1448. return true;
  1449. }
  1450. if (!includePrefix.empty()) {
  1451. if (findUi(cmStrCat(sourceDirPrefix, includePrefix, this->UiName))) {
  1452. return true;
  1453. }
  1454. }
  1455. // Additional AUTOUIC search paths
  1456. auto const& searchPaths = this->UicConst().SearchPaths;
  1457. if (!searchPaths.empty()) {
  1458. for (std::string const& sPath : searchPaths) {
  1459. if (findUi(cmStrCat(sPath, '/', this->UiName))) {
  1460. return true;
  1461. }
  1462. }
  1463. if (!includePrefix.empty()) {
  1464. for (std::string const& sPath : searchPaths) {
  1465. if (findUi(cmStrCat(sPath, '/', includePrefix, this->UiName))) {
  1466. return true;
  1467. }
  1468. }
  1469. }
  1470. }
  1471. return false;
  1472. }
  1473. bool cmQtAutoMocUicT::JobEvalCacheUicT::RegisterMapping(
  1474. std::string const& includeString, SourceFileHandleT includerFileHandle)
  1475. {
  1476. auto& Includes = this->Gen()->UicEval().Includes;
  1477. auto it = Includes.find(includeString);
  1478. if (it != Includes.end()) {
  1479. MappingHandleT const& handle = it->second;
  1480. if (handle->SourceFile != this->UiFileHandle) {
  1481. // The output file already gets generated - from a different .ui file!
  1482. std::string files =
  1483. cmStrCat(" ", this->MessagePath(includerFileHandle->FileName), '\n');
  1484. for (auto const& item : handle->IncluderFiles) {
  1485. files += cmStrCat(" ", this->MessagePath(item->FileName), '\n');
  1486. }
  1487. this->LogError(
  1488. GenT::UIC,
  1489. cmStrCat(
  1490. "The source files\n", files, "contain the same include string ",
  1491. Quoted(includeString),
  1492. ", but\nthe uic file would be generated from different "
  1493. "user interface files\n ",
  1494. this->MessagePath(this->UiFileHandle->FileName), " and\n ",
  1495. this->MessagePath(handle->SourceFile->FileName),
  1496. ".\nConsider to\n"
  1497. " - add a directory prefix to a \"ui_<NAME>.h\" include "
  1498. "(e.g \"sub/ui_<NAME>.h\")\n"
  1499. " - rename the <NAME>.ui file(s) and adjust the \"ui_<NAME>.h\" "
  1500. "include(s)\n"));
  1501. return false;
  1502. }
  1503. // Add includer file to existing mapping
  1504. handle->IncluderFiles.emplace_back(std::move(includerFileHandle));
  1505. } else {
  1506. // New mapping handle
  1507. MappingHandleT handle = std::make_shared<MappingT>();
  1508. handle->IncludeString = includeString;
  1509. handle->IncluderFiles.emplace_back(std::move(includerFileHandle));
  1510. handle->SourceFile = this->UiFileHandle;
  1511. handle->OutputFile = this->Gen()->AbsoluteIncludePath(includeString);
  1512. // Register mapping
  1513. Includes.emplace(includeString, std::move(handle));
  1514. }
  1515. return true;
  1516. }
  1517. void cmQtAutoMocUicT::JobEvalCacheFinishT::Process()
  1518. {
  1519. // Add discovered header parse jobs
  1520. this->Gen()->CreateParseJobs<JobParseHeaderT>(
  1521. this->MocEval().HeadersDiscovered);
  1522. // Add dependency probing jobs
  1523. {
  1524. // Add fence job to ensure all parsing has finished
  1525. this->Gen()->WorkerPool().EmplaceJob<JobFenceT>();
  1526. if (this->MocConst().Enabled) {
  1527. this->Gen()->WorkerPool().EmplaceJob<JobProbeDepsMocT>();
  1528. }
  1529. if (this->UicConst().Enabled) {
  1530. this->Gen()->WorkerPool().EmplaceJob<JobProbeDepsUicT>();
  1531. }
  1532. // Add probe finish job
  1533. this->Gen()->WorkerPool().EmplaceJob<JobProbeDepsFinishT>();
  1534. }
  1535. }
  1536. void cmQtAutoMocUicT::JobProbeDepsMocT::Process()
  1537. {
  1538. // Create moc header jobs
  1539. for (auto const& pair : this->MocEval().HeaderMappings) {
  1540. // Register if this mapping is a candidate for mocs_compilation.cpp
  1541. bool const compFile = pair.second->IncludeString.empty();
  1542. if (compFile) {
  1543. this->MocEval().CompFiles.emplace_back(
  1544. pair.second->SourceFile->BuildPath);
  1545. }
  1546. if (!this->Generate(pair.second, compFile)) {
  1547. return;
  1548. }
  1549. }
  1550. // Create moc source jobs
  1551. for (auto const& pair : this->MocEval().SourceMappings) {
  1552. if (!this->Generate(pair.second, false)) {
  1553. return;
  1554. }
  1555. }
  1556. }
  1557. bool cmQtAutoMocUicT::JobProbeDepsMocT::Generate(MappingHandleT const& mapping,
  1558. bool compFile) const
  1559. {
  1560. std::unique_ptr<std::string> reason;
  1561. if (this->Log().Verbose()) {
  1562. reason = cm::make_unique<std::string>();
  1563. }
  1564. if (this->Probe(*mapping, reason.get())) {
  1565. // Register the parent directory for creation
  1566. this->MocEval().OutputDirs.emplace(
  1567. cmQtAutoGen::ParentDir(mapping->OutputFile));
  1568. // Fetch the cache entry for the source file
  1569. std::string const& sourceFile = mapping->SourceFile->FileName;
  1570. ParseCacheT::GetOrInsertT cacheEntry =
  1571. this->BaseEval().ParseCache.GetOrInsert(sourceFile);
  1572. // Add moc job
  1573. this->Gen()->WorkerPool().EmplaceJob<JobCompileMocT>(
  1574. mapping, std::move(reason), std::move(cacheEntry.first));
  1575. // Check if a moc job for a mocs_compilation.cpp entry was generated
  1576. if (compFile) {
  1577. this->MocEval().CompUpdated = true;
  1578. }
  1579. }
  1580. return true;
  1581. }
  1582. bool cmQtAutoMocUicT::JobProbeDepsMocT::Probe(MappingT const& mapping,
  1583. std::string* reason) const
  1584. {
  1585. std::string const& sourceFile = mapping.SourceFile->FileName;
  1586. std::string const& outputFile = mapping.OutputFile;
  1587. // Test if the output file exists
  1588. cmFileTime outputFileTime;
  1589. if (!outputFileTime.Load(outputFile)) {
  1590. if (reason != nullptr) {
  1591. *reason = cmStrCat("Generating ", this->MessagePath(outputFile),
  1592. ", because it doesn't exist, from ",
  1593. this->MessagePath(sourceFile));
  1594. }
  1595. return true;
  1596. }
  1597. // Test if any setting changed
  1598. if (this->MocConst().SettingsChanged) {
  1599. if (reason != nullptr) {
  1600. *reason = cmStrCat("Generating ", this->MessagePath(outputFile),
  1601. ", because the uic settings changed, from ",
  1602. this->MessagePath(sourceFile));
  1603. }
  1604. return true;
  1605. }
  1606. // Test if the source file is newer
  1607. if (outputFileTime.Older(mapping.SourceFile->FileTime)) {
  1608. if (reason != nullptr) {
  1609. *reason = cmStrCat("Generating ", this->MessagePath(outputFile),
  1610. ", because it's older than its source file, from ",
  1611. this->MessagePath(sourceFile));
  1612. }
  1613. return true;
  1614. }
  1615. // Test if the moc_predefs file is newer
  1616. if (!this->MocConst().PredefsFileAbs.empty()) {
  1617. if (outputFileTime.Older(this->MocEval().PredefsTime)) {
  1618. if (reason != nullptr) {
  1619. *reason = cmStrCat("Generating ", this->MessagePath(outputFile),
  1620. ", because it's older than ",
  1621. this->MessagePath(this->MocConst().PredefsFileAbs),
  1622. ", from ", this->MessagePath(sourceFile));
  1623. }
  1624. return true;
  1625. }
  1626. }
  1627. // Test if the moc executable is newer
  1628. if (outputFileTime.Older(this->MocConst().ExecutableTime)) {
  1629. if (reason != nullptr) {
  1630. *reason = cmStrCat("Generating ", this->MessagePath(outputFile),
  1631. ", because it's older than the moc executable, from ",
  1632. this->MessagePath(sourceFile));
  1633. }
  1634. return true;
  1635. }
  1636. // Test if a dependency file is newer
  1637. {
  1638. // Check dependency timestamps
  1639. std::string const sourceDir = SubDirPrefix(sourceFile);
  1640. auto& dependencies = mapping.SourceFile->ParseData->Moc.Depends;
  1641. for (auto it = dependencies.begin(); it != dependencies.end(); ++it) {
  1642. auto& dep = *it;
  1643. // Find dependency file
  1644. auto const depMatch = this->FindDependency(sourceDir, dep);
  1645. if (depMatch.first.empty()) {
  1646. if (reason != nullptr) {
  1647. *reason = cmStrCat("Generating ", this->MessagePath(outputFile),
  1648. " from ", this->MessagePath(sourceFile),
  1649. ", because its dependency ",
  1650. this->MessagePath(dep), " vanished.");
  1651. }
  1652. dependencies.erase(it);
  1653. this->BaseEval().ParseCacheChanged = true;
  1654. return true;
  1655. }
  1656. // Test if dependency file is older
  1657. if (outputFileTime.Older(depMatch.second)) {
  1658. if (reason != nullptr) {
  1659. *reason = cmStrCat("Generating ", this->MessagePath(outputFile),
  1660. ", because it's older than its dependency file ",
  1661. this->MessagePath(depMatch.first), ", from ",
  1662. this->MessagePath(sourceFile));
  1663. }
  1664. return true;
  1665. }
  1666. }
  1667. }
  1668. return false;
  1669. }
  1670. std::pair<std::string, cmFileTime>
  1671. cmQtAutoMocUicT::JobProbeDepsMocT::FindDependency(
  1672. std::string const& sourceDir, std::string const& includeString) const
  1673. {
  1674. using ResPair = std::pair<std::string, cmFileTime>;
  1675. // moc's dependency file contains absolute paths
  1676. if (this->MocConst().CanOutputDependencies) {
  1677. ResPair res{ includeString, {} };
  1678. if (res.second.Load(res.first)) {
  1679. return res;
  1680. }
  1681. return {};
  1682. }
  1683. // Search in vicinity of the source
  1684. {
  1685. ResPair res{ sourceDir + includeString, {} };
  1686. if (res.second.Load(res.first)) {
  1687. return res;
  1688. }
  1689. }
  1690. // Search in include directories
  1691. for (std::string const& includePath : this->MocConst().IncludePaths) {
  1692. ResPair res{ cmStrCat(includePath, '/', includeString), {} };
  1693. if (res.second.Load(res.first)) {
  1694. return res;
  1695. }
  1696. }
  1697. // Return empty
  1698. return ResPair();
  1699. }
  1700. void cmQtAutoMocUicT::JobProbeDepsUicT::Process()
  1701. {
  1702. for (auto const& pair : this->Gen()->UicEval().Includes) {
  1703. MappingHandleT const& mapping = pair.second;
  1704. std::unique_ptr<std::string> reason;
  1705. if (this->Log().Verbose()) {
  1706. reason = cm::make_unique<std::string>();
  1707. }
  1708. if (!this->Probe(*mapping, reason.get())) {
  1709. continue;
  1710. }
  1711. // Register the parent directory for creation
  1712. this->UicEval().OutputDirs.emplace(
  1713. cmQtAutoGen::ParentDir(mapping->OutputFile));
  1714. // Add uic job
  1715. this->Gen()->WorkerPool().EmplaceJob<JobCompileUicT>(mapping,
  1716. std::move(reason));
  1717. }
  1718. }
  1719. bool cmQtAutoMocUicT::JobProbeDepsUicT::Probe(MappingT const& mapping,
  1720. std::string* reason) const
  1721. {
  1722. std::string const& sourceFile = mapping.SourceFile->FileName;
  1723. std::string const& outputFile = mapping.OutputFile;
  1724. // Test if the build file exists
  1725. cmFileTime outputFileTime;
  1726. if (!outputFileTime.Load(outputFile)) {
  1727. if (reason != nullptr) {
  1728. *reason = cmStrCat("Generating ", this->MessagePath(outputFile),
  1729. ", because it doesn't exist, from ",
  1730. this->MessagePath(sourceFile));
  1731. }
  1732. return true;
  1733. }
  1734. // Test if the uic settings changed
  1735. if (this->UicConst().SettingsChanged) {
  1736. if (reason != nullptr) {
  1737. *reason = cmStrCat("Generating ", this->MessagePath(outputFile),
  1738. ", because the uic settings changed, from ",
  1739. this->MessagePath(sourceFile));
  1740. }
  1741. return true;
  1742. }
  1743. // Test if the source file is newer
  1744. if (outputFileTime.Older(mapping.SourceFile->FileTime)) {
  1745. if (reason != nullptr) {
  1746. *reason = cmStrCat("Generating ", this->MessagePath(outputFile),
  1747. " because it's older than the source file ",
  1748. this->MessagePath(sourceFile));
  1749. }
  1750. return true;
  1751. }
  1752. // Test if the uic executable is newer
  1753. if (outputFileTime.Older(this->UicConst().ExecutableTime)) {
  1754. if (reason != nullptr) {
  1755. *reason = cmStrCat("Generating ", this->MessagePath(outputFile),
  1756. ", because it's older than the uic executable, from ",
  1757. this->MessagePath(sourceFile));
  1758. }
  1759. return true;
  1760. }
  1761. return false;
  1762. }
  1763. void cmQtAutoMocUicT::JobProbeDepsFinishT::Process()
  1764. {
  1765. // Create output directories
  1766. {
  1767. using StringSet = std::unordered_set<std::string>;
  1768. auto createDirs = [this](GenT genType, StringSet const& dirSet) {
  1769. for (std::string const& dirName : dirSet) {
  1770. if (!cmSystemTools::MakeDirectory(dirName)) {
  1771. this->LogError(genType,
  1772. cmStrCat("Creating directory ",
  1773. this->MessagePath(dirName), " failed."));
  1774. return;
  1775. }
  1776. }
  1777. };
  1778. if (this->MocConst().Enabled && this->UicConst().Enabled) {
  1779. StringSet outputDirs = this->MocEval().OutputDirs;
  1780. outputDirs.insert(this->UicEval().OutputDirs.begin(),
  1781. this->UicEval().OutputDirs.end());
  1782. createDirs(GenT::GEN, outputDirs);
  1783. } else if (this->MocConst().Enabled) {
  1784. createDirs(GenT::MOC, this->MocEval().OutputDirs);
  1785. } else if (this->UicConst().Enabled) {
  1786. createDirs(GenT::UIC, this->UicEval().OutputDirs);
  1787. }
  1788. }
  1789. if (this->MocConst().Enabled) {
  1790. // Add mocs compilations job
  1791. this->Gen()->WorkerPool().EmplaceJob<JobMocsCompilationT>();
  1792. }
  1793. if (!this->BaseConst().DepFile.empty()) {
  1794. // Add job to merge dep files
  1795. this->Gen()->WorkerPool().EmplaceJob<JobDepFilesMergeT>();
  1796. }
  1797. // Add finish job
  1798. this->Gen()->WorkerPool().EmplaceJob<JobFinishT>();
  1799. }
  1800. void cmQtAutoMocUicT::JobCompileMocT::Process()
  1801. {
  1802. std::string const& sourceFile = this->Mapping->SourceFile->FileName;
  1803. std::string const& outputFile = this->Mapping->OutputFile;
  1804. // Compose moc command
  1805. std::vector<std::string> cmd;
  1806. {
  1807. // Reserve large enough
  1808. cmd.reserve(this->MocConst().OptionsDefinitions.size() +
  1809. this->MocConst().OptionsIncludes.size() +
  1810. this->MocConst().OptionsExtra.size() + 16);
  1811. cmd.push_back(this->MocConst().Executable);
  1812. // Add definitions
  1813. cm::append(cmd, this->MocConst().OptionsDefinitions);
  1814. // Add includes
  1815. cm::append(cmd, this->MocConst().OptionsIncludes);
  1816. // Add predefs include
  1817. if (!this->MocConst().PredefsFileAbs.empty()) {
  1818. cmd.emplace_back("--include");
  1819. cmd.push_back(this->MocConst().PredefsFileAbs);
  1820. }
  1821. // Add path prefix on demand
  1822. if (this->MocConst().PathPrefix && this->Mapping->SourceFile->IsHeader) {
  1823. for (std::string const& dir : this->MocConst().IncludePaths) {
  1824. cm::string_view prefix = sourceFile;
  1825. if (cmHasPrefix(prefix, dir)) {
  1826. prefix.remove_prefix(dir.size());
  1827. if (cmHasPrefix(prefix, '/')) {
  1828. prefix.remove_prefix(1);
  1829. auto slashPos = prefix.rfind('/');
  1830. if (slashPos != cm::string_view::npos) {
  1831. cmd.emplace_back("-p");
  1832. cmd.emplace_back(prefix.substr(0, slashPos));
  1833. } else {
  1834. cmd.emplace_back("-p");
  1835. cmd.emplace_back("./");
  1836. }
  1837. break;
  1838. }
  1839. }
  1840. }
  1841. }
  1842. // Add extra options
  1843. cm::append(cmd, this->MocConst().OptionsExtra);
  1844. if (this->MocConst().CanOutputDependencies) {
  1845. cmd.emplace_back("--output-dep-file");
  1846. }
  1847. // Add output file
  1848. cmd.emplace_back("-o");
  1849. cmd.push_back(outputFile);
  1850. // Add source file
  1851. cmd.push_back(sourceFile);
  1852. }
  1853. // Execute moc command
  1854. cmWorkerPool::ProcessResultT result;
  1855. if (!this->RunProcess(GenT::MOC, result, cmd, this->Reason.get())) {
  1856. // Moc command failed
  1857. std::string includers;
  1858. if (!this->Mapping->IncluderFiles.empty()) {
  1859. includers = "included by\n";
  1860. for (auto const& item : this->Mapping->IncluderFiles) {
  1861. includers += cmStrCat(" ", this->MessagePath(item->FileName), '\n');
  1862. }
  1863. }
  1864. this->LogCommandError(GenT::MOC,
  1865. cmStrCat("The moc process failed to compile\n ",
  1866. this->MessagePath(sourceFile), "\ninto\n ",
  1867. this->MessagePath(outputFile), '\n',
  1868. includers, result.ErrorMessage),
  1869. cmd, result.StdOut);
  1870. return;
  1871. }
  1872. // Moc command success. Print moc output.
  1873. if (!result.StdOut.empty()) {
  1874. this->Log().Info(GenT::MOC, result.StdOut);
  1875. }
  1876. // Extract dependencies from the dep file moc generated for us
  1877. if (this->MocConst().CanOutputDependencies) {
  1878. const std::string depfile = outputFile + ".d";
  1879. if (this->Log().Verbose()) {
  1880. this->Log().Info(
  1881. GenT::MOC, "Reading dependencies from " + this->MessagePath(depfile));
  1882. }
  1883. if (!cmSystemTools::FileExists(depfile)) {
  1884. this->Log().Warning(GenT::MOC,
  1885. "Dependency file " + this->MessagePath(depfile) +
  1886. " does not exist.");
  1887. return;
  1888. }
  1889. this->CacheEntry->Moc.Depends = dependenciesFromDepFile(depfile.c_str());
  1890. }
  1891. }
  1892. void cmQtAutoMocUicT::JobCompileUicT::Process()
  1893. {
  1894. std::string const& sourceFile = this->Mapping->SourceFile->FileName;
  1895. std::string const& outputFile = this->Mapping->OutputFile;
  1896. // Compose uic command
  1897. std::vector<std::string> cmd;
  1898. cmd.push_back(this->UicConst().Executable);
  1899. {
  1900. std::vector<std::string> allOpts = this->UicConst().Options;
  1901. auto optionIt = this->UicConst().UiFiles.find(sourceFile);
  1902. if (optionIt != this->UicConst().UiFiles.end()) {
  1903. UicMergeOptions(allOpts, optionIt->second.Options,
  1904. (this->BaseConst().QtVersion.Major == 5));
  1905. }
  1906. cm::append(cmd, allOpts);
  1907. }
  1908. cmd.emplace_back("-o");
  1909. cmd.emplace_back(outputFile);
  1910. cmd.emplace_back(sourceFile);
  1911. cmWorkerPool::ProcessResultT result;
  1912. if (this->RunProcess(GenT::UIC, result, cmd, this->Reason.get())) {
  1913. // Uic command success
  1914. // Print uic output
  1915. if (!result.StdOut.empty()) {
  1916. this->Log().Info(GenT::UIC, result.StdOut);
  1917. }
  1918. } else {
  1919. // Uic command failed
  1920. std::string includers;
  1921. for (auto const& item : this->Mapping->IncluderFiles) {
  1922. includers += cmStrCat(" ", this->MessagePath(item->FileName), '\n');
  1923. }
  1924. this->LogCommandError(GenT::UIC,
  1925. cmStrCat("The uic process failed to compile\n ",
  1926. this->MessagePath(sourceFile), "\ninto\n ",
  1927. this->MessagePath(outputFile),
  1928. "\nincluded by\n", includers,
  1929. result.ErrorMessage),
  1930. cmd, result.StdOut);
  1931. }
  1932. }
  1933. void cmQtAutoMocUicT::JobMocsCompilationT::Process()
  1934. {
  1935. // Compose mocs compilation file content
  1936. std::string content =
  1937. "// This file is autogenerated. Changes will be overwritten.\n";
  1938. if (this->MocEval().CompFiles.empty()) {
  1939. // Placeholder content
  1940. content += "// No files found that require moc or the moc files are "
  1941. "included\n"
  1942. "enum some_compilers { need_more_than_nothing };\n";
  1943. } else {
  1944. // Valid content
  1945. const bool mc = this->BaseConst().MultiConfig;
  1946. cm::string_view const wrapFront = mc ? "#include <" : "#include \"";
  1947. cm::string_view const wrapBack = mc ? ">\n" : "\"\n";
  1948. content += cmWrap(wrapFront, this->MocEval().CompFiles, wrapBack, "");
  1949. }
  1950. std::string const& compAbs = this->MocConst().CompFileAbs;
  1951. if (cmQtAutoGenerator::FileDiffers(compAbs, content)) {
  1952. // Actually write mocs compilation file
  1953. if (this->Log().Verbose()) {
  1954. this->Log().Info(
  1955. GenT::MOC, "Generating MOC compilation " + this->MessagePath(compAbs));
  1956. }
  1957. if (!FileWrite(compAbs, content)) {
  1958. this->LogError(GenT::MOC,
  1959. cmStrCat("Writing MOC compilation ",
  1960. this->MessagePath(compAbs), " failed."));
  1961. }
  1962. } else if (this->MocEval().CompUpdated) {
  1963. // Only touch mocs compilation file
  1964. if (this->Log().Verbose()) {
  1965. this->Log().Info(
  1966. GenT::MOC, "Touching MOC compilation " + this->MessagePath(compAbs));
  1967. }
  1968. if (!cmSystemTools::Touch(compAbs, false)) {
  1969. this->LogError(GenT::MOC,
  1970. cmStrCat("Touching MOC compilation ",
  1971. this->MessagePath(compAbs), " failed."));
  1972. }
  1973. }
  1974. }
  1975. /*
  1976. * Escapes paths for Ninja depfiles.
  1977. * This is a re-implementation of what moc does when writing depfiles.
  1978. */
  1979. std::string escapeDependencyPath(cm::string_view path)
  1980. {
  1981. std::string escapedPath;
  1982. escapedPath.reserve(path.size());
  1983. const size_t s = path.size();
  1984. int backslashCount = 0;
  1985. for (size_t i = 0; i < s; ++i) {
  1986. if (path[i] == '\\') {
  1987. ++backslashCount;
  1988. } else {
  1989. if (path[i] == '$') {
  1990. escapedPath.push_back('$');
  1991. } else if (path[i] == '#') {
  1992. escapedPath.push_back('\\');
  1993. } else if (path[i] == ' ') {
  1994. // Double the amount of written backslashes,
  1995. // and add one more to escape the space.
  1996. while (backslashCount-- >= 0) {
  1997. escapedPath.push_back('\\');
  1998. }
  1999. }
  2000. backslashCount = 0;
  2001. }
  2002. escapedPath.push_back(path[i]);
  2003. }
  2004. return escapedPath;
  2005. }
  2006. /*
  2007. * Return the initial dependencies of the merged depfile.
  2008. * Those are dependencies from the project files, not from moc runs.
  2009. */
  2010. std::vector<std::string>
  2011. cmQtAutoMocUicT::JobDepFilesMergeT::initialDependencies() const
  2012. {
  2013. std::vector<std::string> dependencies;
  2014. dependencies.reserve(this->BaseConst().ListFiles.size() +
  2015. this->BaseEval().Headers.size() +
  2016. this->BaseEval().Sources.size());
  2017. cm::append(dependencies, this->BaseConst().ListFiles);
  2018. auto append_file_path =
  2019. [&dependencies](const SourceFileMapT::value_type& p) {
  2020. dependencies.push_back(p.first);
  2021. };
  2022. std::for_each(this->BaseEval().Headers.begin(),
  2023. this->BaseEval().Headers.end(), append_file_path);
  2024. std::for_each(this->BaseEval().Sources.begin(),
  2025. this->BaseEval().Sources.end(), append_file_path);
  2026. return dependencies;
  2027. }
  2028. void cmQtAutoMocUicT::JobDepFilesMergeT::Process()
  2029. {
  2030. if (this->Log().Verbose()) {
  2031. this->Log().Info(
  2032. GenT::MOC,
  2033. cmStrCat("Merging MOC dependencies into ",
  2034. this->MessagePath(this->BaseConst().DepFile.c_str())));
  2035. }
  2036. auto processDepFile =
  2037. [](const std::string& mocOutputFile) -> std::vector<std::string> {
  2038. std::string f = mocOutputFile + ".d";
  2039. if (!cmSystemTools::FileExists(f)) {
  2040. return {};
  2041. }
  2042. return dependenciesFromDepFile(f.c_str());
  2043. };
  2044. std::vector<std::string> dependencies = this->initialDependencies();
  2045. ParseCacheT& parseCache = this->BaseEval().ParseCache;
  2046. auto processMappingEntry = [&](const MappingMapT::value_type& m) {
  2047. auto cacheEntry = parseCache.GetOrInsert(m.first);
  2048. if (cacheEntry.first->Moc.Depends.empty()) {
  2049. cacheEntry.first->Moc.Depends = processDepFile(m.second->OutputFile);
  2050. }
  2051. dependencies.insert(dependencies.end(),
  2052. cacheEntry.first->Moc.Depends.begin(),
  2053. cacheEntry.first->Moc.Depends.end());
  2054. };
  2055. std::for_each(this->MocEval().HeaderMappings.begin(),
  2056. this->MocEval().HeaderMappings.end(), processMappingEntry);
  2057. std::for_each(this->MocEval().SourceMappings.begin(),
  2058. this->MocEval().SourceMappings.end(), processMappingEntry);
  2059. // Remove duplicates to make the depfile smaller
  2060. std::sort(dependencies.begin(), dependencies.end());
  2061. dependencies.erase(std::unique(dependencies.begin(), dependencies.end()),
  2062. dependencies.end());
  2063. // Add form files
  2064. for (const auto& uif : this->UicEval().UiFiles) {
  2065. dependencies.push_back(uif.first);
  2066. }
  2067. // Write the file
  2068. cmsys::ofstream ofs;
  2069. ofs.open(this->BaseConst().DepFile.c_str(),
  2070. (std::ios::out | std::ios::binary | std::ios::trunc));
  2071. if (!ofs) {
  2072. this->LogError(GenT::GEN,
  2073. cmStrCat("Cannot open ",
  2074. this->MessagePath(this->BaseConst().DepFile),
  2075. " for writing."));
  2076. return;
  2077. }
  2078. ofs << this->BaseConst().DepFileRuleName << ": \\\n";
  2079. for (const std::string& file : dependencies) {
  2080. ofs << '\t' << escapeDependencyPath(file) << " \\\n";
  2081. if (!ofs.good()) {
  2082. this->LogError(GenT::GEN,
  2083. cmStrCat("Writing depfile",
  2084. this->MessagePath(this->BaseConst().DepFile),
  2085. " failed."));
  2086. return;
  2087. }
  2088. }
  2089. // Add the CMake executable to re-new cache data if necessary.
  2090. // Also, this is the last entry, so don't add a backslash.
  2091. ofs << '\t' << escapeDependencyPath(this->BaseConst().CMakeExecutable)
  2092. << '\n';
  2093. }
  2094. void cmQtAutoMocUicT::JobFinishT::Process()
  2095. {
  2096. this->Gen()->AbortSuccess();
  2097. }
  2098. cmQtAutoMocUicT::cmQtAutoMocUicT()
  2099. : cmQtAutoGenerator(GenT::GEN)
  2100. {
  2101. }
  2102. cmQtAutoMocUicT::~cmQtAutoMocUicT() = default;
  2103. bool cmQtAutoMocUicT::InitFromInfo(InfoT const& info)
  2104. {
  2105. // -- Required settings
  2106. if (!info.GetBool("MULTI_CONFIG", this->BaseConst_.MultiConfig, true) ||
  2107. !info.GetUInt("QT_VERSION_MAJOR", this->BaseConst_.QtVersion.Major,
  2108. true) ||
  2109. !info.GetUInt("QT_VERSION_MINOR", this->BaseConst_.QtVersion.Minor,
  2110. true) ||
  2111. !info.GetUInt("PARALLEL", this->BaseConst_.ThreadCount, false) ||
  2112. !info.GetString("BUILD_DIR", this->BaseConst_.AutogenBuildDir, true) ||
  2113. !info.GetStringConfig("INCLUDE_DIR", this->BaseConst_.AutogenIncludeDir,
  2114. true) ||
  2115. !info.GetString("CMAKE_EXECUTABLE", this->BaseConst_.CMakeExecutable,
  2116. true) ||
  2117. !info.GetStringConfig("PARSE_CACHE_FILE",
  2118. this->BaseConst_.ParseCacheFile, true) ||
  2119. !info.GetString("DEP_FILE", this->BaseConst_.DepFile, false) ||
  2120. !info.GetString("DEP_FILE_RULE_NAME", this->BaseConst_.DepFileRuleName,
  2121. false) ||
  2122. !info.GetStringConfig("SETTINGS_FILE", this->SettingsFile_, true) ||
  2123. !info.GetArray("CMAKE_LIST_FILES", this->BaseConst_.ListFiles, true) ||
  2124. !info.GetArray("HEADER_EXTENSIONS", this->BaseConst_.HeaderExtensions,
  2125. true) ||
  2126. !info.GetString("QT_MOC_EXECUTABLE", this->MocConst_.Executable,
  2127. false) ||
  2128. !info.GetString("QT_UIC_EXECUTABLE", this->UicConst_.Executable,
  2129. false)) {
  2130. return false;
  2131. }
  2132. // -- Checks
  2133. if (!this->BaseConst_.CMakeExecutableTime.Load(
  2134. this->BaseConst_.CMakeExecutable)) {
  2135. return info.LogError(
  2136. cmStrCat("The CMake executable ",
  2137. this->MessagePath(this->BaseConst_.CMakeExecutable),
  2138. " does not exist."));
  2139. }
  2140. // -- Evaluate values
  2141. this->BaseConst_.ThreadCount =
  2142. std::min(this->BaseConst_.ThreadCount, ParallelMax);
  2143. this->WorkerPool_.SetThreadCount(this->BaseConst_.ThreadCount);
  2144. // -- Moc
  2145. if (!this->MocConst_.Executable.empty()) {
  2146. // -- Moc is enabled
  2147. this->MocConst_.Enabled = true;
  2148. // -- Temporary buffers
  2149. struct
  2150. {
  2151. std::vector<std::string> MacroNames;
  2152. std::vector<std::string> DependFilters;
  2153. } tmp;
  2154. // -- Required settings
  2155. if (!info.GetBool("MOC_RELAXED_MODE", this->MocConst_.RelaxedMode,
  2156. false) ||
  2157. !info.GetBool("MOC_PATH_PREFIX", this->MocConst_.PathPrefix, true) ||
  2158. !info.GetArray("MOC_SKIP", this->MocConst_.SkipList, false) ||
  2159. !info.GetArrayConfig("MOC_DEFINITIONS", this->MocConst_.Definitions,
  2160. false) ||
  2161. !info.GetArrayConfig("MOC_INCLUDES", this->MocConst_.IncludePaths,
  2162. false) ||
  2163. !info.GetArray("MOC_OPTIONS", this->MocConst_.OptionsExtra, false) ||
  2164. !info.GetStringConfig("MOC_COMPILATION_FILE",
  2165. this->MocConst_.CompFileAbs, true) ||
  2166. !info.GetArray("MOC_PREDEFS_CMD", this->MocConst_.PredefsCmd, false) ||
  2167. !info.GetStringConfig("MOC_PREDEFS_FILE",
  2168. this->MocConst_.PredefsFileAbs,
  2169. !this->MocConst_.PredefsCmd.empty()) ||
  2170. !info.GetArray("MOC_MACRO_NAMES", tmp.MacroNames, true) ||
  2171. !info.GetArray("MOC_DEPEND_FILTERS", tmp.DependFilters, false)) {
  2172. return false;
  2173. }
  2174. // -- Evaluate settings
  2175. for (std::string const& item : tmp.MacroNames) {
  2176. this->MocConst_.MacroFilters.emplace_back(
  2177. item, ("[\n][ \t]*{?[ \t]*" + item).append("[^a-zA-Z0-9_]"));
  2178. }
  2179. // Can moc output dependencies or do we need to setup dependency filters?
  2180. if (this->BaseConst_.QtVersion >= IntegerVersion(5, 15)) {
  2181. this->MocConst_.CanOutputDependencies = true;
  2182. } else {
  2183. Json::Value const& val = info.GetValue("MOC_DEPEND_FILTERS");
  2184. if (!val.isArray()) {
  2185. return info.LogError("MOC_DEPEND_FILTERS JSON value is not an array.");
  2186. }
  2187. Json::ArrayIndex const arraySize = val.size();
  2188. for (Json::ArrayIndex ii = 0; ii != arraySize; ++ii) {
  2189. // Test entry closure
  2190. auto testEntry = [&info, ii](bool test, cm::string_view msg) -> bool {
  2191. if (!test) {
  2192. info.LogError(
  2193. cmStrCat("MOC_DEPEND_FILTERS filter ", ii, ": ", msg));
  2194. }
  2195. return !test;
  2196. };
  2197. Json::Value const& pairVal = val[ii];
  2198. if (testEntry(pairVal.isArray(), "JSON value is not an array.") ||
  2199. testEntry(pairVal.size() == 2, "JSON array size invalid.")) {
  2200. return false;
  2201. }
  2202. Json::Value const& keyVal = pairVal[0u];
  2203. Json::Value const& expVal = pairVal[1u];
  2204. if (testEntry(keyVal.isString(),
  2205. "JSON value for keyword is not a string.") ||
  2206. testEntry(expVal.isString(),
  2207. "JSON value for regular expression is not a string.")) {
  2208. return false;
  2209. }
  2210. std::string const key = keyVal.asString();
  2211. std::string const exp = expVal.asString();
  2212. if (testEntry(!key.empty(), "Keyword is empty.") ||
  2213. testEntry(!exp.empty(), "Regular expression is empty.")) {
  2214. return false;
  2215. }
  2216. this->MocConst_.DependFilters.emplace_back(key, exp);
  2217. if (testEntry(
  2218. this->MocConst_.DependFilters.back().Exp.is_valid(),
  2219. cmStrCat("Regular expression compilation failed.\nKeyword: ",
  2220. Quoted(key), "\nExpression: ", Quoted(exp)))) {
  2221. return false;
  2222. }
  2223. }
  2224. }
  2225. // Check if moc executable exists (by reading the file time)
  2226. if (!this->MocConst_.ExecutableTime.Load(this->MocConst_.Executable)) {
  2227. return info.LogError(cmStrCat(
  2228. "The moc executable ", this->MessagePath(this->MocConst_.Executable),
  2229. " does not exist."));
  2230. }
  2231. }
  2232. // -- Uic
  2233. if (!this->UicConst_.Executable.empty()) {
  2234. // Uic is enabled
  2235. this->UicConst_.Enabled = true;
  2236. // -- Required settings
  2237. if (!info.GetArray("UIC_SKIP", this->UicConst_.SkipList, false) ||
  2238. !info.GetArray("UIC_SEARCH_PATHS", this->UicConst_.SearchPaths,
  2239. false) ||
  2240. !info.GetArrayConfig("UIC_OPTIONS", this->UicConst_.Options, false)) {
  2241. return false;
  2242. }
  2243. // .ui files
  2244. {
  2245. Json::Value const& val = info.GetValue("UIC_UI_FILES");
  2246. if (!val.isArray()) {
  2247. return info.LogError("UIC_UI_FILES JSON value is not an array.");
  2248. }
  2249. Json::ArrayIndex const arraySize = val.size();
  2250. for (Json::ArrayIndex ii = 0; ii != arraySize; ++ii) {
  2251. // Test entry closure
  2252. auto testEntry = [&info, ii](bool test, cm::string_view msg) -> bool {
  2253. if (!test) {
  2254. info.LogError(cmStrCat("UIC_UI_FILES entry ", ii, ": ", msg));
  2255. }
  2256. return !test;
  2257. };
  2258. Json::Value const& entry = val[ii];
  2259. if (testEntry(entry.isArray(), "JSON value is not an array.") ||
  2260. testEntry(entry.size() == 2, "JSON array size invalid.")) {
  2261. return false;
  2262. }
  2263. Json::Value const& entryName = entry[0u];
  2264. Json::Value const& entryOptions = entry[1u];
  2265. if (testEntry(entryName.isString(),
  2266. "JSON value for name is not a string.") ||
  2267. testEntry(entryOptions.isArray(),
  2268. "JSON value for options is not an array.")) {
  2269. return false;
  2270. }
  2271. auto& uiFile = this->UicConst_.UiFiles[entryName.asString()];
  2272. InfoT::GetJsonArray(uiFile.Options, entryOptions);
  2273. }
  2274. }
  2275. // -- Evaluate settings
  2276. // Check if uic executable exists (by reading the file time)
  2277. if (!this->UicConst_.ExecutableTime.Load(this->UicConst_.Executable)) {
  2278. return info.LogError(cmStrCat(
  2279. "The uic executable ", this->MessagePath(this->UicConst_.Executable),
  2280. " does not exist."));
  2281. }
  2282. }
  2283. // -- Headers
  2284. {
  2285. Json::Value const& val = info.GetValue("HEADERS");
  2286. if (!val.isArray()) {
  2287. return info.LogError("HEADERS JSON value is not an array.");
  2288. }
  2289. Json::ArrayIndex const arraySize = val.size();
  2290. for (Json::ArrayIndex ii = 0; ii != arraySize; ++ii) {
  2291. // Test entry closure
  2292. auto testEntry = [&info, ii](bool test, cm::string_view msg) -> bool {
  2293. if (!test) {
  2294. info.LogError(cmStrCat("HEADERS entry ", ii, ": ", msg));
  2295. }
  2296. return !test;
  2297. };
  2298. Json::Value const& entry = val[ii];
  2299. if (testEntry(entry.isArray(), "JSON value is not an array.") ||
  2300. testEntry(entry.size() == 4, "JSON array size invalid.")) {
  2301. return false;
  2302. }
  2303. Json::Value const& entryName = entry[0u];
  2304. Json::Value const& entryFlags = entry[1u];
  2305. Json::Value const& entryConfigs = entry[2u];
  2306. Json::Value const& entryBuild = entry[3u];
  2307. if (testEntry(entryName.isString(),
  2308. "JSON value for name is not a string.") ||
  2309. testEntry(entryFlags.isString(),
  2310. "JSON value for flags is not a string.") ||
  2311. testEntry(entryConfigs.isNull() || entryConfigs.isArray(),
  2312. "JSON value for configs is not null or array.") ||
  2313. testEntry(entryBuild.isString(),
  2314. "JSON value for build path is not a string.")) {
  2315. return false;
  2316. }
  2317. std::string name = entryName.asString();
  2318. std::string flags = entryFlags.asString();
  2319. std::string build = entryBuild.asString();
  2320. if (testEntry(flags.size() == 2, "Invalid flags string size")) {
  2321. return false;
  2322. }
  2323. if (entryConfigs.isArray()) {
  2324. bool configFound = false;
  2325. Json::ArrayIndex const configArraySize = entryConfigs.size();
  2326. for (Json::ArrayIndex ci = 0; ci != configArraySize; ++ci) {
  2327. Json::Value const& config = entryConfigs[ci];
  2328. if (testEntry(config.isString(),
  2329. "JSON value in config array is not a string.")) {
  2330. return false;
  2331. }
  2332. configFound = configFound || config.asString() == this->InfoConfig();
  2333. }
  2334. if (!configFound) {
  2335. continue;
  2336. }
  2337. }
  2338. cmFileTime fileTime;
  2339. if (!fileTime.Load(name)) {
  2340. return info.LogError(cmStrCat(
  2341. "The header file ", this->MessagePath(name), " does not exist."));
  2342. }
  2343. SourceFileHandleT sourceHandle = std::make_shared<SourceFileT>(name);
  2344. sourceHandle->FileTime = fileTime;
  2345. sourceHandle->IsHeader = true;
  2346. sourceHandle->Moc = (flags[0] == 'M');
  2347. sourceHandle->Uic = (flags[1] == 'U');
  2348. if (sourceHandle->Moc && this->MocConst().Enabled) {
  2349. if (build.empty()) {
  2350. return info.LogError(
  2351. cmStrCat("Header file ", ii, " build path is empty"));
  2352. }
  2353. sourceHandle->BuildPath = std::move(build);
  2354. }
  2355. this->BaseEval().Headers.emplace(std::move(name),
  2356. std::move(sourceHandle));
  2357. }
  2358. }
  2359. // -- Sources
  2360. {
  2361. Json::Value const& val = info.GetValue("SOURCES");
  2362. if (!val.isArray()) {
  2363. return info.LogError("SOURCES JSON value is not an array.");
  2364. }
  2365. Json::ArrayIndex const arraySize = val.size();
  2366. for (Json::ArrayIndex ii = 0; ii != arraySize; ++ii) {
  2367. // Test entry closure
  2368. auto testEntry = [&info, ii](bool test, cm::string_view msg) -> bool {
  2369. if (!test) {
  2370. info.LogError(cmStrCat("SOURCES entry ", ii, ": ", msg));
  2371. }
  2372. return !test;
  2373. };
  2374. Json::Value const& entry = val[ii];
  2375. if (testEntry(entry.isArray(), "JSON value is not an array.") ||
  2376. testEntry(entry.size() == 3, "JSON array size invalid.")) {
  2377. return false;
  2378. }
  2379. Json::Value const& entryName = entry[0u];
  2380. Json::Value const& entryFlags = entry[1u];
  2381. Json::Value const& entryConfigs = entry[2u];
  2382. if (testEntry(entryName.isString(),
  2383. "JSON value for name is not a string.") ||
  2384. testEntry(entryFlags.isString(),
  2385. "JSON value for flags is not a string.") ||
  2386. testEntry(entryConfigs.isNull() || entryConfigs.isArray(),
  2387. "JSON value for configs is not null or array.")) {
  2388. return false;
  2389. }
  2390. std::string name = entryName.asString();
  2391. std::string flags = entryFlags.asString();
  2392. if (testEntry(flags.size() == 2, "Invalid flags string size")) {
  2393. return false;
  2394. }
  2395. if (entryConfigs.isArray()) {
  2396. bool configFound = false;
  2397. Json::ArrayIndex const configArraySize = entryConfigs.size();
  2398. for (Json::ArrayIndex ci = 0; ci != configArraySize; ++ci) {
  2399. Json::Value const& config = entryConfigs[ci];
  2400. if (testEntry(config.isString(),
  2401. "JSON value in config array is not a string.")) {
  2402. return false;
  2403. }
  2404. configFound = configFound || config.asString() == this->InfoConfig();
  2405. }
  2406. if (!configFound) {
  2407. continue;
  2408. }
  2409. }
  2410. cmFileTime fileTime;
  2411. if (!fileTime.Load(name)) {
  2412. return info.LogError(cmStrCat(
  2413. "The source file ", this->MessagePath(name), " does not exist."));
  2414. }
  2415. SourceFileHandleT sourceHandle = std::make_shared<SourceFileT>(name);
  2416. sourceHandle->FileTime = fileTime;
  2417. sourceHandle->IsHeader = false;
  2418. sourceHandle->Moc = (flags[0] == 'M');
  2419. sourceHandle->Uic = (flags[1] == 'U');
  2420. this->BaseEval().Sources.emplace(std::move(name),
  2421. std::move(sourceHandle));
  2422. }
  2423. }
  2424. // -- Init derived information
  2425. // Moc variables
  2426. if (this->MocConst().Enabled) {
  2427. // Compose moc includes list
  2428. {
  2429. // Compute framework paths
  2430. std::set<std::string> frameworkPaths;
  2431. for (std::string const& path : this->MocConst().IncludePaths) {
  2432. // Extract framework path
  2433. if (cmHasLiteralSuffix(path, ".framework/Headers")) {
  2434. // Go up twice to get to the framework root
  2435. std::vector<std::string> pathComponents;
  2436. cmSystemTools::SplitPath(path, pathComponents);
  2437. frameworkPaths.emplace(cmSystemTools::JoinPath(
  2438. pathComponents.begin(), pathComponents.end() - 2));
  2439. }
  2440. }
  2441. // Reserve options
  2442. this->MocConst_.OptionsIncludes.reserve(
  2443. this->MocConst().IncludePaths.size() + frameworkPaths.size() * 2);
  2444. // Append includes
  2445. for (std::string const& path : this->MocConst().IncludePaths) {
  2446. this->MocConst_.OptionsIncludes.emplace_back("-I" + path);
  2447. }
  2448. // Append framework includes
  2449. for (std::string const& path : frameworkPaths) {
  2450. this->MocConst_.OptionsIncludes.emplace_back("-F");
  2451. this->MocConst_.OptionsIncludes.push_back(path);
  2452. }
  2453. }
  2454. // Compose moc definitions list
  2455. {
  2456. this->MocConst_.OptionsDefinitions.reserve(
  2457. this->MocConst().Definitions.size());
  2458. for (std::string const& def : this->MocConst().Definitions) {
  2459. this->MocConst_.OptionsDefinitions.emplace_back("-D" + def);
  2460. }
  2461. }
  2462. }
  2463. return true;
  2464. }
  2465. template <class JOBTYPE>
  2466. void cmQtAutoMocUicT::CreateParseJobs(SourceFileMapT const& sourceMap)
  2467. {
  2468. cmFileTime const parseCacheTime = this->BaseEval().ParseCacheTime;
  2469. ParseCacheT& parseCache = this->BaseEval().ParseCache;
  2470. for (auto& src : sourceMap) {
  2471. // Get or create the file parse data reference
  2472. ParseCacheT::GetOrInsertT cacheEntry = parseCache.GetOrInsert(src.first);
  2473. src.second->ParseData = std::move(cacheEntry.first);
  2474. // Create a parse job if the cache file was missing or is older
  2475. if (cacheEntry.second || src.second->FileTime.Newer(parseCacheTime)) {
  2476. this->BaseEval().ParseCacheChanged = true;
  2477. this->WorkerPool().EmplaceJob<JOBTYPE>(src.second);
  2478. }
  2479. }
  2480. }
  2481. /** Concurrently callable implementation of cmSystemTools::CollapseFullPath */
  2482. std::string cmQtAutoMocUicT::CollapseFullPathTS(std::string const& path) const
  2483. {
  2484. std::lock_guard<std::mutex> guard(this->CMakeLibMutex_);
  2485. return cmSystemTools::CollapseFullPath(path,
  2486. this->ProjectDirs().CurrentSource);
  2487. }
  2488. void cmQtAutoMocUicT::InitJobs()
  2489. {
  2490. // Add moc_predefs.h job
  2491. if (this->MocConst().Enabled && !this->MocConst().PredefsCmd.empty()) {
  2492. this->WorkerPool().EmplaceJob<JobMocPredefsT>();
  2493. }
  2494. // Add header parse jobs
  2495. this->CreateParseJobs<JobParseHeaderT>(this->BaseEval().Headers);
  2496. // Add source parse jobs
  2497. this->CreateParseJobs<JobParseSourceT>(this->BaseEval().Sources);
  2498. // Add parse cache evaluations jobs
  2499. {
  2500. // Add a fence job to ensure all parsing has finished
  2501. this->WorkerPool().EmplaceJob<JobFenceT>();
  2502. if (this->MocConst().Enabled) {
  2503. this->WorkerPool().EmplaceJob<JobEvalCacheMocT>();
  2504. }
  2505. if (this->UicConst().Enabled) {
  2506. this->WorkerPool().EmplaceJob<JobEvalCacheUicT>();
  2507. }
  2508. // Add evaluate job
  2509. this->WorkerPool().EmplaceJob<JobEvalCacheFinishT>();
  2510. }
  2511. }
  2512. bool cmQtAutoMocUicT::Process()
  2513. {
  2514. this->SettingsFileRead();
  2515. this->ParseCacheRead();
  2516. if (!this->CreateDirectories()) {
  2517. return false;
  2518. }
  2519. this->InitJobs();
  2520. if (!this->WorkerPool_.Process(this)) {
  2521. return false;
  2522. }
  2523. if (this->JobError_) {
  2524. return false;
  2525. }
  2526. if (!this->ParseCacheWrite()) {
  2527. return false;
  2528. }
  2529. if (!this->SettingsFileWrite()) {
  2530. return false;
  2531. }
  2532. return true;
  2533. }
  2534. void cmQtAutoMocUicT::SettingsFileRead()
  2535. {
  2536. // Compose current settings strings
  2537. {
  2538. cmCryptoHash cryptoHash(cmCryptoHash::AlgoSHA256);
  2539. auto cha = [&cryptoHash](cm::string_view value) {
  2540. cryptoHash.Append(value);
  2541. cryptoHash.Append(";");
  2542. };
  2543. if (this->MocConst_.Enabled) {
  2544. cryptoHash.Initialize();
  2545. cha(this->MocConst().Executable);
  2546. for (auto const& item : this->MocConst().OptionsDefinitions) {
  2547. cha(item);
  2548. }
  2549. for (auto const& item : this->MocConst().OptionsIncludes) {
  2550. cha(item);
  2551. }
  2552. for (auto const& item : this->MocConst().OptionsExtra) {
  2553. cha(item);
  2554. }
  2555. for (auto const& item : this->MocConst().PredefsCmd) {
  2556. cha(item);
  2557. }
  2558. for (auto const& filter : this->MocConst().DependFilters) {
  2559. cha(filter.Key);
  2560. }
  2561. for (auto const& filter : this->MocConst().MacroFilters) {
  2562. cha(filter.Key);
  2563. }
  2564. this->SettingsStringMoc_ = cryptoHash.FinalizeHex();
  2565. }
  2566. if (this->UicConst().Enabled) {
  2567. cryptoHash.Initialize();
  2568. cha(this->UicConst().Executable);
  2569. std::for_each(this->UicConst().Options.begin(),
  2570. this->UicConst().Options.end(), cha);
  2571. for (const auto& item : this->UicConst().UiFiles) {
  2572. cha(item.first);
  2573. auto const& opts = item.second.Options;
  2574. std::for_each(opts.begin(), opts.end(), cha);
  2575. }
  2576. this->SettingsStringUic_ = cryptoHash.FinalizeHex();
  2577. }
  2578. }
  2579. // Read old settings and compare
  2580. {
  2581. std::string content;
  2582. if (cmQtAutoGenerator::FileRead(content, this->SettingsFile_)) {
  2583. if (this->MocConst().Enabled) {
  2584. if (this->SettingsStringMoc_ != SettingsFind(content, "moc")) {
  2585. this->MocConst_.SettingsChanged = true;
  2586. }
  2587. }
  2588. if (this->UicConst().Enabled) {
  2589. if (this->SettingsStringUic_ != SettingsFind(content, "uic")) {
  2590. this->UicConst_.SettingsChanged = true;
  2591. }
  2592. }
  2593. // In case any setting changed remove the old settings file.
  2594. // This triggers a full rebuild on the next run if the current
  2595. // build is aborted before writing the current settings in the end.
  2596. if (this->MocConst().SettingsChanged ||
  2597. this->UicConst().SettingsChanged) {
  2598. cmSystemTools::RemoveFile(this->SettingsFile_);
  2599. }
  2600. } else {
  2601. // Settings file read failed
  2602. if (this->MocConst().Enabled) {
  2603. this->MocConst_.SettingsChanged = true;
  2604. }
  2605. if (this->UicConst().Enabled) {
  2606. this->UicConst_.SettingsChanged = true;
  2607. }
  2608. }
  2609. }
  2610. }
  2611. bool cmQtAutoMocUicT::SettingsFileWrite()
  2612. {
  2613. // Only write if any setting changed
  2614. if (this->MocConst().SettingsChanged || this->UicConst().SettingsChanged) {
  2615. if (this->Log().Verbose()) {
  2616. this->Log().Info(GenT::GEN,
  2617. cmStrCat("Writing the settings file ",
  2618. this->MessagePath(this->SettingsFile_)));
  2619. }
  2620. // Compose settings file content
  2621. std::string content;
  2622. {
  2623. auto SettingAppend = [&content](cm::string_view key,
  2624. cm::string_view value) {
  2625. if (!value.empty()) {
  2626. content += cmStrCat(key, ':', value, '\n');
  2627. }
  2628. };
  2629. SettingAppend("moc", this->SettingsStringMoc_);
  2630. SettingAppend("uic", this->SettingsStringUic_);
  2631. }
  2632. // Write settings file
  2633. std::string error;
  2634. if (!cmQtAutoGenerator::FileWrite(this->SettingsFile_, content, &error)) {
  2635. this->Log().Error(GenT::GEN,
  2636. cmStrCat("Writing the settings file ",
  2637. this->MessagePath(this->SettingsFile_),
  2638. " failed.\n", error));
  2639. // Remove old settings file to trigger a full rebuild on the next run
  2640. cmSystemTools::RemoveFile(this->SettingsFile_);
  2641. return false;
  2642. }
  2643. }
  2644. return true;
  2645. }
  2646. void cmQtAutoMocUicT::ParseCacheRead()
  2647. {
  2648. cm::string_view reason;
  2649. // Don't read the cache if it is invalid
  2650. if (!this->BaseEval().ParseCacheTime.Load(
  2651. this->BaseConst().ParseCacheFile)) {
  2652. reason = "Refreshing parse cache because it doesn't exist.";
  2653. } else if (this->MocConst().SettingsChanged ||
  2654. this->UicConst().SettingsChanged) {
  2655. reason = "Refreshing parse cache because the settings changed.";
  2656. } else if (this->BaseEval().ParseCacheTime.Older(
  2657. this->BaseConst().CMakeExecutableTime)) {
  2658. reason =
  2659. "Refreshing parse cache because it is older than the CMake executable.";
  2660. }
  2661. if (!reason.empty()) {
  2662. // Don't read but refresh the complete parse cache
  2663. if (this->Log().Verbose()) {
  2664. this->Log().Info(GenT::GEN, reason);
  2665. }
  2666. this->BaseEval().ParseCacheChanged = true;
  2667. } else {
  2668. // Read parse cache
  2669. this->BaseEval().ParseCache.ReadFromFile(this->BaseConst().ParseCacheFile);
  2670. }
  2671. }
  2672. bool cmQtAutoMocUicT::ParseCacheWrite()
  2673. {
  2674. if (this->BaseEval().ParseCacheChanged) {
  2675. if (this->Log().Verbose()) {
  2676. this->Log().Info(
  2677. GenT::GEN,
  2678. cmStrCat("Writing the parse cache file ",
  2679. this->MessagePath(this->BaseConst().ParseCacheFile)));
  2680. }
  2681. if (!this->BaseEval().ParseCache.WriteToFile(
  2682. this->BaseConst().ParseCacheFile)) {
  2683. this->Log().Error(
  2684. GenT::GEN,
  2685. cmStrCat("Writing the parse cache file ",
  2686. this->MessagePath(this->BaseConst().ParseCacheFile),
  2687. " failed."));
  2688. return false;
  2689. }
  2690. }
  2691. return true;
  2692. }
  2693. bool cmQtAutoMocUicT::CreateDirectories()
  2694. {
  2695. // Create AUTOGEN include directory
  2696. if (!cmSystemTools::MakeDirectory(this->BaseConst().AutogenIncludeDir)) {
  2697. this->Log().Error(
  2698. GenT::GEN,
  2699. cmStrCat("Creating the AUTOGEN include directory ",
  2700. this->MessagePath(this->BaseConst().AutogenIncludeDir),
  2701. " failed."));
  2702. return false;
  2703. }
  2704. return true;
  2705. }
  2706. std::vector<std::string> cmQtAutoMocUicT::dependenciesFromDepFile(
  2707. const char* filePath)
  2708. {
  2709. auto const content = cmReadGccDepfile(filePath);
  2710. if (!content || content->empty()) {
  2711. return {};
  2712. }
  2713. // Moc outputs a depfile with exactly one rule.
  2714. // Discard the rule and return the dependencies.
  2715. return content->front().paths;
  2716. }
  2717. void cmQtAutoMocUicT::Abort(bool error)
  2718. {
  2719. if (error) {
  2720. this->JobError_.store(true);
  2721. }
  2722. this->WorkerPool_.Abort();
  2723. }
  2724. std::string cmQtAutoMocUicT::AbsoluteBuildPath(
  2725. cm::string_view relativePath) const
  2726. {
  2727. return cmStrCat(this->BaseConst().AutogenBuildDir, '/', relativePath);
  2728. }
  2729. std::string cmQtAutoMocUicT::AbsoluteIncludePath(
  2730. cm::string_view relativePath) const
  2731. {
  2732. return cmStrCat(this->BaseConst().AutogenIncludeDir, '/', relativePath);
  2733. }
  2734. } // End of unnamed namespace
  2735. bool cmQtAutoMocUic(cm::string_view infoFile, cm::string_view config)
  2736. {
  2737. return cmQtAutoMocUicT().Run(infoFile, config);
  2738. }