cmQtAutoGenerators.cxx 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407
  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. std::string cmQtAutoGenerators::ListQt5RccInputs(cmSourceFile* sf,
  149. cmTarget const* target,
  150. std::vector<std::string>& depends)
  151. {
  152. std::string rccCommand = this->GetRccExecutable(target);
  153. std::vector<std::string> qrcEntries;
  154. std::vector<std::string> command;
  155. command.push_back(rccCommand);
  156. command.push_back("--list");
  157. std::string absFile = cmsys::SystemTools::GetRealPath(
  158. sf->GetFullPath());
  159. command.push_back(absFile);
  160. std::string output;
  161. int retVal = 0;
  162. bool result = cmSystemTools::RunSingleCommand(command, &output,
  163. &retVal, 0,
  164. cmSystemTools::OUTPUT_NONE);
  165. if (!result || retVal)
  166. {
  167. std::cerr << "AUTOGEN: error: Rcc list process for " << sf->GetFullPath()
  168. << " failed:\n" << output << std::endl;
  169. return std::string();
  170. }
  171. std::istringstream ostr(output);
  172. std::string oline;
  173. while(std::getline(ostr, oline))
  174. {
  175. // Strip CR characters rcc may have printed (possibly more than one!).
  176. std::string::size_type cr = oline.find('\r');
  177. if (cr != oline.npos)
  178. {
  179. oline = oline.substr(0, cr);
  180. }
  181. if (oline.empty())
  182. {
  183. // The output of rcc --list contains many empty lines.
  184. continue;
  185. }
  186. if (cmHasLiteralPrefix(oline, "RCC: Error in"))
  187. {
  188. static std::string searchString = "Cannot find file '";
  189. std::string::size_type pos = oline.find(searchString);
  190. if (pos == std::string::npos)
  191. {
  192. std::cerr << "AUTOGEN: error: Rcc lists unparsable output "
  193. << oline << std::endl;
  194. return std::string();
  195. }
  196. pos += searchString.length();
  197. std::string::size_type sz = oline.size() - pos - 1;
  198. qrcEntries.push_back(oline.substr(pos, sz));
  199. }
  200. else
  201. {
  202. qrcEntries.push_back(oline);
  203. }
  204. }
  205. depends.insert(depends.end(), qrcEntries.begin(), qrcEntries.end());
  206. return cmJoin(qrcEntries, "@list_sep@");
  207. }
  208. std::string cmQtAutoGenerators::ListQt4RccInputs(cmSourceFile* sf,
  209. std::vector<std::string>& depends)
  210. {
  211. const std::string qrcContents = ReadAll(sf->GetFullPath());
  212. cmsys::RegularExpression fileMatchRegex("(<file[^<]+)");
  213. std::string entriesList;
  214. const char* sep = "";
  215. size_t offset = 0;
  216. while (fileMatchRegex.find(qrcContents.c_str() + offset))
  217. {
  218. std::string qrcEntry = fileMatchRegex.match(1);
  219. offset += qrcEntry.size();
  220. cmsys::RegularExpression fileReplaceRegex("(^<file[^>]*>)");
  221. fileReplaceRegex.find(qrcEntry);
  222. std::string tag = fileReplaceRegex.match(1);
  223. qrcEntry = qrcEntry.substr(tag.size());
  224. if (!cmSystemTools::FileIsFullPath(qrcEntry.c_str()))
  225. {
  226. qrcEntry = sf->GetLocation().GetDirectory() + "/" + qrcEntry;
  227. }
  228. entriesList += sep;
  229. entriesList += qrcEntry;
  230. sep = "@list_sep@";
  231. depends.push_back(qrcEntry);
  232. }
  233. return entriesList;
  234. }
  235. bool cmQtAutoGenerators::InitializeAutogenTarget(cmTarget* target)
  236. {
  237. cmMakefile* makefile = target->GetMakefile();
  238. // don't do anything if there is no Qt4 or Qt5Core (which contains moc):
  239. std::string qtMajorVersion = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
  240. if (qtMajorVersion == "")
  241. {
  242. qtMajorVersion = makefile->GetSafeDefinition("Qt5Core_VERSION_MAJOR");
  243. }
  244. if (qtMajorVersion != "4" && qtMajorVersion != "5")
  245. {
  246. return false;
  247. }
  248. if (target->GetPropertyAsBool("AUTOMOC"))
  249. {
  250. std::string automocTargetName = getAutogenTargetName(target);
  251. std::string mocCppFile = makefile->GetCurrentBinaryDirectory();
  252. mocCppFile += "/";
  253. mocCppFile += automocTargetName;
  254. mocCppFile += ".cpp";
  255. makefile->GetOrCreateSource(mocCppFile, true);
  256. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES",
  257. mocCppFile.c_str(), false);
  258. target->AddSource(mocCppFile);
  259. }
  260. // create a custom target for running generators at buildtime:
  261. std::string autogenTargetName = getAutogenTargetName(target);
  262. std::string targetDir = getAutogenTargetDir(target);
  263. cmCustomCommandLine currentLine;
  264. currentLine.push_back(makefile->GetSafeDefinition("CMAKE_COMMAND"));
  265. currentLine.push_back("-E");
  266. currentLine.push_back("cmake_autogen");
  267. currentLine.push_back(targetDir);
  268. currentLine.push_back("$<CONFIGURATION>");
  269. cmCustomCommandLines commandLines;
  270. commandLines.push_back(currentLine);
  271. std::string workingDirectory = cmSystemTools::CollapseFullPath(
  272. "", makefile->GetCurrentBinaryDirectory());
  273. std::vector<std::string> depends;
  274. if (const char *autogenDepends =
  275. target->GetProperty("AUTOGEN_TARGET_DEPENDS"))
  276. {
  277. cmSystemTools::ExpandListArgument(autogenDepends, depends);
  278. }
  279. std::vector<std::string> toolNames;
  280. if (target->GetPropertyAsBool("AUTOMOC"))
  281. {
  282. toolNames.push_back("moc");
  283. }
  284. if (target->GetPropertyAsBool("AUTOUIC"))
  285. {
  286. toolNames.push_back("uic");
  287. }
  288. if (target->GetPropertyAsBool("AUTORCC"))
  289. {
  290. toolNames.push_back("rcc");
  291. }
  292. std::string tools = toolNames[0];
  293. toolNames.erase(toolNames.begin());
  294. while (toolNames.size() > 1)
  295. {
  296. tools += ", " + toolNames[0];
  297. toolNames.erase(toolNames.begin());
  298. }
  299. if (toolNames.size() == 1)
  300. {
  301. tools += " and " + toolNames[0];
  302. }
  303. std::string autogenComment = "Automatic " + tools + " for target ";
  304. autogenComment += target->GetName();
  305. #if defined(_WIN32) && !defined(__CYGWIN__)
  306. bool usePRE_BUILD = false;
  307. cmLocalGenerator* localGen = makefile->GetLocalGenerator();
  308. cmGlobalGenerator* gg = localGen->GetGlobalGenerator();
  309. if(gg->GetName().find("Visual Studio") != std::string::npos)
  310. {
  311. cmLocalVisualStudioGenerator* vslg =
  312. static_cast<cmLocalVisualStudioGenerator*>(localGen);
  313. // Under VS >= 7 use a PRE_BUILD event instead of a separate target to
  314. // reduce the number of targets loaded into the IDE.
  315. // This also works around a VS 11 bug that may skip updating the target:
  316. // https://connect.microsoft.com/VisualStudio/feedback/details/769495
  317. usePRE_BUILD = vslg->GetVersion() >= cmLocalVisualStudioGenerator::VS7;
  318. if(usePRE_BUILD)
  319. {
  320. for (std::vector<std::string>::iterator it = depends.begin();
  321. it != depends.end(); ++it)
  322. {
  323. if(!makefile->FindTargetToUse(it->c_str()))
  324. {
  325. usePRE_BUILD = false;
  326. break;
  327. }
  328. }
  329. }
  330. }
  331. #endif
  332. std::vector<std::string> rcc_output;
  333. bool const isNinja =
  334. makefile->GetLocalGenerator()->GetGlobalGenerator()->GetName() == "Ninja";
  335. if(isNinja
  336. #if defined(_WIN32) && !defined(__CYGWIN__)
  337. || usePRE_BUILD
  338. #endif
  339. )
  340. {
  341. std::vector<cmSourceFile*> srcFiles;
  342. target->GetConfigCommonSourceFiles(srcFiles);
  343. for(std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  344. fileIt != srcFiles.end();
  345. ++fileIt)
  346. {
  347. cmSourceFile* sf = *fileIt;
  348. std::string absFile = cmsys::SystemTools::GetRealPath(
  349. sf->GetFullPath());
  350. std::string ext = sf->GetExtension();
  351. if (target->GetPropertyAsBool("AUTORCC"))
  352. {
  353. if (ext == "qrc"
  354. && !cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTORCC")))
  355. {
  356. std::string basename = cmsys::SystemTools::
  357. GetFilenameWithoutLastExtension(absFile);
  358. std::string rcc_output_dir = target->GetSupportDirectory();
  359. cmSystemTools::MakeDirectory(rcc_output_dir.c_str());
  360. std::string rcc_output_file = rcc_output_dir;
  361. rcc_output_file += "/qrc_" + basename + ".cpp";
  362. rcc_output.push_back(rcc_output_file);
  363. if (!cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED")))
  364. {
  365. if (qtMajorVersion == "5")
  366. {
  367. this->ListQt5RccInputs(sf, target, depends);
  368. }
  369. else
  370. {
  371. this->ListQt4RccInputs(sf, depends);
  372. }
  373. #if defined(_WIN32) && !defined(__CYGWIN__)
  374. usePRE_BUILD = false;
  375. #endif
  376. }
  377. }
  378. }
  379. }
  380. }
  381. #if defined(_WIN32) && !defined(__CYGWIN__)
  382. if(usePRE_BUILD)
  383. {
  384. // Add the pre-build command directly to bypass the OBJECT_LIBRARY
  385. // rejection in cmMakefile::AddCustomCommandToTarget because we know
  386. // PRE_BUILD will work for an OBJECT_LIBRARY in this specific case.
  387. std::vector<std::string> no_output;
  388. std::vector<std::string> no_byproducts;
  389. cmCustomCommand cc(makefile, no_output, no_byproducts, depends,
  390. commandLines, autogenComment.c_str(),
  391. workingDirectory.c_str());
  392. cc.SetEscapeOldStyle(false);
  393. cc.SetEscapeAllowMakeVars(true);
  394. target->AddPreBuildCommand(cc);
  395. }
  396. else
  397. #endif
  398. {
  399. cmTarget* autogenTarget = 0;
  400. if (!rcc_output.empty() && !isNinja)
  401. {
  402. std::vector<std::string> no_byproducts;
  403. makefile->AddCustomCommandToOutput(rcc_output, no_byproducts,
  404. depends, "",
  405. commandLines, 0,
  406. workingDirectory.c_str(),
  407. false, false);
  408. cmCustomCommandLines no_commands;
  409. autogenTarget = makefile->AddUtilityCommand(
  410. autogenTargetName, true,
  411. workingDirectory.c_str(), rcc_output,
  412. no_commands, false, autogenComment.c_str());
  413. }
  414. else
  415. {
  416. autogenTarget = makefile->AddUtilityCommand(
  417. autogenTargetName, true,
  418. workingDirectory.c_str(),
  419. /*byproducts=*/rcc_output, depends,
  420. commandLines, false, autogenComment.c_str());
  421. }
  422. // Set target folder
  423. const char* autogenFolder = makefile->GetState()
  424. ->GetGlobalProperty("AUTOMOC_TARGETS_FOLDER");
  425. if (!autogenFolder)
  426. {
  427. autogenFolder = makefile->GetState()
  428. ->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
  429. }
  430. if (autogenFolder && *autogenFolder)
  431. {
  432. autogenTarget->SetProperty("FOLDER", autogenFolder);
  433. }
  434. else
  435. {
  436. // inherit FOLDER property from target (#13688)
  437. copyTargetProperty(autogenTarget, target, "FOLDER");
  438. }
  439. target->AddUtility(autogenTargetName);
  440. }
  441. return true;
  442. }
  443. static void GetCompileDefinitionsAndDirectories(cmTarget const* target,
  444. const std::string& config,
  445. std::string &incs,
  446. std::string &defs)
  447. {
  448. cmMakefile* makefile = target->GetMakefile();
  449. cmLocalGenerator* localGen = makefile->GetLocalGenerator();
  450. std::vector<std::string> includeDirs;
  451. cmGeneratorTarget *gtgt = target->GetMakefile()->GetLocalGenerator()
  452. ->GetGlobalGenerator()
  453. ->GetGeneratorTarget(target);
  454. // Get the include dirs for this target, without stripping the implicit
  455. // include dirs off, see http://public.kitware.com/Bug/view.php?id=13667
  456. localGen->GetIncludeDirectories(includeDirs, gtgt, "CXX", config, false);
  457. incs = cmJoin(includeDirs, ";");
  458. std::set<std::string> defines;
  459. localGen->AddCompileDefinitions(defines, target, config, "CXX");
  460. defs += cmJoin(defines, ";");
  461. }
  462. void cmQtAutoGenerators::SetupAutoGenerateTarget(cmTarget const* target)
  463. {
  464. cmMakefile* makefile = target->GetMakefile();
  465. // forget the variables added here afterwards again:
  466. cmMakefile::ScopePushPop varScope(makefile);
  467. static_cast<void>(varScope);
  468. // create a custom target for running generators at buildtime:
  469. std::string autogenTargetName = getAutogenTargetName(target);
  470. makefile->AddDefinition("_moc_target_name",
  471. cmLocalGenerator::EscapeForCMake(autogenTargetName).c_str());
  472. makefile->AddDefinition("_origin_target_name",
  473. cmLocalGenerator::EscapeForCMake(target->GetName()).c_str());
  474. std::string targetDir = getAutogenTargetDir(target);
  475. const char *qtVersion = makefile->GetDefinition("Qt5Core_VERSION_MAJOR");
  476. if (!qtVersion)
  477. {
  478. qtVersion = makefile->GetDefinition("QT_VERSION_MAJOR");
  479. }
  480. if (const char *targetQtVersion =
  481. target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION", ""))
  482. {
  483. qtVersion = targetQtVersion;
  484. }
  485. if (qtVersion)
  486. {
  487. makefile->AddDefinition("_target_qt_version", qtVersion);
  488. }
  489. std::map<std::string, std::string> configIncludes;
  490. std::map<std::string, std::string> configDefines;
  491. std::map<std::string, std::string> configUicOptions;
  492. if (target->GetPropertyAsBool("AUTOMOC")
  493. || target->GetPropertyAsBool("AUTOUIC")
  494. || target->GetPropertyAsBool("AUTORCC"))
  495. {
  496. this->SetupSourceFiles(target);
  497. }
  498. makefile->AddDefinition("_cpp_files",
  499. cmLocalGenerator::EscapeForCMake(this->Sources).c_str());
  500. if (target->GetPropertyAsBool("AUTOMOC"))
  501. {
  502. this->SetupAutoMocTarget(target, autogenTargetName,
  503. configIncludes, configDefines);
  504. }
  505. if (target->GetPropertyAsBool("AUTOUIC"))
  506. {
  507. this->SetupAutoUicTarget(target, configUicOptions);
  508. }
  509. if (target->GetPropertyAsBool("AUTORCC"))
  510. {
  511. this->SetupAutoRccTarget(target);
  512. }
  513. const char* cmakeRoot = makefile->GetSafeDefinition("CMAKE_ROOT");
  514. std::string inputFile = cmakeRoot;
  515. inputFile += "/Modules/AutogenInfo.cmake.in";
  516. std::string outputFile = targetDir;
  517. outputFile += "/AutogenInfo.cmake";
  518. makefile->AddDefinition("_qt_rcc_inputs",
  519. makefile->GetDefinition("_qt_rcc_inputs_" + target->GetName()));
  520. makefile->ConfigureFile(inputFile.c_str(), outputFile.c_str(),
  521. false, true, false);
  522. // Ensure we have write permission in case .in was read-only.
  523. mode_t perm = 0;
  524. #if defined(WIN32) && !defined(__CYGWIN__)
  525. mode_t mode_write = S_IWRITE;
  526. #else
  527. mode_t mode_write = S_IWUSR;
  528. #endif
  529. cmSystemTools::GetPermissions(outputFile, perm);
  530. if (!(perm & mode_write))
  531. {
  532. cmSystemTools::SetPermissions(outputFile, perm | mode_write);
  533. }
  534. if (!configDefines.empty()
  535. || !configIncludes.empty()
  536. || !configUicOptions.empty())
  537. {
  538. cmsys::ofstream infoFile(outputFile.c_str(), std::ios::app);
  539. if ( !infoFile )
  540. {
  541. std::string error = "Internal CMake error when trying to open file: ";
  542. error += outputFile.c_str();
  543. error += " for writing.";
  544. cmSystemTools::Error(error.c_str());
  545. return;
  546. }
  547. if (!configDefines.empty())
  548. {
  549. for (std::map<std::string, std::string>::iterator
  550. it = configDefines.begin(), end = configDefines.end();
  551. it != end; ++it)
  552. {
  553. infoFile << "set(AM_MOC_COMPILE_DEFINITIONS_" << it->first <<
  554. " " << it->second << ")\n";
  555. }
  556. }
  557. if (!configIncludes.empty())
  558. {
  559. for (std::map<std::string, std::string>::iterator
  560. it = configIncludes.begin(), end = configIncludes.end();
  561. it != end; ++it)
  562. {
  563. infoFile << "set(AM_MOC_INCLUDES_" << it->first <<
  564. " " << it->second << ")\n";
  565. }
  566. }
  567. if (!configUicOptions.empty())
  568. {
  569. for (std::map<std::string, std::string>::iterator
  570. it = configUicOptions.begin(), end = configUicOptions.end();
  571. it != end; ++it)
  572. {
  573. infoFile << "set(AM_UIC_TARGET_OPTIONS_" << it->first <<
  574. " " << it->second << ")\n";
  575. }
  576. }
  577. }
  578. }
  579. void cmQtAutoGenerators::SetupSourceFiles(cmTarget const* target)
  580. {
  581. cmMakefile* makefile = target->GetMakefile();
  582. const char* sepFiles = "";
  583. const char* sepHeaders = "";
  584. std::vector<cmSourceFile*> srcFiles;
  585. target->GetConfigCommonSourceFiles(srcFiles);
  586. const char *skipMocSep = "";
  587. const char *skipUicSep = "";
  588. std::vector<std::string> newRccFiles;
  589. for(std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  590. fileIt != srcFiles.end();
  591. ++fileIt)
  592. {
  593. cmSourceFile* sf = *fileIt;
  594. std::string absFile = cmsys::SystemTools::GetRealPath(
  595. sf->GetFullPath());
  596. bool skipMoc = cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTOMOC"));
  597. bool generated = cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED"));
  598. if(cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTOUIC")))
  599. {
  600. this->SkipUic += skipUicSep;
  601. this->SkipUic += absFile;
  602. skipUicSep = ";";
  603. }
  604. std::string ext = sf->GetExtension();
  605. if (target->GetPropertyAsBool("AUTORCC"))
  606. {
  607. if (ext == "qrc"
  608. && !cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTORCC")))
  609. {
  610. std::string basename = cmsys::SystemTools::
  611. GetFilenameWithoutLastExtension(absFile);
  612. std::string rcc_output_dir = target->GetSupportDirectory();
  613. cmSystemTools::MakeDirectory(rcc_output_dir.c_str());
  614. std::string rcc_output_file = rcc_output_dir;
  615. rcc_output_file += "/qrc_" + basename + ".cpp";
  616. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES",
  617. rcc_output_file.c_str(), false);
  618. makefile->GetOrCreateSource(rcc_output_file, true);
  619. newRccFiles.push_back(rcc_output_file);
  620. }
  621. }
  622. if (!generated)
  623. {
  624. if (skipMoc)
  625. {
  626. this->SkipMoc += skipMocSep;
  627. this->SkipMoc += absFile;
  628. skipMocSep = ";";
  629. }
  630. else
  631. {
  632. cmSystemTools::FileFormat fileType = cmSystemTools::GetFileFormat(
  633. ext.c_str());
  634. if (fileType == cmSystemTools::CXX_FILE_FORMAT)
  635. {
  636. this->Sources += sepFiles;
  637. this->Sources += absFile;
  638. sepFiles = ";";
  639. }
  640. else if (fileType == cmSystemTools::HEADER_FILE_FORMAT)
  641. {
  642. this->Headers += sepHeaders;
  643. this->Headers += absFile;
  644. sepHeaders = ";";
  645. }
  646. }
  647. }
  648. }
  649. for(std::vector<std::string>::const_iterator fileIt = newRccFiles.begin();
  650. fileIt != newRccFiles.end();
  651. ++fileIt)
  652. {
  653. const_cast<cmTarget*>(target)->AddSource(*fileIt);
  654. }
  655. }
  656. void cmQtAutoGenerators::SetupAutoMocTarget(cmTarget const* target,
  657. const std::string &autogenTargetName,
  658. std::map<std::string, std::string> &configIncludes,
  659. std::map<std::string, std::string> &configDefines)
  660. {
  661. cmMakefile* makefile = target->GetMakefile();
  662. const char* tmp = target->GetProperty("AUTOMOC_MOC_OPTIONS");
  663. std::string _moc_options = (tmp!=0 ? tmp : "");
  664. makefile->AddDefinition("_moc_options",
  665. cmLocalGenerator::EscapeForCMake(_moc_options).c_str());
  666. makefile->AddDefinition("_skip_moc",
  667. cmLocalGenerator::EscapeForCMake(this->SkipMoc).c_str());
  668. makefile->AddDefinition("_moc_headers",
  669. cmLocalGenerator::EscapeForCMake(this->Headers).c_str());
  670. bool relaxedMode = makefile->IsOn("CMAKE_AUTOMOC_RELAXED_MODE");
  671. makefile->AddDefinition("_moc_relaxed_mode", relaxedMode ? "TRUE" : "FALSE");
  672. std::string _moc_incs;
  673. std::string _moc_compile_defs;
  674. std::vector<std::string> configs;
  675. const std::string& config = makefile->GetConfigurations(configs);
  676. GetCompileDefinitionsAndDirectories(target, config,
  677. _moc_incs, _moc_compile_defs);
  678. makefile->AddDefinition("_moc_incs",
  679. cmLocalGenerator::EscapeForCMake(_moc_incs).c_str());
  680. makefile->AddDefinition("_moc_compile_defs",
  681. cmLocalGenerator::EscapeForCMake(_moc_compile_defs).c_str());
  682. for (std::vector<std::string>::const_iterator li = configs.begin();
  683. li != configs.end(); ++li)
  684. {
  685. std::string config_moc_incs;
  686. std::string config_moc_compile_defs;
  687. GetCompileDefinitionsAndDirectories(target, *li,
  688. config_moc_incs,
  689. config_moc_compile_defs);
  690. if (config_moc_incs != _moc_incs)
  691. {
  692. configIncludes[*li] =
  693. cmLocalGenerator::EscapeForCMake(config_moc_incs);
  694. if(_moc_incs.empty())
  695. {
  696. _moc_incs = config_moc_incs;
  697. }
  698. }
  699. if (config_moc_compile_defs != _moc_compile_defs)
  700. {
  701. configDefines[*li] =
  702. cmLocalGenerator::EscapeForCMake(config_moc_compile_defs);
  703. if(_moc_compile_defs.empty())
  704. {
  705. _moc_compile_defs = config_moc_compile_defs;
  706. }
  707. }
  708. }
  709. const char *qtVersion = makefile->GetDefinition("_target_qt_version");
  710. if (strcmp(qtVersion, "5") == 0)
  711. {
  712. cmTarget *qt5Moc = makefile->FindTargetToUse("Qt5::moc");
  713. if (!qt5Moc)
  714. {
  715. cmSystemTools::Error("Qt5::moc target not found ",
  716. autogenTargetName.c_str());
  717. return;
  718. }
  719. makefile->AddDefinition("_qt_moc_executable", qt5Moc->GetLocation(""));
  720. }
  721. else if (strcmp(qtVersion, "4") == 0)
  722. {
  723. cmTarget *qt4Moc = makefile->FindTargetToUse("Qt4::moc");
  724. if (!qt4Moc)
  725. {
  726. cmSystemTools::Error("Qt4::moc target not found ",
  727. autogenTargetName.c_str());
  728. return;
  729. }
  730. makefile->AddDefinition("_qt_moc_executable", qt4Moc->GetLocation(""));
  731. }
  732. else
  733. {
  734. cmSystemTools::Error("The CMAKE_AUTOMOC feature supports only Qt 4 and "
  735. "Qt 5 ", autogenTargetName.c_str());
  736. }
  737. }
  738. void cmQtAutoGenerators::MergeUicOptions(std::vector<std::string> &opts,
  739. const std::vector<std::string> &fileOpts,
  740. bool isQt5)
  741. {
  742. static const char* valueOptions[] = {
  743. "tr",
  744. "translate",
  745. "postfix",
  746. "generator",
  747. "include", // Since Qt 5.3
  748. "g"
  749. };
  750. std::vector<std::string> extraOpts;
  751. for(std::vector<std::string>::const_iterator it = fileOpts.begin();
  752. it != fileOpts.end(); ++it)
  753. {
  754. std::vector<std::string>::iterator existingIt
  755. = std::find(opts.begin(), opts.end(), *it);
  756. if (existingIt != opts.end())
  757. {
  758. const char *o = it->c_str();
  759. if (*o == '-')
  760. {
  761. ++o;
  762. }
  763. if (isQt5 && *o == '-')
  764. {
  765. ++o;
  766. }
  767. if (std::find_if(cmArrayBegin(valueOptions), cmArrayEnd(valueOptions),
  768. cmStrCmp(*it)) != cmArrayEnd(valueOptions))
  769. {
  770. assert(existingIt + 1 != opts.end());
  771. *(existingIt + 1) = *(it + 1);
  772. ++it;
  773. }
  774. }
  775. else
  776. {
  777. extraOpts.push_back(*it);
  778. }
  779. }
  780. opts.insert(opts.end(), extraOpts.begin(), extraOpts.end());
  781. }
  782. static void GetUicOpts(cmTarget const* target, const std::string& config,
  783. std::string &optString)
  784. {
  785. std::vector<std::string> opts;
  786. target->GetAutoUicOptions(opts, config);
  787. optString = cmJoin(opts, ";");
  788. }
  789. void cmQtAutoGenerators::SetupAutoUicTarget(cmTarget const* target,
  790. std::map<std::string, std::string> &configUicOptions)
  791. {
  792. cmMakefile *makefile = target->GetMakefile();
  793. std::set<std::string> skipped;
  794. std::vector<std::string> skipVec;
  795. cmSystemTools::ExpandListArgument(this->SkipUic, skipVec);
  796. skipped.insert(skipVec.begin(), skipVec.end());
  797. makefile->AddDefinition("_skip_uic",
  798. cmLocalGenerator::EscapeForCMake(this->SkipUic).c_str());
  799. std::vector<cmSourceFile*> uiFilesWithOptions
  800. = makefile->GetQtUiFilesWithOptions();
  801. const char *qtVersion = makefile->GetDefinition("_target_qt_version");
  802. std::string _uic_opts;
  803. std::vector<std::string> configs;
  804. const std::string& config = makefile->GetConfigurations(configs);
  805. GetUicOpts(target, config, _uic_opts);
  806. if (!_uic_opts.empty())
  807. {
  808. _uic_opts = cmLocalGenerator::EscapeForCMake(_uic_opts);
  809. makefile->AddDefinition("_uic_target_options", _uic_opts.c_str());
  810. }
  811. for (std::vector<std::string>::const_iterator li = configs.begin();
  812. li != configs.end(); ++li)
  813. {
  814. std::string config_uic_opts;
  815. GetUicOpts(target, *li, config_uic_opts);
  816. if (config_uic_opts != _uic_opts)
  817. {
  818. configUicOptions[*li] =
  819. cmLocalGenerator::EscapeForCMake(config_uic_opts);
  820. if(_uic_opts.empty())
  821. {
  822. _uic_opts = config_uic_opts;
  823. }
  824. }
  825. }
  826. std::string uiFileFiles;
  827. std::string uiFileOptions;
  828. const char* sep = "";
  829. for(std::vector<cmSourceFile*>::const_iterator fileIt =
  830. uiFilesWithOptions.begin();
  831. fileIt != uiFilesWithOptions.end();
  832. ++fileIt)
  833. {
  834. cmSourceFile* sf = *fileIt;
  835. std::string absFile = cmsys::SystemTools::GetRealPath(
  836. sf->GetFullPath());
  837. if (!skipped.insert(absFile).second)
  838. {
  839. continue;
  840. }
  841. uiFileFiles += sep;
  842. uiFileFiles += absFile;
  843. uiFileOptions += sep;
  844. std::string opts = sf->GetProperty("AUTOUIC_OPTIONS");
  845. cmSystemTools::ReplaceString(opts, ";", "@list_sep@");
  846. uiFileOptions += opts;
  847. sep = ";";
  848. }
  849. makefile->AddDefinition("_qt_uic_options_files",
  850. cmLocalGenerator::EscapeForCMake(uiFileFiles).c_str());
  851. makefile->AddDefinition("_qt_uic_options_options",
  852. cmLocalGenerator::EscapeForCMake(uiFileOptions).c_str());
  853. std::string targetName = target->GetName();
  854. if (strcmp(qtVersion, "5") == 0)
  855. {
  856. cmTarget *qt5Uic = makefile->FindTargetToUse("Qt5::uic");
  857. if (!qt5Uic)
  858. {
  859. // Project does not use Qt5Widgets, but has AUTOUIC ON anyway
  860. }
  861. else
  862. {
  863. makefile->AddDefinition("_qt_uic_executable", qt5Uic->GetLocation(""));
  864. }
  865. }
  866. else if (strcmp(qtVersion, "4") == 0)
  867. {
  868. cmTarget *qt4Uic = makefile->FindTargetToUse("Qt4::uic");
  869. if (!qt4Uic)
  870. {
  871. cmSystemTools::Error("Qt4::uic target not found ",
  872. targetName.c_str());
  873. return;
  874. }
  875. makefile->AddDefinition("_qt_uic_executable", qt4Uic->GetLocation(""));
  876. }
  877. else
  878. {
  879. cmSystemTools::Error("The CMAKE_AUTOUIC feature supports only Qt 4 and "
  880. "Qt 5 ", targetName.c_str());
  881. }
  882. }
  883. void cmQtAutoGenerators::MergeRccOptions(std::vector<std::string> &opts,
  884. const std::vector<std::string> &fileOpts,
  885. bool isQt5)
  886. {
  887. static const char* valueOptions[] = {
  888. "name",
  889. "root",
  890. "compress",
  891. "threshold"
  892. };
  893. std::vector<std::string> extraOpts;
  894. for(std::vector<std::string>::const_iterator it = fileOpts.begin();
  895. it != fileOpts.end(); ++it)
  896. {
  897. std::vector<std::string>::iterator existingIt
  898. = std::find(opts.begin(), opts.end(), *it);
  899. if (existingIt != opts.end())
  900. {
  901. const char *o = it->c_str();
  902. if (*o == '-')
  903. {
  904. ++o;
  905. }
  906. if (isQt5 && *o == '-')
  907. {
  908. ++o;
  909. }
  910. if (std::find_if(cmArrayBegin(valueOptions), cmArrayEnd(valueOptions),
  911. cmStrCmp(*it)) != cmArrayEnd(valueOptions))
  912. {
  913. assert(existingIt + 1 != opts.end());
  914. *(existingIt + 1) = *(it + 1);
  915. ++it;
  916. }
  917. }
  918. else
  919. {
  920. extraOpts.push_back(*it);
  921. }
  922. }
  923. opts.insert(opts.end(), extraOpts.begin(), extraOpts.end());
  924. }
  925. void cmQtAutoGenerators::SetupAutoRccTarget(cmTarget const* target)
  926. {
  927. std::string _rcc_files;
  928. const char* sepRccFiles = "";
  929. cmMakefile *makefile = target->GetMakefile();
  930. std::vector<cmSourceFile*> srcFiles;
  931. target->GetConfigCommonSourceFiles(srcFiles);
  932. std::string qrcInputs;
  933. const char* qrcInputsSep = "";
  934. std::string rccFileFiles;
  935. std::string rccFileOptions;
  936. const char *optionSep = "";
  937. const char *qtVersion = makefile->GetDefinition("_target_qt_version");
  938. std::vector<std::string> rccOptions;
  939. if (const char* opts = target->GetProperty("AUTORCC_OPTIONS"))
  940. {
  941. cmSystemTools::ExpandListArgument(opts, rccOptions);
  942. }
  943. std::string qtMajorVersion = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
  944. if (qtMajorVersion == "")
  945. {
  946. qtMajorVersion = makefile->GetSafeDefinition("Qt5Core_VERSION_MAJOR");
  947. }
  948. for(std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  949. fileIt != srcFiles.end();
  950. ++fileIt)
  951. {
  952. cmSourceFile* sf = *fileIt;
  953. std::string ext = sf->GetExtension();
  954. if (ext == "qrc")
  955. {
  956. std::string absFile = cmsys::SystemTools::GetRealPath(
  957. sf->GetFullPath());
  958. bool skip = cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTORCC"));
  959. if (!skip)
  960. {
  961. _rcc_files += sepRccFiles;
  962. _rcc_files += absFile;
  963. sepRccFiles = ";";
  964. if (const char *prop = sf->GetProperty("AUTORCC_OPTIONS"))
  965. {
  966. std::vector<std::string> optsVec;
  967. cmSystemTools::ExpandListArgument(prop, optsVec);
  968. this->MergeRccOptions(rccOptions, optsVec,
  969. strcmp(qtVersion, "5") == 0);
  970. }
  971. if (!rccOptions.empty())
  972. {
  973. rccFileFiles += optionSep;
  974. rccFileFiles += absFile;
  975. rccFileOptions += optionSep;
  976. }
  977. const char *listSep = "";
  978. for(std::vector<std::string>::const_iterator it = rccOptions.begin();
  979. it != rccOptions.end();
  980. ++it)
  981. {
  982. rccFileOptions += listSep;
  983. rccFileOptions += *it;
  984. listSep = "@list_sep@";
  985. }
  986. optionSep = ";";
  987. std::vector<std::string> depends;
  988. std::string entriesList;
  989. if (!cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED")))
  990. {
  991. if (qtMajorVersion == "5")
  992. {
  993. entriesList = this->ListQt5RccInputs(sf, target, depends);
  994. }
  995. else
  996. {
  997. entriesList = this->ListQt4RccInputs(sf, depends);
  998. }
  999. if (entriesList.empty())
  1000. {
  1001. return;
  1002. }
  1003. }
  1004. qrcInputs += qrcInputsSep;
  1005. qrcInputs += entriesList;
  1006. qrcInputsSep = ";";
  1007. }
  1008. }
  1009. }
  1010. makefile->AddDefinition("_qt_rcc_inputs_" + target->GetName(),
  1011. cmLocalGenerator::EscapeForCMake(qrcInputs).c_str());
  1012. makefile->AddDefinition("_rcc_files",
  1013. cmLocalGenerator::EscapeForCMake(_rcc_files).c_str());
  1014. makefile->AddDefinition("_qt_rcc_options_files",
  1015. cmLocalGenerator::EscapeForCMake(rccFileFiles).c_str());
  1016. makefile->AddDefinition("_qt_rcc_options_options",
  1017. cmLocalGenerator::EscapeForCMake(rccFileOptions).c_str());
  1018. makefile->AddDefinition("_qt_rcc_executable",
  1019. this->GetRccExecutable(target).c_str());
  1020. }
  1021. std::string cmQtAutoGenerators::GetRccExecutable(cmTarget const* target)
  1022. {
  1023. cmMakefile *makefile = target->GetMakefile();
  1024. const char *qtVersion = makefile->GetDefinition("_target_qt_version");
  1025. if (!qtVersion)
  1026. {
  1027. qtVersion = makefile->GetDefinition("Qt5Core_VERSION_MAJOR");
  1028. if (!qtVersion)
  1029. {
  1030. qtVersion = makefile->GetDefinition("QT_VERSION_MAJOR");
  1031. }
  1032. if (const char *targetQtVersion =
  1033. target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION",
  1034. ""))
  1035. {
  1036. qtVersion = targetQtVersion;
  1037. }
  1038. }
  1039. std::string targetName = target->GetName();
  1040. if (strcmp(qtVersion, "5") == 0)
  1041. {
  1042. cmTarget *qt5Rcc = makefile->FindTargetToUse("Qt5::rcc");
  1043. if (!qt5Rcc)
  1044. {
  1045. cmSystemTools::Error("Qt5::rcc target not found ",
  1046. targetName.c_str());
  1047. return std::string();
  1048. }
  1049. return qt5Rcc->GetLocation("");
  1050. }
  1051. else if (strcmp(qtVersion, "4") == 0)
  1052. {
  1053. cmTarget *qt4Rcc = makefile->FindTargetToUse("Qt4::rcc");
  1054. if (!qt4Rcc)
  1055. {
  1056. cmSystemTools::Error("Qt4::rcc target not found ",
  1057. targetName.c_str());
  1058. return std::string();
  1059. }
  1060. return qt4Rcc->GetLocation("");
  1061. }
  1062. cmSystemTools::Error("The CMAKE_AUTORCC feature supports only Qt 4 and "
  1063. "Qt 5 ", targetName.c_str());
  1064. return std::string();
  1065. }
  1066. static cmGlobalGenerator* CreateGlobalGenerator(cmake* cm,
  1067. const std::string& targetDirectory)
  1068. {
  1069. cmGlobalGenerator* gg = new cmGlobalGenerator();
  1070. gg->SetCMakeInstance(cm);
  1071. cmLocalGenerator* lg = gg->CreateLocalGenerator();
  1072. lg->GetMakefile()->SetHomeOutputDirectory(targetDirectory);
  1073. lg->GetMakefile()->SetCurrentBinaryDirectory(targetDirectory);
  1074. lg->GetMakefile()->SetHomeDirectory(targetDirectory);
  1075. lg->GetMakefile()->SetCurrentSourceDirectory(targetDirectory);
  1076. gg->SetCurrentLocalGenerator(lg);
  1077. return gg;
  1078. }
  1079. bool cmQtAutoGenerators::Run(const std::string& targetDirectory,
  1080. const std::string& config)
  1081. {
  1082. bool success = true;
  1083. cmake cm;
  1084. cmGlobalGenerator* gg = CreateGlobalGenerator(&cm, targetDirectory);
  1085. cmMakefile* makefile = gg->GetCurrentLocalGenerator()->GetMakefile();
  1086. this->ReadAutogenInfoFile(makefile, targetDirectory, config);
  1087. this->ReadOldMocDefinitionsFile(makefile, targetDirectory);
  1088. this->Init();
  1089. if (this->QtMajorVersion == "4" || this->QtMajorVersion == "5")
  1090. {
  1091. success = this->RunAutogen(makefile);
  1092. }
  1093. this->WriteOldMocDefinitionsFile(targetDirectory);
  1094. delete gg->GetCurrentLocalGenerator();
  1095. delete gg;
  1096. gg = NULL;
  1097. makefile = NULL;
  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, &retVal);
  1965. if (!result || retVal)
  1966. {
  1967. std::cerr << "AUTOGEN: error: process for " << mocFilePath <<" failed:\n"
  1968. << output << std::endl;
  1969. this->RunMocFailed = true;
  1970. cmSystemTools::RemoveFile(mocFilePath);
  1971. }
  1972. return true;
  1973. }
  1974. return false;
  1975. }
  1976. bool cmQtAutoGenerators::GenerateUi(const std::string& realName,
  1977. const std::string& uiFileName)
  1978. {
  1979. if (!cmsys::SystemTools::FileExists(this->Builddir.c_str(), false))
  1980. {
  1981. cmsys::SystemTools::MakeDirectory(this->Builddir.c_str());
  1982. }
  1983. const std::string path = cmsys::SystemTools::GetFilenamePath(
  1984. realName) + '/';
  1985. std::string ui_output_file = "ui_" + uiFileName + ".h";
  1986. std::string ui_input_file = path + uiFileName + ".ui";
  1987. int sourceNewerThanUi = 0;
  1988. bool success = cmsys::SystemTools::FileTimeCompare(ui_input_file,
  1989. this->Builddir + ui_output_file,
  1990. &sourceNewerThanUi);
  1991. if (this->GenerateAll || !success || sourceNewerThanUi >= 0)
  1992. {
  1993. std::string msg = "Generating ";
  1994. msg += ui_output_file;
  1995. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue
  1996. |cmsysTerminal_Color_ForegroundBold,
  1997. msg.c_str(), true, this->ColorOutput);
  1998. std::vector<std::string> command;
  1999. command.push_back(this->UicExecutable);
  2000. std::vector<std::string> opts = this->UicTargetOptions;
  2001. std::map<std::string, std::string>::const_iterator optionIt
  2002. = this->UicOptions.find(ui_input_file);
  2003. if (optionIt != this->UicOptions.end())
  2004. {
  2005. std::vector<std::string> fileOpts;
  2006. cmSystemTools::ExpandListArgument(optionIt->second, fileOpts);
  2007. this->MergeUicOptions(opts, fileOpts, this->QtMajorVersion == "5");
  2008. }
  2009. command.insert(command.end(), opts.begin(), opts.end());
  2010. command.push_back("-o");
  2011. command.push_back(this->Builddir + ui_output_file);
  2012. command.push_back(ui_input_file);
  2013. if (this->Verbose)
  2014. {
  2015. for(std::vector<std::string>::const_iterator cmdIt = command.begin();
  2016. cmdIt != command.end();
  2017. ++cmdIt)
  2018. {
  2019. std::cout << *cmdIt << " ";
  2020. }
  2021. std::cout << std::endl;
  2022. }
  2023. std::string output;
  2024. int retVal = 0;
  2025. bool result = cmSystemTools::RunSingleCommand(command, &output, &retVal);
  2026. if (!result || retVal)
  2027. {
  2028. std::cerr << "AUTOUIC: error: process for " << ui_output_file <<
  2029. " needed by\n \"" << realName << "\"\nfailed:\n" << output
  2030. << std::endl;
  2031. this->RunUicFailed = true;
  2032. cmSystemTools::RemoveFile(ui_output_file);
  2033. return false;
  2034. }
  2035. return true;
  2036. }
  2037. return false;
  2038. }
  2039. bool cmQtAutoGenerators::InputFilesNewerThanQrc(const std::string& qrcFile,
  2040. const std::string& rccOutput)
  2041. {
  2042. std::vector<std::string> const& files = this->RccInputs[qrcFile];
  2043. for (std::vector<std::string>::const_iterator it = files.begin();
  2044. it != files.end(); ++it)
  2045. {
  2046. int inputNewerThanQrc = 0;
  2047. bool success = cmsys::SystemTools::FileTimeCompare(*it,
  2048. rccOutput,
  2049. &inputNewerThanQrc);
  2050. if (!success || inputNewerThanQrc >= 0)
  2051. {
  2052. return true;
  2053. }
  2054. }
  2055. return false;
  2056. }
  2057. bool cmQtAutoGenerators::GenerateQrc()
  2058. {
  2059. for(std::vector<std::string>::const_iterator si = this->RccSources.begin();
  2060. si != this->RccSources.end(); ++si)
  2061. {
  2062. std::string ext = cmsys::SystemTools::GetFilenameLastExtension(*si);
  2063. if (ext != ".qrc")
  2064. {
  2065. continue;
  2066. }
  2067. std::vector<std::string> command;
  2068. command.push_back(this->RccExecutable);
  2069. std::string basename = cmsys::SystemTools::
  2070. GetFilenameWithoutLastExtension(*si);
  2071. std::string rcc_output_file = this->Builddir
  2072. + "CMakeFiles/" + this->OriginTargetName
  2073. + ".dir/qrc_" + basename + ".cpp";
  2074. int sourceNewerThanQrc = 0;
  2075. bool generateQrc = !cmsys::SystemTools::FileTimeCompare(*si,
  2076. rcc_output_file,
  2077. &sourceNewerThanQrc);
  2078. generateQrc = generateQrc || (sourceNewerThanQrc >= 0);
  2079. generateQrc = generateQrc || this->InputFilesNewerThanQrc(*si,
  2080. rcc_output_file);
  2081. if (this->GenerateAll || generateQrc)
  2082. {
  2083. std::map<std::string, std::string>::const_iterator optionIt
  2084. = this->RccOptions.find(*si);
  2085. if (optionIt != this->RccOptions.end())
  2086. {
  2087. cmSystemTools::ExpandListArgument(optionIt->second, command);
  2088. }
  2089. command.push_back("-name");
  2090. command.push_back(basename);
  2091. command.push_back("-o");
  2092. command.push_back(rcc_output_file);
  2093. command.push_back(*si);
  2094. if (this->Verbose)
  2095. {
  2096. for(std::vector<std::string>::const_iterator cmdIt = command.begin();
  2097. cmdIt != command.end();
  2098. ++cmdIt)
  2099. {
  2100. std::cout << *cmdIt << " ";
  2101. }
  2102. std::cout << std::endl;
  2103. }
  2104. std::string output;
  2105. int retVal = 0;
  2106. bool result = cmSystemTools::RunSingleCommand(command, &output, &retVal);
  2107. if (!result || retVal)
  2108. {
  2109. std::cerr << "AUTORCC: error: process for " << rcc_output_file <<
  2110. " failed:\n" << output << std::endl;
  2111. this->RunRccFailed = true;
  2112. cmSystemTools::RemoveFile(rcc_output_file);
  2113. return false;
  2114. }
  2115. }
  2116. }
  2117. return true;
  2118. }
  2119. std::string cmQtAutoGenerators::Join(const std::vector<std::string>& lst,
  2120. char separator)
  2121. {
  2122. if (lst.empty())
  2123. {
  2124. return "";
  2125. }
  2126. std::string result;
  2127. for (std::vector<std::string>::const_iterator it = lst.begin();
  2128. it != lst.end();
  2129. ++it)
  2130. {
  2131. result += "." + (*it) + separator;
  2132. }
  2133. result.erase(result.end() - 1);
  2134. return result;
  2135. }
  2136. bool cmQtAutoGenerators::StartsWith(const std::string& str,
  2137. const std::string& with)
  2138. {
  2139. return (str.substr(0, with.length()) == with);
  2140. }
  2141. bool cmQtAutoGenerators::EndsWith(const std::string& str,
  2142. const std::string& with)
  2143. {
  2144. if (with.length() > (str.length()))
  2145. {
  2146. return false;
  2147. }
  2148. return (str.substr(str.length() - with.length(), with.length()) == with);
  2149. }