cmQtAutoGenerators.cxx 70 KB

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