cmQtAutoGenerators.cxx 62 KB

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