cmQtAutoGenerators.cxx 78 KB

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