cmQtAutoMocUic.cxx 87 KB

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