cmQtAutoGenerators.cxx 70 KB

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