cmQtAutoGenerators.cxx 69 KB

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