cmQtAutoGenerators.cxx 79 KB

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