cmQtAutoGenerators.cxx 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmQtAutoGenerators.h"
  4. #include <algorithm>
  5. #include <assert.h>
  6. #include <cmConfigure.h>
  7. #include <cmsys/FStream.hxx>
  8. #include <cmsys/Terminal.h>
  9. #include <list>
  10. #include <sstream>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <utility>
  14. #include "cmAlgorithms.h"
  15. #include "cmFilePathChecksum.h"
  16. #include "cmGlobalGenerator.h"
  17. #include "cmMakefile.h"
  18. #include "cmOutputConverter.h"
  19. #include "cmStateDirectory.h"
  20. #include "cmStateSnapshot.h"
  21. #include "cmSystemTools.h"
  22. #include "cm_auto_ptr.hxx"
  23. #include "cmake.h"
  24. #if defined(__APPLE__)
  25. #include <unistd.h>
  26. #endif
  27. // -- Static variables
  28. static const char* SettingsKeyMoc = "AM_MOC_OLD_SETTINGS";
  29. static const char* SettingsKeyUic = "AM_UIC_OLD_SETTINGS";
  30. static const char* SettingsKeyRcc = "AM_RCC_OLD_SETTINGS";
  31. // -- Static functions
  32. /**
  33. * @brief Returns a the string escaped and enclosed in quotes
  34. */
  35. static std::string Quoted(const std::string& text)
  36. {
  37. static const char* rep[18] = { "\\", "\\\\", "\"", "\\\"", "\a", "\\a",
  38. "\b", "\\b", "\f", "\\f", "\n", "\\n",
  39. "\r", "\\r", "\t", "\\t", "\v", "\\v" };
  40. std::string res = text;
  41. for (const char* const* it = cmArrayBegin(rep); it != cmArrayEnd(rep);
  42. it += 2) {
  43. cmSystemTools::ReplaceString(res, *it, *(it + 1));
  44. }
  45. res = '"' + res;
  46. res += '"';
  47. return res;
  48. }
  49. static std::string GetConfigDefinition(cmMakefile* makefile,
  50. const std::string& key,
  51. const std::string& config)
  52. {
  53. std::string keyConf = key;
  54. if (!config.empty()) {
  55. keyConf += "_";
  56. keyConf += config;
  57. }
  58. const char* valueConf = makefile->GetDefinition(keyConf);
  59. if (valueConf != CM_NULLPTR) {
  60. return valueConf;
  61. }
  62. return makefile->GetSafeDefinition(key);
  63. }
  64. static std::string SettingsFile(const std::string& targetDirectory)
  65. {
  66. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  67. cmSystemTools::ConvertToUnixSlashes(filename);
  68. filename += "/AutogenOldSettings.cmake";
  69. return filename;
  70. }
  71. inline static bool SettingsMatch(cmMakefile* makefile, const char* key,
  72. const std::string& value)
  73. {
  74. return (value == makefile->GetSafeDefinition(key));
  75. }
  76. static void SettingWrite(std::ostream& ostr, const char* key,
  77. const std::string& value)
  78. {
  79. if (!value.empty()) {
  80. ostr << "set(" << key << " " << cmOutputConverter::EscapeForCMake(value)
  81. << ")\n";
  82. }
  83. }
  84. static bool FileNameIsUnique(const std::string& filePath,
  85. const std::map<std::string, std::string>& fileMap)
  86. {
  87. size_t count(0);
  88. const std::string fileName = cmsys::SystemTools::GetFilenameName(filePath);
  89. for (std::map<std::string, std::string>::const_iterator si = fileMap.begin();
  90. si != fileMap.end(); ++si) {
  91. if (cmsys::SystemTools::GetFilenameName(si->first) == fileName) {
  92. ++count;
  93. if (count > 1) {
  94. return false;
  95. }
  96. }
  97. }
  98. return true;
  99. }
  100. static std::string ReadAll(const std::string& filename)
  101. {
  102. cmsys::ifstream file(filename.c_str());
  103. std::ostringstream stream;
  104. stream << file.rdbuf();
  105. file.close();
  106. return stream.str();
  107. }
  108. /**
  109. * @brief Tests if buildFile doesn't exist or is older than sourceFile
  110. * @return True if buildFile doesn't exist or is older than sourceFile
  111. */
  112. static bool FileAbsentOrOlder(const std::string& buildFile,
  113. const std::string& sourceFile)
  114. {
  115. int result = 0;
  116. bool success =
  117. cmsys::SystemTools::FileTimeCompare(buildFile, sourceFile, &result);
  118. return (!success || (result <= 0));
  119. }
  120. static bool ListContains(const std::vector<std::string>& list,
  121. const std::string& entry)
  122. {
  123. return (std::find(list.begin(), list.end(), entry) != list.end());
  124. }
  125. static std::string JoinOptionsList(const std::vector<std::string>& opts)
  126. {
  127. return cmOutputConverter::EscapeForCMake(cmJoin(opts, ";"));
  128. }
  129. static std::string JoinOptionsMap(
  130. const std::map<std::string, std::string>& opts)
  131. {
  132. std::string result;
  133. for (std::map<std::string, std::string>::const_iterator it = opts.begin();
  134. it != opts.end(); ++it) {
  135. if (it != opts.begin()) {
  136. result += "@list_sep@";
  137. }
  138. result += it->first;
  139. result += "===";
  140. result += it->second;
  141. }
  142. return result;
  143. }
  144. static std::string JoinExts(const std::vector<std::string>& lst)
  145. {
  146. std::string result;
  147. if (!lst.empty()) {
  148. const std::string separator = ",";
  149. for (std::vector<std::string>::const_iterator it = lst.begin();
  150. it != lst.end(); ++it) {
  151. if (it != lst.begin()) {
  152. result += separator;
  153. }
  154. result += '.';
  155. result += *it;
  156. }
  157. }
  158. return result;
  159. }
  160. static void UicMergeOptions(std::vector<std::string>& opts,
  161. const std::vector<std::string>& fileOpts,
  162. bool isQt5)
  163. {
  164. static const char* valueOptions[] = { "tr", "translate",
  165. "postfix", "generator",
  166. "include", // Since Qt 5.3
  167. "g" };
  168. std::vector<std::string> extraOpts;
  169. for (std::vector<std::string>::const_iterator it = fileOpts.begin();
  170. it != fileOpts.end(); ++it) {
  171. std::vector<std::string>::iterator existingIt =
  172. std::find(opts.begin(), opts.end(), *it);
  173. if (existingIt != opts.end()) {
  174. const char* o = it->c_str();
  175. if (*o == '-') {
  176. ++o;
  177. }
  178. if (isQt5 && *o == '-') {
  179. ++o;
  180. }
  181. if (std::find_if(cmArrayBegin(valueOptions), cmArrayEnd(valueOptions),
  182. cmStrCmp(*it)) != cmArrayEnd(valueOptions)) {
  183. assert(existingIt + 1 != opts.end());
  184. *(existingIt + 1) = *(it + 1);
  185. ++it;
  186. }
  187. } else {
  188. extraOpts.push_back(*it);
  189. }
  190. }
  191. opts.insert(opts.end(), extraOpts.begin(), extraOpts.end());
  192. }
  193. // -- Class methods
  194. cmQtAutoGenerators::cmQtAutoGenerators()
  195. : Verbose(cmsys::SystemTools::HasEnv("VERBOSE"))
  196. , ColorOutput(true)
  197. , RunMocFailed(false)
  198. , RunUicFailed(false)
  199. , RunRccFailed(false)
  200. , GenerateAllMoc(false)
  201. , GenerateAllUic(false)
  202. , GenerateAllRcc(false)
  203. {
  204. std::string colorEnv;
  205. cmsys::SystemTools::GetEnv("COLOR", colorEnv);
  206. if (!colorEnv.empty()) {
  207. if (cmSystemTools::IsOn(colorEnv.c_str())) {
  208. this->ColorOutput = true;
  209. } else {
  210. this->ColorOutput = false;
  211. }
  212. }
  213. this->MacroFilters[0].first = "Q_OBJECT";
  214. this->MacroFilters[0].second.compile("[\n][ \t]*Q_OBJECT[^a-zA-Z0-9_]");
  215. this->MacroFilters[1].first = "Q_GADGET";
  216. this->MacroFilters[1].second.compile("[\n][ \t]*Q_GADGET[^a-zA-Z0-9_]");
  217. // Precompile regular expressions
  218. this->RegExpMocInclude.compile(
  219. "[\n][ \t]*#[ \t]*include[ \t]+"
  220. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  221. this->RegExpUicInclude.compile("[\n][ \t]*#[ \t]*include[ \t]+"
  222. "[\"<](([^ \">]+/)?ui_[^ \">/]+\\.h)[\">]");
  223. }
  224. bool cmQtAutoGenerators::Run(const std::string& targetDirectory,
  225. const std::string& config)
  226. {
  227. cmake cm;
  228. cm.SetHomeOutputDirectory(targetDirectory);
  229. cm.SetHomeDirectory(targetDirectory);
  230. cm.GetCurrentSnapshot().SetDefaultDefinitions();
  231. cmGlobalGenerator gg(&cm);
  232. cmStateSnapshot snapshot = cm.GetCurrentSnapshot();
  233. snapshot.GetDirectory().SetCurrentBinary(targetDirectory);
  234. snapshot.GetDirectory().SetCurrentSource(targetDirectory);
  235. CM_AUTO_PTR<cmMakefile> mf(new cmMakefile(&gg, snapshot));
  236. gg.SetCurrentMakefile(mf.get());
  237. bool success = false;
  238. if (this->ReadAutogenInfoFile(mf.get(), targetDirectory, config)) {
  239. // Read old settings
  240. this->SettingsFileRead(mf.get(), targetDirectory);
  241. // Init and run
  242. this->Init(mf.get());
  243. if (this->RunAutogen()) {
  244. // Write current settings
  245. if (this->SettingsFileWrite(targetDirectory)) {
  246. success = true;
  247. }
  248. }
  249. }
  250. return success;
  251. }
  252. bool cmQtAutoGenerators::MocDependFilterPush(const std::string& key,
  253. const std::string& regExp)
  254. {
  255. bool success = false;
  256. if (!key.empty()) {
  257. if (!regExp.empty()) {
  258. MocDependFilter filter;
  259. filter.key = key;
  260. if (filter.regExp.compile(regExp)) {
  261. this->MocDependFilters.push_back(filter);
  262. success = true;
  263. } else {
  264. this->LogError("AutoMoc: Error in AUTOMOC_DEPEND_FILTERS: Compiling "
  265. "regular expression failed.\nKey: " +
  266. Quoted(key) + "\nExp.: " + Quoted(regExp));
  267. }
  268. } else {
  269. this->LogError("AutoMoc: Error in AUTOMOC_DEPEND_FILTERS: Regular "
  270. "expression is empty");
  271. }
  272. } else {
  273. this->LogError("AutoMoc: Error in AUTOMOC_DEPEND_FILTERS: Key is empty");
  274. }
  275. return success;
  276. }
  277. bool cmQtAutoGenerators::ReadAutogenInfoFile(
  278. cmMakefile* makefile, const std::string& targetDirectory,
  279. const std::string& config)
  280. {
  281. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  282. cmSystemTools::ConvertToUnixSlashes(filename);
  283. filename += "/AutogenInfo.cmake";
  284. if (!makefile->ReadListFile(filename.c_str())) {
  285. this->LogError("AutoGen: Error processing file: " + filename);
  286. return false;
  287. }
  288. // - Target names
  289. this->OriginTargetName =
  290. makefile->GetSafeDefinition("AM_ORIGIN_TARGET_NAME");
  291. this->AutogenTargetName = makefile->GetSafeDefinition("AM_TARGET_NAME");
  292. // - Directories
  293. this->ProjectSourceDir = makefile->GetSafeDefinition("AM_CMAKE_SOURCE_DIR");
  294. this->ProjectBinaryDir = makefile->GetSafeDefinition("AM_CMAKE_BINARY_DIR");
  295. this->CurrentSourceDir =
  296. makefile->GetSafeDefinition("AM_CMAKE_CURRENT_SOURCE_DIR");
  297. this->CurrentBinaryDir =
  298. makefile->GetSafeDefinition("AM_CMAKE_CURRENT_BINARY_DIR");
  299. // - Qt environment
  300. this->QtMajorVersion = makefile->GetSafeDefinition("AM_QT_VERSION_MAJOR");
  301. if (this->QtMajorVersion == "") {
  302. this->QtMajorVersion =
  303. makefile->GetSafeDefinition("AM_Qt5Core_VERSION_MAJOR");
  304. }
  305. // Check Qt version
  306. if ((this->QtMajorVersion != "4") && (this->QtMajorVersion != "5")) {
  307. this->LogError("AutoGen: Error: Unsupported Qt version: " +
  308. Quoted(this->QtMajorVersion));
  309. return false;
  310. }
  311. this->MocExecutable = makefile->GetSafeDefinition("AM_QT_MOC_EXECUTABLE");
  312. this->UicExecutable = makefile->GetSafeDefinition("AM_QT_UIC_EXECUTABLE");
  313. this->RccExecutable = makefile->GetSafeDefinition("AM_QT_RCC_EXECUTABLE");
  314. // - File Lists
  315. cmSystemTools::ExpandListArgument(makefile->GetSafeDefinition("AM_SOURCES"),
  316. this->Sources);
  317. cmSystemTools::ExpandListArgument(makefile->GetSafeDefinition("AM_HEADERS"),
  318. this->Headers);
  319. // - Moc
  320. cmSystemTools::ExpandListArgument(makefile->GetSafeDefinition("AM_MOC_SKIP"),
  321. this->MocSkipList);
  322. cmSystemTools::ExpandListArgument(
  323. GetConfigDefinition(makefile, "AM_MOC_COMPILE_DEFINITIONS", config),
  324. this->MocDefinitions);
  325. cmSystemTools::ExpandListArgument(
  326. GetConfigDefinition(makefile, "AM_MOC_INCLUDES", config),
  327. this->MocIncludePaths);
  328. cmSystemTools::ExpandListArgument(
  329. makefile->GetSafeDefinition("AM_MOC_OPTIONS"), this->MocOptions);
  330. {
  331. std::vector<std::string> mocDependFilters;
  332. cmSystemTools::ExpandListArgument(
  333. makefile->GetSafeDefinition("AM_MOC_DEPEND_FILTERS"), mocDependFilters);
  334. // Insert Q_PLUGIN_METADATA dependency filter
  335. if (this->QtMajorVersion != "4") {
  336. this->MocDependFilterPush("Q_PLUGIN_METADATA",
  337. "[\n][ \t]*Q_PLUGIN_METADATA[ \t]*\\("
  338. "[^\\)]*FILE[ \t]*\"([^\"]+)\"");
  339. }
  340. // Insert user defined dependency filters
  341. if ((mocDependFilters.size() % 2) == 0) {
  342. for (std::vector<std::string>::const_iterator dit =
  343. mocDependFilters.begin();
  344. dit != mocDependFilters.end(); dit += 2) {
  345. if (!this->MocDependFilterPush(*dit, *(dit + 1))) {
  346. return false;
  347. }
  348. }
  349. } else {
  350. this->LogError("AutoMoc: Error: AUTOMOC_DEPEND_FILTERS list size is not "
  351. "a multiple of 2");
  352. }
  353. }
  354. // - Uic
  355. cmSystemTools::ExpandListArgument(makefile->GetSafeDefinition("AM_UIC_SKIP"),
  356. this->UicSkipList);
  357. cmSystemTools::ExpandListArgument(
  358. GetConfigDefinition(makefile, "AM_UIC_TARGET_OPTIONS", config),
  359. this->UicTargetOptions);
  360. {
  361. std::vector<std::string> uicFilesVec;
  362. std::vector<std::string> uicOptionsVec;
  363. cmSystemTools::ExpandListArgument(
  364. makefile->GetSafeDefinition("AM_UIC_OPTIONS_FILES"), uicFilesVec);
  365. cmSystemTools::ExpandListArgument(
  366. makefile->GetSafeDefinition("AM_UIC_OPTIONS_OPTIONS"), uicOptionsVec);
  367. if (uicFilesVec.size() != uicOptionsVec.size()) {
  368. this->LogError(
  369. "AutoGen: Error: Uic files/options lists size missmatch in: " +
  370. filename);
  371. return false;
  372. }
  373. for (std::vector<std::string>::iterator fileIt = uicFilesVec.begin(),
  374. optionIt = uicOptionsVec.begin();
  375. fileIt != uicFilesVec.end(); ++fileIt, ++optionIt) {
  376. cmSystemTools::ReplaceString(*optionIt, "@list_sep@", ";");
  377. this->UicOptions[*fileIt] = *optionIt;
  378. }
  379. }
  380. // - Rcc
  381. cmSystemTools::ExpandListArgument(
  382. makefile->GetSafeDefinition("AM_RCC_SOURCES"), this->RccSources);
  383. {
  384. std::vector<std::string> rccFilesVec;
  385. std::vector<std::string> rccOptionsVec;
  386. cmSystemTools::ExpandListArgument(
  387. makefile->GetSafeDefinition("AM_RCC_OPTIONS_FILES"), rccFilesVec);
  388. cmSystemTools::ExpandListArgument(
  389. makefile->GetSafeDefinition("AM_RCC_OPTIONS_OPTIONS"), rccOptionsVec);
  390. if (rccFilesVec.size() != rccOptionsVec.size()) {
  391. this->LogError(
  392. "AutoGen: Error: RCC files/options lists size missmatch in: " +
  393. filename);
  394. return false;
  395. }
  396. for (std::vector<std::string>::iterator fileIt = rccFilesVec.begin(),
  397. optionIt = rccOptionsVec.begin();
  398. fileIt != rccFilesVec.end(); ++fileIt, ++optionIt) {
  399. cmSystemTools::ReplaceString(*optionIt, "@list_sep@", ";");
  400. this->RccOptions[*fileIt] = *optionIt;
  401. }
  402. }
  403. {
  404. std::vector<std::string> rccInputLists;
  405. cmSystemTools::ExpandListArgument(
  406. makefile->GetSafeDefinition("AM_RCC_INPUTS"), rccInputLists);
  407. // qrc files in the end of the list may have been empty
  408. if (rccInputLists.size() < this->RccSources.size()) {
  409. rccInputLists.resize(this->RccSources.size());
  410. }
  411. if (this->RccSources.size() != rccInputLists.size()) {
  412. this->LogError(
  413. "AutoGen: Error: RCC sources/inputs lists size missmatch in: " +
  414. filename);
  415. return false;
  416. }
  417. for (std::vector<std::string>::iterator fileIt = this->RccSources.begin(),
  418. inputIt = rccInputLists.begin();
  419. fileIt != this->RccSources.end(); ++fileIt, ++inputIt) {
  420. cmSystemTools::ReplaceString(*inputIt, "@list_sep@", ";");
  421. std::vector<std::string> rccInputFiles;
  422. cmSystemTools::ExpandListArgument(*inputIt, rccInputFiles);
  423. this->RccInputs[*fileIt] = rccInputFiles;
  424. }
  425. }
  426. // - Flags
  427. this->IncludeProjectDirsBefore =
  428. makefile->IsOn("AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE");
  429. this->MocRelaxedMode = makefile->IsOn("AM_MOC_RELAXED_MODE");
  430. return true;
  431. }
  432. void cmQtAutoGenerators::SettingsFileRead(cmMakefile* makefile,
  433. const std::string& targetDirectory)
  434. {
  435. // Compose current settings strings
  436. if (this->MocEnabled()) {
  437. std::string& str = this->SettingsStringMoc;
  438. str += JoinOptionsList(this->MocDefinitions);
  439. str += " ~~~ ";
  440. str += JoinOptionsList(this->MocIncludePaths);
  441. str += " ~~~ ";
  442. str += JoinOptionsList(this->MocOptions);
  443. str += " ~~~ ";
  444. str += this->IncludeProjectDirsBefore ? "TRUE" : "FALSE";
  445. str += " ~~~ ";
  446. }
  447. if (this->UicEnabled()) {
  448. std::string& str = this->SettingsStringUic;
  449. str += JoinOptionsList(this->UicTargetOptions);
  450. str += " ~~~ ";
  451. str += JoinOptionsMap(this->UicOptions);
  452. str += " ~~~ ";
  453. }
  454. if (this->RccEnabled()) {
  455. std::string& str = this->SettingsStringRcc;
  456. str += JoinOptionsMap(this->RccOptions);
  457. str += " ~~~ ";
  458. }
  459. // Read old settings
  460. const std::string filename = SettingsFile(targetDirectory);
  461. if (makefile->ReadListFile(filename.c_str())) {
  462. if (!SettingsMatch(makefile, SettingsKeyMoc, this->SettingsStringMoc)) {
  463. this->GenerateAllMoc = true;
  464. }
  465. if (!SettingsMatch(makefile, SettingsKeyUic, this->SettingsStringUic)) {
  466. this->GenerateAllUic = true;
  467. }
  468. if (!SettingsMatch(makefile, SettingsKeyRcc, this->SettingsStringRcc)) {
  469. this->GenerateAllRcc = true;
  470. }
  471. // In case any setting changed remove the old settings file.
  472. // This triggers a full rebuild on the next run if the current
  473. // build is aborted before writing the current settings in the end.
  474. if (this->GenerateAllAny()) {
  475. cmSystemTools::RemoveFile(filename);
  476. }
  477. } else {
  478. // If the file could not be read re-generate everythiung.
  479. this->GenerateAllMoc = true;
  480. this->GenerateAllUic = true;
  481. this->GenerateAllRcc = true;
  482. }
  483. }
  484. bool cmQtAutoGenerators::SettingsFileWrite(const std::string& targetDirectory)
  485. {
  486. bool success = true;
  487. // Only write if any setting changed
  488. if (this->GenerateAllAny()) {
  489. const std::string filename = SettingsFile(targetDirectory);
  490. if (this->Verbose) {
  491. this->LogInfo("AutoGen: Writing settings file " + filename);
  492. }
  493. cmsys::ofstream outfile;
  494. outfile.open(filename.c_str(), std::ios::trunc);
  495. if (outfile) {
  496. SettingWrite(outfile, SettingsKeyMoc, this->SettingsStringMoc);
  497. SettingWrite(outfile, SettingsKeyUic, this->SettingsStringUic);
  498. SettingWrite(outfile, SettingsKeyRcc, this->SettingsStringRcc);
  499. success = outfile.good();
  500. outfile.close();
  501. } else {
  502. success = false;
  503. // Remove old settings file to trigger full rebuild on next run
  504. cmSystemTools::RemoveFile(filename);
  505. this->LogError("AutoGen: Error: Writing old settings file failed: " +
  506. filename);
  507. }
  508. }
  509. return success;
  510. }
  511. void cmQtAutoGenerators::Init(cmMakefile* makefile)
  512. {
  513. this->AutogenBuildSubDir = this->AutogenTargetName;
  514. this->AutogenBuildSubDir += "/";
  515. this->MocCppFilenameRel = this->AutogenBuildSubDir;
  516. this->MocCppFilenameRel += "moc_compilation.cpp";
  517. this->MocCppFilenameAbs = this->CurrentBinaryDir + this->MocCppFilenameRel;
  518. // Init file path checksum generator
  519. fpathCheckSum.setupParentDirs(this->CurrentSourceDir, this->CurrentBinaryDir,
  520. this->ProjectSourceDir,
  521. this->ProjectBinaryDir);
  522. // Acquire header extensions
  523. this->HeaderExtensions = makefile->GetCMakeInstance()->GetHeaderExtensions();
  524. // Sort include directories on demand
  525. if (this->IncludeProjectDirsBefore) {
  526. // Move strings to temporary list
  527. std::list<std::string> includes;
  528. includes.insert(includes.end(), this->MocIncludePaths.begin(),
  529. this->MocIncludePaths.end());
  530. this->MocIncludePaths.clear();
  531. this->MocIncludePaths.reserve(includes.size());
  532. // Append project directories only
  533. {
  534. const char* movePaths[2] = { this->ProjectBinaryDir.c_str(),
  535. this->ProjectSourceDir.c_str() };
  536. for (const char* const* mpit = cmArrayBegin(movePaths);
  537. mpit != cmArrayEnd(movePaths); ++mpit) {
  538. std::list<std::string>::iterator it = includes.begin();
  539. while (it != includes.end()) {
  540. const std::string& path = *it;
  541. if (cmsys::SystemTools::StringStartsWith(path, *mpit)) {
  542. this->MocIncludePaths.push_back(path);
  543. it = includes.erase(it);
  544. } else {
  545. ++it;
  546. }
  547. }
  548. }
  549. }
  550. // Append remaining directories
  551. this->MocIncludePaths.insert(this->MocIncludePaths.end(), includes.begin(),
  552. includes.end());
  553. }
  554. // Compose moc includes list
  555. {
  556. std::set<std::string> frameworkPaths;
  557. for (std::vector<std::string>::const_iterator it =
  558. this->MocIncludePaths.begin();
  559. it != this->MocIncludePaths.end(); ++it) {
  560. const std::string& path = *it;
  561. this->MocIncludes.push_back("-I" + path);
  562. // Extract framework path
  563. if (cmHasLiteralSuffix(path, ".framework/Headers")) {
  564. // Go up twice to get to the framework root
  565. std::vector<std::string> pathComponents;
  566. cmsys::SystemTools::SplitPath(path, pathComponents);
  567. std::string frameworkPath = cmsys::SystemTools::JoinPath(
  568. pathComponents.begin(), pathComponents.end() - 2);
  569. frameworkPaths.insert(frameworkPath);
  570. }
  571. }
  572. // Append framework includes
  573. for (std::set<std::string>::const_iterator it = frameworkPaths.begin();
  574. it != frameworkPaths.end(); ++it) {
  575. this->MocIncludes.push_back("-F");
  576. this->MocIncludes.push_back(*it);
  577. }
  578. }
  579. }
  580. bool cmQtAutoGenerators::RunAutogen()
  581. {
  582. // the program goes through all .cpp files to see which moc files are
  583. // included. It is not really interesting how the moc file is named, but
  584. // what file the moc is created from. Once a moc is included the same moc
  585. // may not be included in the moc_compilation.cpp file anymore. OTOH if
  586. // there's a header containing Q_OBJECT where no corresponding moc file
  587. // is included anywhere a moc_<filename>.cpp file is created and included
  588. // in the moc_compilation.cpp file.
  589. // key = moc source filepath, value = moc output filepath
  590. std::map<std::string, std::string> mocsIncluded;
  591. std::map<std::string, std::string> mocsNotIncluded;
  592. std::map<std::string, std::set<std::string> > mocDepends;
  593. std::map<std::string, std::vector<std::string> > uisIncluded;
  594. // collects all headers which may need to be mocced
  595. std::set<std::string> mocHeaderFiles;
  596. std::set<std::string> uicHeaderFiles;
  597. // Parse sources
  598. for (std::vector<std::string>::const_iterator it = this->Sources.begin();
  599. it != this->Sources.end(); ++it) {
  600. const std::string& absFilename = cmsys::SystemTools::GetRealPath(*it);
  601. // Parse source file for MOC/UIC
  602. if (!this->ParseSourceFile(absFilename, mocsIncluded, mocDepends,
  603. uisIncluded, this->MocRelaxedMode)) {
  604. return false;
  605. }
  606. // Find additional headers
  607. this->SearchHeadersForSourceFile(absFilename, mocHeaderFiles,
  608. uicHeaderFiles);
  609. }
  610. // Parse headers
  611. for (std::vector<std::string>::const_iterator it = this->Headers.begin();
  612. it != this->Headers.end(); ++it) {
  613. const std::string& headerName = cmsys::SystemTools::GetRealPath(*it);
  614. if (!this->MocSkip(headerName)) {
  615. mocHeaderFiles.insert(headerName);
  616. }
  617. if (!this->UicSkip(headerName)) {
  618. uicHeaderFiles.insert(headerName);
  619. }
  620. }
  621. this->ParseHeaders(mocHeaderFiles, uicHeaderFiles, mocsIncluded,
  622. mocsNotIncluded, mocDepends, uisIncluded);
  623. // Generate files
  624. if (!this->MocGenerateAll(mocsIncluded, mocsNotIncluded, mocDepends)) {
  625. return false;
  626. }
  627. if (!this->UicGenerateAll(uisIncluded)) {
  628. return false;
  629. }
  630. if (!this->RccGenerateAll()) {
  631. return false;
  632. }
  633. return true;
  634. }
  635. /**
  636. * @brief Tests if the C++ content requires moc processing
  637. * @return True if moc is required
  638. */
  639. bool cmQtAutoGenerators::MocRequired(const std::string& contentText,
  640. std::string* macroName)
  641. {
  642. for (unsigned int ii = 0; ii != cmArraySize(this->MacroFilters); ++ii) {
  643. MacroFilter& filter = this->MacroFilters[ii];
  644. // Run a simple find string operation before the expensive
  645. // regular expression check
  646. if (contentText.find(filter.first) != std::string::npos) {
  647. if (filter.second.find(contentText)) {
  648. // Return macro name on demand
  649. if (macroName != CM_NULLPTR) {
  650. *macroName = filter.first;
  651. }
  652. return true;
  653. }
  654. }
  655. }
  656. return false;
  657. }
  658. void cmQtAutoGenerators::MocFindDepends(
  659. const std::string& absFilename, const std::string& contentText,
  660. std::map<std::string, std::set<std::string> >& mocDepends)
  661. {
  662. for (std::vector<MocDependFilter>::iterator fit =
  663. this->MocDependFilters.begin();
  664. fit != this->MocDependFilters.end(); ++fit) {
  665. MocDependFilter& filter = *fit;
  666. // Run a simple find string operation before the expensive
  667. // regular expression check
  668. if (contentText.find(filter.key) != std::string::npos) {
  669. // Run regular expression check loop
  670. const char* contentChars = contentText.c_str();
  671. while (filter.regExp.find(contentChars)) {
  672. // Evaluate match
  673. const std::string match = filter.regExp.match(1);
  674. if (!match.empty()) {
  675. // Find the dependency file
  676. const std::string incFile =
  677. this->FindIncludedFile(absFilename, match);
  678. if (!incFile.empty()) {
  679. mocDepends[absFilename].insert(incFile);
  680. if (this->Verbose) {
  681. this->LogInfo("AutoMoc: Found dependency:\n " +
  682. Quoted(absFilename) + "\n " + Quoted(incFile));
  683. }
  684. } else {
  685. this->LogWarning("AutoMoc: Warning: " + Quoted(absFilename) +
  686. "\n" + "Could not find dependency file " +
  687. Quoted(match));
  688. }
  689. }
  690. contentChars += filter.regExp.end();
  691. }
  692. }
  693. }
  694. }
  695. /**
  696. * @brief Tests if the file should be ignored for moc scanning
  697. * @return True if the file should be ignored
  698. */
  699. bool cmQtAutoGenerators::MocSkip(const std::string& absFilename) const
  700. {
  701. if (this->MocEnabled()) {
  702. // Test if the file name is on the skip list
  703. if (!ListContains(this->MocSkipList, absFilename)) {
  704. return false;
  705. }
  706. }
  707. return true;
  708. }
  709. /**
  710. * @brief Tests if the file name is in the skip list
  711. */
  712. bool cmQtAutoGenerators::UicSkip(const std::string& absFilename) const
  713. {
  714. if (this->UicEnabled()) {
  715. // Test if the file name is on the skip list
  716. if (!ListContains(this->UicSkipList, absFilename)) {
  717. return false;
  718. }
  719. }
  720. return true;
  721. }
  722. /**
  723. * @return True on success
  724. */
  725. bool cmQtAutoGenerators::ParseSourceFile(
  726. const std::string& absFilename,
  727. std::map<std::string, std::string>& mocsIncluded,
  728. std::map<std::string, std::set<std::string> >& mocDepends,
  729. std::map<std::string, std::vector<std::string> >& uisIncluded, bool relaxed)
  730. {
  731. bool success = true;
  732. const std::string contentText = ReadAll(absFilename);
  733. if (contentText.empty()) {
  734. std::ostringstream ost;
  735. ost << "AutoGen: Warning: " << absFilename << "\n"
  736. << "The file is empty\n";
  737. this->LogWarning(ost.str());
  738. } else {
  739. // Parse source contents for MOC
  740. if (success && !this->MocSkip(absFilename)) {
  741. success = this->MocParseSourceContent(absFilename, contentText,
  742. mocsIncluded, mocDepends, relaxed);
  743. }
  744. // Parse source contents for UIC
  745. if (success && !this->UicSkip(absFilename)) {
  746. this->UicParseContent(absFilename, contentText, uisIncluded);
  747. }
  748. }
  749. return success;
  750. }
  751. void cmQtAutoGenerators::UicParseContent(
  752. const std::string& absFilename, const std::string& contentText,
  753. std::map<std::string, std::vector<std::string> >& uisIncluded)
  754. {
  755. if (this->Verbose) {
  756. this->LogInfo("AutoUic: Checking " + absFilename);
  757. }
  758. const char* contentChars = contentText.c_str();
  759. if (strstr(contentChars, "ui_") != CM_NULLPTR) {
  760. while (this->RegExpUicInclude.find(contentChars)) {
  761. const std::string currentUi = this->RegExpUicInclude.match(1);
  762. const std::string basename =
  763. cmsys::SystemTools::GetFilenameWithoutLastExtension(currentUi);
  764. // basename should be the part of the ui filename used for
  765. // finding the correct header, so we need to remove the ui_ part
  766. uisIncluded[absFilename].push_back(basename.substr(3));
  767. contentChars += this->RegExpUicInclude.end();
  768. }
  769. }
  770. }
  771. /**
  772. * @return True on success
  773. */
  774. bool cmQtAutoGenerators::MocParseSourceContent(
  775. const std::string& absFilename, const std::string& contentText,
  776. std::map<std::string, std::string>& mocsIncluded,
  777. std::map<std::string, std::set<std::string> >& mocDepends, bool relaxed)
  778. {
  779. if (this->Verbose) {
  780. this->LogInfo("AutoMoc: Checking " + absFilename);
  781. }
  782. const std::string scannedFileAbsPath =
  783. cmsys::SystemTools::GetFilenamePath(absFilename) + '/';
  784. const std::string scannedFileBasename =
  785. cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename);
  786. std::string macroName;
  787. const bool requiresMoc = this->MocRequired(contentText, &macroName);
  788. bool ownDotMocIncluded = false;
  789. std::string ownMocUnderscoreInclude;
  790. std::string ownMocUnderscoreHeader;
  791. // first a simple string check for "moc" is *much* faster than the regexp,
  792. // and if the string search already fails, we don't have to try the
  793. // expensive regexp
  794. const char* contentChars = contentText.c_str();
  795. if (strstr(contentChars, "moc") != CM_NULLPTR) {
  796. // Iterate over all included moc files
  797. while (this->RegExpMocInclude.find(contentChars)) {
  798. const std::string incString = this->RegExpMocInclude.match(1);
  799. // Basename of the moc include
  800. const std::string incBasename =
  801. cmsys::SystemTools::GetFilenameWithoutLastExtension(incString);
  802. std::string incSubDir;
  803. if (incString.find_first_of('/') != std::string::npos) {
  804. incSubDir = cmsys::SystemTools::GetFilenamePath(incString);
  805. incSubDir += '/';
  806. }
  807. // If the moc include is of the moc_foo.cpp style we expect
  808. // the Q_OBJECT class declaration in a header file.
  809. // If the moc include is of the foo.moc style we need to look for
  810. // a Q_OBJECT macro in the current source file, if it contains the
  811. // macro we generate the moc file from the source file.
  812. if (cmHasLiteralPrefix(incBasename, "moc_")) {
  813. // Include: moc_FOO.cxx
  814. // Remove the moc_ part
  815. const std::string incRealBasename = incBasename.substr(4);
  816. const std::string headerToMoc =
  817. this->FindMocHeader(scannedFileAbsPath, incRealBasename, incSubDir);
  818. if (!headerToMoc.empty()) {
  819. // Register moc job
  820. mocsIncluded[headerToMoc] = incString;
  821. this->MocFindDepends(headerToMoc, contentText, mocDepends);
  822. // Store meta information for relaxed mode
  823. if (relaxed && (incRealBasename == scannedFileBasename)) {
  824. ownMocUnderscoreInclude = incString;
  825. ownMocUnderscoreHeader = headerToMoc;
  826. }
  827. } else {
  828. std::ostringstream ost;
  829. ost << "AutoMoc: Error: " << absFilename << "\n"
  830. << "The file includes the moc file " << Quoted(incString)
  831. << ", but could not find header "
  832. << Quoted(incRealBasename + "{" +
  833. JoinExts(this->HeaderExtensions) + "}");
  834. ;
  835. this->LogError(ost.str());
  836. return false;
  837. }
  838. } else {
  839. // Include: FOO.moc
  840. std::string fileToMoc;
  841. if (relaxed) {
  842. // Mode: Relaxed
  843. if (requiresMoc && (incBasename == scannedFileBasename)) {
  844. // Include self
  845. fileToMoc = absFilename;
  846. ownDotMocIncluded = true;
  847. } else {
  848. // In relaxed mode try to find a header instead but issue a warning
  849. const std::string headerToMoc =
  850. this->FindMocHeader(scannedFileAbsPath, incBasename, incSubDir);
  851. if (!headerToMoc.empty()) {
  852. // This is for KDE4 compatibility:
  853. fileToMoc = headerToMoc;
  854. if (!requiresMoc && (incBasename == scannedFileBasename)) {
  855. std::ostringstream ost;
  856. ost << "AutoMoc: Warning: " << Quoted(absFilename) << "\n"
  857. << "The file includes the moc file " << Quoted(incString)
  858. << ", but does not contain a Q_OBJECT or Q_GADGET macro.\n"
  859. << "Running moc on " << Quoted(headerToMoc) << "!\n"
  860. << "Include " << Quoted("moc_" + incBasename + ".cpp")
  861. << " for a compatibility with strict mode (see "
  862. "CMAKE_AUTOMOC_RELAXED_MODE).\n";
  863. this->LogWarning(ost.str());
  864. } else {
  865. std::ostringstream ost;
  866. ost << "AutoMoc: Warning: " << Quoted(absFilename) << "\n"
  867. << "The file includes the moc file " << Quoted(incString)
  868. << " instead of " << Quoted("moc_" + incBasename + ".cpp")
  869. << ".\n"
  870. << "Running moc on " << Quoted(headerToMoc) << "!\n"
  871. << "Include " << Quoted("moc_" + incBasename + ".cpp")
  872. << " for compatibility with strict mode (see "
  873. "CMAKE_AUTOMOC_RELAXED_MODE).\n";
  874. this->LogWarning(ost.str());
  875. }
  876. } else {
  877. std::ostringstream ost;
  878. ost << "AutoMoc: Error: " << Quoted(absFilename) << "\n"
  879. << "The file includes the moc file " << Quoted(incString)
  880. << ". which seems to be the moc file from a different "
  881. "source file. CMake also could not find a matching "
  882. "header.";
  883. this->LogError(ost.str());
  884. return false;
  885. }
  886. }
  887. } else {
  888. // Mode: Strict
  889. if (incBasename == scannedFileBasename) {
  890. // Include self
  891. fileToMoc = absFilename;
  892. ownDotMocIncluded = true;
  893. // Accept but issue a warning if moc isn't required
  894. if (!requiresMoc) {
  895. std::ostringstream ost;
  896. ost << "AutoMoc: Error: " << Quoted(absFilename) << "\n"
  897. << "The file includes the moc file " << Quoted(incString)
  898. << ", but does not contain a Q_OBJECT or Q_GADGET "
  899. "macro.";
  900. this->LogWarning(ost.str());
  901. }
  902. } else {
  903. // Don't allow FOO.moc include other than self in strict mode
  904. std::ostringstream ost;
  905. ost << "AutoMoc: Error: " << Quoted(absFilename) << "\n"
  906. << "The file includes the moc file " << Quoted(incString)
  907. << ", which seems to be the moc file from a different "
  908. "source file. This is not supported. Include "
  909. << Quoted(scannedFileBasename + ".moc")
  910. << " to run moc on this source file.";
  911. this->LogError(ost.str());
  912. return false;
  913. }
  914. }
  915. if (!fileToMoc.empty()) {
  916. mocsIncluded[fileToMoc] = incString;
  917. this->MocFindDepends(fileToMoc, contentText, mocDepends);
  918. }
  919. }
  920. // Forward content pointer
  921. contentChars += this->RegExpMocInclude.end();
  922. }
  923. }
  924. if (requiresMoc && !ownDotMocIncluded) {
  925. // In this case, check whether the scanned file itself contains a Q_OBJECT.
  926. // If this is the case, the moc_foo.cpp should probably be generated from
  927. // foo.cpp instead of foo.h, because otherwise it won't build.
  928. // But warn, since this is not how it is supposed to be used.
  929. if (relaxed && !ownMocUnderscoreInclude.empty()) {
  930. // This is for KDE4 compatibility:
  931. std::ostringstream ost;
  932. ost << "AutoMoc: Warning: " << Quoted(absFilename) << "\n"
  933. << "The file contains a " << macroName
  934. << " macro, but does not include "
  935. << Quoted(scannedFileBasename + ".moc") << ", but instead includes "
  936. << Quoted(ownMocUnderscoreInclude) << ".\n"
  937. << "Running moc on " << Quoted(absFilename) << "!\n"
  938. << "Better include " << Quoted(scannedFileBasename + ".moc")
  939. << " for compatibility with strict mode (see "
  940. "CMAKE_AUTOMOC_RELAXED_MODE).";
  941. this->LogWarning(ost.str());
  942. // Use scanned source file instead of scanned header file as moc source
  943. mocsIncluded[absFilename] = ownMocUnderscoreInclude;
  944. this->MocFindDepends(absFilename, contentText, mocDepends);
  945. // Remove
  946. mocsIncluded.erase(ownMocUnderscoreHeader);
  947. } else {
  948. // Otherwise always error out since it will not compile:
  949. std::ostringstream ost;
  950. ost << "AutoMoc: Error: " << Quoted(absFilename) << "\n"
  951. << "The file contains a " << macroName
  952. << " macro, but does not include "
  953. << Quoted(scannedFileBasename + ".moc") << "!\n"
  954. << "Consider adding the include or enabling SKIP_AUTOMOC for this "
  955. "file.";
  956. this->LogError(ost.str());
  957. return false;
  958. }
  959. }
  960. return true;
  961. }
  962. void cmQtAutoGenerators::MocParseHeaderContent(
  963. const std::string& absFilename, const std::string& contentText,
  964. std::map<std::string, std::string>& mocsNotIncluded,
  965. std::map<std::string, std::set<std::string> >& mocDepends)
  966. {
  967. // Log
  968. if (this->Verbose) {
  969. this->LogInfo("AutoMoc: Checking " + absFilename);
  970. }
  971. if (this->MocRequired(contentText)) {
  972. // Register moc job
  973. mocsNotIncluded[absFilename] =
  974. this->ChecksumedPath(absFilename, "moc_", ".cpp");
  975. this->MocFindDepends(absFilename, contentText, mocDepends);
  976. }
  977. }
  978. void cmQtAutoGenerators::SearchHeadersForSourceFile(
  979. const std::string& absFilename, std::set<std::string>& mocHeaderFiles,
  980. std::set<std::string>& uicHeaderFiles) const
  981. {
  982. std::string basepaths[2];
  983. {
  984. std::string bpath = cmsys::SystemTools::GetFilenamePath(absFilename);
  985. bpath += '/';
  986. bpath += cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename);
  987. // search for default header files and private header files
  988. basepaths[0] = bpath;
  989. basepaths[1] = bpath + "_p";
  990. }
  991. for (const std::string* bpit = cmArrayBegin(basepaths);
  992. bpit != cmArrayEnd(basepaths); ++bpit) {
  993. std::string headerName;
  994. if (this->FindHeader(headerName, *bpit)) {
  995. // Moc headers
  996. if (!this->MocSkip(absFilename) && !this->MocSkip(headerName)) {
  997. mocHeaderFiles.insert(headerName);
  998. }
  999. // Uic headers
  1000. if (!this->UicSkip(absFilename) && !this->UicSkip(headerName)) {
  1001. uicHeaderFiles.insert(headerName);
  1002. }
  1003. break;
  1004. }
  1005. }
  1006. }
  1007. void cmQtAutoGenerators::ParseHeaders(
  1008. const std::set<std::string>& mocHeaderFiles,
  1009. const std::set<std::string>& uicHeaderFiles,
  1010. const std::map<std::string, std::string>& mocsIncluded,
  1011. std::map<std::string, std::string>& mocsNotIncluded,
  1012. std::map<std::string, std::set<std::string> >& mocDepends,
  1013. std::map<std::string, std::vector<std::string> >& uisIncluded)
  1014. {
  1015. // Merged header files list to read files only once
  1016. std::set<std::string> headerFiles;
  1017. headerFiles.insert(mocHeaderFiles.begin(), mocHeaderFiles.end());
  1018. headerFiles.insert(uicHeaderFiles.begin(), uicHeaderFiles.end());
  1019. for (std::set<std::string>::const_iterator hIt = headerFiles.begin();
  1020. hIt != headerFiles.end(); ++hIt) {
  1021. const std::string& headerName = *hIt;
  1022. const std::string contentText = ReadAll(headerName);
  1023. // Parse header content for MOC
  1024. if ((mocHeaderFiles.find(headerName) != mocHeaderFiles.end()) &&
  1025. (mocsIncluded.find(headerName) == mocsIncluded.end())) {
  1026. this->MocParseHeaderContent(headerName, contentText, mocsNotIncluded,
  1027. mocDepends);
  1028. }
  1029. // Parse header content for UIC
  1030. if (uicHeaderFiles.find(headerName) != uicHeaderFiles.end()) {
  1031. this->UicParseContent(headerName, contentText, uisIncluded);
  1032. }
  1033. }
  1034. }
  1035. bool cmQtAutoGenerators::MocGenerateAll(
  1036. const std::map<std::string, std::string>& mocsIncluded,
  1037. const std::map<std::string, std::string>& mocsNotIncluded,
  1038. const std::map<std::string, std::set<std::string> >& mocDepends)
  1039. {
  1040. if (!this->MocEnabled()) {
  1041. return true;
  1042. }
  1043. bool mocCompFileGenerated = false;
  1044. bool mocCompChanged = false;
  1045. // look for name collisions
  1046. {
  1047. std::multimap<std::string, std::string> collisions;
  1048. // Test merged map of included and notIncluded
  1049. std::map<std::string, std::string> mergedMocs(mocsIncluded);
  1050. mergedMocs.insert(mocsNotIncluded.begin(), mocsNotIncluded.end());
  1051. if (this->NameCollisionTest(mergedMocs, collisions)) {
  1052. std::ostringstream ost;
  1053. ost << "AutoMoc: Error: "
  1054. "The same moc file will be generated "
  1055. "from different sources.\n"
  1056. "To avoid this error either\n"
  1057. "- rename the source files or\n"
  1058. "- do not include the (moc_NAME.cpp|NAME.moc) file";
  1059. this->LogErrorNameCollision(ost.str(), collisions);
  1060. return false;
  1061. }
  1062. }
  1063. // Generate moc files that are included by source files.
  1064. {
  1065. const std::string subDir = "include/";
  1066. for (std::map<std::string, std::string>::const_iterator it =
  1067. mocsIncluded.begin();
  1068. it != mocsIncluded.end(); ++it) {
  1069. if (!this->MocGenerateFile(it->first, it->second, subDir, mocDepends)) {
  1070. if (this->RunMocFailed) {
  1071. return false;
  1072. }
  1073. }
  1074. }
  1075. }
  1076. // Generate moc files that are _not_ included by source files.
  1077. {
  1078. const std::string subDir;
  1079. for (std::map<std::string, std::string>::const_iterator it =
  1080. mocsNotIncluded.begin();
  1081. it != mocsNotIncluded.end(); ++it) {
  1082. if (this->MocGenerateFile(it->first, it->second, subDir, mocDepends)) {
  1083. mocCompFileGenerated = true;
  1084. } else {
  1085. if (this->RunMocFailed) {
  1086. return false;
  1087. }
  1088. }
  1089. }
  1090. }
  1091. // Compose moc_compilation.cpp content
  1092. std::string automocSource;
  1093. {
  1094. std::ostringstream ost;
  1095. ost << "/* This file is autogenerated, do not edit*/\n";
  1096. if (mocsNotIncluded.empty()) {
  1097. // Dummy content
  1098. ost << "enum some_compilers { need_more_than_nothing };\n";
  1099. } else {
  1100. // Valid content
  1101. for (std::map<std::string, std::string>::const_iterator it =
  1102. mocsNotIncluded.begin();
  1103. it != mocsNotIncluded.end(); ++it) {
  1104. ost << "#include \"" << it->second << "\"\n";
  1105. }
  1106. }
  1107. automocSource = ost.str();
  1108. }
  1109. // Check if the content of moc_compilation.cpp changed
  1110. {
  1111. const std::string oldContents = ReadAll(this->MocCppFilenameAbs);
  1112. mocCompChanged = (oldContents != automocSource);
  1113. }
  1114. bool success = true;
  1115. if (mocCompChanged) {
  1116. // Actually write moc_compilation.cpp
  1117. this->LogBold("Generating MOC compilation " + this->MocCppFilenameRel);
  1118. // Make sure the parent directory exists
  1119. success = this->MakeParentDirectory(this->MocCppFilenameAbs);
  1120. if (success) {
  1121. cmsys::ofstream outfile;
  1122. outfile.open(this->MocCppFilenameAbs.c_str(), std::ios::trunc);
  1123. if (!outfile) {
  1124. success = false;
  1125. this->LogError("AutoMoc: Error opening " + this->MocCppFilenameAbs);
  1126. } else {
  1127. outfile << automocSource;
  1128. // Check for write errors
  1129. if (!outfile.good()) {
  1130. success = false;
  1131. this->LogError("AutoMoc: Error writing " + this->MocCppFilenameAbs);
  1132. }
  1133. }
  1134. }
  1135. } else if (mocCompFileGenerated) {
  1136. // Only touch moc_compilation.cpp
  1137. if (this->Verbose) {
  1138. this->LogInfo("Touching MOC compilation " + this->MocCppFilenameRel);
  1139. }
  1140. cmSystemTools::Touch(this->MocCppFilenameAbs, false);
  1141. }
  1142. return success;
  1143. }
  1144. /**
  1145. * @return True if a moc file was created. False may indicate an error.
  1146. */
  1147. bool cmQtAutoGenerators::MocGenerateFile(
  1148. const std::string& sourceFile, const std::string& mocFileName,
  1149. const std::string& subDirPrefix,
  1150. const std::map<std::string, std::set<std::string> >& mocDepends)
  1151. {
  1152. bool mocGenerated = false;
  1153. bool generateMoc = this->GenerateAllMoc;
  1154. const std::string mocFileRel =
  1155. this->AutogenBuildSubDir + subDirPrefix + mocFileName;
  1156. const std::string mocFileAbs = this->CurrentBinaryDir + mocFileRel;
  1157. if (!generateMoc) {
  1158. // Test if the source file is newer that the build file
  1159. generateMoc = FileAbsentOrOlder(mocFileAbs, sourceFile);
  1160. if (!generateMoc) {
  1161. // Test if a dependency file changed
  1162. std::map<std::string, std::set<std::string> >::const_iterator dit =
  1163. mocDepends.find(sourceFile);
  1164. if (dit != mocDepends.end()) {
  1165. for (std::set<std::string>::const_iterator fit = dit->second.begin();
  1166. fit != dit->second.end(); ++fit) {
  1167. if (FileAbsentOrOlder(mocFileAbs, *fit)) {
  1168. generateMoc = true;
  1169. break;
  1170. }
  1171. }
  1172. }
  1173. }
  1174. }
  1175. if (generateMoc) {
  1176. // Log
  1177. this->LogBold("Generating MOC source " + mocFileRel);
  1178. // Make sure the parent directory exists
  1179. if (this->MakeParentDirectory(mocFileAbs)) {
  1180. // Compose moc command
  1181. std::vector<std::string> cmd;
  1182. cmd.push_back(this->MocExecutable);
  1183. cmd.insert(cmd.end(), this->MocIncludes.begin(),
  1184. this->MocIncludes.end());
  1185. // Add definitions
  1186. for (std::vector<std::string>::const_iterator it =
  1187. this->MocDefinitions.begin();
  1188. it != this->MocDefinitions.end(); ++it) {
  1189. cmd.push_back("-D" + (*it));
  1190. }
  1191. cmd.insert(cmd.end(), this->MocOptions.begin(), this->MocOptions.end());
  1192. #ifdef _WIN32
  1193. cmd.push_back("-DWIN32");
  1194. #endif
  1195. cmd.push_back("-o");
  1196. cmd.push_back(mocFileAbs);
  1197. cmd.push_back(sourceFile);
  1198. // Log moc command
  1199. if (this->Verbose) {
  1200. this->LogCommand(cmd);
  1201. }
  1202. // Execute moc command
  1203. bool res = false;
  1204. int retVal = 0;
  1205. std::string output;
  1206. res = cmSystemTools::RunSingleCommand(cmd, &output, &output, &retVal);
  1207. if (!res || (retVal != 0)) {
  1208. // Command failed
  1209. {
  1210. std::ostringstream ost;
  1211. ost << "AutoMoc: Error: moc process failed for\n";
  1212. ost << Quoted(mocFileRel) << "\n";
  1213. ost << "AutoMoc: Command:\n" << cmJoin(cmd, " ") << "\n";
  1214. ost << "AutoMoc: Command output:\n" << output << "\n";
  1215. this->LogError(ost.str());
  1216. }
  1217. cmSystemTools::RemoveFile(mocFileAbs);
  1218. this->RunMocFailed = true;
  1219. } else {
  1220. // Success
  1221. mocGenerated = true;
  1222. }
  1223. } else {
  1224. // Parent directory creation failed
  1225. this->RunMocFailed = true;
  1226. }
  1227. }
  1228. return mocGenerated;
  1229. }
  1230. bool cmQtAutoGenerators::UicGenerateAll(
  1231. const std::map<std::string, std::vector<std::string> >& uisIncluded)
  1232. {
  1233. if (!this->UicEnabled()) {
  1234. return true;
  1235. }
  1236. // single map with input / output names
  1237. std::map<std::string, std::map<std::string, std::string> > uiGenMap;
  1238. std::map<std::string, std::string> testMap;
  1239. for (std::map<std::string, std::vector<std::string> >::const_iterator it =
  1240. uisIncluded.begin();
  1241. it != uisIncluded.end(); ++it) {
  1242. // source file path
  1243. std::string sourcePath = cmsys::SystemTools::GetFilenamePath(it->first);
  1244. sourcePath += '/';
  1245. // insert new map for source file an use new reference
  1246. uiGenMap[it->first] = std::map<std::string, std::string>();
  1247. std::map<std::string, std::string>& sourceMap = uiGenMap[it->first];
  1248. for (std::vector<std::string>::const_iterator sit = it->second.begin();
  1249. sit != it->second.end(); ++sit) {
  1250. const std::string& uiFileName = *sit;
  1251. const std::string uiInputFile = sourcePath + uiFileName + ".ui";
  1252. const std::string uiOutputFile = "ui_" + uiFileName + ".h";
  1253. sourceMap[uiInputFile] = uiOutputFile;
  1254. testMap[uiInputFile] = uiOutputFile;
  1255. }
  1256. }
  1257. // look for name collisions
  1258. {
  1259. std::multimap<std::string, std::string> collisions;
  1260. if (this->NameCollisionTest(testMap, collisions)) {
  1261. std::ostringstream ost;
  1262. ost << "AutoUic: Error: The same ui_NAME.h file will be generated "
  1263. "from different sources.\n"
  1264. "To avoid this error rename the source files.\n";
  1265. this->LogErrorNameCollision(ost.str(), collisions);
  1266. return false;
  1267. }
  1268. }
  1269. testMap.clear();
  1270. // generate ui files
  1271. for (std::map<std::string,
  1272. std::map<std::string, std::string> >::const_iterator it =
  1273. uiGenMap.begin();
  1274. it != uiGenMap.end(); ++it) {
  1275. for (std::map<std::string, std::string>::const_iterator sit =
  1276. it->second.begin();
  1277. sit != it->second.end(); ++sit) {
  1278. if (!this->UicGenerateFile(it->first, sit->first, sit->second)) {
  1279. if (this->RunUicFailed) {
  1280. return false;
  1281. }
  1282. }
  1283. }
  1284. }
  1285. return true;
  1286. }
  1287. /**
  1288. * @return True if a uic file was created. False may indicate an error.
  1289. */
  1290. bool cmQtAutoGenerators::UicGenerateFile(const std::string& realName,
  1291. const std::string& uiInputFile,
  1292. const std::string& uiOutputFile)
  1293. {
  1294. bool uicGenerated = false;
  1295. bool generateUic = this->GenerateAllUic;
  1296. const std::string uicFileRel =
  1297. this->AutogenBuildSubDir + "include/" + uiOutputFile;
  1298. const std::string uicFileAbs = this->CurrentBinaryDir + uicFileRel;
  1299. if (!generateUic) {
  1300. // Test if the source file is newer that the build file
  1301. generateUic = FileAbsentOrOlder(uicFileAbs, uiInputFile);
  1302. }
  1303. if (generateUic) {
  1304. // Log
  1305. this->LogBold("Generating UIC header " + uicFileRel);
  1306. // Make sure the parent directory exists
  1307. if (this->MakeParentDirectory(uicFileAbs)) {
  1308. // Compose uic command
  1309. std::vector<std::string> cmd;
  1310. cmd.push_back(this->UicExecutable);
  1311. {
  1312. std::vector<std::string> opts = this->UicTargetOptions;
  1313. std::map<std::string, std::string>::const_iterator optionIt =
  1314. this->UicOptions.find(uiInputFile);
  1315. if (optionIt != this->UicOptions.end()) {
  1316. std::vector<std::string> fileOpts;
  1317. cmSystemTools::ExpandListArgument(optionIt->second, fileOpts);
  1318. UicMergeOptions(opts, fileOpts, (this->QtMajorVersion == "5"));
  1319. }
  1320. cmd.insert(cmd.end(), opts.begin(), opts.end());
  1321. }
  1322. cmd.push_back("-o");
  1323. cmd.push_back(uicFileAbs);
  1324. cmd.push_back(uiInputFile);
  1325. // Log command
  1326. if (this->Verbose) {
  1327. this->LogCommand(cmd);
  1328. }
  1329. // Execute command
  1330. bool res = false;
  1331. int retVal = 0;
  1332. std::string output;
  1333. res = cmSystemTools::RunSingleCommand(cmd, &output, &output, &retVal);
  1334. if (!res || (retVal != 0)) {
  1335. // Command failed
  1336. {
  1337. std::ostringstream ost;
  1338. ost << "AutoUic: Error: uic process failed for\n";
  1339. ost << Quoted(uicFileRel) << " needed by\n";
  1340. ost << Quoted(realName) << "\n";
  1341. ost << "AutoUic: Command:\n" << cmJoin(cmd, " ") << "\n";
  1342. ost << "AutoUic: Command output:\n" << output << "\n";
  1343. this->LogError(ost.str());
  1344. }
  1345. cmSystemTools::RemoveFile(uicFileAbs);
  1346. this->RunUicFailed = true;
  1347. } else {
  1348. // Success
  1349. uicGenerated = true;
  1350. }
  1351. } else {
  1352. // Parent directory creation failed
  1353. this->RunUicFailed = true;
  1354. }
  1355. }
  1356. return uicGenerated;
  1357. }
  1358. bool cmQtAutoGenerators::RccGenerateAll()
  1359. {
  1360. if (!this->RccEnabled()) {
  1361. return true;
  1362. }
  1363. // generate single map with input / output names
  1364. std::map<std::string, std::string> qrcGenMap;
  1365. for (std::vector<std::string>::const_iterator si = this->RccSources.begin();
  1366. si != this->RccSources.end(); ++si) {
  1367. const std::string ext = cmsys::SystemTools::GetFilenameLastExtension(*si);
  1368. if (ext == ".qrc") {
  1369. qrcGenMap[*si] =
  1370. this->AutogenBuildSubDir + this->ChecksumedPath(*si, "qrc_", ".cpp");
  1371. }
  1372. }
  1373. // look for name collisions
  1374. {
  1375. std::multimap<std::string, std::string> collisions;
  1376. if (this->NameCollisionTest(qrcGenMap, collisions)) {
  1377. std::ostringstream ost;
  1378. ost << "AutoRcc: Error: The same qrc_NAME.cpp file"
  1379. " will be generated from different sources.\n"
  1380. "To avoid this error rename the source .qrc files.\n";
  1381. this->LogErrorNameCollision(ost.str(), collisions);
  1382. return false;
  1383. }
  1384. }
  1385. // generate qrc files
  1386. for (std::map<std::string, std::string>::const_iterator si =
  1387. qrcGenMap.begin();
  1388. si != qrcGenMap.end(); ++si) {
  1389. bool unique = FileNameIsUnique(si->first, qrcGenMap);
  1390. if (!this->RccGenerateFile(si->first, si->second, unique)) {
  1391. if (this->RunRccFailed) {
  1392. return false;
  1393. }
  1394. }
  1395. }
  1396. return true;
  1397. }
  1398. /**
  1399. * @return True if a rcc file was created. False may indicate an error.
  1400. */
  1401. bool cmQtAutoGenerators::RccGenerateFile(const std::string& rccInputFile,
  1402. const std::string& rccOutputFile,
  1403. bool unique_n)
  1404. {
  1405. bool rccGenerated = false;
  1406. bool generateRcc = this->GenerateAllRcc;
  1407. const std::string rccBuildFile = this->CurrentBinaryDir + rccOutputFile;
  1408. if (!generateRcc) {
  1409. // Test if the resources list file is newer than build file
  1410. generateRcc = FileAbsentOrOlder(rccBuildFile, rccInputFile);
  1411. if (!generateRcc) {
  1412. // Test if any resource file is newer than the build file
  1413. const std::vector<std::string>& files = this->RccInputs[rccInputFile];
  1414. for (std::vector<std::string>::const_iterator it = files.begin();
  1415. it != files.end(); ++it) {
  1416. if (FileAbsentOrOlder(rccBuildFile, *it)) {
  1417. generateRcc = true;
  1418. break;
  1419. }
  1420. }
  1421. }
  1422. }
  1423. if (generateRcc) {
  1424. // Log
  1425. this->LogBold("Generating RCC source " + rccOutputFile);
  1426. // Make sure the parent directory exists
  1427. if (this->MakeParentDirectory(rccBuildFile)) {
  1428. // Compose symbol name
  1429. std::string symbolName =
  1430. cmsys::SystemTools::GetFilenameWithoutLastExtension(rccInputFile);
  1431. if (!unique_n) {
  1432. symbolName += "_";
  1433. symbolName += fpathCheckSum.getPart(rccInputFile);
  1434. }
  1435. // Replace '-' with '_'. The former is valid for
  1436. // file names but not for symbol names.
  1437. std::replace(symbolName.begin(), symbolName.end(), '-', '_');
  1438. // Compose rcc command
  1439. std::vector<std::string> cmd;
  1440. cmd.push_back(this->RccExecutable);
  1441. {
  1442. std::map<std::string, std::string>::const_iterator optionIt =
  1443. this->RccOptions.find(rccInputFile);
  1444. if (optionIt != this->RccOptions.end()) {
  1445. cmSystemTools::ExpandListArgument(optionIt->second, cmd);
  1446. }
  1447. }
  1448. cmd.push_back("-name");
  1449. cmd.push_back(symbolName);
  1450. cmd.push_back("-o");
  1451. cmd.push_back(rccBuildFile);
  1452. cmd.push_back(rccInputFile);
  1453. // Log command
  1454. if (this->Verbose) {
  1455. this->LogCommand(cmd);
  1456. }
  1457. // Execute command
  1458. bool res = false;
  1459. int retVal = 0;
  1460. std::string output;
  1461. res = cmSystemTools::RunSingleCommand(cmd, &output, &output, &retVal);
  1462. if (!res || (retVal != 0)) {
  1463. // Command failed
  1464. {
  1465. std::ostringstream ost;
  1466. ost << "AutoRcc: Error: rcc process failed for\n";
  1467. ost << Quoted(rccOutputFile) << "\n";
  1468. ost << "AutoRcc: Command:\n" << cmJoin(cmd, " ") << "\n";
  1469. ost << "AutoRcc: Command output:\n" << output << "\n";
  1470. this->LogError(ost.str());
  1471. }
  1472. cmSystemTools::RemoveFile(rccBuildFile);
  1473. this->RunRccFailed = true;
  1474. } else {
  1475. // Success
  1476. rccGenerated = true;
  1477. }
  1478. } else {
  1479. // Parent directory creation failed
  1480. this->RunRccFailed = true;
  1481. }
  1482. }
  1483. return rccGenerated;
  1484. }
  1485. void cmQtAutoGenerators::LogErrorNameCollision(
  1486. const std::string& message,
  1487. const std::multimap<std::string, std::string>& collisions) const
  1488. {
  1489. typedef std::multimap<std::string, std::string>::const_iterator Iter;
  1490. std::ostringstream ost;
  1491. // Add message
  1492. if (!message.empty()) {
  1493. ost << message;
  1494. if (message[message.size() - 1] != '\n') {
  1495. ost << '\n';
  1496. }
  1497. }
  1498. // Append collision list
  1499. for (Iter it = collisions.begin(); it != collisions.end(); ++it) {
  1500. ost << it->first << " : " << it->second << '\n';
  1501. }
  1502. this->LogError(ost.str());
  1503. }
  1504. void cmQtAutoGenerators::LogBold(const std::string& message) const
  1505. {
  1506. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue |
  1507. cmsysTerminal_Color_ForegroundBold,
  1508. message.c_str(), true, this->ColorOutput);
  1509. }
  1510. void cmQtAutoGenerators::LogInfo(const std::string& message) const
  1511. {
  1512. std::string msg(message);
  1513. if (!msg.empty()) {
  1514. if (msg[msg.size() - 1] != '\n') {
  1515. msg.push_back('\n');
  1516. }
  1517. cmSystemTools::Stdout(msg.c_str(), msg.size());
  1518. }
  1519. }
  1520. void cmQtAutoGenerators::LogWarning(const std::string& message) const
  1521. {
  1522. std::string msg(message);
  1523. if (!msg.empty()) {
  1524. if (msg[msg.size() - 1] != '\n') {
  1525. msg.push_back('\n');
  1526. }
  1527. // Append empty line
  1528. msg.push_back('\n');
  1529. cmSystemTools::Stdout(msg.c_str(), msg.size());
  1530. }
  1531. }
  1532. void cmQtAutoGenerators::LogError(const std::string& message) const
  1533. {
  1534. std::string msg(message);
  1535. if (!msg.empty()) {
  1536. if (msg[msg.size() - 1] != '\n') {
  1537. msg.push_back('\n');
  1538. }
  1539. // Append empty line
  1540. msg.push_back('\n');
  1541. cmSystemTools::Stderr(msg.c_str(), msg.size());
  1542. }
  1543. }
  1544. void cmQtAutoGenerators::LogCommand(
  1545. const std::vector<std::string>& command) const
  1546. {
  1547. this->LogInfo(cmJoin(command, " "));
  1548. }
  1549. /**
  1550. * @brief Collects name collisions as output/input pairs
  1551. * @return True if there were collisions
  1552. */
  1553. bool cmQtAutoGenerators::NameCollisionTest(
  1554. const std::map<std::string, std::string>& genFiles,
  1555. std::multimap<std::string, std::string>& collisions) const
  1556. {
  1557. typedef std::map<std::string, std::string>::const_iterator Iter;
  1558. typedef std::map<std::string, std::string>::value_type VType;
  1559. for (Iter ait = genFiles.begin(); ait != genFiles.end(); ++ait) {
  1560. bool first_match(true);
  1561. for (Iter bit = (++Iter(ait)); bit != genFiles.end(); ++bit) {
  1562. if (ait->second == bit->second) {
  1563. if (first_match) {
  1564. if (collisions.find(ait->second) != collisions.end()) {
  1565. // We already know of this collision from before
  1566. break;
  1567. }
  1568. collisions.insert(VType(ait->second, ait->first));
  1569. first_match = false;
  1570. }
  1571. collisions.insert(VType(bit->second, bit->first));
  1572. }
  1573. }
  1574. }
  1575. return !collisions.empty();
  1576. }
  1577. /**
  1578. * @brief Generates a file path based on the checksum of the source file path
  1579. * @return The path
  1580. */
  1581. std::string cmQtAutoGenerators::ChecksumedPath(const std::string& sourceFile,
  1582. const char* basePrefix,
  1583. const char* baseSuffix) const
  1584. {
  1585. std::string res = fpathCheckSum.getPart(sourceFile);
  1586. res += "/";
  1587. res += basePrefix;
  1588. res += cmsys::SystemTools::GetFilenameWithoutLastExtension(sourceFile);
  1589. res += baseSuffix;
  1590. return res;
  1591. }
  1592. /**
  1593. * @brief Tries to find the header file to the given file base path by
  1594. * appending different header extensions
  1595. * @return True on success
  1596. */
  1597. bool cmQtAutoGenerators::FindHeader(std::string& header,
  1598. const std::string& testBasePath) const
  1599. {
  1600. for (std::vector<std::string>::const_iterator ext =
  1601. this->HeaderExtensions.begin();
  1602. ext != this->HeaderExtensions.end(); ++ext) {
  1603. std::string testFilePath(testBasePath);
  1604. testFilePath += '.';
  1605. testFilePath += (*ext);
  1606. if (cmsys::SystemTools::FileExists(testFilePath.c_str())) {
  1607. header = testFilePath;
  1608. return true;
  1609. }
  1610. }
  1611. return false;
  1612. }
  1613. bool cmQtAutoGenerators::FindHeaderGlobal(
  1614. std::string& header, const std::string& testBasePath) const
  1615. {
  1616. for (std::vector<std::string>::const_iterator iit =
  1617. this->MocIncludePaths.begin();
  1618. iit != this->MocIncludePaths.end(); ++iit) {
  1619. const std::string fullPath = ((*iit) + '/' + testBasePath);
  1620. if (FindHeader(header, fullPath)) {
  1621. return true;
  1622. }
  1623. }
  1624. return false;
  1625. }
  1626. std::string cmQtAutoGenerators::FindMocHeader(const std::string& basePath,
  1627. const std::string& baseName,
  1628. const std::string& subDir) const
  1629. {
  1630. std::string header;
  1631. do {
  1632. if (!subDir.empty()) {
  1633. if (this->FindHeader(header, basePath + subDir + baseName)) {
  1634. break;
  1635. }
  1636. }
  1637. if (this->FindHeader(header, basePath + baseName)) {
  1638. break;
  1639. }
  1640. // Try include directories
  1641. if (this->FindHeaderGlobal(header, subDir + baseName)) {
  1642. break;
  1643. }
  1644. } while (false);
  1645. // Sanitize
  1646. if (!header.empty()) {
  1647. header = cmsys::SystemTools::GetRealPath(header);
  1648. }
  1649. return header;
  1650. }
  1651. std::string cmQtAutoGenerators::FindIncludedFile(
  1652. const std::string& sourceFile, const std::string& includeString) const
  1653. {
  1654. // Search in vicinity of the source
  1655. {
  1656. std::string testPath = cmSystemTools::GetFilenamePath(sourceFile);
  1657. testPath += '/';
  1658. testPath += includeString;
  1659. if (cmsys::SystemTools::FileExists(testPath.c_str())) {
  1660. return cmsys::SystemTools::GetRealPath(testPath);
  1661. }
  1662. }
  1663. // Search globally
  1664. return FindInIncludeDirectories(includeString);
  1665. }
  1666. /**
  1667. * @brief Tries to find a file in the include directories
  1668. * @return True on success
  1669. */
  1670. std::string cmQtAutoGenerators::FindInIncludeDirectories(
  1671. const std::string& includeString) const
  1672. {
  1673. std::string res;
  1674. for (std::vector<std::string>::const_iterator iit =
  1675. this->MocIncludePaths.begin();
  1676. iit != this->MocIncludePaths.end(); ++iit) {
  1677. const std::string fullPath = ((*iit) + '/' + includeString);
  1678. if (cmsys::SystemTools::FileExists(fullPath.c_str())) {
  1679. res = cmsys::SystemTools::GetRealPath(fullPath);
  1680. break;
  1681. }
  1682. }
  1683. return res;
  1684. }
  1685. /**
  1686. * @brief Generates the parent directory of the given file on demand
  1687. * @return True on success
  1688. */
  1689. bool cmQtAutoGenerators::MakeParentDirectory(const std::string& filename) const
  1690. {
  1691. bool success = true;
  1692. const std::string dirName = cmSystemTools::GetFilenamePath(filename);
  1693. if (!dirName.empty()) {
  1694. success = cmsys::SystemTools::MakeDirectory(dirName);
  1695. if (!success) {
  1696. this->LogError("AutoGen: Error: Directory creation failed: " + dirName);
  1697. }
  1698. }
  1699. return success;
  1700. }