cmQtAutoMocUic.cxx 94 KB

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