cmQtAutoMocUic.cxx 95 KB

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