cmQtAutoGenerators.cxx 79 KB

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