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