cmQtAutoGeneratorMocUic.cxx 61 KB

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