cmQtAutoGenerators.cxx 66 KB

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