cmQtAutoGeneratorMocUic.cxx 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024
  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(1);
  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(1))) {
  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().AutogenIncludeDirAbs;
  637. BuildFile += IncludeString;
  638. } else {
  639. std::string buildRel = wrk.Base().FilePathChecksum.getPart(SourceFile);
  640. buildRel += '/';
  641. buildRel += "moc_";
  642. buildRel += cmSystemTools::GetFilenameWithoutLastExtension(SourceFile);
  643. if (wrk.Base().MultiConfig != MultiConfigT::SINGLE) {
  644. buildRel += wrk.Base().ConfigSuffix;
  645. }
  646. buildRel += ".cpp";
  647. wrk.Gen().ParallelMocAutoRegister(buildRel);
  648. BuildFile = wrk.Base().AbsoluteBuildPath(buildRel);
  649. }
  650. if (UpdateRequired(wrk)) {
  651. GenerateMoc(wrk);
  652. }
  653. }
  654. bool cmQtAutoGeneratorMocUic::JobMocT::UpdateRequired(WorkerT& wrk)
  655. {
  656. bool const verbose = wrk.Gen().Log().Verbose();
  657. // Test if the build file exists
  658. if (!wrk.FileSys().FileExists(BuildFile)) {
  659. if (verbose) {
  660. std::string reason = "Generating ";
  661. reason += Quoted(BuildFile);
  662. reason += " from its source file ";
  663. reason += Quoted(SourceFile);
  664. reason += " because it doesn't exist";
  665. wrk.LogInfo(GeneratorT::MOC, reason);
  666. }
  667. return true;
  668. }
  669. // Test if any setting changed
  670. if (wrk.Moc().SettingsChanged) {
  671. if (verbose) {
  672. std::string reason = "Generating ";
  673. reason += Quoted(BuildFile);
  674. reason += " from ";
  675. reason += Quoted(SourceFile);
  676. reason += " because the MOC settings changed";
  677. wrk.LogInfo(GeneratorT::MOC, reason);
  678. }
  679. return true;
  680. }
  681. // Test if the moc_predefs file is newer
  682. if (!wrk.Moc().PredefsFileAbs.empty()) {
  683. bool isOlder = false;
  684. {
  685. std::string error;
  686. isOlder = wrk.FileSys().FileIsOlderThan(
  687. BuildFile, wrk.Moc().PredefsFileAbs, &error);
  688. if (!isOlder && !error.empty()) {
  689. wrk.LogError(GeneratorT::MOC, error);
  690. return false;
  691. }
  692. }
  693. if (isOlder) {
  694. if (verbose) {
  695. std::string reason = "Generating ";
  696. reason += Quoted(BuildFile);
  697. reason += " because it's older than: ";
  698. reason += Quoted(wrk.Moc().PredefsFileAbs);
  699. wrk.LogInfo(GeneratorT::MOC, reason);
  700. }
  701. return true;
  702. }
  703. }
  704. // Test if the source file is newer
  705. {
  706. bool isOlder = false;
  707. {
  708. std::string error;
  709. isOlder = wrk.FileSys().FileIsOlderThan(BuildFile, SourceFile, &error);
  710. if (!isOlder && !error.empty()) {
  711. wrk.LogError(GeneratorT::MOC, error);
  712. return false;
  713. }
  714. }
  715. if (isOlder) {
  716. if (verbose) {
  717. std::string reason = "Generating ";
  718. reason += Quoted(BuildFile);
  719. reason += " because it's older than its source file ";
  720. reason += Quoted(SourceFile);
  721. wrk.LogInfo(GeneratorT::MOC, reason);
  722. }
  723. return true;
  724. }
  725. }
  726. // Test if a dependency file is newer
  727. {
  728. // Read dependencies on demand
  729. if (!DependsValid) {
  730. std::string content;
  731. {
  732. std::string error;
  733. if (!wrk.FileSys().FileRead(content, SourceFile, &error)) {
  734. std::string emsg = "Could not read file\n ";
  735. emsg += Quoted(SourceFile);
  736. emsg += "\nrequired by moc include ";
  737. emsg += Quoted(IncludeString);
  738. emsg += " in\n ";
  739. emsg += Quoted(IncluderFile);
  740. emsg += ".\n";
  741. emsg += error;
  742. wrk.LogError(GeneratorT::MOC, emsg);
  743. return false;
  744. }
  745. }
  746. FindDependencies(wrk, content);
  747. }
  748. // Check dependency timestamps
  749. std::string error;
  750. std::string sourceDir = SubDirPrefix(SourceFile);
  751. for (std::string const& depFileRel : Depends) {
  752. std::string depFileAbs =
  753. wrk.Moc().FindIncludedFile(sourceDir, depFileRel);
  754. if (!depFileAbs.empty()) {
  755. if (wrk.FileSys().FileIsOlderThan(BuildFile, depFileAbs, &error)) {
  756. if (verbose) {
  757. std::string reason = "Generating ";
  758. reason += Quoted(BuildFile);
  759. reason += " from ";
  760. reason += Quoted(SourceFile);
  761. reason += " because it is older than it's dependency file ";
  762. reason += Quoted(depFileAbs);
  763. wrk.LogInfo(GeneratorT::MOC, reason);
  764. }
  765. return true;
  766. }
  767. if (!error.empty()) {
  768. wrk.LogError(GeneratorT::MOC, error);
  769. return false;
  770. }
  771. } else {
  772. std::string message = "Could not find dependency file ";
  773. message += Quoted(depFileRel);
  774. wrk.LogFileWarning(GeneratorT::MOC, SourceFile, message);
  775. }
  776. }
  777. }
  778. return false;
  779. }
  780. void cmQtAutoGeneratorMocUic::JobMocT::GenerateMoc(WorkerT& wrk)
  781. {
  782. // Make sure the parent directory exists
  783. if (wrk.FileSys().MakeParentDirectory(GeneratorT::MOC, BuildFile)) {
  784. // Compose moc command
  785. std::vector<std::string> cmd;
  786. cmd.push_back(wrk.Moc().Executable);
  787. // Add options
  788. cmd.insert(cmd.end(), wrk.Moc().AllOptions.begin(),
  789. wrk.Moc().AllOptions.end());
  790. // Add predefs include
  791. if (!wrk.Moc().PredefsFileAbs.empty()) {
  792. cmd.push_back("--include");
  793. cmd.push_back(wrk.Moc().PredefsFileAbs);
  794. }
  795. cmd.push_back("-o");
  796. cmd.push_back(BuildFile);
  797. cmd.push_back(SourceFile);
  798. // Execute moc command
  799. ProcessResultT result;
  800. if (wrk.RunProcess(GeneratorT::MOC, result, cmd)) {
  801. // Moc command success
  802. if (IncludeString.empty()) {
  803. // Notify the generator that a not included file changed
  804. wrk.Gen().ParallelMocAutoUpdated();
  805. }
  806. } else {
  807. // Moc command failed
  808. {
  809. std::string emsg = "The moc process failed to compile\n ";
  810. emsg += Quoted(SourceFile);
  811. emsg += "\ninto\n ";
  812. emsg += Quoted(BuildFile);
  813. emsg += ".\n";
  814. emsg += result.ErrorMessage;
  815. wrk.LogCommandError(GeneratorT::MOC, emsg, cmd, result.StdOut);
  816. }
  817. wrk.FileSys().FileRemove(BuildFile);
  818. }
  819. }
  820. }
  821. void cmQtAutoGeneratorMocUic::JobUicT::Process(WorkerT& wrk)
  822. {
  823. // Compute build file name
  824. BuildFile = wrk.Base().AutogenIncludeDirAbs;
  825. BuildFile += IncludeString;
  826. if (UpdateRequired(wrk)) {
  827. GenerateUic(wrk);
  828. }
  829. }
  830. bool cmQtAutoGeneratorMocUic::JobUicT::UpdateRequired(WorkerT& wrk)
  831. {
  832. bool const verbose = wrk.Gen().Log().Verbose();
  833. // Test if the build file exists
  834. if (!wrk.FileSys().FileExists(BuildFile)) {
  835. if (verbose) {
  836. std::string reason = "Generating ";
  837. reason += Quoted(BuildFile);
  838. reason += " from its source file ";
  839. reason += Quoted(SourceFile);
  840. reason += " because it doesn't exist";
  841. wrk.LogInfo(GeneratorT::UIC, reason);
  842. }
  843. return true;
  844. }
  845. // Test if the uic settings changed
  846. if (wrk.Uic().SettingsChanged) {
  847. if (verbose) {
  848. std::string reason = "Generating ";
  849. reason += Quoted(BuildFile);
  850. reason += " from ";
  851. reason += Quoted(SourceFile);
  852. reason += " because the UIC settings changed";
  853. wrk.LogInfo(GeneratorT::UIC, reason);
  854. }
  855. return true;
  856. }
  857. // Test if the source file is newer
  858. {
  859. bool isOlder = false;
  860. {
  861. std::string error;
  862. isOlder = wrk.FileSys().FileIsOlderThan(BuildFile, SourceFile, &error);
  863. if (!isOlder && !error.empty()) {
  864. wrk.LogError(GeneratorT::UIC, error);
  865. return false;
  866. }
  867. }
  868. if (isOlder) {
  869. if (verbose) {
  870. std::string reason = "Generating ";
  871. reason += Quoted(BuildFile);
  872. reason += " because it's older than its source file ";
  873. reason += Quoted(SourceFile);
  874. wrk.LogInfo(GeneratorT::UIC, reason);
  875. }
  876. return true;
  877. }
  878. }
  879. return false;
  880. }
  881. void cmQtAutoGeneratorMocUic::JobUicT::GenerateUic(WorkerT& wrk)
  882. {
  883. // Make sure the parent directory exists
  884. if (wrk.FileSys().MakeParentDirectory(GeneratorT::UIC, BuildFile)) {
  885. // Compose uic command
  886. std::vector<std::string> cmd;
  887. cmd.push_back(wrk.Uic().Executable);
  888. {
  889. std::vector<std::string> allOpts = wrk.Uic().TargetOptions;
  890. auto optionIt = wrk.Uic().Options.find(SourceFile);
  891. if (optionIt != wrk.Uic().Options.end()) {
  892. UicMergeOptions(allOpts, optionIt->second,
  893. (wrk.Base().QtVersionMajor == 5));
  894. }
  895. cmd.insert(cmd.end(), allOpts.begin(), allOpts.end());
  896. }
  897. cmd.push_back("-o");
  898. cmd.push_back(BuildFile);
  899. cmd.push_back(SourceFile);
  900. ProcessResultT result;
  901. if (wrk.RunProcess(GeneratorT::UIC, result, cmd)) {
  902. // Success
  903. } else {
  904. // Command failed
  905. {
  906. std::string emsg = "The uic process failed to compile\n ";
  907. emsg += Quoted(SourceFile);
  908. emsg += "\ninto\n ";
  909. emsg += Quoted(BuildFile);
  910. emsg += "\nincluded by\n ";
  911. emsg += Quoted(IncluderFile);
  912. emsg += ".\n";
  913. emsg += result.ErrorMessage;
  914. wrk.LogCommandError(GeneratorT::UIC, emsg, cmd, result.StdOut);
  915. }
  916. wrk.FileSys().FileRemove(BuildFile);
  917. }
  918. }
  919. }
  920. void cmQtAutoGeneratorMocUic::JobDeleterT::operator()(JobT* job)
  921. {
  922. delete job;
  923. }
  924. cmQtAutoGeneratorMocUic::WorkerT::WorkerT(cmQtAutoGeneratorMocUic* gen,
  925. uv_loop_t* uvLoop)
  926. : Gen_(gen)
  927. {
  928. // Initialize uv asynchronous callback for process starting
  929. ProcessRequest_.init(*uvLoop, &WorkerT::UVProcessStart, this);
  930. // Start thread
  931. Thread_ = std::thread(&WorkerT::Loop, this);
  932. }
  933. cmQtAutoGeneratorMocUic::WorkerT::~WorkerT()
  934. {
  935. // Join thread
  936. if (Thread_.joinable()) {
  937. Thread_.join();
  938. }
  939. }
  940. void cmQtAutoGeneratorMocUic::WorkerT::LogInfo(
  941. GeneratorT genType, std::string const& message) const
  942. {
  943. return Log().Info(genType, message);
  944. }
  945. void cmQtAutoGeneratorMocUic::WorkerT::LogWarning(
  946. GeneratorT genType, std::string const& message) const
  947. {
  948. return Log().Warning(genType, message);
  949. }
  950. void cmQtAutoGeneratorMocUic::WorkerT::LogFileWarning(
  951. GeneratorT genType, std::string const& filename,
  952. std::string const& message) const
  953. {
  954. return Log().WarningFile(genType, filename, message);
  955. }
  956. void cmQtAutoGeneratorMocUic::WorkerT::LogError(
  957. GeneratorT genType, std::string const& message) const
  958. {
  959. Gen().ParallelRegisterJobError();
  960. Log().Error(genType, message);
  961. }
  962. void cmQtAutoGeneratorMocUic::WorkerT::LogFileError(
  963. GeneratorT genType, std::string const& filename,
  964. std::string const& message) const
  965. {
  966. Gen().ParallelRegisterJobError();
  967. Log().ErrorFile(genType, filename, message);
  968. }
  969. void cmQtAutoGeneratorMocUic::WorkerT::LogCommandError(
  970. GeneratorT genType, std::string const& message,
  971. std::vector<std::string> const& command, std::string const& output) const
  972. {
  973. Gen().ParallelRegisterJobError();
  974. Log().ErrorCommand(genType, message, command, output);
  975. }
  976. bool cmQtAutoGeneratorMocUic::WorkerT::RunProcess(
  977. GeneratorT genType, ProcessResultT& result,
  978. std::vector<std::string> const& command)
  979. {
  980. if (command.empty()) {
  981. return false;
  982. }
  983. // Create process instance
  984. {
  985. std::lock_guard<std::mutex> lock(ProcessMutex_);
  986. Process_ = cm::make_unique<ReadOnlyProcessT>();
  987. Process_->setup(&result, true, command, Gen().Base().AutogenBuildDir);
  988. }
  989. // Send asynchronous process start request to libuv loop
  990. ProcessRequest_.send();
  991. // Log command
  992. if (this->Log().Verbose()) {
  993. std::string msg = "Running command:\n";
  994. msg += QuotedCommand(command);
  995. msg += '\n';
  996. this->LogInfo(genType, msg);
  997. }
  998. // Wait until the process has been finished and destroyed
  999. {
  1000. std::unique_lock<std::mutex> ulock(ProcessMutex_);
  1001. while (Process_) {
  1002. ProcessCondition_.wait(ulock);
  1003. }
  1004. }
  1005. return !result.error();
  1006. }
  1007. void cmQtAutoGeneratorMocUic::WorkerT::Loop()
  1008. {
  1009. while (true) {
  1010. Gen().WorkerSwapJob(JobHandle_);
  1011. if (JobHandle_) {
  1012. JobHandle_->Process(*this);
  1013. } else {
  1014. break;
  1015. }
  1016. }
  1017. }
  1018. void cmQtAutoGeneratorMocUic::WorkerT::UVProcessStart(uv_async_t* handle)
  1019. {
  1020. auto& wrk = *reinterpret_cast<WorkerT*>(handle->data);
  1021. {
  1022. std::lock_guard<std::mutex> lock(wrk.ProcessMutex_);
  1023. if (wrk.Process_ && !wrk.Process_->IsStarted()) {
  1024. wrk.Process_->start(handle->loop,
  1025. std::bind(&WorkerT::UVProcessFinished, &wrk));
  1026. }
  1027. }
  1028. }
  1029. void cmQtAutoGeneratorMocUic::WorkerT::UVProcessFinished()
  1030. {
  1031. {
  1032. std::lock_guard<std::mutex> lock(ProcessMutex_);
  1033. if (Process_ && Process_->IsFinished()) {
  1034. Process_.reset();
  1035. }
  1036. }
  1037. // Notify idling thread
  1038. ProcessCondition_.notify_one();
  1039. }
  1040. cmQtAutoGeneratorMocUic::cmQtAutoGeneratorMocUic()
  1041. : Base_(&FileSys())
  1042. , Moc_(&FileSys())
  1043. , Stage_(StageT::SETTINGS_READ)
  1044. , JobsRemain_(0)
  1045. , JobError_(false)
  1046. , JobThreadsAbort_(false)
  1047. , MocAutoFileUpdated_(false)
  1048. {
  1049. // Precompile regular expressions
  1050. Moc_.RegExpInclude.compile(
  1051. "[\n][ \t]*#[ \t]*include[ \t]+"
  1052. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  1053. Uic_.RegExpInclude.compile("[\n][ \t]*#[ \t]*include[ \t]+"
  1054. "[\"<](([^ \">]+/)?ui_[^ \">/]+\\.h)[\">]");
  1055. // Initialize libuv asynchronous iteration request
  1056. UVRequest().init(*UVLoop(), &cmQtAutoGeneratorMocUic::UVPollStage, this);
  1057. }
  1058. cmQtAutoGeneratorMocUic::~cmQtAutoGeneratorMocUic()
  1059. {
  1060. }
  1061. bool cmQtAutoGeneratorMocUic::Init(cmMakefile* makefile)
  1062. {
  1063. // -- Meta
  1064. Base_.HeaderExtensions = makefile->GetCMakeInstance()->GetHeaderExtensions();
  1065. // Utility lambdas
  1066. auto InfoGet = [makefile](const char* key) {
  1067. return makefile->GetSafeDefinition(key);
  1068. };
  1069. auto InfoGetBool = [makefile](const char* key) {
  1070. return makefile->IsOn(key);
  1071. };
  1072. auto InfoGetList = [makefile](const char* key) -> std::vector<std::string> {
  1073. std::vector<std::string> list;
  1074. cmSystemTools::ExpandListArgument(makefile->GetSafeDefinition(key), list);
  1075. return list;
  1076. };
  1077. auto InfoGetLists =
  1078. [makefile](const char* key) -> std::vector<std::vector<std::string>> {
  1079. std::vector<std::vector<std::string>> lists;
  1080. {
  1081. std::string const value = makefile->GetSafeDefinition(key);
  1082. std::string::size_type pos = 0;
  1083. while (pos < value.size()) {
  1084. std::string::size_type next = value.find(ListSep, pos);
  1085. std::string::size_type length =
  1086. (next != std::string::npos) ? next - pos : value.size() - pos;
  1087. // Remove enclosing braces
  1088. if (length >= 2) {
  1089. std::string::const_iterator itBeg = value.begin() + (pos + 1);
  1090. std::string::const_iterator itEnd = itBeg + (length - 2);
  1091. {
  1092. std::string subValue(itBeg, itEnd);
  1093. std::vector<std::string> list;
  1094. cmSystemTools::ExpandListArgument(subValue, list);
  1095. lists.push_back(std::move(list));
  1096. }
  1097. }
  1098. pos += length;
  1099. pos += ListSep.size();
  1100. }
  1101. }
  1102. return lists;
  1103. };
  1104. auto InfoGetConfig = [makefile, this](const char* key) -> std::string {
  1105. const char* valueConf = nullptr;
  1106. {
  1107. std::string keyConf = key;
  1108. keyConf += '_';
  1109. keyConf += InfoConfig();
  1110. valueConf = makefile->GetDefinition(keyConf);
  1111. }
  1112. if (valueConf == nullptr) {
  1113. valueConf = makefile->GetSafeDefinition(key);
  1114. }
  1115. return std::string(valueConf);
  1116. };
  1117. auto InfoGetConfigList =
  1118. [&InfoGetConfig](const char* key) -> std::vector<std::string> {
  1119. std::vector<std::string> list;
  1120. cmSystemTools::ExpandListArgument(InfoGetConfig(key), list);
  1121. return list;
  1122. };
  1123. // -- Read info file
  1124. if (!makefile->ReadListFile(InfoFile().c_str())) {
  1125. Log().ErrorFile(GeneratorT::GEN, InfoFile(), "File processing failed");
  1126. return false;
  1127. }
  1128. // -- Meta
  1129. Base_.MultiConfig = MultiConfigType(InfoGet("AM_MULTI_CONFIG"));
  1130. Base_.ConfigSuffix = InfoGetConfig("AM_CONFIG_SUFFIX");
  1131. if (Base_.ConfigSuffix.empty()) {
  1132. Base_.ConfigSuffix = "_";
  1133. Base_.ConfigSuffix += InfoConfig();
  1134. }
  1135. SettingsFile_ = InfoGetConfig("AM_SETTINGS_FILE");
  1136. if (SettingsFile_.empty()) {
  1137. Log().ErrorFile(GeneratorT::GEN, InfoFile(), "Settings file name missing");
  1138. return false;
  1139. }
  1140. {
  1141. unsigned long num = Base_.NumThreads;
  1142. if (cmSystemTools::StringToULong(InfoGet("AM_PARALLEL"), &num)) {
  1143. num = std::max<unsigned long>(num, 1);
  1144. num = std::min<unsigned long>(num, ParallelMax);
  1145. Base_.NumThreads = static_cast<unsigned int>(num);
  1146. }
  1147. }
  1148. // - Files and directories
  1149. Base_.ProjectSourceDir = InfoGet("AM_CMAKE_SOURCE_DIR");
  1150. Base_.ProjectBinaryDir = InfoGet("AM_CMAKE_BINARY_DIR");
  1151. Base_.CurrentSourceDir = InfoGet("AM_CMAKE_CURRENT_SOURCE_DIR");
  1152. Base_.CurrentBinaryDir = InfoGet("AM_CMAKE_CURRENT_BINARY_DIR");
  1153. Base_.IncludeProjectDirsBefore =
  1154. InfoGetBool("AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE");
  1155. Base_.AutogenBuildDir = InfoGet("AM_BUILD_DIR");
  1156. if (Base_.AutogenBuildDir.empty()) {
  1157. Log().ErrorFile(GeneratorT::GEN, InfoFile(),
  1158. "Autogen build directory missing");
  1159. return false;
  1160. }
  1161. // - Qt environment
  1162. {
  1163. unsigned long qtv = Base_.QtVersionMajor;
  1164. if (cmSystemTools::StringToULong(InfoGet("AM_QT_VERSION_MAJOR"), &qtv)) {
  1165. Base_.QtVersionMajor = static_cast<unsigned int>(qtv);
  1166. }
  1167. }
  1168. // - Moc
  1169. Moc_.Executable = InfoGet("AM_QT_MOC_EXECUTABLE");
  1170. Moc_.Enabled = !Moc().Executable.empty();
  1171. if (Moc().Enabled) {
  1172. {
  1173. auto lst = InfoGetList("AM_MOC_SKIP");
  1174. Moc_.SkipList.insert(lst.begin(), lst.end());
  1175. }
  1176. Moc_.Definitions = InfoGetConfigList("AM_MOC_DEFINITIONS");
  1177. #ifdef _WIN32
  1178. {
  1179. std::string win32("WIN32");
  1180. auto itB = Moc().Definitions.cbegin();
  1181. auto itE = Moc().Definitions.cend();
  1182. if (std::find(itB, itE, win32) == itE) {
  1183. Moc_.Definitions.emplace_back(std::move(win32));
  1184. }
  1185. }
  1186. #endif
  1187. Moc_.IncludePaths = InfoGetConfigList("AM_MOC_INCLUDES");
  1188. Moc_.Options = InfoGetList("AM_MOC_OPTIONS");
  1189. Moc_.RelaxedMode = InfoGetBool("AM_MOC_RELAXED_MODE");
  1190. for (std::string const& item : InfoGetList("AM_MOC_MACRO_NAMES")) {
  1191. Moc_.MacroFilters.emplace_back(
  1192. item, ("[\n][ \t]*{?[ \t]*" + item).append("[^a-zA-Z0-9_]"));
  1193. }
  1194. {
  1195. auto pushFilter = [this](std::string const& key, std::string const& exp,
  1196. std::string& error) {
  1197. if (!key.empty()) {
  1198. if (!exp.empty()) {
  1199. Moc_.DependFilters.push_back(KeyExpT());
  1200. KeyExpT& filter(Moc_.DependFilters.back());
  1201. if (filter.Exp.compile(exp)) {
  1202. filter.Key = key;
  1203. } else {
  1204. error = "Regular expression compiling failed";
  1205. }
  1206. } else {
  1207. error = "Regular expression is empty";
  1208. }
  1209. } else {
  1210. error = "Key is empty";
  1211. }
  1212. if (!error.empty()) {
  1213. error = ("AUTOMOC_DEPEND_FILTERS: " + error);
  1214. error += "\n";
  1215. error += " Key: ";
  1216. error += Quoted(key);
  1217. error += "\n";
  1218. error += " Exp: ";
  1219. error += Quoted(exp);
  1220. error += "\n";
  1221. }
  1222. };
  1223. std::string error;
  1224. // Insert default filter for Q_PLUGIN_METADATA
  1225. if (Base().QtVersionMajor != 4) {
  1226. pushFilter("Q_PLUGIN_METADATA", "[\n][ \t]*Q_PLUGIN_METADATA[ \t]*\\("
  1227. "[^\\)]*FILE[ \t]*\"([^\"]+)\"",
  1228. error);
  1229. }
  1230. // Insert user defined dependency filters
  1231. {
  1232. std::vector<std::string> flts = InfoGetList("AM_MOC_DEPEND_FILTERS");
  1233. if ((flts.size() % 2) == 0) {
  1234. for (std::vector<std::string>::iterator itC = flts.begin(),
  1235. itE = flts.end();
  1236. itC != itE; itC += 2) {
  1237. pushFilter(*itC, *(itC + 1), error);
  1238. if (!error.empty()) {
  1239. break;
  1240. }
  1241. }
  1242. } else {
  1243. Log().ErrorFile(
  1244. GeneratorT::MOC, InfoFile(),
  1245. "AUTOMOC_DEPEND_FILTERS list size is not a multiple of 2");
  1246. return false;
  1247. }
  1248. }
  1249. if (!error.empty()) {
  1250. Log().ErrorFile(GeneratorT::MOC, InfoFile(), error);
  1251. return false;
  1252. }
  1253. }
  1254. Moc_.PredefsCmd = InfoGetList("AM_MOC_PREDEFS_CMD");
  1255. // Install moc predefs job
  1256. if (!Moc().PredefsCmd.empty()) {
  1257. JobQueues_.MocPredefs.emplace_back(new JobMocPredefsT());
  1258. }
  1259. }
  1260. // - Uic
  1261. Uic_.Executable = InfoGet("AM_QT_UIC_EXECUTABLE");
  1262. Uic_.Enabled = !Uic().Executable.empty();
  1263. if (Uic().Enabled) {
  1264. {
  1265. auto lst = InfoGetList("AM_UIC_SKIP");
  1266. Uic_.SkipList.insert(lst.begin(), lst.end());
  1267. }
  1268. Uic_.SearchPaths = InfoGetList("AM_UIC_SEARCH_PATHS");
  1269. Uic_.TargetOptions = InfoGetConfigList("AM_UIC_TARGET_OPTIONS");
  1270. {
  1271. auto sources = InfoGetList("AM_UIC_OPTIONS_FILES");
  1272. auto options = InfoGetLists("AM_UIC_OPTIONS_OPTIONS");
  1273. // Compare list sizes
  1274. if (sources.size() != options.size()) {
  1275. std::ostringstream ost;
  1276. ost << "files/options lists sizes missmatch (" << sources.size() << "/"
  1277. << options.size() << ")";
  1278. Log().ErrorFile(GeneratorT::UIC, InfoFile(), ost.str());
  1279. return false;
  1280. }
  1281. auto fitEnd = sources.cend();
  1282. auto fit = sources.begin();
  1283. auto oit = options.begin();
  1284. while (fit != fitEnd) {
  1285. Uic_.Options[*fit] = std::move(*oit);
  1286. ++fit;
  1287. ++oit;
  1288. }
  1289. }
  1290. }
  1291. // Initialize source file jobs
  1292. {
  1293. std::hash<std::string> stringHash;
  1294. std::set<std::size_t> uniqueHeaders;
  1295. // Add header jobs
  1296. for (std::string& hdr : InfoGetList("AM_HEADERS")) {
  1297. const bool moc = !Moc().skipped(hdr);
  1298. const bool uic = !Uic().skipped(hdr);
  1299. if ((moc || uic) && uniqueHeaders.emplace(stringHash(hdr)).second) {
  1300. JobQueues_.Headers.emplace_back(
  1301. new JobParseT(std::move(hdr), moc, uic, true));
  1302. }
  1303. }
  1304. // Add source jobs
  1305. {
  1306. std::vector<std::string> sources = InfoGetList("AM_SOURCES");
  1307. // Add header(s) for the source file
  1308. for (std::string& src : sources) {
  1309. const bool srcMoc = !Moc().skipped(src);
  1310. const bool srcUic = !Uic().skipped(src);
  1311. if (!srcMoc && !srcUic) {
  1312. continue;
  1313. }
  1314. // Search for the default header file and a private header
  1315. {
  1316. std::array<std::string, 2> bases;
  1317. bases[0] = SubDirPrefix(src);
  1318. bases[0] += cmSystemTools::GetFilenameWithoutLastExtension(src);
  1319. bases[1] = bases[0];
  1320. bases[1] += "_p";
  1321. for (std::string const& headerBase : bases) {
  1322. std::string header;
  1323. if (Base().FindHeader(header, headerBase)) {
  1324. const bool moc = srcMoc && !Moc().skipped(header);
  1325. const bool uic = srcUic && !Uic().skipped(header);
  1326. if ((moc || uic) &&
  1327. uniqueHeaders.emplace(stringHash(header)).second) {
  1328. JobQueues_.Headers.emplace_back(
  1329. new JobParseT(std::move(header), moc, uic, true));
  1330. }
  1331. }
  1332. }
  1333. }
  1334. // Add source job
  1335. JobQueues_.Sources.emplace_back(
  1336. new JobParseT(std::move(src), srcMoc, srcUic));
  1337. }
  1338. }
  1339. }
  1340. // Init derived information
  1341. // ------------------------
  1342. // Init file path checksum generator
  1343. Base_.FilePathChecksum.setupParentDirs(
  1344. Base().CurrentSourceDir, Base().CurrentBinaryDir, Base().ProjectSourceDir,
  1345. Base().ProjectBinaryDir);
  1346. // include directory
  1347. Base_.AutogenIncludeDirRel = "include";
  1348. if (Base().MultiConfig != MultiConfigT::SINGLE) {
  1349. Base_.AutogenIncludeDirRel += Base().ConfigSuffix;
  1350. }
  1351. Base_.AutogenIncludeDirRel += "/";
  1352. Base_.AutogenIncludeDirAbs =
  1353. Base_.AbsoluteBuildPath(Base().AutogenIncludeDirRel);
  1354. // Moc variables
  1355. if (Moc().Enabled) {
  1356. // Mocs compilation file
  1357. Moc_.CompFileRel = "mocs_compilation";
  1358. if (Base_.MultiConfig == MultiConfigT::MULTI) {
  1359. Moc_.CompFileRel += Base().ConfigSuffix;
  1360. }
  1361. Moc_.CompFileRel += ".cpp";
  1362. Moc_.CompFileAbs = Base_.AbsoluteBuildPath(Moc().CompFileRel);
  1363. // Moc predefs file
  1364. if (!Moc_.PredefsCmd.empty()) {
  1365. Moc_.PredefsFileRel = "moc_predefs";
  1366. if (Base_.MultiConfig != MultiConfigT::SINGLE) {
  1367. Moc_.PredefsFileRel += Base().ConfigSuffix;
  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().AutogenIncludeDirAbs)) {
  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 (!JobThreadsAbort_ && Moc().Enabled) {
  1861. // Compose mocs compilation file content
  1862. {
  1863. std::string content =
  1864. "// This file is autogenerated. Changes will be overwritten.\n";
  1865. if (MocAutoFiles_.empty()) {
  1866. // Placeholder content
  1867. content += "// No files found that require moc or the moc files are "
  1868. "included\n";
  1869. content += "enum some_compilers { need_more_than_nothing };\n";
  1870. } else {
  1871. // Valid content
  1872. for (std::string const& mocfile : MocAutoFiles_) {
  1873. content += "#include \"";
  1874. content += mocfile;
  1875. content += "\"\n";
  1876. }
  1877. }
  1878. std::string const& compRel = Moc().CompFileRel;
  1879. std::string const& compAbs = Moc().CompFileAbs;
  1880. if (FileSys().FileDiffers(compAbs, content)) {
  1881. // Actually write mocs compilation file
  1882. if (Log().Verbose()) {
  1883. Log().Info(GeneratorT::MOC, "Generating MOC compilation " + compRel);
  1884. }
  1885. if (!FileSys().FileWrite(GeneratorT::MOC, compAbs, content)) {
  1886. Log().ErrorFile(GeneratorT::MOC, compAbs,
  1887. "mocs compilation file writing failed");
  1888. RegisterJobError();
  1889. return;
  1890. }
  1891. } else if (MocAutoFileUpdated_) {
  1892. // Only touch mocs compilation file
  1893. if (Log().Verbose()) {
  1894. Log().Info(GeneratorT::MOC, "Touching mocs compilation " + compRel);
  1895. }
  1896. FileSys().Touch(compAbs);
  1897. }
  1898. }
  1899. }
  1900. }