cmQtAutoGenerators.cxx 78 KB

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