cmQtAutoGeneratorMocUic.cxx 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037
  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 "cmQtAutoGeneratorMocUic.h"
  4. #include <algorithm>
  5. #include <array>
  6. #include <cstddef>
  7. #include <functional>
  8. #include <list>
  9. #include <memory>
  10. #include <set>
  11. #include <sstream>
  12. #include <utility>
  13. #include "cmAlgorithms.h"
  14. #include "cmCryptoHash.h"
  15. #include "cmMakefile.h"
  16. #include "cmQtAutoGen.h"
  17. #include "cmSystemTools.h"
  18. #include "cmake.h"
  19. #if defined(__APPLE__)
  20. # include <unistd.h>
  21. #endif
  22. // -- Class methods
  23. std::string cmQtAutoGeneratorMocUic::BaseSettingsT::AbsoluteBuildPath(
  24. std::string const& relativePath) const
  25. {
  26. return FileSys->CollapseCombinedPath(AutogenBuildDir, relativePath);
  27. }
  28. /**
  29. * @brief Tries to find the header file to the given file base path by
  30. * appending different header extensions
  31. * @return True on success
  32. */
  33. bool cmQtAutoGeneratorMocUic::BaseSettingsT::FindHeader(
  34. std::string& header, std::string const& testBasePath) const
  35. {
  36. for (std::string const& ext : HeaderExtensions) {
  37. std::string testFilePath(testBasePath);
  38. testFilePath.push_back('.');
  39. testFilePath += ext;
  40. if (FileSys->FileExists(testFilePath)) {
  41. header = testFilePath;
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. bool cmQtAutoGeneratorMocUic::MocSettingsT::skipped(
  48. std::string const& fileName) const
  49. {
  50. return (!Enabled || (SkipList.find(fileName) != SkipList.end()));
  51. }
  52. /**
  53. * @brief Returns the first relevant Qt macro name found in the given C++ code
  54. * @return The name of the Qt macro or an empty string
  55. */
  56. std::string cmQtAutoGeneratorMocUic::MocSettingsT::FindMacro(
  57. std::string const& content) const
  58. {
  59. for (KeyExpT const& filter : MacroFilters) {
  60. // Run a simple find string operation before the expensive
  61. // regular expression check
  62. if (content.find(filter.Key) != std::string::npos) {
  63. cmsys::RegularExpressionMatch match;
  64. if (filter.Exp.find(content.c_str(), match)) {
  65. // Return macro name on demand
  66. return filter.Key;
  67. }
  68. }
  69. }
  70. return std::string();
  71. }
  72. std::string cmQtAutoGeneratorMocUic::MocSettingsT::MacrosString() const
  73. {
  74. std::string res;
  75. const auto itB = MacroFilters.cbegin();
  76. const auto itE = MacroFilters.cend();
  77. const auto itL = itE - 1;
  78. auto itC = itB;
  79. for (; itC != itE; ++itC) {
  80. // Separator
  81. if (itC != itB) {
  82. if (itC != itL) {
  83. res += ", ";
  84. } else {
  85. res += " or ";
  86. }
  87. }
  88. // Key
  89. res += itC->Key;
  90. }
  91. return res;
  92. }
  93. std::string cmQtAutoGeneratorMocUic::MocSettingsT::FindIncludedFile(
  94. std::string const& sourcePath, std::string const& includeString) const
  95. {
  96. // Search in vicinity of the source
  97. {
  98. std::string testPath = sourcePath;
  99. testPath += includeString;
  100. if (FileSys->FileExists(testPath)) {
  101. return FileSys->GetRealPath(testPath);
  102. }
  103. }
  104. // Search in include directories
  105. for (std::string const& path : IncludePaths) {
  106. std::string fullPath = path;
  107. fullPath.push_back('/');
  108. fullPath += includeString;
  109. if (FileSys->FileExists(fullPath)) {
  110. return FileSys->GetRealPath(fullPath);
  111. }
  112. }
  113. // Return empty string
  114. return std::string();
  115. }
  116. void cmQtAutoGeneratorMocUic::MocSettingsT::FindDependencies(
  117. std::string const& content, std::set<std::string>& depends) const
  118. {
  119. if (!DependFilters.empty() && !content.empty()) {
  120. for (KeyExpT const& filter : DependFilters) {
  121. // Run a simple find string check
  122. if (content.find(filter.Key) != std::string::npos) {
  123. // Run the expensive regular expression check loop
  124. const char* contentChars = content.c_str();
  125. cmsys::RegularExpressionMatch match;
  126. while (filter.Exp.find(contentChars, match)) {
  127. {
  128. std::string dep = match.match(1);
  129. if (!dep.empty()) {
  130. depends.emplace(std::move(dep));
  131. }
  132. }
  133. contentChars += match.end();
  134. }
  135. }
  136. }
  137. }
  138. }
  139. bool cmQtAutoGeneratorMocUic::UicSettingsT::skipped(
  140. std::string const& fileName) const
  141. {
  142. return (!Enabled || (SkipList.find(fileName) != SkipList.end()));
  143. }
  144. void cmQtAutoGeneratorMocUic::JobParseT::Process(WorkerT& wrk)
  145. {
  146. if (AutoMoc && Header) {
  147. // Don't parse header for moc if the file is included by a source already
  148. if (wrk.Gen().ParallelMocIncluded(FileName)) {
  149. AutoMoc = false;
  150. }
  151. }
  152. if (AutoMoc || AutoUic) {
  153. std::string error;
  154. MetaT meta;
  155. if (wrk.FileSys().FileRead(meta.Content, FileName, &error)) {
  156. if (!meta.Content.empty()) {
  157. meta.FileDir = wrk.FileSys().SubDirPrefix(FileName);
  158. meta.FileBase =
  159. wrk.FileSys().GetFilenameWithoutLastExtension(FileName);
  160. bool success = true;
  161. if (AutoMoc) {
  162. if (Header) {
  163. success = ParseMocHeader(wrk, meta);
  164. } else {
  165. success = ParseMocSource(wrk, meta);
  166. }
  167. }
  168. if (AutoUic && success) {
  169. ParseUic(wrk, meta);
  170. }
  171. } else {
  172. wrk.LogFileWarning(GenT::GEN, FileName, "The source file is empty");
  173. }
  174. } else {
  175. wrk.LogFileError(GenT::GEN, FileName,
  176. "Could not read the file: " + error);
  177. }
  178. }
  179. }
  180. bool cmQtAutoGeneratorMocUic::JobParseT::ParseMocSource(WorkerT& wrk,
  181. MetaT const& meta)
  182. {
  183. struct JobPre
  184. {
  185. bool self; // source file is self
  186. bool underscore; // "moc_" style include
  187. std::string SourceFile;
  188. std::string IncludeString;
  189. };
  190. struct MocInclude
  191. {
  192. std::string Inc; // full include string
  193. std::string Dir; // include string directory
  194. std::string Base; // include string file base
  195. };
  196. // Check if this source file contains a relevant macro
  197. std::string const ownMacro = wrk.Moc().FindMacro(meta.Content);
  198. // Extract moc includes from file
  199. std::deque<MocInclude> mocIncsUsc;
  200. std::deque<MocInclude> mocIncsDot;
  201. {
  202. if (meta.Content.find("moc") != std::string::npos) {
  203. const char* contentChars = meta.Content.c_str();
  204. cmsys::RegularExpressionMatch match;
  205. while (wrk.Moc().RegExpInclude.find(contentChars, match)) {
  206. std::string incString = match.match(2);
  207. std::string incDir(wrk.FileSys().SubDirPrefix(incString));
  208. std::string incBase =
  209. wrk.FileSys().GetFilenameWithoutLastExtension(incString);
  210. if (cmHasLiteralPrefix(incBase, "moc_")) {
  211. // moc_<BASE>.cxx
  212. // Remove the moc_ part from the base name
  213. mocIncsUsc.emplace_back(MocInclude{
  214. std::move(incString), std::move(incDir), incBase.substr(4) });
  215. } else {
  216. // <BASE>.moc
  217. mocIncsDot.emplace_back(MocInclude{
  218. std::move(incString), std::move(incDir), std::move(incBase) });
  219. }
  220. // Forward content pointer
  221. contentChars += match.end();
  222. }
  223. }
  224. }
  225. // Check if there is anything to do
  226. if (ownMacro.empty() && mocIncsUsc.empty() && mocIncsDot.empty()) {
  227. return true;
  228. }
  229. bool ownDotMocIncluded = false;
  230. bool ownMocUscIncluded = false;
  231. std::deque<JobPre> jobs;
  232. // Process moc_<BASE>.cxx includes
  233. for (const MocInclude& mocInc : mocIncsUsc) {
  234. std::string const header =
  235. MocFindIncludedHeader(wrk, meta.FileDir, mocInc.Dir + mocInc.Base);
  236. if (!header.empty()) {
  237. // Check if header is skipped
  238. if (wrk.Moc().skipped(header)) {
  239. continue;
  240. }
  241. // Register moc job
  242. const bool ownMoc = (mocInc.Base == meta.FileBase);
  243. jobs.emplace_back(JobPre{ ownMoc, true, header, mocInc.Inc });
  244. // Store meta information for relaxed mode
  245. if (ownMoc) {
  246. ownMocUscIncluded = true;
  247. }
  248. } else {
  249. {
  250. std::string emsg = "The file includes the moc file ";
  251. emsg += Quoted(mocInc.Inc);
  252. emsg += ", but the header ";
  253. emsg += Quoted(MocStringHeaders(wrk, mocInc.Base));
  254. emsg += " could not be found.";
  255. wrk.LogFileError(GenT::MOC, FileName, emsg);
  256. }
  257. return false;
  258. }
  259. }
  260. // Process <BASE>.moc includes
  261. for (const MocInclude& mocInc : mocIncsDot) {
  262. const bool ownMoc = (mocInc.Base == meta.FileBase);
  263. if (wrk.Moc().RelaxedMode) {
  264. // Relaxed mode
  265. if (!ownMacro.empty() && ownMoc) {
  266. // Add self
  267. jobs.emplace_back(JobPre{ ownMoc, false, FileName, mocInc.Inc });
  268. ownDotMocIncluded = true;
  269. } else {
  270. // In relaxed mode try to find a header instead but issue a warning.
  271. // This is for KDE4 compatibility
  272. std::string const header =
  273. MocFindIncludedHeader(wrk, meta.FileDir, mocInc.Dir + mocInc.Base);
  274. if (!header.empty()) {
  275. // Check if header is skipped
  276. if (wrk.Moc().skipped(header)) {
  277. continue;
  278. }
  279. // Register moc job
  280. jobs.emplace_back(JobPre{ ownMoc, false, header, mocInc.Inc });
  281. if (ownMacro.empty()) {
  282. if (ownMoc) {
  283. std::string emsg = "The file includes the moc file ";
  284. emsg += Quoted(mocInc.Inc);
  285. emsg += ", but does not contain a ";
  286. emsg += wrk.Moc().MacrosString();
  287. emsg += " macro.\nRunning moc on\n ";
  288. emsg += Quoted(header);
  289. emsg += "!\nBetter include ";
  290. emsg += Quoted("moc_" + mocInc.Base + ".cpp");
  291. emsg += " for a compatibility with strict mode.\n"
  292. "(CMAKE_AUTOMOC_RELAXED_MODE warning)\n";
  293. wrk.LogFileWarning(GenT::MOC, FileName, emsg);
  294. } else {
  295. std::string emsg = "The file includes the moc file ";
  296. emsg += Quoted(mocInc.Inc);
  297. emsg += " instead of ";
  298. emsg += Quoted("moc_" + mocInc.Base + ".cpp");
  299. emsg += ".\nRunning moc on\n ";
  300. emsg += Quoted(header);
  301. emsg += "!\nBetter include ";
  302. emsg += Quoted("moc_" + mocInc.Base + ".cpp");
  303. emsg += " for compatibility with strict mode.\n"
  304. "(CMAKE_AUTOMOC_RELAXED_MODE warning)\n";
  305. wrk.LogFileWarning(GenT::MOC, FileName, emsg);
  306. }
  307. }
  308. } else {
  309. {
  310. std::string emsg = "The file includes the moc file ";
  311. emsg += Quoted(mocInc.Inc);
  312. emsg += ", which seems to be the moc file from a different "
  313. "source file.\nCMAKE_AUTOMOC_RELAXED_MODE: Also a "
  314. "matching header ";
  315. emsg += Quoted(MocStringHeaders(wrk, mocInc.Base));
  316. emsg += " could not be found.";
  317. wrk.LogFileError(GenT::MOC, FileName, emsg);
  318. }
  319. return false;
  320. }
  321. }
  322. } else {
  323. // Strict mode
  324. if (ownMoc) {
  325. // Include self
  326. jobs.emplace_back(JobPre{ ownMoc, false, FileName, mocInc.Inc });
  327. ownDotMocIncluded = true;
  328. // Accept but issue a warning if moc isn't required
  329. if (ownMacro.empty()) {
  330. std::string emsg = "The file includes the moc file ";
  331. emsg += Quoted(mocInc.Inc);
  332. emsg += ", but does not contain a ";
  333. emsg += wrk.Moc().MacrosString();
  334. emsg += " macro.";
  335. wrk.LogFileWarning(GenT::MOC, FileName, emsg);
  336. }
  337. } else {
  338. // Don't allow <BASE>.moc include other than self in strict mode
  339. {
  340. std::string emsg = "The file includes the moc file ";
  341. emsg += Quoted(mocInc.Inc);
  342. emsg += ", which seems to be the moc file from a different "
  343. "source file.\nThis is not supported. Include ";
  344. emsg += Quoted(meta.FileBase + ".moc");
  345. emsg += " to run moc on this source file.";
  346. wrk.LogFileError(GenT::MOC, FileName, emsg);
  347. }
  348. return false;
  349. }
  350. }
  351. }
  352. if (!ownMacro.empty() && !ownDotMocIncluded) {
  353. // In this case, check whether the scanned file itself contains a
  354. // Q_OBJECT.
  355. // If this is the case, the moc_foo.cpp should probably be generated from
  356. // foo.cpp instead of foo.h, because otherwise it won't build.
  357. // But warn, since this is not how it is supposed to be used.
  358. // This is for KDE4 compatibility.
  359. if (wrk.Moc().RelaxedMode && ownMocUscIncluded) {
  360. JobPre uscJobPre;
  361. // Remove underscore job request
  362. {
  363. auto itC = jobs.begin();
  364. auto itE = jobs.end();
  365. for (; itC != itE; ++itC) {
  366. JobPre& job(*itC);
  367. if (job.self && job.underscore) {
  368. uscJobPre = std::move(job);
  369. jobs.erase(itC);
  370. break;
  371. }
  372. }
  373. }
  374. // Issue a warning
  375. {
  376. std::string emsg = "The file contains a ";
  377. emsg += ownMacro;
  378. emsg += " macro, but does not include ";
  379. emsg += Quoted(meta.FileBase + ".moc");
  380. emsg += ". Instead it includes ";
  381. emsg += Quoted(uscJobPre.IncludeString);
  382. emsg += ".\nRunning moc on\n ";
  383. emsg += Quoted(FileName);
  384. emsg += "!\nBetter include ";
  385. emsg += Quoted(meta.FileBase + ".moc");
  386. emsg += " for compatibility with strict mode.\n"
  387. "(CMAKE_AUTOMOC_RELAXED_MODE warning)";
  388. wrk.LogFileWarning(GenT::MOC, FileName, emsg);
  389. }
  390. // Add own source job
  391. jobs.emplace_back(
  392. JobPre{ true, false, FileName, uscJobPre.IncludeString });
  393. } else {
  394. // Otherwise always error out since it will not compile.
  395. {
  396. std::string emsg = "The file contains a ";
  397. emsg += ownMacro;
  398. emsg += " macro, but does not include ";
  399. emsg += Quoted(meta.FileBase + ".moc");
  400. emsg += "!\nConsider to\n - add #include \"";
  401. emsg += meta.FileBase;
  402. emsg += ".moc\"\n - enable SKIP_AUTOMOC for this file";
  403. wrk.LogFileError(GenT::MOC, FileName, emsg);
  404. }
  405. return false;
  406. }
  407. }
  408. // Convert pre jobs to actual jobs
  409. for (JobPre& jobPre : jobs) {
  410. JobHandleT jobHandle(new JobMocT(std::move(jobPre.SourceFile), FileName,
  411. std::move(jobPre.IncludeString)));
  412. if (jobPre.self) {
  413. // Read dependencies from this source
  414. static_cast<JobMocT&>(*jobHandle).FindDependencies(wrk, meta.Content);
  415. }
  416. if (!wrk.Gen().ParallelJobPushMoc(jobHandle)) {
  417. return false;
  418. }
  419. }
  420. return true;
  421. }
  422. bool cmQtAutoGeneratorMocUic::JobParseT::ParseMocHeader(WorkerT& wrk,
  423. MetaT const& meta)
  424. {
  425. bool success = true;
  426. std::string const macroName = wrk.Moc().FindMacro(meta.Content);
  427. if (!macroName.empty()) {
  428. JobHandleT jobHandle(
  429. new JobMocT(std::string(FileName), std::string(), std::string()));
  430. // Read dependencies from this source
  431. static_cast<JobMocT&>(*jobHandle).FindDependencies(wrk, meta.Content);
  432. success = wrk.Gen().ParallelJobPushMoc(jobHandle);
  433. }
  434. return success;
  435. }
  436. std::string cmQtAutoGeneratorMocUic::JobParseT::MocStringHeaders(
  437. WorkerT& wrk, std::string const& fileBase) const
  438. {
  439. std::string res = fileBase;
  440. res += ".{";
  441. res += cmJoin(wrk.Base().HeaderExtensions, ",");
  442. res += "}";
  443. return res;
  444. }
  445. std::string cmQtAutoGeneratorMocUic::JobParseT::MocFindIncludedHeader(
  446. WorkerT& wrk, std::string const& includerDir, std::string const& includeBase)
  447. {
  448. std::string header;
  449. // Search in vicinity of the source
  450. if (!wrk.Base().FindHeader(header, includerDir + includeBase)) {
  451. // Search in include directories
  452. for (std::string const& path : wrk.Moc().IncludePaths) {
  453. std::string fullPath = path;
  454. fullPath.push_back('/');
  455. fullPath += includeBase;
  456. if (wrk.Base().FindHeader(header, fullPath)) {
  457. break;
  458. }
  459. }
  460. }
  461. // Sanitize
  462. if (!header.empty()) {
  463. header = wrk.FileSys().GetRealPath(header);
  464. }
  465. return header;
  466. }
  467. bool cmQtAutoGeneratorMocUic::JobParseT::ParseUic(WorkerT& wrk,
  468. MetaT const& meta)
  469. {
  470. bool success = true;
  471. if (meta.Content.find("ui_") != std::string::npos) {
  472. const char* contentChars = meta.Content.c_str();
  473. cmsys::RegularExpressionMatch match;
  474. while (wrk.Uic().RegExpInclude.find(contentChars, match)) {
  475. if (!ParseUicInclude(wrk, meta, match.match(2))) {
  476. success = false;
  477. break;
  478. }
  479. contentChars += match.end();
  480. }
  481. }
  482. return success;
  483. }
  484. bool cmQtAutoGeneratorMocUic::JobParseT::ParseUicInclude(
  485. WorkerT& wrk, MetaT const& meta, std::string&& includeString)
  486. {
  487. bool success = false;
  488. std::string uiInputFile = UicFindIncludedFile(wrk, meta, includeString);
  489. if (!uiInputFile.empty()) {
  490. if (!wrk.Uic().skipped(uiInputFile)) {
  491. JobHandleT jobHandle(new JobUicT(std::move(uiInputFile), FileName,
  492. std::move(includeString)));
  493. success = wrk.Gen().ParallelJobPushUic(jobHandle);
  494. } else {
  495. // A skipped file is successful
  496. success = true;
  497. }
  498. }
  499. return success;
  500. }
  501. std::string cmQtAutoGeneratorMocUic::JobParseT::UicFindIncludedFile(
  502. WorkerT& wrk, MetaT const& meta, std::string const& includeString)
  503. {
  504. std::string res;
  505. std::string searchFile =
  506. wrk.FileSys().GetFilenameWithoutLastExtension(includeString).substr(3);
  507. searchFile += ".ui";
  508. // Collect search paths list
  509. std::deque<std::string> testFiles;
  510. {
  511. std::string const searchPath = wrk.FileSys().SubDirPrefix(includeString);
  512. std::string searchFileFull;
  513. if (!searchPath.empty()) {
  514. searchFileFull = searchPath;
  515. searchFileFull += searchFile;
  516. }
  517. // Vicinity of the source
  518. {
  519. std::string const sourcePath = meta.FileDir;
  520. testFiles.push_back(sourcePath + searchFile);
  521. if (!searchPath.empty()) {
  522. testFiles.push_back(sourcePath + searchFileFull);
  523. }
  524. }
  525. // AUTOUIC search paths
  526. if (!wrk.Uic().SearchPaths.empty()) {
  527. for (std::string const& sPath : wrk.Uic().SearchPaths) {
  528. testFiles.push_back((sPath + "/").append(searchFile));
  529. }
  530. if (!searchPath.empty()) {
  531. for (std::string const& sPath : wrk.Uic().SearchPaths) {
  532. testFiles.push_back((sPath + "/").append(searchFileFull));
  533. }
  534. }
  535. }
  536. }
  537. // Search for the .ui file!
  538. for (std::string const& testFile : testFiles) {
  539. if (wrk.FileSys().FileExists(testFile)) {
  540. res = wrk.FileSys().GetRealPath(testFile);
  541. break;
  542. }
  543. }
  544. // Log error
  545. if (res.empty()) {
  546. std::string emsg = "Could not find ";
  547. emsg += Quoted(searchFile);
  548. emsg += " in\n";
  549. for (std::string const& testFile : testFiles) {
  550. emsg += " ";
  551. emsg += Quoted(testFile);
  552. emsg += "\n";
  553. }
  554. wrk.LogFileError(GenT::UIC, FileName, emsg);
  555. }
  556. return res;
  557. }
  558. void cmQtAutoGeneratorMocUic::JobMocPredefsT::Process(WorkerT& wrk)
  559. {
  560. // (Re)generate moc_predefs.h on demand
  561. bool generate(false);
  562. bool fileExists(wrk.FileSys().FileExists(wrk.Moc().PredefsFileAbs));
  563. if (!fileExists) {
  564. if (wrk.Log().Verbose()) {
  565. std::string reason = "Generating ";
  566. reason += Quoted(wrk.Moc().PredefsFileRel);
  567. reason += " because it doesn't exist";
  568. wrk.LogInfo(GenT::MOC, reason);
  569. }
  570. generate = true;
  571. } else if (wrk.Moc().SettingsChanged) {
  572. if (wrk.Log().Verbose()) {
  573. std::string reason = "Generating ";
  574. reason += Quoted(wrk.Moc().PredefsFileRel);
  575. reason += " because the settings changed.";
  576. wrk.LogInfo(GenT::MOC, reason);
  577. }
  578. generate = true;
  579. }
  580. if (generate) {
  581. ProcessResultT result;
  582. {
  583. // Compose command
  584. std::vector<std::string> cmd = wrk.Moc().PredefsCmd;
  585. // Add includes
  586. cmd.insert(cmd.end(), wrk.Moc().Includes.begin(),
  587. wrk.Moc().Includes.end());
  588. // Add definitions
  589. for (std::string const& def : wrk.Moc().Definitions) {
  590. cmd.push_back("-D" + def);
  591. }
  592. // Execute command
  593. if (!wrk.RunProcess(GenT::MOC, result, cmd)) {
  594. std::string emsg = "The content generation command for ";
  595. emsg += Quoted(wrk.Moc().PredefsFileRel);
  596. emsg += " failed.\n";
  597. emsg += result.ErrorMessage;
  598. wrk.LogCommandError(GenT::MOC, emsg, cmd, result.StdOut);
  599. }
  600. }
  601. // (Re)write predefs file only on demand
  602. if (!result.error()) {
  603. if (!fileExists ||
  604. wrk.FileSys().FileDiffers(wrk.Moc().PredefsFileAbs, result.StdOut)) {
  605. if (wrk.FileSys().FileWrite(GenT::MOC, wrk.Moc().PredefsFileAbs,
  606. result.StdOut)) {
  607. // Success
  608. } else {
  609. std::string emsg = "Writing ";
  610. emsg += Quoted(wrk.Moc().PredefsFileRel);
  611. emsg += " failed.";
  612. wrk.LogFileError(GenT::MOC, wrk.Moc().PredefsFileAbs, emsg);
  613. }
  614. } else {
  615. // Touch to update the time stamp
  616. if (wrk.Log().Verbose()) {
  617. std::string msg = "Touching ";
  618. msg += Quoted(wrk.Moc().PredefsFileRel);
  619. msg += ".";
  620. wrk.LogInfo(GenT::MOC, msg);
  621. }
  622. wrk.FileSys().Touch(wrk.Moc().PredefsFileAbs);
  623. }
  624. }
  625. }
  626. }
  627. void cmQtAutoGeneratorMocUic::JobMocT::FindDependencies(
  628. WorkerT& wrk, std::string const& content)
  629. {
  630. wrk.Moc().FindDependencies(content, Depends);
  631. DependsValid = true;
  632. }
  633. void cmQtAutoGeneratorMocUic::JobMocT::Process(WorkerT& wrk)
  634. {
  635. // Compute build file name
  636. if (!IncludeString.empty()) {
  637. BuildFile = wrk.Base().AutogenIncludeDir;
  638. BuildFile += '/';
  639. BuildFile += IncludeString;
  640. } else {
  641. // Relative build path
  642. std::string relPath = wrk.FileSys().GetFilePathChecksum(SourceFile);
  643. relPath += "/moc_";
  644. relPath += wrk.FileSys().GetFilenameWithoutLastExtension(SourceFile);
  645. // Register relative file path with duplication check
  646. relPath = wrk.Gen().ParallelMocAutoRegister(relPath);
  647. // Absolute build path
  648. if (wrk.Base().MultiConfig) {
  649. BuildFile = wrk.Base().AutogenIncludeDir;
  650. BuildFile += '/';
  651. BuildFile += relPath;
  652. } else {
  653. BuildFile = wrk.Base().AbsoluteBuildPath(relPath);
  654. }
  655. }
  656. if (UpdateRequired(wrk)) {
  657. GenerateMoc(wrk);
  658. }
  659. }
  660. bool cmQtAutoGeneratorMocUic::JobMocT::UpdateRequired(WorkerT& wrk)
  661. {
  662. bool const verbose = wrk.Gen().Log().Verbose();
  663. // Test if the build file exists
  664. if (!wrk.FileSys().FileExists(BuildFile)) {
  665. if (verbose) {
  666. std::string reason = "Generating ";
  667. reason += Quoted(BuildFile);
  668. reason += " from its source file ";
  669. reason += Quoted(SourceFile);
  670. reason += " because it doesn't exist";
  671. wrk.LogInfo(GenT::MOC, reason);
  672. }
  673. return true;
  674. }
  675. // Test if any setting changed
  676. if (wrk.Moc().SettingsChanged) {
  677. if (verbose) {
  678. std::string reason = "Generating ";
  679. reason += Quoted(BuildFile);
  680. reason += " from ";
  681. reason += Quoted(SourceFile);
  682. reason += " because the MOC settings changed";
  683. wrk.LogInfo(GenT::MOC, reason);
  684. }
  685. return true;
  686. }
  687. // Test if the moc_predefs file is newer
  688. if (!wrk.Moc().PredefsFileAbs.empty()) {
  689. bool isOlder = false;
  690. {
  691. std::string error;
  692. isOlder = wrk.FileSys().FileIsOlderThan(
  693. BuildFile, wrk.Moc().PredefsFileAbs, &error);
  694. if (!isOlder && !error.empty()) {
  695. wrk.LogError(GenT::MOC, error);
  696. return false;
  697. }
  698. }
  699. if (isOlder) {
  700. if (verbose) {
  701. std::string reason = "Generating ";
  702. reason += Quoted(BuildFile);
  703. reason += " because it's older than: ";
  704. reason += Quoted(wrk.Moc().PredefsFileAbs);
  705. wrk.LogInfo(GenT::MOC, reason);
  706. }
  707. return true;
  708. }
  709. }
  710. // Test if the source file is newer
  711. {
  712. bool isOlder = false;
  713. {
  714. std::string error;
  715. isOlder = wrk.FileSys().FileIsOlderThan(BuildFile, SourceFile, &error);
  716. if (!isOlder && !error.empty()) {
  717. wrk.LogError(GenT::MOC, error);
  718. return false;
  719. }
  720. }
  721. if (isOlder) {
  722. if (verbose) {
  723. std::string reason = "Generating ";
  724. reason += Quoted(BuildFile);
  725. reason += " because it's older than its source file ";
  726. reason += Quoted(SourceFile);
  727. wrk.LogInfo(GenT::MOC, reason);
  728. }
  729. return true;
  730. }
  731. }
  732. // Test if a dependency file is newer
  733. {
  734. // Read dependencies on demand
  735. if (!DependsValid) {
  736. std::string content;
  737. {
  738. std::string error;
  739. if (!wrk.FileSys().FileRead(content, SourceFile, &error)) {
  740. std::string emsg = "Could not read file\n ";
  741. emsg += Quoted(SourceFile);
  742. emsg += "\nrequired by moc include ";
  743. emsg += Quoted(IncludeString);
  744. emsg += " in\n ";
  745. emsg += Quoted(IncluderFile);
  746. emsg += ".\n";
  747. emsg += error;
  748. wrk.LogError(GenT::MOC, emsg);
  749. return false;
  750. }
  751. }
  752. FindDependencies(wrk, content);
  753. }
  754. // Check dependency timestamps
  755. std::string error;
  756. std::string sourceDir = wrk.FileSys().SubDirPrefix(SourceFile);
  757. for (std::string const& depFileRel : Depends) {
  758. std::string depFileAbs =
  759. wrk.Moc().FindIncludedFile(sourceDir, depFileRel);
  760. if (!depFileAbs.empty()) {
  761. if (wrk.FileSys().FileIsOlderThan(BuildFile, depFileAbs, &error)) {
  762. if (verbose) {
  763. std::string reason = "Generating ";
  764. reason += Quoted(BuildFile);
  765. reason += " from ";
  766. reason += Quoted(SourceFile);
  767. reason += " because it is older than it's dependency file ";
  768. reason += Quoted(depFileAbs);
  769. wrk.LogInfo(GenT::MOC, reason);
  770. }
  771. return true;
  772. }
  773. if (!error.empty()) {
  774. wrk.LogError(GenT::MOC, error);
  775. return false;
  776. }
  777. } else {
  778. std::string message = "Could not find dependency file ";
  779. message += Quoted(depFileRel);
  780. wrk.LogFileWarning(GenT::MOC, SourceFile, message);
  781. }
  782. }
  783. }
  784. return false;
  785. }
  786. void cmQtAutoGeneratorMocUic::JobMocT::GenerateMoc(WorkerT& wrk)
  787. {
  788. // Make sure the parent directory exists
  789. if (wrk.FileSys().MakeParentDirectory(GenT::MOC, BuildFile)) {
  790. // Compose moc command
  791. std::vector<std::string> cmd;
  792. cmd.push_back(wrk.Moc().Executable);
  793. // Add options
  794. cmd.insert(cmd.end(), wrk.Moc().AllOptions.begin(),
  795. wrk.Moc().AllOptions.end());
  796. // Add predefs include
  797. if (!wrk.Moc().PredefsFileAbs.empty()) {
  798. cmd.emplace_back("--include");
  799. cmd.push_back(wrk.Moc().PredefsFileAbs);
  800. }
  801. cmd.emplace_back("-o");
  802. cmd.push_back(BuildFile);
  803. cmd.push_back(SourceFile);
  804. // Execute moc command
  805. ProcessResultT result;
  806. if (wrk.RunProcess(GenT::MOC, result, cmd)) {
  807. // Moc command success
  808. // Print moc output
  809. if (!result.StdOut.empty()) {
  810. wrk.LogInfo(GenT::MOC, result.StdOut);
  811. }
  812. // Notify the generator that a not included file changed (on demand)
  813. if (IncludeString.empty()) {
  814. wrk.Gen().ParallelMocAutoUpdated();
  815. }
  816. } else {
  817. // Moc command failed
  818. {
  819. std::string emsg = "The moc process failed to compile\n ";
  820. emsg += Quoted(SourceFile);
  821. emsg += "\ninto\n ";
  822. emsg += Quoted(BuildFile);
  823. emsg += ".\n";
  824. emsg += result.ErrorMessage;
  825. wrk.LogCommandError(GenT::MOC, emsg, cmd, result.StdOut);
  826. }
  827. wrk.FileSys().FileRemove(BuildFile);
  828. }
  829. }
  830. }
  831. void cmQtAutoGeneratorMocUic::JobUicT::Process(WorkerT& wrk)
  832. {
  833. // Compute build file name
  834. BuildFile = wrk.Base().AutogenIncludeDir;
  835. BuildFile += '/';
  836. BuildFile += IncludeString;
  837. if (UpdateRequired(wrk)) {
  838. GenerateUic(wrk);
  839. }
  840. }
  841. bool cmQtAutoGeneratorMocUic::JobUicT::UpdateRequired(WorkerT& wrk)
  842. {
  843. bool const verbose = wrk.Gen().Log().Verbose();
  844. // Test if the build file exists
  845. if (!wrk.FileSys().FileExists(BuildFile)) {
  846. if (verbose) {
  847. std::string reason = "Generating ";
  848. reason += Quoted(BuildFile);
  849. reason += " from its source file ";
  850. reason += Quoted(SourceFile);
  851. reason += " because it doesn't exist";
  852. wrk.LogInfo(GenT::UIC, reason);
  853. }
  854. return true;
  855. }
  856. // Test if the uic settings changed
  857. if (wrk.Uic().SettingsChanged) {
  858. if (verbose) {
  859. std::string reason = "Generating ";
  860. reason += Quoted(BuildFile);
  861. reason += " from ";
  862. reason += Quoted(SourceFile);
  863. reason += " because the UIC settings changed";
  864. wrk.LogInfo(GenT::UIC, reason);
  865. }
  866. return true;
  867. }
  868. // Test if the source file is newer
  869. {
  870. bool isOlder = false;
  871. {
  872. std::string error;
  873. isOlder = wrk.FileSys().FileIsOlderThan(BuildFile, SourceFile, &error);
  874. if (!isOlder && !error.empty()) {
  875. wrk.LogError(GenT::UIC, error);
  876. return false;
  877. }
  878. }
  879. if (isOlder) {
  880. if (verbose) {
  881. std::string reason = "Generating ";
  882. reason += Quoted(BuildFile);
  883. reason += " because it's older than its source file ";
  884. reason += Quoted(SourceFile);
  885. wrk.LogInfo(GenT::UIC, reason);
  886. }
  887. return true;
  888. }
  889. }
  890. return false;
  891. }
  892. void cmQtAutoGeneratorMocUic::JobUicT::GenerateUic(WorkerT& wrk)
  893. {
  894. // Make sure the parent directory exists
  895. if (wrk.FileSys().MakeParentDirectory(GenT::UIC, BuildFile)) {
  896. // Compose uic command
  897. std::vector<std::string> cmd;
  898. cmd.push_back(wrk.Uic().Executable);
  899. {
  900. std::vector<std::string> allOpts = wrk.Uic().TargetOptions;
  901. auto optionIt = wrk.Uic().Options.find(SourceFile);
  902. if (optionIt != wrk.Uic().Options.end()) {
  903. UicMergeOptions(allOpts, optionIt->second,
  904. (wrk.Base().QtVersionMajor == 5));
  905. }
  906. cmd.insert(cmd.end(), allOpts.begin(), allOpts.end());
  907. }
  908. cmd.emplace_back("-o");
  909. cmd.push_back(BuildFile);
  910. cmd.push_back(SourceFile);
  911. ProcessResultT result;
  912. if (wrk.RunProcess(GenT::UIC, result, cmd)) {
  913. // Uic command success
  914. // Print uic output
  915. if (!result.StdOut.empty()) {
  916. wrk.LogInfo(GenT::UIC, result.StdOut);
  917. }
  918. } else {
  919. // Uic command failed
  920. {
  921. std::string emsg = "The uic process failed to compile\n ";
  922. emsg += Quoted(SourceFile);
  923. emsg += "\ninto\n ";
  924. emsg += Quoted(BuildFile);
  925. emsg += "\nincluded by\n ";
  926. emsg += Quoted(IncluderFile);
  927. emsg += ".\n";
  928. emsg += result.ErrorMessage;
  929. wrk.LogCommandError(GenT::UIC, emsg, cmd, result.StdOut);
  930. }
  931. wrk.FileSys().FileRemove(BuildFile);
  932. }
  933. }
  934. }
  935. void cmQtAutoGeneratorMocUic::JobDeleterT::operator()(JobT* job)
  936. {
  937. delete job;
  938. }
  939. cmQtAutoGeneratorMocUic::WorkerT::WorkerT(cmQtAutoGeneratorMocUic* gen,
  940. uv_loop_t* uvLoop)
  941. : Gen_(gen)
  942. {
  943. // Initialize uv asynchronous callback for process starting
  944. ProcessRequest_.init(*uvLoop, &WorkerT::UVProcessStart, this);
  945. // Start thread
  946. Thread_ = std::thread(&WorkerT::Loop, this);
  947. }
  948. cmQtAutoGeneratorMocUic::WorkerT::~WorkerT()
  949. {
  950. // Join thread
  951. if (Thread_.joinable()) {
  952. Thread_.join();
  953. }
  954. }
  955. void cmQtAutoGeneratorMocUic::WorkerT::LogInfo(
  956. GenT genType, std::string const& message) const
  957. {
  958. Log().Info(genType, message);
  959. }
  960. void cmQtAutoGeneratorMocUic::WorkerT::LogWarning(
  961. GenT genType, std::string const& message) const
  962. {
  963. Log().Warning(genType, message);
  964. }
  965. void cmQtAutoGeneratorMocUic::WorkerT::LogFileWarning(
  966. GenT genType, std::string const& filename, std::string const& message) const
  967. {
  968. Log().WarningFile(genType, filename, message);
  969. }
  970. void cmQtAutoGeneratorMocUic::WorkerT::LogError(
  971. GenT genType, std::string const& message) const
  972. {
  973. Gen().ParallelRegisterJobError();
  974. Log().Error(genType, message);
  975. }
  976. void cmQtAutoGeneratorMocUic::WorkerT::LogFileError(
  977. GenT genType, std::string const& filename, std::string const& message) const
  978. {
  979. Gen().ParallelRegisterJobError();
  980. Log().ErrorFile(genType, filename, message);
  981. }
  982. void cmQtAutoGeneratorMocUic::WorkerT::LogCommandError(
  983. GenT genType, std::string const& message,
  984. std::vector<std::string> const& command, std::string const& output) const
  985. {
  986. Gen().ParallelRegisterJobError();
  987. Log().ErrorCommand(genType, message, command, output);
  988. }
  989. bool cmQtAutoGeneratorMocUic::WorkerT::RunProcess(
  990. GenT genType, ProcessResultT& result,
  991. std::vector<std::string> const& command)
  992. {
  993. if (command.empty()) {
  994. return false;
  995. }
  996. // Create process instance
  997. {
  998. std::lock_guard<std::mutex> lock(ProcessMutex_);
  999. Process_ = cm::make_unique<ReadOnlyProcessT>();
  1000. Process_->setup(&result, true, command, Gen().Base().AutogenBuildDir);
  1001. }
  1002. // Send asynchronous process start request to libuv loop
  1003. ProcessRequest_.send();
  1004. // Log command
  1005. if (this->Log().Verbose()) {
  1006. std::string msg = "Running command:\n";
  1007. msg += QuotedCommand(command);
  1008. msg += '\n';
  1009. this->LogInfo(genType, msg);
  1010. }
  1011. // Wait until the process has been finished and destroyed
  1012. {
  1013. std::unique_lock<std::mutex> ulock(ProcessMutex_);
  1014. while (Process_) {
  1015. ProcessCondition_.wait(ulock);
  1016. }
  1017. }
  1018. return !result.error();
  1019. }
  1020. void cmQtAutoGeneratorMocUic::WorkerT::Loop()
  1021. {
  1022. while (true) {
  1023. Gen().WorkerSwapJob(JobHandle_);
  1024. if (JobHandle_) {
  1025. JobHandle_->Process(*this);
  1026. } else {
  1027. break;
  1028. }
  1029. }
  1030. }
  1031. void cmQtAutoGeneratorMocUic::WorkerT::UVProcessStart(uv_async_t* handle)
  1032. {
  1033. auto& wrk = *reinterpret_cast<WorkerT*>(handle->data);
  1034. {
  1035. std::lock_guard<std::mutex> lock(wrk.ProcessMutex_);
  1036. if (wrk.Process_ && !wrk.Process_->IsStarted()) {
  1037. wrk.Process_->start(handle->loop, [&wrk] { wrk.UVProcessFinished(); });
  1038. }
  1039. }
  1040. }
  1041. void cmQtAutoGeneratorMocUic::WorkerT::UVProcessFinished()
  1042. {
  1043. {
  1044. std::lock_guard<std::mutex> lock(ProcessMutex_);
  1045. if (Process_ && Process_->IsFinished()) {
  1046. Process_.reset();
  1047. }
  1048. }
  1049. // Notify idling thread
  1050. ProcessCondition_.notify_one();
  1051. }
  1052. cmQtAutoGeneratorMocUic::cmQtAutoGeneratorMocUic()
  1053. : Base_(&FileSys())
  1054. , Moc_(&FileSys())
  1055. {
  1056. // Precompile regular expressions
  1057. Moc_.RegExpInclude.compile(
  1058. "(^|\n)[ \t]*#[ \t]*include[ \t]+"
  1059. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  1060. Uic_.RegExpInclude.compile("(^|\n)[ \t]*#[ \t]*include[ \t]+"
  1061. "[\"<](([^ \">]+/)?ui_[^ \">/]+\\.h)[\">]");
  1062. // Initialize libuv asynchronous iteration request
  1063. UVRequest().init(*UVLoop(), &cmQtAutoGeneratorMocUic::UVPollStage, this);
  1064. }
  1065. cmQtAutoGeneratorMocUic::~cmQtAutoGeneratorMocUic() = default;
  1066. bool cmQtAutoGeneratorMocUic::Init(cmMakefile* makefile)
  1067. {
  1068. // -- Meta
  1069. Base_.HeaderExtensions = makefile->GetCMakeInstance()->GetHeaderExtensions();
  1070. // Utility lambdas
  1071. auto InfoGet = [makefile](const char* key) {
  1072. return makefile->GetSafeDefinition(key);
  1073. };
  1074. auto InfoGetBool = [makefile](const char* key) {
  1075. return makefile->IsOn(key);
  1076. };
  1077. auto InfoGetList = [makefile](const char* key) -> std::vector<std::string> {
  1078. std::vector<std::string> list;
  1079. cmSystemTools::ExpandListArgument(makefile->GetSafeDefinition(key), list);
  1080. return list;
  1081. };
  1082. auto InfoGetLists =
  1083. [makefile](const char* key) -> std::vector<std::vector<std::string>> {
  1084. std::vector<std::vector<std::string>> lists;
  1085. {
  1086. std::string const value = makefile->GetSafeDefinition(key);
  1087. std::string::size_type pos = 0;
  1088. while (pos < value.size()) {
  1089. std::string::size_type next = value.find(ListSep, pos);
  1090. std::string::size_type length =
  1091. (next != std::string::npos) ? next - pos : value.size() - pos;
  1092. // Remove enclosing braces
  1093. if (length >= 2) {
  1094. std::string::const_iterator itBeg = value.begin() + (pos + 1);
  1095. std::string::const_iterator itEnd = itBeg + (length - 2);
  1096. {
  1097. std::string subValue(itBeg, itEnd);
  1098. std::vector<std::string> list;
  1099. cmSystemTools::ExpandListArgument(subValue, list);
  1100. lists.push_back(std::move(list));
  1101. }
  1102. }
  1103. pos += length;
  1104. pos += ListSep.size();
  1105. }
  1106. }
  1107. return lists;
  1108. };
  1109. auto InfoGetConfig = [makefile, this](const char* key) -> std::string {
  1110. const char* valueConf = nullptr;
  1111. {
  1112. std::string keyConf = key;
  1113. keyConf += '_';
  1114. keyConf += InfoConfig();
  1115. valueConf = makefile->GetDefinition(keyConf);
  1116. }
  1117. if (valueConf == nullptr) {
  1118. return makefile->GetSafeDefinition(key);
  1119. }
  1120. return std::string(valueConf);
  1121. };
  1122. auto InfoGetConfigList =
  1123. [&InfoGetConfig](const char* key) -> std::vector<std::string> {
  1124. std::vector<std::string> list;
  1125. cmSystemTools::ExpandListArgument(InfoGetConfig(key), list);
  1126. return list;
  1127. };
  1128. // -- Read info file
  1129. if (!makefile->ReadListFile(InfoFile())) {
  1130. Log().ErrorFile(GenT::GEN, InfoFile(), "File processing failed");
  1131. return false;
  1132. }
  1133. // -- Meta
  1134. Log().RaiseVerbosity(InfoGet("AM_VERBOSITY"));
  1135. Base_.MultiConfig = InfoGetBool("AM_MULTI_CONFIG");
  1136. {
  1137. unsigned long num = Base_.NumThreads;
  1138. if (cmSystemTools::StringToULong(InfoGet("AM_PARALLEL").c_str(), &num)) {
  1139. num = std::max<unsigned long>(num, 1);
  1140. num = std::min<unsigned long>(num, ParallelMax);
  1141. Base_.NumThreads = static_cast<unsigned int>(num);
  1142. }
  1143. }
  1144. // - Files and directories
  1145. Base_.ProjectSourceDir = InfoGet("AM_CMAKE_SOURCE_DIR");
  1146. Base_.ProjectBinaryDir = InfoGet("AM_CMAKE_BINARY_DIR");
  1147. Base_.CurrentSourceDir = InfoGet("AM_CMAKE_CURRENT_SOURCE_DIR");
  1148. Base_.CurrentBinaryDir = InfoGet("AM_CMAKE_CURRENT_BINARY_DIR");
  1149. Base_.IncludeProjectDirsBefore =
  1150. InfoGetBool("AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE");
  1151. Base_.AutogenBuildDir = InfoGet("AM_BUILD_DIR");
  1152. if (Base_.AutogenBuildDir.empty()) {
  1153. Log().ErrorFile(GenT::GEN, InfoFile(), "Autogen build directory missing");
  1154. return false;
  1155. }
  1156. // include directory
  1157. Base_.AutogenIncludeDir = InfoGetConfig("AM_INCLUDE_DIR");
  1158. if (Base_.AutogenIncludeDir.empty()) {
  1159. Log().ErrorFile(GenT::GEN, InfoFile(),
  1160. "Autogen include directory missing");
  1161. return false;
  1162. }
  1163. // - Files
  1164. SettingsFile_ = InfoGetConfig("AM_SETTINGS_FILE");
  1165. if (SettingsFile_.empty()) {
  1166. Log().ErrorFile(GenT::GEN, InfoFile(), "Settings file name missing");
  1167. return false;
  1168. }
  1169. // - Qt environment
  1170. {
  1171. unsigned long qtv = Base_.QtVersionMajor;
  1172. if (cmSystemTools::StringToULong(InfoGet("AM_QT_VERSION_MAJOR").c_str(),
  1173. &qtv)) {
  1174. Base_.QtVersionMajor = static_cast<unsigned int>(qtv);
  1175. }
  1176. }
  1177. // - Moc
  1178. Moc_.Executable = InfoGet("AM_QT_MOC_EXECUTABLE");
  1179. Moc_.Enabled = !Moc().Executable.empty();
  1180. if (Moc().Enabled) {
  1181. {
  1182. auto lst = InfoGetList("AM_MOC_SKIP");
  1183. Moc_.SkipList.insert(lst.begin(), lst.end());
  1184. }
  1185. Moc_.Definitions = InfoGetConfigList("AM_MOC_DEFINITIONS");
  1186. Moc_.IncludePaths = InfoGetConfigList("AM_MOC_INCLUDES");
  1187. Moc_.Options = InfoGetList("AM_MOC_OPTIONS");
  1188. Moc_.RelaxedMode = InfoGetBool("AM_MOC_RELAXED_MODE");
  1189. for (std::string const& item : InfoGetList("AM_MOC_MACRO_NAMES")) {
  1190. Moc_.MacroFilters.emplace_back(
  1191. item, ("[\n][ \t]*{?[ \t]*" + item).append("[^a-zA-Z0-9_]"));
  1192. }
  1193. {
  1194. auto pushFilter = [this](std::string const& key, std::string const& exp,
  1195. std::string& error) {
  1196. if (!key.empty()) {
  1197. if (!exp.empty()) {
  1198. Moc_.DependFilters.emplace_back();
  1199. KeyExpT& filter(Moc_.DependFilters.back());
  1200. if (filter.Exp.compile(exp)) {
  1201. filter.Key = key;
  1202. } else {
  1203. error = "Regular expression compiling failed";
  1204. }
  1205. } else {
  1206. error = "Regular expression is empty";
  1207. }
  1208. } else {
  1209. error = "Key is empty";
  1210. }
  1211. if (!error.empty()) {
  1212. error = ("AUTOMOC_DEPEND_FILTERS: " + error);
  1213. error += "\n";
  1214. error += " Key: ";
  1215. error += Quoted(key);
  1216. error += "\n";
  1217. error += " Exp: ";
  1218. error += Quoted(exp);
  1219. error += "\n";
  1220. }
  1221. };
  1222. std::string error;
  1223. // Insert default filter for Q_PLUGIN_METADATA
  1224. if (Base().QtVersionMajor != 4) {
  1225. pushFilter("Q_PLUGIN_METADATA",
  1226. "[\n][ \t]*Q_PLUGIN_METADATA[ \t]*\\("
  1227. "[^\\)]*FILE[ \t]*\"([^\"]+)\"",
  1228. error);
  1229. }
  1230. // Insert user defined dependency filters
  1231. {
  1232. std::vector<std::string> flts = InfoGetList("AM_MOC_DEPEND_FILTERS");
  1233. if ((flts.size() % 2) == 0) {
  1234. for (std::vector<std::string>::iterator itC = flts.begin(),
  1235. itE = flts.end();
  1236. itC != itE; itC += 2) {
  1237. pushFilter(*itC, *(itC + 1), error);
  1238. if (!error.empty()) {
  1239. break;
  1240. }
  1241. }
  1242. } else {
  1243. Log().ErrorFile(
  1244. GenT::MOC, InfoFile(),
  1245. "AUTOMOC_DEPEND_FILTERS list size is not a multiple of 2");
  1246. return false;
  1247. }
  1248. }
  1249. if (!error.empty()) {
  1250. Log().ErrorFile(GenT::MOC, InfoFile(), error);
  1251. return false;
  1252. }
  1253. }
  1254. Moc_.PredefsCmd = InfoGetList("AM_MOC_PREDEFS_CMD");
  1255. // Install moc predefs job
  1256. if (!Moc().PredefsCmd.empty()) {
  1257. JobQueues_.MocPredefs.emplace_back(new JobMocPredefsT());
  1258. }
  1259. }
  1260. // - Uic
  1261. Uic_.Executable = InfoGet("AM_QT_UIC_EXECUTABLE");
  1262. Uic_.Enabled = !Uic().Executable.empty();
  1263. if (Uic().Enabled) {
  1264. {
  1265. auto lst = InfoGetList("AM_UIC_SKIP");
  1266. Uic_.SkipList.insert(lst.begin(), lst.end());
  1267. }
  1268. Uic_.SearchPaths = InfoGetList("AM_UIC_SEARCH_PATHS");
  1269. Uic_.TargetOptions = InfoGetConfigList("AM_UIC_TARGET_OPTIONS");
  1270. {
  1271. auto sources = InfoGetList("AM_UIC_OPTIONS_FILES");
  1272. auto options = InfoGetLists("AM_UIC_OPTIONS_OPTIONS");
  1273. // Compare list sizes
  1274. if (sources.size() != options.size()) {
  1275. std::ostringstream ost;
  1276. ost << "files/options lists sizes mismatch (" << sources.size() << "/"
  1277. << options.size() << ")";
  1278. Log().ErrorFile(GenT::UIC, InfoFile(), ost.str());
  1279. return false;
  1280. }
  1281. auto fitEnd = sources.cend();
  1282. auto fit = sources.begin();
  1283. auto oit = options.begin();
  1284. while (fit != fitEnd) {
  1285. Uic_.Options[*fit] = std::move(*oit);
  1286. ++fit;
  1287. ++oit;
  1288. }
  1289. }
  1290. }
  1291. // Initialize source file jobs
  1292. {
  1293. std::hash<std::string> stringHash;
  1294. std::set<std::size_t> uniqueHeaders;
  1295. // Add header jobs
  1296. for (std::string& hdr : InfoGetList("AM_HEADERS")) {
  1297. const bool moc = !Moc().skipped(hdr);
  1298. const bool uic = !Uic().skipped(hdr);
  1299. if ((moc || uic) && uniqueHeaders.emplace(stringHash(hdr)).second) {
  1300. JobQueues_.Headers.emplace_back(
  1301. new JobParseT(std::move(hdr), moc, uic, true));
  1302. }
  1303. }
  1304. // Add source jobs
  1305. {
  1306. std::vector<std::string> sources = InfoGetList("AM_SOURCES");
  1307. // Add header(s) for the source file
  1308. for (std::string& src : sources) {
  1309. const bool srcMoc = !Moc().skipped(src);
  1310. const bool srcUic = !Uic().skipped(src);
  1311. if (!srcMoc && !srcUic) {
  1312. continue;
  1313. }
  1314. // Search for the default header file and a private header
  1315. {
  1316. std::array<std::string, 2> bases;
  1317. bases[0] = FileSys().SubDirPrefix(src);
  1318. bases[0] += FileSys().GetFilenameWithoutLastExtension(src);
  1319. bases[1] = bases[0];
  1320. bases[1] += "_p";
  1321. for (std::string const& headerBase : bases) {
  1322. std::string header;
  1323. if (Base().FindHeader(header, headerBase)) {
  1324. const bool moc = srcMoc && !Moc().skipped(header);
  1325. const bool uic = srcUic && !Uic().skipped(header);
  1326. if ((moc || uic) &&
  1327. uniqueHeaders.emplace(stringHash(header)).second) {
  1328. JobQueues_.Headers.emplace_back(
  1329. new JobParseT(std::move(header), moc, uic, true));
  1330. }
  1331. }
  1332. }
  1333. }
  1334. // Add source job
  1335. JobQueues_.Sources.emplace_back(
  1336. new JobParseT(std::move(src), srcMoc, srcUic));
  1337. }
  1338. }
  1339. }
  1340. // Init derived information
  1341. // ------------------------
  1342. // Init file path checksum generator
  1343. FileSys().setupFilePathChecksum(
  1344. Base().CurrentSourceDir, Base().CurrentBinaryDir, Base().ProjectSourceDir,
  1345. Base().ProjectBinaryDir);
  1346. // Moc variables
  1347. if (Moc().Enabled) {
  1348. // Mocs compilation file
  1349. Moc_.CompFileAbs = Base().AbsoluteBuildPath("mocs_compilation.cpp");
  1350. // Moc predefs file
  1351. if (!Moc_.PredefsCmd.empty()) {
  1352. Moc_.PredefsFileRel = "moc_predefs";
  1353. if (Base_.MultiConfig) {
  1354. Moc_.PredefsFileRel += '_';
  1355. Moc_.PredefsFileRel += InfoConfig();
  1356. }
  1357. Moc_.PredefsFileRel += ".h";
  1358. Moc_.PredefsFileAbs = Base_.AbsoluteBuildPath(Moc().PredefsFileRel);
  1359. }
  1360. // Sort include directories on demand
  1361. if (Base().IncludeProjectDirsBefore) {
  1362. // Move strings to temporary list
  1363. std::list<std::string> includes;
  1364. includes.insert(includes.end(), Moc().IncludePaths.begin(),
  1365. Moc().IncludePaths.end());
  1366. Moc_.IncludePaths.clear();
  1367. Moc_.IncludePaths.reserve(includes.size());
  1368. // Append project directories only
  1369. {
  1370. std::array<std::string const*, 2> const movePaths = {
  1371. { &Base().ProjectBinaryDir, &Base().ProjectSourceDir }
  1372. };
  1373. for (std::string const* ppath : movePaths) {
  1374. std::list<std::string>::iterator it = includes.begin();
  1375. while (it != includes.end()) {
  1376. std::string const& path = *it;
  1377. if (cmSystemTools::StringStartsWith(path, ppath->c_str())) {
  1378. Moc_.IncludePaths.push_back(path);
  1379. it = includes.erase(it);
  1380. } else {
  1381. ++it;
  1382. }
  1383. }
  1384. }
  1385. }
  1386. // Append remaining directories
  1387. Moc_.IncludePaths.insert(Moc_.IncludePaths.end(), includes.begin(),
  1388. includes.end());
  1389. }
  1390. // Compose moc includes list
  1391. {
  1392. std::set<std::string> frameworkPaths;
  1393. for (std::string const& path : Moc().IncludePaths) {
  1394. Moc_.Includes.push_back("-I" + path);
  1395. // Extract framework path
  1396. if (cmHasLiteralSuffix(path, ".framework/Headers")) {
  1397. // Go up twice to get to the framework root
  1398. std::vector<std::string> pathComponents;
  1399. FileSys().SplitPath(path, pathComponents);
  1400. std::string frameworkPath = FileSys().JoinPath(
  1401. pathComponents.begin(), pathComponents.end() - 2);
  1402. frameworkPaths.insert(frameworkPath);
  1403. }
  1404. }
  1405. // Append framework includes
  1406. for (std::string const& path : frameworkPaths) {
  1407. Moc_.Includes.emplace_back("-F");
  1408. Moc_.Includes.push_back(path);
  1409. }
  1410. }
  1411. // Setup single list with all options
  1412. {
  1413. // Add includes
  1414. Moc_.AllOptions.insert(Moc_.AllOptions.end(), Moc().Includes.begin(),
  1415. Moc().Includes.end());
  1416. // Add definitions
  1417. for (std::string const& def : Moc().Definitions) {
  1418. Moc_.AllOptions.push_back("-D" + def);
  1419. }
  1420. // Add options
  1421. Moc_.AllOptions.insert(Moc_.AllOptions.end(), Moc().Options.begin(),
  1422. Moc().Options.end());
  1423. }
  1424. }
  1425. return true;
  1426. }
  1427. bool cmQtAutoGeneratorMocUic::Process()
  1428. {
  1429. // Run libuv event loop
  1430. UVRequest().send();
  1431. if (uv_run(UVLoop(), UV_RUN_DEFAULT) == 0) {
  1432. if (JobError_) {
  1433. return false;
  1434. }
  1435. } else {
  1436. return false;
  1437. }
  1438. return true;
  1439. }
  1440. void cmQtAutoGeneratorMocUic::UVPollStage(uv_async_t* handle)
  1441. {
  1442. reinterpret_cast<cmQtAutoGeneratorMocUic*>(handle->data)->PollStage();
  1443. }
  1444. void cmQtAutoGeneratorMocUic::PollStage()
  1445. {
  1446. switch (Stage_) {
  1447. case StageT::SETTINGS_READ:
  1448. SettingsFileRead();
  1449. SetStage(StageT::CREATE_DIRECTORIES);
  1450. break;
  1451. case StageT::CREATE_DIRECTORIES:
  1452. CreateDirectories();
  1453. SetStage(StageT::PARSE_SOURCES);
  1454. break;
  1455. case StageT::PARSE_SOURCES:
  1456. if (ThreadsStartJobs(JobQueues_.Sources)) {
  1457. SetStage(StageT::PARSE_HEADERS);
  1458. }
  1459. break;
  1460. case StageT::PARSE_HEADERS:
  1461. if (ThreadsStartJobs(JobQueues_.Headers)) {
  1462. SetStage(StageT::MOC_PREDEFS);
  1463. }
  1464. break;
  1465. case StageT::MOC_PREDEFS:
  1466. if (ThreadsStartJobs(JobQueues_.MocPredefs)) {
  1467. SetStage(StageT::MOC_PROCESS);
  1468. }
  1469. break;
  1470. case StageT::MOC_PROCESS:
  1471. if (ThreadsStartJobs(JobQueues_.Moc)) {
  1472. SetStage(StageT::MOCS_COMPILATION);
  1473. }
  1474. break;
  1475. case StageT::MOCS_COMPILATION:
  1476. if (ThreadsJobsDone()) {
  1477. MocGenerateCompilation();
  1478. SetStage(StageT::UIC_PROCESS);
  1479. }
  1480. break;
  1481. case StageT::UIC_PROCESS:
  1482. if (ThreadsStartJobs(JobQueues_.Uic)) {
  1483. SetStage(StageT::SETTINGS_WRITE);
  1484. }
  1485. break;
  1486. case StageT::SETTINGS_WRITE:
  1487. SettingsFileWrite();
  1488. SetStage(StageT::FINISH);
  1489. break;
  1490. case StageT::FINISH:
  1491. if (ThreadsJobsDone()) {
  1492. // Clear all libuv handles
  1493. ThreadsStop();
  1494. UVRequest().reset();
  1495. // Set highest END stage manually
  1496. Stage_ = StageT::END;
  1497. }
  1498. break;
  1499. case StageT::END:
  1500. break;
  1501. }
  1502. }
  1503. void cmQtAutoGeneratorMocUic::SetStage(StageT stage)
  1504. {
  1505. if (JobError_) {
  1506. stage = StageT::FINISH;
  1507. }
  1508. // Only allow to increase the stage
  1509. if (Stage_ < stage) {
  1510. Stage_ = stage;
  1511. UVRequest().send();
  1512. }
  1513. }
  1514. void cmQtAutoGeneratorMocUic::SettingsFileRead()
  1515. {
  1516. // Compose current settings strings
  1517. {
  1518. cmCryptoHash crypt(cmCryptoHash::AlgoSHA256);
  1519. std::string const sep(" ~~~ ");
  1520. if (Moc_.Enabled) {
  1521. std::string str;
  1522. str += Moc().Executable;
  1523. str += sep;
  1524. str += cmJoin(Moc().AllOptions, ";");
  1525. str += sep;
  1526. str += Base().IncludeProjectDirsBefore ? "TRUE" : "FALSE";
  1527. str += sep;
  1528. str += cmJoin(Moc().PredefsCmd, ";");
  1529. str += sep;
  1530. SettingsStringMoc_ = crypt.HashString(str);
  1531. }
  1532. if (Uic().Enabled) {
  1533. std::string str;
  1534. str += Uic().Executable;
  1535. str += sep;
  1536. str += cmJoin(Uic().TargetOptions, ";");
  1537. for (const auto& item : Uic().Options) {
  1538. str += sep;
  1539. str += item.first;
  1540. str += sep;
  1541. str += cmJoin(item.second, ";");
  1542. }
  1543. str += sep;
  1544. SettingsStringUic_ = crypt.HashString(str);
  1545. }
  1546. }
  1547. // Read old settings and compare
  1548. {
  1549. std::string content;
  1550. if (FileSys().FileRead(content, SettingsFile_)) {
  1551. if (Moc().Enabled) {
  1552. if (SettingsStringMoc_ != SettingsFind(content, "moc")) {
  1553. Moc_.SettingsChanged = true;
  1554. }
  1555. }
  1556. if (Uic().Enabled) {
  1557. if (SettingsStringUic_ != SettingsFind(content, "uic")) {
  1558. Uic_.SettingsChanged = true;
  1559. }
  1560. }
  1561. // In case any setting changed remove the old settings file.
  1562. // This triggers a full rebuild on the next run if the current
  1563. // build is aborted before writing the current settings in the end.
  1564. if (Moc().SettingsChanged || Uic().SettingsChanged) {
  1565. FileSys().FileRemove(SettingsFile_);
  1566. }
  1567. } else {
  1568. // Settings file read failed
  1569. if (Moc().Enabled) {
  1570. Moc_.SettingsChanged = true;
  1571. }
  1572. if (Uic().Enabled) {
  1573. Uic_.SettingsChanged = true;
  1574. }
  1575. }
  1576. }
  1577. }
  1578. void cmQtAutoGeneratorMocUic::SettingsFileWrite()
  1579. {
  1580. std::lock_guard<std::mutex> jobsLock(JobsMutex_);
  1581. // Only write if any setting changed
  1582. if (!JobError_ && (Moc().SettingsChanged || Uic().SettingsChanged)) {
  1583. if (Log().Verbose()) {
  1584. Log().Info(GenT::GEN, "Writing settings file " + Quoted(SettingsFile_));
  1585. }
  1586. // Compose settings file content
  1587. std::string content;
  1588. {
  1589. auto SettingAppend = [&content](const char* key,
  1590. std::string const& value) {
  1591. if (!value.empty()) {
  1592. content += key;
  1593. content += ':';
  1594. content += value;
  1595. content += '\n';
  1596. }
  1597. };
  1598. SettingAppend("moc", SettingsStringMoc_);
  1599. SettingAppend("uic", SettingsStringUic_);
  1600. }
  1601. // Write settings file
  1602. if (!FileSys().FileWrite(GenT::GEN, SettingsFile_, content)) {
  1603. Log().ErrorFile(GenT::GEN, SettingsFile_,
  1604. "Settings file writing failed");
  1605. // Remove old settings file to trigger a full rebuild on the next run
  1606. FileSys().FileRemove(SettingsFile_);
  1607. RegisterJobError();
  1608. }
  1609. }
  1610. }
  1611. void cmQtAutoGeneratorMocUic::CreateDirectories()
  1612. {
  1613. // Create AUTOGEN include directory
  1614. if (!FileSys().MakeDirectory(GenT::GEN, Base().AutogenIncludeDir)) {
  1615. RegisterJobError();
  1616. }
  1617. }
  1618. bool cmQtAutoGeneratorMocUic::ThreadsStartJobs(JobQueueT& queue)
  1619. {
  1620. bool done = false;
  1621. std::size_t queueSize = queue.size();
  1622. // Change the active queue
  1623. {
  1624. std::lock_guard<std::mutex> jobsLock(JobsMutex_);
  1625. // Check if there are still unfinished jobs from the previous queue
  1626. if (JobsRemain_ == 0) {
  1627. if (!JobThreadsAbort_) {
  1628. JobQueue_.swap(queue);
  1629. JobsRemain_ = queueSize;
  1630. } else {
  1631. // Abort requested
  1632. queue.clear();
  1633. queueSize = 0;
  1634. }
  1635. done = true;
  1636. }
  1637. }
  1638. if (done && (queueSize != 0)) {
  1639. // Start new threads on demand
  1640. if (Workers_.empty()) {
  1641. Workers_.resize(Base().NumThreads);
  1642. for (auto& item : Workers_) {
  1643. item = cm::make_unique<WorkerT>(this, UVLoop());
  1644. }
  1645. } else {
  1646. // Notify threads
  1647. if (queueSize == 1) {
  1648. JobsConditionRead_.notify_one();
  1649. } else {
  1650. JobsConditionRead_.notify_all();
  1651. }
  1652. }
  1653. }
  1654. return done;
  1655. }
  1656. void cmQtAutoGeneratorMocUic::ThreadsStop()
  1657. {
  1658. if (!Workers_.empty()) {
  1659. // Clear all jobs
  1660. {
  1661. std::lock_guard<std::mutex> jobsLock(JobsMutex_);
  1662. JobThreadsAbort_ = true;
  1663. JobsRemain_ -= JobQueue_.size();
  1664. JobQueue_.clear();
  1665. JobQueues_.Sources.clear();
  1666. JobQueues_.Headers.clear();
  1667. JobQueues_.MocPredefs.clear();
  1668. JobQueues_.Moc.clear();
  1669. JobQueues_.Uic.clear();
  1670. }
  1671. // Wake threads
  1672. JobsConditionRead_.notify_all();
  1673. // Join and clear threads
  1674. Workers_.clear();
  1675. }
  1676. }
  1677. bool cmQtAutoGeneratorMocUic::ThreadsJobsDone()
  1678. {
  1679. std::lock_guard<std::mutex> jobsLock(JobsMutex_);
  1680. return (JobsRemain_ == 0);
  1681. }
  1682. void cmQtAutoGeneratorMocUic::WorkerSwapJob(JobHandleT& jobHandle)
  1683. {
  1684. bool const jobProcessed(jobHandle);
  1685. if (jobProcessed) {
  1686. jobHandle.reset(nullptr);
  1687. }
  1688. {
  1689. std::unique_lock<std::mutex> jobsLock(JobsMutex_);
  1690. // Reduce the remaining job count and notify the libuv loop
  1691. // when all jobs are done
  1692. if (jobProcessed) {
  1693. --JobsRemain_;
  1694. if (JobsRemain_ == 0) {
  1695. UVRequest().send();
  1696. }
  1697. }
  1698. // Wait for new jobs
  1699. while (!JobThreadsAbort_ && JobQueue_.empty()) {
  1700. JobsConditionRead_.wait(jobsLock);
  1701. }
  1702. // Try to pick up a new job handle
  1703. if (!JobThreadsAbort_ && !JobQueue_.empty()) {
  1704. jobHandle = std::move(JobQueue_.front());
  1705. JobQueue_.pop_front();
  1706. }
  1707. }
  1708. }
  1709. void cmQtAutoGeneratorMocUic::ParallelRegisterJobError()
  1710. {
  1711. std::lock_guard<std::mutex> jobsLock(JobsMutex_);
  1712. RegisterJobError();
  1713. }
  1714. // Private method that requires cmQtAutoGeneratorMocUic::JobsMutex_ to be
  1715. // locked
  1716. void cmQtAutoGeneratorMocUic::RegisterJobError()
  1717. {
  1718. JobError_ = true;
  1719. if (!JobThreadsAbort_) {
  1720. JobThreadsAbort_ = true;
  1721. // Clear remaining jobs
  1722. if (JobsRemain_ != 0) {
  1723. JobsRemain_ -= JobQueue_.size();
  1724. JobQueue_.clear();
  1725. }
  1726. }
  1727. }
  1728. bool cmQtAutoGeneratorMocUic::ParallelJobPushMoc(JobHandleT& jobHandle)
  1729. {
  1730. std::lock_guard<std::mutex> jobsLock(JobsMutex_);
  1731. if (!JobThreadsAbort_) {
  1732. bool pushJobHandle = true;
  1733. // Do additional tests if this is an included moc job
  1734. const JobMocT& mocJob(static_cast<JobMocT&>(*jobHandle));
  1735. if (!mocJob.IncludeString.empty()) {
  1736. // Register included moc file and look for collisions
  1737. MocIncludedFiles_.emplace(mocJob.SourceFile);
  1738. if (!MocIncludedStrings_.emplace(mocJob.IncludeString).second) {
  1739. // Another source file includes the same moc file!
  1740. for (const JobHandleT& otherHandle : JobQueues_.Moc) {
  1741. const JobMocT& otherJob(static_cast<JobMocT&>(*otherHandle));
  1742. if (otherJob.IncludeString == mocJob.IncludeString) {
  1743. // Check if the same moc file would be generated from different
  1744. // source files which is an error.
  1745. if (otherJob.SourceFile != mocJob.SourceFile) {
  1746. // Include string collision
  1747. std::string error = "The two source files\n ";
  1748. error += Quoted(mocJob.IncluderFile);
  1749. error += " and\n ";
  1750. error += Quoted(otherJob.IncluderFile);
  1751. error += "\ncontain the same moc include string ";
  1752. error += Quoted(mocJob.IncludeString);
  1753. error += "\nbut the moc file would be generated from different "
  1754. "source files\n ";
  1755. error += Quoted(mocJob.SourceFile);
  1756. error += " and\n ";
  1757. error += Quoted(otherJob.SourceFile);
  1758. error += ".\nConsider to\n"
  1759. "- not include the \"moc_<NAME>.cpp\" file\n"
  1760. "- add a directory prefix to a \"<NAME>.moc\" include "
  1761. "(e.g \"sub/<NAME>.moc\")\n"
  1762. "- rename the source file(s)\n";
  1763. Log().Error(GenT::MOC, error);
  1764. RegisterJobError();
  1765. }
  1766. // Do not push this job in since the included moc file already
  1767. // gets generated by an other job.
  1768. pushJobHandle = false;
  1769. break;
  1770. }
  1771. }
  1772. }
  1773. }
  1774. // Push job on demand
  1775. if (pushJobHandle) {
  1776. JobQueues_.Moc.emplace_back(std::move(jobHandle));
  1777. }
  1778. }
  1779. return !JobError_;
  1780. }
  1781. bool cmQtAutoGeneratorMocUic::ParallelJobPushUic(JobHandleT& jobHandle)
  1782. {
  1783. std::lock_guard<std::mutex> jobsLock(JobsMutex_);
  1784. if (!JobThreadsAbort_) {
  1785. bool pushJobHandle = true;
  1786. // Look for include collisions.
  1787. const JobUicT& uicJob(static_cast<JobUicT&>(*jobHandle));
  1788. for (const JobHandleT& otherHandle : JobQueues_.Uic) {
  1789. const JobUicT& otherJob(static_cast<JobUicT&>(*otherHandle));
  1790. if (otherJob.IncludeString == uicJob.IncludeString) {
  1791. // Check if the same uic file would be generated from different
  1792. // source files which would be an error.
  1793. if (otherJob.SourceFile != uicJob.SourceFile) {
  1794. // Include string collision
  1795. std::string error = "The two source files\n ";
  1796. error += Quoted(uicJob.IncluderFile);
  1797. error += " and\n ";
  1798. error += Quoted(otherJob.IncluderFile);
  1799. error += "\ncontain the same uic include string ";
  1800. error += Quoted(uicJob.IncludeString);
  1801. error += "\nbut the uic file would be generated from different "
  1802. "source files\n ";
  1803. error += Quoted(uicJob.SourceFile);
  1804. error += " and\n ";
  1805. error += Quoted(otherJob.SourceFile);
  1806. error +=
  1807. ".\nConsider to\n"
  1808. "- add a directory prefix to a \"ui_<NAME>.h\" include "
  1809. "(e.g \"sub/ui_<NAME>.h\")\n"
  1810. "- rename the <NAME>.ui file(s) and adjust the \"ui_<NAME>.h\" "
  1811. "include(s)\n";
  1812. Log().Error(GenT::UIC, error);
  1813. RegisterJobError();
  1814. }
  1815. // Do not push this job in since the uic file already
  1816. // gets generated by an other job.
  1817. pushJobHandle = false;
  1818. break;
  1819. }
  1820. }
  1821. if (pushJobHandle) {
  1822. JobQueues_.Uic.emplace_back(std::move(jobHandle));
  1823. }
  1824. }
  1825. return !JobError_;
  1826. }
  1827. bool cmQtAutoGeneratorMocUic::ParallelMocIncluded(
  1828. std::string const& sourceFile)
  1829. {
  1830. std::lock_guard<std::mutex> mocLock(JobsMutex_);
  1831. return (MocIncludedFiles_.find(sourceFile) != MocIncludedFiles_.end());
  1832. }
  1833. std::string cmQtAutoGeneratorMocUic::ParallelMocAutoRegister(
  1834. std::string const& baseName)
  1835. {
  1836. std::string res;
  1837. {
  1838. std::lock_guard<std::mutex> mocLock(JobsMutex_);
  1839. res = baseName;
  1840. res += ".cpp";
  1841. if (MocAutoFiles_.find(res) == MocAutoFiles_.end()) {
  1842. MocAutoFiles_.emplace(res);
  1843. } else {
  1844. // Append number suffix to the file name
  1845. for (unsigned int ii = 2; ii != 1024; ++ii) {
  1846. res = baseName;
  1847. res += '_';
  1848. res += std::to_string(ii);
  1849. res += ".cpp";
  1850. if (MocAutoFiles_.find(res) == MocAutoFiles_.end()) {
  1851. MocAutoFiles_.emplace(res);
  1852. break;
  1853. }
  1854. }
  1855. }
  1856. }
  1857. return res;
  1858. }
  1859. void cmQtAutoGeneratorMocUic::ParallelMocAutoUpdated()
  1860. {
  1861. std::lock_guard<std::mutex> mocLock(JobsMutex_);
  1862. MocAutoFileUpdated_ = true;
  1863. }
  1864. void cmQtAutoGeneratorMocUic::MocGenerateCompilation()
  1865. {
  1866. std::lock_guard<std::mutex> mocLock(JobsMutex_);
  1867. if (!JobError_ && Moc().Enabled) {
  1868. // Write mocs compilation build file
  1869. {
  1870. // Compose mocs compilation file content
  1871. std::string content =
  1872. "// This file is autogenerated. Changes will be overwritten.\n";
  1873. if (MocAutoFiles_.empty()) {
  1874. // Placeholder content
  1875. content += "// No files found that require moc or the moc files are "
  1876. "included\n";
  1877. content += "enum some_compilers { need_more_than_nothing };\n";
  1878. } else {
  1879. // Valid content
  1880. char const sbeg = Base().MultiConfig ? '<' : '"';
  1881. char const send = Base().MultiConfig ? '>' : '"';
  1882. for (std::string const& mocfile : MocAutoFiles_) {
  1883. content += "#include ";
  1884. content += sbeg;
  1885. content += mocfile;
  1886. content += send;
  1887. content += '\n';
  1888. }
  1889. }
  1890. std::string const& compAbs = Moc().CompFileAbs;
  1891. if (FileSys().FileDiffers(compAbs, content)) {
  1892. // Actually write mocs compilation file
  1893. if (Log().Verbose()) {
  1894. Log().Info(GenT::MOC, "Generating MOC compilation " + compAbs);
  1895. }
  1896. if (!FileSys().FileWrite(GenT::MOC, compAbs, content)) {
  1897. Log().ErrorFile(GenT::MOC, compAbs,
  1898. "mocs compilation file writing failed");
  1899. RegisterJobError();
  1900. return;
  1901. }
  1902. } else if (MocAutoFileUpdated_) {
  1903. // Only touch mocs compilation file
  1904. if (Log().Verbose()) {
  1905. Log().Info(GenT::MOC, "Touching mocs compilation " + compAbs);
  1906. }
  1907. FileSys().Touch(compAbs);
  1908. }
  1909. }
  1910. // Write mocs compilation wrapper file
  1911. if (Base().MultiConfig) {
  1912. }
  1913. }
  1914. }