cmQtAutoGenerators.cxx 69 KB

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