cmQtAutoGenerators.cxx 62 KB

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