cmQtAutoGenerators.cxx 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426
  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. cm->SetHomeOutputDirectory(targetDirectory);
  1083. cm->SetHomeDirectory(targetDirectory);
  1084. cmLocalGenerator* lg = gg->CreateLocalGenerator();
  1085. lg->GetMakefile()->SetCurrentBinaryDirectory(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. cm.SetHomeOutputDirectory(targetDirectory);
  1096. cm.SetHomeDirectory(targetDirectory);
  1097. cmGlobalGenerator* gg = CreateGlobalGenerator(&cm, targetDirectory);
  1098. cmMakefile* makefile = gg->GetCurrentLocalGenerator()->GetMakefile();
  1099. this->ReadAutogenInfoFile(makefile, targetDirectory, config);
  1100. this->ReadOldMocDefinitionsFile(makefile, targetDirectory);
  1101. this->Init();
  1102. if (this->QtMajorVersion == "4" || this->QtMajorVersion == "5")
  1103. {
  1104. success = this->RunAutogen(makefile);
  1105. }
  1106. this->WriteOldMocDefinitionsFile(targetDirectory);
  1107. delete gg->GetCurrentLocalGenerator();
  1108. delete gg;
  1109. gg = NULL;
  1110. makefile = NULL;
  1111. return success;
  1112. }
  1113. bool cmQtAutoGenerators::ReadAutogenInfoFile(cmMakefile* makefile,
  1114. const std::string& targetDirectory,
  1115. const std::string& config)
  1116. {
  1117. std::string filename(
  1118. cmSystemTools::CollapseFullPath(targetDirectory));
  1119. cmSystemTools::ConvertToUnixSlashes(filename);
  1120. filename += "/AutogenInfo.cmake";
  1121. if (!makefile->ReadListFile(filename.c_str()))
  1122. {
  1123. cmSystemTools::Error("Error processing file: ", filename.c_str());
  1124. return false;
  1125. }
  1126. this->QtMajorVersion = makefile->GetSafeDefinition("AM_QT_VERSION_MAJOR");
  1127. if (this->QtMajorVersion == "")
  1128. {
  1129. this->QtMajorVersion = makefile->GetSafeDefinition(
  1130. "AM_Qt5Core_VERSION_MAJOR");
  1131. }
  1132. this->Sources = makefile->GetSafeDefinition("AM_SOURCES");
  1133. {
  1134. std::string rccSources = makefile->GetSafeDefinition("AM_RCC_SOURCES");
  1135. cmSystemTools::ExpandListArgument(rccSources, this->RccSources);
  1136. }
  1137. this->SkipMoc = makefile->GetSafeDefinition("AM_SKIP_MOC");
  1138. this->SkipUic = makefile->GetSafeDefinition("AM_SKIP_UIC");
  1139. this->Headers = makefile->GetSafeDefinition("AM_HEADERS");
  1140. this->IncludeProjectDirsBefore = makefile->IsOn(
  1141. "AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE");
  1142. this->Srcdir = makefile->GetSafeDefinition("AM_CMAKE_CURRENT_SOURCE_DIR");
  1143. this->Builddir = makefile->GetSafeDefinition("AM_CMAKE_CURRENT_BINARY_DIR");
  1144. this->MocExecutable = makefile->GetSafeDefinition("AM_QT_MOC_EXECUTABLE");
  1145. this->UicExecutable = makefile->GetSafeDefinition("AM_QT_UIC_EXECUTABLE");
  1146. this->RccExecutable = makefile->GetSafeDefinition("AM_QT_RCC_EXECUTABLE");
  1147. {
  1148. std::string compileDefsPropOrig = "AM_MOC_COMPILE_DEFINITIONS";
  1149. std::string compileDefsProp = compileDefsPropOrig;
  1150. if(!config.empty())
  1151. {
  1152. compileDefsProp += "_";
  1153. compileDefsProp += config;
  1154. }
  1155. const char *compileDefs = makefile->GetDefinition(compileDefsProp);
  1156. this->MocCompileDefinitionsStr = compileDefs ? compileDefs
  1157. : makefile->GetSafeDefinition(compileDefsPropOrig);
  1158. }
  1159. {
  1160. std::string includesPropOrig = "AM_MOC_INCLUDES";
  1161. std::string includesProp = includesPropOrig;
  1162. if(!config.empty())
  1163. {
  1164. includesProp += "_";
  1165. includesProp += config;
  1166. }
  1167. const char *includes = makefile->GetDefinition(includesProp);
  1168. this->MocIncludesStr = includes ? includes
  1169. : makefile->GetSafeDefinition(includesPropOrig);
  1170. }
  1171. this->MocOptionsStr = makefile->GetSafeDefinition("AM_MOC_OPTIONS");
  1172. this->ProjectBinaryDir = makefile->GetSafeDefinition("AM_CMAKE_BINARY_DIR");
  1173. this->ProjectSourceDir = makefile->GetSafeDefinition("AM_CMAKE_SOURCE_DIR");
  1174. this->TargetName = makefile->GetSafeDefinition("AM_TARGET_NAME");
  1175. this->OriginTargetName
  1176. = makefile->GetSafeDefinition("AM_ORIGIN_TARGET_NAME");
  1177. {
  1178. const char *uicOptionsFiles
  1179. = makefile->GetSafeDefinition("AM_UIC_OPTIONS_FILES");
  1180. std::string uicOptionsPropOrig = "AM_UIC_TARGET_OPTIONS";
  1181. std::string uicOptionsProp = uicOptionsPropOrig;
  1182. if(!config.empty())
  1183. {
  1184. uicOptionsProp += "_";
  1185. uicOptionsProp += config;
  1186. }
  1187. const char *uicTargetOptions
  1188. = makefile->GetSafeDefinition(uicOptionsProp);
  1189. cmSystemTools::ExpandListArgument(
  1190. uicTargetOptions ? uicTargetOptions
  1191. : makefile->GetSafeDefinition(uicOptionsPropOrig),
  1192. this->UicTargetOptions);
  1193. const char *uicOptionsOptions
  1194. = makefile->GetSafeDefinition("AM_UIC_OPTIONS_OPTIONS");
  1195. std::vector<std::string> uicFilesVec;
  1196. cmSystemTools::ExpandListArgument(uicOptionsFiles, uicFilesVec);
  1197. std::vector<std::string> uicOptionsVec;
  1198. cmSystemTools::ExpandListArgument(uicOptionsOptions, uicOptionsVec);
  1199. if (uicFilesVec.size() != uicOptionsVec.size())
  1200. {
  1201. return false;
  1202. }
  1203. for (std::vector<std::string>::iterator fileIt = uicFilesVec.begin(),
  1204. optionIt = uicOptionsVec.begin();
  1205. fileIt != uicFilesVec.end();
  1206. ++fileIt, ++optionIt)
  1207. {
  1208. cmSystemTools::ReplaceString(*optionIt, "@list_sep@", ";");
  1209. this->UicOptions[*fileIt] = *optionIt;
  1210. }
  1211. }
  1212. {
  1213. const char *rccOptionsFiles
  1214. = makefile->GetSafeDefinition("AM_RCC_OPTIONS_FILES");
  1215. const char *rccOptionsOptions
  1216. = makefile->GetSafeDefinition("AM_RCC_OPTIONS_OPTIONS");
  1217. std::vector<std::string> rccFilesVec;
  1218. cmSystemTools::ExpandListArgument(rccOptionsFiles, rccFilesVec);
  1219. std::vector<std::string> rccOptionsVec;
  1220. cmSystemTools::ExpandListArgument(rccOptionsOptions, rccOptionsVec);
  1221. if (rccFilesVec.size() != rccOptionsVec.size())
  1222. {
  1223. return false;
  1224. }
  1225. for (std::vector<std::string>::iterator fileIt = rccFilesVec.begin(),
  1226. optionIt = rccOptionsVec.begin();
  1227. fileIt != rccFilesVec.end();
  1228. ++fileIt, ++optionIt)
  1229. {
  1230. cmSystemTools::ReplaceString(*optionIt, "@list_sep@", ";");
  1231. this->RccOptions[*fileIt] = *optionIt;
  1232. }
  1233. const char *rccInputs = makefile->GetSafeDefinition("AM_RCC_INPUTS");
  1234. std::vector<std::string> rccInputLists;
  1235. cmSystemTools::ExpandListArgument(rccInputs, rccInputLists);
  1236. if (this->RccSources.size() != rccInputLists.size())
  1237. {
  1238. cmSystemTools::Error("Error processing file: ", filename.c_str());
  1239. return false;
  1240. }
  1241. for (std::vector<std::string>::iterator fileIt = this->RccSources.begin(),
  1242. inputIt = rccInputLists.begin();
  1243. fileIt != this->RccSources.end();
  1244. ++fileIt, ++inputIt)
  1245. {
  1246. cmSystemTools::ReplaceString(*inputIt, "@list_sep@", ";");
  1247. std::vector<std::string> rccInputFiles;
  1248. cmSystemTools::ExpandListArgument(*inputIt, rccInputFiles);
  1249. this->RccInputs[*fileIt] = rccInputFiles;
  1250. }
  1251. }
  1252. this->CurrentCompileSettingsStr = this->MakeCompileSettingsString(makefile);
  1253. this->RelaxedMode = makefile->IsOn("AM_RELAXED_MODE");
  1254. return true;
  1255. }
  1256. std::string cmQtAutoGenerators::MakeCompileSettingsString(cmMakefile* makefile)
  1257. {
  1258. std::string s;
  1259. s += makefile->GetSafeDefinition("AM_MOC_COMPILE_DEFINITIONS");
  1260. s += " ~~~ ";
  1261. s += makefile->GetSafeDefinition("AM_MOC_INCLUDES");
  1262. s += " ~~~ ";
  1263. s += makefile->GetSafeDefinition("AM_MOC_OPTIONS");
  1264. s += " ~~~ ";
  1265. s += makefile->IsOn("AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE") ? "TRUE"
  1266. : "FALSE";
  1267. s += " ~~~ ";
  1268. return s;
  1269. }
  1270. bool cmQtAutoGenerators::ReadOldMocDefinitionsFile(cmMakefile* makefile,
  1271. const std::string& targetDirectory)
  1272. {
  1273. std::string filename(
  1274. cmSystemTools::CollapseFullPath(targetDirectory));
  1275. cmSystemTools::ConvertToUnixSlashes(filename);
  1276. filename += "/AutomocOldMocDefinitions.cmake";
  1277. if (makefile->ReadListFile(filename.c_str()))
  1278. {
  1279. this->OldCompileSettingsStr =
  1280. makefile->GetSafeDefinition("AM_OLD_COMPILE_SETTINGS");
  1281. }
  1282. return true;
  1283. }
  1284. void
  1285. cmQtAutoGenerators::WriteOldMocDefinitionsFile(
  1286. const std::string& targetDirectory)
  1287. {
  1288. std::string filename(
  1289. cmSystemTools::CollapseFullPath(targetDirectory));
  1290. cmSystemTools::ConvertToUnixSlashes(filename);
  1291. filename += "/AutomocOldMocDefinitions.cmake";
  1292. cmsys::ofstream outfile;
  1293. outfile.open(filename.c_str(),
  1294. std::ios::trunc);
  1295. outfile << "set(AM_OLD_COMPILE_SETTINGS "
  1296. << cmLocalGenerator::EscapeForCMake(
  1297. this->CurrentCompileSettingsStr) << ")\n";
  1298. outfile.close();
  1299. }
  1300. void cmQtAutoGenerators::Init()
  1301. {
  1302. this->OutMocCppFilename = this->Builddir;
  1303. this->OutMocCppFilename += this->TargetName;
  1304. this->OutMocCppFilename += ".cpp";
  1305. std::vector<std::string> cdefList;
  1306. cmSystemTools::ExpandListArgument(this->MocCompileDefinitionsStr, cdefList);
  1307. for(std::vector<std::string>::const_iterator it = cdefList.begin();
  1308. it != cdefList.end();
  1309. ++it)
  1310. {
  1311. this->MocDefinitions.push_back("-D" + (*it));
  1312. }
  1313. cmSystemTools::ExpandListArgument(this->MocOptionsStr, this->MocOptions);
  1314. std::vector<std::string> incPaths;
  1315. cmSystemTools::ExpandListArgument(this->MocIncludesStr, incPaths);
  1316. std::set<std::string> frameworkPaths;
  1317. for(std::vector<std::string>::const_iterator it = incPaths.begin();
  1318. it != incPaths.end();
  1319. ++it)
  1320. {
  1321. const std::string &path = *it;
  1322. this->MocIncludes.push_back("-I" + path);
  1323. if (cmHasLiteralSuffix(path, ".framework/Headers"))
  1324. {
  1325. // Go up twice to get to the framework root
  1326. std::vector<std::string> pathComponents;
  1327. cmsys::SystemTools::SplitPath(path, pathComponents);
  1328. std::string frameworkPath =cmsys::SystemTools::JoinPath(
  1329. pathComponents.begin(), pathComponents.end() - 2);
  1330. frameworkPaths.insert(frameworkPath);
  1331. }
  1332. }
  1333. for (std::set<std::string>::const_iterator it = frameworkPaths.begin();
  1334. it != frameworkPaths.end(); ++it)
  1335. {
  1336. this->MocIncludes.push_back("-F");
  1337. this->MocIncludes.push_back(*it);
  1338. }
  1339. if (this->IncludeProjectDirsBefore)
  1340. {
  1341. const std::string binDir = "-I" + this->ProjectBinaryDir;
  1342. const std::string srcDir = "-I" + this->ProjectSourceDir;
  1343. std::list<std::string> sortedMocIncludes;
  1344. std::list<std::string>::iterator it = this->MocIncludes.begin();
  1345. while (it != this->MocIncludes.end())
  1346. {
  1347. if (this->StartsWith(*it, binDir))
  1348. {
  1349. sortedMocIncludes.push_back(*it);
  1350. it = this->MocIncludes.erase(it);
  1351. }
  1352. else
  1353. {
  1354. ++it;
  1355. }
  1356. }
  1357. it = this->MocIncludes.begin();
  1358. while (it != this->MocIncludes.end())
  1359. {
  1360. if (this->StartsWith(*it, srcDir))
  1361. {
  1362. sortedMocIncludes.push_back(*it);
  1363. it = this->MocIncludes.erase(it);
  1364. }
  1365. else
  1366. {
  1367. ++it;
  1368. }
  1369. }
  1370. sortedMocIncludes.insert(sortedMocIncludes.end(),
  1371. this->MocIncludes.begin(), this->MocIncludes.end());
  1372. this->MocIncludes = sortedMocIncludes;
  1373. }
  1374. }
  1375. bool cmQtAutoGenerators::RunAutogen(cmMakefile* makefile)
  1376. {
  1377. if (!cmsys::SystemTools::FileExists(this->OutMocCppFilename.c_str())
  1378. || (this->OldCompileSettingsStr != this->CurrentCompileSettingsStr))
  1379. {
  1380. this->GenerateAll = true;
  1381. }
  1382. // the program goes through all .cpp files to see which moc files are
  1383. // included. It is not really interesting how the moc file is named, but
  1384. // what file the moc is created from. Once a moc is included the same moc
  1385. // may not be included in the _automoc.cpp file anymore. OTOH if there's a
  1386. // header containing Q_OBJECT where no corresponding moc file is included
  1387. // anywhere a moc_<filename>.cpp file is created and included in
  1388. // the _automoc.cpp file.
  1389. // key = moc source filepath, value = moc output filepath
  1390. std::map<std::string, std::string> includedMocs;
  1391. // collect all headers which may need to be mocced
  1392. std::set<std::string> headerFiles;
  1393. std::vector<std::string> sourceFiles;
  1394. cmSystemTools::ExpandListArgument(this->Sources, sourceFiles);
  1395. const std::vector<std::string>& headerExtensions =
  1396. makefile->GetHeaderExtensions();
  1397. std::map<std::string, std::vector<std::string> > includedUis;
  1398. std::map<std::string, std::vector<std::string> > skippedUis;
  1399. std::vector<std::string> uicSkipped;
  1400. cmSystemTools::ExpandListArgument(this->SkipUic, uicSkipped);
  1401. for (std::vector<std::string>::const_iterator it = sourceFiles.begin();
  1402. it != sourceFiles.end();
  1403. ++it)
  1404. {
  1405. const bool skipUic = std::find(uicSkipped.begin(), uicSkipped.end(), *it)
  1406. != uicSkipped.end();
  1407. std::map<std::string, std::vector<std::string> >& uiFiles
  1408. = skipUic ? skippedUis : includedUis;
  1409. const std::string &absFilename = *it;
  1410. if (this->Verbose)
  1411. {
  1412. std::cout << "AUTOGEN: Checking " << absFilename << std::endl;
  1413. }
  1414. if (this->RelaxedMode)
  1415. {
  1416. this->ParseCppFile(absFilename, headerExtensions, includedMocs,
  1417. uiFiles);
  1418. }
  1419. else
  1420. {
  1421. this->StrictParseCppFile(absFilename, headerExtensions, includedMocs,
  1422. uiFiles);
  1423. }
  1424. this->SearchHeadersForCppFile(absFilename, headerExtensions, headerFiles);
  1425. }
  1426. {
  1427. std::vector<std::string> mocSkipped;
  1428. cmSystemTools::ExpandListArgument(this->SkipMoc, mocSkipped);
  1429. for (std::vector<std::string>::const_iterator it = mocSkipped.begin();
  1430. it != mocSkipped.end();
  1431. ++it)
  1432. {
  1433. if (std::find(uicSkipped.begin(), uicSkipped.end(), *it)
  1434. != uicSkipped.end())
  1435. {
  1436. const std::string &absFilename = *it;
  1437. if (this->Verbose)
  1438. {
  1439. std::cout << "AUTOGEN: Checking " << absFilename << std::endl;
  1440. }
  1441. this->ParseForUic(absFilename, includedUis);
  1442. }
  1443. }
  1444. }
  1445. std::vector<std::string> headerFilesVec;
  1446. cmSystemTools::ExpandListArgument(this->Headers, headerFilesVec);
  1447. headerFiles.insert(headerFilesVec.begin(), headerFilesVec.end());
  1448. // key = moc source filepath, value = moc output filename
  1449. std::map<std::string, std::string> notIncludedMocs;
  1450. this->ParseHeaders(headerFiles, includedMocs, notIncludedMocs, includedUis);
  1451. // run moc on all the moc's that are #included in source files
  1452. for(std::map<std::string, std::string>::const_iterator
  1453. it = includedMocs.begin();
  1454. it != includedMocs.end();
  1455. ++it)
  1456. {
  1457. this->GenerateMoc(it->first, it->second);
  1458. }
  1459. for(std::map<std::string, std::vector<std::string> >::const_iterator
  1460. it = includedUis.begin();
  1461. it != includedUis.end();
  1462. ++it)
  1463. {
  1464. for (std::vector<std::string>::const_iterator nit = it->second.begin();
  1465. nit != it->second.end();
  1466. ++nit)
  1467. {
  1468. this->GenerateUi(it->first, *nit);
  1469. }
  1470. }
  1471. if(!this->RccExecutable.empty())
  1472. {
  1473. this->GenerateQrc();
  1474. }
  1475. cmsys_ios::stringstream outStream;
  1476. outStream << "/* This file is autogenerated, do not edit*/\n";
  1477. bool automocCppChanged = false;
  1478. if (notIncludedMocs.empty())
  1479. {
  1480. outStream << "enum some_compilers { need_more_than_nothing };\n";
  1481. }
  1482. else
  1483. {
  1484. // run moc on the remaining headers and include them in
  1485. // the _automoc.cpp file
  1486. for(std::map<std::string, std::string>::const_iterator
  1487. it = notIncludedMocs.begin();
  1488. it != notIncludedMocs.end();
  1489. ++it)
  1490. {
  1491. bool mocSuccess = this->GenerateMoc(it->first, it->second);
  1492. if (mocSuccess)
  1493. {
  1494. automocCppChanged = true;
  1495. }
  1496. outStream << "#include \"" << it->second << "\"\n";
  1497. }
  1498. }
  1499. if (this->RunMocFailed)
  1500. {
  1501. std::cerr << "moc failed..." << std::endl;
  1502. return false;
  1503. }
  1504. if (this->RunUicFailed)
  1505. {
  1506. std::cerr << "uic failed..." << std::endl;
  1507. return false;
  1508. }
  1509. if (this->RunRccFailed)
  1510. {
  1511. std::cerr << "rcc failed..." << std::endl;
  1512. return false;
  1513. }
  1514. outStream.flush();
  1515. std::string automocSource = outStream.str();
  1516. if (!automocCppChanged)
  1517. {
  1518. // compare contents of the _automoc.cpp file
  1519. const std::string oldContents = ReadAll(this->OutMocCppFilename);
  1520. if (oldContents == automocSource)
  1521. {
  1522. // nothing changed: don't touch the _automoc.cpp file
  1523. return true;
  1524. }
  1525. }
  1526. // source file that includes all remaining moc files (_automoc.cpp file)
  1527. cmsys::ofstream outfile;
  1528. outfile.open(this->OutMocCppFilename.c_str(),
  1529. std::ios::trunc);
  1530. outfile << automocSource;
  1531. outfile.close();
  1532. return true;
  1533. }
  1534. void cmQtAutoGenerators::ParseCppFile(const std::string& absFilename,
  1535. const std::vector<std::string>& headerExtensions,
  1536. std::map<std::string, std::string>& includedMocs,
  1537. std::map<std::string, std::vector<std::string> > &includedUis)
  1538. {
  1539. cmsys::RegularExpression mocIncludeRegExp(
  1540. "[\n][ \t]*#[ \t]*include[ \t]+"
  1541. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  1542. const std::string contentsString = ReadAll(absFilename);
  1543. if (contentsString.empty())
  1544. {
  1545. std::cerr << "AUTOGEN: warning: " << absFilename << ": file is empty\n"
  1546. << std::endl;
  1547. return;
  1548. }
  1549. this->ParseForUic(absFilename, contentsString, includedUis);
  1550. if (this->MocExecutable.empty())
  1551. {
  1552. return;
  1553. }
  1554. const std::string absPath = cmsys::SystemTools::GetFilenamePath(
  1555. cmsys::SystemTools::GetRealPath(absFilename)) + '/';
  1556. const std::string scannedFileBasename = cmsys::SystemTools::
  1557. GetFilenameWithoutLastExtension(absFilename);
  1558. std::string macroName;
  1559. const bool requiresMoc = requiresMocing(contentsString, macroName);
  1560. bool dotMocIncluded = false;
  1561. bool mocUnderscoreIncluded = false;
  1562. std::string ownMocUnderscoreFile;
  1563. std::string ownDotMocFile;
  1564. std::string ownMocHeaderFile;
  1565. std::string::size_type matchOffset = 0;
  1566. // first a simple string check for "moc" is *much* faster than the regexp,
  1567. // and if the string search already fails, we don't have to try the
  1568. // expensive regexp
  1569. if ((strstr(contentsString.c_str(), "moc") != NULL)
  1570. && (mocIncludeRegExp.find(contentsString)))
  1571. {
  1572. // for every moc include in the file
  1573. do
  1574. {
  1575. const std::string currentMoc = mocIncludeRegExp.match(1);
  1576. //std::cout << "found moc include: " << currentMoc << std::endl;
  1577. std::string basename = cmsys::SystemTools::
  1578. GetFilenameWithoutLastExtension(currentMoc);
  1579. const bool moc_style = cmHasLiteralPrefix(basename, "moc_");
  1580. // If the moc include is of the moc_foo.cpp style we expect
  1581. // the Q_OBJECT class declaration in a header file.
  1582. // If the moc include is of the foo.moc style we need to look for
  1583. // a Q_OBJECT macro in the current source file, if it contains the
  1584. // macro we generate the moc file from the source file.
  1585. // Q_OBJECT
  1586. if (moc_style)
  1587. {
  1588. // basename should be the part of the moc filename used for
  1589. // finding the correct header, so we need to remove the moc_ part
  1590. basename = basename.substr(4);
  1591. std::string mocSubDir = extractSubDir(absPath, currentMoc);
  1592. std::string headerToMoc = findMatchingHeader(
  1593. absPath, mocSubDir, basename, headerExtensions);
  1594. if (!headerToMoc.empty())
  1595. {
  1596. includedMocs[headerToMoc] = currentMoc;
  1597. if (basename == scannedFileBasename)
  1598. {
  1599. mocUnderscoreIncluded = true;
  1600. ownMocUnderscoreFile = currentMoc;
  1601. ownMocHeaderFile = headerToMoc;
  1602. }
  1603. }
  1604. else
  1605. {
  1606. std::cerr << "AUTOGEN: error: " << absFilename << ": The file "
  1607. << "includes the moc file \"" << currentMoc << "\", "
  1608. << "but could not find header \"" << basename
  1609. << '{' << this->Join(headerExtensions, ',') << "}\" ";
  1610. if (mocSubDir.empty())
  1611. {
  1612. std::cerr << "in " << absPath << "\n" << std::endl;
  1613. }
  1614. else
  1615. {
  1616. std::cerr << "neither in " << absPath
  1617. << " nor in " << mocSubDir << "\n" << std::endl;
  1618. }
  1619. ::exit(EXIT_FAILURE);
  1620. }
  1621. }
  1622. else
  1623. {
  1624. std::string fileToMoc = absFilename;
  1625. if ((basename != scannedFileBasename) || (requiresMoc==false))
  1626. {
  1627. std::string mocSubDir = extractSubDir(absPath, currentMoc);
  1628. std::string headerToMoc = findMatchingHeader(
  1629. absPath, mocSubDir, basename, headerExtensions);
  1630. if (!headerToMoc.empty())
  1631. {
  1632. // this is for KDE4 compatibility:
  1633. fileToMoc = headerToMoc;
  1634. if ((requiresMoc==false) &&(basename==scannedFileBasename))
  1635. {
  1636. std::cerr << "AUTOGEN: warning: " << absFilename << ": The file "
  1637. "includes the moc file \"" << currentMoc <<
  1638. "\", but does not contain a " << macroName
  1639. << " macro. Running moc on "
  1640. << "\"" << headerToMoc << "\" ! Include \"moc_"
  1641. << basename << ".cpp\" for a compatiblity with "
  1642. "strict mode (see CMAKE_AUTOMOC_RELAXED_MODE).\n"
  1643. << std::endl;
  1644. }
  1645. else
  1646. {
  1647. std::cerr << "AUTOGEN: warning: " << absFilename << ": The file "
  1648. "includes the moc file \"" << currentMoc <<
  1649. "\" instead of \"moc_" << basename << ".cpp\". "
  1650. "Running moc on "
  1651. << "\"" << headerToMoc << "\" ! Include \"moc_"
  1652. << basename << ".cpp\" for compatiblity with "
  1653. "strict mode (see CMAKE_AUTOMOC_RELAXED_MODE).\n"
  1654. << std::endl;
  1655. }
  1656. }
  1657. else
  1658. {
  1659. std::cerr <<"AUTOGEN: error: " << absFilename << ": The file "
  1660. "includes the moc file \"" << currentMoc <<
  1661. "\", which seems to be the moc file from a different "
  1662. "source file. CMake also could not find a matching "
  1663. "header.\n" << std::endl;
  1664. ::exit(EXIT_FAILURE);
  1665. }
  1666. }
  1667. else
  1668. {
  1669. dotMocIncluded = true;
  1670. ownDotMocFile = currentMoc;
  1671. }
  1672. includedMocs[fileToMoc] = currentMoc;
  1673. }
  1674. matchOffset += mocIncludeRegExp.end();
  1675. } while(mocIncludeRegExp.find(contentsString.c_str() + matchOffset));
  1676. }
  1677. // In this case, check whether the scanned file itself contains a Q_OBJECT.
  1678. // If this is the case, the moc_foo.cpp should probably be generated from
  1679. // foo.cpp instead of foo.h, because otherwise it won't build.
  1680. // But warn, since this is not how it is supposed to be used.
  1681. if ((dotMocIncluded == false) && (requiresMoc == true))
  1682. {
  1683. if (mocUnderscoreIncluded == true)
  1684. {
  1685. // this is for KDE4 compatibility:
  1686. std::cerr << "AUTOGEN: warning: " << absFilename << ": The file "
  1687. << "contains a " << macroName << " macro, but does not "
  1688. "include "
  1689. << "\"" << scannedFileBasename << ".moc\", but instead "
  1690. "includes "
  1691. << "\"" << ownMocUnderscoreFile << "\". Running moc on "
  1692. << "\"" << absFilename << "\" ! Better include \""
  1693. << scannedFileBasename << ".moc\" for compatiblity with "
  1694. "strict mode (see CMAKE_AUTOMOC_RELAXED_MODE).\n"
  1695. << std::endl;
  1696. includedMocs[absFilename] = ownMocUnderscoreFile;
  1697. includedMocs.erase(ownMocHeaderFile);
  1698. }
  1699. else
  1700. {
  1701. // otherwise always error out since it will not compile:
  1702. std::cerr << "AUTOGEN: error: " << absFilename << ": The file "
  1703. << "contains a " << macroName << " macro, but does not "
  1704. "include "
  1705. << "\"" << scannedFileBasename << ".moc\" !\n"
  1706. << std::endl;
  1707. ::exit(EXIT_FAILURE);
  1708. }
  1709. }
  1710. }
  1711. void cmQtAutoGenerators::StrictParseCppFile(const std::string& absFilename,
  1712. const std::vector<std::string>& headerExtensions,
  1713. std::map<std::string, std::string>& includedMocs,
  1714. std::map<std::string, std::vector<std::string> >& includedUis)
  1715. {
  1716. cmsys::RegularExpression mocIncludeRegExp(
  1717. "[\n][ \t]*#[ \t]*include[ \t]+"
  1718. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  1719. const std::string contentsString = ReadAll(absFilename);
  1720. if (contentsString.empty())
  1721. {
  1722. std::cerr << "AUTOGEN: warning: " << absFilename << ": file is empty\n"
  1723. << std::endl;
  1724. return;
  1725. }
  1726. this->ParseForUic(absFilename, contentsString, includedUis);
  1727. if (this->MocExecutable.empty())
  1728. {
  1729. return;
  1730. }
  1731. const std::string absPath = cmsys::SystemTools::GetFilenamePath(
  1732. cmsys::SystemTools::GetRealPath(absFilename)) + '/';
  1733. const std::string scannedFileBasename = cmsys::SystemTools::
  1734. GetFilenameWithoutLastExtension(absFilename);
  1735. bool dotMocIncluded = false;
  1736. std::string::size_type matchOffset = 0;
  1737. // first a simple string check for "moc" is *much* faster than the regexp,
  1738. // and if the string search already fails, we don't have to try the
  1739. // expensive regexp
  1740. if ((strstr(contentsString.c_str(), "moc") != NULL)
  1741. && (mocIncludeRegExp.find(contentsString)))
  1742. {
  1743. // for every moc include in the file
  1744. do
  1745. {
  1746. const std::string currentMoc = mocIncludeRegExp.match(1);
  1747. std::string basename = cmsys::SystemTools::
  1748. GetFilenameWithoutLastExtension(currentMoc);
  1749. const bool mocUnderscoreStyle = cmHasLiteralPrefix(basename, "moc_");
  1750. // If the moc include is of the moc_foo.cpp style we expect
  1751. // the Q_OBJECT class declaration in a header file.
  1752. // If the moc include is of the foo.moc style we need to look for
  1753. // a Q_OBJECT macro in the current source file, if it contains the
  1754. // macro we generate the moc file from the source file.
  1755. if (mocUnderscoreStyle)
  1756. {
  1757. // basename should be the part of the moc filename used for
  1758. // finding the correct header, so we need to remove the moc_ part
  1759. basename = basename.substr(4);
  1760. std::string mocSubDir = extractSubDir(absPath, currentMoc);
  1761. std::string headerToMoc = findMatchingHeader(
  1762. absPath, mocSubDir, basename, headerExtensions);
  1763. if (!headerToMoc.empty())
  1764. {
  1765. includedMocs[headerToMoc] = currentMoc;
  1766. }
  1767. else
  1768. {
  1769. std::cerr << "AUTOGEN: error: " << absFilename << " The file "
  1770. << "includes the moc file \"" << currentMoc << "\", "
  1771. << "but could not find header \"" << basename
  1772. << '{' << this->Join(headerExtensions, ',') << "}\" ";
  1773. if (mocSubDir.empty())
  1774. {
  1775. std::cerr << "in " << absPath << "\n" << std::endl;
  1776. }
  1777. else
  1778. {
  1779. std::cerr << "neither in " << absPath
  1780. << " nor in " << mocSubDir << "\n" << std::endl;
  1781. }
  1782. ::exit(EXIT_FAILURE);
  1783. }
  1784. }
  1785. else
  1786. {
  1787. if (basename != scannedFileBasename)
  1788. {
  1789. std::cerr <<"AUTOGEN: error: " << absFilename << ": The file "
  1790. "includes the moc file \"" << currentMoc <<
  1791. "\", which seems to be the moc file from a different "
  1792. "source file. This is not supported. "
  1793. "Include \"" << scannedFileBasename << ".moc\" to run "
  1794. "moc on this source file.\n" << std::endl;
  1795. ::exit(EXIT_FAILURE);
  1796. }
  1797. dotMocIncluded = true;
  1798. includedMocs[absFilename] = currentMoc;
  1799. }
  1800. matchOffset += mocIncludeRegExp.end();
  1801. } while(mocIncludeRegExp.find(contentsString.c_str() + matchOffset));
  1802. }
  1803. // In this case, check whether the scanned file itself contains a Q_OBJECT.
  1804. // If this is the case, the moc_foo.cpp should probably be generated from
  1805. // foo.cpp instead of foo.h, because otherwise it won't build.
  1806. // But warn, since this is not how it is supposed to be used.
  1807. std::string macroName;
  1808. if ((dotMocIncluded == false) && (requiresMocing(contentsString,
  1809. macroName)))
  1810. {
  1811. // otherwise always error out since it will not compile:
  1812. std::cerr << "AUTOGEN: error: " << absFilename << ": The file "
  1813. << "contains a " << macroName << " macro, but does not include "
  1814. << "\"" << scannedFileBasename << ".moc\" !\n"
  1815. << std::endl;
  1816. ::exit(EXIT_FAILURE);
  1817. }
  1818. }
  1819. void cmQtAutoGenerators::ParseForUic(const std::string& absFilename,
  1820. std::map<std::string, std::vector<std::string> >& includedUis)
  1821. {
  1822. if (this->UicExecutable.empty())
  1823. {
  1824. return;
  1825. }
  1826. const std::string contentsString = ReadAll(absFilename);
  1827. if (contentsString.empty())
  1828. {
  1829. std::cerr << "AUTOGEN: warning: " << absFilename << ": file is empty\n"
  1830. << std::endl;
  1831. return;
  1832. }
  1833. this->ParseForUic(absFilename, contentsString, includedUis);
  1834. }
  1835. void cmQtAutoGenerators::ParseForUic(const std::string& absFilename,
  1836. const std::string& contentsString,
  1837. std::map<std::string, std::vector<std::string> >& includedUis)
  1838. {
  1839. if (this->UicExecutable.empty())
  1840. {
  1841. return;
  1842. }
  1843. cmsys::RegularExpression uiIncludeRegExp(
  1844. "[\n][ \t]*#[ \t]*include[ \t]+"
  1845. "[\"<](([^ \">]+/)?ui_[^ \">/]+\\.h)[\">]");
  1846. std::string::size_type matchOffset = 0;
  1847. const std::string realName =
  1848. cmsys::SystemTools::GetRealPath(absFilename);
  1849. matchOffset = 0;
  1850. if ((strstr(contentsString.c_str(), "ui_") != NULL)
  1851. && (uiIncludeRegExp.find(contentsString)))
  1852. {
  1853. do
  1854. {
  1855. const std::string currentUi = uiIncludeRegExp.match(1);
  1856. std::string basename = cmsys::SystemTools::
  1857. GetFilenameWithoutLastExtension(currentUi);
  1858. // basename should be the part of the ui filename used for
  1859. // finding the correct header, so we need to remove the ui_ part
  1860. basename = basename.substr(3);
  1861. includedUis[realName].push_back(basename);
  1862. matchOffset += uiIncludeRegExp.end();
  1863. } while(uiIncludeRegExp.find(contentsString.c_str() + matchOffset));
  1864. }
  1865. }
  1866. void
  1867. cmQtAutoGenerators::SearchHeadersForCppFile(const std::string& absFilename,
  1868. const std::vector<std::string>& headerExtensions,
  1869. std::set<std::string>& absHeaders)
  1870. {
  1871. // search for header files and private header files we may need to moc:
  1872. const std::string basename =
  1873. cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename);
  1874. const std::string absPath = cmsys::SystemTools::GetFilenamePath(
  1875. cmsys::SystemTools::GetRealPath(absFilename)) + '/';
  1876. for(std::vector<std::string>::const_iterator ext = headerExtensions.begin();
  1877. ext != headerExtensions.end();
  1878. ++ext)
  1879. {
  1880. const std::string headerName = absPath + basename + "." + (*ext);
  1881. if (cmsys::SystemTools::FileExists(headerName.c_str()))
  1882. {
  1883. absHeaders.insert(headerName);
  1884. break;
  1885. }
  1886. }
  1887. for(std::vector<std::string>::const_iterator ext = headerExtensions.begin();
  1888. ext != headerExtensions.end();
  1889. ++ext)
  1890. {
  1891. const std::string privateHeaderName = absPath+basename+"_p."+(*ext);
  1892. if (cmsys::SystemTools::FileExists(privateHeaderName.c_str()))
  1893. {
  1894. absHeaders.insert(privateHeaderName);
  1895. break;
  1896. }
  1897. }
  1898. }
  1899. void cmQtAutoGenerators::ParseHeaders(const std::set<std::string>& absHeaders,
  1900. const std::map<std::string, std::string>& includedMocs,
  1901. std::map<std::string, std::string>& notIncludedMocs,
  1902. std::map<std::string, std::vector<std::string> >& includedUis)
  1903. {
  1904. for(std::set<std::string>::const_iterator hIt=absHeaders.begin();
  1905. hIt!=absHeaders.end();
  1906. ++hIt)
  1907. {
  1908. const std::string& headerName = *hIt;
  1909. const std::string contents = ReadAll(headerName);
  1910. if (!this->MocExecutable.empty()
  1911. && includedMocs.find(headerName) == includedMocs.end())
  1912. {
  1913. if (this->Verbose)
  1914. {
  1915. std::cout << "AUTOGEN: Checking " << headerName << std::endl;
  1916. }
  1917. const std::string basename = cmsys::SystemTools::
  1918. GetFilenameWithoutLastExtension(headerName);
  1919. const std::string currentMoc = "moc_" + basename + ".cpp";
  1920. std::string macroName;
  1921. if (requiresMocing(contents, macroName))
  1922. {
  1923. //std::cout << "header contains Q_OBJECT macro";
  1924. notIncludedMocs[headerName] = currentMoc;
  1925. }
  1926. }
  1927. this->ParseForUic(headerName, contents, includedUis);
  1928. }
  1929. }
  1930. bool cmQtAutoGenerators::GenerateMoc(const std::string& sourceFile,
  1931. const std::string& mocFileName)
  1932. {
  1933. const std::string mocFilePath = this->Builddir + mocFileName;
  1934. int sourceNewerThanMoc = 0;
  1935. bool success = cmsys::SystemTools::FileTimeCompare(sourceFile,
  1936. mocFilePath,
  1937. &sourceNewerThanMoc);
  1938. if (this->GenerateAll || !success || sourceNewerThanMoc >= 0)
  1939. {
  1940. // make sure the directory for the resulting moc file exists
  1941. std::string mocDir = mocFilePath.substr(0, mocFilePath.rfind('/'));
  1942. if (!cmsys::SystemTools::FileExists(mocDir.c_str(), false))
  1943. {
  1944. cmsys::SystemTools::MakeDirectory(mocDir.c_str());
  1945. }
  1946. std::string msg = "Generating ";
  1947. msg += mocFileName;
  1948. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue
  1949. |cmsysTerminal_Color_ForegroundBold,
  1950. msg.c_str(), true, this->ColorOutput);
  1951. std::vector<std::string> command;
  1952. command.push_back(this->MocExecutable);
  1953. command.insert(command.end(),
  1954. this->MocIncludes.begin(), this->MocIncludes.end());
  1955. command.insert(command.end(),
  1956. this->MocDefinitions.begin(), this->MocDefinitions.end());
  1957. command.insert(command.end(),
  1958. this->MocOptions.begin(), this->MocOptions.end());
  1959. #ifdef _WIN32
  1960. command.push_back("-DWIN32");
  1961. #endif
  1962. command.push_back("-o");
  1963. command.push_back(mocFilePath);
  1964. command.push_back(sourceFile);
  1965. if (this->Verbose)
  1966. {
  1967. for(std::vector<std::string>::const_iterator cmdIt = command.begin();
  1968. cmdIt != command.end();
  1969. ++cmdIt)
  1970. {
  1971. std::cout << *cmdIt << " ";
  1972. }
  1973. std::cout << std::endl;
  1974. }
  1975. std::string output;
  1976. int retVal = 0;
  1977. bool result = cmSystemTools::RunSingleCommand(command, &output, &output,
  1978. &retVal);
  1979. if (!result || retVal)
  1980. {
  1981. std::cerr << "AUTOGEN: error: process for " << mocFilePath <<" failed:\n"
  1982. << output << std::endl;
  1983. this->RunMocFailed = true;
  1984. cmSystemTools::RemoveFile(mocFilePath);
  1985. }
  1986. return true;
  1987. }
  1988. return false;
  1989. }
  1990. bool cmQtAutoGenerators::GenerateUi(const std::string& realName,
  1991. const std::string& uiFileName)
  1992. {
  1993. if (!cmsys::SystemTools::FileExists(this->Builddir.c_str(), false))
  1994. {
  1995. cmsys::SystemTools::MakeDirectory(this->Builddir.c_str());
  1996. }
  1997. const std::string path = cmsys::SystemTools::GetFilenamePath(
  1998. realName) + '/';
  1999. std::string ui_output_file = "ui_" + uiFileName + ".h";
  2000. std::string ui_input_file = path + uiFileName + ".ui";
  2001. int sourceNewerThanUi = 0;
  2002. bool success = cmsys::SystemTools::FileTimeCompare(ui_input_file,
  2003. this->Builddir + ui_output_file,
  2004. &sourceNewerThanUi);
  2005. if (this->GenerateAll || !success || sourceNewerThanUi >= 0)
  2006. {
  2007. std::string msg = "Generating ";
  2008. msg += ui_output_file;
  2009. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue
  2010. |cmsysTerminal_Color_ForegroundBold,
  2011. msg.c_str(), true, this->ColorOutput);
  2012. std::vector<std::string> command;
  2013. command.push_back(this->UicExecutable);
  2014. std::vector<std::string> opts = this->UicTargetOptions;
  2015. std::map<std::string, std::string>::const_iterator optionIt
  2016. = this->UicOptions.find(ui_input_file);
  2017. if (optionIt != this->UicOptions.end())
  2018. {
  2019. std::vector<std::string> fileOpts;
  2020. cmSystemTools::ExpandListArgument(optionIt->second, fileOpts);
  2021. this->MergeUicOptions(opts, fileOpts, this->QtMajorVersion == "5");
  2022. }
  2023. command.insert(command.end(), opts.begin(), opts.end());
  2024. command.push_back("-o");
  2025. command.push_back(this->Builddir + ui_output_file);
  2026. command.push_back(ui_input_file);
  2027. if (this->Verbose)
  2028. {
  2029. for(std::vector<std::string>::const_iterator cmdIt = command.begin();
  2030. cmdIt != command.end();
  2031. ++cmdIt)
  2032. {
  2033. std::cout << *cmdIt << " ";
  2034. }
  2035. std::cout << std::endl;
  2036. }
  2037. std::string output;
  2038. int retVal = 0;
  2039. bool result = cmSystemTools::RunSingleCommand(command, &output, &output,
  2040. &retVal);
  2041. if (!result || retVal)
  2042. {
  2043. std::cerr << "AUTOUIC: error: process for " << ui_output_file <<
  2044. " needed by\n \"" << realName << "\"\nfailed:\n" << output
  2045. << std::endl;
  2046. this->RunUicFailed = true;
  2047. cmSystemTools::RemoveFile(ui_output_file);
  2048. return false;
  2049. }
  2050. return true;
  2051. }
  2052. return false;
  2053. }
  2054. bool cmQtAutoGenerators::InputFilesNewerThanQrc(const std::string& qrcFile,
  2055. const std::string& rccOutput)
  2056. {
  2057. std::vector<std::string> const& files = this->RccInputs[qrcFile];
  2058. for (std::vector<std::string>::const_iterator it = files.begin();
  2059. it != files.end(); ++it)
  2060. {
  2061. int inputNewerThanQrc = 0;
  2062. bool success = cmsys::SystemTools::FileTimeCompare(*it,
  2063. rccOutput,
  2064. &inputNewerThanQrc);
  2065. if (!success || inputNewerThanQrc >= 0)
  2066. {
  2067. return true;
  2068. }
  2069. }
  2070. return false;
  2071. }
  2072. bool cmQtAutoGenerators::GenerateQrc()
  2073. {
  2074. for(std::vector<std::string>::const_iterator si = this->RccSources.begin();
  2075. si != this->RccSources.end(); ++si)
  2076. {
  2077. std::string ext = cmsys::SystemTools::GetFilenameLastExtension(*si);
  2078. if (ext != ".qrc")
  2079. {
  2080. continue;
  2081. }
  2082. std::vector<std::string> command;
  2083. command.push_back(this->RccExecutable);
  2084. std::string basename = cmsys::SystemTools::
  2085. GetFilenameWithoutLastExtension(*si);
  2086. std::string rcc_output_file = this->Builddir
  2087. + "CMakeFiles/" + this->OriginTargetName
  2088. + ".dir/qrc_" + basename + ".cpp";
  2089. int sourceNewerThanQrc = 0;
  2090. bool generateQrc = !cmsys::SystemTools::FileTimeCompare(*si,
  2091. rcc_output_file,
  2092. &sourceNewerThanQrc);
  2093. generateQrc = generateQrc || (sourceNewerThanQrc >= 0);
  2094. generateQrc = generateQrc || this->InputFilesNewerThanQrc(*si,
  2095. rcc_output_file);
  2096. if (this->GenerateAll || generateQrc)
  2097. {
  2098. std::map<std::string, std::string>::const_iterator optionIt
  2099. = this->RccOptions.find(*si);
  2100. if (optionIt != this->RccOptions.end())
  2101. {
  2102. cmSystemTools::ExpandListArgument(optionIt->second, command);
  2103. }
  2104. command.push_back("-name");
  2105. command.push_back(basename);
  2106. command.push_back("-o");
  2107. command.push_back(rcc_output_file);
  2108. command.push_back(*si);
  2109. if (this->Verbose)
  2110. {
  2111. for(std::vector<std::string>::const_iterator cmdIt = command.begin();
  2112. cmdIt != command.end();
  2113. ++cmdIt)
  2114. {
  2115. std::cout << *cmdIt << " ";
  2116. }
  2117. std::cout << std::endl;
  2118. }
  2119. std::string output;
  2120. int retVal = 0;
  2121. bool result = cmSystemTools::RunSingleCommand(command, &output, &output,
  2122. &retVal);
  2123. if (!result || retVal)
  2124. {
  2125. std::cerr << "AUTORCC: error: process for " << rcc_output_file <<
  2126. " failed:\n" << output << std::endl;
  2127. this->RunRccFailed = true;
  2128. cmSystemTools::RemoveFile(rcc_output_file);
  2129. return false;
  2130. }
  2131. }
  2132. }
  2133. return true;
  2134. }
  2135. std::string cmQtAutoGenerators::Join(const std::vector<std::string>& lst,
  2136. char separator)
  2137. {
  2138. if (lst.empty())
  2139. {
  2140. return "";
  2141. }
  2142. std::string result;
  2143. for (std::vector<std::string>::const_iterator it = lst.begin();
  2144. it != lst.end();
  2145. ++it)
  2146. {
  2147. result += "." + (*it) + separator;
  2148. }
  2149. result.erase(result.end() - 1);
  2150. return result;
  2151. }
  2152. bool cmQtAutoGenerators::StartsWith(const std::string& str,
  2153. const std::string& with)
  2154. {
  2155. return (str.substr(0, with.length()) == with);
  2156. }
  2157. bool cmQtAutoGenerators::EndsWith(const std::string& str,
  2158. const std::string& with)
  2159. {
  2160. if (with.length() > (str.length()))
  2161. {
  2162. return false;
  2163. }
  2164. return (str.substr(str.length() - with.length(), with.length()) == with);
  2165. }