cmQtAutoGenerators.cxx 78 KB

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