cmQtAutoMocUic.cxx 95 KB

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