cmQtAutoGenerators.cxx 69 KB

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