cmQtAutoMocUic.cxx 94 KB

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