cmQtAutoGenerators.cxx 69 KB

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