cmQtAutoGenerators.cxx 78 KB

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