cmQtAutoGenerators.cxx 78 KB

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