cmQtAutoGenerators.cxx 79 KB

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