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