cmQtAutoGenerators.cxx 79 KB

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