cmQtAutoMocUic.cxx 88 KB

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