cmQtAutoGenerators.cxx 69 KB

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