cmQtAutoGenerators.cxx 67 KB

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