cmQtAutoGenerators.cxx 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410
  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 "cmGlobalVisualStudioGenerator.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. cmGlobalVisualStudioGenerator* vsgg =
  323. static_cast<cmGlobalVisualStudioGenerator*>(gg);
  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 = vsgg->GetVersion() >= cmGlobalVisualStudioGenerator::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. bool cmQtAutoGenerators::Run(const std::string& targetDirectory,
  1077. const std::string& config)
  1078. {
  1079. bool success = true;
  1080. cmake cm;
  1081. cm.SetHomeOutputDirectory(targetDirectory);
  1082. cm.SetHomeDirectory(targetDirectory);
  1083. cmGlobalGenerator gg;
  1084. gg.SetCMakeInstance(&cm);
  1085. cmLocalGenerator* lg = gg.MakeLocalGenerator();
  1086. lg->GetMakefile()->SetCurrentBinaryDirectory(targetDirectory);
  1087. lg->GetMakefile()->SetCurrentSourceDirectory(targetDirectory);
  1088. gg.SetCurrentLocalGenerator(lg);
  1089. this->ReadAutogenInfoFile(lg->GetMakefile(), targetDirectory, config);
  1090. this->ReadOldMocDefinitionsFile(lg->GetMakefile(), targetDirectory);
  1091. this->Init();
  1092. if (this->QtMajorVersion == "4" || this->QtMajorVersion == "5")
  1093. {
  1094. success = this->RunAutogen(lg->GetMakefile());
  1095. }
  1096. this->WriteOldMocDefinitionsFile(targetDirectory);
  1097. delete lg;
  1098. return success;
  1099. }
  1100. bool cmQtAutoGenerators::ReadAutogenInfoFile(cmMakefile* makefile,
  1101. const std::string& targetDirectory,
  1102. const std::string& config)
  1103. {
  1104. std::string filename(
  1105. cmSystemTools::CollapseFullPath(targetDirectory));
  1106. cmSystemTools::ConvertToUnixSlashes(filename);
  1107. filename += "/AutogenInfo.cmake";
  1108. if (!makefile->ReadListFile(filename.c_str()))
  1109. {
  1110. cmSystemTools::Error("Error processing file: ", filename.c_str());
  1111. return false;
  1112. }
  1113. this->QtMajorVersion = makefile->GetSafeDefinition("AM_QT_VERSION_MAJOR");
  1114. if (this->QtMajorVersion == "")
  1115. {
  1116. this->QtMajorVersion = makefile->GetSafeDefinition(
  1117. "AM_Qt5Core_VERSION_MAJOR");
  1118. }
  1119. this->Sources = makefile->GetSafeDefinition("AM_SOURCES");
  1120. {
  1121. std::string rccSources = makefile->GetSafeDefinition("AM_RCC_SOURCES");
  1122. cmSystemTools::ExpandListArgument(rccSources, this->RccSources);
  1123. }
  1124. this->SkipMoc = makefile->GetSafeDefinition("AM_SKIP_MOC");
  1125. this->SkipUic = makefile->GetSafeDefinition("AM_SKIP_UIC");
  1126. this->Headers = makefile->GetSafeDefinition("AM_HEADERS");
  1127. this->IncludeProjectDirsBefore = makefile->IsOn(
  1128. "AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE");
  1129. this->Srcdir = makefile->GetSafeDefinition("AM_CMAKE_CURRENT_SOURCE_DIR");
  1130. this->Builddir = makefile->GetSafeDefinition("AM_CMAKE_CURRENT_BINARY_DIR");
  1131. this->MocExecutable = makefile->GetSafeDefinition("AM_QT_MOC_EXECUTABLE");
  1132. this->UicExecutable = makefile->GetSafeDefinition("AM_QT_UIC_EXECUTABLE");
  1133. this->RccExecutable = makefile->GetSafeDefinition("AM_QT_RCC_EXECUTABLE");
  1134. {
  1135. std::string compileDefsPropOrig = "AM_MOC_COMPILE_DEFINITIONS";
  1136. std::string compileDefsProp = compileDefsPropOrig;
  1137. if(!config.empty())
  1138. {
  1139. compileDefsProp += "_";
  1140. compileDefsProp += config;
  1141. }
  1142. const char *compileDefs = makefile->GetDefinition(compileDefsProp);
  1143. this->MocCompileDefinitionsStr = compileDefs ? compileDefs
  1144. : makefile->GetSafeDefinition(compileDefsPropOrig);
  1145. }
  1146. {
  1147. std::string includesPropOrig = "AM_MOC_INCLUDES";
  1148. std::string includesProp = includesPropOrig;
  1149. if(!config.empty())
  1150. {
  1151. includesProp += "_";
  1152. includesProp += config;
  1153. }
  1154. const char *includes = makefile->GetDefinition(includesProp);
  1155. this->MocIncludesStr = includes ? includes
  1156. : makefile->GetSafeDefinition(includesPropOrig);
  1157. }
  1158. this->MocOptionsStr = makefile->GetSafeDefinition("AM_MOC_OPTIONS");
  1159. this->ProjectBinaryDir = makefile->GetSafeDefinition("AM_CMAKE_BINARY_DIR");
  1160. this->ProjectSourceDir = makefile->GetSafeDefinition("AM_CMAKE_SOURCE_DIR");
  1161. this->TargetName = makefile->GetSafeDefinition("AM_TARGET_NAME");
  1162. this->OriginTargetName
  1163. = makefile->GetSafeDefinition("AM_ORIGIN_TARGET_NAME");
  1164. {
  1165. const char *uicOptionsFiles
  1166. = makefile->GetSafeDefinition("AM_UIC_OPTIONS_FILES");
  1167. std::string uicOptionsPropOrig = "AM_UIC_TARGET_OPTIONS";
  1168. std::string uicOptionsProp = uicOptionsPropOrig;
  1169. if(!config.empty())
  1170. {
  1171. uicOptionsProp += "_";
  1172. uicOptionsProp += config;
  1173. }
  1174. const char *uicTargetOptions
  1175. = makefile->GetSafeDefinition(uicOptionsProp);
  1176. cmSystemTools::ExpandListArgument(
  1177. uicTargetOptions ? uicTargetOptions
  1178. : makefile->GetSafeDefinition(uicOptionsPropOrig),
  1179. this->UicTargetOptions);
  1180. const char *uicOptionsOptions
  1181. = makefile->GetSafeDefinition("AM_UIC_OPTIONS_OPTIONS");
  1182. std::vector<std::string> uicFilesVec;
  1183. cmSystemTools::ExpandListArgument(uicOptionsFiles, uicFilesVec);
  1184. std::vector<std::string> uicOptionsVec;
  1185. cmSystemTools::ExpandListArgument(uicOptionsOptions, uicOptionsVec);
  1186. if (uicFilesVec.size() != uicOptionsVec.size())
  1187. {
  1188. return false;
  1189. }
  1190. for (std::vector<std::string>::iterator fileIt = uicFilesVec.begin(),
  1191. optionIt = uicOptionsVec.begin();
  1192. fileIt != uicFilesVec.end();
  1193. ++fileIt, ++optionIt)
  1194. {
  1195. cmSystemTools::ReplaceString(*optionIt, "@list_sep@", ";");
  1196. this->UicOptions[*fileIt] = *optionIt;
  1197. }
  1198. }
  1199. {
  1200. const char *rccOptionsFiles
  1201. = makefile->GetSafeDefinition("AM_RCC_OPTIONS_FILES");
  1202. const char *rccOptionsOptions
  1203. = makefile->GetSafeDefinition("AM_RCC_OPTIONS_OPTIONS");
  1204. std::vector<std::string> rccFilesVec;
  1205. cmSystemTools::ExpandListArgument(rccOptionsFiles, rccFilesVec);
  1206. std::vector<std::string> rccOptionsVec;
  1207. cmSystemTools::ExpandListArgument(rccOptionsOptions, rccOptionsVec);
  1208. if (rccFilesVec.size() != rccOptionsVec.size())
  1209. {
  1210. return false;
  1211. }
  1212. for (std::vector<std::string>::iterator fileIt = rccFilesVec.begin(),
  1213. optionIt = rccOptionsVec.begin();
  1214. fileIt != rccFilesVec.end();
  1215. ++fileIt, ++optionIt)
  1216. {
  1217. cmSystemTools::ReplaceString(*optionIt, "@list_sep@", ";");
  1218. this->RccOptions[*fileIt] = *optionIt;
  1219. }
  1220. const char *rccInputs = makefile->GetSafeDefinition("AM_RCC_INPUTS");
  1221. std::vector<std::string> rccInputLists;
  1222. cmSystemTools::ExpandListArgument(rccInputs, rccInputLists);
  1223. if (this->RccSources.size() != rccInputLists.size())
  1224. {
  1225. cmSystemTools::Error("Error processing file: ", filename.c_str());
  1226. return false;
  1227. }
  1228. for (std::vector<std::string>::iterator fileIt = this->RccSources.begin(),
  1229. inputIt = rccInputLists.begin();
  1230. fileIt != this->RccSources.end();
  1231. ++fileIt, ++inputIt)
  1232. {
  1233. cmSystemTools::ReplaceString(*inputIt, "@list_sep@", ";");
  1234. std::vector<std::string> rccInputFiles;
  1235. cmSystemTools::ExpandListArgument(*inputIt, rccInputFiles);
  1236. this->RccInputs[*fileIt] = rccInputFiles;
  1237. }
  1238. }
  1239. this->CurrentCompileSettingsStr = this->MakeCompileSettingsString(makefile);
  1240. this->RelaxedMode = makefile->IsOn("AM_RELAXED_MODE");
  1241. return true;
  1242. }
  1243. std::string cmQtAutoGenerators::MakeCompileSettingsString(cmMakefile* makefile)
  1244. {
  1245. std::string s;
  1246. s += makefile->GetSafeDefinition("AM_MOC_COMPILE_DEFINITIONS");
  1247. s += " ~~~ ";
  1248. s += makefile->GetSafeDefinition("AM_MOC_INCLUDES");
  1249. s += " ~~~ ";
  1250. s += makefile->GetSafeDefinition("AM_MOC_OPTIONS");
  1251. s += " ~~~ ";
  1252. s += makefile->IsOn("AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE") ? "TRUE"
  1253. : "FALSE";
  1254. s += " ~~~ ";
  1255. return s;
  1256. }
  1257. bool cmQtAutoGenerators::ReadOldMocDefinitionsFile(cmMakefile* makefile,
  1258. const std::string& targetDirectory)
  1259. {
  1260. std::string filename(
  1261. cmSystemTools::CollapseFullPath(targetDirectory));
  1262. cmSystemTools::ConvertToUnixSlashes(filename);
  1263. filename += "/AutomocOldMocDefinitions.cmake";
  1264. if (makefile->ReadListFile(filename.c_str()))
  1265. {
  1266. this->OldCompileSettingsStr =
  1267. makefile->GetSafeDefinition("AM_OLD_COMPILE_SETTINGS");
  1268. }
  1269. return true;
  1270. }
  1271. void
  1272. cmQtAutoGenerators::WriteOldMocDefinitionsFile(
  1273. const std::string& targetDirectory)
  1274. {
  1275. std::string filename(
  1276. cmSystemTools::CollapseFullPath(targetDirectory));
  1277. cmSystemTools::ConvertToUnixSlashes(filename);
  1278. filename += "/AutomocOldMocDefinitions.cmake";
  1279. cmsys::ofstream outfile;
  1280. outfile.open(filename.c_str(),
  1281. std::ios::trunc);
  1282. outfile << "set(AM_OLD_COMPILE_SETTINGS "
  1283. << cmLocalGenerator::EscapeForCMake(
  1284. this->CurrentCompileSettingsStr) << ")\n";
  1285. outfile.close();
  1286. }
  1287. void cmQtAutoGenerators::Init()
  1288. {
  1289. this->OutMocCppFilename = this->Builddir;
  1290. this->OutMocCppFilename += this->TargetName;
  1291. this->OutMocCppFilename += ".cpp";
  1292. std::vector<std::string> cdefList;
  1293. cmSystemTools::ExpandListArgument(this->MocCompileDefinitionsStr, cdefList);
  1294. for(std::vector<std::string>::const_iterator it = cdefList.begin();
  1295. it != cdefList.end();
  1296. ++it)
  1297. {
  1298. this->MocDefinitions.push_back("-D" + (*it));
  1299. }
  1300. cmSystemTools::ExpandListArgument(this->MocOptionsStr, this->MocOptions);
  1301. std::vector<std::string> incPaths;
  1302. cmSystemTools::ExpandListArgument(this->MocIncludesStr, incPaths);
  1303. std::set<std::string> frameworkPaths;
  1304. for(std::vector<std::string>::const_iterator it = incPaths.begin();
  1305. it != incPaths.end();
  1306. ++it)
  1307. {
  1308. const std::string &path = *it;
  1309. this->MocIncludes.push_back("-I" + path);
  1310. if (cmHasLiteralSuffix(path, ".framework/Headers"))
  1311. {
  1312. // Go up twice to get to the framework root
  1313. std::vector<std::string> pathComponents;
  1314. cmsys::SystemTools::SplitPath(path, pathComponents);
  1315. std::string frameworkPath =cmsys::SystemTools::JoinPath(
  1316. pathComponents.begin(), pathComponents.end() - 2);
  1317. frameworkPaths.insert(frameworkPath);
  1318. }
  1319. }
  1320. for (std::set<std::string>::const_iterator it = frameworkPaths.begin();
  1321. it != frameworkPaths.end(); ++it)
  1322. {
  1323. this->MocIncludes.push_back("-F");
  1324. this->MocIncludes.push_back(*it);
  1325. }
  1326. if (this->IncludeProjectDirsBefore)
  1327. {
  1328. const std::string binDir = "-I" + this->ProjectBinaryDir;
  1329. const std::string srcDir = "-I" + this->ProjectSourceDir;
  1330. std::list<std::string> sortedMocIncludes;
  1331. std::list<std::string>::iterator it = this->MocIncludes.begin();
  1332. while (it != this->MocIncludes.end())
  1333. {
  1334. if (this->StartsWith(*it, binDir))
  1335. {
  1336. sortedMocIncludes.push_back(*it);
  1337. it = this->MocIncludes.erase(it);
  1338. }
  1339. else
  1340. {
  1341. ++it;
  1342. }
  1343. }
  1344. it = this->MocIncludes.begin();
  1345. while (it != this->MocIncludes.end())
  1346. {
  1347. if (this->StartsWith(*it, srcDir))
  1348. {
  1349. sortedMocIncludes.push_back(*it);
  1350. it = this->MocIncludes.erase(it);
  1351. }
  1352. else
  1353. {
  1354. ++it;
  1355. }
  1356. }
  1357. sortedMocIncludes.insert(sortedMocIncludes.end(),
  1358. this->MocIncludes.begin(), this->MocIncludes.end());
  1359. this->MocIncludes = sortedMocIncludes;
  1360. }
  1361. }
  1362. bool cmQtAutoGenerators::RunAutogen(cmMakefile* makefile)
  1363. {
  1364. if (!cmsys::SystemTools::FileExists(this->OutMocCppFilename.c_str())
  1365. || (this->OldCompileSettingsStr != this->CurrentCompileSettingsStr))
  1366. {
  1367. this->GenerateAll = true;
  1368. }
  1369. // the program goes through all .cpp files to see which moc files are
  1370. // included. It is not really interesting how the moc file is named, but
  1371. // what file the moc is created from. Once a moc is included the same moc
  1372. // may not be included in the _automoc.cpp file anymore. OTOH if there's a
  1373. // header containing Q_OBJECT where no corresponding moc file is included
  1374. // anywhere a moc_<filename>.cpp file is created and included in
  1375. // the _automoc.cpp file.
  1376. // key = moc source filepath, value = moc output filepath
  1377. std::map<std::string, std::string> includedMocs;
  1378. // collect all headers which may need to be mocced
  1379. std::set<std::string> headerFiles;
  1380. std::vector<std::string> sourceFiles;
  1381. cmSystemTools::ExpandListArgument(this->Sources, sourceFiles);
  1382. const std::vector<std::string>& headerExtensions =
  1383. makefile->GetHeaderExtensions();
  1384. std::map<std::string, std::vector<std::string> > includedUis;
  1385. std::map<std::string, std::vector<std::string> > skippedUis;
  1386. std::vector<std::string> uicSkipped;
  1387. cmSystemTools::ExpandListArgument(this->SkipUic, uicSkipped);
  1388. for (std::vector<std::string>::const_iterator it = sourceFiles.begin();
  1389. it != sourceFiles.end();
  1390. ++it)
  1391. {
  1392. const bool skipUic = std::find(uicSkipped.begin(), uicSkipped.end(), *it)
  1393. != uicSkipped.end();
  1394. std::map<std::string, std::vector<std::string> >& uiFiles
  1395. = skipUic ? skippedUis : includedUis;
  1396. const std::string &absFilename = *it;
  1397. if (this->Verbose)
  1398. {
  1399. std::cout << "AUTOGEN: Checking " << absFilename << std::endl;
  1400. }
  1401. if (this->RelaxedMode)
  1402. {
  1403. this->ParseCppFile(absFilename, headerExtensions, includedMocs,
  1404. uiFiles);
  1405. }
  1406. else
  1407. {
  1408. this->StrictParseCppFile(absFilename, headerExtensions, includedMocs,
  1409. uiFiles);
  1410. }
  1411. this->SearchHeadersForCppFile(absFilename, headerExtensions, headerFiles);
  1412. }
  1413. {
  1414. std::vector<std::string> mocSkipped;
  1415. cmSystemTools::ExpandListArgument(this->SkipMoc, mocSkipped);
  1416. for (std::vector<std::string>::const_iterator it = mocSkipped.begin();
  1417. it != mocSkipped.end();
  1418. ++it)
  1419. {
  1420. if (std::find(uicSkipped.begin(), uicSkipped.end(), *it)
  1421. != uicSkipped.end())
  1422. {
  1423. const std::string &absFilename = *it;
  1424. if (this->Verbose)
  1425. {
  1426. std::cout << "AUTOGEN: Checking " << absFilename << std::endl;
  1427. }
  1428. this->ParseForUic(absFilename, includedUis);
  1429. }
  1430. }
  1431. }
  1432. std::vector<std::string> headerFilesVec;
  1433. cmSystemTools::ExpandListArgument(this->Headers, headerFilesVec);
  1434. headerFiles.insert(headerFilesVec.begin(), headerFilesVec.end());
  1435. // key = moc source filepath, value = moc output filename
  1436. std::map<std::string, std::string> notIncludedMocs;
  1437. this->ParseHeaders(headerFiles, includedMocs, notIncludedMocs, includedUis);
  1438. // run moc on all the moc's that are #included in source files
  1439. for(std::map<std::string, std::string>::const_iterator
  1440. it = includedMocs.begin();
  1441. it != includedMocs.end();
  1442. ++it)
  1443. {
  1444. this->GenerateMoc(it->first, it->second);
  1445. }
  1446. for(std::map<std::string, std::vector<std::string> >::const_iterator
  1447. it = includedUis.begin();
  1448. it != includedUis.end();
  1449. ++it)
  1450. {
  1451. for (std::vector<std::string>::const_iterator nit = it->second.begin();
  1452. nit != it->second.end();
  1453. ++nit)
  1454. {
  1455. this->GenerateUi(it->first, *nit);
  1456. }
  1457. }
  1458. if(!this->RccExecutable.empty())
  1459. {
  1460. this->GenerateQrc();
  1461. }
  1462. cmsys_ios::stringstream outStream;
  1463. outStream << "/* This file is autogenerated, do not edit*/\n";
  1464. bool automocCppChanged = false;
  1465. if (notIncludedMocs.empty())
  1466. {
  1467. outStream << "enum some_compilers { need_more_than_nothing };\n";
  1468. }
  1469. else
  1470. {
  1471. // run moc on the remaining headers and include them in
  1472. // the _automoc.cpp file
  1473. for(std::map<std::string, std::string>::const_iterator
  1474. it = notIncludedMocs.begin();
  1475. it != notIncludedMocs.end();
  1476. ++it)
  1477. {
  1478. bool mocSuccess = this->GenerateMoc(it->first, it->second);
  1479. if (mocSuccess)
  1480. {
  1481. automocCppChanged = true;
  1482. }
  1483. outStream << "#include \"" << it->second << "\"\n";
  1484. }
  1485. }
  1486. if (this->RunMocFailed)
  1487. {
  1488. std::cerr << "moc failed..." << std::endl;
  1489. return false;
  1490. }
  1491. if (this->RunUicFailed)
  1492. {
  1493. std::cerr << "uic failed..." << std::endl;
  1494. return false;
  1495. }
  1496. if (this->RunRccFailed)
  1497. {
  1498. std::cerr << "rcc failed..." << std::endl;
  1499. return false;
  1500. }
  1501. outStream.flush();
  1502. std::string automocSource = outStream.str();
  1503. if (!automocCppChanged)
  1504. {
  1505. // compare contents of the _automoc.cpp file
  1506. const std::string oldContents = ReadAll(this->OutMocCppFilename);
  1507. if (oldContents == automocSource)
  1508. {
  1509. // nothing changed: don't touch the _automoc.cpp file
  1510. return true;
  1511. }
  1512. }
  1513. // source file that includes all remaining moc files (_automoc.cpp file)
  1514. cmsys::ofstream outfile;
  1515. outfile.open(this->OutMocCppFilename.c_str(),
  1516. std::ios::trunc);
  1517. outfile << automocSource;
  1518. outfile.close();
  1519. return true;
  1520. }
  1521. void cmQtAutoGenerators::ParseCppFile(const std::string& absFilename,
  1522. const std::vector<std::string>& headerExtensions,
  1523. std::map<std::string, std::string>& includedMocs,
  1524. std::map<std::string, std::vector<std::string> > &includedUis)
  1525. {
  1526. cmsys::RegularExpression mocIncludeRegExp(
  1527. "[\n][ \t]*#[ \t]*include[ \t]+"
  1528. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  1529. const std::string contentsString = ReadAll(absFilename);
  1530. if (contentsString.empty())
  1531. {
  1532. std::cerr << "AUTOGEN: warning: " << absFilename << ": file is empty\n"
  1533. << std::endl;
  1534. return;
  1535. }
  1536. this->ParseForUic(absFilename, contentsString, includedUis);
  1537. if (this->MocExecutable.empty())
  1538. {
  1539. return;
  1540. }
  1541. const std::string absPath = cmsys::SystemTools::GetFilenamePath(
  1542. cmsys::SystemTools::GetRealPath(absFilename)) + '/';
  1543. const std::string scannedFileBasename = cmsys::SystemTools::
  1544. GetFilenameWithoutLastExtension(absFilename);
  1545. std::string macroName;
  1546. const bool requiresMoc = requiresMocing(contentsString, macroName);
  1547. bool dotMocIncluded = false;
  1548. bool mocUnderscoreIncluded = false;
  1549. std::string ownMocUnderscoreFile;
  1550. std::string ownDotMocFile;
  1551. std::string ownMocHeaderFile;
  1552. std::string::size_type matchOffset = 0;
  1553. // first a simple string check for "moc" is *much* faster than the regexp,
  1554. // and if the string search already fails, we don't have to try the
  1555. // expensive regexp
  1556. if ((strstr(contentsString.c_str(), "moc") != NULL)
  1557. && (mocIncludeRegExp.find(contentsString)))
  1558. {
  1559. // for every moc include in the file
  1560. do
  1561. {
  1562. const std::string currentMoc = mocIncludeRegExp.match(1);
  1563. //std::cout << "found moc include: " << currentMoc << std::endl;
  1564. std::string basename = cmsys::SystemTools::
  1565. GetFilenameWithoutLastExtension(currentMoc);
  1566. const bool moc_style = cmHasLiteralPrefix(basename, "moc_");
  1567. // If the moc include is of the moc_foo.cpp style we expect
  1568. // the Q_OBJECT class declaration in a header file.
  1569. // If the moc include is of the foo.moc style we need to look for
  1570. // a Q_OBJECT macro in the current source file, if it contains the
  1571. // macro we generate the moc file from the source file.
  1572. // Q_OBJECT
  1573. if (moc_style)
  1574. {
  1575. // basename should be the part of the moc filename used for
  1576. // finding the correct header, so we need to remove the moc_ part
  1577. basename = basename.substr(4);
  1578. std::string mocSubDir = extractSubDir(absPath, currentMoc);
  1579. std::string headerToMoc = findMatchingHeader(
  1580. absPath, mocSubDir, basename, headerExtensions);
  1581. if (!headerToMoc.empty())
  1582. {
  1583. includedMocs[headerToMoc] = currentMoc;
  1584. if (basename == scannedFileBasename)
  1585. {
  1586. mocUnderscoreIncluded = true;
  1587. ownMocUnderscoreFile = currentMoc;
  1588. ownMocHeaderFile = headerToMoc;
  1589. }
  1590. }
  1591. else
  1592. {
  1593. std::cerr << "AUTOGEN: error: " << absFilename << ": The file "
  1594. << "includes the moc file \"" << currentMoc << "\", "
  1595. << "but could not find header \"" << basename
  1596. << '{' << this->Join(headerExtensions, ',') << "}\" ";
  1597. if (mocSubDir.empty())
  1598. {
  1599. std::cerr << "in " << absPath << "\n" << std::endl;
  1600. }
  1601. else
  1602. {
  1603. std::cerr << "neither in " << absPath
  1604. << " nor in " << mocSubDir << "\n" << std::endl;
  1605. }
  1606. ::exit(EXIT_FAILURE);
  1607. }
  1608. }
  1609. else
  1610. {
  1611. std::string fileToMoc = absFilename;
  1612. if ((basename != scannedFileBasename) || (requiresMoc==false))
  1613. {
  1614. std::string mocSubDir = extractSubDir(absPath, currentMoc);
  1615. std::string headerToMoc = findMatchingHeader(
  1616. absPath, mocSubDir, basename, headerExtensions);
  1617. if (!headerToMoc.empty())
  1618. {
  1619. // this is for KDE4 compatibility:
  1620. fileToMoc = headerToMoc;
  1621. if ((requiresMoc==false) &&(basename==scannedFileBasename))
  1622. {
  1623. std::cerr << "AUTOGEN: warning: " << absFilename << ": The file "
  1624. "includes the moc file \"" << currentMoc <<
  1625. "\", but does not contain a " << macroName
  1626. << " macro. Running moc on "
  1627. << "\"" << headerToMoc << "\" ! Include \"moc_"
  1628. << basename << ".cpp\" for a compatiblity with "
  1629. "strict mode (see CMAKE_AUTOMOC_RELAXED_MODE).\n"
  1630. << std::endl;
  1631. }
  1632. else
  1633. {
  1634. std::cerr << "AUTOGEN: warning: " << absFilename << ": The file "
  1635. "includes the moc file \"" << currentMoc <<
  1636. "\" instead of \"moc_" << basename << ".cpp\". "
  1637. "Running moc on "
  1638. << "\"" << headerToMoc << "\" ! Include \"moc_"
  1639. << basename << ".cpp\" for compatiblity with "
  1640. "strict mode (see CMAKE_AUTOMOC_RELAXED_MODE).\n"
  1641. << std::endl;
  1642. }
  1643. }
  1644. else
  1645. {
  1646. std::cerr <<"AUTOGEN: error: " << absFilename << ": The file "
  1647. "includes the moc file \"" << currentMoc <<
  1648. "\", which seems to be the moc file from a different "
  1649. "source file. CMake also could not find a matching "
  1650. "header.\n" << std::endl;
  1651. ::exit(EXIT_FAILURE);
  1652. }
  1653. }
  1654. else
  1655. {
  1656. dotMocIncluded = true;
  1657. ownDotMocFile = currentMoc;
  1658. }
  1659. includedMocs[fileToMoc] = currentMoc;
  1660. }
  1661. matchOffset += mocIncludeRegExp.end();
  1662. } while(mocIncludeRegExp.find(contentsString.c_str() + matchOffset));
  1663. }
  1664. // In this case, check whether the scanned file itself contains a Q_OBJECT.
  1665. // If this is the case, the moc_foo.cpp should probably be generated from
  1666. // foo.cpp instead of foo.h, because otherwise it won't build.
  1667. // But warn, since this is not how it is supposed to be used.
  1668. if ((dotMocIncluded == false) && (requiresMoc == true))
  1669. {
  1670. if (mocUnderscoreIncluded == true)
  1671. {
  1672. // this is for KDE4 compatibility:
  1673. std::cerr << "AUTOGEN: warning: " << absFilename << ": The file "
  1674. << "contains a " << macroName << " macro, but does not "
  1675. "include "
  1676. << "\"" << scannedFileBasename << ".moc\", but instead "
  1677. "includes "
  1678. << "\"" << ownMocUnderscoreFile << "\". Running moc on "
  1679. << "\"" << absFilename << "\" ! Better include \""
  1680. << scannedFileBasename << ".moc\" for compatiblity with "
  1681. "strict mode (see CMAKE_AUTOMOC_RELAXED_MODE).\n"
  1682. << std::endl;
  1683. includedMocs[absFilename] = ownMocUnderscoreFile;
  1684. includedMocs.erase(ownMocHeaderFile);
  1685. }
  1686. else
  1687. {
  1688. // otherwise always error out since it will not compile:
  1689. std::cerr << "AUTOGEN: error: " << absFilename << ": The file "
  1690. << "contains a " << macroName << " macro, but does not "
  1691. "include "
  1692. << "\"" << scannedFileBasename << ".moc\" !\n"
  1693. << std::endl;
  1694. ::exit(EXIT_FAILURE);
  1695. }
  1696. }
  1697. }
  1698. void cmQtAutoGenerators::StrictParseCppFile(const std::string& absFilename,
  1699. const std::vector<std::string>& headerExtensions,
  1700. std::map<std::string, std::string>& includedMocs,
  1701. std::map<std::string, std::vector<std::string> >& includedUis)
  1702. {
  1703. cmsys::RegularExpression mocIncludeRegExp(
  1704. "[\n][ \t]*#[ \t]*include[ \t]+"
  1705. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  1706. const std::string contentsString = ReadAll(absFilename);
  1707. if (contentsString.empty())
  1708. {
  1709. std::cerr << "AUTOGEN: warning: " << absFilename << ": file is empty\n"
  1710. << std::endl;
  1711. return;
  1712. }
  1713. this->ParseForUic(absFilename, contentsString, includedUis);
  1714. if (this->MocExecutable.empty())
  1715. {
  1716. return;
  1717. }
  1718. const std::string absPath = cmsys::SystemTools::GetFilenamePath(
  1719. cmsys::SystemTools::GetRealPath(absFilename)) + '/';
  1720. const std::string scannedFileBasename = cmsys::SystemTools::
  1721. GetFilenameWithoutLastExtension(absFilename);
  1722. bool dotMocIncluded = false;
  1723. std::string::size_type matchOffset = 0;
  1724. // first a simple string check for "moc" is *much* faster than the regexp,
  1725. // and if the string search already fails, we don't have to try the
  1726. // expensive regexp
  1727. if ((strstr(contentsString.c_str(), "moc") != NULL)
  1728. && (mocIncludeRegExp.find(contentsString)))
  1729. {
  1730. // for every moc include in the file
  1731. do
  1732. {
  1733. const std::string currentMoc = mocIncludeRegExp.match(1);
  1734. std::string basename = cmsys::SystemTools::
  1735. GetFilenameWithoutLastExtension(currentMoc);
  1736. const bool mocUnderscoreStyle = cmHasLiteralPrefix(basename, "moc_");
  1737. // If the moc include is of the moc_foo.cpp style we expect
  1738. // the Q_OBJECT class declaration in a header file.
  1739. // If the moc include is of the foo.moc style we need to look for
  1740. // a Q_OBJECT macro in the current source file, if it contains the
  1741. // macro we generate the moc file from the source file.
  1742. if (mocUnderscoreStyle)
  1743. {
  1744. // basename should be the part of the moc filename used for
  1745. // finding the correct header, so we need to remove the moc_ part
  1746. basename = basename.substr(4);
  1747. std::string mocSubDir = extractSubDir(absPath, currentMoc);
  1748. std::string headerToMoc = findMatchingHeader(
  1749. absPath, mocSubDir, basename, headerExtensions);
  1750. if (!headerToMoc.empty())
  1751. {
  1752. includedMocs[headerToMoc] = currentMoc;
  1753. }
  1754. else
  1755. {
  1756. std::cerr << "AUTOGEN: error: " << absFilename << " The file "
  1757. << "includes the moc file \"" << currentMoc << "\", "
  1758. << "but could not find header \"" << basename
  1759. << '{' << this->Join(headerExtensions, ',') << "}\" ";
  1760. if (mocSubDir.empty())
  1761. {
  1762. std::cerr << "in " << absPath << "\n" << std::endl;
  1763. }
  1764. else
  1765. {
  1766. std::cerr << "neither in " << absPath
  1767. << " nor in " << mocSubDir << "\n" << std::endl;
  1768. }
  1769. ::exit(EXIT_FAILURE);
  1770. }
  1771. }
  1772. else
  1773. {
  1774. if (basename != scannedFileBasename)
  1775. {
  1776. std::cerr <<"AUTOGEN: error: " << absFilename << ": The file "
  1777. "includes the moc file \"" << currentMoc <<
  1778. "\", which seems to be the moc file from a different "
  1779. "source file. This is not supported. "
  1780. "Include \"" << scannedFileBasename << ".moc\" to run "
  1781. "moc on this source file.\n" << std::endl;
  1782. ::exit(EXIT_FAILURE);
  1783. }
  1784. dotMocIncluded = true;
  1785. includedMocs[absFilename] = currentMoc;
  1786. }
  1787. matchOffset += mocIncludeRegExp.end();
  1788. } while(mocIncludeRegExp.find(contentsString.c_str() + matchOffset));
  1789. }
  1790. // In this case, check whether the scanned file itself contains a Q_OBJECT.
  1791. // If this is the case, the moc_foo.cpp should probably be generated from
  1792. // foo.cpp instead of foo.h, because otherwise it won't build.
  1793. // But warn, since this is not how it is supposed to be used.
  1794. std::string macroName;
  1795. if ((dotMocIncluded == false) && (requiresMocing(contentsString,
  1796. macroName)))
  1797. {
  1798. // otherwise always error out since it will not compile:
  1799. std::cerr << "AUTOGEN: error: " << absFilename << ": The file "
  1800. << "contains a " << macroName << " macro, but does not include "
  1801. << "\"" << scannedFileBasename << ".moc\" !\n"
  1802. << std::endl;
  1803. ::exit(EXIT_FAILURE);
  1804. }
  1805. }
  1806. void cmQtAutoGenerators::ParseForUic(const std::string& absFilename,
  1807. std::map<std::string, std::vector<std::string> >& includedUis)
  1808. {
  1809. if (this->UicExecutable.empty())
  1810. {
  1811. return;
  1812. }
  1813. const std::string contentsString = ReadAll(absFilename);
  1814. if (contentsString.empty())
  1815. {
  1816. std::cerr << "AUTOGEN: warning: " << absFilename << ": file is empty\n"
  1817. << std::endl;
  1818. return;
  1819. }
  1820. this->ParseForUic(absFilename, contentsString, includedUis);
  1821. }
  1822. void cmQtAutoGenerators::ParseForUic(const std::string& absFilename,
  1823. const std::string& contentsString,
  1824. std::map<std::string, std::vector<std::string> >& includedUis)
  1825. {
  1826. if (this->UicExecutable.empty())
  1827. {
  1828. return;
  1829. }
  1830. cmsys::RegularExpression uiIncludeRegExp(
  1831. "[\n][ \t]*#[ \t]*include[ \t]+"
  1832. "[\"<](([^ \">]+/)?ui_[^ \">/]+\\.h)[\">]");
  1833. std::string::size_type matchOffset = 0;
  1834. const std::string realName =
  1835. cmsys::SystemTools::GetRealPath(absFilename);
  1836. matchOffset = 0;
  1837. if ((strstr(contentsString.c_str(), "ui_") != NULL)
  1838. && (uiIncludeRegExp.find(contentsString)))
  1839. {
  1840. do
  1841. {
  1842. const std::string currentUi = uiIncludeRegExp.match(1);
  1843. std::string basename = cmsys::SystemTools::
  1844. GetFilenameWithoutLastExtension(currentUi);
  1845. // basename should be the part of the ui filename used for
  1846. // finding the correct header, so we need to remove the ui_ part
  1847. basename = basename.substr(3);
  1848. includedUis[realName].push_back(basename);
  1849. matchOffset += uiIncludeRegExp.end();
  1850. } while(uiIncludeRegExp.find(contentsString.c_str() + matchOffset));
  1851. }
  1852. }
  1853. void
  1854. cmQtAutoGenerators::SearchHeadersForCppFile(const std::string& absFilename,
  1855. const std::vector<std::string>& headerExtensions,
  1856. std::set<std::string>& absHeaders)
  1857. {
  1858. // search for header files and private header files we may need to moc:
  1859. const std::string basename =
  1860. cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename);
  1861. const std::string absPath = cmsys::SystemTools::GetFilenamePath(
  1862. cmsys::SystemTools::GetRealPath(absFilename)) + '/';
  1863. for(std::vector<std::string>::const_iterator ext = headerExtensions.begin();
  1864. ext != headerExtensions.end();
  1865. ++ext)
  1866. {
  1867. const std::string headerName = absPath + basename + "." + (*ext);
  1868. if (cmsys::SystemTools::FileExists(headerName.c_str()))
  1869. {
  1870. absHeaders.insert(headerName);
  1871. break;
  1872. }
  1873. }
  1874. for(std::vector<std::string>::const_iterator ext = headerExtensions.begin();
  1875. ext != headerExtensions.end();
  1876. ++ext)
  1877. {
  1878. const std::string privateHeaderName = absPath+basename+"_p."+(*ext);
  1879. if (cmsys::SystemTools::FileExists(privateHeaderName.c_str()))
  1880. {
  1881. absHeaders.insert(privateHeaderName);
  1882. break;
  1883. }
  1884. }
  1885. }
  1886. void cmQtAutoGenerators::ParseHeaders(const std::set<std::string>& absHeaders,
  1887. const std::map<std::string, std::string>& includedMocs,
  1888. std::map<std::string, std::string>& notIncludedMocs,
  1889. std::map<std::string, std::vector<std::string> >& includedUis)
  1890. {
  1891. for(std::set<std::string>::const_iterator hIt=absHeaders.begin();
  1892. hIt!=absHeaders.end();
  1893. ++hIt)
  1894. {
  1895. const std::string& headerName = *hIt;
  1896. const std::string contents = ReadAll(headerName);
  1897. if (!this->MocExecutable.empty()
  1898. && includedMocs.find(headerName) == includedMocs.end())
  1899. {
  1900. if (this->Verbose)
  1901. {
  1902. std::cout << "AUTOGEN: Checking " << headerName << std::endl;
  1903. }
  1904. const std::string basename = cmsys::SystemTools::
  1905. GetFilenameWithoutLastExtension(headerName);
  1906. const std::string currentMoc = "moc_" + basename + ".cpp";
  1907. std::string macroName;
  1908. if (requiresMocing(contents, macroName))
  1909. {
  1910. //std::cout << "header contains Q_OBJECT macro";
  1911. notIncludedMocs[headerName] = currentMoc;
  1912. }
  1913. }
  1914. this->ParseForUic(headerName, contents, includedUis);
  1915. }
  1916. }
  1917. bool cmQtAutoGenerators::GenerateMoc(const std::string& sourceFile,
  1918. const std::string& mocFileName)
  1919. {
  1920. const std::string mocFilePath = this->Builddir + mocFileName;
  1921. int sourceNewerThanMoc = 0;
  1922. bool success = cmsys::SystemTools::FileTimeCompare(sourceFile,
  1923. mocFilePath,
  1924. &sourceNewerThanMoc);
  1925. if (this->GenerateAll || !success || sourceNewerThanMoc >= 0)
  1926. {
  1927. // make sure the directory for the resulting moc file exists
  1928. std::string mocDir = mocFilePath.substr(0, mocFilePath.rfind('/'));
  1929. if (!cmsys::SystemTools::FileExists(mocDir.c_str(), false))
  1930. {
  1931. cmsys::SystemTools::MakeDirectory(mocDir.c_str());
  1932. }
  1933. std::string msg = "Generating ";
  1934. msg += mocFileName;
  1935. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue
  1936. |cmsysTerminal_Color_ForegroundBold,
  1937. msg.c_str(), true, this->ColorOutput);
  1938. std::vector<std::string> command;
  1939. command.push_back(this->MocExecutable);
  1940. command.insert(command.end(),
  1941. this->MocIncludes.begin(), this->MocIncludes.end());
  1942. command.insert(command.end(),
  1943. this->MocDefinitions.begin(), this->MocDefinitions.end());
  1944. command.insert(command.end(),
  1945. this->MocOptions.begin(), this->MocOptions.end());
  1946. #ifdef _WIN32
  1947. command.push_back("-DWIN32");
  1948. #endif
  1949. command.push_back("-o");
  1950. command.push_back(mocFilePath);
  1951. command.push_back(sourceFile);
  1952. if (this->Verbose)
  1953. {
  1954. for(std::vector<std::string>::const_iterator cmdIt = command.begin();
  1955. cmdIt != command.end();
  1956. ++cmdIt)
  1957. {
  1958. std::cout << *cmdIt << " ";
  1959. }
  1960. std::cout << std::endl;
  1961. }
  1962. std::string output;
  1963. int retVal = 0;
  1964. bool result = cmSystemTools::RunSingleCommand(command, &output, &output,
  1965. &retVal);
  1966. if (!result || retVal)
  1967. {
  1968. std::cerr << "AUTOGEN: error: process for " << mocFilePath <<" failed:\n"
  1969. << output << std::endl;
  1970. this->RunMocFailed = true;
  1971. cmSystemTools::RemoveFile(mocFilePath);
  1972. }
  1973. return true;
  1974. }
  1975. return false;
  1976. }
  1977. bool cmQtAutoGenerators::GenerateUi(const std::string& realName,
  1978. const std::string& uiFileName)
  1979. {
  1980. if (!cmsys::SystemTools::FileExists(this->Builddir.c_str(), false))
  1981. {
  1982. cmsys::SystemTools::MakeDirectory(this->Builddir.c_str());
  1983. }
  1984. const std::string path = cmsys::SystemTools::GetFilenamePath(
  1985. realName) + '/';
  1986. std::string ui_output_file = "ui_" + uiFileName + ".h";
  1987. std::string ui_input_file = path + uiFileName + ".ui";
  1988. int sourceNewerThanUi = 0;
  1989. bool success = cmsys::SystemTools::FileTimeCompare(ui_input_file,
  1990. this->Builddir + ui_output_file,
  1991. &sourceNewerThanUi);
  1992. if (this->GenerateAll || !success || sourceNewerThanUi >= 0)
  1993. {
  1994. std::string msg = "Generating ";
  1995. msg += ui_output_file;
  1996. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue
  1997. |cmsysTerminal_Color_ForegroundBold,
  1998. msg.c_str(), true, this->ColorOutput);
  1999. std::vector<std::string> command;
  2000. command.push_back(this->UicExecutable);
  2001. std::vector<std::string> opts = this->UicTargetOptions;
  2002. std::map<std::string, std::string>::const_iterator optionIt
  2003. = this->UicOptions.find(ui_input_file);
  2004. if (optionIt != this->UicOptions.end())
  2005. {
  2006. std::vector<std::string> fileOpts;
  2007. cmSystemTools::ExpandListArgument(optionIt->second, fileOpts);
  2008. this->MergeUicOptions(opts, fileOpts, this->QtMajorVersion == "5");
  2009. }
  2010. command.insert(command.end(), opts.begin(), opts.end());
  2011. command.push_back("-o");
  2012. command.push_back(this->Builddir + ui_output_file);
  2013. command.push_back(ui_input_file);
  2014. if (this->Verbose)
  2015. {
  2016. for(std::vector<std::string>::const_iterator cmdIt = command.begin();
  2017. cmdIt != command.end();
  2018. ++cmdIt)
  2019. {
  2020. std::cout << *cmdIt << " ";
  2021. }
  2022. std::cout << std::endl;
  2023. }
  2024. std::string output;
  2025. int retVal = 0;
  2026. bool result = cmSystemTools::RunSingleCommand(command, &output, &output,
  2027. &retVal);
  2028. if (!result || retVal)
  2029. {
  2030. std::cerr << "AUTOUIC: error: process for " << ui_output_file <<
  2031. " needed by\n \"" << realName << "\"\nfailed:\n" << output
  2032. << std::endl;
  2033. this->RunUicFailed = true;
  2034. cmSystemTools::RemoveFile(ui_output_file);
  2035. return false;
  2036. }
  2037. return true;
  2038. }
  2039. return false;
  2040. }
  2041. bool cmQtAutoGenerators::InputFilesNewerThanQrc(const std::string& qrcFile,
  2042. const std::string& rccOutput)
  2043. {
  2044. std::vector<std::string> const& files = this->RccInputs[qrcFile];
  2045. for (std::vector<std::string>::const_iterator it = files.begin();
  2046. it != files.end(); ++it)
  2047. {
  2048. int inputNewerThanQrc = 0;
  2049. bool success = cmsys::SystemTools::FileTimeCompare(*it,
  2050. rccOutput,
  2051. &inputNewerThanQrc);
  2052. if (!success || inputNewerThanQrc >= 0)
  2053. {
  2054. return true;
  2055. }
  2056. }
  2057. return false;
  2058. }
  2059. bool cmQtAutoGenerators::GenerateQrc()
  2060. {
  2061. for(std::vector<std::string>::const_iterator si = this->RccSources.begin();
  2062. si != this->RccSources.end(); ++si)
  2063. {
  2064. std::string ext = cmsys::SystemTools::GetFilenameLastExtension(*si);
  2065. if (ext != ".qrc")
  2066. {
  2067. continue;
  2068. }
  2069. std::vector<std::string> command;
  2070. command.push_back(this->RccExecutable);
  2071. std::string basename = cmsys::SystemTools::
  2072. GetFilenameWithoutLastExtension(*si);
  2073. std::string rcc_output_file = this->Builddir
  2074. + "CMakeFiles/" + this->OriginTargetName
  2075. + ".dir/qrc_" + basename + ".cpp";
  2076. int sourceNewerThanQrc = 0;
  2077. bool generateQrc = !cmsys::SystemTools::FileTimeCompare(*si,
  2078. rcc_output_file,
  2079. &sourceNewerThanQrc);
  2080. generateQrc = generateQrc || (sourceNewerThanQrc >= 0);
  2081. generateQrc = generateQrc || this->InputFilesNewerThanQrc(*si,
  2082. rcc_output_file);
  2083. if (this->GenerateAll || generateQrc)
  2084. {
  2085. std::map<std::string, std::string>::const_iterator optionIt
  2086. = this->RccOptions.find(*si);
  2087. if (optionIt != this->RccOptions.end())
  2088. {
  2089. cmSystemTools::ExpandListArgument(optionIt->second, command);
  2090. }
  2091. command.push_back("-name");
  2092. command.push_back(basename);
  2093. command.push_back("-o");
  2094. command.push_back(rcc_output_file);
  2095. command.push_back(*si);
  2096. if (this->Verbose)
  2097. {
  2098. for(std::vector<std::string>::const_iterator cmdIt = command.begin();
  2099. cmdIt != command.end();
  2100. ++cmdIt)
  2101. {
  2102. std::cout << *cmdIt << " ";
  2103. }
  2104. std::cout << std::endl;
  2105. }
  2106. std::string output;
  2107. int retVal = 0;
  2108. bool result = cmSystemTools::RunSingleCommand(command, &output, &output,
  2109. &retVal);
  2110. if (!result || retVal)
  2111. {
  2112. std::cerr << "AUTORCC: error: process for " << rcc_output_file <<
  2113. " failed:\n" << output << std::endl;
  2114. this->RunRccFailed = true;
  2115. cmSystemTools::RemoveFile(rcc_output_file);
  2116. return false;
  2117. }
  2118. }
  2119. }
  2120. return true;
  2121. }
  2122. std::string cmQtAutoGenerators::Join(const std::vector<std::string>& lst,
  2123. char separator)
  2124. {
  2125. if (lst.empty())
  2126. {
  2127. return "";
  2128. }
  2129. std::string result;
  2130. for (std::vector<std::string>::const_iterator it = lst.begin();
  2131. it != lst.end();
  2132. ++it)
  2133. {
  2134. result += "." + (*it) + separator;
  2135. }
  2136. result.erase(result.end() - 1);
  2137. return result;
  2138. }
  2139. bool cmQtAutoGenerators::StartsWith(const std::string& str,
  2140. const std::string& with)
  2141. {
  2142. return (str.substr(0, with.length()) == with);
  2143. }
  2144. bool cmQtAutoGenerators::EndsWith(const std::string& str,
  2145. const std::string& with)
  2146. {
  2147. if (with.length() > (str.length()))
  2148. {
  2149. return false;
  2150. }
  2151. return (str.substr(str.length() - with.length(), with.length()) == with);
  2152. }