cmQtAutoMocUic.cxx 87 KB

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