cmQtAutoGenerators.cxx 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  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. std::string incFile;
  677. if (this->FindIncludedFile(incFile, absFilename, match)) {
  678. mocDepends[absFilename].insert(incFile);
  679. if (this->Verbose) {
  680. this->LogInfo("AutoMoc: Found dependency:\n " +
  681. Quoted(absFilename) + "\n " + Quoted(incFile));
  682. }
  683. } else {
  684. this->LogWarning("AutoMoc: Warning: " + Quoted(absFilename) +
  685. "\n" + "Could not find dependency file " +
  686. Quoted(match));
  687. }
  688. }
  689. contentChars += filter.regExp.end();
  690. }
  691. }
  692. }
  693. }
  694. /**
  695. * @brief Tests if the file should be ignored for moc scanning
  696. * @return True if the file should be ignored
  697. */
  698. bool cmQtAutoGenerators::MocSkip(const std::string& absFilename) const
  699. {
  700. if (this->MocEnabled()) {
  701. // Test if the file name is on the skip list
  702. if (!ListContains(this->MocSkipList, absFilename)) {
  703. return false;
  704. }
  705. }
  706. return true;
  707. }
  708. /**
  709. * @brief Tests if the file name is in the skip list
  710. */
  711. bool cmQtAutoGenerators::UicSkip(const std::string& absFilename) const
  712. {
  713. if (this->UicEnabled()) {
  714. // Test if the file name is on the skip list
  715. if (!ListContains(this->UicSkipList, absFilename)) {
  716. return false;
  717. }
  718. }
  719. return true;
  720. }
  721. /**
  722. * @return True on success
  723. */
  724. bool cmQtAutoGenerators::ParseSourceFile(
  725. const std::string& absFilename,
  726. std::map<std::string, std::string>& mocsIncluded,
  727. std::map<std::string, std::set<std::string> >& mocDepends,
  728. std::map<std::string, std::vector<std::string> >& uisIncluded, bool relaxed)
  729. {
  730. bool success = true;
  731. const std::string contentText = ReadAll(absFilename);
  732. if (contentText.empty()) {
  733. std::ostringstream ost;
  734. ost << "AutoGen: Warning: " << absFilename << "\n"
  735. << "The file is empty\n";
  736. this->LogWarning(ost.str());
  737. } else {
  738. // Parse source contents for MOC
  739. if (success && !this->MocSkip(absFilename)) {
  740. success = this->MocParseSourceContent(absFilename, contentText,
  741. mocsIncluded, mocDepends, relaxed);
  742. }
  743. // Parse source contents for UIC
  744. if (success && !this->UicSkip(absFilename)) {
  745. this->UicParseContent(absFilename, contentText, uisIncluded);
  746. }
  747. }
  748. return success;
  749. }
  750. void cmQtAutoGenerators::UicParseContent(
  751. const std::string& absFilename, const std::string& contentText,
  752. std::map<std::string, std::vector<std::string> >& uisIncluded)
  753. {
  754. if (this->Verbose) {
  755. this->LogInfo("AutoUic: Checking " + absFilename);
  756. }
  757. const char* contentChars = contentText.c_str();
  758. if (strstr(contentChars, "ui_") != CM_NULLPTR) {
  759. while (this->RegExpUicInclude.find(contentChars)) {
  760. const std::string currentUi = this->RegExpUicInclude.match(1);
  761. const std::string basename =
  762. cmsys::SystemTools::GetFilenameWithoutLastExtension(currentUi);
  763. // basename should be the part of the ui filename used for
  764. // finding the correct header, so we need to remove the ui_ part
  765. uisIncluded[absFilename].push_back(basename.substr(3));
  766. contentChars += this->RegExpUicInclude.end();
  767. }
  768. }
  769. }
  770. /**
  771. * @return True on success
  772. */
  773. bool cmQtAutoGenerators::MocParseSourceContent(
  774. const std::string& absFilename, const std::string& contentText,
  775. std::map<std::string, std::string>& mocsIncluded,
  776. std::map<std::string, std::set<std::string> >& mocDepends, bool relaxed)
  777. {
  778. if (this->Verbose) {
  779. this->LogInfo("AutoMoc: Checking " + absFilename);
  780. }
  781. const std::string scannedFileAbsPath =
  782. cmsys::SystemTools::GetFilenamePath(absFilename) + '/';
  783. const std::string scannedFileBasename =
  784. cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename);
  785. std::string macroName;
  786. const bool requiresMoc = this->MocRequired(contentText, &macroName);
  787. bool ownDotMocIncluded = false;
  788. std::string ownMocUnderscoreInclude;
  789. std::string ownMocUnderscoreHeader;
  790. // first a simple string check for "moc" is *much* faster than the regexp,
  791. // and if the string search already fails, we don't have to try the
  792. // expensive regexp
  793. const char* contentChars = contentText.c_str();
  794. if (strstr(contentChars, "moc") != CM_NULLPTR) {
  795. // Iterate over all included moc files
  796. while (this->RegExpMocInclude.find(contentChars)) {
  797. const std::string incString = this->RegExpMocInclude.match(1);
  798. // Basename of the moc include
  799. const std::string incBasename =
  800. cmsys::SystemTools::GetFilenameWithoutLastExtension(incString);
  801. std::string incSubDir;
  802. if (incString.find_first_of('/') != std::string::npos) {
  803. incSubDir = cmsys::SystemTools::GetFilenamePath(incString);
  804. incSubDir += '/';
  805. }
  806. // If the moc include is of the moc_foo.cpp style we expect
  807. // the Q_OBJECT class declaration in a header file.
  808. // If the moc include is of the foo.moc style we need to look for
  809. // a Q_OBJECT macro in the current source file, if it contains the
  810. // macro we generate the moc file from the source file.
  811. if (cmHasLiteralPrefix(incBasename, "moc_")) {
  812. // Include: moc_FOO.cxx
  813. // Remove the moc_ part
  814. const std::string incRealBasename = incBasename.substr(4);
  815. const std::string headerToMoc =
  816. this->FindMocHeader(scannedFileAbsPath, incRealBasename, incSubDir);
  817. if (!headerToMoc.empty()) {
  818. // Register moc job
  819. mocsIncluded[headerToMoc] = incString;
  820. this->MocFindDepends(headerToMoc, contentText, mocDepends);
  821. // Store meta information for relaxed mode
  822. if (relaxed && (incRealBasename == scannedFileBasename)) {
  823. ownMocUnderscoreInclude = incString;
  824. ownMocUnderscoreHeader = headerToMoc;
  825. }
  826. } else {
  827. std::ostringstream ost;
  828. ost << "AutoMoc: Error: " << absFilename << "\n"
  829. << "The file includes the moc file " << Quoted(incString)
  830. << ", but could not find header "
  831. << Quoted(incRealBasename + "{" +
  832. JoinExts(this->HeaderExtensions) + "}");
  833. ;
  834. this->LogError(ost.str());
  835. return false;
  836. }
  837. } else {
  838. // Include: FOO.moc
  839. std::string fileToMoc;
  840. if (relaxed) {
  841. // Mode: Relaxed
  842. if (requiresMoc && (incBasename == scannedFileBasename)) {
  843. // Include self
  844. fileToMoc = absFilename;
  845. ownDotMocIncluded = true;
  846. } else {
  847. // In relaxed mode try to find a header instead but issue a warning
  848. const std::string headerToMoc =
  849. this->FindMocHeader(scannedFileAbsPath, incBasename, incSubDir);
  850. if (!headerToMoc.empty()) {
  851. // This is for KDE4 compatibility:
  852. fileToMoc = headerToMoc;
  853. if (!requiresMoc && (incBasename == scannedFileBasename)) {
  854. std::ostringstream ost;
  855. ost << "AutoMoc: Warning: " << Quoted(absFilename) << "\n"
  856. << "The file includes the moc file " << Quoted(incString)
  857. << ", but does not contain a Q_OBJECT or Q_GADGET macro.\n"
  858. << "Running moc on " << Quoted(headerToMoc) << "!\n"
  859. << "Include " << Quoted("moc_" + incBasename + ".cpp")
  860. << " for a compatibility with strict mode (see "
  861. "CMAKE_AUTOMOC_RELAXED_MODE).\n";
  862. this->LogWarning(ost.str());
  863. } else {
  864. std::ostringstream ost;
  865. ost << "AutoMoc: Warning: " << Quoted(absFilename) << "\n"
  866. << "The file includes the moc file " << Quoted(incString)
  867. << " instead of " << Quoted("moc_" + incBasename + ".cpp")
  868. << ".\n"
  869. << "Running moc on " << Quoted(headerToMoc) << "!\n"
  870. << "Include " << Quoted("moc_" + incBasename + ".cpp")
  871. << " for compatibility with strict mode (see "
  872. "CMAKE_AUTOMOC_RELAXED_MODE).\n";
  873. this->LogWarning(ost.str());
  874. }
  875. } else {
  876. std::ostringstream ost;
  877. ost << "AutoMoc: Error: " << Quoted(absFilename) << "\n"
  878. << "The file includes the moc file " << Quoted(incString)
  879. << ". which seems to be the moc file from a different "
  880. "source file. CMake also could not find a matching "
  881. "header.";
  882. this->LogError(ost.str());
  883. return false;
  884. }
  885. }
  886. } else {
  887. // Mode: Strict
  888. if (incBasename == scannedFileBasename) {
  889. // Include self
  890. fileToMoc = absFilename;
  891. ownDotMocIncluded = true;
  892. // Accept but issue a warning if moc isn't required
  893. if (!requiresMoc) {
  894. std::ostringstream ost;
  895. ost << "AutoMoc: Error: " << Quoted(absFilename) << "\n"
  896. << "The file includes the moc file " << Quoted(incString)
  897. << ", but does not contain a Q_OBJECT or Q_GADGET "
  898. "macro.";
  899. this->LogWarning(ost.str());
  900. }
  901. } else {
  902. // Don't allow FOO.moc include other than self in strict mode
  903. std::ostringstream ost;
  904. ost << "AutoMoc: Error: " << Quoted(absFilename) << "\n"
  905. << "The file includes the moc file " << Quoted(incString)
  906. << ", which seems to be the moc file from a different "
  907. "source file. This is not supported. Include "
  908. << Quoted(scannedFileBasename + ".moc")
  909. << " to run moc on this source file.";
  910. this->LogError(ost.str());
  911. return false;
  912. }
  913. }
  914. if (!fileToMoc.empty()) {
  915. mocsIncluded[fileToMoc] = incString;
  916. this->MocFindDepends(fileToMoc, contentText, mocDepends);
  917. }
  918. }
  919. // Forward content pointer
  920. contentChars += this->RegExpMocInclude.end();
  921. }
  922. }
  923. if (requiresMoc && !ownDotMocIncluded) {
  924. // In this case, check whether the scanned file itself contains a Q_OBJECT.
  925. // If this is the case, the moc_foo.cpp should probably be generated from
  926. // foo.cpp instead of foo.h, because otherwise it won't build.
  927. // But warn, since this is not how it is supposed to be used.
  928. if (relaxed && !ownMocUnderscoreInclude.empty()) {
  929. // This is for KDE4 compatibility:
  930. std::ostringstream ost;
  931. ost << "AutoMoc: Warning: " << Quoted(absFilename) << "\n"
  932. << "The file contains a " << macroName
  933. << " macro, but does not include "
  934. << Quoted(scannedFileBasename + ".moc") << ", but instead includes "
  935. << Quoted(ownMocUnderscoreInclude) << ".\n"
  936. << "Running moc on " << Quoted(absFilename) << "!\n"
  937. << "Better include " << Quoted(scannedFileBasename + ".moc")
  938. << " for compatibility with strict mode (see "
  939. "CMAKE_AUTOMOC_RELAXED_MODE).";
  940. this->LogWarning(ost.str());
  941. // Use scanned source file instead of scanned header file as moc source
  942. mocsIncluded[absFilename] = ownMocUnderscoreInclude;
  943. this->MocFindDepends(absFilename, contentText, mocDepends);
  944. // Remove
  945. mocsIncluded.erase(ownMocUnderscoreHeader);
  946. } else {
  947. // Otherwise always error out since it will not compile:
  948. std::ostringstream ost;
  949. ost << "AutoMoc: Error: " << Quoted(absFilename) << "\n"
  950. << "The file contains a " << macroName
  951. << " macro, but does not include "
  952. << Quoted(scannedFileBasename + ".moc") << "!\n"
  953. << "Consider adding the include or enabling SKIP_AUTOMOC for this "
  954. "file.";
  955. this->LogError(ost.str());
  956. return false;
  957. }
  958. }
  959. return true;
  960. }
  961. void cmQtAutoGenerators::MocParseHeaderContent(
  962. const std::string& absFilename, const std::string& contentText,
  963. std::map<std::string, std::string>& mocsNotIncluded,
  964. std::map<std::string, std::set<std::string> >& mocDepends)
  965. {
  966. // Log
  967. if (this->Verbose) {
  968. this->LogInfo("AutoMoc: Checking " + absFilename);
  969. }
  970. if (this->MocRequired(contentText)) {
  971. // Register moc job
  972. mocsNotIncluded[absFilename] =
  973. this->ChecksumedPath(absFilename, "moc_", ".cpp");
  974. this->MocFindDepends(absFilename, contentText, mocDepends);
  975. }
  976. }
  977. void cmQtAutoGenerators::SearchHeadersForSourceFile(
  978. const std::string& absFilename, std::set<std::string>& mocHeaderFiles,
  979. std::set<std::string>& uicHeaderFiles) const
  980. {
  981. std::string basepaths[2];
  982. {
  983. std::string bpath = cmsys::SystemTools::GetFilenamePath(absFilename);
  984. bpath += '/';
  985. bpath += cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename);
  986. // search for default header files and private header files
  987. basepaths[0] = bpath;
  988. basepaths[1] = bpath + "_p";
  989. }
  990. for (const std::string* bpit = cmArrayBegin(basepaths);
  991. bpit != cmArrayEnd(basepaths); ++bpit) {
  992. std::string headerName;
  993. if (this->FindHeader(headerName, *bpit)) {
  994. // Moc headers
  995. if (!this->MocSkip(absFilename) && !this->MocSkip(headerName)) {
  996. mocHeaderFiles.insert(headerName);
  997. }
  998. // Uic headers
  999. if (!this->UicSkip(absFilename) && !this->UicSkip(headerName)) {
  1000. uicHeaderFiles.insert(headerName);
  1001. }
  1002. break;
  1003. }
  1004. }
  1005. }
  1006. void cmQtAutoGenerators::ParseHeaders(
  1007. const std::set<std::string>& mocHeaderFiles,
  1008. const std::set<std::string>& uicHeaderFiles,
  1009. const std::map<std::string, std::string>& mocsIncluded,
  1010. std::map<std::string, std::string>& mocsNotIncluded,
  1011. std::map<std::string, std::set<std::string> >& mocDepends,
  1012. std::map<std::string, std::vector<std::string> >& uisIncluded)
  1013. {
  1014. // Merged header files list to read files only once
  1015. std::set<std::string> headerFiles;
  1016. headerFiles.insert(mocHeaderFiles.begin(), mocHeaderFiles.end());
  1017. headerFiles.insert(uicHeaderFiles.begin(), uicHeaderFiles.end());
  1018. for (std::set<std::string>::const_iterator hIt = headerFiles.begin();
  1019. hIt != headerFiles.end(); ++hIt) {
  1020. const std::string& headerName = *hIt;
  1021. const std::string contentText = ReadAll(headerName);
  1022. // Parse header content for MOC
  1023. if ((mocHeaderFiles.find(headerName) != mocHeaderFiles.end()) &&
  1024. (mocsIncluded.find(headerName) == mocsIncluded.end())) {
  1025. this->MocParseHeaderContent(headerName, contentText, mocsNotIncluded,
  1026. mocDepends);
  1027. }
  1028. // Parse header content for UIC
  1029. if (uicHeaderFiles.find(headerName) != uicHeaderFiles.end()) {
  1030. this->UicParseContent(headerName, contentText, uisIncluded);
  1031. }
  1032. }
  1033. }
  1034. bool cmQtAutoGenerators::MocGenerateAll(
  1035. const std::map<std::string, std::string>& mocsIncluded,
  1036. const std::map<std::string, std::string>& mocsNotIncluded,
  1037. const std::map<std::string, std::set<std::string> >& mocDepends)
  1038. {
  1039. if (!this->MocEnabled()) {
  1040. return true;
  1041. }
  1042. bool mocCompFileGenerated = false;
  1043. bool mocCompChanged = false;
  1044. // look for name collisions
  1045. {
  1046. std::multimap<std::string, std::string> collisions;
  1047. // Test merged map of included and notIncluded
  1048. std::map<std::string, std::string> mergedMocs(mocsIncluded);
  1049. mergedMocs.insert(mocsNotIncluded.begin(), mocsNotIncluded.end());
  1050. if (this->NameCollisionTest(mergedMocs, collisions)) {
  1051. std::ostringstream ost;
  1052. ost << "AutoMoc: Error: "
  1053. "The same moc file will be generated "
  1054. "from different sources.\n"
  1055. "To avoid this error either\n"
  1056. "- rename the source files or\n"
  1057. "- do not include the (moc_NAME.cpp|NAME.moc) file";
  1058. this->LogErrorNameCollision(ost.str(), collisions);
  1059. return false;
  1060. }
  1061. }
  1062. // Generate moc files that are included by source files.
  1063. {
  1064. const std::string subDir = "include/";
  1065. for (std::map<std::string, std::string>::const_iterator it =
  1066. mocsIncluded.begin();
  1067. it != mocsIncluded.end(); ++it) {
  1068. if (!this->MocGenerateFile(it->first, it->second, subDir, mocDepends)) {
  1069. if (this->RunMocFailed) {
  1070. return false;
  1071. }
  1072. }
  1073. }
  1074. }
  1075. // Generate moc files that are _not_ included by source files.
  1076. {
  1077. const std::string subDir;
  1078. for (std::map<std::string, std::string>::const_iterator it =
  1079. mocsNotIncluded.begin();
  1080. it != mocsNotIncluded.end(); ++it) {
  1081. if (this->MocGenerateFile(it->first, it->second, subDir, mocDepends)) {
  1082. mocCompFileGenerated = true;
  1083. } else {
  1084. if (this->RunMocFailed) {
  1085. return false;
  1086. }
  1087. }
  1088. }
  1089. }
  1090. // Compose moc_compilation.cpp content
  1091. std::string automocSource;
  1092. {
  1093. std::ostringstream ost;
  1094. ost << "/* This file is autogenerated, do not edit*/\n";
  1095. if (mocsNotIncluded.empty()) {
  1096. // Dummy content
  1097. ost << "enum some_compilers { need_more_than_nothing };\n";
  1098. } else {
  1099. // Valid content
  1100. for (std::map<std::string, std::string>::const_iterator it =
  1101. mocsNotIncluded.begin();
  1102. it != mocsNotIncluded.end(); ++it) {
  1103. ost << "#include \"" << it->second << "\"\n";
  1104. }
  1105. }
  1106. automocSource = ost.str();
  1107. }
  1108. // Check if the content of moc_compilation.cpp changed
  1109. {
  1110. const std::string oldContents = ReadAll(this->MocCppFilenameAbs);
  1111. mocCompChanged = (oldContents != automocSource);
  1112. }
  1113. bool success = true;
  1114. if (mocCompChanged) {
  1115. // Actually write moc_compilation.cpp
  1116. this->LogBold("Generating MOC compilation " + this->MocCppFilenameRel);
  1117. // Make sure the parent directory exists
  1118. success = this->MakeParentDirectory(this->MocCppFilenameAbs);
  1119. if (success) {
  1120. cmsys::ofstream outfile;
  1121. outfile.open(this->MocCppFilenameAbs.c_str(), std::ios::trunc);
  1122. if (!outfile) {
  1123. success = false;
  1124. this->LogError("AutoMoc: Error opening " + this->MocCppFilenameAbs);
  1125. } else {
  1126. outfile << automocSource;
  1127. // Check for write errors
  1128. if (!outfile.good()) {
  1129. success = false;
  1130. this->LogError("AutoMoc: Error writing " + this->MocCppFilenameAbs);
  1131. }
  1132. }
  1133. }
  1134. } else if (mocCompFileGenerated) {
  1135. // Only touch moc_compilation.cpp
  1136. if (this->Verbose) {
  1137. this->LogInfo("Touching MOC compilation " + this->MocCppFilenameRel);
  1138. }
  1139. cmSystemTools::Touch(this->MocCppFilenameAbs, false);
  1140. }
  1141. return success;
  1142. }
  1143. /**
  1144. * @return True if a moc file was created. False may indicate an error.
  1145. */
  1146. bool cmQtAutoGenerators::MocGenerateFile(
  1147. const std::string& sourceFile, const std::string& mocFileName,
  1148. const std::string& subDirPrefix,
  1149. const std::map<std::string, std::set<std::string> >& mocDepends)
  1150. {
  1151. bool mocGenerated = false;
  1152. bool generateMoc = this->GenerateAllMoc;
  1153. const std::string mocFileRel =
  1154. this->AutogenBuildSubDir + subDirPrefix + mocFileName;
  1155. const std::string mocFileAbs = this->CurrentBinaryDir + mocFileRel;
  1156. if (!generateMoc) {
  1157. // Test if the source file is newer that the build file
  1158. generateMoc = FileAbsentOrOlder(mocFileAbs, sourceFile);
  1159. if (!generateMoc) {
  1160. // Test if a dependency file changed
  1161. std::map<std::string, std::set<std::string> >::const_iterator dit =
  1162. mocDepends.find(sourceFile);
  1163. if (dit != mocDepends.end()) {
  1164. for (std::set<std::string>::const_iterator fit = dit->second.begin();
  1165. fit != dit->second.end(); ++fit) {
  1166. if (FileAbsentOrOlder(mocFileAbs, *fit)) {
  1167. generateMoc = true;
  1168. break;
  1169. }
  1170. }
  1171. }
  1172. }
  1173. }
  1174. if (generateMoc) {
  1175. // Log
  1176. this->LogBold("Generating MOC source " + mocFileRel);
  1177. // Make sure the parent directory exists
  1178. if (this->MakeParentDirectory(mocFileAbs)) {
  1179. // Compose moc command
  1180. std::vector<std::string> cmd;
  1181. cmd.push_back(this->MocExecutable);
  1182. cmd.insert(cmd.end(), this->MocIncludes.begin(),
  1183. this->MocIncludes.end());
  1184. // Add definitions
  1185. for (std::vector<std::string>::const_iterator it =
  1186. this->MocDefinitions.begin();
  1187. it != this->MocDefinitions.end(); ++it) {
  1188. cmd.push_back("-D" + (*it));
  1189. }
  1190. cmd.insert(cmd.end(), this->MocOptions.begin(), this->MocOptions.end());
  1191. #ifdef _WIN32
  1192. cmd.push_back("-DWIN32");
  1193. #endif
  1194. cmd.push_back("-o");
  1195. cmd.push_back(mocFileAbs);
  1196. cmd.push_back(sourceFile);
  1197. // Log moc command
  1198. if (this->Verbose) {
  1199. this->LogCommand(cmd);
  1200. }
  1201. // Execute moc command
  1202. bool res = false;
  1203. int retVal = 0;
  1204. std::string output;
  1205. res = cmSystemTools::RunSingleCommand(cmd, &output, &output, &retVal);
  1206. if (!res || (retVal != 0)) {
  1207. // Command failed
  1208. {
  1209. std::ostringstream ost;
  1210. ost << "AutoMoc: Error: moc process failed for\n";
  1211. ost << Quoted(mocFileRel) << "\n";
  1212. ost << "AutoMoc: Command:\n" << cmJoin(cmd, " ") << "\n";
  1213. ost << "AutoMoc: Command output:\n" << output << "\n";
  1214. this->LogError(ost.str());
  1215. }
  1216. cmSystemTools::RemoveFile(mocFileAbs);
  1217. this->RunMocFailed = true;
  1218. } else {
  1219. // Success
  1220. mocGenerated = true;
  1221. }
  1222. } else {
  1223. // Parent directory creation failed
  1224. this->RunMocFailed = true;
  1225. }
  1226. }
  1227. return mocGenerated;
  1228. }
  1229. bool cmQtAutoGenerators::UicGenerateAll(
  1230. const std::map<std::string, std::vector<std::string> >& uisIncluded)
  1231. {
  1232. if (!this->UicEnabled()) {
  1233. return true;
  1234. }
  1235. // single map with input / output names
  1236. std::map<std::string, std::map<std::string, std::string> > uiGenMap;
  1237. std::map<std::string, std::string> testMap;
  1238. for (std::map<std::string, std::vector<std::string> >::const_iterator it =
  1239. uisIncluded.begin();
  1240. it != uisIncluded.end(); ++it) {
  1241. // source file path
  1242. std::string sourcePath = cmsys::SystemTools::GetFilenamePath(it->first);
  1243. sourcePath += '/';
  1244. // insert new map for source file an use new reference
  1245. uiGenMap[it->first] = std::map<std::string, std::string>();
  1246. std::map<std::string, std::string>& sourceMap = uiGenMap[it->first];
  1247. for (std::vector<std::string>::const_iterator sit = it->second.begin();
  1248. sit != it->second.end(); ++sit) {
  1249. const std::string& uiFileName = *sit;
  1250. const std::string uiInputFile = sourcePath + uiFileName + ".ui";
  1251. const std::string uiOutputFile = "ui_" + uiFileName + ".h";
  1252. sourceMap[uiInputFile] = uiOutputFile;
  1253. testMap[uiInputFile] = uiOutputFile;
  1254. }
  1255. }
  1256. // look for name collisions
  1257. {
  1258. std::multimap<std::string, std::string> collisions;
  1259. if (this->NameCollisionTest(testMap, collisions)) {
  1260. std::ostringstream ost;
  1261. ost << "AutoUic: Error: The same ui_NAME.h file will be generated "
  1262. "from different sources.\n"
  1263. "To avoid this error rename the source files.\n";
  1264. this->LogErrorNameCollision(ost.str(), collisions);
  1265. return false;
  1266. }
  1267. }
  1268. testMap.clear();
  1269. // generate ui files
  1270. for (std::map<std::string,
  1271. std::map<std::string, std::string> >::const_iterator it =
  1272. uiGenMap.begin();
  1273. it != uiGenMap.end(); ++it) {
  1274. for (std::map<std::string, std::string>::const_iterator sit =
  1275. it->second.begin();
  1276. sit != it->second.end(); ++sit) {
  1277. if (!this->UicGenerateFile(it->first, sit->first, sit->second)) {
  1278. if (this->RunUicFailed) {
  1279. return false;
  1280. }
  1281. }
  1282. }
  1283. }
  1284. return true;
  1285. }
  1286. /**
  1287. * @return True if a uic file was created. False may indicate an error.
  1288. */
  1289. bool cmQtAutoGenerators::UicGenerateFile(const std::string& realName,
  1290. const std::string& uiInputFile,
  1291. const std::string& uiOutputFile)
  1292. {
  1293. bool uicGenerated = false;
  1294. bool generateUic = this->GenerateAllUic;
  1295. const std::string uicFileRel =
  1296. this->AutogenBuildSubDir + "include/" + uiOutputFile;
  1297. const std::string uicFileAbs = this->CurrentBinaryDir + uicFileRel;
  1298. if (!generateUic) {
  1299. // Test if the source file is newer that the build file
  1300. generateUic = FileAbsentOrOlder(uicFileAbs, uiInputFile);
  1301. }
  1302. if (generateUic) {
  1303. // Log
  1304. this->LogBold("Generating UIC header " + uicFileRel);
  1305. // Make sure the parent directory exists
  1306. if (this->MakeParentDirectory(uicFileAbs)) {
  1307. // Compose uic command
  1308. std::vector<std::string> cmd;
  1309. cmd.push_back(this->UicExecutable);
  1310. {
  1311. std::vector<std::string> opts = this->UicTargetOptions;
  1312. std::map<std::string, std::string>::const_iterator optionIt =
  1313. this->UicOptions.find(uiInputFile);
  1314. if (optionIt != this->UicOptions.end()) {
  1315. std::vector<std::string> fileOpts;
  1316. cmSystemTools::ExpandListArgument(optionIt->second, fileOpts);
  1317. UicMergeOptions(opts, fileOpts, (this->QtMajorVersion == "5"));
  1318. }
  1319. cmd.insert(cmd.end(), opts.begin(), opts.end());
  1320. }
  1321. cmd.push_back("-o");
  1322. cmd.push_back(uicFileAbs);
  1323. cmd.push_back(uiInputFile);
  1324. // Log command
  1325. if (this->Verbose) {
  1326. this->LogCommand(cmd);
  1327. }
  1328. // Execute command
  1329. bool res = false;
  1330. int retVal = 0;
  1331. std::string output;
  1332. res = cmSystemTools::RunSingleCommand(cmd, &output, &output, &retVal);
  1333. if (!res || (retVal != 0)) {
  1334. // Command failed
  1335. {
  1336. std::ostringstream ost;
  1337. ost << "AutoUic: Error: uic process failed for\n";
  1338. ost << Quoted(uicFileRel) << " needed by\n";
  1339. ost << Quoted(realName) << "\n";
  1340. ost << "AutoUic: Command:\n" << cmJoin(cmd, " ") << "\n";
  1341. ost << "AutoUic: Command output:\n" << output << "\n";
  1342. this->LogError(ost.str());
  1343. }
  1344. cmSystemTools::RemoveFile(uicFileAbs);
  1345. this->RunUicFailed = true;
  1346. } else {
  1347. // Success
  1348. uicGenerated = true;
  1349. }
  1350. } else {
  1351. // Parent directory creation failed
  1352. this->RunUicFailed = true;
  1353. }
  1354. }
  1355. return uicGenerated;
  1356. }
  1357. bool cmQtAutoGenerators::RccGenerateAll()
  1358. {
  1359. if (!this->RccEnabled()) {
  1360. return true;
  1361. }
  1362. // generate single map with input / output names
  1363. std::map<std::string, std::string> qrcGenMap;
  1364. for (std::vector<std::string>::const_iterator si = this->RccSources.begin();
  1365. si != this->RccSources.end(); ++si) {
  1366. const std::string ext = cmsys::SystemTools::GetFilenameLastExtension(*si);
  1367. if (ext == ".qrc") {
  1368. qrcGenMap[*si] =
  1369. this->AutogenBuildSubDir + this->ChecksumedPath(*si, "qrc_", ".cpp");
  1370. }
  1371. }
  1372. // look for name collisions
  1373. {
  1374. std::multimap<std::string, std::string> collisions;
  1375. if (this->NameCollisionTest(qrcGenMap, collisions)) {
  1376. std::ostringstream ost;
  1377. ost << "AutoRcc: Error: The same qrc_NAME.cpp file"
  1378. " will be generated from different sources.\n"
  1379. "To avoid this error rename the source .qrc files.\n";
  1380. this->LogErrorNameCollision(ost.str(), collisions);
  1381. return false;
  1382. }
  1383. }
  1384. // generate qrc files
  1385. for (std::map<std::string, std::string>::const_iterator si =
  1386. qrcGenMap.begin();
  1387. si != qrcGenMap.end(); ++si) {
  1388. bool unique = FileNameIsUnique(si->first, qrcGenMap);
  1389. if (!this->RccGenerateFile(si->first, si->second, unique)) {
  1390. if (this->RunRccFailed) {
  1391. return false;
  1392. }
  1393. }
  1394. }
  1395. return true;
  1396. }
  1397. /**
  1398. * @return True if a rcc file was created. False may indicate an error.
  1399. */
  1400. bool cmQtAutoGenerators::RccGenerateFile(const std::string& rccInputFile,
  1401. const std::string& rccOutputFile,
  1402. bool unique_n)
  1403. {
  1404. bool rccGenerated = false;
  1405. bool generateRcc = this->GenerateAllRcc;
  1406. const std::string rccBuildFile = this->CurrentBinaryDir + rccOutputFile;
  1407. if (!generateRcc) {
  1408. // Test if the resources list file is newer than build file
  1409. generateRcc = FileAbsentOrOlder(rccBuildFile, rccInputFile);
  1410. if (!generateRcc) {
  1411. // Test if any resource file is newer than the build file
  1412. const std::vector<std::string>& files = this->RccInputs[rccInputFile];
  1413. for (std::vector<std::string>::const_iterator it = files.begin();
  1414. it != files.end(); ++it) {
  1415. if (FileAbsentOrOlder(rccBuildFile, *it)) {
  1416. generateRcc = true;
  1417. break;
  1418. }
  1419. }
  1420. }
  1421. }
  1422. if (generateRcc) {
  1423. // Log
  1424. this->LogBold("Generating RCC source " + rccOutputFile);
  1425. // Make sure the parent directory exists
  1426. if (this->MakeParentDirectory(rccBuildFile)) {
  1427. // Compose symbol name
  1428. std::string symbolName =
  1429. cmsys::SystemTools::GetFilenameWithoutLastExtension(rccInputFile);
  1430. if (!unique_n) {
  1431. symbolName += "_";
  1432. symbolName += fpathCheckSum.getPart(rccInputFile);
  1433. }
  1434. // Replace '-' with '_'. The former is valid for
  1435. // file names but not for symbol names.
  1436. std::replace(symbolName.begin(), symbolName.end(), '-', '_');
  1437. // Compose rcc command
  1438. std::vector<std::string> cmd;
  1439. cmd.push_back(this->RccExecutable);
  1440. {
  1441. std::map<std::string, std::string>::const_iterator optionIt =
  1442. this->RccOptions.find(rccInputFile);
  1443. if (optionIt != this->RccOptions.end()) {
  1444. cmSystemTools::ExpandListArgument(optionIt->second, cmd);
  1445. }
  1446. }
  1447. cmd.push_back("-name");
  1448. cmd.push_back(symbolName);
  1449. cmd.push_back("-o");
  1450. cmd.push_back(rccBuildFile);
  1451. cmd.push_back(rccInputFile);
  1452. // Log command
  1453. if (this->Verbose) {
  1454. this->LogCommand(cmd);
  1455. }
  1456. // Execute command
  1457. bool res = false;
  1458. int retVal = 0;
  1459. std::string output;
  1460. res = cmSystemTools::RunSingleCommand(cmd, &output, &output, &retVal);
  1461. if (!res || (retVal != 0)) {
  1462. // Command failed
  1463. {
  1464. std::ostringstream ost;
  1465. ost << "AutoRcc: Error: rcc process failed for\n";
  1466. ost << Quoted(rccOutputFile) << "\n";
  1467. ost << "AutoRcc: Command:\n" << cmJoin(cmd, " ") << "\n";
  1468. ost << "AutoRcc: Command output:\n" << output << "\n";
  1469. this->LogError(ost.str());
  1470. }
  1471. cmSystemTools::RemoveFile(rccBuildFile);
  1472. this->RunRccFailed = true;
  1473. } else {
  1474. // Success
  1475. rccGenerated = true;
  1476. }
  1477. } else {
  1478. // Parent directory creation failed
  1479. this->RunRccFailed = true;
  1480. }
  1481. }
  1482. return rccGenerated;
  1483. }
  1484. void cmQtAutoGenerators::LogErrorNameCollision(
  1485. const std::string& message,
  1486. const std::multimap<std::string, std::string>& collisions) const
  1487. {
  1488. typedef std::multimap<std::string, std::string>::const_iterator Iter;
  1489. std::ostringstream ost;
  1490. // Add message
  1491. if (!message.empty()) {
  1492. ost << message;
  1493. if (message[message.size() - 1] != '\n') {
  1494. ost << '\n';
  1495. }
  1496. }
  1497. // Append collision list
  1498. for (Iter it = collisions.begin(); it != collisions.end(); ++it) {
  1499. ost << it->first << " : " << it->second << '\n';
  1500. }
  1501. this->LogError(ost.str());
  1502. }
  1503. void cmQtAutoGenerators::LogBold(const std::string& message) const
  1504. {
  1505. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue |
  1506. cmsysTerminal_Color_ForegroundBold,
  1507. message.c_str(), true, this->ColorOutput);
  1508. }
  1509. void cmQtAutoGenerators::LogInfo(const std::string& message) const
  1510. {
  1511. std::string msg(message);
  1512. if (!msg.empty()) {
  1513. if (msg[msg.size() - 1] != '\n') {
  1514. msg.push_back('\n');
  1515. }
  1516. cmSystemTools::Stdout(msg.c_str(), msg.size());
  1517. }
  1518. }
  1519. void cmQtAutoGenerators::LogWarning(const std::string& message) const
  1520. {
  1521. std::string msg(message);
  1522. if (!msg.empty()) {
  1523. if (msg[msg.size() - 1] != '\n') {
  1524. msg.push_back('\n');
  1525. }
  1526. // Append empty line
  1527. msg.push_back('\n');
  1528. cmSystemTools::Stdout(msg.c_str(), msg.size());
  1529. }
  1530. }
  1531. void cmQtAutoGenerators::LogError(const std::string& message) const
  1532. {
  1533. std::string msg(message);
  1534. if (!msg.empty()) {
  1535. if (msg[msg.size() - 1] != '\n') {
  1536. msg.push_back('\n');
  1537. }
  1538. // Append empty line
  1539. msg.push_back('\n');
  1540. cmSystemTools::Stderr(msg.c_str(), msg.size());
  1541. }
  1542. }
  1543. void cmQtAutoGenerators::LogCommand(
  1544. const std::vector<std::string>& command) const
  1545. {
  1546. this->LogInfo(cmJoin(command, " "));
  1547. }
  1548. /**
  1549. * @brief Collects name collisions as output/input pairs
  1550. * @return True if there were collisions
  1551. */
  1552. bool cmQtAutoGenerators::NameCollisionTest(
  1553. const std::map<std::string, std::string>& genFiles,
  1554. std::multimap<std::string, std::string>& collisions) const
  1555. {
  1556. typedef std::map<std::string, std::string>::const_iterator Iter;
  1557. typedef std::map<std::string, std::string>::value_type VType;
  1558. for (Iter ait = genFiles.begin(); ait != genFiles.end(); ++ait) {
  1559. bool first_match(true);
  1560. for (Iter bit = (++Iter(ait)); bit != genFiles.end(); ++bit) {
  1561. if (ait->second == bit->second) {
  1562. if (first_match) {
  1563. if (collisions.find(ait->second) != collisions.end()) {
  1564. // We already know of this collision from before
  1565. break;
  1566. }
  1567. collisions.insert(VType(ait->second, ait->first));
  1568. first_match = false;
  1569. }
  1570. collisions.insert(VType(bit->second, bit->first));
  1571. }
  1572. }
  1573. }
  1574. return !collisions.empty();
  1575. }
  1576. /**
  1577. * @brief Generates a file path based on the checksum of the source file path
  1578. * @return The path
  1579. */
  1580. std::string cmQtAutoGenerators::ChecksumedPath(const std::string& sourceFile,
  1581. const char* basePrefix,
  1582. const char* baseSuffix) const
  1583. {
  1584. std::string res = fpathCheckSum.getPart(sourceFile);
  1585. res += "/";
  1586. res += basePrefix;
  1587. res += cmsys::SystemTools::GetFilenameWithoutLastExtension(sourceFile);
  1588. res += baseSuffix;
  1589. return res;
  1590. }
  1591. /**
  1592. * @brief Tries to find the header file to the given file base path by
  1593. * appending different header extensions
  1594. * @return True on success
  1595. */
  1596. bool cmQtAutoGenerators::FindHeader(std::string& header,
  1597. const std::string& testBasePath) const
  1598. {
  1599. for (std::vector<std::string>::const_iterator ext =
  1600. this->HeaderExtensions.begin();
  1601. ext != this->HeaderExtensions.end(); ++ext) {
  1602. std::string testFilePath(testBasePath);
  1603. testFilePath += '.';
  1604. testFilePath += (*ext);
  1605. if (cmsys::SystemTools::FileExists(testFilePath.c_str())) {
  1606. header = testFilePath;
  1607. return true;
  1608. }
  1609. }
  1610. return false;
  1611. }
  1612. bool cmQtAutoGenerators::FindHeaderGlobal(
  1613. std::string& header, const std::string& testBasePath) const
  1614. {
  1615. for (std::vector<std::string>::const_iterator iit =
  1616. this->MocIncludePaths.begin();
  1617. iit != this->MocIncludePaths.end(); ++iit) {
  1618. const std::string fullPath = ((*iit) + '/' + testBasePath);
  1619. if (FindHeader(header, fullPath)) {
  1620. return true;
  1621. }
  1622. }
  1623. return false;
  1624. }
  1625. std::string cmQtAutoGenerators::FindMocHeader(const std::string& basePath,
  1626. const std::string& baseName,
  1627. const std::string& subDir) const
  1628. {
  1629. std::string header;
  1630. do {
  1631. if (!subDir.empty()) {
  1632. if (this->FindHeader(header, basePath + subDir + baseName)) {
  1633. break;
  1634. }
  1635. }
  1636. if (this->FindHeader(header, basePath + baseName)) {
  1637. break;
  1638. }
  1639. // Try include directories
  1640. if (this->FindHeaderGlobal(header, subDir + baseName)) {
  1641. break;
  1642. }
  1643. } while (false);
  1644. // Sanitize
  1645. if (!header.empty()) {
  1646. header = cmsys::SystemTools::GetRealPath(header);
  1647. }
  1648. return header;
  1649. }
  1650. bool cmQtAutoGenerators::FindIncludedFile(
  1651. std::string& absFile, const std::string& sourceFile,
  1652. const std::string& includeString) const
  1653. {
  1654. bool success = false;
  1655. // Search in vicinity of the source
  1656. {
  1657. std::string testPath = cmSystemTools::GetFilenamePath(sourceFile);
  1658. testPath += '/';
  1659. testPath += includeString;
  1660. if (cmsys::SystemTools::FileExists(testPath.c_str())) {
  1661. absFile = cmsys::SystemTools::GetRealPath(testPath);
  1662. success = true;
  1663. }
  1664. }
  1665. // Search in include directories
  1666. if (!success) {
  1667. for (std::vector<std::string>::const_iterator iit =
  1668. this->MocIncludePaths.begin();
  1669. iit != this->MocIncludePaths.end(); ++iit) {
  1670. const std::string fullPath = ((*iit) + '/' + includeString);
  1671. if (cmsys::SystemTools::FileExists(fullPath.c_str())) {
  1672. absFile = cmsys::SystemTools::GetRealPath(fullPath);
  1673. success = true;
  1674. break;
  1675. }
  1676. }
  1677. }
  1678. return success;
  1679. }
  1680. /**
  1681. * @brief Generates the parent directory of the given file on demand
  1682. * @return True on success
  1683. */
  1684. bool cmQtAutoGenerators::MakeParentDirectory(const std::string& filename) const
  1685. {
  1686. bool success = true;
  1687. const std::string dirName = cmSystemTools::GetFilenamePath(filename);
  1688. if (!dirName.empty()) {
  1689. success = cmsys::SystemTools::MakeDirectory(dirName);
  1690. if (!success) {
  1691. this->LogError("AutoGen: Error: Directory creation failed: " + dirName);
  1692. }
  1693. }
  1694. return success;
  1695. }