cmQtAutoGeneratorMocUic.cxx 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026
  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 "cmQtAutoGen.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 FileSys->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->GetRealPath(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->GetRealPath(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 = wrk.FileSys().SubDirPrefix(FileName);
  156. meta.FileBase =
  157. wrk.FileSys().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(wrk.FileSys().SubDirPrefix(incString));
  207. std::string incBase =
  208. wrk.FileSys().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 dependencies 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 dependencies 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().GetRealPath(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. wrk.FileSys().GetFilenameWithoutLastExtension(includeString).substr(3);
  506. searchFile += ".ui";
  507. // Collect search paths list
  508. std::deque<std::string> testFiles;
  509. {
  510. std::string const searchPath = wrk.FileSys().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().GetRealPath(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.FileSys().GetFilePathChecksum(SourceFile);
  641. rel += "/moc_";
  642. rel += wrk.FileSys().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 = wrk.FileSys().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. // Print moc output
  808. if (!result.StdOut.empty()) {
  809. wrk.LogInfo(GeneratorT::MOC, result.StdOut);
  810. }
  811. // Notify the generator that a not included file changed (on demand)
  812. if (IncludeString.empty()) {
  813. wrk.Gen().ParallelMocAutoUpdated();
  814. }
  815. } else {
  816. // Moc command failed
  817. {
  818. std::string emsg = "The moc process failed to compile\n ";
  819. emsg += Quoted(SourceFile);
  820. emsg += "\ninto\n ";
  821. emsg += Quoted(BuildFile);
  822. emsg += ".\n";
  823. emsg += result.ErrorMessage;
  824. wrk.LogCommandError(GeneratorT::MOC, emsg, cmd, result.StdOut);
  825. }
  826. wrk.FileSys().FileRemove(BuildFile);
  827. }
  828. }
  829. }
  830. void cmQtAutoGeneratorMocUic::JobUicT::Process(WorkerT& wrk)
  831. {
  832. // Compute build file name
  833. BuildFile = wrk.Base().AutogenIncludeDir;
  834. BuildFile += '/';
  835. BuildFile += IncludeString;
  836. if (UpdateRequired(wrk)) {
  837. GenerateUic(wrk);
  838. }
  839. }
  840. bool cmQtAutoGeneratorMocUic::JobUicT::UpdateRequired(WorkerT& wrk)
  841. {
  842. bool const verbose = wrk.Gen().Log().Verbose();
  843. // Test if the build file exists
  844. if (!wrk.FileSys().FileExists(BuildFile)) {
  845. if (verbose) {
  846. std::string reason = "Generating ";
  847. reason += Quoted(BuildFile);
  848. reason += " from its source file ";
  849. reason += Quoted(SourceFile);
  850. reason += " because it doesn't exist";
  851. wrk.LogInfo(GeneratorT::UIC, reason);
  852. }
  853. return true;
  854. }
  855. // Test if the uic settings changed
  856. if (wrk.Uic().SettingsChanged) {
  857. if (verbose) {
  858. std::string reason = "Generating ";
  859. reason += Quoted(BuildFile);
  860. reason += " from ";
  861. reason += Quoted(SourceFile);
  862. reason += " because the UIC settings changed";
  863. wrk.LogInfo(GeneratorT::UIC, reason);
  864. }
  865. return true;
  866. }
  867. // Test if the source file is newer
  868. {
  869. bool isOlder = false;
  870. {
  871. std::string error;
  872. isOlder = wrk.FileSys().FileIsOlderThan(BuildFile, SourceFile, &error);
  873. if (!isOlder && !error.empty()) {
  874. wrk.LogError(GeneratorT::UIC, error);
  875. return false;
  876. }
  877. }
  878. if (isOlder) {
  879. if (verbose) {
  880. std::string reason = "Generating ";
  881. reason += Quoted(BuildFile);
  882. reason += " because it's older than its source file ";
  883. reason += Quoted(SourceFile);
  884. wrk.LogInfo(GeneratorT::UIC, reason);
  885. }
  886. return true;
  887. }
  888. }
  889. return false;
  890. }
  891. void cmQtAutoGeneratorMocUic::JobUicT::GenerateUic(WorkerT& wrk)
  892. {
  893. // Make sure the parent directory exists
  894. if (wrk.FileSys().MakeParentDirectory(GeneratorT::UIC, BuildFile)) {
  895. // Compose uic command
  896. std::vector<std::string> cmd;
  897. cmd.push_back(wrk.Uic().Executable);
  898. {
  899. std::vector<std::string> allOpts = wrk.Uic().TargetOptions;
  900. auto optionIt = wrk.Uic().Options.find(SourceFile);
  901. if (optionIt != wrk.Uic().Options.end()) {
  902. UicMergeOptions(allOpts, optionIt->second,
  903. (wrk.Base().QtVersionMajor == 5));
  904. }
  905. cmd.insert(cmd.end(), allOpts.begin(), allOpts.end());
  906. }
  907. cmd.push_back("-o");
  908. cmd.push_back(BuildFile);
  909. cmd.push_back(SourceFile);
  910. ProcessResultT result;
  911. if (wrk.RunProcess(GeneratorT::UIC, result, cmd)) {
  912. // Uic command success
  913. // Print uic output
  914. if (!result.StdOut.empty()) {
  915. wrk.LogInfo(GeneratorT::UIC, result.StdOut);
  916. }
  917. } else {
  918. // Uic command failed
  919. {
  920. std::string emsg = "The uic process failed to compile\n ";
  921. emsg += Quoted(SourceFile);
  922. emsg += "\ninto\n ";
  923. emsg += Quoted(BuildFile);
  924. emsg += "\nincluded by\n ";
  925. emsg += Quoted(IncluderFile);
  926. emsg += ".\n";
  927. emsg += result.ErrorMessage;
  928. wrk.LogCommandError(GeneratorT::UIC, emsg, cmd, result.StdOut);
  929. }
  930. wrk.FileSys().FileRemove(BuildFile);
  931. }
  932. }
  933. }
  934. void cmQtAutoGeneratorMocUic::JobDeleterT::operator()(JobT* job)
  935. {
  936. delete job;
  937. }
  938. cmQtAutoGeneratorMocUic::WorkerT::WorkerT(cmQtAutoGeneratorMocUic* gen,
  939. uv_loop_t* uvLoop)
  940. : Gen_(gen)
  941. {
  942. // Initialize uv asynchronous callback for process starting
  943. ProcessRequest_.init(*uvLoop, &WorkerT::UVProcessStart, this);
  944. // Start thread
  945. Thread_ = std::thread(&WorkerT::Loop, this);
  946. }
  947. cmQtAutoGeneratorMocUic::WorkerT::~WorkerT()
  948. {
  949. // Join thread
  950. if (Thread_.joinable()) {
  951. Thread_.join();
  952. }
  953. }
  954. void cmQtAutoGeneratorMocUic::WorkerT::LogInfo(
  955. GeneratorT genType, std::string const& message) const
  956. {
  957. return Log().Info(genType, message);
  958. }
  959. void cmQtAutoGeneratorMocUic::WorkerT::LogWarning(
  960. GeneratorT genType, std::string const& message) const
  961. {
  962. return Log().Warning(genType, message);
  963. }
  964. void cmQtAutoGeneratorMocUic::WorkerT::LogFileWarning(
  965. GeneratorT genType, std::string const& filename,
  966. std::string const& message) const
  967. {
  968. return Log().WarningFile(genType, filename, message);
  969. }
  970. void cmQtAutoGeneratorMocUic::WorkerT::LogError(
  971. GeneratorT genType, std::string const& message) const
  972. {
  973. Gen().ParallelRegisterJobError();
  974. Log().Error(genType, message);
  975. }
  976. void cmQtAutoGeneratorMocUic::WorkerT::LogFileError(
  977. GeneratorT genType, std::string const& filename,
  978. std::string const& message) const
  979. {
  980. Gen().ParallelRegisterJobError();
  981. Log().ErrorFile(genType, filename, message);
  982. }
  983. void cmQtAutoGeneratorMocUic::WorkerT::LogCommandError(
  984. GeneratorT genType, std::string const& message,
  985. std::vector<std::string> const& command, std::string const& output) const
  986. {
  987. Gen().ParallelRegisterJobError();
  988. Log().ErrorCommand(genType, message, command, output);
  989. }
  990. bool cmQtAutoGeneratorMocUic::WorkerT::RunProcess(
  991. GeneratorT genType, ProcessResultT& result,
  992. std::vector<std::string> const& command)
  993. {
  994. if (command.empty()) {
  995. return false;
  996. }
  997. // Create process instance
  998. {
  999. std::lock_guard<std::mutex> lock(ProcessMutex_);
  1000. Process_ = cm::make_unique<ReadOnlyProcessT>();
  1001. Process_->setup(&result, true, command, Gen().Base().AutogenBuildDir);
  1002. }
  1003. // Send asynchronous process start request to libuv loop
  1004. ProcessRequest_.send();
  1005. // Log command
  1006. if (this->Log().Verbose()) {
  1007. std::string msg = "Running command:\n";
  1008. msg += QuotedCommand(command);
  1009. msg += '\n';
  1010. this->LogInfo(genType, msg);
  1011. }
  1012. // Wait until the process has been finished and destroyed
  1013. {
  1014. std::unique_lock<std::mutex> ulock(ProcessMutex_);
  1015. while (Process_) {
  1016. ProcessCondition_.wait(ulock);
  1017. }
  1018. }
  1019. return !result.error();
  1020. }
  1021. void cmQtAutoGeneratorMocUic::WorkerT::Loop()
  1022. {
  1023. while (true) {
  1024. Gen().WorkerSwapJob(JobHandle_);
  1025. if (JobHandle_) {
  1026. JobHandle_->Process(*this);
  1027. } else {
  1028. break;
  1029. }
  1030. }
  1031. }
  1032. void cmQtAutoGeneratorMocUic::WorkerT::UVProcessStart(uv_async_t* handle)
  1033. {
  1034. auto& wrk = *reinterpret_cast<WorkerT*>(handle->data);
  1035. {
  1036. std::lock_guard<std::mutex> lock(wrk.ProcessMutex_);
  1037. if (wrk.Process_ && !wrk.Process_->IsStarted()) {
  1038. wrk.Process_->start(handle->loop,
  1039. std::bind(&WorkerT::UVProcessFinished, &wrk));
  1040. }
  1041. }
  1042. }
  1043. void cmQtAutoGeneratorMocUic::WorkerT::UVProcessFinished()
  1044. {
  1045. {
  1046. std::lock_guard<std::mutex> lock(ProcessMutex_);
  1047. if (Process_ && Process_->IsFinished()) {
  1048. Process_.reset();
  1049. }
  1050. }
  1051. // Notify idling thread
  1052. ProcessCondition_.notify_one();
  1053. }
  1054. cmQtAutoGeneratorMocUic::cmQtAutoGeneratorMocUic()
  1055. : Base_(&FileSys())
  1056. , Moc_(&FileSys())
  1057. , Stage_(StageT::SETTINGS_READ)
  1058. , JobsRemain_(0)
  1059. , JobError_(false)
  1060. , JobThreadsAbort_(false)
  1061. , MocAutoFileUpdated_(false)
  1062. {
  1063. // Precompile regular expressions
  1064. Moc_.RegExpInclude.compile(
  1065. "(^|\n)[ \t]*#[ \t]*include[ \t]+"
  1066. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  1067. Uic_.RegExpInclude.compile("(^|\n)[ \t]*#[ \t]*include[ \t]+"
  1068. "[\"<](([^ \">]+/)?ui_[^ \">/]+\\.h)[\">]");
  1069. // Initialize libuv asynchronous iteration request
  1070. UVRequest().init(*UVLoop(), &cmQtAutoGeneratorMocUic::UVPollStage, this);
  1071. }
  1072. cmQtAutoGeneratorMocUic::~cmQtAutoGeneratorMocUic()
  1073. {
  1074. }
  1075. bool cmQtAutoGeneratorMocUic::Init(cmMakefile* makefile)
  1076. {
  1077. // -- Meta
  1078. Base_.HeaderExtensions = makefile->GetCMakeInstance()->GetHeaderExtensions();
  1079. // Utility lambdas
  1080. auto InfoGet = [makefile](const char* key) {
  1081. return makefile->GetSafeDefinition(key);
  1082. };
  1083. auto InfoGetBool = [makefile](const char* key) {
  1084. return makefile->IsOn(key);
  1085. };
  1086. auto InfoGetList = [makefile](const char* key) -> std::vector<std::string> {
  1087. std::vector<std::string> list;
  1088. cmSystemTools::ExpandListArgument(makefile->GetSafeDefinition(key), list);
  1089. return list;
  1090. };
  1091. auto InfoGetLists =
  1092. [makefile](const char* key) -> std::vector<std::vector<std::string>> {
  1093. std::vector<std::vector<std::string>> lists;
  1094. {
  1095. std::string const value = makefile->GetSafeDefinition(key);
  1096. std::string::size_type pos = 0;
  1097. while (pos < value.size()) {
  1098. std::string::size_type next = value.find(ListSep, pos);
  1099. std::string::size_type length =
  1100. (next != std::string::npos) ? next - pos : value.size() - pos;
  1101. // Remove enclosing braces
  1102. if (length >= 2) {
  1103. std::string::const_iterator itBeg = value.begin() + (pos + 1);
  1104. std::string::const_iterator itEnd = itBeg + (length - 2);
  1105. {
  1106. std::string subValue(itBeg, itEnd);
  1107. std::vector<std::string> list;
  1108. cmSystemTools::ExpandListArgument(subValue, list);
  1109. lists.push_back(std::move(list));
  1110. }
  1111. }
  1112. pos += length;
  1113. pos += ListSep.size();
  1114. }
  1115. }
  1116. return lists;
  1117. };
  1118. auto InfoGetConfig = [makefile, this](const char* key) -> std::string {
  1119. const char* valueConf = nullptr;
  1120. {
  1121. std::string keyConf = key;
  1122. keyConf += '_';
  1123. keyConf += InfoConfig();
  1124. valueConf = makefile->GetDefinition(keyConf);
  1125. }
  1126. if (valueConf == nullptr) {
  1127. return makefile->GetSafeDefinition(key);
  1128. }
  1129. return std::string(valueConf);
  1130. };
  1131. auto InfoGetConfigList =
  1132. [&InfoGetConfig](const char* key) -> std::vector<std::string> {
  1133. std::vector<std::string> list;
  1134. cmSystemTools::ExpandListArgument(InfoGetConfig(key), list);
  1135. return list;
  1136. };
  1137. // -- Read info file
  1138. if (!makefile->ReadListFile(InfoFile().c_str())) {
  1139. Log().ErrorFile(GeneratorT::GEN, InfoFile(), "File processing failed");
  1140. return false;
  1141. }
  1142. // -- Meta
  1143. Log().RaiseVerbosity(InfoGet("AM_VERBOSITY"));
  1144. Base_.MultiConfig = InfoGetBool("AM_MULTI_CONFIG");
  1145. {
  1146. unsigned long num = Base_.NumThreads;
  1147. if (cmSystemTools::StringToULong(InfoGet("AM_PARALLEL").c_str(), &num)) {
  1148. num = std::max<unsigned long>(num, 1);
  1149. num = std::min<unsigned long>(num, ParallelMax);
  1150. Base_.NumThreads = static_cast<unsigned int>(num);
  1151. }
  1152. }
  1153. // - Files and directories
  1154. Base_.ProjectSourceDir = InfoGet("AM_CMAKE_SOURCE_DIR");
  1155. Base_.ProjectBinaryDir = InfoGet("AM_CMAKE_BINARY_DIR");
  1156. Base_.CurrentSourceDir = InfoGet("AM_CMAKE_CURRENT_SOURCE_DIR");
  1157. Base_.CurrentBinaryDir = InfoGet("AM_CMAKE_CURRENT_BINARY_DIR");
  1158. Base_.IncludeProjectDirsBefore =
  1159. InfoGetBool("AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE");
  1160. Base_.AutogenBuildDir = InfoGet("AM_BUILD_DIR");
  1161. if (Base_.AutogenBuildDir.empty()) {
  1162. Log().ErrorFile(GeneratorT::GEN, InfoFile(),
  1163. "Autogen build directory missing");
  1164. return false;
  1165. }
  1166. // include directory
  1167. Base_.AutogenIncludeDir = InfoGetConfig("AM_INCLUDE_DIR");
  1168. if (Base_.AutogenIncludeDir.empty()) {
  1169. Log().ErrorFile(GeneratorT::GEN, InfoFile(),
  1170. "Autogen include directory missing");
  1171. return false;
  1172. }
  1173. // - Files
  1174. SettingsFile_ = InfoGetConfig("AM_SETTINGS_FILE");
  1175. if (SettingsFile_.empty()) {
  1176. Log().ErrorFile(GeneratorT::GEN, InfoFile(), "Settings file name missing");
  1177. return false;
  1178. }
  1179. // - Qt environment
  1180. {
  1181. unsigned long qtv = Base_.QtVersionMajor;
  1182. if (cmSystemTools::StringToULong(InfoGet("AM_QT_VERSION_MAJOR").c_str(),
  1183. &qtv)) {
  1184. Base_.QtVersionMajor = static_cast<unsigned int>(qtv);
  1185. }
  1186. }
  1187. // - Moc
  1188. Moc_.Executable = InfoGet("AM_QT_MOC_EXECUTABLE");
  1189. Moc_.Enabled = !Moc().Executable.empty();
  1190. if (Moc().Enabled) {
  1191. {
  1192. auto lst = InfoGetList("AM_MOC_SKIP");
  1193. Moc_.SkipList.insert(lst.begin(), lst.end());
  1194. }
  1195. Moc_.Definitions = InfoGetConfigList("AM_MOC_DEFINITIONS");
  1196. Moc_.IncludePaths = InfoGetConfigList("AM_MOC_INCLUDES");
  1197. Moc_.Options = InfoGetList("AM_MOC_OPTIONS");
  1198. Moc_.RelaxedMode = InfoGetBool("AM_MOC_RELAXED_MODE");
  1199. for (std::string const& item : InfoGetList("AM_MOC_MACRO_NAMES")) {
  1200. Moc_.MacroFilters.emplace_back(
  1201. item, ("[\n][ \t]*{?[ \t]*" + item).append("[^a-zA-Z0-9_]"));
  1202. }
  1203. {
  1204. auto pushFilter = [this](std::string const& key, std::string const& exp,
  1205. std::string& error) {
  1206. if (!key.empty()) {
  1207. if (!exp.empty()) {
  1208. Moc_.DependFilters.push_back(KeyExpT());
  1209. KeyExpT& filter(Moc_.DependFilters.back());
  1210. if (filter.Exp.compile(exp)) {
  1211. filter.Key = key;
  1212. } else {
  1213. error = "Regular expression compiling failed";
  1214. }
  1215. } else {
  1216. error = "Regular expression is empty";
  1217. }
  1218. } else {
  1219. error = "Key is empty";
  1220. }
  1221. if (!error.empty()) {
  1222. error = ("AUTOMOC_DEPEND_FILTERS: " + error);
  1223. error += "\n";
  1224. error += " Key: ";
  1225. error += Quoted(key);
  1226. error += "\n";
  1227. error += " Exp: ";
  1228. error += Quoted(exp);
  1229. error += "\n";
  1230. }
  1231. };
  1232. std::string error;
  1233. // Insert default filter for Q_PLUGIN_METADATA
  1234. if (Base().QtVersionMajor != 4) {
  1235. pushFilter("Q_PLUGIN_METADATA",
  1236. "[\n][ \t]*Q_PLUGIN_METADATA[ \t]*\\("
  1237. "[^\\)]*FILE[ \t]*\"([^\"]+)\"",
  1238. error);
  1239. }
  1240. // Insert user defined dependency filters
  1241. {
  1242. std::vector<std::string> flts = InfoGetList("AM_MOC_DEPEND_FILTERS");
  1243. if ((flts.size() % 2) == 0) {
  1244. for (std::vector<std::string>::iterator itC = flts.begin(),
  1245. itE = flts.end();
  1246. itC != itE; itC += 2) {
  1247. pushFilter(*itC, *(itC + 1), error);
  1248. if (!error.empty()) {
  1249. break;
  1250. }
  1251. }
  1252. } else {
  1253. Log().ErrorFile(
  1254. GeneratorT::MOC, InfoFile(),
  1255. "AUTOMOC_DEPEND_FILTERS list size is not a multiple of 2");
  1256. return false;
  1257. }
  1258. }
  1259. if (!error.empty()) {
  1260. Log().ErrorFile(GeneratorT::MOC, InfoFile(), error);
  1261. return false;
  1262. }
  1263. }
  1264. Moc_.PredefsCmd = InfoGetList("AM_MOC_PREDEFS_CMD");
  1265. // Install moc predefs job
  1266. if (!Moc().PredefsCmd.empty()) {
  1267. JobQueues_.MocPredefs.emplace_back(new JobMocPredefsT());
  1268. }
  1269. }
  1270. // - Uic
  1271. Uic_.Executable = InfoGet("AM_QT_UIC_EXECUTABLE");
  1272. Uic_.Enabled = !Uic().Executable.empty();
  1273. if (Uic().Enabled) {
  1274. {
  1275. auto lst = InfoGetList("AM_UIC_SKIP");
  1276. Uic_.SkipList.insert(lst.begin(), lst.end());
  1277. }
  1278. Uic_.SearchPaths = InfoGetList("AM_UIC_SEARCH_PATHS");
  1279. Uic_.TargetOptions = InfoGetConfigList("AM_UIC_TARGET_OPTIONS");
  1280. {
  1281. auto sources = InfoGetList("AM_UIC_OPTIONS_FILES");
  1282. auto options = InfoGetLists("AM_UIC_OPTIONS_OPTIONS");
  1283. // Compare list sizes
  1284. if (sources.size() != options.size()) {
  1285. std::ostringstream ost;
  1286. ost << "files/options lists sizes mismatch (" << sources.size() << "/"
  1287. << options.size() << ")";
  1288. Log().ErrorFile(GeneratorT::UIC, InfoFile(), ost.str());
  1289. return false;
  1290. }
  1291. auto fitEnd = sources.cend();
  1292. auto fit = sources.begin();
  1293. auto oit = options.begin();
  1294. while (fit != fitEnd) {
  1295. Uic_.Options[*fit] = std::move(*oit);
  1296. ++fit;
  1297. ++oit;
  1298. }
  1299. }
  1300. }
  1301. // Initialize source file jobs
  1302. {
  1303. std::hash<std::string> stringHash;
  1304. std::set<std::size_t> uniqueHeaders;
  1305. // Add header jobs
  1306. for (std::string& hdr : InfoGetList("AM_HEADERS")) {
  1307. const bool moc = !Moc().skipped(hdr);
  1308. const bool uic = !Uic().skipped(hdr);
  1309. if ((moc || uic) && uniqueHeaders.emplace(stringHash(hdr)).second) {
  1310. JobQueues_.Headers.emplace_back(
  1311. new JobParseT(std::move(hdr), moc, uic, true));
  1312. }
  1313. }
  1314. // Add source jobs
  1315. {
  1316. std::vector<std::string> sources = InfoGetList("AM_SOURCES");
  1317. // Add header(s) for the source file
  1318. for (std::string& src : sources) {
  1319. const bool srcMoc = !Moc().skipped(src);
  1320. const bool srcUic = !Uic().skipped(src);
  1321. if (!srcMoc && !srcUic) {
  1322. continue;
  1323. }
  1324. // Search for the default header file and a private header
  1325. {
  1326. std::array<std::string, 2> bases;
  1327. bases[0] = FileSys().SubDirPrefix(src);
  1328. bases[0] += FileSys().GetFilenameWithoutLastExtension(src);
  1329. bases[1] = bases[0];
  1330. bases[1] += "_p";
  1331. for (std::string const& headerBase : bases) {
  1332. std::string header;
  1333. if (Base().FindHeader(header, headerBase)) {
  1334. const bool moc = srcMoc && !Moc().skipped(header);
  1335. const bool uic = srcUic && !Uic().skipped(header);
  1336. if ((moc || uic) &&
  1337. uniqueHeaders.emplace(stringHash(header)).second) {
  1338. JobQueues_.Headers.emplace_back(
  1339. new JobParseT(std::move(header), moc, uic, true));
  1340. }
  1341. }
  1342. }
  1343. }
  1344. // Add source job
  1345. JobQueues_.Sources.emplace_back(
  1346. new JobParseT(std::move(src), srcMoc, srcUic));
  1347. }
  1348. }
  1349. }
  1350. // Init derived information
  1351. // ------------------------
  1352. // Init file path checksum generator
  1353. FileSys().setupFilePathChecksum(
  1354. Base().CurrentSourceDir, Base().CurrentBinaryDir, Base().ProjectSourceDir,
  1355. Base().ProjectBinaryDir);
  1356. // Moc variables
  1357. if (Moc().Enabled) {
  1358. // Mocs compilation file
  1359. Moc_.CompFileAbs = Base().AbsoluteBuildPath("mocs_compilation.cpp");
  1360. // Moc predefs file
  1361. if (!Moc_.PredefsCmd.empty()) {
  1362. Moc_.PredefsFileRel = "moc_predefs";
  1363. if (Base_.MultiConfig) {
  1364. Moc_.PredefsFileRel += '_';
  1365. Moc_.PredefsFileRel += InfoConfig();
  1366. }
  1367. Moc_.PredefsFileRel += ".h";
  1368. Moc_.PredefsFileAbs = Base_.AbsoluteBuildPath(Moc().PredefsFileRel);
  1369. }
  1370. // Sort include directories on demand
  1371. if (Base().IncludeProjectDirsBefore) {
  1372. // Move strings to temporary list
  1373. std::list<std::string> includes;
  1374. includes.insert(includes.end(), Moc().IncludePaths.begin(),
  1375. Moc().IncludePaths.end());
  1376. Moc_.IncludePaths.clear();
  1377. Moc_.IncludePaths.reserve(includes.size());
  1378. // Append project directories only
  1379. {
  1380. std::array<std::string const*, 2> const movePaths = {
  1381. { &Base().ProjectBinaryDir, &Base().ProjectSourceDir }
  1382. };
  1383. for (std::string const* ppath : movePaths) {
  1384. std::list<std::string>::iterator it = includes.begin();
  1385. while (it != includes.end()) {
  1386. std::string const& path = *it;
  1387. if (cmSystemTools::StringStartsWith(path, ppath->c_str())) {
  1388. Moc_.IncludePaths.push_back(path);
  1389. it = includes.erase(it);
  1390. } else {
  1391. ++it;
  1392. }
  1393. }
  1394. }
  1395. }
  1396. // Append remaining directories
  1397. Moc_.IncludePaths.insert(Moc_.IncludePaths.end(), includes.begin(),
  1398. includes.end());
  1399. }
  1400. // Compose moc includes list
  1401. {
  1402. std::set<std::string> frameworkPaths;
  1403. for (std::string const& path : Moc().IncludePaths) {
  1404. Moc_.Includes.push_back("-I" + path);
  1405. // Extract framework path
  1406. if (cmHasLiteralSuffix(path, ".framework/Headers")) {
  1407. // Go up twice to get to the framework root
  1408. std::vector<std::string> pathComponents;
  1409. FileSys().SplitPath(path, pathComponents);
  1410. std::string frameworkPath = FileSys().JoinPath(
  1411. pathComponents.begin(), pathComponents.end() - 2);
  1412. frameworkPaths.insert(frameworkPath);
  1413. }
  1414. }
  1415. // Append framework includes
  1416. for (std::string const& path : frameworkPaths) {
  1417. Moc_.Includes.push_back("-F");
  1418. Moc_.Includes.push_back(path);
  1419. }
  1420. }
  1421. // Setup single list with all options
  1422. {
  1423. // Add includes
  1424. Moc_.AllOptions.insert(Moc_.AllOptions.end(), Moc().Includes.begin(),
  1425. Moc().Includes.end());
  1426. // Add definitions
  1427. for (std::string const& def : Moc().Definitions) {
  1428. Moc_.AllOptions.push_back("-D" + def);
  1429. }
  1430. // Add options
  1431. Moc_.AllOptions.insert(Moc_.AllOptions.end(), Moc().Options.begin(),
  1432. Moc().Options.end());
  1433. }
  1434. }
  1435. return true;
  1436. }
  1437. bool cmQtAutoGeneratorMocUic::Process()
  1438. {
  1439. // Run libuv event loop
  1440. UVRequest().send();
  1441. if (uv_run(UVLoop(), UV_RUN_DEFAULT) == 0) {
  1442. if (JobError_) {
  1443. return false;
  1444. }
  1445. } else {
  1446. return false;
  1447. }
  1448. return true;
  1449. }
  1450. void cmQtAutoGeneratorMocUic::UVPollStage(uv_async_t* handle)
  1451. {
  1452. reinterpret_cast<cmQtAutoGeneratorMocUic*>(handle->data)->PollStage();
  1453. }
  1454. void cmQtAutoGeneratorMocUic::PollStage()
  1455. {
  1456. switch (Stage_) {
  1457. case StageT::SETTINGS_READ:
  1458. SettingsFileRead();
  1459. SetStage(StageT::CREATE_DIRECTORIES);
  1460. break;
  1461. case StageT::CREATE_DIRECTORIES:
  1462. CreateDirectories();
  1463. SetStage(StageT::PARSE_SOURCES);
  1464. break;
  1465. case StageT::PARSE_SOURCES:
  1466. if (ThreadsStartJobs(JobQueues_.Sources)) {
  1467. SetStage(StageT::PARSE_HEADERS);
  1468. }
  1469. break;
  1470. case StageT::PARSE_HEADERS:
  1471. if (ThreadsStartJobs(JobQueues_.Headers)) {
  1472. SetStage(StageT::MOC_PREDEFS);
  1473. }
  1474. break;
  1475. case StageT::MOC_PREDEFS:
  1476. if (ThreadsStartJobs(JobQueues_.MocPredefs)) {
  1477. SetStage(StageT::MOC_PROCESS);
  1478. }
  1479. break;
  1480. case StageT::MOC_PROCESS:
  1481. if (ThreadsStartJobs(JobQueues_.Moc)) {
  1482. SetStage(StageT::MOCS_COMPILATION);
  1483. }
  1484. break;
  1485. case StageT::MOCS_COMPILATION:
  1486. if (ThreadsJobsDone()) {
  1487. MocGenerateCompilation();
  1488. SetStage(StageT::UIC_PROCESS);
  1489. }
  1490. break;
  1491. case StageT::UIC_PROCESS:
  1492. if (ThreadsStartJobs(JobQueues_.Uic)) {
  1493. SetStage(StageT::SETTINGS_WRITE);
  1494. }
  1495. break;
  1496. case StageT::SETTINGS_WRITE:
  1497. SettingsFileWrite();
  1498. SetStage(StageT::FINISH);
  1499. break;
  1500. case StageT::FINISH:
  1501. if (ThreadsJobsDone()) {
  1502. // Clear all libuv handles
  1503. ThreadsStop();
  1504. UVRequest().reset();
  1505. // Set highest END stage manually
  1506. Stage_ = StageT::END;
  1507. }
  1508. break;
  1509. case StageT::END:
  1510. break;
  1511. }
  1512. }
  1513. void cmQtAutoGeneratorMocUic::SetStage(StageT stage)
  1514. {
  1515. if (JobError_) {
  1516. stage = StageT::FINISH;
  1517. }
  1518. // Only allow to increase the stage
  1519. if (Stage_ < stage) {
  1520. Stage_ = stage;
  1521. UVRequest().send();
  1522. }
  1523. }
  1524. void cmQtAutoGeneratorMocUic::SettingsFileRead()
  1525. {
  1526. // Compose current settings strings
  1527. {
  1528. cmCryptoHash crypt(cmCryptoHash::AlgoSHA256);
  1529. std::string const sep(" ~~~ ");
  1530. if (Moc_.Enabled) {
  1531. std::string str;
  1532. str += Moc().Executable;
  1533. str += sep;
  1534. str += cmJoin(Moc().AllOptions, ";");
  1535. str += sep;
  1536. str += Base().IncludeProjectDirsBefore ? "TRUE" : "FALSE";
  1537. str += sep;
  1538. str += cmJoin(Moc().PredefsCmd, ";");
  1539. str += sep;
  1540. SettingsStringMoc_ = crypt.HashString(str);
  1541. }
  1542. if (Uic().Enabled) {
  1543. std::string str;
  1544. str += Uic().Executable;
  1545. str += sep;
  1546. str += cmJoin(Uic().TargetOptions, ";");
  1547. for (const auto& item : Uic().Options) {
  1548. str += sep;
  1549. str += item.first;
  1550. str += sep;
  1551. str += cmJoin(item.second, ";");
  1552. }
  1553. str += sep;
  1554. SettingsStringUic_ = crypt.HashString(str);
  1555. }
  1556. }
  1557. // Read old settings and compare
  1558. {
  1559. std::string content;
  1560. if (FileSys().FileRead(content, SettingsFile_)) {
  1561. if (Moc().Enabled) {
  1562. if (SettingsStringMoc_ != SettingsFind(content, "moc")) {
  1563. Moc_.SettingsChanged = true;
  1564. }
  1565. }
  1566. if (Uic().Enabled) {
  1567. if (SettingsStringUic_ != SettingsFind(content, "uic")) {
  1568. Uic_.SettingsChanged = true;
  1569. }
  1570. }
  1571. // In case any setting changed remove the old settings file.
  1572. // This triggers a full rebuild on the next run if the current
  1573. // build is aborted before writing the current settings in the end.
  1574. if (Moc().SettingsChanged || Uic().SettingsChanged) {
  1575. FileSys().FileRemove(SettingsFile_);
  1576. }
  1577. } else {
  1578. // Settings file read failed
  1579. if (Moc().Enabled) {
  1580. Moc_.SettingsChanged = true;
  1581. }
  1582. if (Uic().Enabled) {
  1583. Uic_.SettingsChanged = true;
  1584. }
  1585. }
  1586. }
  1587. }
  1588. void cmQtAutoGeneratorMocUic::SettingsFileWrite()
  1589. {
  1590. std::lock_guard<std::mutex> jobsLock(JobsMutex_);
  1591. // Only write if any setting changed
  1592. if (!JobError_ && (Moc().SettingsChanged || Uic().SettingsChanged)) {
  1593. if (Log().Verbose()) {
  1594. Log().Info(GeneratorT::GEN,
  1595. "Writing settings file " + Quoted(SettingsFile_));
  1596. }
  1597. // Compose settings file content
  1598. std::string content;
  1599. {
  1600. auto SettingAppend = [&content](const char* key,
  1601. std::string const& value) {
  1602. if (!value.empty()) {
  1603. content += key;
  1604. content += ':';
  1605. content += value;
  1606. content += '\n';
  1607. }
  1608. };
  1609. SettingAppend("moc", SettingsStringMoc_);
  1610. SettingAppend("uic", SettingsStringUic_);
  1611. }
  1612. // Write settings file
  1613. if (!FileSys().FileWrite(GeneratorT::GEN, SettingsFile_, content)) {
  1614. Log().ErrorFile(GeneratorT::GEN, SettingsFile_,
  1615. "Settings file writing failed");
  1616. // Remove old settings file to trigger a full rebuild on the next run
  1617. FileSys().FileRemove(SettingsFile_);
  1618. RegisterJobError();
  1619. }
  1620. }
  1621. }
  1622. void cmQtAutoGeneratorMocUic::CreateDirectories()
  1623. {
  1624. // Create AUTOGEN include directory
  1625. if (!FileSys().MakeDirectory(GeneratorT::GEN, Base().AutogenIncludeDir)) {
  1626. RegisterJobError();
  1627. }
  1628. }
  1629. bool cmQtAutoGeneratorMocUic::ThreadsStartJobs(JobQueueT& queue)
  1630. {
  1631. bool done = false;
  1632. std::size_t queueSize = queue.size();
  1633. // Change the active queue
  1634. {
  1635. std::lock_guard<std::mutex> jobsLock(JobsMutex_);
  1636. // Check if there are still unfinished jobs from the previous queue
  1637. if (JobsRemain_ == 0) {
  1638. if (!JobThreadsAbort_) {
  1639. JobQueue_.swap(queue);
  1640. JobsRemain_ = queueSize;
  1641. } else {
  1642. // Abort requested
  1643. queue.clear();
  1644. queueSize = 0;
  1645. }
  1646. done = true;
  1647. }
  1648. }
  1649. if (done && (queueSize != 0)) {
  1650. // Start new threads on demand
  1651. if (Workers_.empty()) {
  1652. Workers_.resize(Base().NumThreads);
  1653. for (auto& item : Workers_) {
  1654. item = cm::make_unique<WorkerT>(this, UVLoop());
  1655. }
  1656. } else {
  1657. // Notify threads
  1658. if (queueSize == 1) {
  1659. JobsConditionRead_.notify_one();
  1660. } else {
  1661. JobsConditionRead_.notify_all();
  1662. }
  1663. }
  1664. }
  1665. return done;
  1666. }
  1667. void cmQtAutoGeneratorMocUic::ThreadsStop()
  1668. {
  1669. if (!Workers_.empty()) {
  1670. // Clear all jobs
  1671. {
  1672. std::lock_guard<std::mutex> jobsLock(JobsMutex_);
  1673. JobThreadsAbort_ = true;
  1674. JobsRemain_ -= JobQueue_.size();
  1675. JobQueue_.clear();
  1676. JobQueues_.Sources.clear();
  1677. JobQueues_.Headers.clear();
  1678. JobQueues_.MocPredefs.clear();
  1679. JobQueues_.Moc.clear();
  1680. JobQueues_.Uic.clear();
  1681. }
  1682. // Wake threads
  1683. JobsConditionRead_.notify_all();
  1684. // Join and clear threads
  1685. Workers_.clear();
  1686. }
  1687. }
  1688. bool cmQtAutoGeneratorMocUic::ThreadsJobsDone()
  1689. {
  1690. std::lock_guard<std::mutex> jobsLock(JobsMutex_);
  1691. return (JobsRemain_ == 0);
  1692. }
  1693. void cmQtAutoGeneratorMocUic::WorkerSwapJob(JobHandleT& jobHandle)
  1694. {
  1695. bool const jobProcessed(jobHandle);
  1696. if (jobProcessed) {
  1697. jobHandle.reset(nullptr);
  1698. }
  1699. {
  1700. std::unique_lock<std::mutex> jobsLock(JobsMutex_);
  1701. // Reduce the remaining job count and notify the libuv loop
  1702. // when all jobs are done
  1703. if (jobProcessed) {
  1704. --JobsRemain_;
  1705. if (JobsRemain_ == 0) {
  1706. UVRequest().send();
  1707. }
  1708. }
  1709. // Wait for new jobs
  1710. while (!JobThreadsAbort_ && JobQueue_.empty()) {
  1711. JobsConditionRead_.wait(jobsLock);
  1712. }
  1713. // Try to pick up a new job handle
  1714. if (!JobThreadsAbort_ && !JobQueue_.empty()) {
  1715. jobHandle = std::move(JobQueue_.front());
  1716. JobQueue_.pop_front();
  1717. }
  1718. }
  1719. }
  1720. void cmQtAutoGeneratorMocUic::ParallelRegisterJobError()
  1721. {
  1722. std::lock_guard<std::mutex> jobsLock(JobsMutex_);
  1723. RegisterJobError();
  1724. }
  1725. // Private method that requires cmQtAutoGeneratorMocUic::JobsMutex_ to be
  1726. // locked
  1727. void cmQtAutoGeneratorMocUic::RegisterJobError()
  1728. {
  1729. JobError_ = true;
  1730. if (!JobThreadsAbort_) {
  1731. JobThreadsAbort_ = true;
  1732. // Clear remaining jobs
  1733. if (JobsRemain_ != 0) {
  1734. JobsRemain_ -= JobQueue_.size();
  1735. JobQueue_.clear();
  1736. }
  1737. }
  1738. }
  1739. bool cmQtAutoGeneratorMocUic::ParallelJobPushMoc(JobHandleT& jobHandle)
  1740. {
  1741. std::lock_guard<std::mutex> jobsLock(JobsMutex_);
  1742. if (!JobThreadsAbort_) {
  1743. bool pushJobHandle = true;
  1744. // Do additional tests if this is an included moc job
  1745. const JobMocT& mocJob(static_cast<JobMocT&>(*jobHandle));
  1746. if (!mocJob.IncludeString.empty()) {
  1747. // Register included moc file and look for collisions
  1748. MocIncludedFiles_.emplace(mocJob.SourceFile);
  1749. if (!MocIncludedStrings_.emplace(mocJob.IncludeString).second) {
  1750. // Another source file includes the same moc file!
  1751. for (const JobHandleT& otherHandle : JobQueues_.Moc) {
  1752. const JobMocT& otherJob(static_cast<JobMocT&>(*otherHandle));
  1753. if (otherJob.IncludeString == mocJob.IncludeString) {
  1754. // Check if the same moc file would be generated from different
  1755. // source files which is an error.
  1756. if (otherJob.SourceFile != mocJob.SourceFile) {
  1757. // Include string collision
  1758. std::string error = "The two source files\n ";
  1759. error += Quoted(mocJob.IncluderFile);
  1760. error += " and\n ";
  1761. error += Quoted(otherJob.IncluderFile);
  1762. error += "\ncontain the same moc include string ";
  1763. error += Quoted(mocJob.IncludeString);
  1764. error += "\nbut the moc file would be generated from different "
  1765. "source files\n ";
  1766. error += Quoted(mocJob.SourceFile);
  1767. error += " and\n ";
  1768. error += Quoted(otherJob.SourceFile);
  1769. error += ".\nConsider to\n"
  1770. "- not include the \"moc_<NAME>.cpp\" file\n"
  1771. "- add a directory prefix to a \"<NAME>.moc\" include "
  1772. "(e.g \"sub/<NAME>.moc\")\n"
  1773. "- rename the source file(s)\n";
  1774. Log().Error(GeneratorT::MOC, error);
  1775. RegisterJobError();
  1776. }
  1777. // Do not push this job in since the included moc file already
  1778. // gets generated by an other job.
  1779. pushJobHandle = false;
  1780. break;
  1781. }
  1782. }
  1783. }
  1784. }
  1785. // Push job on demand
  1786. if (pushJobHandle) {
  1787. JobQueues_.Moc.emplace_back(std::move(jobHandle));
  1788. }
  1789. }
  1790. return !JobError_;
  1791. }
  1792. bool cmQtAutoGeneratorMocUic::ParallelJobPushUic(JobHandleT& jobHandle)
  1793. {
  1794. std::lock_guard<std::mutex> jobsLock(JobsMutex_);
  1795. if (!JobThreadsAbort_) {
  1796. bool pushJobHandle = true;
  1797. // Look for include collisions.
  1798. const JobUicT& uicJob(static_cast<JobUicT&>(*jobHandle));
  1799. for (const JobHandleT& otherHandle : JobQueues_.Uic) {
  1800. const JobUicT& otherJob(static_cast<JobUicT&>(*otherHandle));
  1801. if (otherJob.IncludeString == uicJob.IncludeString) {
  1802. // Check if the same uic file would be generated from different
  1803. // source files which would be an error.
  1804. if (otherJob.SourceFile != uicJob.SourceFile) {
  1805. // Include string collision
  1806. std::string error = "The two source files\n ";
  1807. error += Quoted(uicJob.IncluderFile);
  1808. error += " and\n ";
  1809. error += Quoted(otherJob.IncluderFile);
  1810. error += "\ncontain the same uic include string ";
  1811. error += Quoted(uicJob.IncludeString);
  1812. error += "\nbut the uic file would be generated from different "
  1813. "source files\n ";
  1814. error += Quoted(uicJob.SourceFile);
  1815. error += " and\n ";
  1816. error += Quoted(otherJob.SourceFile);
  1817. error +=
  1818. ".\nConsider to\n"
  1819. "- add a directory prefix to a \"ui_<NAME>.h\" include "
  1820. "(e.g \"sub/ui_<NAME>.h\")\n"
  1821. "- rename the <NAME>.ui file(s) and adjust the \"ui_<NAME>.h\" "
  1822. "include(s)\n";
  1823. Log().Error(GeneratorT::UIC, error);
  1824. RegisterJobError();
  1825. }
  1826. // Do not push this job in since the uic file already
  1827. // gets generated by an other job.
  1828. pushJobHandle = false;
  1829. break;
  1830. }
  1831. }
  1832. if (pushJobHandle) {
  1833. JobQueues_.Uic.emplace_back(std::move(jobHandle));
  1834. }
  1835. }
  1836. return !JobError_;
  1837. }
  1838. bool cmQtAutoGeneratorMocUic::ParallelMocIncluded(
  1839. std::string const& sourceFile)
  1840. {
  1841. std::lock_guard<std::mutex> mocLock(JobsMutex_);
  1842. return (MocIncludedFiles_.find(sourceFile) != MocIncludedFiles_.end());
  1843. }
  1844. void cmQtAutoGeneratorMocUic::ParallelMocAutoRegister(
  1845. std::string const& mocFile)
  1846. {
  1847. std::lock_guard<std::mutex> mocLock(JobsMutex_);
  1848. MocAutoFiles_.emplace(mocFile);
  1849. }
  1850. void cmQtAutoGeneratorMocUic::ParallelMocAutoUpdated()
  1851. {
  1852. std::lock_guard<std::mutex> mocLock(JobsMutex_);
  1853. MocAutoFileUpdated_ = true;
  1854. }
  1855. void cmQtAutoGeneratorMocUic::MocGenerateCompilation()
  1856. {
  1857. std::lock_guard<std::mutex> mocLock(JobsMutex_);
  1858. if (!JobError_ && Moc().Enabled) {
  1859. // Write mocs compilation build file
  1860. {
  1861. // Compose mocs compilation file content
  1862. std::string content =
  1863. "// This file is autogenerated. Changes will be overwritten.\n";
  1864. if (MocAutoFiles_.empty()) {
  1865. // Placeholder content
  1866. content += "// No files found that require moc or the moc files are "
  1867. "included\n";
  1868. content += "enum some_compilers { need_more_than_nothing };\n";
  1869. } else {
  1870. // Valid content
  1871. char const sbeg = Base().MultiConfig ? '<' : '"';
  1872. char const send = Base().MultiConfig ? '>' : '"';
  1873. for (std::string const& mocfile : MocAutoFiles_) {
  1874. content += "#include ";
  1875. content += sbeg;
  1876. content += mocfile;
  1877. content += send;
  1878. content += '\n';
  1879. }
  1880. }
  1881. std::string const& compAbs = Moc().CompFileAbs;
  1882. if (FileSys().FileDiffers(compAbs, content)) {
  1883. // Actually write mocs compilation file
  1884. if (Log().Verbose()) {
  1885. Log().Info(GeneratorT::MOC, "Generating MOC compilation " + compAbs);
  1886. }
  1887. if (!FileSys().FileWrite(GeneratorT::MOC, compAbs, content)) {
  1888. Log().ErrorFile(GeneratorT::MOC, compAbs,
  1889. "mocs compilation file writing failed");
  1890. RegisterJobError();
  1891. return;
  1892. }
  1893. } else if (MocAutoFileUpdated_) {
  1894. // Only touch mocs compilation file
  1895. if (Log().Verbose()) {
  1896. Log().Info(GeneratorT::MOC, "Touching mocs compilation " + compAbs);
  1897. }
  1898. FileSys().Touch(compAbs);
  1899. }
  1900. }
  1901. // Write mocs compilation wrapper file
  1902. if (Base().MultiConfig) {
  1903. }
  1904. }
  1905. }