cmQtAutoGenerators.cxx 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2004-2011 Kitware, Inc.
  4. Copyright 2011 Alexander Neundorf ([email protected])
  5. Distributed under the OSI-approved BSD License (the "License");
  6. see accompanying file Copyright.txt for details.
  7. This software is distributed WITHOUT ANY WARRANTY; without even the
  8. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. See the License for more information.
  10. ============================================================================*/
  11. #include "cmGlobalGenerator.h"
  12. #include "cmLocalGenerator.h"
  13. #include "cmMakefile.h"
  14. #include "cmSourceFile.h"
  15. #include "cmSystemTools.h"
  16. #include "cmState.h"
  17. #include "cmAlgorithms.h"
  18. #if defined(_WIN32) && !defined(__CYGWIN__)
  19. # include "cmLocalVisualStudioGenerator.h"
  20. #endif
  21. #include <sys/stat.h>
  22. #include <cmsys/Terminal.h>
  23. #include <cmsys/ios/sstream>
  24. #include <cmsys/FStream.hxx>
  25. #include <assert.h>
  26. #include <string.h>
  27. #if defined(__APPLE__)
  28. #include <unistd.h>
  29. #endif
  30. #include "cmQtAutoGenerators.h"
  31. static bool requiresMocing(const std::string& text, std::string &macroName)
  32. {
  33. // this simple check is much much faster than the regexp
  34. if (strstr(text.c_str(), "Q_OBJECT") == NULL
  35. && strstr(text.c_str(), "Q_GADGET") == NULL)
  36. {
  37. return false;
  38. }
  39. cmsys::RegularExpression qObjectRegExp("[\n][ \t]*Q_OBJECT[^a-zA-Z0-9_]");
  40. if (qObjectRegExp.find(text))
  41. {
  42. macroName = "Q_OBJECT";
  43. return true;
  44. }
  45. cmsys::RegularExpression qGadgetRegExp("[\n][ \t]*Q_GADGET[^a-zA-Z0-9_]");
  46. if (qGadgetRegExp.find(text))
  47. {
  48. macroName = "Q_GADGET";
  49. return true;
  50. }
  51. return false;
  52. }
  53. static std::string findMatchingHeader(const std::string& absPath,
  54. const std::string& mocSubDir,
  55. const std::string& basename,
  56. const std::vector<std::string>& headerExtensions)
  57. {
  58. std::string header;
  59. for(std::vector<std::string>::const_iterator ext = headerExtensions.begin();
  60. ext != headerExtensions.end();
  61. ++ext)
  62. {
  63. std::string sourceFilePath = absPath + basename + "." + (*ext);
  64. if (cmsys::SystemTools::FileExists(sourceFilePath.c_str()))
  65. {
  66. header = sourceFilePath;
  67. break;
  68. }
  69. if (!mocSubDir.empty())
  70. {
  71. sourceFilePath = mocSubDir + basename + "." + (*ext);
  72. if (cmsys::SystemTools::FileExists(sourceFilePath.c_str()))
  73. {
  74. header = sourceFilePath;
  75. break;
  76. }
  77. }
  78. }
  79. return header;
  80. }
  81. static std::string extractSubDir(const std::string& absPath,
  82. const std::string& currentMoc)
  83. {
  84. std::string subDir;
  85. if (currentMoc.find_first_of('/') != std::string::npos)
  86. {
  87. subDir = absPath
  88. + cmsys::SystemTools::GetFilenamePath(currentMoc) + '/';
  89. }
  90. return subDir;
  91. }
  92. static void copyTargetProperty(cmTarget* destinationTarget,
  93. cmTarget* sourceTarget,
  94. const std::string& propertyName)
  95. {
  96. const char* propertyValue = sourceTarget->GetProperty(propertyName);
  97. if (propertyValue)
  98. {
  99. destinationTarget->SetProperty(propertyName, propertyValue);
  100. }
  101. }
  102. static std::string ReadAll(const std::string& filename)
  103. {
  104. cmsys::ifstream file(filename.c_str());
  105. cmsys_ios::stringstream stream;
  106. stream << file.rdbuf();
  107. file.close();
  108. return stream.str();
  109. }
  110. cmQtAutoGenerators::cmQtAutoGenerators()
  111. :Verbose(cmsys::SystemTools::GetEnv("VERBOSE") != 0)
  112. ,ColorOutput(true)
  113. ,RunMocFailed(false)
  114. ,RunUicFailed(false)
  115. ,RunRccFailed(false)
  116. ,GenerateAll(false)
  117. {
  118. std::string colorEnv = "";
  119. cmsys::SystemTools::GetEnv("COLOR", colorEnv);
  120. if(!colorEnv.empty())
  121. {
  122. if(cmSystemTools::IsOn(colorEnv.c_str()))
  123. {
  124. this->ColorOutput = true;
  125. }
  126. else
  127. {
  128. this->ColorOutput = false;
  129. }
  130. }
  131. }
  132. static std::string getAutogenTargetName(cmTarget const* target)
  133. {
  134. std::string autogenTargetName = target->GetName();
  135. autogenTargetName += "_automoc";
  136. return autogenTargetName;
  137. }
  138. static std::string getAutogenTargetDir(cmTarget const* target)
  139. {
  140. cmMakefile* makefile = target->GetMakefile();
  141. std::string targetDir = makefile->GetCurrentBinaryDirectory();
  142. targetDir += makefile->GetCMakeInstance()->GetCMakeFilesDirectory();
  143. targetDir += "/";
  144. targetDir += getAutogenTargetName(target);
  145. targetDir += ".dir/";
  146. return targetDir;
  147. }
  148. static std::string cmQtAutoGeneratorsStripCR(std::string const& line)
  149. {
  150. // Strip CR characters rcc may have printed (possibly more than one!).
  151. std::string::size_type cr = line.find('\r');
  152. if (cr != line.npos)
  153. {
  154. return line.substr(0, cr);
  155. }
  156. return line;
  157. }
  158. std::string cmQtAutoGenerators::ListQt5RccInputs(cmSourceFile* sf,
  159. cmTarget const* target,
  160. std::vector<std::string>& depends)
  161. {
  162. std::string rccCommand = this->GetRccExecutable(target);
  163. std::vector<std::string> qrcEntries;
  164. std::vector<std::string> command;
  165. command.push_back(rccCommand);
  166. command.push_back("--list");
  167. std::string absFile = cmsys::SystemTools::GetRealPath(
  168. sf->GetFullPath());
  169. command.push_back(absFile);
  170. std::string rccStdOut;
  171. std::string rccStdErr;
  172. int retVal = 0;
  173. bool result = cmSystemTools::RunSingleCommand(
  174. command, &rccStdOut, &rccStdErr,
  175. &retVal, 0, cmSystemTools::OUTPUT_NONE);
  176. if (!result || retVal)
  177. {
  178. std::cerr << "AUTOGEN: error: Rcc list process for " << sf->GetFullPath()
  179. << " failed:\n" << rccStdOut << "\n" << rccStdErr << std::endl;
  180. return std::string();
  181. }
  182. {
  183. std::istringstream ostr(rccStdOut);
  184. std::string oline;
  185. while(std::getline(ostr, oline))
  186. {
  187. oline = cmQtAutoGeneratorsStripCR(oline);
  188. if(!oline.empty())
  189. {
  190. qrcEntries.push_back(oline);
  191. }
  192. }
  193. }
  194. {
  195. std::istringstream estr(rccStdErr);
  196. std::string eline;
  197. while(std::getline(estr, eline))
  198. {
  199. eline = cmQtAutoGeneratorsStripCR(eline);
  200. if (cmHasLiteralPrefix(eline, "RCC: Error in"))
  201. {
  202. static std::string searchString = "Cannot find file '";
  203. std::string::size_type pos = eline.find(searchString);
  204. if (pos == std::string::npos)
  205. {
  206. std::cerr << "AUTOGEN: error: Rcc lists unparsable output "
  207. << eline << std::endl;
  208. return std::string();
  209. }
  210. pos += searchString.length();
  211. std::string::size_type sz = eline.size() - pos - 1;
  212. qrcEntries.push_back(eline.substr(pos, sz));
  213. }
  214. }
  215. }
  216. depends.insert(depends.end(), qrcEntries.begin(), qrcEntries.end());
  217. return cmJoin(qrcEntries, "@list_sep@");
  218. }
  219. std::string cmQtAutoGenerators::ListQt4RccInputs(cmSourceFile* sf,
  220. std::vector<std::string>& depends)
  221. {
  222. const std::string qrcContents = ReadAll(sf->GetFullPath());
  223. cmsys::RegularExpression fileMatchRegex("(<file[^<]+)");
  224. std::string entriesList;
  225. const char* sep = "";
  226. size_t offset = 0;
  227. while (fileMatchRegex.find(qrcContents.c_str() + offset))
  228. {
  229. std::string qrcEntry = fileMatchRegex.match(1);
  230. offset += qrcEntry.size();
  231. cmsys::RegularExpression fileReplaceRegex("(^<file[^>]*>)");
  232. fileReplaceRegex.find(qrcEntry);
  233. std::string tag = fileReplaceRegex.match(1);
  234. qrcEntry = qrcEntry.substr(tag.size());
  235. if (!cmSystemTools::FileIsFullPath(qrcEntry.c_str()))
  236. {
  237. qrcEntry = sf->GetLocation().GetDirectory() + "/" + qrcEntry;
  238. }
  239. entriesList += sep;
  240. entriesList += qrcEntry;
  241. sep = "@list_sep@";
  242. depends.push_back(qrcEntry);
  243. }
  244. return entriesList;
  245. }
  246. bool cmQtAutoGenerators::InitializeAutogenTarget(cmTarget* target)
  247. {
  248. cmMakefile* makefile = target->GetMakefile();
  249. // don't do anything if there is no Qt4 or Qt5Core (which contains moc):
  250. std::string qtMajorVersion = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
  251. if (qtMajorVersion == "")
  252. {
  253. qtMajorVersion = makefile->GetSafeDefinition("Qt5Core_VERSION_MAJOR");
  254. }
  255. if (qtMajorVersion != "4" && qtMajorVersion != "5")
  256. {
  257. return false;
  258. }
  259. if (target->GetPropertyAsBool("AUTOMOC"))
  260. {
  261. std::string automocTargetName = getAutogenTargetName(target);
  262. std::string mocCppFile = makefile->GetCurrentBinaryDirectory();
  263. mocCppFile += "/";
  264. mocCppFile += automocTargetName;
  265. mocCppFile += ".cpp";
  266. makefile->GetOrCreateSource(mocCppFile, true);
  267. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES",
  268. mocCppFile.c_str(), false);
  269. target->AddSource(mocCppFile);
  270. }
  271. // create a custom target for running generators at buildtime:
  272. std::string autogenTargetName = getAutogenTargetName(target);
  273. std::string targetDir = getAutogenTargetDir(target);
  274. cmCustomCommandLine currentLine;
  275. currentLine.push_back(makefile->GetSafeDefinition("CMAKE_COMMAND"));
  276. currentLine.push_back("-E");
  277. currentLine.push_back("cmake_autogen");
  278. currentLine.push_back(targetDir);
  279. currentLine.push_back("$<CONFIGURATION>");
  280. cmCustomCommandLines commandLines;
  281. commandLines.push_back(currentLine);
  282. std::string workingDirectory = cmSystemTools::CollapseFullPath(
  283. "", makefile->GetCurrentBinaryDirectory());
  284. std::vector<std::string> depends;
  285. if (const char *autogenDepends =
  286. target->GetProperty("AUTOGEN_TARGET_DEPENDS"))
  287. {
  288. cmSystemTools::ExpandListArgument(autogenDepends, depends);
  289. }
  290. std::vector<std::string> toolNames;
  291. if (target->GetPropertyAsBool("AUTOMOC"))
  292. {
  293. toolNames.push_back("moc");
  294. }
  295. if (target->GetPropertyAsBool("AUTOUIC"))
  296. {
  297. toolNames.push_back("uic");
  298. }
  299. if (target->GetPropertyAsBool("AUTORCC"))
  300. {
  301. toolNames.push_back("rcc");
  302. }
  303. std::string tools = toolNames[0];
  304. toolNames.erase(toolNames.begin());
  305. while (toolNames.size() > 1)
  306. {
  307. tools += ", " + toolNames[0];
  308. toolNames.erase(toolNames.begin());
  309. }
  310. if (toolNames.size() == 1)
  311. {
  312. tools += " and " + toolNames[0];
  313. }
  314. std::string autogenComment = "Automatic " + tools + " for target ";
  315. autogenComment += target->GetName();
  316. #if defined(_WIN32) && !defined(__CYGWIN__)
  317. bool usePRE_BUILD = false;
  318. cmLocalGenerator* localGen = makefile->GetLocalGenerator();
  319. cmGlobalGenerator* gg = localGen->GetGlobalGenerator();
  320. if(gg->GetName().find("Visual Studio") != std::string::npos)
  321. {
  322. cmLocalVisualStudioGenerator* vslg =
  323. static_cast<cmLocalVisualStudioGenerator*>(localGen);
  324. // Under VS >= 7 use a PRE_BUILD event instead of a separate target to
  325. // reduce the number of targets loaded into the IDE.
  326. // This also works around a VS 11 bug that may skip updating the target:
  327. // https://connect.microsoft.com/VisualStudio/feedback/details/769495
  328. usePRE_BUILD = vslg->GetVersion() >= cmLocalVisualStudioGenerator::VS7;
  329. if(usePRE_BUILD)
  330. {
  331. for (std::vector<std::string>::iterator it = depends.begin();
  332. it != depends.end(); ++it)
  333. {
  334. if(!makefile->FindTargetToUse(it->c_str()))
  335. {
  336. usePRE_BUILD = false;
  337. break;
  338. }
  339. }
  340. }
  341. }
  342. #endif
  343. std::vector<std::string> rcc_output;
  344. bool const isNinja =
  345. makefile->GetGlobalGenerator()->GetName() == "Ninja";
  346. if(isNinja
  347. #if defined(_WIN32) && !defined(__CYGWIN__)
  348. || usePRE_BUILD
  349. #endif
  350. )
  351. {
  352. std::vector<cmSourceFile*> srcFiles;
  353. target->GetConfigCommonSourceFiles(srcFiles);
  354. for(std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  355. fileIt != srcFiles.end();
  356. ++fileIt)
  357. {
  358. cmSourceFile* sf = *fileIt;
  359. std::string absFile = cmsys::SystemTools::GetRealPath(
  360. sf->GetFullPath());
  361. std::string ext = sf->GetExtension();
  362. if (target->GetPropertyAsBool("AUTORCC"))
  363. {
  364. if (ext == "qrc"
  365. && !cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTORCC")))
  366. {
  367. std::string basename = cmsys::SystemTools::
  368. GetFilenameWithoutLastExtension(absFile);
  369. std::string rcc_output_dir = target->GetSupportDirectory();
  370. cmSystemTools::MakeDirectory(rcc_output_dir.c_str());
  371. std::string rcc_output_file = rcc_output_dir;
  372. rcc_output_file += "/qrc_" + basename + ".cpp";
  373. rcc_output.push_back(rcc_output_file);
  374. if (!cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED")))
  375. {
  376. if (qtMajorVersion == "5")
  377. {
  378. this->ListQt5RccInputs(sf, target, depends);
  379. }
  380. else
  381. {
  382. this->ListQt4RccInputs(sf, depends);
  383. }
  384. #if defined(_WIN32) && !defined(__CYGWIN__)
  385. usePRE_BUILD = false;
  386. #endif
  387. }
  388. }
  389. }
  390. }
  391. }
  392. #if defined(_WIN32) && !defined(__CYGWIN__)
  393. if(usePRE_BUILD)
  394. {
  395. // Add the pre-build command directly to bypass the OBJECT_LIBRARY
  396. // rejection in cmMakefile::AddCustomCommandToTarget because we know
  397. // PRE_BUILD will work for an OBJECT_LIBRARY in this specific case.
  398. std::vector<std::string> no_output;
  399. std::vector<std::string> no_byproducts;
  400. cmCustomCommand cc(makefile, no_output, no_byproducts, depends,
  401. commandLines, autogenComment.c_str(),
  402. workingDirectory.c_str());
  403. cc.SetEscapeOldStyle(false);
  404. cc.SetEscapeAllowMakeVars(true);
  405. target->AddPreBuildCommand(cc);
  406. }
  407. else
  408. #endif
  409. {
  410. cmTarget* autogenTarget = 0;
  411. if (!rcc_output.empty() && !isNinja)
  412. {
  413. std::vector<std::string> no_byproducts;
  414. makefile->AddCustomCommandToOutput(rcc_output, no_byproducts,
  415. depends, "",
  416. commandLines, 0,
  417. workingDirectory.c_str(),
  418. false, false);
  419. cmCustomCommandLines no_commands;
  420. autogenTarget = makefile->AddUtilityCommand(
  421. autogenTargetName, true,
  422. workingDirectory.c_str(), rcc_output,
  423. no_commands, false, autogenComment.c_str());
  424. }
  425. else
  426. {
  427. autogenTarget = makefile->AddUtilityCommand(
  428. autogenTargetName, true,
  429. workingDirectory.c_str(),
  430. /*byproducts=*/rcc_output, depends,
  431. commandLines, false, autogenComment.c_str());
  432. }
  433. // Set target folder
  434. const char* autogenFolder = makefile->GetState()
  435. ->GetGlobalProperty("AUTOMOC_TARGETS_FOLDER");
  436. if (!autogenFolder)
  437. {
  438. autogenFolder = makefile->GetState()
  439. ->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
  440. }
  441. if (autogenFolder && *autogenFolder)
  442. {
  443. autogenTarget->SetProperty("FOLDER", autogenFolder);
  444. }
  445. else
  446. {
  447. // inherit FOLDER property from target (#13688)
  448. copyTargetProperty(autogenTarget, target, "FOLDER");
  449. }
  450. target->AddUtility(autogenTargetName);
  451. }
  452. return true;
  453. }
  454. static void GetCompileDefinitionsAndDirectories(cmTarget const* target,
  455. const std::string& config,
  456. std::string &incs,
  457. std::string &defs)
  458. {
  459. cmMakefile* makefile = target->GetMakefile();
  460. cmLocalGenerator* localGen = makefile->GetLocalGenerator();
  461. std::vector<std::string> includeDirs;
  462. cmGeneratorTarget *gtgt = localGen->GetGlobalGenerator()
  463. ->GetGeneratorTarget(target);
  464. // Get the include dirs for this target, without stripping the implicit
  465. // include dirs off, see http://public.kitware.com/Bug/view.php?id=13667
  466. localGen->GetIncludeDirectories(includeDirs, gtgt, "CXX", config, false);
  467. incs = cmJoin(includeDirs, ";");
  468. std::set<std::string> defines;
  469. localGen->AddCompileDefinitions(defines, target, config, "CXX");
  470. defs += cmJoin(defines, ";");
  471. }
  472. void cmQtAutoGenerators::SetupAutoGenerateTarget(cmTarget const* target)
  473. {
  474. cmMakefile* makefile = target->GetMakefile();
  475. // forget the variables added here afterwards again:
  476. cmMakefile::ScopePushPop varScope(makefile);
  477. static_cast<void>(varScope);
  478. // create a custom target for running generators at buildtime:
  479. std::string autogenTargetName = getAutogenTargetName(target);
  480. makefile->AddDefinition("_moc_target_name",
  481. cmLocalGenerator::EscapeForCMake(autogenTargetName).c_str());
  482. makefile->AddDefinition("_origin_target_name",
  483. cmLocalGenerator::EscapeForCMake(target->GetName()).c_str());
  484. std::string targetDir = getAutogenTargetDir(target);
  485. const char *qtVersion = makefile->GetDefinition("Qt5Core_VERSION_MAJOR");
  486. if (!qtVersion)
  487. {
  488. qtVersion = makefile->GetDefinition("QT_VERSION_MAJOR");
  489. }
  490. if (const char *targetQtVersion =
  491. target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION", ""))
  492. {
  493. qtVersion = targetQtVersion;
  494. }
  495. if (qtVersion)
  496. {
  497. makefile->AddDefinition("_target_qt_version", qtVersion);
  498. }
  499. std::map<std::string, std::string> configIncludes;
  500. std::map<std::string, std::string> configDefines;
  501. std::map<std::string, std::string> configUicOptions;
  502. if (target->GetPropertyAsBool("AUTOMOC")
  503. || target->GetPropertyAsBool("AUTOUIC")
  504. || target->GetPropertyAsBool("AUTORCC"))
  505. {
  506. this->SetupSourceFiles(target);
  507. }
  508. makefile->AddDefinition("_cpp_files",
  509. cmLocalGenerator::EscapeForCMake(this->Sources).c_str());
  510. if (target->GetPropertyAsBool("AUTOMOC"))
  511. {
  512. this->SetupAutoMocTarget(target, autogenTargetName,
  513. configIncludes, configDefines);
  514. }
  515. if (target->GetPropertyAsBool("AUTOUIC"))
  516. {
  517. this->SetupAutoUicTarget(target, configUicOptions);
  518. }
  519. if (target->GetPropertyAsBool("AUTORCC"))
  520. {
  521. this->SetupAutoRccTarget(target);
  522. }
  523. const char* cmakeRoot = makefile->GetSafeDefinition("CMAKE_ROOT");
  524. std::string inputFile = cmakeRoot;
  525. inputFile += "/Modules/AutogenInfo.cmake.in";
  526. std::string outputFile = targetDir;
  527. outputFile += "/AutogenInfo.cmake";
  528. makefile->AddDefinition("_qt_rcc_inputs",
  529. makefile->GetDefinition("_qt_rcc_inputs_" + target->GetName()));
  530. makefile->ConfigureFile(inputFile.c_str(), outputFile.c_str(),
  531. false, true, false);
  532. // Ensure we have write permission in case .in was read-only.
  533. mode_t perm = 0;
  534. #if defined(WIN32) && !defined(__CYGWIN__)
  535. mode_t mode_write = S_IWRITE;
  536. #else
  537. mode_t mode_write = S_IWUSR;
  538. #endif
  539. cmSystemTools::GetPermissions(outputFile, perm);
  540. if (!(perm & mode_write))
  541. {
  542. cmSystemTools::SetPermissions(outputFile, perm | mode_write);
  543. }
  544. if (!configDefines.empty()
  545. || !configIncludes.empty()
  546. || !configUicOptions.empty())
  547. {
  548. cmsys::ofstream infoFile(outputFile.c_str(), std::ios::app);
  549. if ( !infoFile )
  550. {
  551. std::string error = "Internal CMake error when trying to open file: ";
  552. error += outputFile.c_str();
  553. error += " for writing.";
  554. cmSystemTools::Error(error.c_str());
  555. return;
  556. }
  557. if (!configDefines.empty())
  558. {
  559. for (std::map<std::string, std::string>::iterator
  560. it = configDefines.begin(), end = configDefines.end();
  561. it != end; ++it)
  562. {
  563. infoFile << "set(AM_MOC_COMPILE_DEFINITIONS_" << it->first <<
  564. " " << it->second << ")\n";
  565. }
  566. }
  567. if (!configIncludes.empty())
  568. {
  569. for (std::map<std::string, std::string>::iterator
  570. it = configIncludes.begin(), end = configIncludes.end();
  571. it != end; ++it)
  572. {
  573. infoFile << "set(AM_MOC_INCLUDES_" << it->first <<
  574. " " << it->second << ")\n";
  575. }
  576. }
  577. if (!configUicOptions.empty())
  578. {
  579. for (std::map<std::string, std::string>::iterator
  580. it = configUicOptions.begin(), end = configUicOptions.end();
  581. it != end; ++it)
  582. {
  583. infoFile << "set(AM_UIC_TARGET_OPTIONS_" << it->first <<
  584. " " << it->second << ")\n";
  585. }
  586. }
  587. }
  588. }
  589. void cmQtAutoGenerators::SetupSourceFiles(cmTarget const* target)
  590. {
  591. cmMakefile* makefile = target->GetMakefile();
  592. const char* sepFiles = "";
  593. const char* sepHeaders = "";
  594. std::vector<cmSourceFile*> srcFiles;
  595. target->GetConfigCommonSourceFiles(srcFiles);
  596. const char *skipMocSep = "";
  597. const char *skipUicSep = "";
  598. std::vector<std::string> newRccFiles;
  599. for(std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  600. fileIt != srcFiles.end();
  601. ++fileIt)
  602. {
  603. cmSourceFile* sf = *fileIt;
  604. std::string absFile = cmsys::SystemTools::GetRealPath(
  605. sf->GetFullPath());
  606. bool skipMoc = cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTOMOC"));
  607. bool generated = cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED"));
  608. if(cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTOUIC")))
  609. {
  610. this->SkipUic += skipUicSep;
  611. this->SkipUic += absFile;
  612. skipUicSep = ";";
  613. }
  614. std::string ext = sf->GetExtension();
  615. if (target->GetPropertyAsBool("AUTORCC"))
  616. {
  617. if (ext == "qrc"
  618. && !cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTORCC")))
  619. {
  620. std::string basename = cmsys::SystemTools::
  621. GetFilenameWithoutLastExtension(absFile);
  622. std::string rcc_output_dir = target->GetSupportDirectory();
  623. cmSystemTools::MakeDirectory(rcc_output_dir.c_str());
  624. std::string rcc_output_file = rcc_output_dir;
  625. rcc_output_file += "/qrc_" + basename + ".cpp";
  626. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES",
  627. rcc_output_file.c_str(), false);
  628. makefile->GetOrCreateSource(rcc_output_file, true);
  629. newRccFiles.push_back(rcc_output_file);
  630. }
  631. }
  632. if (!generated)
  633. {
  634. if (skipMoc)
  635. {
  636. this->SkipMoc += skipMocSep;
  637. this->SkipMoc += absFile;
  638. skipMocSep = ";";
  639. }
  640. else
  641. {
  642. cmSystemTools::FileFormat fileType = cmSystemTools::GetFileFormat(
  643. ext.c_str());
  644. if (fileType == cmSystemTools::CXX_FILE_FORMAT)
  645. {
  646. this->Sources += sepFiles;
  647. this->Sources += absFile;
  648. sepFiles = ";";
  649. }
  650. else if (fileType == cmSystemTools::HEADER_FILE_FORMAT)
  651. {
  652. this->Headers += sepHeaders;
  653. this->Headers += absFile;
  654. sepHeaders = ";";
  655. }
  656. }
  657. }
  658. }
  659. for(std::vector<std::string>::const_iterator fileIt = newRccFiles.begin();
  660. fileIt != newRccFiles.end();
  661. ++fileIt)
  662. {
  663. const_cast<cmTarget*>(target)->AddSource(*fileIt);
  664. }
  665. }
  666. void cmQtAutoGenerators::SetupAutoMocTarget(cmTarget const* target,
  667. const std::string &autogenTargetName,
  668. std::map<std::string, std::string> &configIncludes,
  669. std::map<std::string, std::string> &configDefines)
  670. {
  671. cmMakefile* makefile = target->GetMakefile();
  672. const char* tmp = target->GetProperty("AUTOMOC_MOC_OPTIONS");
  673. std::string _moc_options = (tmp!=0 ? tmp : "");
  674. makefile->AddDefinition("_moc_options",
  675. cmLocalGenerator::EscapeForCMake(_moc_options).c_str());
  676. makefile->AddDefinition("_skip_moc",
  677. cmLocalGenerator::EscapeForCMake(this->SkipMoc).c_str());
  678. makefile->AddDefinition("_moc_headers",
  679. cmLocalGenerator::EscapeForCMake(this->Headers).c_str());
  680. bool relaxedMode = makefile->IsOn("CMAKE_AUTOMOC_RELAXED_MODE");
  681. makefile->AddDefinition("_moc_relaxed_mode", relaxedMode ? "TRUE" : "FALSE");
  682. std::string _moc_incs;
  683. std::string _moc_compile_defs;
  684. std::vector<std::string> configs;
  685. const std::string& config = makefile->GetConfigurations(configs);
  686. GetCompileDefinitionsAndDirectories(target, config,
  687. _moc_incs, _moc_compile_defs);
  688. makefile->AddDefinition("_moc_incs",
  689. cmLocalGenerator::EscapeForCMake(_moc_incs).c_str());
  690. makefile->AddDefinition("_moc_compile_defs",
  691. cmLocalGenerator::EscapeForCMake(_moc_compile_defs).c_str());
  692. for (std::vector<std::string>::const_iterator li = configs.begin();
  693. li != configs.end(); ++li)
  694. {
  695. std::string config_moc_incs;
  696. std::string config_moc_compile_defs;
  697. GetCompileDefinitionsAndDirectories(target, *li,
  698. config_moc_incs,
  699. config_moc_compile_defs);
  700. if (config_moc_incs != _moc_incs)
  701. {
  702. configIncludes[*li] =
  703. cmLocalGenerator::EscapeForCMake(config_moc_incs);
  704. if(_moc_incs.empty())
  705. {
  706. _moc_incs = config_moc_incs;
  707. }
  708. }
  709. if (config_moc_compile_defs != _moc_compile_defs)
  710. {
  711. configDefines[*li] =
  712. cmLocalGenerator::EscapeForCMake(config_moc_compile_defs);
  713. if(_moc_compile_defs.empty())
  714. {
  715. _moc_compile_defs = config_moc_compile_defs;
  716. }
  717. }
  718. }
  719. const char *qtVersion = makefile->GetDefinition("_target_qt_version");
  720. if (strcmp(qtVersion, "5") == 0)
  721. {
  722. cmTarget *qt5Moc = makefile->FindTargetToUse("Qt5::moc");
  723. if (!qt5Moc)
  724. {
  725. cmSystemTools::Error("Qt5::moc target not found ",
  726. autogenTargetName.c_str());
  727. return;
  728. }
  729. makefile->AddDefinition("_qt_moc_executable", qt5Moc->GetLocation(""));
  730. }
  731. else if (strcmp(qtVersion, "4") == 0)
  732. {
  733. cmTarget *qt4Moc = makefile->FindTargetToUse("Qt4::moc");
  734. if (!qt4Moc)
  735. {
  736. cmSystemTools::Error("Qt4::moc target not found ",
  737. autogenTargetName.c_str());
  738. return;
  739. }
  740. makefile->AddDefinition("_qt_moc_executable", qt4Moc->GetLocation(""));
  741. }
  742. else
  743. {
  744. cmSystemTools::Error("The CMAKE_AUTOMOC feature supports only Qt 4 and "
  745. "Qt 5 ", autogenTargetName.c_str());
  746. }
  747. }
  748. void cmQtAutoGenerators::MergeUicOptions(std::vector<std::string> &opts,
  749. const std::vector<std::string> &fileOpts,
  750. bool isQt5)
  751. {
  752. static const char* valueOptions[] = {
  753. "tr",
  754. "translate",
  755. "postfix",
  756. "generator",
  757. "include", // Since Qt 5.3
  758. "g"
  759. };
  760. std::vector<std::string> extraOpts;
  761. for(std::vector<std::string>::const_iterator it = fileOpts.begin();
  762. it != fileOpts.end(); ++it)
  763. {
  764. std::vector<std::string>::iterator existingIt
  765. = std::find(opts.begin(), opts.end(), *it);
  766. if (existingIt != opts.end())
  767. {
  768. const char *o = it->c_str();
  769. if (*o == '-')
  770. {
  771. ++o;
  772. }
  773. if (isQt5 && *o == '-')
  774. {
  775. ++o;
  776. }
  777. if (std::find_if(cmArrayBegin(valueOptions), cmArrayEnd(valueOptions),
  778. cmStrCmp(*it)) != cmArrayEnd(valueOptions))
  779. {
  780. assert(existingIt + 1 != opts.end());
  781. *(existingIt + 1) = *(it + 1);
  782. ++it;
  783. }
  784. }
  785. else
  786. {
  787. extraOpts.push_back(*it);
  788. }
  789. }
  790. opts.insert(opts.end(), extraOpts.begin(), extraOpts.end());
  791. }
  792. static void GetUicOpts(cmTarget const* target, const std::string& config,
  793. std::string &optString)
  794. {
  795. std::vector<std::string> opts;
  796. target->GetAutoUicOptions(opts, config);
  797. optString = cmJoin(opts, ";");
  798. }
  799. void cmQtAutoGenerators::SetupAutoUicTarget(cmTarget const* target,
  800. std::map<std::string, std::string> &configUicOptions)
  801. {
  802. cmMakefile *makefile = target->GetMakefile();
  803. std::set<std::string> skipped;
  804. std::vector<std::string> skipVec;
  805. cmSystemTools::ExpandListArgument(this->SkipUic, skipVec);
  806. skipped.insert(skipVec.begin(), skipVec.end());
  807. makefile->AddDefinition("_skip_uic",
  808. cmLocalGenerator::EscapeForCMake(this->SkipUic).c_str());
  809. std::vector<cmSourceFile*> uiFilesWithOptions
  810. = makefile->GetQtUiFilesWithOptions();
  811. const char *qtVersion = makefile->GetDefinition("_target_qt_version");
  812. std::string _uic_opts;
  813. std::vector<std::string> configs;
  814. const std::string& config = makefile->GetConfigurations(configs);
  815. GetUicOpts(target, config, _uic_opts);
  816. if (!_uic_opts.empty())
  817. {
  818. _uic_opts = cmLocalGenerator::EscapeForCMake(_uic_opts);
  819. makefile->AddDefinition("_uic_target_options", _uic_opts.c_str());
  820. }
  821. for (std::vector<std::string>::const_iterator li = configs.begin();
  822. li != configs.end(); ++li)
  823. {
  824. std::string config_uic_opts;
  825. GetUicOpts(target, *li, config_uic_opts);
  826. if (config_uic_opts != _uic_opts)
  827. {
  828. configUicOptions[*li] =
  829. cmLocalGenerator::EscapeForCMake(config_uic_opts);
  830. if(_uic_opts.empty())
  831. {
  832. _uic_opts = config_uic_opts;
  833. }
  834. }
  835. }
  836. std::string uiFileFiles;
  837. std::string uiFileOptions;
  838. const char* sep = "";
  839. for(std::vector<cmSourceFile*>::const_iterator fileIt =
  840. uiFilesWithOptions.begin();
  841. fileIt != uiFilesWithOptions.end();
  842. ++fileIt)
  843. {
  844. cmSourceFile* sf = *fileIt;
  845. std::string absFile = cmsys::SystemTools::GetRealPath(
  846. sf->GetFullPath());
  847. if (!skipped.insert(absFile).second)
  848. {
  849. continue;
  850. }
  851. uiFileFiles += sep;
  852. uiFileFiles += absFile;
  853. uiFileOptions += sep;
  854. std::string opts = sf->GetProperty("AUTOUIC_OPTIONS");
  855. cmSystemTools::ReplaceString(opts, ";", "@list_sep@");
  856. uiFileOptions += opts;
  857. sep = ";";
  858. }
  859. makefile->AddDefinition("_qt_uic_options_files",
  860. cmLocalGenerator::EscapeForCMake(uiFileFiles).c_str());
  861. makefile->AddDefinition("_qt_uic_options_options",
  862. cmLocalGenerator::EscapeForCMake(uiFileOptions).c_str());
  863. std::string targetName = target->GetName();
  864. if (strcmp(qtVersion, "5") == 0)
  865. {
  866. cmTarget *qt5Uic = makefile->FindTargetToUse("Qt5::uic");
  867. if (!qt5Uic)
  868. {
  869. // Project does not use Qt5Widgets, but has AUTOUIC ON anyway
  870. }
  871. else
  872. {
  873. makefile->AddDefinition("_qt_uic_executable", qt5Uic->GetLocation(""));
  874. }
  875. }
  876. else if (strcmp(qtVersion, "4") == 0)
  877. {
  878. cmTarget *qt4Uic = makefile->FindTargetToUse("Qt4::uic");
  879. if (!qt4Uic)
  880. {
  881. cmSystemTools::Error("Qt4::uic target not found ",
  882. targetName.c_str());
  883. return;
  884. }
  885. makefile->AddDefinition("_qt_uic_executable", qt4Uic->GetLocation(""));
  886. }
  887. else
  888. {
  889. cmSystemTools::Error("The CMAKE_AUTOUIC feature supports only Qt 4 and "
  890. "Qt 5 ", targetName.c_str());
  891. }
  892. }
  893. void cmQtAutoGenerators::MergeRccOptions(std::vector<std::string> &opts,
  894. const std::vector<std::string> &fileOpts,
  895. bool isQt5)
  896. {
  897. static const char* valueOptions[] = {
  898. "name",
  899. "root",
  900. "compress",
  901. "threshold"
  902. };
  903. std::vector<std::string> extraOpts;
  904. for(std::vector<std::string>::const_iterator it = fileOpts.begin();
  905. it != fileOpts.end(); ++it)
  906. {
  907. std::vector<std::string>::iterator existingIt
  908. = std::find(opts.begin(), opts.end(), *it);
  909. if (existingIt != opts.end())
  910. {
  911. const char *o = it->c_str();
  912. if (*o == '-')
  913. {
  914. ++o;
  915. }
  916. if (isQt5 && *o == '-')
  917. {
  918. ++o;
  919. }
  920. if (std::find_if(cmArrayBegin(valueOptions), cmArrayEnd(valueOptions),
  921. cmStrCmp(*it)) != cmArrayEnd(valueOptions))
  922. {
  923. assert(existingIt + 1 != opts.end());
  924. *(existingIt + 1) = *(it + 1);
  925. ++it;
  926. }
  927. }
  928. else
  929. {
  930. extraOpts.push_back(*it);
  931. }
  932. }
  933. opts.insert(opts.end(), extraOpts.begin(), extraOpts.end());
  934. }
  935. void cmQtAutoGenerators::SetupAutoRccTarget(cmTarget const* target)
  936. {
  937. std::string _rcc_files;
  938. const char* sepRccFiles = "";
  939. cmMakefile *makefile = target->GetMakefile();
  940. std::vector<cmSourceFile*> srcFiles;
  941. target->GetConfigCommonSourceFiles(srcFiles);
  942. std::string qrcInputs;
  943. const char* qrcInputsSep = "";
  944. std::string rccFileFiles;
  945. std::string rccFileOptions;
  946. const char *optionSep = "";
  947. const char *qtVersion = makefile->GetDefinition("_target_qt_version");
  948. std::vector<std::string> rccOptions;
  949. if (const char* opts = target->GetProperty("AUTORCC_OPTIONS"))
  950. {
  951. cmSystemTools::ExpandListArgument(opts, rccOptions);
  952. }
  953. std::string qtMajorVersion = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
  954. if (qtMajorVersion == "")
  955. {
  956. qtMajorVersion = makefile->GetSafeDefinition("Qt5Core_VERSION_MAJOR");
  957. }
  958. for(std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  959. fileIt != srcFiles.end();
  960. ++fileIt)
  961. {
  962. cmSourceFile* sf = *fileIt;
  963. std::string ext = sf->GetExtension();
  964. if (ext == "qrc")
  965. {
  966. std::string absFile = cmsys::SystemTools::GetRealPath(
  967. sf->GetFullPath());
  968. bool skip = cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTORCC"));
  969. if (!skip)
  970. {
  971. _rcc_files += sepRccFiles;
  972. _rcc_files += absFile;
  973. sepRccFiles = ";";
  974. if (const char *prop = sf->GetProperty("AUTORCC_OPTIONS"))
  975. {
  976. std::vector<std::string> optsVec;
  977. cmSystemTools::ExpandListArgument(prop, optsVec);
  978. this->MergeRccOptions(rccOptions, optsVec,
  979. strcmp(qtVersion, "5") == 0);
  980. }
  981. if (!rccOptions.empty())
  982. {
  983. rccFileFiles += optionSep;
  984. rccFileFiles += absFile;
  985. rccFileOptions += optionSep;
  986. }
  987. const char *listSep = "";
  988. for(std::vector<std::string>::const_iterator it = rccOptions.begin();
  989. it != rccOptions.end();
  990. ++it)
  991. {
  992. rccFileOptions += listSep;
  993. rccFileOptions += *it;
  994. listSep = "@list_sep@";
  995. }
  996. optionSep = ";";
  997. std::vector<std::string> depends;
  998. std::string entriesList;
  999. if (!cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED")))
  1000. {
  1001. if (qtMajorVersion == "5")
  1002. {
  1003. entriesList = this->ListQt5RccInputs(sf, target, depends);
  1004. }
  1005. else
  1006. {
  1007. entriesList = this->ListQt4RccInputs(sf, depends);
  1008. }
  1009. if (entriesList.empty())
  1010. {
  1011. return;
  1012. }
  1013. }
  1014. qrcInputs += qrcInputsSep;
  1015. qrcInputs += entriesList;
  1016. qrcInputsSep = ";";
  1017. }
  1018. }
  1019. }
  1020. makefile->AddDefinition("_qt_rcc_inputs_" + target->GetName(),
  1021. cmLocalGenerator::EscapeForCMake(qrcInputs).c_str());
  1022. makefile->AddDefinition("_rcc_files",
  1023. cmLocalGenerator::EscapeForCMake(_rcc_files).c_str());
  1024. makefile->AddDefinition("_qt_rcc_options_files",
  1025. cmLocalGenerator::EscapeForCMake(rccFileFiles).c_str());
  1026. makefile->AddDefinition("_qt_rcc_options_options",
  1027. cmLocalGenerator::EscapeForCMake(rccFileOptions).c_str());
  1028. makefile->AddDefinition("_qt_rcc_executable",
  1029. this->GetRccExecutable(target).c_str());
  1030. }
  1031. std::string cmQtAutoGenerators::GetRccExecutable(cmTarget const* target)
  1032. {
  1033. cmMakefile *makefile = target->GetMakefile();
  1034. const char *qtVersion = makefile->GetDefinition("_target_qt_version");
  1035. if (!qtVersion)
  1036. {
  1037. qtVersion = makefile->GetDefinition("Qt5Core_VERSION_MAJOR");
  1038. if (!qtVersion)
  1039. {
  1040. qtVersion = makefile->GetDefinition("QT_VERSION_MAJOR");
  1041. }
  1042. if (const char *targetQtVersion =
  1043. target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION",
  1044. ""))
  1045. {
  1046. qtVersion = targetQtVersion;
  1047. }
  1048. }
  1049. std::string targetName = target->GetName();
  1050. if (strcmp(qtVersion, "5") == 0)
  1051. {
  1052. cmTarget *qt5Rcc = makefile->FindTargetToUse("Qt5::rcc");
  1053. if (!qt5Rcc)
  1054. {
  1055. cmSystemTools::Error("Qt5::rcc target not found ",
  1056. targetName.c_str());
  1057. return std::string();
  1058. }
  1059. return qt5Rcc->GetLocation("");
  1060. }
  1061. else if (strcmp(qtVersion, "4") == 0)
  1062. {
  1063. cmTarget *qt4Rcc = makefile->FindTargetToUse("Qt4::rcc");
  1064. if (!qt4Rcc)
  1065. {
  1066. cmSystemTools::Error("Qt4::rcc target not found ",
  1067. targetName.c_str());
  1068. return std::string();
  1069. }
  1070. return qt4Rcc->GetLocation("");
  1071. }
  1072. cmSystemTools::Error("The CMAKE_AUTORCC feature supports only Qt 4 and "
  1073. "Qt 5 ", targetName.c_str());
  1074. return std::string();
  1075. }
  1076. static cmGlobalGenerator* CreateGlobalGenerator(cmake* cm,
  1077. const std::string& targetDirectory)
  1078. {
  1079. cmGlobalGenerator* gg = new cmGlobalGenerator();
  1080. gg->SetCMakeInstance(cm);
  1081. cm->SetHomeOutputDirectory(targetDirectory);
  1082. cm->SetHomeDirectory(targetDirectory);
  1083. cmLocalGenerator* lg = gg->MakeLocalGenerator();
  1084. lg->GetMakefile()->SetCurrentBinaryDirectory(targetDirectory);
  1085. lg->GetMakefile()->SetCurrentSourceDirectory(targetDirectory);
  1086. gg->SetCurrentLocalGenerator(lg);
  1087. return gg;
  1088. }
  1089. bool cmQtAutoGenerators::Run(const std::string& targetDirectory,
  1090. const std::string& config)
  1091. {
  1092. bool success = true;
  1093. cmake cm;
  1094. cm.SetHomeOutputDirectory(targetDirectory);
  1095. cm.SetHomeDirectory(targetDirectory);
  1096. cmGlobalGenerator* gg = CreateGlobalGenerator(&cm, targetDirectory);
  1097. cmMakefile* makefile = gg->GetCurrentLocalGenerator()->GetMakefile();
  1098. this->ReadAutogenInfoFile(makefile, targetDirectory, config);
  1099. this->ReadOldMocDefinitionsFile(makefile, targetDirectory);
  1100. this->Init();
  1101. if (this->QtMajorVersion == "4" || this->QtMajorVersion == "5")
  1102. {
  1103. success = this->RunAutogen(makefile);
  1104. }
  1105. this->WriteOldMocDefinitionsFile(targetDirectory);
  1106. delete gg->GetCurrentLocalGenerator();
  1107. delete gg;
  1108. gg = NULL;
  1109. makefile = NULL;
  1110. return success;
  1111. }
  1112. bool cmQtAutoGenerators::ReadAutogenInfoFile(cmMakefile* makefile,
  1113. const std::string& targetDirectory,
  1114. const std::string& config)
  1115. {
  1116. std::string filename(
  1117. cmSystemTools::CollapseFullPath(targetDirectory));
  1118. cmSystemTools::ConvertToUnixSlashes(filename);
  1119. filename += "/AutogenInfo.cmake";
  1120. if (!makefile->ReadListFile(filename.c_str()))
  1121. {
  1122. cmSystemTools::Error("Error processing file: ", filename.c_str());
  1123. return false;
  1124. }
  1125. this->QtMajorVersion = makefile->GetSafeDefinition("AM_QT_VERSION_MAJOR");
  1126. if (this->QtMajorVersion == "")
  1127. {
  1128. this->QtMajorVersion = makefile->GetSafeDefinition(
  1129. "AM_Qt5Core_VERSION_MAJOR");
  1130. }
  1131. this->Sources = makefile->GetSafeDefinition("AM_SOURCES");
  1132. {
  1133. std::string rccSources = makefile->GetSafeDefinition("AM_RCC_SOURCES");
  1134. cmSystemTools::ExpandListArgument(rccSources, this->RccSources);
  1135. }
  1136. this->SkipMoc = makefile->GetSafeDefinition("AM_SKIP_MOC");
  1137. this->SkipUic = makefile->GetSafeDefinition("AM_SKIP_UIC");
  1138. this->Headers = makefile->GetSafeDefinition("AM_HEADERS");
  1139. this->IncludeProjectDirsBefore = makefile->IsOn(
  1140. "AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE");
  1141. this->Srcdir = makefile->GetSafeDefinition("AM_CMAKE_CURRENT_SOURCE_DIR");
  1142. this->Builddir = makefile->GetSafeDefinition("AM_CMAKE_CURRENT_BINARY_DIR");
  1143. this->MocExecutable = makefile->GetSafeDefinition("AM_QT_MOC_EXECUTABLE");
  1144. this->UicExecutable = makefile->GetSafeDefinition("AM_QT_UIC_EXECUTABLE");
  1145. this->RccExecutable = makefile->GetSafeDefinition("AM_QT_RCC_EXECUTABLE");
  1146. {
  1147. std::string compileDefsPropOrig = "AM_MOC_COMPILE_DEFINITIONS";
  1148. std::string compileDefsProp = compileDefsPropOrig;
  1149. if(!config.empty())
  1150. {
  1151. compileDefsProp += "_";
  1152. compileDefsProp += config;
  1153. }
  1154. const char *compileDefs = makefile->GetDefinition(compileDefsProp);
  1155. this->MocCompileDefinitionsStr = compileDefs ? compileDefs
  1156. : makefile->GetSafeDefinition(compileDefsPropOrig);
  1157. }
  1158. {
  1159. std::string includesPropOrig = "AM_MOC_INCLUDES";
  1160. std::string includesProp = includesPropOrig;
  1161. if(!config.empty())
  1162. {
  1163. includesProp += "_";
  1164. includesProp += config;
  1165. }
  1166. const char *includes = makefile->GetDefinition(includesProp);
  1167. this->MocIncludesStr = includes ? includes
  1168. : makefile->GetSafeDefinition(includesPropOrig);
  1169. }
  1170. this->MocOptionsStr = makefile->GetSafeDefinition("AM_MOC_OPTIONS");
  1171. this->ProjectBinaryDir = makefile->GetSafeDefinition("AM_CMAKE_BINARY_DIR");
  1172. this->ProjectSourceDir = makefile->GetSafeDefinition("AM_CMAKE_SOURCE_DIR");
  1173. this->TargetName = makefile->GetSafeDefinition("AM_TARGET_NAME");
  1174. this->OriginTargetName
  1175. = makefile->GetSafeDefinition("AM_ORIGIN_TARGET_NAME");
  1176. {
  1177. const char *uicOptionsFiles
  1178. = makefile->GetSafeDefinition("AM_UIC_OPTIONS_FILES");
  1179. std::string uicOptionsPropOrig = "AM_UIC_TARGET_OPTIONS";
  1180. std::string uicOptionsProp = uicOptionsPropOrig;
  1181. if(!config.empty())
  1182. {
  1183. uicOptionsProp += "_";
  1184. uicOptionsProp += config;
  1185. }
  1186. const char *uicTargetOptions
  1187. = makefile->GetSafeDefinition(uicOptionsProp);
  1188. cmSystemTools::ExpandListArgument(
  1189. uicTargetOptions ? uicTargetOptions
  1190. : makefile->GetSafeDefinition(uicOptionsPropOrig),
  1191. this->UicTargetOptions);
  1192. const char *uicOptionsOptions
  1193. = makefile->GetSafeDefinition("AM_UIC_OPTIONS_OPTIONS");
  1194. std::vector<std::string> uicFilesVec;
  1195. cmSystemTools::ExpandListArgument(uicOptionsFiles, uicFilesVec);
  1196. std::vector<std::string> uicOptionsVec;
  1197. cmSystemTools::ExpandListArgument(uicOptionsOptions, uicOptionsVec);
  1198. if (uicFilesVec.size() != uicOptionsVec.size())
  1199. {
  1200. return false;
  1201. }
  1202. for (std::vector<std::string>::iterator fileIt = uicFilesVec.begin(),
  1203. optionIt = uicOptionsVec.begin();
  1204. fileIt != uicFilesVec.end();
  1205. ++fileIt, ++optionIt)
  1206. {
  1207. cmSystemTools::ReplaceString(*optionIt, "@list_sep@", ";");
  1208. this->UicOptions[*fileIt] = *optionIt;
  1209. }
  1210. }
  1211. {
  1212. const char *rccOptionsFiles
  1213. = makefile->GetSafeDefinition("AM_RCC_OPTIONS_FILES");
  1214. const char *rccOptionsOptions
  1215. = makefile->GetSafeDefinition("AM_RCC_OPTIONS_OPTIONS");
  1216. std::vector<std::string> rccFilesVec;
  1217. cmSystemTools::ExpandListArgument(rccOptionsFiles, rccFilesVec);
  1218. std::vector<std::string> rccOptionsVec;
  1219. cmSystemTools::ExpandListArgument(rccOptionsOptions, rccOptionsVec);
  1220. if (rccFilesVec.size() != rccOptionsVec.size())
  1221. {
  1222. return false;
  1223. }
  1224. for (std::vector<std::string>::iterator fileIt = rccFilesVec.begin(),
  1225. optionIt = rccOptionsVec.begin();
  1226. fileIt != rccFilesVec.end();
  1227. ++fileIt, ++optionIt)
  1228. {
  1229. cmSystemTools::ReplaceString(*optionIt, "@list_sep@", ";");
  1230. this->RccOptions[*fileIt] = *optionIt;
  1231. }
  1232. const char *rccInputs = makefile->GetSafeDefinition("AM_RCC_INPUTS");
  1233. std::vector<std::string> rccInputLists;
  1234. cmSystemTools::ExpandListArgument(rccInputs, rccInputLists);
  1235. if (this->RccSources.size() != rccInputLists.size())
  1236. {
  1237. cmSystemTools::Error("Error processing file: ", filename.c_str());
  1238. return false;
  1239. }
  1240. for (std::vector<std::string>::iterator fileIt = this->RccSources.begin(),
  1241. inputIt = rccInputLists.begin();
  1242. fileIt != this->RccSources.end();
  1243. ++fileIt, ++inputIt)
  1244. {
  1245. cmSystemTools::ReplaceString(*inputIt, "@list_sep@", ";");
  1246. std::vector<std::string> rccInputFiles;
  1247. cmSystemTools::ExpandListArgument(*inputIt, rccInputFiles);
  1248. this->RccInputs[*fileIt] = rccInputFiles;
  1249. }
  1250. }
  1251. this->CurrentCompileSettingsStr = this->MakeCompileSettingsString(makefile);
  1252. this->RelaxedMode = makefile->IsOn("AM_RELAXED_MODE");
  1253. return true;
  1254. }
  1255. std::string cmQtAutoGenerators::MakeCompileSettingsString(cmMakefile* makefile)
  1256. {
  1257. std::string s;
  1258. s += makefile->GetSafeDefinition("AM_MOC_COMPILE_DEFINITIONS");
  1259. s += " ~~~ ";
  1260. s += makefile->GetSafeDefinition("AM_MOC_INCLUDES");
  1261. s += " ~~~ ";
  1262. s += makefile->GetSafeDefinition("AM_MOC_OPTIONS");
  1263. s += " ~~~ ";
  1264. s += makefile->IsOn("AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE") ? "TRUE"
  1265. : "FALSE";
  1266. s += " ~~~ ";
  1267. return s;
  1268. }
  1269. bool cmQtAutoGenerators::ReadOldMocDefinitionsFile(cmMakefile* makefile,
  1270. const std::string& targetDirectory)
  1271. {
  1272. std::string filename(
  1273. cmSystemTools::CollapseFullPath(targetDirectory));
  1274. cmSystemTools::ConvertToUnixSlashes(filename);
  1275. filename += "/AutomocOldMocDefinitions.cmake";
  1276. if (makefile->ReadListFile(filename.c_str()))
  1277. {
  1278. this->OldCompileSettingsStr =
  1279. makefile->GetSafeDefinition("AM_OLD_COMPILE_SETTINGS");
  1280. }
  1281. return true;
  1282. }
  1283. void
  1284. cmQtAutoGenerators::WriteOldMocDefinitionsFile(
  1285. const std::string& targetDirectory)
  1286. {
  1287. std::string filename(
  1288. cmSystemTools::CollapseFullPath(targetDirectory));
  1289. cmSystemTools::ConvertToUnixSlashes(filename);
  1290. filename += "/AutomocOldMocDefinitions.cmake";
  1291. cmsys::ofstream outfile;
  1292. outfile.open(filename.c_str(),
  1293. std::ios::trunc);
  1294. outfile << "set(AM_OLD_COMPILE_SETTINGS "
  1295. << cmLocalGenerator::EscapeForCMake(
  1296. this->CurrentCompileSettingsStr) << ")\n";
  1297. outfile.close();
  1298. }
  1299. void cmQtAutoGenerators::Init()
  1300. {
  1301. this->OutMocCppFilename = this->Builddir;
  1302. this->OutMocCppFilename += this->TargetName;
  1303. this->OutMocCppFilename += ".cpp";
  1304. std::vector<std::string> cdefList;
  1305. cmSystemTools::ExpandListArgument(this->MocCompileDefinitionsStr, cdefList);
  1306. for(std::vector<std::string>::const_iterator it = cdefList.begin();
  1307. it != cdefList.end();
  1308. ++it)
  1309. {
  1310. this->MocDefinitions.push_back("-D" + (*it));
  1311. }
  1312. cmSystemTools::ExpandListArgument(this->MocOptionsStr, this->MocOptions);
  1313. std::vector<std::string> incPaths;
  1314. cmSystemTools::ExpandListArgument(this->MocIncludesStr, incPaths);
  1315. std::set<std::string> frameworkPaths;
  1316. for(std::vector<std::string>::const_iterator it = incPaths.begin();
  1317. it != incPaths.end();
  1318. ++it)
  1319. {
  1320. const std::string &path = *it;
  1321. this->MocIncludes.push_back("-I" + path);
  1322. if (cmHasLiteralSuffix(path, ".framework/Headers"))
  1323. {
  1324. // Go up twice to get to the framework root
  1325. std::vector<std::string> pathComponents;
  1326. cmsys::SystemTools::SplitPath(path, pathComponents);
  1327. std::string frameworkPath =cmsys::SystemTools::JoinPath(
  1328. pathComponents.begin(), pathComponents.end() - 2);
  1329. frameworkPaths.insert(frameworkPath);
  1330. }
  1331. }
  1332. for (std::set<std::string>::const_iterator it = frameworkPaths.begin();
  1333. it != frameworkPaths.end(); ++it)
  1334. {
  1335. this->MocIncludes.push_back("-F");
  1336. this->MocIncludes.push_back(*it);
  1337. }
  1338. if (this->IncludeProjectDirsBefore)
  1339. {
  1340. const std::string binDir = "-I" + this->ProjectBinaryDir;
  1341. const std::string srcDir = "-I" + this->ProjectSourceDir;
  1342. std::list<std::string> sortedMocIncludes;
  1343. std::list<std::string>::iterator it = this->MocIncludes.begin();
  1344. while (it != this->MocIncludes.end())
  1345. {
  1346. if (this->StartsWith(*it, binDir))
  1347. {
  1348. sortedMocIncludes.push_back(*it);
  1349. it = this->MocIncludes.erase(it);
  1350. }
  1351. else
  1352. {
  1353. ++it;
  1354. }
  1355. }
  1356. it = this->MocIncludes.begin();
  1357. while (it != this->MocIncludes.end())
  1358. {
  1359. if (this->StartsWith(*it, srcDir))
  1360. {
  1361. sortedMocIncludes.push_back(*it);
  1362. it = this->MocIncludes.erase(it);
  1363. }
  1364. else
  1365. {
  1366. ++it;
  1367. }
  1368. }
  1369. sortedMocIncludes.insert(sortedMocIncludes.end(),
  1370. this->MocIncludes.begin(), this->MocIncludes.end());
  1371. this->MocIncludes = sortedMocIncludes;
  1372. }
  1373. }
  1374. bool cmQtAutoGenerators::RunAutogen(cmMakefile* makefile)
  1375. {
  1376. if (!cmsys::SystemTools::FileExists(this->OutMocCppFilename.c_str())
  1377. || (this->OldCompileSettingsStr != this->CurrentCompileSettingsStr))
  1378. {
  1379. this->GenerateAll = true;
  1380. }
  1381. // the program goes through all .cpp files to see which moc files are
  1382. // included. It is not really interesting how the moc file is named, but
  1383. // what file the moc is created from. Once a moc is included the same moc
  1384. // may not be included in the _automoc.cpp file anymore. OTOH if there's a
  1385. // header containing Q_OBJECT where no corresponding moc file is included
  1386. // anywhere a moc_<filename>.cpp file is created and included in
  1387. // the _automoc.cpp file.
  1388. // key = moc source filepath, value = moc output filepath
  1389. std::map<std::string, std::string> includedMocs;
  1390. // collect all headers which may need to be mocced
  1391. std::set<std::string> headerFiles;
  1392. std::vector<std::string> sourceFiles;
  1393. cmSystemTools::ExpandListArgument(this->Sources, sourceFiles);
  1394. const std::vector<std::string>& headerExtensions =
  1395. makefile->GetHeaderExtensions();
  1396. std::map<std::string, std::vector<std::string> > includedUis;
  1397. std::map<std::string, std::vector<std::string> > skippedUis;
  1398. std::vector<std::string> uicSkipped;
  1399. cmSystemTools::ExpandListArgument(this->SkipUic, uicSkipped);
  1400. for (std::vector<std::string>::const_iterator it = sourceFiles.begin();
  1401. it != sourceFiles.end();
  1402. ++it)
  1403. {
  1404. const bool skipUic = std::find(uicSkipped.begin(), uicSkipped.end(), *it)
  1405. != uicSkipped.end();
  1406. std::map<std::string, std::vector<std::string> >& uiFiles
  1407. = skipUic ? skippedUis : includedUis;
  1408. const std::string &absFilename = *it;
  1409. if (this->Verbose)
  1410. {
  1411. std::cout << "AUTOGEN: Checking " << absFilename << std::endl;
  1412. }
  1413. if (this->RelaxedMode)
  1414. {
  1415. this->ParseCppFile(absFilename, headerExtensions, includedMocs,
  1416. uiFiles);
  1417. }
  1418. else
  1419. {
  1420. this->StrictParseCppFile(absFilename, headerExtensions, includedMocs,
  1421. uiFiles);
  1422. }
  1423. this->SearchHeadersForCppFile(absFilename, headerExtensions, headerFiles);
  1424. }
  1425. {
  1426. std::vector<std::string> mocSkipped;
  1427. cmSystemTools::ExpandListArgument(this->SkipMoc, mocSkipped);
  1428. for (std::vector<std::string>::const_iterator it = mocSkipped.begin();
  1429. it != mocSkipped.end();
  1430. ++it)
  1431. {
  1432. if (std::find(uicSkipped.begin(), uicSkipped.end(), *it)
  1433. != uicSkipped.end())
  1434. {
  1435. const std::string &absFilename = *it;
  1436. if (this->Verbose)
  1437. {
  1438. std::cout << "AUTOGEN: Checking " << absFilename << std::endl;
  1439. }
  1440. this->ParseForUic(absFilename, includedUis);
  1441. }
  1442. }
  1443. }
  1444. std::vector<std::string> headerFilesVec;
  1445. cmSystemTools::ExpandListArgument(this->Headers, headerFilesVec);
  1446. headerFiles.insert(headerFilesVec.begin(), headerFilesVec.end());
  1447. // key = moc source filepath, value = moc output filename
  1448. std::map<std::string, std::string> notIncludedMocs;
  1449. this->ParseHeaders(headerFiles, includedMocs, notIncludedMocs, includedUis);
  1450. // run moc on all the moc's that are #included in source files
  1451. for(std::map<std::string, std::string>::const_iterator
  1452. it = includedMocs.begin();
  1453. it != includedMocs.end();
  1454. ++it)
  1455. {
  1456. this->GenerateMoc(it->first, it->second);
  1457. }
  1458. for(std::map<std::string, std::vector<std::string> >::const_iterator
  1459. it = includedUis.begin();
  1460. it != includedUis.end();
  1461. ++it)
  1462. {
  1463. for (std::vector<std::string>::const_iterator nit = it->second.begin();
  1464. nit != it->second.end();
  1465. ++nit)
  1466. {
  1467. this->GenerateUi(it->first, *nit);
  1468. }
  1469. }
  1470. if(!this->RccExecutable.empty())
  1471. {
  1472. this->GenerateQrc();
  1473. }
  1474. cmsys_ios::stringstream outStream;
  1475. outStream << "/* This file is autogenerated, do not edit*/\n";
  1476. bool automocCppChanged = false;
  1477. if (notIncludedMocs.empty())
  1478. {
  1479. outStream << "enum some_compilers { need_more_than_nothing };\n";
  1480. }
  1481. else
  1482. {
  1483. // run moc on the remaining headers and include them in
  1484. // the _automoc.cpp file
  1485. for(std::map<std::string, std::string>::const_iterator
  1486. it = notIncludedMocs.begin();
  1487. it != notIncludedMocs.end();
  1488. ++it)
  1489. {
  1490. bool mocSuccess = this->GenerateMoc(it->first, it->second);
  1491. if (mocSuccess)
  1492. {
  1493. automocCppChanged = true;
  1494. }
  1495. outStream << "#include \"" << it->second << "\"\n";
  1496. }
  1497. }
  1498. if (this->RunMocFailed)
  1499. {
  1500. std::cerr << "moc failed..." << std::endl;
  1501. return false;
  1502. }
  1503. if (this->RunUicFailed)
  1504. {
  1505. std::cerr << "uic failed..." << std::endl;
  1506. return false;
  1507. }
  1508. if (this->RunRccFailed)
  1509. {
  1510. std::cerr << "rcc failed..." << std::endl;
  1511. return false;
  1512. }
  1513. outStream.flush();
  1514. std::string automocSource = outStream.str();
  1515. if (!automocCppChanged)
  1516. {
  1517. // compare contents of the _automoc.cpp file
  1518. const std::string oldContents = ReadAll(this->OutMocCppFilename);
  1519. if (oldContents == automocSource)
  1520. {
  1521. // nothing changed: don't touch the _automoc.cpp file
  1522. return true;
  1523. }
  1524. }
  1525. // source file that includes all remaining moc files (_automoc.cpp file)
  1526. cmsys::ofstream outfile;
  1527. outfile.open(this->OutMocCppFilename.c_str(),
  1528. std::ios::trunc);
  1529. outfile << automocSource;
  1530. outfile.close();
  1531. return true;
  1532. }
  1533. void cmQtAutoGenerators::ParseCppFile(const std::string& absFilename,
  1534. const std::vector<std::string>& headerExtensions,
  1535. std::map<std::string, std::string>& includedMocs,
  1536. std::map<std::string, std::vector<std::string> > &includedUis)
  1537. {
  1538. cmsys::RegularExpression mocIncludeRegExp(
  1539. "[\n][ \t]*#[ \t]*include[ \t]+"
  1540. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  1541. const std::string contentsString = ReadAll(absFilename);
  1542. if (contentsString.empty())
  1543. {
  1544. std::cerr << "AUTOGEN: warning: " << absFilename << ": file is empty\n"
  1545. << std::endl;
  1546. return;
  1547. }
  1548. this->ParseForUic(absFilename, contentsString, includedUis);
  1549. if (this->MocExecutable.empty())
  1550. {
  1551. return;
  1552. }
  1553. const std::string absPath = cmsys::SystemTools::GetFilenamePath(
  1554. cmsys::SystemTools::GetRealPath(absFilename)) + '/';
  1555. const std::string scannedFileBasename = cmsys::SystemTools::
  1556. GetFilenameWithoutLastExtension(absFilename);
  1557. std::string macroName;
  1558. const bool requiresMoc = requiresMocing(contentsString, macroName);
  1559. bool dotMocIncluded = false;
  1560. bool mocUnderscoreIncluded = false;
  1561. std::string ownMocUnderscoreFile;
  1562. std::string ownDotMocFile;
  1563. std::string ownMocHeaderFile;
  1564. std::string::size_type matchOffset = 0;
  1565. // first a simple string check for "moc" is *much* faster than the regexp,
  1566. // and if the string search already fails, we don't have to try the
  1567. // expensive regexp
  1568. if ((strstr(contentsString.c_str(), "moc") != NULL)
  1569. && (mocIncludeRegExp.find(contentsString)))
  1570. {
  1571. // for every moc include in the file
  1572. do
  1573. {
  1574. const std::string currentMoc = mocIncludeRegExp.match(1);
  1575. //std::cout << "found moc include: " << currentMoc << std::endl;
  1576. std::string basename = cmsys::SystemTools::
  1577. GetFilenameWithoutLastExtension(currentMoc);
  1578. const bool moc_style = cmHasLiteralPrefix(basename, "moc_");
  1579. // If the moc include is of the moc_foo.cpp style we expect
  1580. // the Q_OBJECT class declaration in a header file.
  1581. // If the moc include is of the foo.moc style we need to look for
  1582. // a Q_OBJECT macro in the current source file, if it contains the
  1583. // macro we generate the moc file from the source file.
  1584. // Q_OBJECT
  1585. if (moc_style)
  1586. {
  1587. // basename should be the part of the moc filename used for
  1588. // finding the correct header, so we need to remove the moc_ part
  1589. basename = basename.substr(4);
  1590. std::string mocSubDir = extractSubDir(absPath, currentMoc);
  1591. std::string headerToMoc = findMatchingHeader(
  1592. absPath, mocSubDir, basename, headerExtensions);
  1593. if (!headerToMoc.empty())
  1594. {
  1595. includedMocs[headerToMoc] = currentMoc;
  1596. if (basename == scannedFileBasename)
  1597. {
  1598. mocUnderscoreIncluded = true;
  1599. ownMocUnderscoreFile = currentMoc;
  1600. ownMocHeaderFile = headerToMoc;
  1601. }
  1602. }
  1603. else
  1604. {
  1605. std::cerr << "AUTOGEN: error: " << absFilename << ": The file "
  1606. << "includes the moc file \"" << currentMoc << "\", "
  1607. << "but could not find header \"" << basename
  1608. << '{' << this->Join(headerExtensions, ',') << "}\" ";
  1609. if (mocSubDir.empty())
  1610. {
  1611. std::cerr << "in " << absPath << "\n" << std::endl;
  1612. }
  1613. else
  1614. {
  1615. std::cerr << "neither in " << absPath
  1616. << " nor in " << mocSubDir << "\n" << std::endl;
  1617. }
  1618. ::exit(EXIT_FAILURE);
  1619. }
  1620. }
  1621. else
  1622. {
  1623. std::string fileToMoc = absFilename;
  1624. if ((basename != scannedFileBasename) || (requiresMoc==false))
  1625. {
  1626. std::string mocSubDir = extractSubDir(absPath, currentMoc);
  1627. std::string headerToMoc = findMatchingHeader(
  1628. absPath, mocSubDir, basename, headerExtensions);
  1629. if (!headerToMoc.empty())
  1630. {
  1631. // this is for KDE4 compatibility:
  1632. fileToMoc = headerToMoc;
  1633. if ((requiresMoc==false) &&(basename==scannedFileBasename))
  1634. {
  1635. std::cerr << "AUTOGEN: warning: " << absFilename << ": The file "
  1636. "includes the moc file \"" << currentMoc <<
  1637. "\", but does not contain a " << macroName
  1638. << " macro. Running moc on "
  1639. << "\"" << headerToMoc << "\" ! Include \"moc_"
  1640. << basename << ".cpp\" for a compatiblity with "
  1641. "strict mode (see CMAKE_AUTOMOC_RELAXED_MODE).\n"
  1642. << std::endl;
  1643. }
  1644. else
  1645. {
  1646. std::cerr << "AUTOGEN: warning: " << absFilename << ": The file "
  1647. "includes the moc file \"" << currentMoc <<
  1648. "\" instead of \"moc_" << basename << ".cpp\". "
  1649. "Running moc on "
  1650. << "\"" << headerToMoc << "\" ! Include \"moc_"
  1651. << basename << ".cpp\" for compatiblity with "
  1652. "strict mode (see CMAKE_AUTOMOC_RELAXED_MODE).\n"
  1653. << std::endl;
  1654. }
  1655. }
  1656. else
  1657. {
  1658. std::cerr <<"AUTOGEN: error: " << absFilename << ": The file "
  1659. "includes the moc file \"" << currentMoc <<
  1660. "\", which seems to be the moc file from a different "
  1661. "source file. CMake also could not find a matching "
  1662. "header.\n" << std::endl;
  1663. ::exit(EXIT_FAILURE);
  1664. }
  1665. }
  1666. else
  1667. {
  1668. dotMocIncluded = true;
  1669. ownDotMocFile = currentMoc;
  1670. }
  1671. includedMocs[fileToMoc] = currentMoc;
  1672. }
  1673. matchOffset += mocIncludeRegExp.end();
  1674. } while(mocIncludeRegExp.find(contentsString.c_str() + matchOffset));
  1675. }
  1676. // In this case, check whether the scanned file itself contains a Q_OBJECT.
  1677. // If this is the case, the moc_foo.cpp should probably be generated from
  1678. // foo.cpp instead of foo.h, because otherwise it won't build.
  1679. // But warn, since this is not how it is supposed to be used.
  1680. if ((dotMocIncluded == false) && (requiresMoc == true))
  1681. {
  1682. if (mocUnderscoreIncluded == true)
  1683. {
  1684. // this is for KDE4 compatibility:
  1685. std::cerr << "AUTOGEN: warning: " << absFilename << ": The file "
  1686. << "contains a " << macroName << " macro, but does not "
  1687. "include "
  1688. << "\"" << scannedFileBasename << ".moc\", but instead "
  1689. "includes "
  1690. << "\"" << ownMocUnderscoreFile << "\". Running moc on "
  1691. << "\"" << absFilename << "\" ! Better include \""
  1692. << scannedFileBasename << ".moc\" for compatiblity with "
  1693. "strict mode (see CMAKE_AUTOMOC_RELAXED_MODE).\n"
  1694. << std::endl;
  1695. includedMocs[absFilename] = ownMocUnderscoreFile;
  1696. includedMocs.erase(ownMocHeaderFile);
  1697. }
  1698. else
  1699. {
  1700. // otherwise always error out since it will not compile:
  1701. std::cerr << "AUTOGEN: error: " << absFilename << ": The file "
  1702. << "contains a " << macroName << " macro, but does not "
  1703. "include "
  1704. << "\"" << scannedFileBasename << ".moc\" !\n"
  1705. << std::endl;
  1706. ::exit(EXIT_FAILURE);
  1707. }
  1708. }
  1709. }
  1710. void cmQtAutoGenerators::StrictParseCppFile(const std::string& absFilename,
  1711. const std::vector<std::string>& headerExtensions,
  1712. std::map<std::string, std::string>& includedMocs,
  1713. std::map<std::string, std::vector<std::string> >& includedUis)
  1714. {
  1715. cmsys::RegularExpression mocIncludeRegExp(
  1716. "[\n][ \t]*#[ \t]*include[ \t]+"
  1717. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  1718. const std::string contentsString = ReadAll(absFilename);
  1719. if (contentsString.empty())
  1720. {
  1721. std::cerr << "AUTOGEN: warning: " << absFilename << ": file is empty\n"
  1722. << std::endl;
  1723. return;
  1724. }
  1725. this->ParseForUic(absFilename, contentsString, includedUis);
  1726. if (this->MocExecutable.empty())
  1727. {
  1728. return;
  1729. }
  1730. const std::string absPath = cmsys::SystemTools::GetFilenamePath(
  1731. cmsys::SystemTools::GetRealPath(absFilename)) + '/';
  1732. const std::string scannedFileBasename = cmsys::SystemTools::
  1733. GetFilenameWithoutLastExtension(absFilename);
  1734. bool dotMocIncluded = false;
  1735. std::string::size_type matchOffset = 0;
  1736. // first a simple string check for "moc" is *much* faster than the regexp,
  1737. // and if the string search already fails, we don't have to try the
  1738. // expensive regexp
  1739. if ((strstr(contentsString.c_str(), "moc") != NULL)
  1740. && (mocIncludeRegExp.find(contentsString)))
  1741. {
  1742. // for every moc include in the file
  1743. do
  1744. {
  1745. const std::string currentMoc = mocIncludeRegExp.match(1);
  1746. std::string basename = cmsys::SystemTools::
  1747. GetFilenameWithoutLastExtension(currentMoc);
  1748. const bool mocUnderscoreStyle = cmHasLiteralPrefix(basename, "moc_");
  1749. // If the moc include is of the moc_foo.cpp style we expect
  1750. // the Q_OBJECT class declaration in a header file.
  1751. // If the moc include is of the foo.moc style we need to look for
  1752. // a Q_OBJECT macro in the current source file, if it contains the
  1753. // macro we generate the moc file from the source file.
  1754. if (mocUnderscoreStyle)
  1755. {
  1756. // basename should be the part of the moc filename used for
  1757. // finding the correct header, so we need to remove the moc_ part
  1758. basename = basename.substr(4);
  1759. std::string mocSubDir = extractSubDir(absPath, currentMoc);
  1760. std::string headerToMoc = findMatchingHeader(
  1761. absPath, mocSubDir, basename, headerExtensions);
  1762. if (!headerToMoc.empty())
  1763. {
  1764. includedMocs[headerToMoc] = currentMoc;
  1765. }
  1766. else
  1767. {
  1768. std::cerr << "AUTOGEN: error: " << absFilename << " The file "
  1769. << "includes the moc file \"" << currentMoc << "\", "
  1770. << "but could not find header \"" << basename
  1771. << '{' << this->Join(headerExtensions, ',') << "}\" ";
  1772. if (mocSubDir.empty())
  1773. {
  1774. std::cerr << "in " << absPath << "\n" << std::endl;
  1775. }
  1776. else
  1777. {
  1778. std::cerr << "neither in " << absPath
  1779. << " nor in " << mocSubDir << "\n" << std::endl;
  1780. }
  1781. ::exit(EXIT_FAILURE);
  1782. }
  1783. }
  1784. else
  1785. {
  1786. if (basename != scannedFileBasename)
  1787. {
  1788. std::cerr <<"AUTOGEN: error: " << absFilename << ": The file "
  1789. "includes the moc file \"" << currentMoc <<
  1790. "\", which seems to be the moc file from a different "
  1791. "source file. This is not supported. "
  1792. "Include \"" << scannedFileBasename << ".moc\" to run "
  1793. "moc on this source file.\n" << std::endl;
  1794. ::exit(EXIT_FAILURE);
  1795. }
  1796. dotMocIncluded = true;
  1797. includedMocs[absFilename] = currentMoc;
  1798. }
  1799. matchOffset += mocIncludeRegExp.end();
  1800. } while(mocIncludeRegExp.find(contentsString.c_str() + matchOffset));
  1801. }
  1802. // In this case, check whether the scanned file itself contains a Q_OBJECT.
  1803. // If this is the case, the moc_foo.cpp should probably be generated from
  1804. // foo.cpp instead of foo.h, because otherwise it won't build.
  1805. // But warn, since this is not how it is supposed to be used.
  1806. std::string macroName;
  1807. if ((dotMocIncluded == false) && (requiresMocing(contentsString,
  1808. macroName)))
  1809. {
  1810. // otherwise always error out since it will not compile:
  1811. std::cerr << "AUTOGEN: error: " << absFilename << ": The file "
  1812. << "contains a " << macroName << " macro, but does not include "
  1813. << "\"" << scannedFileBasename << ".moc\" !\n"
  1814. << std::endl;
  1815. ::exit(EXIT_FAILURE);
  1816. }
  1817. }
  1818. void cmQtAutoGenerators::ParseForUic(const std::string& absFilename,
  1819. std::map<std::string, std::vector<std::string> >& includedUis)
  1820. {
  1821. if (this->UicExecutable.empty())
  1822. {
  1823. return;
  1824. }
  1825. const std::string contentsString = ReadAll(absFilename);
  1826. if (contentsString.empty())
  1827. {
  1828. std::cerr << "AUTOGEN: warning: " << absFilename << ": file is empty\n"
  1829. << std::endl;
  1830. return;
  1831. }
  1832. this->ParseForUic(absFilename, contentsString, includedUis);
  1833. }
  1834. void cmQtAutoGenerators::ParseForUic(const std::string& absFilename,
  1835. const std::string& contentsString,
  1836. std::map<std::string, std::vector<std::string> >& includedUis)
  1837. {
  1838. if (this->UicExecutable.empty())
  1839. {
  1840. return;
  1841. }
  1842. cmsys::RegularExpression uiIncludeRegExp(
  1843. "[\n][ \t]*#[ \t]*include[ \t]+"
  1844. "[\"<](([^ \">]+/)?ui_[^ \">/]+\\.h)[\">]");
  1845. std::string::size_type matchOffset = 0;
  1846. const std::string realName =
  1847. cmsys::SystemTools::GetRealPath(absFilename);
  1848. matchOffset = 0;
  1849. if ((strstr(contentsString.c_str(), "ui_") != NULL)
  1850. && (uiIncludeRegExp.find(contentsString)))
  1851. {
  1852. do
  1853. {
  1854. const std::string currentUi = uiIncludeRegExp.match(1);
  1855. std::string basename = cmsys::SystemTools::
  1856. GetFilenameWithoutLastExtension(currentUi);
  1857. // basename should be the part of the ui filename used for
  1858. // finding the correct header, so we need to remove the ui_ part
  1859. basename = basename.substr(3);
  1860. includedUis[realName].push_back(basename);
  1861. matchOffset += uiIncludeRegExp.end();
  1862. } while(uiIncludeRegExp.find(contentsString.c_str() + matchOffset));
  1863. }
  1864. }
  1865. void
  1866. cmQtAutoGenerators::SearchHeadersForCppFile(const std::string& absFilename,
  1867. const std::vector<std::string>& headerExtensions,
  1868. std::set<std::string>& absHeaders)
  1869. {
  1870. // search for header files and private header files we may need to moc:
  1871. const std::string basename =
  1872. cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename);
  1873. const std::string absPath = cmsys::SystemTools::GetFilenamePath(
  1874. cmsys::SystemTools::GetRealPath(absFilename)) + '/';
  1875. for(std::vector<std::string>::const_iterator ext = headerExtensions.begin();
  1876. ext != headerExtensions.end();
  1877. ++ext)
  1878. {
  1879. const std::string headerName = absPath + basename + "." + (*ext);
  1880. if (cmsys::SystemTools::FileExists(headerName.c_str()))
  1881. {
  1882. absHeaders.insert(headerName);
  1883. break;
  1884. }
  1885. }
  1886. for(std::vector<std::string>::const_iterator ext = headerExtensions.begin();
  1887. ext != headerExtensions.end();
  1888. ++ext)
  1889. {
  1890. const std::string privateHeaderName = absPath+basename+"_p."+(*ext);
  1891. if (cmsys::SystemTools::FileExists(privateHeaderName.c_str()))
  1892. {
  1893. absHeaders.insert(privateHeaderName);
  1894. break;
  1895. }
  1896. }
  1897. }
  1898. void cmQtAutoGenerators::ParseHeaders(const std::set<std::string>& absHeaders,
  1899. const std::map<std::string, std::string>& includedMocs,
  1900. std::map<std::string, std::string>& notIncludedMocs,
  1901. std::map<std::string, std::vector<std::string> >& includedUis)
  1902. {
  1903. for(std::set<std::string>::const_iterator hIt=absHeaders.begin();
  1904. hIt!=absHeaders.end();
  1905. ++hIt)
  1906. {
  1907. const std::string& headerName = *hIt;
  1908. const std::string contents = ReadAll(headerName);
  1909. if (!this->MocExecutable.empty()
  1910. && includedMocs.find(headerName) == includedMocs.end())
  1911. {
  1912. if (this->Verbose)
  1913. {
  1914. std::cout << "AUTOGEN: Checking " << headerName << std::endl;
  1915. }
  1916. const std::string basename = cmsys::SystemTools::
  1917. GetFilenameWithoutLastExtension(headerName);
  1918. const std::string currentMoc = "moc_" + basename + ".cpp";
  1919. std::string macroName;
  1920. if (requiresMocing(contents, macroName))
  1921. {
  1922. //std::cout << "header contains Q_OBJECT macro";
  1923. notIncludedMocs[headerName] = currentMoc;
  1924. }
  1925. }
  1926. this->ParseForUic(headerName, contents, includedUis);
  1927. }
  1928. }
  1929. bool cmQtAutoGenerators::GenerateMoc(const std::string& sourceFile,
  1930. const std::string& mocFileName)
  1931. {
  1932. const std::string mocFilePath = this->Builddir + mocFileName;
  1933. int sourceNewerThanMoc = 0;
  1934. bool success = cmsys::SystemTools::FileTimeCompare(sourceFile,
  1935. mocFilePath,
  1936. &sourceNewerThanMoc);
  1937. if (this->GenerateAll || !success || sourceNewerThanMoc >= 0)
  1938. {
  1939. // make sure the directory for the resulting moc file exists
  1940. std::string mocDir = mocFilePath.substr(0, mocFilePath.rfind('/'));
  1941. if (!cmsys::SystemTools::FileExists(mocDir.c_str(), false))
  1942. {
  1943. cmsys::SystemTools::MakeDirectory(mocDir.c_str());
  1944. }
  1945. std::string msg = "Generating ";
  1946. msg += mocFileName;
  1947. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue
  1948. |cmsysTerminal_Color_ForegroundBold,
  1949. msg.c_str(), true, this->ColorOutput);
  1950. std::vector<std::string> command;
  1951. command.push_back(this->MocExecutable);
  1952. command.insert(command.end(),
  1953. this->MocIncludes.begin(), this->MocIncludes.end());
  1954. command.insert(command.end(),
  1955. this->MocDefinitions.begin(), this->MocDefinitions.end());
  1956. command.insert(command.end(),
  1957. this->MocOptions.begin(), this->MocOptions.end());
  1958. #ifdef _WIN32
  1959. command.push_back("-DWIN32");
  1960. #endif
  1961. command.push_back("-o");
  1962. command.push_back(mocFilePath);
  1963. command.push_back(sourceFile);
  1964. if (this->Verbose)
  1965. {
  1966. for(std::vector<std::string>::const_iterator cmdIt = command.begin();
  1967. cmdIt != command.end();
  1968. ++cmdIt)
  1969. {
  1970. std::cout << *cmdIt << " ";
  1971. }
  1972. std::cout << std::endl;
  1973. }
  1974. std::string output;
  1975. int retVal = 0;
  1976. bool result = cmSystemTools::RunSingleCommand(command, &output, &output,
  1977. &retVal);
  1978. if (!result || retVal)
  1979. {
  1980. std::cerr << "AUTOGEN: error: process for " << mocFilePath <<" failed:\n"
  1981. << output << std::endl;
  1982. this->RunMocFailed = true;
  1983. cmSystemTools::RemoveFile(mocFilePath);
  1984. }
  1985. return true;
  1986. }
  1987. return false;
  1988. }
  1989. bool cmQtAutoGenerators::GenerateUi(const std::string& realName,
  1990. const std::string& uiFileName)
  1991. {
  1992. if (!cmsys::SystemTools::FileExists(this->Builddir.c_str(), false))
  1993. {
  1994. cmsys::SystemTools::MakeDirectory(this->Builddir.c_str());
  1995. }
  1996. const std::string path = cmsys::SystemTools::GetFilenamePath(
  1997. realName) + '/';
  1998. std::string ui_output_file = "ui_" + uiFileName + ".h";
  1999. std::string ui_input_file = path + uiFileName + ".ui";
  2000. int sourceNewerThanUi = 0;
  2001. bool success = cmsys::SystemTools::FileTimeCompare(ui_input_file,
  2002. this->Builddir + ui_output_file,
  2003. &sourceNewerThanUi);
  2004. if (this->GenerateAll || !success || sourceNewerThanUi >= 0)
  2005. {
  2006. std::string msg = "Generating ";
  2007. msg += ui_output_file;
  2008. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue
  2009. |cmsysTerminal_Color_ForegroundBold,
  2010. msg.c_str(), true, this->ColorOutput);
  2011. std::vector<std::string> command;
  2012. command.push_back(this->UicExecutable);
  2013. std::vector<std::string> opts = this->UicTargetOptions;
  2014. std::map<std::string, std::string>::const_iterator optionIt
  2015. = this->UicOptions.find(ui_input_file);
  2016. if (optionIt != this->UicOptions.end())
  2017. {
  2018. std::vector<std::string> fileOpts;
  2019. cmSystemTools::ExpandListArgument(optionIt->second, fileOpts);
  2020. this->MergeUicOptions(opts, fileOpts, this->QtMajorVersion == "5");
  2021. }
  2022. command.insert(command.end(), opts.begin(), opts.end());
  2023. command.push_back("-o");
  2024. command.push_back(this->Builddir + ui_output_file);
  2025. command.push_back(ui_input_file);
  2026. if (this->Verbose)
  2027. {
  2028. for(std::vector<std::string>::const_iterator cmdIt = command.begin();
  2029. cmdIt != command.end();
  2030. ++cmdIt)
  2031. {
  2032. std::cout << *cmdIt << " ";
  2033. }
  2034. std::cout << std::endl;
  2035. }
  2036. std::string output;
  2037. int retVal = 0;
  2038. bool result = cmSystemTools::RunSingleCommand(command, &output, &output,
  2039. &retVal);
  2040. if (!result || retVal)
  2041. {
  2042. std::cerr << "AUTOUIC: error: process for " << ui_output_file <<
  2043. " needed by\n \"" << realName << "\"\nfailed:\n" << output
  2044. << std::endl;
  2045. this->RunUicFailed = true;
  2046. cmSystemTools::RemoveFile(ui_output_file);
  2047. return false;
  2048. }
  2049. return true;
  2050. }
  2051. return false;
  2052. }
  2053. bool cmQtAutoGenerators::InputFilesNewerThanQrc(const std::string& qrcFile,
  2054. const std::string& rccOutput)
  2055. {
  2056. std::vector<std::string> const& files = this->RccInputs[qrcFile];
  2057. for (std::vector<std::string>::const_iterator it = files.begin();
  2058. it != files.end(); ++it)
  2059. {
  2060. int inputNewerThanQrc = 0;
  2061. bool success = cmsys::SystemTools::FileTimeCompare(*it,
  2062. rccOutput,
  2063. &inputNewerThanQrc);
  2064. if (!success || inputNewerThanQrc >= 0)
  2065. {
  2066. return true;
  2067. }
  2068. }
  2069. return false;
  2070. }
  2071. bool cmQtAutoGenerators::GenerateQrc()
  2072. {
  2073. for(std::vector<std::string>::const_iterator si = this->RccSources.begin();
  2074. si != this->RccSources.end(); ++si)
  2075. {
  2076. std::string ext = cmsys::SystemTools::GetFilenameLastExtension(*si);
  2077. if (ext != ".qrc")
  2078. {
  2079. continue;
  2080. }
  2081. std::vector<std::string> command;
  2082. command.push_back(this->RccExecutable);
  2083. std::string basename = cmsys::SystemTools::
  2084. GetFilenameWithoutLastExtension(*si);
  2085. std::string rcc_output_file = this->Builddir
  2086. + "CMakeFiles/" + this->OriginTargetName
  2087. + ".dir/qrc_" + basename + ".cpp";
  2088. int sourceNewerThanQrc = 0;
  2089. bool generateQrc = !cmsys::SystemTools::FileTimeCompare(*si,
  2090. rcc_output_file,
  2091. &sourceNewerThanQrc);
  2092. generateQrc = generateQrc || (sourceNewerThanQrc >= 0);
  2093. generateQrc = generateQrc || this->InputFilesNewerThanQrc(*si,
  2094. rcc_output_file);
  2095. if (this->GenerateAll || generateQrc)
  2096. {
  2097. std::map<std::string, std::string>::const_iterator optionIt
  2098. = this->RccOptions.find(*si);
  2099. if (optionIt != this->RccOptions.end())
  2100. {
  2101. cmSystemTools::ExpandListArgument(optionIt->second, command);
  2102. }
  2103. command.push_back("-name");
  2104. command.push_back(basename);
  2105. command.push_back("-o");
  2106. command.push_back(rcc_output_file);
  2107. command.push_back(*si);
  2108. if (this->Verbose)
  2109. {
  2110. for(std::vector<std::string>::const_iterator cmdIt = command.begin();
  2111. cmdIt != command.end();
  2112. ++cmdIt)
  2113. {
  2114. std::cout << *cmdIt << " ";
  2115. }
  2116. std::cout << std::endl;
  2117. }
  2118. std::string output;
  2119. int retVal = 0;
  2120. bool result = cmSystemTools::RunSingleCommand(command, &output, &output,
  2121. &retVal);
  2122. if (!result || retVal)
  2123. {
  2124. std::cerr << "AUTORCC: error: process for " << rcc_output_file <<
  2125. " failed:\n" << output << std::endl;
  2126. this->RunRccFailed = true;
  2127. cmSystemTools::RemoveFile(rcc_output_file);
  2128. return false;
  2129. }
  2130. }
  2131. }
  2132. return true;
  2133. }
  2134. std::string cmQtAutoGenerators::Join(const std::vector<std::string>& lst,
  2135. char separator)
  2136. {
  2137. if (lst.empty())
  2138. {
  2139. return "";
  2140. }
  2141. std::string result;
  2142. for (std::vector<std::string>::const_iterator it = lst.begin();
  2143. it != lst.end();
  2144. ++it)
  2145. {
  2146. result += "." + (*it) + separator;
  2147. }
  2148. result.erase(result.end() - 1);
  2149. return result;
  2150. }
  2151. bool cmQtAutoGenerators::StartsWith(const std::string& str,
  2152. const std::string& with)
  2153. {
  2154. return (str.substr(0, with.length()) == with);
  2155. }
  2156. bool cmQtAutoGenerators::EndsWith(const std::string& str,
  2157. const std::string& with)
  2158. {
  2159. if (with.length() > (str.length()))
  2160. {
  2161. return false;
  2162. }
  2163. return (str.substr(str.length() - with.length(), with.length()) == with);
  2164. }