cmQtAutoGenerators.cxx 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062
  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. // -- Meta
  333. InfoGetConfig(makefile, "AM_CONFIG_SUFFIX", config, this->ConfigSuffix);
  334. // - Old settings file
  335. {
  336. this->SettingsFile = cmSystemTools::CollapseFullPath(targetDirectory);
  337. cmSystemTools::ConvertToUnixSlashes(this->SettingsFile);
  338. this->SettingsFile += "/AutogenOldSettings";
  339. this->SettingsFile += this->ConfigSuffix;
  340. this->SettingsFile += ".cmake";
  341. }
  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";
  600. this->MocPredefsFileRel += this->ConfigSuffix;
  601. this->MocPredefsFileRel += ".h";
  602. this->MocPredefsFileAbs = cmSystemTools::CollapseCombinedPath(
  603. this->AutogenBuildDir, this->MocPredefsFileRel);
  604. }
  605. // Init file path checksum generator
  606. FPathChecksum.setupParentDirs(this->CurrentSourceDir, this->CurrentBinaryDir,
  607. this->ProjectSourceDir,
  608. this->ProjectBinaryDir);
  609. // Acquire header extensions
  610. this->HeaderExtensions = makefile->GetCMakeInstance()->GetHeaderExtensions();
  611. // Sort include directories on demand
  612. if (this->IncludeProjectDirsBefore) {
  613. // Move strings to temporary list
  614. std::list<std::string> includes;
  615. includes.insert(includes.end(), this->MocIncludePaths.begin(),
  616. this->MocIncludePaths.end());
  617. this->MocIncludePaths.clear();
  618. this->MocIncludePaths.reserve(includes.size());
  619. // Append project directories only
  620. {
  621. const char* movePaths[2] = { this->ProjectBinaryDir.c_str(),
  622. this->ProjectSourceDir.c_str() };
  623. for (const char* const* mpit = cmArrayBegin(movePaths);
  624. mpit != cmArrayEnd(movePaths); ++mpit) {
  625. std::list<std::string>::iterator it = includes.begin();
  626. while (it != includes.end()) {
  627. const std::string& path = *it;
  628. if (cmsys::SystemTools::StringStartsWith(path, *mpit)) {
  629. this->MocIncludePaths.push_back(path);
  630. it = includes.erase(it);
  631. } else {
  632. ++it;
  633. }
  634. }
  635. }
  636. }
  637. // Append remaining directories
  638. this->MocIncludePaths.insert(this->MocIncludePaths.end(), includes.begin(),
  639. includes.end());
  640. }
  641. // Compose moc includes list
  642. {
  643. std::set<std::string> frameworkPaths;
  644. for (std::vector<std::string>::const_iterator it =
  645. this->MocIncludePaths.begin();
  646. it != this->MocIncludePaths.end(); ++it) {
  647. const std::string& path = *it;
  648. this->MocIncludes.push_back("-I" + path);
  649. // Extract framework path
  650. if (cmHasLiteralSuffix(path, ".framework/Headers")) {
  651. // Go up twice to get to the framework root
  652. std::vector<std::string> pathComponents;
  653. cmsys::SystemTools::SplitPath(path, pathComponents);
  654. std::string frameworkPath = cmsys::SystemTools::JoinPath(
  655. pathComponents.begin(), pathComponents.end() - 2);
  656. frameworkPaths.insert(frameworkPath);
  657. }
  658. }
  659. // Append framework includes
  660. for (std::set<std::string>::const_iterator it = frameworkPaths.begin();
  661. it != frameworkPaths.end(); ++it) {
  662. this->MocIncludes.push_back("-F");
  663. this->MocIncludes.push_back(*it);
  664. }
  665. }
  666. }
  667. bool cmQtAutoGenerators::RunAutogen()
  668. {
  669. // the program goes through all .cpp files to see which moc files are
  670. // included. It is not really interesting how the moc file is named, but
  671. // what file the moc is created from. Once a moc is included the same moc
  672. // may not be included in the mocs_compilation.cpp file anymore.
  673. // OTOH if there's a header containing Q_OBJECT where no corresponding
  674. // moc file is included anywhere a moc_<filename>.cpp file is created and
  675. // included in the mocs_compilation.cpp file.
  676. // key = moc source filepath, value = moc output filepath
  677. std::map<std::string, std::string> mocsIncluded;
  678. std::map<std::string, std::string> mocsNotIncluded;
  679. std::map<std::string, std::set<std::string> > mocDepends;
  680. std::map<std::string, std::vector<std::string> > uisIncluded;
  681. // collects all headers which may need to be mocced
  682. std::set<std::string> mocHeaderFiles;
  683. std::set<std::string> uicHeaderFiles;
  684. // Parse sources
  685. for (std::vector<std::string>::const_iterator it = this->Sources.begin();
  686. it != this->Sources.end(); ++it) {
  687. const std::string& absFilename = cmsys::SystemTools::GetRealPath(*it);
  688. // Parse source file for MOC/UIC
  689. if (!this->ParseSourceFile(absFilename, mocsIncluded, mocDepends,
  690. uisIncluded, this->MocRelaxedMode)) {
  691. return false;
  692. }
  693. // Find additional headers
  694. this->SearchHeadersForSourceFile(absFilename, mocHeaderFiles,
  695. uicHeaderFiles);
  696. }
  697. // Parse headers
  698. for (std::vector<std::string>::const_iterator it = this->Headers.begin();
  699. it != this->Headers.end(); ++it) {
  700. const std::string& headerName = cmsys::SystemTools::GetRealPath(*it);
  701. if (!this->MocSkip(headerName)) {
  702. mocHeaderFiles.insert(headerName);
  703. }
  704. if (!this->UicSkip(headerName)) {
  705. uicHeaderFiles.insert(headerName);
  706. }
  707. }
  708. if (!this->ParseHeaders(mocHeaderFiles, uicHeaderFiles, mocsIncluded,
  709. mocsNotIncluded, mocDepends, uisIncluded)) {
  710. return false;
  711. };
  712. // Generate files
  713. if (!this->MocGenerateAll(mocsIncluded, mocsNotIncluded, mocDepends)) {
  714. return false;
  715. }
  716. if (!this->UicGenerateAll(uisIncluded)) {
  717. return false;
  718. }
  719. if (!this->RccGenerateAll()) {
  720. return false;
  721. }
  722. return true;
  723. }
  724. /**
  725. * @brief Tests if the C++ content requires moc processing
  726. * @return True if moc is required
  727. */
  728. bool cmQtAutoGenerators::MocRequired(const std::string& contentText,
  729. std::string* macroName)
  730. {
  731. for (unsigned int ii = 0; ii != cmArraySize(this->MocMacroFilters); ++ii) {
  732. MocMacroFilter& filter = this->MocMacroFilters[ii];
  733. // Run a simple find string operation before the expensive
  734. // regular expression check
  735. if (contentText.find(filter.first) != std::string::npos) {
  736. if (filter.second.find(contentText)) {
  737. // Return macro name on demand
  738. if (macroName != CM_NULLPTR) {
  739. *macroName = filter.first;
  740. }
  741. return true;
  742. }
  743. }
  744. }
  745. return false;
  746. }
  747. void cmQtAutoGenerators::MocFindDepends(
  748. const std::string& absFilename, const std::string& contentText,
  749. std::map<std::string, std::set<std::string> >& mocDepends)
  750. {
  751. for (std::vector<MocDependFilter>::iterator fit =
  752. this->MocDependFilters.begin();
  753. fit != this->MocDependFilters.end(); ++fit) {
  754. MocDependFilter& filter = *fit;
  755. // Run a simple find string operation before the expensive
  756. // regular expression check
  757. if (contentText.find(filter.key) != std::string::npos) {
  758. // Run regular expression check loop
  759. const std::string sourcePath = SubDirPrefix(absFilename);
  760. const char* contentChars = contentText.c_str();
  761. while (filter.regExp.find(contentChars)) {
  762. // Evaluate match
  763. const std::string match = filter.regExp.match(1);
  764. if (!match.empty()) {
  765. // Find the dependency file
  766. std::string incFile;
  767. if (this->MocFindIncludedFile(incFile, sourcePath, match)) {
  768. mocDepends[absFilename].insert(incFile);
  769. if (this->Verbose) {
  770. this->LogInfo("AutoMoc: Found dependency:\n " +
  771. Quoted(absFilename) + "\n " + Quoted(incFile));
  772. }
  773. } else {
  774. this->LogWarning("AutoMoc: Warning: " + Quoted(absFilename) +
  775. "\n" + "Could not find dependency file " +
  776. Quoted(match));
  777. }
  778. }
  779. contentChars += filter.regExp.end();
  780. }
  781. }
  782. }
  783. }
  784. /**
  785. * @brief Tests if the file should be ignored for moc scanning
  786. * @return True if the file should be ignored
  787. */
  788. bool cmQtAutoGenerators::MocSkip(const std::string& absFilename) const
  789. {
  790. if (this->MocEnabled()) {
  791. // Test if the file name is on the skip list
  792. if (!ListContains(this->MocSkipList, absFilename)) {
  793. return false;
  794. }
  795. }
  796. return true;
  797. }
  798. /**
  799. * @brief Tests if the file name is in the skip list
  800. */
  801. bool cmQtAutoGenerators::UicSkip(const std::string& absFilename) const
  802. {
  803. if (this->UicEnabled()) {
  804. // Test if the file name is on the skip list
  805. if (!ListContains(this->UicSkipList, absFilename)) {
  806. return false;
  807. }
  808. }
  809. return true;
  810. }
  811. /**
  812. * @return True on success
  813. */
  814. bool cmQtAutoGenerators::ParseSourceFile(
  815. const std::string& absFilename,
  816. std::map<std::string, std::string>& mocsIncluded,
  817. std::map<std::string, std::set<std::string> >& mocDepends,
  818. std::map<std::string, std::vector<std::string> >& uisIncluded, bool relaxed)
  819. {
  820. std::string contentText;
  821. bool success = ReadAll(contentText, absFilename);
  822. if (success) {
  823. if (!contentText.empty()) {
  824. // Parse source contents for MOC
  825. if (success && !this->MocSkip(absFilename)) {
  826. success = this->MocParseSourceContent(
  827. absFilename, contentText, mocsIncluded, mocDepends, relaxed);
  828. }
  829. // Parse source contents for UIC
  830. if (success && !this->UicSkip(absFilename)) {
  831. this->UicParseContent(absFilename, contentText, uisIncluded);
  832. }
  833. } else {
  834. std::ostringstream ost;
  835. ost << "AutoGen: Warning: The file is empty:\n"
  836. << Quoted(absFilename) << "\n";
  837. this->LogWarning(ost.str());
  838. }
  839. } else {
  840. std::ostringstream ost;
  841. ost << "AutoGen: Error: Could not read file:\n" << Quoted(absFilename);
  842. this->LogError(ost.str());
  843. }
  844. return success;
  845. }
  846. void cmQtAutoGenerators::UicParseContent(
  847. const std::string& absFilename, const std::string& contentText,
  848. std::map<std::string, std::vector<std::string> >& uisIncluded)
  849. {
  850. if (this->Verbose) {
  851. this->LogInfo("AutoUic: Checking " + absFilename);
  852. }
  853. const char* contentChars = contentText.c_str();
  854. if (strstr(contentChars, "ui_") != CM_NULLPTR) {
  855. while (this->UicRegExpInclude.find(contentChars)) {
  856. uisIncluded[absFilename].push_back(this->UicRegExpInclude.match(1));
  857. contentChars += this->UicRegExpInclude.end();
  858. }
  859. }
  860. }
  861. /**
  862. * @return True on success
  863. */
  864. bool cmQtAutoGenerators::MocParseSourceContent(
  865. const std::string& absFilename, const std::string& contentText,
  866. std::map<std::string, std::string>& mocsIncluded,
  867. std::map<std::string, std::set<std::string> >& mocDepends, bool relaxed)
  868. {
  869. if (this->Verbose) {
  870. this->LogInfo("AutoMoc: Checking " + absFilename);
  871. }
  872. const std::string scannedFileAbsPath = SubDirPrefix(absFilename);
  873. const std::string scannedFileBasename =
  874. cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename);
  875. std::string macroName;
  876. const bool requiresMoc = this->MocRequired(contentText, &macroName);
  877. bool ownDotMocIncluded = false;
  878. std::string ownMocUnderscoreInclude;
  879. std::string ownMocUnderscoreHeader;
  880. // first a simple string check for "moc" is *much* faster than the regexp,
  881. // and if the string search already fails, we don't have to try the
  882. // expensive regexp
  883. const char* contentChars = contentText.c_str();
  884. if (strstr(contentChars, "moc") != CM_NULLPTR) {
  885. // Iterate over all included moc files
  886. while (this->MocRegExpInclude.find(contentChars)) {
  887. const std::string incString = this->MocRegExpInclude.match(1);
  888. // Basename of the moc include
  889. const std::string incSubDir(SubDirPrefix(incString));
  890. const std::string incBasename =
  891. cmsys::SystemTools::GetFilenameWithoutLastExtension(incString);
  892. // If the moc include is of the moc_foo.cpp style we expect
  893. // the Q_OBJECT class declaration in a header file.
  894. // If the moc include is of the foo.moc style we need to look for
  895. // a Q_OBJECT macro in the current source file, if it contains the
  896. // macro we generate the moc file from the source file.
  897. if (cmHasLiteralPrefix(incBasename, "moc_")) {
  898. // Include: moc_FOO.cxx
  899. // Remove the moc_ part
  900. const std::string incRealBasename = incBasename.substr(4);
  901. const std::string headerToMoc =
  902. this->MocFindHeader(scannedFileAbsPath, incSubDir + incRealBasename);
  903. if (!headerToMoc.empty()) {
  904. if (!this->MocSkip(headerToMoc)) {
  905. // Register moc job
  906. mocsIncluded[headerToMoc] = incString;
  907. this->MocFindDepends(headerToMoc, contentText, mocDepends);
  908. // Store meta information for relaxed mode
  909. if (relaxed && (incRealBasename == scannedFileBasename)) {
  910. ownMocUnderscoreInclude = incString;
  911. ownMocUnderscoreHeader = headerToMoc;
  912. }
  913. }
  914. } else {
  915. std::ostringstream ost;
  916. ost << "AutoMoc: Error: " << Quoted(absFilename) << "\n"
  917. << "The file includes the moc file " << Quoted(incString)
  918. << ", but could not find header "
  919. << Quoted(incRealBasename + "{" +
  920. JoinExts(this->HeaderExtensions) + "}");
  921. ;
  922. this->LogError(ost.str());
  923. return false;
  924. }
  925. } else {
  926. // Include: FOO.moc
  927. std::string fileToMoc;
  928. if (relaxed) {
  929. // Mode: Relaxed
  930. if (requiresMoc && (incBasename == scannedFileBasename)) {
  931. // Include self
  932. fileToMoc = absFilename;
  933. ownDotMocIncluded = true;
  934. } else {
  935. // In relaxed mode try to find a header instead but issue a warning
  936. const std::string headerToMoc =
  937. this->MocFindHeader(scannedFileAbsPath, incSubDir + incBasename);
  938. if (!headerToMoc.empty()) {
  939. if (!this->MocSkip(headerToMoc)) {
  940. // This is for KDE4 compatibility:
  941. fileToMoc = headerToMoc;
  942. if (!requiresMoc && (incBasename == scannedFileBasename)) {
  943. std::ostringstream ost;
  944. ost
  945. << "AutoMoc: Warning: " << Quoted(absFilename) << "\n"
  946. << "The file includes the moc file " << Quoted(incString)
  947. << ", but does not contain a Q_OBJECT or Q_GADGET macro.\n"
  948. << "Running moc on " << Quoted(headerToMoc) << "!\n"
  949. << "Include " << Quoted("moc_" + incBasename + ".cpp")
  950. << " for a compatibility with strict mode (see "
  951. "CMAKE_AUTOMOC_RELAXED_MODE).\n";
  952. this->LogWarning(ost.str());
  953. } else {
  954. std::ostringstream ost;
  955. ost << "AutoMoc: Warning: " << Quoted(absFilename) << "\n"
  956. << "The file includes the moc file " << Quoted(incString)
  957. << " instead of "
  958. << Quoted("moc_" + incBasename + ".cpp") << ".\n"
  959. << "Running moc on " << Quoted(headerToMoc) << "!\n"
  960. << "Include " << Quoted("moc_" + incBasename + ".cpp")
  961. << " for compatibility with strict mode (see "
  962. "CMAKE_AUTOMOC_RELAXED_MODE).\n";
  963. this->LogWarning(ost.str());
  964. }
  965. }
  966. } else {
  967. std::ostringstream ost;
  968. ost << "AutoMoc: Error: " << Quoted(absFilename) << "\n"
  969. << "The file includes the moc file " << Quoted(incString)
  970. << ", which seems to be the moc file from a different "
  971. "source file. CMake also could not find a matching "
  972. "header.";
  973. this->LogError(ost.str());
  974. return false;
  975. }
  976. }
  977. } else {
  978. // Mode: Strict
  979. if (incBasename == scannedFileBasename) {
  980. // Include self
  981. fileToMoc = absFilename;
  982. ownDotMocIncluded = true;
  983. // Accept but issue a warning if moc isn't required
  984. if (!requiresMoc) {
  985. std::ostringstream ost;
  986. ost << "AutoMoc: Warning: " << Quoted(absFilename) << "\n"
  987. << "The file includes the moc file " << Quoted(incString)
  988. << ", but does not contain a Q_OBJECT or Q_GADGET "
  989. "macro.";
  990. this->LogWarning(ost.str());
  991. }
  992. } else {
  993. // Don't allow FOO.moc include other than self in strict mode
  994. std::ostringstream ost;
  995. ost << "AutoMoc: Error: " << Quoted(absFilename) << "\n"
  996. << "The file includes the moc file " << Quoted(incString)
  997. << ", which seems to be the moc file from a different "
  998. "source file. This is not supported. Include "
  999. << Quoted(scannedFileBasename + ".moc")
  1000. << " to run moc on this source file.";
  1001. this->LogError(ost.str());
  1002. return false;
  1003. }
  1004. }
  1005. if (!fileToMoc.empty()) {
  1006. mocsIncluded[fileToMoc] = incString;
  1007. this->MocFindDepends(fileToMoc, contentText, mocDepends);
  1008. }
  1009. }
  1010. // Forward content pointer
  1011. contentChars += this->MocRegExpInclude.end();
  1012. }
  1013. }
  1014. if (requiresMoc && !ownDotMocIncluded) {
  1015. // In this case, check whether the scanned file itself contains a Q_OBJECT.
  1016. // If this is the case, the moc_foo.cpp should probably be generated from
  1017. // foo.cpp instead of foo.h, because otherwise it won't build.
  1018. // But warn, since this is not how it is supposed to be used.
  1019. if (relaxed && !ownMocUnderscoreInclude.empty()) {
  1020. // This is for KDE4 compatibility:
  1021. std::ostringstream ost;
  1022. ost << "AutoMoc: Warning: " << Quoted(absFilename) << "\n"
  1023. << "The file contains a " << macroName
  1024. << " macro, but does not include "
  1025. << Quoted(scannedFileBasename + ".moc") << ", but instead includes "
  1026. << Quoted(ownMocUnderscoreInclude) << ".\n"
  1027. << "Running moc on " << Quoted(absFilename) << "!\n"
  1028. << "Better include " << Quoted(scannedFileBasename + ".moc")
  1029. << " for compatibility with strict mode (see "
  1030. "CMAKE_AUTOMOC_RELAXED_MODE).";
  1031. this->LogWarning(ost.str());
  1032. // Use scanned source file instead of scanned header file as moc source
  1033. mocsIncluded[absFilename] = ownMocUnderscoreInclude;
  1034. this->MocFindDepends(absFilename, contentText, mocDepends);
  1035. // Remove
  1036. mocsIncluded.erase(ownMocUnderscoreHeader);
  1037. } else {
  1038. // Otherwise always error out since it will not compile:
  1039. std::ostringstream ost;
  1040. ost << "AutoMoc: Error: " << Quoted(absFilename) << "\n"
  1041. << "The file contains a " << macroName
  1042. << " macro, but does not include "
  1043. << Quoted(scannedFileBasename + ".moc") << "!\n"
  1044. << "Consider adding the include or enabling SKIP_AUTOMOC for this "
  1045. "file.";
  1046. this->LogError(ost.str());
  1047. return false;
  1048. }
  1049. }
  1050. return true;
  1051. }
  1052. void cmQtAutoGenerators::MocParseHeaderContent(
  1053. const std::string& absFilename, const std::string& contentText,
  1054. std::map<std::string, std::string>& mocsNotIncluded,
  1055. std::map<std::string, std::set<std::string> >& mocDepends)
  1056. {
  1057. // Log
  1058. if (this->Verbose) {
  1059. this->LogInfo("AutoMoc: Checking " + absFilename);
  1060. }
  1061. if (this->MocRequired(contentText)) {
  1062. // Register moc job
  1063. mocsNotIncluded[absFilename] =
  1064. this->ChecksumedPath(absFilename, "moc_", this->ConfigSuffix + ".cpp");
  1065. this->MocFindDepends(absFilename, contentText, mocDepends);
  1066. }
  1067. }
  1068. void cmQtAutoGenerators::SearchHeadersForSourceFile(
  1069. const std::string& absFilename, std::set<std::string>& mocHeaderFiles,
  1070. std::set<std::string>& uicHeaderFiles) const
  1071. {
  1072. std::string basepaths[2];
  1073. {
  1074. std::string bpath = SubDirPrefix(absFilename);
  1075. bpath += cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename);
  1076. // search for default header files and private header files
  1077. basepaths[0] = bpath;
  1078. basepaths[1] = bpath + "_p";
  1079. }
  1080. for (const std::string* bpit = cmArrayBegin(basepaths);
  1081. bpit != cmArrayEnd(basepaths); ++bpit) {
  1082. std::string headerName;
  1083. if (this->FindHeader(headerName, *bpit)) {
  1084. // Moc headers
  1085. if (!this->MocSkip(absFilename) && !this->MocSkip(headerName)) {
  1086. mocHeaderFiles.insert(headerName);
  1087. }
  1088. // Uic headers
  1089. if (!this->UicSkip(absFilename) && !this->UicSkip(headerName)) {
  1090. uicHeaderFiles.insert(headerName);
  1091. }
  1092. }
  1093. }
  1094. }
  1095. bool cmQtAutoGenerators::ParseHeaders(
  1096. const std::set<std::string>& mocHeaderFiles,
  1097. const std::set<std::string>& uicHeaderFiles,
  1098. const std::map<std::string, std::string>& mocsIncluded,
  1099. std::map<std::string, std::string>& mocsNotIncluded,
  1100. std::map<std::string, std::set<std::string> >& mocDepends,
  1101. std::map<std::string, std::vector<std::string> >& uisIncluded)
  1102. {
  1103. bool success = true;
  1104. // Merged header files list to read files only once
  1105. std::set<std::string> headerFiles;
  1106. headerFiles.insert(mocHeaderFiles.begin(), mocHeaderFiles.end());
  1107. headerFiles.insert(uicHeaderFiles.begin(), uicHeaderFiles.end());
  1108. for (std::set<std::string>::const_iterator hIt = headerFiles.begin();
  1109. hIt != headerFiles.end(); ++hIt) {
  1110. const std::string& headerName = *hIt;
  1111. std::string contentText;
  1112. if (ReadAll(contentText, headerName)) {
  1113. // Parse header content for MOC
  1114. if ((mocHeaderFiles.find(headerName) != mocHeaderFiles.end()) &&
  1115. (mocsIncluded.find(headerName) == mocsIncluded.end())) {
  1116. this->MocParseHeaderContent(headerName, contentText, mocsNotIncluded,
  1117. mocDepends);
  1118. }
  1119. // Parse header content for UIC
  1120. if (uicHeaderFiles.find(headerName) != uicHeaderFiles.end()) {
  1121. this->UicParseContent(headerName, contentText, uisIncluded);
  1122. }
  1123. } else {
  1124. std::ostringstream ost;
  1125. ost << "AutoGen: Error: Could not read header file:\n"
  1126. << Quoted(headerName);
  1127. this->LogError(ost.str());
  1128. success = false;
  1129. break;
  1130. }
  1131. }
  1132. return success;
  1133. }
  1134. bool cmQtAutoGenerators::MocGenerateAll(
  1135. const std::map<std::string, std::string>& mocsIncluded,
  1136. const std::map<std::string, std::string>& mocsNotIncluded,
  1137. const std::map<std::string, std::set<std::string> >& mocDepends)
  1138. {
  1139. if (!this->MocEnabled()) {
  1140. return true;
  1141. }
  1142. // Look for name collisions
  1143. {
  1144. std::multimap<std::string, std::string> collisions;
  1145. // Test merged map of included and notIncluded
  1146. std::map<std::string, std::string> mergedMocs(mocsIncluded);
  1147. mergedMocs.insert(mocsNotIncluded.begin(), mocsNotIncluded.end());
  1148. if (this->NameCollisionTest(mergedMocs, collisions)) {
  1149. std::ostringstream ost;
  1150. ost << "AutoMoc: Error: "
  1151. "The same moc file will be generated "
  1152. "from different sources.\n"
  1153. "To avoid this error either\n"
  1154. "- rename the source files or\n"
  1155. "- do not include the (moc_NAME.cpp|NAME.moc) file";
  1156. this->LogErrorNameCollision(ost.str(), collisions);
  1157. return false;
  1158. }
  1159. }
  1160. // Generate moc_predefs
  1161. if (!this->MocPredefsCmd.empty()) {
  1162. if (this->MocSettingsChanged ||
  1163. FileAbsentOrOlder(this->MocPredefsFileAbs, this->SettingsFile)) {
  1164. this->LogBold("Generating MOC predefs " + this->MocPredefsFileRel);
  1165. std::string output;
  1166. {
  1167. // Compose command
  1168. std::vector<std::string> cmd = this->MocPredefsCmd;
  1169. // Add includes
  1170. cmd.insert(cmd.end(), this->MocIncludes.begin(),
  1171. this->MocIncludes.end());
  1172. // Add definitions
  1173. for (std::vector<std::string>::const_iterator it =
  1174. this->MocDefinitions.begin();
  1175. it != this->MocDefinitions.end(); ++it) {
  1176. cmd.push_back("-D" + (*it));
  1177. }
  1178. // Add options
  1179. cmd.insert(cmd.end(), this->MocOptions.begin(),
  1180. this->MocOptions.end());
  1181. // Execute command
  1182. if (!this->RunCommand(cmd, output, false)) {
  1183. {
  1184. std::ostringstream ost;
  1185. ost << "AutoMoc: Error: moc predefs generation command failed\n";
  1186. ost << "AutoMoc: Command:\n" << QuotedCommand(cmd) << "\n";
  1187. ost << "AutoMoc: Command output:\n" << output << "\n";
  1188. this->LogError(ost.str());
  1189. }
  1190. return false;
  1191. }
  1192. }
  1193. // (Re)write predefs file only on demand
  1194. if (this->FileDiffers(this->MocPredefsFileAbs, output)) {
  1195. if (this->FileWrite("AutoMoc", this->MocPredefsFileAbs, output)) {
  1196. this->MocPredefsChanged = true;
  1197. } else {
  1198. return false;
  1199. }
  1200. }
  1201. }
  1202. }
  1203. // Generate moc files that are included by source files.
  1204. for (std::map<std::string, std::string>::const_iterator it =
  1205. mocsIncluded.begin();
  1206. it != mocsIncluded.end(); ++it) {
  1207. if (!this->MocGenerateFile(it->first, it->second, mocDepends, true)) {
  1208. if (this->MocRunFailed) {
  1209. return false;
  1210. }
  1211. }
  1212. }
  1213. // Generate moc files that are _not_ included by source files.
  1214. bool mocCompFileGenerated = false;
  1215. for (std::map<std::string, std::string>::const_iterator it =
  1216. mocsNotIncluded.begin();
  1217. it != mocsNotIncluded.end(); ++it) {
  1218. if (this->MocGenerateFile(it->first, it->second, mocDepends, false)) {
  1219. mocCompFileGenerated = true;
  1220. } else {
  1221. if (this->MocRunFailed) {
  1222. return false;
  1223. }
  1224. }
  1225. }
  1226. // Compose mocs compilation file content
  1227. std::string automocSource;
  1228. {
  1229. std::ostringstream ost;
  1230. ost << "/* This file is autogenerated, do not edit*/\n";
  1231. if (mocsNotIncluded.empty()) {
  1232. // Dummy content
  1233. ost << "enum some_compilers { need_more_than_nothing };\n";
  1234. } else {
  1235. // Valid content
  1236. for (std::map<std::string, std::string>::const_iterator it =
  1237. mocsNotIncluded.begin();
  1238. it != mocsNotIncluded.end(); ++it) {
  1239. ost << "#include \"" << it->second << "\"\n";
  1240. }
  1241. }
  1242. automocSource = ost.str();
  1243. }
  1244. if (this->FileDiffers(this->MocCompFileAbs, automocSource)) {
  1245. // Actually write mocs compilation file
  1246. this->LogBold("Generating MOC compilation " + this->MocCompFileRel);
  1247. if (!this->FileWrite("AutoMoc", this->MocCompFileAbs, automocSource)) {
  1248. return false;
  1249. }
  1250. } else if (mocCompFileGenerated) {
  1251. // Only touch mocs compilation file
  1252. if (this->Verbose) {
  1253. this->LogInfo("Touching MOC compilation " + this->MocCompFileRel);
  1254. }
  1255. cmSystemTools::Touch(this->MocCompFileAbs, false);
  1256. }
  1257. return true;
  1258. }
  1259. /**
  1260. * @return True if a moc file was created. False may indicate an error.
  1261. */
  1262. bool cmQtAutoGenerators::MocGenerateFile(
  1263. const std::string& sourceFile, const std::string& mocFileName,
  1264. const std::map<std::string, std::set<std::string> >& mocDepends,
  1265. bool included)
  1266. {
  1267. bool mocGenerated = false;
  1268. bool generateMoc = this->MocSettingsChanged || this->MocPredefsChanged;
  1269. const std::string mocFileRel =
  1270. included ? (this->AutogenIncludeDir + mocFileName) : mocFileName;
  1271. const std::string mocFileAbs =
  1272. cmSystemTools::CollapseCombinedPath(this->AutogenBuildDir, mocFileRel);
  1273. if (!generateMoc) {
  1274. // Test if the source file is newer that the build file
  1275. generateMoc = FileAbsentOrOlder(mocFileAbs, sourceFile);
  1276. if (!generateMoc) {
  1277. // Test if a dependency file changed
  1278. std::map<std::string, std::set<std::string> >::const_iterator dit =
  1279. mocDepends.find(sourceFile);
  1280. if (dit != mocDepends.end()) {
  1281. for (std::set<std::string>::const_iterator fit = dit->second.begin();
  1282. fit != dit->second.end(); ++fit) {
  1283. if (FileAbsentOrOlder(mocFileAbs, *fit)) {
  1284. generateMoc = true;
  1285. break;
  1286. }
  1287. }
  1288. }
  1289. }
  1290. }
  1291. if (generateMoc) {
  1292. // Log
  1293. this->LogBold("Generating MOC source " + mocFileRel);
  1294. // Make sure the parent directory exists
  1295. if (this->MakeParentDirectory("AutoMoc", mocFileAbs)) {
  1296. // Compose moc command
  1297. std::vector<std::string> cmd;
  1298. cmd.push_back(this->MocExecutable);
  1299. // Add includes
  1300. cmd.insert(cmd.end(), this->MocIncludes.begin(),
  1301. this->MocIncludes.end());
  1302. // Add definitions
  1303. for (std::vector<std::string>::const_iterator it =
  1304. this->MocDefinitions.begin();
  1305. it != this->MocDefinitions.end(); ++it) {
  1306. cmd.push_back("-D" + (*it));
  1307. }
  1308. // Add options
  1309. cmd.insert(cmd.end(), this->MocOptions.begin(), this->MocOptions.end());
  1310. // Add predefs include
  1311. if (!this->MocPredefsFileAbs.empty()) {
  1312. cmd.push_back("--include");
  1313. cmd.push_back(this->MocPredefsFileAbs);
  1314. }
  1315. cmd.push_back("-o");
  1316. cmd.push_back(mocFileAbs);
  1317. cmd.push_back(sourceFile);
  1318. // Execute moc command
  1319. std::string output;
  1320. if (this->RunCommand(cmd, output)) {
  1321. // Success
  1322. mocGenerated = true;
  1323. } else {
  1324. // Command failed
  1325. {
  1326. std::ostringstream ost;
  1327. ost << "AutoMoc: Error: moc process failed for\n";
  1328. ost << Quoted(mocFileRel) << "\n";
  1329. ost << "AutoMoc: Command:\n" << QuotedCommand(cmd) << "\n";
  1330. ost << "AutoMoc: Command output:\n" << output << "\n";
  1331. this->LogError(ost.str());
  1332. }
  1333. cmSystemTools::RemoveFile(mocFileAbs);
  1334. this->MocRunFailed = true;
  1335. }
  1336. } else {
  1337. // Parent directory creation failed
  1338. this->MocRunFailed = true;
  1339. }
  1340. }
  1341. return mocGenerated;
  1342. }
  1343. bool cmQtAutoGenerators::UicFindIncludedFile(std::string& absFile,
  1344. const std::string& sourceFile,
  1345. const std::string& includeString)
  1346. {
  1347. bool success = false;
  1348. // Search in vicinity of the source
  1349. {
  1350. std::string testPath = SubDirPrefix(sourceFile);
  1351. testPath += includeString;
  1352. if (cmsys::SystemTools::FileExists(testPath.c_str())) {
  1353. absFile = cmsys::SystemTools::GetRealPath(testPath);
  1354. success = true;
  1355. }
  1356. }
  1357. // Search in include directories
  1358. if (!success) {
  1359. for (std::vector<std::string>::const_iterator iit =
  1360. this->UicSearchPaths.begin();
  1361. iit != this->UicSearchPaths.end(); ++iit) {
  1362. const std::string fullPath = ((*iit) + '/' + includeString);
  1363. if (cmsys::SystemTools::FileExists(fullPath.c_str())) {
  1364. absFile = cmsys::SystemTools::GetRealPath(fullPath);
  1365. success = true;
  1366. break;
  1367. }
  1368. }
  1369. }
  1370. return success;
  1371. }
  1372. bool cmQtAutoGenerators::UicGenerateAll(
  1373. const std::map<std::string, std::vector<std::string> >& uisIncluded)
  1374. {
  1375. if (!this->UicEnabled()) {
  1376. return true;
  1377. }
  1378. // single map with input / output names
  1379. std::map<std::string, std::map<std::string, std::string> > sourceGenMap;
  1380. {
  1381. // Collision lookup map
  1382. std::map<std::string, std::string> testMap;
  1383. // Compile maps
  1384. for (std::map<std::string, std::vector<std::string> >::const_iterator sit =
  1385. uisIncluded.begin();
  1386. sit != uisIncluded.end(); ++sit) {
  1387. const std::string& source(sit->first);
  1388. const std::vector<std::string>& sourceIncs(sit->second);
  1389. // insert new source/destination map
  1390. std::map<std::string, std::string>& uiGenMap = sourceGenMap[source];
  1391. for (std::vector<std::string>::const_iterator uit = sourceIncs.begin();
  1392. uit != sourceIncs.end(); ++uit) {
  1393. // Remove ui_ from the begin filename by substr()
  1394. const std::string uiBasePath = SubDirPrefix(*uit);
  1395. const std::string uiBaseName =
  1396. cmsys::SystemTools::GetFilenameWithoutLastExtension(*uit).substr(3);
  1397. const std::string searchFileName = uiBasePath + uiBaseName + ".ui";
  1398. std::string uiInputFile;
  1399. if (UicFindIncludedFile(uiInputFile, source, searchFileName)) {
  1400. std::string uiOutputFile = uiBasePath + "ui_" + uiBaseName + ".h";
  1401. cmSystemTools::ReplaceString(uiOutputFile, "..", "__");
  1402. uiGenMap[uiInputFile] = uiOutputFile;
  1403. testMap[uiInputFile] = uiOutputFile;
  1404. } else {
  1405. this->LogError("AutoUic: Error: " + Quoted(sit->first) +
  1406. "\nCould not find " + Quoted(searchFileName));
  1407. return false;
  1408. }
  1409. }
  1410. }
  1411. // look for name collisions
  1412. {
  1413. std::multimap<std::string, std::string> collisions;
  1414. if (this->NameCollisionTest(testMap, collisions)) {
  1415. std::ostringstream ost;
  1416. ost << "AutoUic: Error: The same ui_NAME.h file will be generated "
  1417. "from different sources.\n"
  1418. "To avoid this error rename the source files.\n";
  1419. this->LogErrorNameCollision(ost.str(), collisions);
  1420. return false;
  1421. }
  1422. }
  1423. }
  1424. // generate ui files
  1425. for (std::map<std::string,
  1426. std::map<std::string, std::string> >::const_iterator it =
  1427. sourceGenMap.begin();
  1428. it != sourceGenMap.end(); ++it) {
  1429. for (std::map<std::string, std::string>::const_iterator sit =
  1430. it->second.begin();
  1431. sit != it->second.end(); ++sit) {
  1432. if (!this->UicGenerateFile(it->first, sit->first, sit->second)) {
  1433. if (this->UicRunFailed) {
  1434. return false;
  1435. }
  1436. }
  1437. }
  1438. }
  1439. return true;
  1440. }
  1441. /**
  1442. * @return True if a uic file was created. False may indicate an error.
  1443. */
  1444. bool cmQtAutoGenerators::UicGenerateFile(const std::string& realName,
  1445. const std::string& uiInputFile,
  1446. const std::string& uiOutputFile)
  1447. {
  1448. bool uicGenerated = false;
  1449. bool generateUic = this->UicSettingsChanged;
  1450. const std::string uicFileRel = this->AutogenIncludeDir + uiOutputFile;
  1451. const std::string uicFileAbs =
  1452. cmSystemTools::CollapseCombinedPath(this->AutogenBuildDir, uicFileRel);
  1453. if (!generateUic) {
  1454. // Test if the source file is newer that the build file
  1455. generateUic = FileAbsentOrOlder(uicFileAbs, uiInputFile);
  1456. }
  1457. if (generateUic) {
  1458. // Log
  1459. this->LogBold("Generating UIC header " + uicFileRel);
  1460. // Make sure the parent directory exists
  1461. if (this->MakeParentDirectory("AutoUic", uicFileAbs)) {
  1462. // Compose uic command
  1463. std::vector<std::string> cmd;
  1464. cmd.push_back(this->UicExecutable);
  1465. {
  1466. std::vector<std::string> allOpts = this->UicTargetOptions;
  1467. std::map<std::string, std::string>::const_iterator optionIt =
  1468. this->UicOptions.find(uiInputFile);
  1469. if (optionIt != this->UicOptions.end()) {
  1470. std::vector<std::string> fileOpts;
  1471. cmSystemTools::ExpandListArgument(optionIt->second, fileOpts);
  1472. UicMergeOptions(allOpts, fileOpts, (this->QtMajorVersion == "5"));
  1473. }
  1474. cmd.insert(cmd.end(), allOpts.begin(), allOpts.end());
  1475. }
  1476. cmd.push_back("-o");
  1477. cmd.push_back(uicFileAbs);
  1478. cmd.push_back(uiInputFile);
  1479. std::string output;
  1480. if (this->RunCommand(cmd, output)) {
  1481. // Success
  1482. uicGenerated = true;
  1483. } else {
  1484. // Command failed
  1485. {
  1486. std::ostringstream ost;
  1487. ost << "AutoUic: Error: uic process failed for\n";
  1488. ost << Quoted(uicFileRel) << " needed by\n";
  1489. ost << Quoted(realName) << "\n";
  1490. ost << "AutoUic: Command:\n" << QuotedCommand(cmd) << "\n";
  1491. ost << "AutoUic: Command output:\n" << output << "\n";
  1492. this->LogError(ost.str());
  1493. }
  1494. cmSystemTools::RemoveFile(uicFileAbs);
  1495. this->UicRunFailed = true;
  1496. }
  1497. } else {
  1498. // Parent directory creation failed
  1499. this->UicRunFailed = true;
  1500. }
  1501. }
  1502. return uicGenerated;
  1503. }
  1504. bool cmQtAutoGenerators::RccGenerateAll()
  1505. {
  1506. if (!this->RccEnabled()) {
  1507. return true;
  1508. }
  1509. // generate single map with input / output names
  1510. std::map<std::string, std::string> qrcGenMap;
  1511. {
  1512. const std::string qrcPrefix = "qrc_";
  1513. const std::string qrcSuffix = this->ConfigSuffix + ".cpp";
  1514. for (std::vector<std::string>::const_iterator si =
  1515. this->RccSources.begin();
  1516. si != this->RccSources.end(); ++si) {
  1517. const std::string ext =
  1518. cmsys::SystemTools::GetFilenameLastExtension(*si);
  1519. if (ext == ".qrc") {
  1520. qrcGenMap[*si] = this->ChecksumedPath(*si, qrcPrefix, qrcSuffix);
  1521. }
  1522. }
  1523. }
  1524. // look for name collisions
  1525. {
  1526. std::multimap<std::string, std::string> collisions;
  1527. if (this->NameCollisionTest(qrcGenMap, collisions)) {
  1528. std::ostringstream ost;
  1529. ost << "AutoRcc: Error: The same qrc_NAME.cpp file"
  1530. " will be generated from different sources.\n"
  1531. "To avoid this error rename the source .qrc files.\n";
  1532. this->LogErrorNameCollision(ost.str(), collisions);
  1533. return false;
  1534. }
  1535. }
  1536. // generate qrc files
  1537. for (std::map<std::string, std::string>::const_iterator si =
  1538. qrcGenMap.begin();
  1539. si != qrcGenMap.end(); ++si) {
  1540. bool unique = FileNameIsUnique(si->first, qrcGenMap);
  1541. if (!this->RccGenerateFile(si->first, si->second, unique)) {
  1542. if (this->RccRunFailed) {
  1543. return false;
  1544. }
  1545. }
  1546. }
  1547. return true;
  1548. }
  1549. /**
  1550. * @return True if a rcc file was created. False may indicate an error.
  1551. */
  1552. bool cmQtAutoGenerators::RccGenerateFile(const std::string& rccInputFile,
  1553. const std::string& rccOutputFile,
  1554. bool unique_n)
  1555. {
  1556. bool rccGenerated = false;
  1557. bool generateRcc = this->RccSettingsChanged;
  1558. const std::string rccBuildFile =
  1559. cmSystemTools::CollapseCombinedPath(this->AutogenBuildDir, rccOutputFile);
  1560. // Check if regeneration is required
  1561. if (!generateRcc) {
  1562. // Test if the resources list file is newer than build file
  1563. generateRcc = FileAbsentOrOlder(rccBuildFile, rccInputFile);
  1564. if (!generateRcc) {
  1565. // Acquire input file list
  1566. std::vector<std::string> readFiles;
  1567. const std::vector<std::string>* files = &this->RccInputs[rccInputFile];
  1568. if (files->empty()) {
  1569. // Read input file list from qrc file
  1570. std::string error;
  1571. if (cmQtAutoGeneratorCommon::RccListInputs(
  1572. this->QtMajorVersion, this->RccExecutable, rccInputFile,
  1573. readFiles, &error)) {
  1574. files = &readFiles;
  1575. } else {
  1576. files = CM_NULLPTR;
  1577. this->LogError(error);
  1578. this->RccRunFailed = true;
  1579. }
  1580. }
  1581. // Test if any input file is newer than the build file
  1582. if (files != CM_NULLPTR) {
  1583. for (std::vector<std::string>::const_iterator it = files->begin();
  1584. it != files->end(); ++it) {
  1585. if (FileAbsentOrOlder(rccBuildFile, *it)) {
  1586. generateRcc = true;
  1587. break;
  1588. }
  1589. }
  1590. }
  1591. }
  1592. }
  1593. // Regenerate on demand
  1594. if (generateRcc) {
  1595. // Log
  1596. this->LogBold("Generating RCC source " + rccOutputFile);
  1597. // Make sure the parent directory exists
  1598. if (this->MakeParentDirectory("AutoRcc", rccBuildFile)) {
  1599. // Compose symbol name
  1600. std::string symbolName =
  1601. cmsys::SystemTools::GetFilenameWithoutLastExtension(rccInputFile);
  1602. if (!unique_n) {
  1603. symbolName += "_";
  1604. symbolName += FPathChecksum.getPart(rccInputFile);
  1605. }
  1606. // Replace '-' with '_'. The former is valid for
  1607. // file names but not for symbol names.
  1608. std::replace(symbolName.begin(), symbolName.end(), '-', '_');
  1609. // Compose rcc command
  1610. std::vector<std::string> cmd;
  1611. cmd.push_back(this->RccExecutable);
  1612. {
  1613. std::map<std::string, std::string>::const_iterator optionIt =
  1614. this->RccOptions.find(rccInputFile);
  1615. if (optionIt != this->RccOptions.end()) {
  1616. cmSystemTools::ExpandListArgument(optionIt->second, cmd);
  1617. }
  1618. }
  1619. cmd.push_back("-name");
  1620. cmd.push_back(symbolName);
  1621. cmd.push_back("-o");
  1622. cmd.push_back(rccBuildFile);
  1623. cmd.push_back(rccInputFile);
  1624. std::string output;
  1625. if (this->RunCommand(cmd, output)) {
  1626. // Success
  1627. rccGenerated = true;
  1628. } else {
  1629. // Command failed
  1630. {
  1631. std::ostringstream ost;
  1632. ost << "AutoRcc: Error: rcc process failed for\n";
  1633. ost << Quoted(rccOutputFile) << "\n";
  1634. ost << "AutoRcc: Command:\n" << QuotedCommand(cmd) << "\n";
  1635. ost << "AutoRcc: Command output:\n" << output << "\n";
  1636. this->LogError(ost.str());
  1637. }
  1638. cmSystemTools::RemoveFile(rccBuildFile);
  1639. this->RccRunFailed = true;
  1640. }
  1641. } else {
  1642. // Parent directory creation failed
  1643. this->RccRunFailed = true;
  1644. }
  1645. }
  1646. // For a multi configuration generator generate a wrapper file
  1647. if (!this->ConfigSuffix.empty() && !this->RccRunFailed) {
  1648. // Wrapper file name
  1649. const std::string cppSuffix = ".cpp";
  1650. const size_t suffixLength = this->ConfigSuffix.size() + cppSuffix.size();
  1651. const std::string wrapperFileRel =
  1652. rccOutputFile.substr(0, rccOutputFile.size() - suffixLength) + cppSuffix;
  1653. const std::string wrapperFileAbs = cmSystemTools::CollapseCombinedPath(
  1654. this->AutogenBuildDir, wrapperFileRel);
  1655. // Wrapper file content
  1656. std::string content =
  1657. "// This is an autogenerated configuration wrapper file. Do not edit.\n"
  1658. "#include \"";
  1659. content += cmsys::SystemTools::GetFilenameName(rccBuildFile);
  1660. content += "\"\n";
  1661. // Write content to file
  1662. if (this->FileDiffers(wrapperFileAbs, content)) {
  1663. // Write new wrapper file if the content differs
  1664. this->LogBold("Generating RCC wrapper " + wrapperFileRel);
  1665. if (!this->FileWrite("AutoRcc", wrapperFileAbs, content)) {
  1666. // Error
  1667. rccGenerated = false;
  1668. this->RccRunFailed = true;
  1669. }
  1670. } else if (rccGenerated) {
  1671. // Only touch wrapper file if the content matches
  1672. if (this->Verbose) {
  1673. this->LogInfo("Touching RCC wrapper " + wrapperFileRel);
  1674. }
  1675. cmSystemTools::Touch(wrapperFileAbs, false);
  1676. }
  1677. }
  1678. return rccGenerated;
  1679. }
  1680. void cmQtAutoGenerators::LogErrorNameCollision(
  1681. const std::string& message,
  1682. const std::multimap<std::string, std::string>& collisions) const
  1683. {
  1684. typedef std::multimap<std::string, std::string>::const_iterator Iter;
  1685. std::ostringstream ost;
  1686. // Add message
  1687. if (!message.empty()) {
  1688. ost << message;
  1689. if (message[message.size() - 1] != '\n') {
  1690. ost << '\n';
  1691. }
  1692. }
  1693. // Append collision list
  1694. for (Iter it = collisions.begin(); it != collisions.end(); ++it) {
  1695. ost << it->first << " : " << it->second << '\n';
  1696. }
  1697. this->LogError(ost.str());
  1698. }
  1699. void cmQtAutoGenerators::LogBold(const std::string& message) const
  1700. {
  1701. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue |
  1702. cmsysTerminal_Color_ForegroundBold,
  1703. message.c_str(), true, this->ColorOutput);
  1704. }
  1705. void cmQtAutoGenerators::LogInfo(const std::string& message) const
  1706. {
  1707. std::string msg(message);
  1708. if (!msg.empty()) {
  1709. if (msg[msg.size() - 1] != '\n') {
  1710. msg.push_back('\n');
  1711. }
  1712. cmSystemTools::Stdout(msg.c_str(), msg.size());
  1713. }
  1714. }
  1715. void cmQtAutoGenerators::LogWarning(const std::string& message) const
  1716. {
  1717. std::string msg(message);
  1718. if (!msg.empty()) {
  1719. if (msg[msg.size() - 1] != '\n') {
  1720. msg.push_back('\n');
  1721. }
  1722. // Append empty line
  1723. msg.push_back('\n');
  1724. cmSystemTools::Stdout(msg.c_str(), msg.size());
  1725. }
  1726. }
  1727. void cmQtAutoGenerators::LogError(const std::string& message) const
  1728. {
  1729. std::string msg(message);
  1730. if (!msg.empty()) {
  1731. if (msg[msg.size() - 1] != '\n') {
  1732. msg.push_back('\n');
  1733. }
  1734. // Append empty line
  1735. msg.push_back('\n');
  1736. cmSystemTools::Stderr(msg.c_str(), msg.size());
  1737. }
  1738. }
  1739. /**
  1740. * @brief Collects name collisions as output/input pairs
  1741. * @return True if there were collisions
  1742. */
  1743. bool cmQtAutoGenerators::NameCollisionTest(
  1744. const std::map<std::string, std::string>& genFiles,
  1745. std::multimap<std::string, std::string>& collisions) const
  1746. {
  1747. typedef std::map<std::string, std::string>::const_iterator Iter;
  1748. typedef std::map<std::string, std::string>::value_type VType;
  1749. for (Iter ait = genFiles.begin(); ait != genFiles.end(); ++ait) {
  1750. bool first_match(true);
  1751. for (Iter bit = (++Iter(ait)); bit != genFiles.end(); ++bit) {
  1752. if (ait->second == bit->second) {
  1753. if (first_match) {
  1754. if (collisions.find(ait->second) != collisions.end()) {
  1755. // We already know of this collision from before
  1756. break;
  1757. }
  1758. collisions.insert(VType(ait->second, ait->first));
  1759. first_match = false;
  1760. }
  1761. collisions.insert(VType(bit->second, bit->first));
  1762. }
  1763. }
  1764. }
  1765. return !collisions.empty();
  1766. }
  1767. /**
  1768. * @brief Generates a file path based on the checksum of the source file path
  1769. * @return The path
  1770. */
  1771. std::string cmQtAutoGenerators::ChecksumedPath(
  1772. const std::string& sourceFile, const std::string& basePrefix,
  1773. const std::string& baseSuffix) const
  1774. {
  1775. std::string res = FPathChecksum.getPart(sourceFile);
  1776. res += "/";
  1777. res += basePrefix;
  1778. res += cmsys::SystemTools::GetFilenameWithoutLastExtension(sourceFile);
  1779. res += baseSuffix;
  1780. return res;
  1781. }
  1782. /**
  1783. * @brief Generates the parent directory of the given file on demand
  1784. * @return True on success
  1785. */
  1786. bool cmQtAutoGenerators::MakeParentDirectory(const char* logPrefix,
  1787. const std::string& filename) const
  1788. {
  1789. bool success = true;
  1790. const std::string dirName = cmSystemTools::GetFilenamePath(filename);
  1791. if (!dirName.empty()) {
  1792. success = cmsys::SystemTools::MakeDirectory(dirName);
  1793. if (!success) {
  1794. std::string error = logPrefix;
  1795. error += ": Error: Parent directory creation failed for ";
  1796. error += Quoted(filename);
  1797. this->LogError(error);
  1798. }
  1799. }
  1800. return success;
  1801. }
  1802. bool cmQtAutoGenerators::FileDiffers(const std::string& filename,
  1803. const std::string& content)
  1804. {
  1805. bool differs = true;
  1806. {
  1807. std::string oldContents;
  1808. if (ReadAll(oldContents, filename)) {
  1809. differs = (oldContents != content);
  1810. }
  1811. }
  1812. return differs;
  1813. }
  1814. bool cmQtAutoGenerators::FileWrite(const char* logPrefix,
  1815. const std::string& filename,
  1816. const std::string& content)
  1817. {
  1818. std::string error;
  1819. // Make sure the parent directory exists
  1820. if (this->MakeParentDirectory(logPrefix, filename)) {
  1821. cmsys::ofstream outfile;
  1822. outfile.open(filename.c_str(), std::ios::trunc);
  1823. if (outfile) {
  1824. outfile << content;
  1825. // Check for write errors
  1826. if (!outfile.good()) {
  1827. error = logPrefix;
  1828. error += ": Error writing ";
  1829. error += Quoted(filename);
  1830. }
  1831. } else {
  1832. error = logPrefix;
  1833. error = ": Error opening ";
  1834. error += Quoted(filename);
  1835. }
  1836. }
  1837. if (!error.empty()) {
  1838. this->LogError(error);
  1839. return false;
  1840. }
  1841. return true;
  1842. }
  1843. /**
  1844. * @brief Runs a command and returns true on success
  1845. * @return True on success
  1846. */
  1847. bool cmQtAutoGenerators::RunCommand(const std::vector<std::string>& command,
  1848. std::string& output, bool verbose) const
  1849. {
  1850. // Log command
  1851. if (this->Verbose) {
  1852. this->LogInfo(QuotedCommand(command));
  1853. }
  1854. // Execute command
  1855. int retVal = 0;
  1856. bool res = cmSystemTools::RunSingleCommand(
  1857. command, &output, &output, &retVal, CM_NULLPTR,
  1858. verbose ? cmSystemTools::OUTPUT_MERGE : cmSystemTools::OUTPUT_NONE);
  1859. return (res && (retVal == 0));
  1860. }
  1861. /**
  1862. * @brief Tries to find the header file to the given file base path by
  1863. * appending different header extensions
  1864. * @return True on success
  1865. */
  1866. bool cmQtAutoGenerators::FindHeader(std::string& header,
  1867. const std::string& testBasePath) const
  1868. {
  1869. for (std::vector<std::string>::const_iterator ext =
  1870. this->HeaderExtensions.begin();
  1871. ext != this->HeaderExtensions.end(); ++ext) {
  1872. std::string testFilePath(testBasePath);
  1873. testFilePath += '.';
  1874. testFilePath += (*ext);
  1875. if (cmsys::SystemTools::FileExists(testFilePath.c_str())) {
  1876. header = testFilePath;
  1877. return true;
  1878. }
  1879. }
  1880. return false;
  1881. }
  1882. std::string cmQtAutoGenerators::MocFindHeader(
  1883. const std::string& sourcePath, const std::string& includeBase) const
  1884. {
  1885. std::string header;
  1886. // Search in vicinity of the source
  1887. if (!this->FindHeader(header, sourcePath + includeBase)) {
  1888. // Search in include directories
  1889. for (std::vector<std::string>::const_iterator iit =
  1890. this->MocIncludePaths.begin();
  1891. iit != this->MocIncludePaths.end(); ++iit) {
  1892. const std::string fullPath = ((*iit) + '/' + includeBase);
  1893. if (FindHeader(header, fullPath)) {
  1894. break;
  1895. }
  1896. }
  1897. }
  1898. // Sanitize
  1899. if (!header.empty()) {
  1900. header = cmsys::SystemTools::GetRealPath(header);
  1901. }
  1902. return header;
  1903. }
  1904. bool cmQtAutoGenerators::MocFindIncludedFile(
  1905. std::string& absFile, const std::string& sourcePath,
  1906. const std::string& includeString) const
  1907. {
  1908. bool success = false;
  1909. // Search in vicinity of the source
  1910. {
  1911. std::string testPath = sourcePath;
  1912. testPath += includeString;
  1913. if (cmsys::SystemTools::FileExists(testPath.c_str())) {
  1914. absFile = cmsys::SystemTools::GetRealPath(testPath);
  1915. success = true;
  1916. }
  1917. }
  1918. // Search in include directories
  1919. if (!success) {
  1920. for (std::vector<std::string>::const_iterator iit =
  1921. this->MocIncludePaths.begin();
  1922. iit != this->MocIncludePaths.end(); ++iit) {
  1923. const std::string fullPath = ((*iit) + '/' + includeString);
  1924. if (cmsys::SystemTools::FileExists(fullPath.c_str())) {
  1925. absFile = cmsys::SystemTools::GetRealPath(fullPath);
  1926. success = true;
  1927. break;
  1928. }
  1929. }
  1930. }
  1931. return success;
  1932. }