cmQtAutoGenerators.cxx 65 KB

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