cmQtAutoMocUic.cxx 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014
  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. std::vector<std::string> dependenciesFromDepFile(const char* filePath);
  493. // -- Settings
  494. BaseSettingsT BaseConst_;
  495. BaseEvalT BaseEval_;
  496. MocSettingsT MocConst_;
  497. MocEvalT MocEval_;
  498. UicSettingsT UicConst_;
  499. UicEvalT UicEval_;
  500. // -- Settings file
  501. std::string SettingsFile_;
  502. std::string SettingsStringMoc_;
  503. std::string SettingsStringUic_;
  504. // -- Worker thread pool
  505. std::atomic<bool> JobError_ = ATOMIC_VAR_INIT(false);
  506. cmWorkerPool WorkerPool_;
  507. // -- Concurrent processing
  508. mutable std::mutex CMakeLibMutex_;
  509. };
  510. cmQtAutoMocUicT::IncludeKeyT::IncludeKeyT(std::string const& key,
  511. std::size_t basePrefixLength)
  512. : Key(key)
  513. , Dir(SubDirPrefix(key))
  514. , Base(cmSystemTools::GetFilenameWithoutLastExtension(key))
  515. {
  516. if (basePrefixLength != 0) {
  517. this->Base = this->Base.substr(basePrefixLength);
  518. }
  519. }
  520. void cmQtAutoMocUicT::ParseCacheT::FileT::Clear()
  521. {
  522. this->Moc.Macro.clear();
  523. this->Moc.Include.Underscore.clear();
  524. this->Moc.Include.Dot.clear();
  525. this->Moc.Depends.clear();
  526. this->Uic.Include.clear();
  527. this->Uic.Depends.clear();
  528. }
  529. cmQtAutoMocUicT::ParseCacheT::GetOrInsertT
  530. cmQtAutoMocUicT::ParseCacheT::GetOrInsert(std::string const& fileName)
  531. {
  532. // Find existing entry
  533. {
  534. auto it = this->Map_.find(fileName);
  535. if (it != this->Map_.end()) {
  536. return GetOrInsertT{ it->second, false };
  537. }
  538. }
  539. // Insert new entry
  540. return GetOrInsertT{
  541. this->Map_.emplace(fileName, std::make_shared<FileT>()).first->second, true
  542. };
  543. }
  544. cmQtAutoMocUicT::ParseCacheT::ParseCacheT() = default;
  545. cmQtAutoMocUicT::ParseCacheT::~ParseCacheT() = default;
  546. bool cmQtAutoMocUicT::ParseCacheT::ReadFromFile(std::string const& fileName)
  547. {
  548. cmsys::ifstream fin(fileName.c_str());
  549. if (!fin) {
  550. return false;
  551. }
  552. FileHandleT fileHandle;
  553. std::string line;
  554. while (std::getline(fin, line)) {
  555. // Check if this an empty or a comment line
  556. if (line.empty() || line.front() == '#') {
  557. continue;
  558. }
  559. // Drop carriage return character at the end
  560. if (line.back() == '\r') {
  561. line.pop_back();
  562. if (line.empty()) {
  563. continue;
  564. }
  565. }
  566. // Check if this a file name line
  567. if (line.front() != ' ') {
  568. fileHandle = this->GetOrInsert(line).first;
  569. continue;
  570. }
  571. // Bad line or bad file handle
  572. if (!fileHandle || (line.size() < 6)) {
  573. continue;
  574. }
  575. constexpr std::size_t offset = 5;
  576. if (cmHasLiteralPrefix(line, " mmc:")) {
  577. fileHandle->Moc.Macro = line.substr(offset);
  578. continue;
  579. }
  580. if (cmHasLiteralPrefix(line, " miu:")) {
  581. fileHandle->Moc.Include.Underscore.emplace_back(line.substr(offset),
  582. MocUnderscoreLength);
  583. continue;
  584. }
  585. if (cmHasLiteralPrefix(line, " mid:")) {
  586. fileHandle->Moc.Include.Dot.emplace_back(line.substr(offset), 0);
  587. continue;
  588. }
  589. if (cmHasLiteralPrefix(line, " mdp:")) {
  590. fileHandle->Moc.Depends.emplace_back(line.substr(offset));
  591. continue;
  592. }
  593. if (cmHasLiteralPrefix(line, " uic:")) {
  594. fileHandle->Uic.Include.emplace_back(line.substr(offset),
  595. UiUnderscoreLength);
  596. continue;
  597. }
  598. if (cmHasLiteralPrefix(line, " udp:")) {
  599. fileHandle->Uic.Depends.emplace_back(line.substr(offset));
  600. continue;
  601. }
  602. }
  603. return true;
  604. }
  605. bool cmQtAutoMocUicT::ParseCacheT::WriteToFile(std::string const& fileName)
  606. {
  607. cmGeneratedFileStream ofs(fileName);
  608. if (!ofs) {
  609. return false;
  610. }
  611. ofs << "# Generated by CMake. Changes will be overwritten.\n";
  612. for (auto const& pair : this->Map_) {
  613. ofs << pair.first << '\n';
  614. FileT const& file = *pair.second;
  615. if (!file.Moc.Macro.empty()) {
  616. ofs << " mmc:" << file.Moc.Macro << '\n';
  617. }
  618. for (IncludeKeyT const& item : file.Moc.Include.Underscore) {
  619. ofs << " miu:" << item.Key << '\n';
  620. }
  621. for (IncludeKeyT const& item : file.Moc.Include.Dot) {
  622. ofs << " mid:" << item.Key << '\n';
  623. }
  624. for (std::string const& item : file.Moc.Depends) {
  625. ofs << " mdp:" << item << '\n';
  626. }
  627. for (IncludeKeyT const& item : file.Uic.Include) {
  628. ofs << " uic:" << item.Key << '\n';
  629. }
  630. for (std::string const& item : file.Uic.Depends) {
  631. ofs << " udp:" << item << '\n';
  632. }
  633. }
  634. return ofs.Close();
  635. }
  636. cmQtAutoMocUicT::BaseSettingsT::BaseSettingsT() = default;
  637. cmQtAutoMocUicT::BaseSettingsT::~BaseSettingsT() = default;
  638. cmQtAutoMocUicT::MocSettingsT::MocSettingsT()
  639. {
  640. this->RegExpInclude.compile(
  641. "(^|\n)[ \t]*#[ \t]*include[ \t]+"
  642. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  643. }
  644. cmQtAutoMocUicT::MocSettingsT::~MocSettingsT() = default;
  645. bool cmQtAutoMocUicT::MocSettingsT::skipped(std::string const& fileName) const
  646. {
  647. return (!this->Enabled ||
  648. (this->SkipList.find(fileName) != this->SkipList.end()));
  649. }
  650. std::string cmQtAutoMocUicT::MocSettingsT::MacrosString() const
  651. {
  652. std::string res;
  653. const auto itB = this->MacroFilters.cbegin();
  654. const auto itE = this->MacroFilters.cend();
  655. const auto itL = itE - 1;
  656. auto itC = itB;
  657. for (; itC != itE; ++itC) {
  658. // Separator
  659. if (itC != itB) {
  660. if (itC != itL) {
  661. res += ", ";
  662. } else {
  663. res += " or ";
  664. }
  665. }
  666. // Key
  667. res += itC->Key;
  668. }
  669. return res;
  670. }
  671. cmQtAutoMocUicT::UicSettingsT::UicSettingsT()
  672. {
  673. this->RegExpInclude.compile("(^|\n)[ \t]*#[ \t]*include[ \t]+"
  674. "[\"<](([^ \">]+/)?ui_[^ \">/]+\\.h)[\">]");
  675. }
  676. cmQtAutoMocUicT::UicSettingsT::~UicSettingsT() = default;
  677. bool cmQtAutoMocUicT::UicSettingsT::skipped(std::string const& fileName) const
  678. {
  679. return (!this->Enabled ||
  680. (this->SkipList.find(fileName) != this->SkipList.end()));
  681. }
  682. void cmQtAutoMocUicT::JobT::LogError(GenT genType,
  683. cm::string_view message) const
  684. {
  685. this->Gen()->AbortError();
  686. this->Gen()->Log().Error(genType, message);
  687. }
  688. void cmQtAutoMocUicT::JobT::LogCommandError(
  689. GenT genType, cm::string_view message,
  690. std::vector<std::string> const& command, std::string const& output) const
  691. {
  692. this->Gen()->AbortError();
  693. this->Gen()->Log().ErrorCommand(genType, message, command, output);
  694. }
  695. bool cmQtAutoMocUicT::JobT::RunProcess(GenT genType,
  696. cmWorkerPool::ProcessResultT& result,
  697. std::vector<std::string> const& command,
  698. std::string* infoMessage)
  699. {
  700. // Log command
  701. if (this->Log().Verbose()) {
  702. cm::string_view info;
  703. if (infoMessage != nullptr) {
  704. info = *infoMessage;
  705. }
  706. this->Log().Info(
  707. genType,
  708. cmStrCat(info, info.empty() || cmHasSuffix(info, '\n') ? "" : "\n",
  709. QuotedCommand(command), '\n'));
  710. }
  711. // Run command
  712. return this->cmWorkerPool::JobT::RunProcess(
  713. result, command, this->BaseConst().AutogenBuildDir);
  714. }
  715. void cmQtAutoMocUicT::JobMocPredefsT::Process()
  716. {
  717. // (Re)generate moc_predefs.h on demand
  718. std::unique_ptr<std::string> reason;
  719. if (this->Log().Verbose()) {
  720. reason = cm::make_unique<std::string>();
  721. }
  722. if (!this->Update(reason.get())) {
  723. return;
  724. }
  725. std::string const& predefsFileAbs = this->MocConst().PredefsFileAbs;
  726. {
  727. cmWorkerPool::ProcessResultT result;
  728. {
  729. // Compose command
  730. std::vector<std::string> cmd = this->MocConst().PredefsCmd;
  731. // Add definitions
  732. cm::append(cmd, this->MocConst().OptionsDefinitions);
  733. // Add includes
  734. cm::append(cmd, this->MocConst().OptionsIncludes);
  735. // Execute command
  736. if (!this->RunProcess(GenT::MOC, result, cmd, reason.get())) {
  737. this->LogCommandError(GenT::MOC,
  738. cmStrCat("The content generation command for ",
  739. this->MessagePath(predefsFileAbs),
  740. " failed.\n", result.ErrorMessage),
  741. cmd, result.StdOut);
  742. return;
  743. }
  744. }
  745. // (Re)write predefs file only on demand
  746. if (cmQtAutoGenerator::FileDiffers(predefsFileAbs, result.StdOut)) {
  747. if (!cmQtAutoGenerator::FileWrite(predefsFileAbs, result.StdOut)) {
  748. this->LogError(
  749. GenT::MOC,
  750. cmStrCat("Writing ", this->MessagePath(predefsFileAbs), " failed."));
  751. return;
  752. }
  753. } else {
  754. // Touch to update the time stamp
  755. if (this->Log().Verbose()) {
  756. this->Log().Info(GenT::MOC,
  757. "Touching " + this->MessagePath(predefsFileAbs));
  758. }
  759. if (!cmSystemTools::Touch(predefsFileAbs, false)) {
  760. this->LogError(GenT::MOC,
  761. cmStrCat("Touching ", this->MessagePath(predefsFileAbs),
  762. " failed."));
  763. return;
  764. }
  765. }
  766. }
  767. // Read file time afterwards
  768. if (!this->MocEval().PredefsTime.Load(predefsFileAbs)) {
  769. this->LogError(GenT::MOC,
  770. cmStrCat("Reading the file time of ",
  771. this->MessagePath(predefsFileAbs), " failed."));
  772. return;
  773. }
  774. }
  775. bool cmQtAutoMocUicT::JobMocPredefsT::Update(std::string* reason) const
  776. {
  777. // Test if the file exists
  778. if (!this->MocEval().PredefsTime.Load(this->MocConst().PredefsFileAbs)) {
  779. if (reason != nullptr) {
  780. *reason = cmStrCat("Generating ",
  781. this->MessagePath(this->MocConst().PredefsFileAbs),
  782. ", because it doesn't exist.");
  783. }
  784. return true;
  785. }
  786. // Test if the settings changed
  787. if (this->MocConst().SettingsChanged) {
  788. if (reason != nullptr) {
  789. *reason = cmStrCat("Generating ",
  790. this->MessagePath(this->MocConst().PredefsFileAbs),
  791. ", because the moc settings changed.");
  792. }
  793. return true;
  794. }
  795. // Test if the executable is newer
  796. {
  797. std::string const& exec = this->MocConst().PredefsCmd.at(0);
  798. cmFileTime execTime;
  799. if (execTime.Load(exec)) {
  800. if (this->MocEval().PredefsTime.Older(execTime)) {
  801. if (reason != nullptr) {
  802. *reason = cmStrCat(
  803. "Generating ", this->MessagePath(this->MocConst().PredefsFileAbs),
  804. " because it is older than ", this->MessagePath(exec), '.');
  805. }
  806. return true;
  807. }
  808. }
  809. }
  810. return false;
  811. }
  812. bool cmQtAutoMocUicT::JobParseT::ReadFile()
  813. {
  814. // Clear old parse information
  815. this->FileHandle->ParseData->Clear();
  816. std::string const& fileName = this->FileHandle->FileName;
  817. // Write info
  818. if (this->Log().Verbose()) {
  819. this->Log().Info(GenT::GEN,
  820. cmStrCat("Parsing ", this->MessagePath(fileName)));
  821. }
  822. // Read file content
  823. {
  824. std::string error;
  825. if (!cmQtAutoGenerator::FileRead(this->Content, fileName, &error)) {
  826. this->LogError(GenT::GEN,
  827. cmStrCat("Could not read ", this->MessagePath(fileName),
  828. ".\n", error));
  829. return false;
  830. }
  831. }
  832. // Warn if empty
  833. if (this->Content.empty()) {
  834. this->Log().Warning(GenT::GEN,
  835. cmStrCat(this->MessagePath(fileName), " is empty."));
  836. return false;
  837. }
  838. return true;
  839. }
  840. void cmQtAutoMocUicT::JobParseT::CreateKeys(
  841. std::vector<IncludeKeyT>& container, std::set<std::string> const& source,
  842. std::size_t basePrefixLength)
  843. {
  844. if (source.empty()) {
  845. return;
  846. }
  847. container.reserve(source.size());
  848. for (std::string const& src : source) {
  849. container.emplace_back(src, basePrefixLength);
  850. }
  851. }
  852. void cmQtAutoMocUicT::JobParseT::MocMacro()
  853. {
  854. for (KeyExpT const& filter : this->MocConst().MacroFilters) {
  855. // Run a simple find string check
  856. if (this->Content.find(filter.Key) == std::string::npos) {
  857. continue;
  858. }
  859. // Run the expensive regular expression check loop
  860. cmsys::RegularExpressionMatch match;
  861. if (filter.Exp.find(this->Content.c_str(), match)) {
  862. // Keep detected macro name
  863. this->FileHandle->ParseData->Moc.Macro = filter.Key;
  864. return;
  865. }
  866. }
  867. }
  868. void cmQtAutoMocUicT::JobParseT::MocDependecies()
  869. {
  870. if (this->MocConst().DependFilters.empty() ||
  871. this->MocConst().CanOutputDependencies) {
  872. return;
  873. }
  874. // Find dependency strings
  875. std::set<std::string> parseDepends;
  876. for (KeyExpT const& filter : this->MocConst().DependFilters) {
  877. // Run a simple find string check
  878. if (this->Content.find(filter.Key) == std::string::npos) {
  879. continue;
  880. }
  881. // Run the expensive regular expression check loop
  882. const char* contentChars = this->Content.c_str();
  883. cmsys::RegularExpressionMatch match;
  884. while (filter.Exp.find(contentChars, match)) {
  885. {
  886. std::string dep = match.match(1);
  887. if (!dep.empty()) {
  888. parseDepends.emplace(std::move(dep));
  889. }
  890. }
  891. contentChars += match.end();
  892. }
  893. }
  894. // Store dependency strings
  895. {
  896. auto& Depends = this->FileHandle->ParseData->Moc.Depends;
  897. Depends.reserve(parseDepends.size());
  898. for (std::string const& item : parseDepends) {
  899. Depends.emplace_back(item);
  900. // Replace end of line characters in filenames
  901. std::string& path = Depends.back();
  902. std::replace(path.begin(), path.end(), '\n', ' ');
  903. std::replace(path.begin(), path.end(), '\r', ' ');
  904. }
  905. }
  906. }
  907. void cmQtAutoMocUicT::JobParseT::MocIncludes()
  908. {
  909. if (this->Content.find("moc") == std::string::npos) {
  910. return;
  911. }
  912. std::set<std::string> underscore;
  913. std::set<std::string> dot;
  914. {
  915. const char* contentChars = this->Content.c_str();
  916. cmsys::RegularExpression const& regExp = this->MocConst().RegExpInclude;
  917. cmsys::RegularExpressionMatch match;
  918. while (regExp.find(contentChars, match)) {
  919. std::string incString = match.match(2);
  920. std::string const incBase =
  921. cmSystemTools::GetFilenameWithoutLastExtension(incString);
  922. if (cmHasLiteralPrefix(incBase, "moc_")) {
  923. // moc_<BASE>.cpp
  924. // Remove the moc_ part from the base name
  925. underscore.emplace(std::move(incString));
  926. } else {
  927. // <BASE>.moc
  928. dot.emplace(std::move(incString));
  929. }
  930. // Forward content pointer
  931. contentChars += match.end();
  932. }
  933. }
  934. auto& Include = this->FileHandle->ParseData->Moc.Include;
  935. this->CreateKeys(Include.Underscore, underscore, MocUnderscoreLength);
  936. this->CreateKeys(Include.Dot, dot, 0);
  937. }
  938. void cmQtAutoMocUicT::JobParseT::UicIncludes()
  939. {
  940. if (this->Content.find("ui_") == std::string::npos) {
  941. return;
  942. }
  943. std::set<std::string> includes;
  944. {
  945. const char* contentChars = this->Content.c_str();
  946. cmsys::RegularExpression const& regExp = this->UicConst().RegExpInclude;
  947. cmsys::RegularExpressionMatch match;
  948. while (regExp.find(contentChars, match)) {
  949. includes.emplace(match.match(2));
  950. // Forward content pointer
  951. contentChars += match.end();
  952. }
  953. }
  954. this->CreateKeys(this->FileHandle->ParseData->Uic.Include, includes,
  955. UiUnderscoreLength);
  956. }
  957. void cmQtAutoMocUicT::JobParseHeaderT::Process()
  958. {
  959. if (!this->ReadFile()) {
  960. return;
  961. }
  962. // Moc parsing
  963. if (this->FileHandle->Moc) {
  964. this->MocMacro();
  965. this->MocDependecies();
  966. }
  967. // Uic parsing
  968. if (this->FileHandle->Uic) {
  969. this->UicIncludes();
  970. }
  971. }
  972. void cmQtAutoMocUicT::JobParseSourceT::Process()
  973. {
  974. if (!this->ReadFile()) {
  975. return;
  976. }
  977. // Moc parsing
  978. if (this->FileHandle->Moc) {
  979. this->MocMacro();
  980. this->MocDependecies();
  981. this->MocIncludes();
  982. }
  983. // Uic parsing
  984. if (this->FileHandle->Uic) {
  985. this->UicIncludes();
  986. }
  987. }
  988. std::string cmQtAutoMocUicT::JobEvalCacheT::MessageSearchLocations() const
  989. {
  990. std::string res;
  991. res.reserve(512);
  992. for (std::string const& path : this->SearchLocations) {
  993. res += " ";
  994. res += this->MessagePath(path);
  995. res += '\n';
  996. }
  997. return res;
  998. }
  999. void cmQtAutoMocUicT::JobEvalCacheMocT::Process()
  1000. {
  1001. // Evaluate headers
  1002. for (auto const& pair : this->BaseEval().Headers) {
  1003. if (!this->EvalHeader(pair.second)) {
  1004. return;
  1005. }
  1006. }
  1007. // Evaluate sources
  1008. for (auto const& pair : this->BaseEval().Sources) {
  1009. if (!this->EvalSource(pair.second)) {
  1010. return;
  1011. }
  1012. }
  1013. }
  1014. bool cmQtAutoMocUicT::JobEvalCacheMocT::EvalHeader(SourceFileHandleT source)
  1015. {
  1016. SourceFileT const& sourceFile = *source;
  1017. auto const& parseData = sourceFile.ParseData->Moc;
  1018. if (!source->Moc) {
  1019. return true;
  1020. }
  1021. if (!parseData.Macro.empty()) {
  1022. // Create a new mapping
  1023. MappingHandleT handle = std::make_shared<MappingT>();
  1024. handle->SourceFile = std::move(source);
  1025. // Absolute build path
  1026. if (this->BaseConst().MultiConfig) {
  1027. handle->OutputFile =
  1028. this->Gen()->AbsoluteIncludePath(sourceFile.BuildPath);
  1029. } else {
  1030. handle->OutputFile =
  1031. this->Gen()->AbsoluteBuildPath(sourceFile.BuildPath);
  1032. }
  1033. // Register mapping in headers map
  1034. this->RegisterMapping(handle);
  1035. }
  1036. return true;
  1037. }
  1038. bool cmQtAutoMocUicT::JobEvalCacheMocT::EvalSource(
  1039. SourceFileHandleT const& source)
  1040. {
  1041. SourceFileT const& sourceFile = *source;
  1042. auto const& parseData = sourceFile.ParseData->Moc;
  1043. if (!sourceFile.Moc ||
  1044. (parseData.Macro.empty() && parseData.Include.Underscore.empty() &&
  1045. parseData.Include.Dot.empty())) {
  1046. return true;
  1047. }
  1048. std::string const sourceDirPrefix = SubDirPrefix(sourceFile.FileName);
  1049. std::string const sourceBase =
  1050. cmSystemTools::GetFilenameWithoutLastExtension(sourceFile.FileName);
  1051. // For relaxed mode check if the own "moc_" or ".moc" file is included
  1052. bool const relaxedMode = this->MocConst().RelaxedMode;
  1053. bool sourceIncludesMocUnderscore = false;
  1054. bool sourceIncludesDotMoc = false;
  1055. // Check if the sources own "moc_" or ".moc" file is included
  1056. if (relaxedMode) {
  1057. for (IncludeKeyT const& incKey : parseData.Include.Underscore) {
  1058. if (incKey.Base == sourceBase) {
  1059. sourceIncludesMocUnderscore = true;
  1060. break;
  1061. }
  1062. }
  1063. }
  1064. for (IncludeKeyT const& incKey : parseData.Include.Dot) {
  1065. if (incKey.Base == sourceBase) {
  1066. sourceIncludesDotMoc = true;
  1067. break;
  1068. }
  1069. }
  1070. // Check if this source needs to be moc processed but doesn't.
  1071. if (!sourceIncludesDotMoc && !parseData.Macro.empty() &&
  1072. !(relaxedMode && sourceIncludesMocUnderscore)) {
  1073. this->LogError(GenT::MOC,
  1074. cmStrCat(this->MessagePath(sourceFile.FileName),
  1075. "\ncontains a ", Quoted(parseData.Macro),
  1076. " macro, but does not include ",
  1077. this->MessagePath(sourceBase + ".moc"),
  1078. "!\nConsider to\n - add #include \"", sourceBase,
  1079. ".moc\"\n - enable SKIP_AUTOMOC for this file"));
  1080. return false;
  1081. }
  1082. // Evaluate "moc_" includes
  1083. for (IncludeKeyT const& incKey : parseData.Include.Underscore) {
  1084. SourceFileHandleT headerHandle;
  1085. {
  1086. std::string const headerBase = cmStrCat(incKey.Dir, incKey.Base);
  1087. if (!this->FindIncludedHeader(headerHandle, sourceDirPrefix,
  1088. headerBase)) {
  1089. this->LogError(
  1090. GenT::MOC,
  1091. cmStrCat(this->MessagePath(sourceFile.FileName),
  1092. "\nincludes the moc file ", this->MessagePath(incKey.Key),
  1093. ",\nbut a header ", this->MessageHeader(headerBase),
  1094. "\ncould not be found "
  1095. "in the following directories\n",
  1096. this->MessageSearchLocations()));
  1097. return false;
  1098. }
  1099. }
  1100. // The include might be handled differently in relaxed mode
  1101. if (relaxedMode && !sourceIncludesDotMoc && !parseData.Macro.empty() &&
  1102. (incKey.Base == sourceBase)) {
  1103. // The <BASE>.cpp file includes a Qt macro but does not include the
  1104. // <BASE>.moc file. In this case, the moc_<BASE>.cpp should probably
  1105. // be generated from <BASE>.cpp instead of <BASE>.h, because otherwise
  1106. // it won't build. But warn, since this is not how it is supposed to be
  1107. // used. This is for KDE4 compatibility.
  1108. // Issue a warning
  1109. this->Log().Warning(
  1110. GenT::MOC,
  1111. cmStrCat(this->MessagePath(sourceFile.FileName), "\ncontains a ",
  1112. Quoted(parseData.Macro), " macro, but does not include ",
  1113. this->MessagePath(sourceBase + ".moc"),
  1114. ".\nInstead it includes ", this->MessagePath(incKey.Key),
  1115. ".\nRunning moc on the source\n ",
  1116. this->MessagePath(sourceFile.FileName), "!\nBetter include ",
  1117. this->MessagePath(sourceBase + ".moc"),
  1118. " for compatibility with regular mode.\n",
  1119. "This is a CMAKE_AUTOMOC_RELAXED_MODE warning.\n"));
  1120. // Create mapping
  1121. if (!this->RegisterIncluded(incKey.Key, source, source)) {
  1122. return false;
  1123. }
  1124. continue;
  1125. }
  1126. // Check if header is skipped
  1127. if (this->MocConst().skipped(headerHandle->FileName)) {
  1128. continue;
  1129. }
  1130. // Create mapping
  1131. if (!this->RegisterIncluded(incKey.Key, source, std::move(headerHandle))) {
  1132. return false;
  1133. }
  1134. }
  1135. // Evaluate ".moc" includes
  1136. if (relaxedMode) {
  1137. // Relaxed mode
  1138. for (IncludeKeyT const& incKey : parseData.Include.Dot) {
  1139. // Check if this is the sources own .moc file
  1140. bool const ownMoc = (incKey.Base == sourceBase);
  1141. if (ownMoc && !parseData.Macro.empty()) {
  1142. // Create mapping for the regular use case
  1143. if (!this->RegisterIncluded(incKey.Key, source, source)) {
  1144. return false;
  1145. }
  1146. continue;
  1147. }
  1148. // Try to find a header instead but issue a warning.
  1149. // This is for KDE4 compatibility.
  1150. SourceFileHandleT headerHandle;
  1151. {
  1152. std::string const headerBase = cmStrCat(incKey.Dir, incKey.Base);
  1153. if (!this->FindIncludedHeader(headerHandle, sourceDirPrefix,
  1154. headerBase)) {
  1155. this->LogError(
  1156. GenT::MOC,
  1157. cmStrCat(
  1158. this->MessagePath(sourceFile.FileName),
  1159. "\nincludes the moc file ", this->MessagePath(incKey.Key),
  1160. ",\nwhich seems to be the moc file from a different source "
  1161. "file.\nCMAKE_AUTOMOC_RELAXED_MODE:\nAlso a matching header ",
  1162. this->MessageHeader(headerBase),
  1163. "\ncould not be found in the following directories\n",
  1164. this->MessageSearchLocations()));
  1165. return false;
  1166. }
  1167. }
  1168. // Check if header is skipped
  1169. if (this->MocConst().skipped(headerHandle->FileName)) {
  1170. continue;
  1171. }
  1172. // Issue a warning
  1173. if (ownMoc && parseData.Macro.empty()) {
  1174. this->Log().Warning(
  1175. GenT::MOC,
  1176. cmStrCat(
  1177. this->MessagePath(sourceFile.FileName), "\nincludes the moc file ",
  1178. this->MessagePath(incKey.Key), ", but does not contain a\n",
  1179. this->MocConst().MacrosString(),
  1180. " macro.\nRunning moc on the header\n ",
  1181. this->MessagePath(headerHandle->FileName), "!\nBetter include ",
  1182. this->MessagePath("moc_" + incKey.Base + ".cpp"),
  1183. " for a compatibility with regular mode.\n",
  1184. "This is a CMAKE_AUTOMOC_RELAXED_MODE warning.\n"));
  1185. } else {
  1186. this->Log().Warning(
  1187. GenT::MOC,
  1188. cmStrCat(
  1189. this->MessagePath(sourceFile.FileName), "\nincludes the moc file ",
  1190. this->MessagePath(incKey.Key), " instead of ",
  1191. this->MessagePath("moc_" + incKey.Base + ".cpp"),
  1192. ".\nRunning moc on the header\n ",
  1193. this->MessagePath(headerHandle->FileName), "!\nBetter include ",
  1194. this->MessagePath("moc_" + incKey.Base + ".cpp"),
  1195. " for compatibility with regular mode.\n",
  1196. "This is a CMAKE_AUTOMOC_RELAXED_MODE warning.\n"));
  1197. }
  1198. // Create mapping
  1199. if (!this->RegisterIncluded(incKey.Key, source,
  1200. std::move(headerHandle))) {
  1201. return false;
  1202. }
  1203. }
  1204. } else {
  1205. // Strict mode
  1206. for (IncludeKeyT const& incKey : parseData.Include.Dot) {
  1207. // Check if this is the sources own .moc file
  1208. bool const ownMoc = (incKey.Base == sourceBase);
  1209. if (!ownMoc) {
  1210. // Don't allow <BASE>.moc include other than own in regular mode
  1211. this->LogError(
  1212. GenT::MOC,
  1213. cmStrCat(this->MessagePath(sourceFile.FileName),
  1214. "\nincludes the moc file ", this->MessagePath(incKey.Key),
  1215. ",\nwhich seems to be the moc file from a different "
  1216. "source file.\nThis is not supported. Include ",
  1217. this->MessagePath(sourceBase + ".moc"),
  1218. " to run moc on this source file."));
  1219. return false;
  1220. }
  1221. // Accept but issue a warning if moc isn't required
  1222. if (parseData.Macro.empty()) {
  1223. this->Log().Warning(
  1224. GenT::MOC,
  1225. cmStrCat(this->MessagePath(sourceFile.FileName),
  1226. "\nincludes the moc file ", this->MessagePath(incKey.Key),
  1227. ", but does not contain a ",
  1228. this->MocConst().MacrosString(), " macro."));
  1229. }
  1230. // Create mapping
  1231. if (!this->RegisterIncluded(incKey.Key, source, source)) {
  1232. return false;
  1233. }
  1234. }
  1235. }
  1236. return true;
  1237. }
  1238. bool cmQtAutoMocUicT::JobEvalCacheMocT::FindIncludedHeader(
  1239. SourceFileHandleT& headerHandle, cm::string_view includerDir,
  1240. cm::string_view includeBase)
  1241. {
  1242. // Clear search locations
  1243. this->SearchLocations.clear();
  1244. auto findHeader = [this,
  1245. &headerHandle](std::string const& basePath) -> bool {
  1246. bool found = false;
  1247. for (std::string const& ext : this->BaseConst().HeaderExtensions) {
  1248. std::string const testPath =
  1249. this->Gen()->CollapseFullPathTS(cmStrCat(basePath, '.', ext));
  1250. cmFileTime fileTime;
  1251. if (!fileTime.Load(testPath)) {
  1252. // File not found
  1253. continue;
  1254. }
  1255. // Return a known file if it exists already
  1256. {
  1257. auto it = this->BaseEval().Headers.find(testPath);
  1258. if (it != this->BaseEval().Headers.end()) {
  1259. headerHandle = it->second;
  1260. found = true;
  1261. break;
  1262. }
  1263. }
  1264. // Created and return discovered file entry
  1265. {
  1266. SourceFileHandleT& handle =
  1267. this->MocEval().HeadersDiscovered[testPath];
  1268. if (!handle) {
  1269. handle = std::make_shared<SourceFileT>(testPath);
  1270. handle->FileTime = fileTime;
  1271. handle->IsHeader = true;
  1272. handle->Moc = true;
  1273. }
  1274. headerHandle = handle;
  1275. found = true;
  1276. break;
  1277. }
  1278. }
  1279. if (!found) {
  1280. this->SearchLocations.emplace_back(cmQtAutoGen::ParentDir(basePath));
  1281. }
  1282. return found;
  1283. };
  1284. // Search in vicinity of the source
  1285. if (findHeader(cmStrCat(includerDir, includeBase))) {
  1286. return true;
  1287. }
  1288. // Search in include directories
  1289. auto const& includePaths = this->MocConst().IncludePaths;
  1290. return std::any_of(
  1291. includePaths.begin(), includePaths.end(),
  1292. [&findHeader, &includeBase](std::string const& path) -> bool {
  1293. return findHeader(cmStrCat(path, '/', includeBase));
  1294. });
  1295. }
  1296. bool cmQtAutoMocUicT::JobEvalCacheMocT::RegisterIncluded(
  1297. std::string const& includeString, SourceFileHandleT includerFileHandle,
  1298. SourceFileHandleT sourceFileHandle) const
  1299. {
  1300. // Check if this file is already included
  1301. MappingHandleT& handle = this->MocEval().Includes[includeString];
  1302. if (handle) {
  1303. // Check if the output file would be generated from different source files
  1304. if (handle->SourceFile != sourceFileHandle) {
  1305. std::string files =
  1306. cmStrCat(" ", this->MessagePath(includerFileHandle->FileName), '\n');
  1307. for (auto const& item : handle->IncluderFiles) {
  1308. files += cmStrCat(" ", this->MessagePath(item->FileName), '\n');
  1309. }
  1310. this->LogError(
  1311. GenT::MOC,
  1312. cmStrCat("The source files\n", files,
  1313. "contain the same include string ",
  1314. this->MessagePath(includeString),
  1315. ", but\nthe moc file would be generated from different "
  1316. "source files\n ",
  1317. this->MessagePath(sourceFileHandle->FileName), " and\n ",
  1318. this->MessagePath(handle->SourceFile->FileName),
  1319. ".\nConsider to\n"
  1320. " - not include the \"moc_<NAME>.cpp\" file\n"
  1321. " - add a directory prefix to a \"<NAME>.moc\" include "
  1322. "(e.g \"sub/<NAME>.moc\")\n"
  1323. " - rename the source file(s)\n"));
  1324. return false;
  1325. }
  1326. // The same mapping already exists. Just add to the includers list.
  1327. handle->IncluderFiles.emplace_back(std::move(includerFileHandle));
  1328. return true;
  1329. }
  1330. // Create a new mapping
  1331. handle = std::make_shared<MappingT>();
  1332. handle->IncludeString = includeString;
  1333. handle->IncluderFiles.emplace_back(std::move(includerFileHandle));
  1334. handle->SourceFile = std::move(sourceFileHandle);
  1335. handle->OutputFile = this->Gen()->AbsoluteIncludePath(includeString);
  1336. // Register mapping in sources/headers map
  1337. this->RegisterMapping(handle);
  1338. return true;
  1339. }
  1340. void cmQtAutoMocUicT::JobEvalCacheMocT::RegisterMapping(
  1341. MappingHandleT mappingHandle) const
  1342. {
  1343. auto& regMap = mappingHandle->SourceFile->IsHeader
  1344. ? this->MocEval().HeaderMappings
  1345. : this->MocEval().SourceMappings;
  1346. // Check if source file already gets mapped
  1347. auto& regHandle = regMap[mappingHandle->SourceFile->FileName];
  1348. if (!regHandle) {
  1349. // Yet unknown mapping
  1350. regHandle = std::move(mappingHandle);
  1351. } else {
  1352. // Mappings with include string override those without
  1353. if (!mappingHandle->IncludeString.empty()) {
  1354. regHandle = std::move(mappingHandle);
  1355. }
  1356. }
  1357. }
  1358. std::string cmQtAutoMocUicT::JobEvalCacheMocT::MessageHeader(
  1359. cm::string_view headerBase) const
  1360. {
  1361. return this->MessagePath(cmStrCat(
  1362. headerBase, ".{", cmJoin(this->BaseConst().HeaderExtensions, ","), '}'));
  1363. }
  1364. void cmQtAutoMocUicT::JobEvalCacheUicT::Process()
  1365. {
  1366. // Prepare buffers
  1367. this->SearchLocations.reserve((this->UicConst().SearchPaths.size() + 1) * 2);
  1368. // Evaluate headers
  1369. for (auto const& pair : this->BaseEval().Headers) {
  1370. if (!this->EvalFile(pair.second)) {
  1371. return;
  1372. }
  1373. }
  1374. // Evaluate sources
  1375. for (auto const& pair : this->BaseEval().Sources) {
  1376. if (!this->EvalFile(pair.second)) {
  1377. return;
  1378. }
  1379. }
  1380. }
  1381. bool cmQtAutoMocUicT::JobEvalCacheUicT::EvalFile(
  1382. SourceFileHandleT const& sourceFileHandle)
  1383. {
  1384. SourceFileT const& sourceFile = *sourceFileHandle;
  1385. auto const& Include = sourceFile.ParseData->Uic.Include;
  1386. if (!sourceFile.Uic || Include.empty()) {
  1387. return true;
  1388. }
  1389. std::string const sourceDirPrefix = SubDirPrefix(sourceFile.FileName);
  1390. return std::all_of(
  1391. Include.begin(), Include.end(),
  1392. [this, &sourceDirPrefix, &sourceFile,
  1393. &sourceFileHandle](IncludeKeyT const& incKey) -> bool {
  1394. // Find .ui file
  1395. this->UiName = cmStrCat(incKey.Base, ".ui");
  1396. if (!this->FindIncludedUi(sourceDirPrefix, incKey.Dir)) {
  1397. this->LogError(
  1398. GenT::UIC,
  1399. cmStrCat(this->MessagePath(sourceFile.FileName),
  1400. "\nincludes the uic file ", this->MessagePath(incKey.Key),
  1401. ",\nbut the user interface file ",
  1402. this->MessagePath(this->UiName),
  1403. "\ncould not be found in the following directories\n",
  1404. this->MessageSearchLocations()));
  1405. return false;
  1406. }
  1407. // Check if the file is skipped
  1408. if (this->UicConst().skipped(this->UiFileHandle->FileName)) {
  1409. return true;
  1410. }
  1411. // Register mapping
  1412. return this->RegisterMapping(incKey.Key, sourceFileHandle);
  1413. });
  1414. }
  1415. bool cmQtAutoMocUicT::JobEvalCacheUicT::FindIncludedUi(
  1416. cm::string_view sourceDirPrefix, cm::string_view includePrefix)
  1417. {
  1418. // Clear locations buffer
  1419. this->SearchLocations.clear();
  1420. auto findUi = [this](std::string const& testPath) -> bool {
  1421. std::string const fullPath = this->Gen()->CollapseFullPathTS(testPath);
  1422. cmFileTime fileTime;
  1423. if (!fileTime.Load(fullPath)) {
  1424. this->SearchLocations.emplace_back(cmQtAutoGen::ParentDir(fullPath));
  1425. return false;
  1426. }
  1427. // .ui file found in files system!
  1428. // Get or create .ui file handle
  1429. SourceFileHandleT& handle = this->UicEval().UiFiles[fullPath];
  1430. if (!handle) {
  1431. // The file wasn't registered, yet
  1432. handle = std::make_shared<SourceFileT>(fullPath);
  1433. handle->FileTime = fileTime;
  1434. }
  1435. this->UiFileHandle = handle;
  1436. return true;
  1437. };
  1438. // Vicinity of the source
  1439. if (!includePrefix.empty()) {
  1440. if (findUi(cmStrCat(sourceDirPrefix, includePrefix, this->UiName))) {
  1441. return true;
  1442. }
  1443. }
  1444. if (findUi(cmStrCat(sourceDirPrefix, this->UiName))) {
  1445. return true;
  1446. }
  1447. // Additional AUTOUIC search paths
  1448. auto const& searchPaths = this->UicConst().SearchPaths;
  1449. if (!searchPaths.empty()) {
  1450. for (std::string const& sPath : searchPaths) {
  1451. if (findUi(cmStrCat(sPath, '/', this->UiName))) {
  1452. return true;
  1453. }
  1454. }
  1455. if (!includePrefix.empty()) {
  1456. for (std::string const& sPath : searchPaths) {
  1457. if (findUi(cmStrCat(sPath, '/', includePrefix, this->UiName))) {
  1458. return true;
  1459. }
  1460. }
  1461. }
  1462. }
  1463. return false;
  1464. }
  1465. bool cmQtAutoMocUicT::JobEvalCacheUicT::RegisterMapping(
  1466. std::string const& includeString, SourceFileHandleT includerFileHandle)
  1467. {
  1468. auto& Includes = this->Gen()->UicEval().Includes;
  1469. auto it = Includes.find(includeString);
  1470. if (it != Includes.end()) {
  1471. MappingHandleT const& handle = it->second;
  1472. if (handle->SourceFile != this->UiFileHandle) {
  1473. // The output file already gets generated - from a different .ui file!
  1474. std::string files =
  1475. cmStrCat(" ", this->MessagePath(includerFileHandle->FileName), '\n');
  1476. for (auto const& item : handle->IncluderFiles) {
  1477. files += cmStrCat(" ", this->MessagePath(item->FileName), '\n');
  1478. }
  1479. this->LogError(
  1480. GenT::UIC,
  1481. cmStrCat(
  1482. "The source files\n", files, "contain the same include string ",
  1483. Quoted(includeString),
  1484. ", but\nthe uic file would be generated from different "
  1485. "user interface files\n ",
  1486. this->MessagePath(this->UiFileHandle->FileName), " and\n ",
  1487. this->MessagePath(handle->SourceFile->FileName),
  1488. ".\nConsider to\n"
  1489. " - add a directory prefix to a \"ui_<NAME>.h\" include "
  1490. "(e.g \"sub/ui_<NAME>.h\")\n"
  1491. " - rename the <NAME>.ui file(s) and adjust the \"ui_<NAME>.h\" "
  1492. "include(s)\n"));
  1493. return false;
  1494. }
  1495. // Add includer file to existing mapping
  1496. handle->IncluderFiles.emplace_back(std::move(includerFileHandle));
  1497. } else {
  1498. // New mapping handle
  1499. MappingHandleT handle = std::make_shared<MappingT>();
  1500. handle->IncludeString = includeString;
  1501. handle->IncluderFiles.emplace_back(std::move(includerFileHandle));
  1502. handle->SourceFile = this->UiFileHandle;
  1503. handle->OutputFile = this->Gen()->AbsoluteIncludePath(includeString);
  1504. // Register mapping
  1505. Includes.emplace(includeString, std::move(handle));
  1506. }
  1507. return true;
  1508. }
  1509. void cmQtAutoMocUicT::JobEvalCacheFinishT::Process()
  1510. {
  1511. // Add discovered header parse jobs
  1512. this->Gen()->CreateParseJobs<JobParseHeaderT>(
  1513. this->MocEval().HeadersDiscovered);
  1514. // Add dependency probing jobs
  1515. {
  1516. // Add fence job to ensure all parsing has finished
  1517. this->Gen()->WorkerPool().EmplaceJob<JobFenceT>();
  1518. if (this->MocConst().Enabled) {
  1519. this->Gen()->WorkerPool().EmplaceJob<JobProbeDepsMocT>();
  1520. }
  1521. if (this->UicConst().Enabled) {
  1522. this->Gen()->WorkerPool().EmplaceJob<JobProbeDepsUicT>();
  1523. }
  1524. // Add probe finish job
  1525. this->Gen()->WorkerPool().EmplaceJob<JobProbeDepsFinishT>();
  1526. }
  1527. }
  1528. void cmQtAutoMocUicT::JobProbeDepsMocT::Process()
  1529. {
  1530. // Create moc header jobs
  1531. for (auto const& pair : this->MocEval().HeaderMappings) {
  1532. // Register if this mapping is a candidate for mocs_compilation.cpp
  1533. bool const compFile = pair.second->IncludeString.empty();
  1534. if (compFile) {
  1535. this->MocEval().CompFiles.emplace_back(
  1536. pair.second->SourceFile->BuildPath);
  1537. }
  1538. if (!this->Generate(pair.second, compFile)) {
  1539. return;
  1540. }
  1541. }
  1542. // Create moc source jobs
  1543. for (auto const& pair : this->MocEval().SourceMappings) {
  1544. if (!this->Generate(pair.second, false)) {
  1545. return;
  1546. }
  1547. }
  1548. }
  1549. bool cmQtAutoMocUicT::JobProbeDepsMocT::Generate(MappingHandleT const& mapping,
  1550. bool compFile) const
  1551. {
  1552. std::unique_ptr<std::string> reason;
  1553. if (this->Log().Verbose()) {
  1554. reason = cm::make_unique<std::string>();
  1555. }
  1556. if (this->Probe(*mapping, reason.get())) {
  1557. // Register the parent directory for creation
  1558. this->MocEval().OutputDirs.emplace(
  1559. cmQtAutoGen::ParentDir(mapping->OutputFile));
  1560. // Fetch the cache entry for the source file
  1561. std::string const& sourceFile = mapping->SourceFile->FileName;
  1562. ParseCacheT::GetOrInsertT cacheEntry =
  1563. this->BaseEval().ParseCache.GetOrInsert(sourceFile);
  1564. // Add moc job
  1565. this->Gen()->WorkerPool().EmplaceJob<JobCompileMocT>(
  1566. mapping, std::move(reason), std::move(cacheEntry.first));
  1567. // Check if a moc job for a mocs_compilation.cpp entry was generated
  1568. if (compFile) {
  1569. this->MocEval().CompUpdated = true;
  1570. }
  1571. }
  1572. return true;
  1573. }
  1574. bool cmQtAutoMocUicT::JobProbeDepsMocT::Probe(MappingT const& mapping,
  1575. std::string* reason) const
  1576. {
  1577. std::string const& sourceFile = mapping.SourceFile->FileName;
  1578. std::string const& outputFile = mapping.OutputFile;
  1579. // Test if the output file exists
  1580. cmFileTime outputFileTime;
  1581. if (!outputFileTime.Load(outputFile)) {
  1582. if (reason != nullptr) {
  1583. *reason = cmStrCat("Generating ", this->MessagePath(outputFile),
  1584. ", because it doesn't exist, from ",
  1585. this->MessagePath(sourceFile));
  1586. }
  1587. return true;
  1588. }
  1589. // Test if any setting changed
  1590. if (this->MocConst().SettingsChanged) {
  1591. if (reason != nullptr) {
  1592. *reason = cmStrCat("Generating ", this->MessagePath(outputFile),
  1593. ", because the uic settings changed, from ",
  1594. this->MessagePath(sourceFile));
  1595. }
  1596. return true;
  1597. }
  1598. // Test if the source file is newer
  1599. if (outputFileTime.Older(mapping.SourceFile->FileTime)) {
  1600. if (reason != nullptr) {
  1601. *reason = cmStrCat("Generating ", this->MessagePath(outputFile),
  1602. ", because it's older than its source file, from ",
  1603. this->MessagePath(sourceFile));
  1604. }
  1605. return true;
  1606. }
  1607. // Test if the moc_predefs file is newer
  1608. if (!this->MocConst().PredefsFileAbs.empty()) {
  1609. if (outputFileTime.Older(this->MocEval().PredefsTime)) {
  1610. if (reason != nullptr) {
  1611. *reason = cmStrCat("Generating ", this->MessagePath(outputFile),
  1612. ", because it's older than ",
  1613. this->MessagePath(this->MocConst().PredefsFileAbs),
  1614. ", from ", this->MessagePath(sourceFile));
  1615. }
  1616. return true;
  1617. }
  1618. }
  1619. // Test if the moc executable is newer
  1620. if (outputFileTime.Older(this->MocConst().ExecutableTime)) {
  1621. if (reason != nullptr) {
  1622. *reason = cmStrCat("Generating ", this->MessagePath(outputFile),
  1623. ", because it's older than the moc executable, from ",
  1624. this->MessagePath(sourceFile));
  1625. }
  1626. return true;
  1627. }
  1628. // Test if a dependency file is newer
  1629. {
  1630. // Check dependency timestamps
  1631. std::string const sourceDir = SubDirPrefix(sourceFile);
  1632. auto& dependencies = mapping.SourceFile->ParseData->Moc.Depends;
  1633. for (auto it = dependencies.begin(); it != dependencies.end(); ++it) {
  1634. auto& dep = *it;
  1635. // Find dependency file
  1636. auto const depMatch = this->FindDependency(sourceDir, dep);
  1637. if (depMatch.first.empty()) {
  1638. if (reason != nullptr) {
  1639. *reason = cmStrCat("Generating ", this->MessagePath(outputFile),
  1640. " from ", this->MessagePath(sourceFile),
  1641. ", because its dependency ",
  1642. this->MessagePath(dep), " vanished.");
  1643. }
  1644. dependencies.erase(it);
  1645. this->BaseEval().ParseCacheChanged = true;
  1646. return true;
  1647. }
  1648. // Test if dependency file is older
  1649. if (outputFileTime.Older(depMatch.second)) {
  1650. if (reason != nullptr) {
  1651. *reason = cmStrCat("Generating ", this->MessagePath(outputFile),
  1652. ", because it's older than its dependency file ",
  1653. this->MessagePath(depMatch.first), ", from ",
  1654. this->MessagePath(sourceFile));
  1655. }
  1656. return true;
  1657. }
  1658. }
  1659. }
  1660. return false;
  1661. }
  1662. std::pair<std::string, cmFileTime>
  1663. cmQtAutoMocUicT::JobProbeDepsMocT::FindDependency(
  1664. std::string const& sourceDir, std::string const& includeString) const
  1665. {
  1666. using ResPair = std::pair<std::string, cmFileTime>;
  1667. // moc's dependency file contains absolute paths
  1668. if (this->MocConst().CanOutputDependencies) {
  1669. ResPair res{ includeString, {} };
  1670. if (res.second.Load(res.first)) {
  1671. return res;
  1672. }
  1673. return {};
  1674. }
  1675. // Search in vicinity of the source
  1676. {
  1677. ResPair res{ sourceDir + includeString, {} };
  1678. if (res.second.Load(res.first)) {
  1679. return res;
  1680. }
  1681. }
  1682. // Search in include directories
  1683. for (std::string const& includePath : this->MocConst().IncludePaths) {
  1684. ResPair res{ cmStrCat(includePath, '/', includeString), {} };
  1685. if (res.second.Load(res.first)) {
  1686. return res;
  1687. }
  1688. }
  1689. // Return empty
  1690. return ResPair();
  1691. }
  1692. void cmQtAutoMocUicT::JobProbeDepsUicT::Process()
  1693. {
  1694. for (auto const& pair : this->Gen()->UicEval().Includes) {
  1695. MappingHandleT const& mapping = pair.second;
  1696. std::unique_ptr<std::string> reason;
  1697. if (this->Log().Verbose()) {
  1698. reason = cm::make_unique<std::string>();
  1699. }
  1700. if (!this->Probe(*mapping, reason.get())) {
  1701. continue;
  1702. }
  1703. // Register the parent directory for creation
  1704. this->UicEval().OutputDirs.emplace(
  1705. cmQtAutoGen::ParentDir(mapping->OutputFile));
  1706. // Add uic job
  1707. this->Gen()->WorkerPool().EmplaceJob<JobCompileUicT>(mapping,
  1708. std::move(reason));
  1709. }
  1710. }
  1711. bool cmQtAutoMocUicT::JobProbeDepsUicT::Probe(MappingT const& mapping,
  1712. std::string* reason) const
  1713. {
  1714. std::string const& sourceFile = mapping.SourceFile->FileName;
  1715. std::string const& outputFile = mapping.OutputFile;
  1716. // Test if the build file exists
  1717. cmFileTime outputFileTime;
  1718. if (!outputFileTime.Load(outputFile)) {
  1719. if (reason != nullptr) {
  1720. *reason = cmStrCat("Generating ", this->MessagePath(outputFile),
  1721. ", because it doesn't exist, from ",
  1722. this->MessagePath(sourceFile));
  1723. }
  1724. return true;
  1725. }
  1726. // Test if the uic settings changed
  1727. if (this->UicConst().SettingsChanged) {
  1728. if (reason != nullptr) {
  1729. *reason = cmStrCat("Generating ", this->MessagePath(outputFile),
  1730. ", because the uic settings changed, from ",
  1731. this->MessagePath(sourceFile));
  1732. }
  1733. return true;
  1734. }
  1735. // Test if the source file is newer
  1736. if (outputFileTime.Older(mapping.SourceFile->FileTime)) {
  1737. if (reason != nullptr) {
  1738. *reason = cmStrCat("Generating ", this->MessagePath(outputFile),
  1739. " because it's older than the source file ",
  1740. this->MessagePath(sourceFile));
  1741. }
  1742. return true;
  1743. }
  1744. // Test if the uic executable is newer
  1745. if (outputFileTime.Older(this->UicConst().ExecutableTime)) {
  1746. if (reason != nullptr) {
  1747. *reason = cmStrCat("Generating ", this->MessagePath(outputFile),
  1748. ", because it's older than the uic executable, from ",
  1749. this->MessagePath(sourceFile));
  1750. }
  1751. return true;
  1752. }
  1753. return false;
  1754. }
  1755. void cmQtAutoMocUicT::JobProbeDepsFinishT::Process()
  1756. {
  1757. // Create output directories
  1758. {
  1759. using StringSet = std::unordered_set<std::string>;
  1760. auto createDirs = [this](GenT genType, StringSet const& dirSet) {
  1761. for (std::string const& dirName : dirSet) {
  1762. if (!cmSystemTools::MakeDirectory(dirName)) {
  1763. this->LogError(genType,
  1764. cmStrCat("Creating directory ",
  1765. this->MessagePath(dirName), " failed."));
  1766. return;
  1767. }
  1768. }
  1769. };
  1770. if (this->MocConst().Enabled && this->UicConst().Enabled) {
  1771. StringSet outputDirs = this->MocEval().OutputDirs;
  1772. outputDirs.insert(this->UicEval().OutputDirs.begin(),
  1773. this->UicEval().OutputDirs.end());
  1774. createDirs(GenT::GEN, outputDirs);
  1775. } else if (this->MocConst().Enabled) {
  1776. createDirs(GenT::MOC, this->MocEval().OutputDirs);
  1777. } else if (this->UicConst().Enabled) {
  1778. createDirs(GenT::UIC, this->UicEval().OutputDirs);
  1779. }
  1780. }
  1781. if (this->MocConst().Enabled) {
  1782. // Add mocs compilations job
  1783. this->Gen()->WorkerPool().EmplaceJob<JobMocsCompilationT>();
  1784. }
  1785. if (!this->BaseConst().DepFile.empty()) {
  1786. // Add job to merge dep files
  1787. this->Gen()->WorkerPool().EmplaceJob<JobDepFilesMergeT>();
  1788. }
  1789. // Add finish job
  1790. this->Gen()->WorkerPool().EmplaceJob<JobFinishT>();
  1791. }
  1792. void cmQtAutoMocUicT::JobCompileMocT::Process()
  1793. {
  1794. std::string const& sourceFile = this->Mapping->SourceFile->FileName;
  1795. std::string const& outputFile = this->Mapping->OutputFile;
  1796. // Compose moc command
  1797. std::vector<std::string> cmd;
  1798. {
  1799. // Reserve large enough
  1800. cmd.reserve(this->MocConst().OptionsDefinitions.size() +
  1801. this->MocConst().OptionsIncludes.size() +
  1802. this->MocConst().OptionsExtra.size() + 16);
  1803. cmd.push_back(this->MocConst().Executable);
  1804. // Add definitions
  1805. cm::append(cmd, this->MocConst().OptionsDefinitions);
  1806. // Add includes
  1807. cm::append(cmd, this->MocConst().OptionsIncludes);
  1808. // Add predefs include
  1809. if (!this->MocConst().PredefsFileAbs.empty()) {
  1810. cmd.emplace_back("--include");
  1811. cmd.push_back(this->MocConst().PredefsFileAbs);
  1812. }
  1813. // Add path prefix on demand
  1814. if (this->MocConst().PathPrefix && this->Mapping->SourceFile->IsHeader) {
  1815. for (std::string const& dir : this->MocConst().IncludePaths) {
  1816. cm::string_view prefix = sourceFile;
  1817. if (cmHasPrefix(prefix, dir)) {
  1818. prefix.remove_prefix(dir.size());
  1819. if (cmHasPrefix(prefix, '/')) {
  1820. prefix.remove_prefix(1);
  1821. auto slashPos = prefix.rfind('/');
  1822. if (slashPos != cm::string_view::npos) {
  1823. cmd.emplace_back("-p");
  1824. cmd.emplace_back(prefix.substr(0, slashPos));
  1825. } else {
  1826. cmd.emplace_back("-p");
  1827. cmd.emplace_back("./");
  1828. }
  1829. break;
  1830. }
  1831. }
  1832. }
  1833. }
  1834. // Add extra options
  1835. cm::append(cmd, this->MocConst().OptionsExtra);
  1836. if (this->MocConst().CanOutputDependencies) {
  1837. cmd.emplace_back("--output-dep-file");
  1838. }
  1839. // Add output file
  1840. cmd.emplace_back("-o");
  1841. cmd.push_back(outputFile);
  1842. // Add source file
  1843. cmd.push_back(sourceFile);
  1844. }
  1845. // Execute moc command
  1846. cmWorkerPool::ProcessResultT result;
  1847. if (!this->RunProcess(GenT::MOC, result, cmd, this->Reason.get())) {
  1848. // Moc command failed
  1849. std::string includers;
  1850. if (!this->Mapping->IncluderFiles.empty()) {
  1851. includers = "included by\n";
  1852. for (auto const& item : this->Mapping->IncluderFiles) {
  1853. includers += cmStrCat(" ", this->MessagePath(item->FileName), '\n');
  1854. }
  1855. }
  1856. this->LogCommandError(GenT::MOC,
  1857. cmStrCat("The moc process failed to compile\n ",
  1858. this->MessagePath(sourceFile), "\ninto\n ",
  1859. this->MessagePath(outputFile), '\n',
  1860. includers, result.ErrorMessage),
  1861. cmd, result.StdOut);
  1862. return;
  1863. }
  1864. // Moc command success. Print moc output.
  1865. if (!result.StdOut.empty()) {
  1866. this->Log().Info(GenT::MOC, result.StdOut);
  1867. }
  1868. // Extract dependencies from the dep file moc generated for us
  1869. if (this->MocConst().CanOutputDependencies) {
  1870. const std::string depfile = outputFile + ".d";
  1871. if (this->Log().Verbose()) {
  1872. this->Log().Info(
  1873. GenT::MOC, "Reading dependencies from " + this->MessagePath(depfile));
  1874. }
  1875. if (!cmSystemTools::FileExists(depfile)) {
  1876. this->Log().Warning(GenT::MOC,
  1877. "Dependency file " + this->MessagePath(depfile) +
  1878. " does not exist.");
  1879. return;
  1880. }
  1881. this->CacheEntry->Moc.Depends =
  1882. this->Gen()->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. [this](const std::string& mocOutputFile) -> std::vector<std::string> {
  2031. std::string f = mocOutputFile + ".d";
  2032. if (!cmSystemTools::FileExists(f)) {
  2033. return {};
  2034. }
  2035. return this->Gen()->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 SKIP_AUTOMOC files.
  2053. // Also remove AUTOUIC header files to avoid cyclic dependency.
  2054. dependencies.erase(
  2055. std::remove_if(dependencies.begin(), dependencies.end(),
  2056. [this](const std::string& dep) {
  2057. return this->MocConst().skipped(dep) ||
  2058. std::any_of(
  2059. this->UicEval().Includes.begin(),
  2060. this->UicEval().Includes.end(),
  2061. [&dep](MappingMapT::value_type const& mapping) {
  2062. return dep == mapping.second->OutputFile;
  2063. });
  2064. }),
  2065. dependencies.end());
  2066. // Remove duplicates to make the depfile smaller
  2067. std::sort(dependencies.begin(), dependencies.end());
  2068. dependencies.erase(std::unique(dependencies.begin(), dependencies.end()),
  2069. dependencies.end());
  2070. // Add form files
  2071. for (const auto& uif : this->UicEval().UiFiles) {
  2072. dependencies.push_back(uif.first);
  2073. }
  2074. // Write the file
  2075. cmsys::ofstream ofs;
  2076. ofs.open(this->BaseConst().DepFile.c_str(),
  2077. (std::ios::out | std::ios::binary | std::ios::trunc));
  2078. if (!ofs) {
  2079. this->LogError(GenT::GEN,
  2080. cmStrCat("Cannot open ",
  2081. this->MessagePath(this->BaseConst().DepFile),
  2082. " for writing."));
  2083. return;
  2084. }
  2085. ofs << this->BaseConst().DepFileRuleName << ": \\\n";
  2086. for (const std::string& file : dependencies) {
  2087. ofs << '\t' << escapeDependencyPath(file) << " \\\n";
  2088. if (!ofs.good()) {
  2089. this->LogError(GenT::GEN,
  2090. cmStrCat("Writing depfile",
  2091. this->MessagePath(this->BaseConst().DepFile),
  2092. " failed."));
  2093. return;
  2094. }
  2095. }
  2096. // Add the CMake executable to re-new cache data if necessary.
  2097. // Also, this is the last entry, so don't add a backslash.
  2098. ofs << '\t' << escapeDependencyPath(this->BaseConst().CMakeExecutable)
  2099. << '\n';
  2100. }
  2101. void cmQtAutoMocUicT::JobFinishT::Process()
  2102. {
  2103. this->Gen()->AbortSuccess();
  2104. }
  2105. cmQtAutoMocUicT::cmQtAutoMocUicT()
  2106. : cmQtAutoGenerator(GenT::GEN)
  2107. {
  2108. }
  2109. cmQtAutoMocUicT::~cmQtAutoMocUicT() = default;
  2110. bool cmQtAutoMocUicT::InitFromInfo(InfoT const& info)
  2111. {
  2112. // -- Required settings
  2113. if (!info.GetBool("MULTI_CONFIG", this->BaseConst_.MultiConfig, true) ||
  2114. !info.GetUInt("QT_VERSION_MAJOR", this->BaseConst_.QtVersion.Major,
  2115. true) ||
  2116. !info.GetUInt("QT_VERSION_MINOR", this->BaseConst_.QtVersion.Minor,
  2117. true) ||
  2118. !info.GetUInt("PARALLEL", this->BaseConst_.ThreadCount, false) ||
  2119. !info.GetString("BUILD_DIR", this->BaseConst_.AutogenBuildDir, true) ||
  2120. !info.GetStringConfig("INCLUDE_DIR", this->BaseConst_.AutogenIncludeDir,
  2121. true) ||
  2122. !info.GetString("CMAKE_EXECUTABLE", this->BaseConst_.CMakeExecutable,
  2123. true) ||
  2124. !info.GetStringConfig("PARSE_CACHE_FILE",
  2125. this->BaseConst_.ParseCacheFile, true) ||
  2126. !info.GetString("DEP_FILE", this->BaseConst_.DepFile, false) ||
  2127. !info.GetString("DEP_FILE_RULE_NAME", this->BaseConst_.DepFileRuleName,
  2128. false) ||
  2129. !info.GetStringConfig("SETTINGS_FILE", this->SettingsFile_, true) ||
  2130. !info.GetArray("CMAKE_LIST_FILES", this->BaseConst_.ListFiles, true) ||
  2131. !info.GetArray("HEADER_EXTENSIONS", this->BaseConst_.HeaderExtensions,
  2132. true) ||
  2133. !info.GetString("QT_MOC_EXECUTABLE", this->MocConst_.Executable,
  2134. false) ||
  2135. !info.GetString("QT_UIC_EXECUTABLE", this->UicConst_.Executable,
  2136. false)) {
  2137. return false;
  2138. }
  2139. // -- Checks
  2140. if (!this->BaseConst_.CMakeExecutableTime.Load(
  2141. this->BaseConst_.CMakeExecutable)) {
  2142. return info.LogError(
  2143. cmStrCat("The CMake executable ",
  2144. this->MessagePath(this->BaseConst_.CMakeExecutable),
  2145. " does not exist."));
  2146. }
  2147. // -- Evaluate values
  2148. this->BaseConst_.ThreadCount =
  2149. std::min(this->BaseConst_.ThreadCount, ParallelMax);
  2150. this->WorkerPool_.SetThreadCount(this->BaseConst_.ThreadCount);
  2151. // -- Moc
  2152. if (!this->MocConst_.Executable.empty()) {
  2153. // -- Moc is enabled
  2154. this->MocConst_.Enabled = true;
  2155. // -- Temporary buffers
  2156. struct
  2157. {
  2158. std::vector<std::string> MacroNames;
  2159. std::vector<std::string> DependFilters;
  2160. } tmp;
  2161. // -- Required settings
  2162. if (!info.GetBool("MOC_RELAXED_MODE", this->MocConst_.RelaxedMode,
  2163. false) ||
  2164. !info.GetBool("MOC_PATH_PREFIX", this->MocConst_.PathPrefix, true) ||
  2165. !info.GetArray("MOC_SKIP", this->MocConst_.SkipList, false) ||
  2166. !info.GetArrayConfig("MOC_DEFINITIONS", this->MocConst_.Definitions,
  2167. false) ||
  2168. !info.GetArrayConfig("MOC_INCLUDES", this->MocConst_.IncludePaths,
  2169. false) ||
  2170. !info.GetArray("MOC_OPTIONS", this->MocConst_.OptionsExtra, false) ||
  2171. !info.GetStringConfig("MOC_COMPILATION_FILE",
  2172. this->MocConst_.CompFileAbs, true) ||
  2173. !info.GetArray("MOC_PREDEFS_CMD", this->MocConst_.PredefsCmd, false) ||
  2174. !info.GetStringConfig("MOC_PREDEFS_FILE",
  2175. this->MocConst_.PredefsFileAbs,
  2176. !this->MocConst_.PredefsCmd.empty()) ||
  2177. !info.GetArray("MOC_MACRO_NAMES", tmp.MacroNames, true) ||
  2178. !info.GetArray("MOC_DEPEND_FILTERS", tmp.DependFilters, false)) {
  2179. return false;
  2180. }
  2181. // -- Evaluate settings
  2182. for (std::string const& item : tmp.MacroNames) {
  2183. this->MocConst_.MacroFilters.emplace_back(
  2184. item, ("[\n][ \t]*{?[ \t]*" + item).append("[^a-zA-Z0-9_]"));
  2185. }
  2186. // Can moc output dependencies or do we need to setup dependency filters?
  2187. if (this->BaseConst_.QtVersion >= IntegerVersion(5, 15)) {
  2188. this->MocConst_.CanOutputDependencies = true;
  2189. } else {
  2190. Json::Value const& val = info.GetValue("MOC_DEPEND_FILTERS");
  2191. if (!val.isArray()) {
  2192. return info.LogError("MOC_DEPEND_FILTERS JSON value is not an array.");
  2193. }
  2194. Json::ArrayIndex const arraySize = val.size();
  2195. for (Json::ArrayIndex ii = 0; ii != arraySize; ++ii) {
  2196. // Test entry closure
  2197. auto testEntry = [&info, ii](bool test, cm::string_view msg) -> bool {
  2198. if (!test) {
  2199. info.LogError(
  2200. cmStrCat("MOC_DEPEND_FILTERS filter ", ii, ": ", msg));
  2201. }
  2202. return !test;
  2203. };
  2204. Json::Value const& pairVal = val[ii];
  2205. if (testEntry(pairVal.isArray(), "JSON value is not an array.") ||
  2206. testEntry(pairVal.size() == 2, "JSON array size invalid.")) {
  2207. return false;
  2208. }
  2209. Json::Value const& keyVal = pairVal[0u];
  2210. Json::Value const& expVal = pairVal[1u];
  2211. if (testEntry(keyVal.isString(),
  2212. "JSON value for keyword is not a string.") ||
  2213. testEntry(expVal.isString(),
  2214. "JSON value for regular expression is not a string.")) {
  2215. return false;
  2216. }
  2217. std::string const key = keyVal.asString();
  2218. std::string const exp = expVal.asString();
  2219. if (testEntry(!key.empty(), "Keyword is empty.") ||
  2220. testEntry(!exp.empty(), "Regular expression is empty.")) {
  2221. return false;
  2222. }
  2223. this->MocConst_.DependFilters.emplace_back(key, exp);
  2224. if (testEntry(
  2225. this->MocConst_.DependFilters.back().Exp.is_valid(),
  2226. cmStrCat("Regular expression compilation failed.\nKeyword: ",
  2227. Quoted(key), "\nExpression: ", Quoted(exp)))) {
  2228. return false;
  2229. }
  2230. }
  2231. }
  2232. // Check if moc executable exists (by reading the file time)
  2233. if (!this->MocConst_.ExecutableTime.Load(this->MocConst_.Executable)) {
  2234. return info.LogError(cmStrCat(
  2235. "The moc executable ", this->MessagePath(this->MocConst_.Executable),
  2236. " does not exist."));
  2237. }
  2238. }
  2239. // -- Uic
  2240. if (!this->UicConst_.Executable.empty()) {
  2241. // Uic is enabled
  2242. this->UicConst_.Enabled = true;
  2243. // -- Required settings
  2244. if (!info.GetArray("UIC_SKIP", this->UicConst_.SkipList, false) ||
  2245. !info.GetArray("UIC_SEARCH_PATHS", this->UicConst_.SearchPaths,
  2246. false) ||
  2247. !info.GetArrayConfig("UIC_OPTIONS", this->UicConst_.Options, false)) {
  2248. return false;
  2249. }
  2250. // .ui files
  2251. {
  2252. Json::Value const& val = info.GetValue("UIC_UI_FILES");
  2253. if (!val.isArray()) {
  2254. return info.LogError("UIC_UI_FILES JSON value is not an array.");
  2255. }
  2256. Json::ArrayIndex const arraySize = val.size();
  2257. for (Json::ArrayIndex ii = 0; ii != arraySize; ++ii) {
  2258. // Test entry closure
  2259. auto testEntry = [&info, ii](bool test, cm::string_view msg) -> bool {
  2260. if (!test) {
  2261. info.LogError(cmStrCat("UIC_UI_FILES entry ", ii, ": ", msg));
  2262. }
  2263. return !test;
  2264. };
  2265. Json::Value const& entry = val[ii];
  2266. if (testEntry(entry.isArray(), "JSON value is not an array.") ||
  2267. testEntry(entry.size() == 2, "JSON array size invalid.")) {
  2268. return false;
  2269. }
  2270. Json::Value const& entryName = entry[0u];
  2271. Json::Value const& entryOptions = entry[1u];
  2272. if (testEntry(entryName.isString(),
  2273. "JSON value for name is not a string.") ||
  2274. testEntry(entryOptions.isArray(),
  2275. "JSON value for options is not an array.")) {
  2276. return false;
  2277. }
  2278. auto& uiFile = this->UicConst_.UiFiles[entryName.asString()];
  2279. InfoT::GetJsonArray(uiFile.Options, entryOptions);
  2280. }
  2281. }
  2282. // -- Evaluate settings
  2283. // Check if uic executable exists (by reading the file time)
  2284. if (!this->UicConst_.ExecutableTime.Load(this->UicConst_.Executable)) {
  2285. return info.LogError(cmStrCat(
  2286. "The uic executable ", this->MessagePath(this->UicConst_.Executable),
  2287. " does not exist."));
  2288. }
  2289. }
  2290. // -- Headers
  2291. {
  2292. Json::Value const& val = info.GetValue("HEADERS");
  2293. if (!val.isArray()) {
  2294. return info.LogError("HEADERS JSON value is not an array.");
  2295. }
  2296. Json::ArrayIndex const arraySize = val.size();
  2297. for (Json::ArrayIndex ii = 0; ii != arraySize; ++ii) {
  2298. // Test entry closure
  2299. auto testEntry = [&info, ii](bool test, cm::string_view msg) -> bool {
  2300. if (!test) {
  2301. info.LogError(cmStrCat("HEADERS entry ", ii, ": ", msg));
  2302. }
  2303. return !test;
  2304. };
  2305. Json::Value const& entry = val[ii];
  2306. if (testEntry(entry.isArray(), "JSON value is not an array.") ||
  2307. testEntry(entry.size() == 4, "JSON array size invalid.")) {
  2308. return false;
  2309. }
  2310. Json::Value const& entryName = entry[0u];
  2311. Json::Value const& entryFlags = entry[1u];
  2312. Json::Value const& entryBuild = entry[2u];
  2313. Json::Value const& entryConfigs = entry[3u];
  2314. if (testEntry(entryName.isString(),
  2315. "JSON value for name is not a string.") ||
  2316. testEntry(entryFlags.isString(),
  2317. "JSON value for flags is not a string.") ||
  2318. testEntry(entryConfigs.isNull() || entryConfigs.isArray(),
  2319. "JSON value for configs is not null or array.") ||
  2320. testEntry(entryBuild.isString(),
  2321. "JSON value for build path is not a string.")) {
  2322. return false;
  2323. }
  2324. std::string name = entryName.asString();
  2325. std::string flags = entryFlags.asString();
  2326. std::string build = entryBuild.asString();
  2327. if (testEntry(flags.size() == 2, "Invalid flags string size")) {
  2328. return false;
  2329. }
  2330. if (entryConfigs.isArray()) {
  2331. bool configFound = false;
  2332. Json::ArrayIndex const configArraySize = entryConfigs.size();
  2333. for (Json::ArrayIndex ci = 0; ci != configArraySize; ++ci) {
  2334. Json::Value const& config = entryConfigs[ci];
  2335. if (testEntry(config.isString(),
  2336. "JSON value in config array is not a string.")) {
  2337. return false;
  2338. }
  2339. configFound = configFound || config.asString() == this->InfoConfig();
  2340. }
  2341. if (!configFound) {
  2342. continue;
  2343. }
  2344. }
  2345. cmFileTime fileTime;
  2346. if (!fileTime.Load(name)) {
  2347. return info.LogError(cmStrCat(
  2348. "The header file ", this->MessagePath(name), " does not exist."));
  2349. }
  2350. SourceFileHandleT sourceHandle = std::make_shared<SourceFileT>(name);
  2351. sourceHandle->FileTime = fileTime;
  2352. sourceHandle->IsHeader = true;
  2353. sourceHandle->Moc = (flags[0] == 'M');
  2354. sourceHandle->Uic = (flags[1] == 'U');
  2355. if (sourceHandle->Moc && this->MocConst().Enabled) {
  2356. if (build.empty()) {
  2357. return info.LogError(
  2358. cmStrCat("Header file ", ii, " build path is empty"));
  2359. }
  2360. sourceHandle->BuildPath = std::move(build);
  2361. }
  2362. this->BaseEval().Headers.emplace(std::move(name),
  2363. std::move(sourceHandle));
  2364. }
  2365. }
  2366. // -- Sources
  2367. {
  2368. Json::Value const& val = info.GetValue("SOURCES");
  2369. if (!val.isArray()) {
  2370. return info.LogError("SOURCES JSON value is not an array.");
  2371. }
  2372. Json::ArrayIndex const arraySize = val.size();
  2373. for (Json::ArrayIndex ii = 0; ii != arraySize; ++ii) {
  2374. // Test entry closure
  2375. auto testEntry = [&info, ii](bool test, cm::string_view msg) -> bool {
  2376. if (!test) {
  2377. info.LogError(cmStrCat("SOURCES entry ", ii, ": ", msg));
  2378. }
  2379. return !test;
  2380. };
  2381. Json::Value const& entry = val[ii];
  2382. if (testEntry(entry.isArray(), "JSON value is not an array.") ||
  2383. testEntry(entry.size() == 3, "JSON array size invalid.")) {
  2384. return false;
  2385. }
  2386. Json::Value const& entryName = entry[0u];
  2387. Json::Value const& entryFlags = entry[1u];
  2388. Json::Value const& entryConfigs = entry[2u];
  2389. if (testEntry(entryName.isString(),
  2390. "JSON value for name is not a string.") ||
  2391. testEntry(entryFlags.isString(),
  2392. "JSON value for flags is not a string.") ||
  2393. testEntry(entryConfigs.isNull() || entryConfigs.isArray(),
  2394. "JSON value for configs is not null or array.")) {
  2395. return false;
  2396. }
  2397. std::string name = entryName.asString();
  2398. std::string flags = entryFlags.asString();
  2399. if (testEntry(flags.size() == 2, "Invalid flags string size")) {
  2400. return false;
  2401. }
  2402. if (entryConfigs.isArray()) {
  2403. bool configFound = false;
  2404. Json::ArrayIndex const configArraySize = entryConfigs.size();
  2405. for (Json::ArrayIndex ci = 0; ci != configArraySize; ++ci) {
  2406. Json::Value const& config = entryConfigs[ci];
  2407. if (testEntry(config.isString(),
  2408. "JSON value in config array is not a string.")) {
  2409. return false;
  2410. }
  2411. configFound = configFound || config.asString() == this->InfoConfig();
  2412. }
  2413. if (!configFound) {
  2414. continue;
  2415. }
  2416. }
  2417. cmFileTime fileTime;
  2418. if (!fileTime.Load(name)) {
  2419. return info.LogError(cmStrCat(
  2420. "The source file ", this->MessagePath(name), " does not exist."));
  2421. }
  2422. SourceFileHandleT sourceHandle = std::make_shared<SourceFileT>(name);
  2423. sourceHandle->FileTime = fileTime;
  2424. sourceHandle->IsHeader = false;
  2425. sourceHandle->Moc = (flags[0] == 'M');
  2426. sourceHandle->Uic = (flags[1] == 'U');
  2427. this->BaseEval().Sources.emplace(std::move(name),
  2428. std::move(sourceHandle));
  2429. }
  2430. }
  2431. // -- Init derived information
  2432. // Moc variables
  2433. if (this->MocConst().Enabled) {
  2434. // Compose moc includes list
  2435. {
  2436. // Compute framework paths
  2437. std::set<std::string> frameworkPaths;
  2438. for (std::string const& path : this->MocConst().IncludePaths) {
  2439. // Extract framework path
  2440. if (cmHasLiteralSuffix(path, ".framework/Headers")) {
  2441. // Go up twice to get to the framework root
  2442. std::vector<std::string> pathComponents;
  2443. cmSystemTools::SplitPath(path, pathComponents);
  2444. frameworkPaths.emplace(cmSystemTools::JoinPath(
  2445. pathComponents.begin(), pathComponents.end() - 2));
  2446. }
  2447. }
  2448. // Reserve options
  2449. this->MocConst_.OptionsIncludes.reserve(
  2450. this->MocConst().IncludePaths.size() + frameworkPaths.size() * 2);
  2451. // Append includes
  2452. for (std::string const& path : this->MocConst().IncludePaths) {
  2453. this->MocConst_.OptionsIncludes.emplace_back("-I" + path);
  2454. }
  2455. // Append framework includes
  2456. for (std::string const& path : frameworkPaths) {
  2457. this->MocConst_.OptionsIncludes.emplace_back("-F");
  2458. this->MocConst_.OptionsIncludes.push_back(path);
  2459. }
  2460. }
  2461. // Compose moc definitions list
  2462. {
  2463. this->MocConst_.OptionsDefinitions.reserve(
  2464. this->MocConst().Definitions.size());
  2465. for (std::string const& def : this->MocConst().Definitions) {
  2466. this->MocConst_.OptionsDefinitions.emplace_back("-D" + def);
  2467. }
  2468. }
  2469. }
  2470. return true;
  2471. }
  2472. template <class JOBTYPE>
  2473. void cmQtAutoMocUicT::CreateParseJobs(SourceFileMapT const& sourceMap)
  2474. {
  2475. cmFileTime const parseCacheTime = this->BaseEval().ParseCacheTime;
  2476. ParseCacheT& parseCache = this->BaseEval().ParseCache;
  2477. for (const auto& src : sourceMap) {
  2478. // Get or create the file parse data reference
  2479. ParseCacheT::GetOrInsertT cacheEntry = parseCache.GetOrInsert(src.first);
  2480. src.second->ParseData = std::move(cacheEntry.first);
  2481. // Create a parse job if the cache file was missing or is older
  2482. if (cacheEntry.second || src.second->FileTime.Newer(parseCacheTime)) {
  2483. this->BaseEval().ParseCacheChanged = true;
  2484. this->WorkerPool().EmplaceJob<JOBTYPE>(src.second);
  2485. }
  2486. }
  2487. }
  2488. /** Concurrently callable implementation of cmSystemTools::CollapseFullPath */
  2489. std::string cmQtAutoMocUicT::CollapseFullPathTS(std::string const& path) const
  2490. {
  2491. std::lock_guard<std::mutex> guard(this->CMakeLibMutex_);
  2492. #if defined(__NVCOMPILER)
  2493. static_cast<void>(guard); // convince compiler var is used
  2494. #endif
  2495. return cmSystemTools::CollapseFullPath(path,
  2496. this->ProjectDirs().CurrentSource);
  2497. }
  2498. void cmQtAutoMocUicT::InitJobs()
  2499. {
  2500. // Add moc_predefs.h job
  2501. if (this->MocConst().Enabled && !this->MocConst().PredefsCmd.empty()) {
  2502. this->WorkerPool().EmplaceJob<JobMocPredefsT>();
  2503. }
  2504. // Add header parse jobs
  2505. this->CreateParseJobs<JobParseHeaderT>(this->BaseEval().Headers);
  2506. // Add source parse jobs
  2507. this->CreateParseJobs<JobParseSourceT>(this->BaseEval().Sources);
  2508. // Add parse cache evaluations jobs
  2509. {
  2510. // Add a fence job to ensure all parsing has finished
  2511. this->WorkerPool().EmplaceJob<JobFenceT>();
  2512. if (this->MocConst().Enabled) {
  2513. this->WorkerPool().EmplaceJob<JobEvalCacheMocT>();
  2514. }
  2515. if (this->UicConst().Enabled) {
  2516. this->WorkerPool().EmplaceJob<JobEvalCacheUicT>();
  2517. }
  2518. // Add evaluate job
  2519. this->WorkerPool().EmplaceJob<JobEvalCacheFinishT>();
  2520. }
  2521. }
  2522. bool cmQtAutoMocUicT::Process()
  2523. {
  2524. this->SettingsFileRead();
  2525. this->ParseCacheRead();
  2526. if (!this->CreateDirectories()) {
  2527. return false;
  2528. }
  2529. this->InitJobs();
  2530. if (!this->WorkerPool_.Process(this)) {
  2531. return false;
  2532. }
  2533. if (this->JobError_) {
  2534. return false;
  2535. }
  2536. if (!this->ParseCacheWrite()) {
  2537. return false;
  2538. }
  2539. if (!this->SettingsFileWrite()) {
  2540. return false;
  2541. }
  2542. return true;
  2543. }
  2544. void cmQtAutoMocUicT::SettingsFileRead()
  2545. {
  2546. // Compose current settings strings
  2547. {
  2548. cmCryptoHash cryptoHash(cmCryptoHash::AlgoSHA256);
  2549. auto cha = [&cryptoHash](cm::string_view value) {
  2550. cryptoHash.Append(value);
  2551. cryptoHash.Append(";");
  2552. };
  2553. if (this->MocConst_.Enabled) {
  2554. cryptoHash.Initialize();
  2555. cha(this->MocConst().Executable);
  2556. for (auto const& item : this->MocConst().OptionsDefinitions) {
  2557. cha(item);
  2558. }
  2559. for (auto const& item : this->MocConst().OptionsIncludes) {
  2560. cha(item);
  2561. }
  2562. for (auto const& item : this->MocConst().OptionsExtra) {
  2563. cha(item);
  2564. }
  2565. for (auto const& item : this->MocConst().PredefsCmd) {
  2566. cha(item);
  2567. }
  2568. for (auto const& filter : this->MocConst().DependFilters) {
  2569. cha(filter.Key);
  2570. }
  2571. for (auto const& filter : this->MocConst().MacroFilters) {
  2572. cha(filter.Key);
  2573. }
  2574. this->SettingsStringMoc_ = cryptoHash.FinalizeHex();
  2575. }
  2576. if (this->UicConst().Enabled) {
  2577. cryptoHash.Initialize();
  2578. cha(this->UicConst().Executable);
  2579. std::for_each(this->UicConst().Options.begin(),
  2580. this->UicConst().Options.end(), cha);
  2581. for (const auto& item : this->UicConst().UiFiles) {
  2582. cha(item.first);
  2583. auto const& opts = item.second.Options;
  2584. std::for_each(opts.begin(), opts.end(), cha);
  2585. }
  2586. this->SettingsStringUic_ = cryptoHash.FinalizeHex();
  2587. }
  2588. }
  2589. // Read old settings and compare
  2590. {
  2591. std::string content;
  2592. if (cmQtAutoGenerator::FileRead(content, this->SettingsFile_)) {
  2593. if (this->MocConst().Enabled) {
  2594. if (this->SettingsStringMoc_ != SettingsFind(content, "moc")) {
  2595. this->MocConst_.SettingsChanged = true;
  2596. }
  2597. }
  2598. if (this->UicConst().Enabled) {
  2599. if (this->SettingsStringUic_ != SettingsFind(content, "uic")) {
  2600. this->UicConst_.SettingsChanged = true;
  2601. }
  2602. }
  2603. // In case any setting changed remove the old settings file.
  2604. // This triggers a full rebuild on the next run if the current
  2605. // build is aborted before writing the current settings in the end.
  2606. if (this->MocConst().SettingsChanged ||
  2607. this->UicConst().SettingsChanged) {
  2608. cmSystemTools::RemoveFile(this->SettingsFile_);
  2609. }
  2610. } else {
  2611. // Settings file read failed
  2612. if (this->MocConst().Enabled) {
  2613. this->MocConst_.SettingsChanged = true;
  2614. }
  2615. if (this->UicConst().Enabled) {
  2616. this->UicConst_.SettingsChanged = true;
  2617. }
  2618. }
  2619. }
  2620. }
  2621. bool cmQtAutoMocUicT::SettingsFileWrite()
  2622. {
  2623. // Only write if any setting changed
  2624. if (this->MocConst().SettingsChanged || this->UicConst().SettingsChanged) {
  2625. if (this->Log().Verbose()) {
  2626. this->Log().Info(GenT::GEN,
  2627. cmStrCat("Writing the settings file ",
  2628. this->MessagePath(this->SettingsFile_)));
  2629. }
  2630. // Compose settings file content
  2631. std::string content;
  2632. {
  2633. auto SettingAppend = [&content](cm::string_view key,
  2634. cm::string_view value) {
  2635. if (!value.empty()) {
  2636. content += cmStrCat(key, ':', value, '\n');
  2637. }
  2638. };
  2639. SettingAppend("moc", this->SettingsStringMoc_);
  2640. SettingAppend("uic", this->SettingsStringUic_);
  2641. }
  2642. // Write settings file
  2643. std::string error;
  2644. if (!cmQtAutoGenerator::FileWrite(this->SettingsFile_, content, &error)) {
  2645. this->Log().Error(GenT::GEN,
  2646. cmStrCat("Writing the settings file ",
  2647. this->MessagePath(this->SettingsFile_),
  2648. " failed.\n", error));
  2649. // Remove old settings file to trigger a full rebuild on the next run
  2650. cmSystemTools::RemoveFile(this->SettingsFile_);
  2651. return false;
  2652. }
  2653. }
  2654. return true;
  2655. }
  2656. void cmQtAutoMocUicT::ParseCacheRead()
  2657. {
  2658. cm::string_view reason;
  2659. // Don't read the cache if it is invalid
  2660. if (!this->BaseEval().ParseCacheTime.Load(
  2661. this->BaseConst().ParseCacheFile)) {
  2662. reason = "Refreshing parse cache because it doesn't exist.";
  2663. } else if (this->MocConst().SettingsChanged ||
  2664. this->UicConst().SettingsChanged) {
  2665. reason = "Refreshing parse cache because the settings changed.";
  2666. } else if (this->BaseEval().ParseCacheTime.Older(
  2667. this->BaseConst().CMakeExecutableTime)) {
  2668. reason =
  2669. "Refreshing parse cache because it is older than the CMake executable.";
  2670. }
  2671. if (!reason.empty()) {
  2672. // Don't read but refresh the complete parse cache
  2673. if (this->Log().Verbose()) {
  2674. this->Log().Info(GenT::GEN, reason);
  2675. }
  2676. this->BaseEval().ParseCacheChanged = true;
  2677. } else {
  2678. // Read parse cache
  2679. this->BaseEval().ParseCache.ReadFromFile(this->BaseConst().ParseCacheFile);
  2680. }
  2681. }
  2682. bool cmQtAutoMocUicT::ParseCacheWrite()
  2683. {
  2684. if (this->BaseEval().ParseCacheChanged) {
  2685. if (this->Log().Verbose()) {
  2686. this->Log().Info(
  2687. GenT::GEN,
  2688. cmStrCat("Writing the parse cache file ",
  2689. this->MessagePath(this->BaseConst().ParseCacheFile)));
  2690. }
  2691. if (!this->BaseEval().ParseCache.WriteToFile(
  2692. this->BaseConst().ParseCacheFile)) {
  2693. this->Log().Error(
  2694. GenT::GEN,
  2695. cmStrCat("Writing the parse cache file ",
  2696. this->MessagePath(this->BaseConst().ParseCacheFile),
  2697. " failed."));
  2698. return false;
  2699. }
  2700. }
  2701. return true;
  2702. }
  2703. bool cmQtAutoMocUicT::CreateDirectories()
  2704. {
  2705. // Create AUTOGEN include directory
  2706. if (!cmSystemTools::MakeDirectory(this->BaseConst().AutogenIncludeDir)) {
  2707. this->Log().Error(
  2708. GenT::GEN,
  2709. cmStrCat("Creating the AUTOGEN include directory ",
  2710. this->MessagePath(this->BaseConst().AutogenIncludeDir),
  2711. " failed."));
  2712. return false;
  2713. }
  2714. return true;
  2715. }
  2716. std::vector<std::string> cmQtAutoMocUicT::dependenciesFromDepFile(
  2717. const char* filePath)
  2718. {
  2719. std::lock_guard<std::mutex> guard(this->CMakeLibMutex_);
  2720. #if defined(__NVCOMPILER)
  2721. static_cast<void>(guard); // convince compiler var is used
  2722. #endif
  2723. auto const content = cmReadGccDepfile(filePath);
  2724. if (!content || content->empty()) {
  2725. return {};
  2726. }
  2727. // Moc outputs a depfile with exactly one rule.
  2728. // Discard the rule and return the dependencies.
  2729. return content->front().paths;
  2730. }
  2731. void cmQtAutoMocUicT::Abort(bool error)
  2732. {
  2733. if (error) {
  2734. this->JobError_.store(true);
  2735. }
  2736. this->WorkerPool_.Abort();
  2737. }
  2738. std::string cmQtAutoMocUicT::AbsoluteBuildPath(
  2739. cm::string_view relativePath) const
  2740. {
  2741. return cmStrCat(this->BaseConst().AutogenBuildDir, '/', relativePath);
  2742. }
  2743. std::string cmQtAutoMocUicT::AbsoluteIncludePath(
  2744. cm::string_view relativePath) const
  2745. {
  2746. return cmStrCat(this->BaseConst().AutogenIncludeDir, '/', relativePath);
  2747. }
  2748. } // End of unnamed namespace
  2749. bool cmQtAutoMocUic(cm::string_view infoFile, cm::string_view config)
  2750. {
  2751. return cmQtAutoMocUicT().Run(infoFile, config);
  2752. }