cmQtAutoGenerators.cxx 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021
  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("[\n][ \t]*Q_OBJECT[^a-zA-Z0-9_]");
  257. this->MocMacroFilters[1].first = "Q_GADGET";
  258. this->MocMacroFilters[1].second.compile("[\n][ \t]*Q_GADGET[^a-zA-Z0-9_]");
  259. // Precompile regular expressions
  260. this->MocRegExpInclude.compile(
  261. "[\n][ \t]*#[ \t]*include[ \t]+"
  262. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  263. this->UicRegExpInclude.compile("[\n][ \t]*#[ \t]*include[ \t]+"
  264. "[\"<](([^ \">]+/)?ui_[^ \">/]+\\.h)[\">]");
  265. }
  266. bool cmQtAutoGenerators::Run(const std::string& targetDirectory,
  267. const std::string& config)
  268. {
  269. cmake cm(cmake::RoleScript);
  270. cm.SetHomeOutputDirectory(targetDirectory);
  271. cm.SetHomeDirectory(targetDirectory);
  272. cm.GetCurrentSnapshot().SetDefaultDefinitions();
  273. cmGlobalGenerator gg(&cm);
  274. cmStateSnapshot snapshot = cm.GetCurrentSnapshot();
  275. snapshot.GetDirectory().SetCurrentBinary(targetDirectory);
  276. snapshot.GetDirectory().SetCurrentSource(targetDirectory);
  277. CM_AUTO_PTR<cmMakefile> mf(new cmMakefile(&gg, snapshot));
  278. gg.SetCurrentMakefile(mf.get());
  279. bool success = false;
  280. if (this->ReadAutogenInfoFile(mf.get(), targetDirectory, config)) {
  281. // Read old settings
  282. this->SettingsFileRead(mf.get());
  283. // Init and run
  284. this->Init(mf.get());
  285. if (this->RunAutogen()) {
  286. // Write current settings
  287. if (this->SettingsFileWrite()) {
  288. success = true;
  289. }
  290. }
  291. }
  292. return success;
  293. }
  294. bool cmQtAutoGenerators::MocDependFilterPush(const std::string& key,
  295. const std::string& regExp)
  296. {
  297. bool success = false;
  298. if (!key.empty()) {
  299. if (!regExp.empty()) {
  300. MocDependFilter filter;
  301. filter.key = key;
  302. if (filter.regExp.compile(regExp)) {
  303. this->MocDependFilters.push_back(filter);
  304. success = true;
  305. } else {
  306. this->LogError("AutoMoc: Error in AUTOMOC_DEPEND_FILTERS: Compiling "
  307. "regular expression failed.\nKey: " +
  308. Quoted(key) + "\nExp.: " + Quoted(regExp));
  309. }
  310. } else {
  311. this->LogError("AutoMoc: Error in AUTOMOC_DEPEND_FILTERS: Regular "
  312. "expression is empty");
  313. }
  314. } else {
  315. this->LogError("AutoMoc: Error in AUTOMOC_DEPEND_FILTERS: Key is empty");
  316. }
  317. return success;
  318. }
  319. bool cmQtAutoGenerators::ReadAutogenInfoFile(
  320. cmMakefile* makefile, const std::string& targetDirectory,
  321. const std::string& config)
  322. {
  323. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  324. cmSystemTools::ConvertToUnixSlashes(filename);
  325. filename += "/AutogenInfo.cmake";
  326. if (!makefile->ReadListFile(filename.c_str())) {
  327. this->LogError("AutoGen: Error processing file: " + filename);
  328. return false;
  329. }
  330. // - Old settings file
  331. {
  332. this->SettingsFile = cmSystemTools::CollapseFullPath(targetDirectory);
  333. cmSystemTools::ConvertToUnixSlashes(this->SettingsFile);
  334. this->SettingsFile += "/AutogenOldSettings";
  335. this->SettingsFile += this->ConfigSuffix;
  336. this->SettingsFile += ".cmake";
  337. }
  338. // -- Meta
  339. InfoGetConfig(makefile, "AM_CONFIG_SUFFIX", config, this->ConfigSuffix);
  340. // - Files and directories
  341. InfoGet(makefile, "AM_CMAKE_SOURCE_DIR", this->ProjectSourceDir);
  342. InfoGet(makefile, "AM_CMAKE_BINARY_DIR", this->ProjectBinaryDir);
  343. InfoGet(makefile, "AM_CMAKE_CURRENT_SOURCE_DIR", this->CurrentSourceDir);
  344. InfoGet(makefile, "AM_CMAKE_CURRENT_BINARY_DIR", this->CurrentBinaryDir);
  345. InfoGet(makefile, "AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE",
  346. this->IncludeProjectDirsBefore);
  347. InfoGet(makefile, "AM_BUILD_DIR", this->AutogenBuildDir);
  348. if (this->AutogenBuildDir.empty()) {
  349. this->LogError("AutoGen: Error: Missing autogen build directory ");
  350. return false;
  351. }
  352. InfoGet(makefile, "AM_SOURCES", this->Sources);
  353. InfoGet(makefile, "AM_HEADERS", this->Headers);
  354. // - Qt environment
  355. InfoGet(makefile, "AM_QT_VERSION_MAJOR", this->QtMajorVersion);
  356. if (this->QtMajorVersion.empty()) {
  357. InfoGet(makefile, "AM_Qt5Core_VERSION_MAJOR", this->QtMajorVersion);
  358. }
  359. InfoGet(makefile, "AM_QT_MOC_EXECUTABLE", this->MocExecutable);
  360. InfoGet(makefile, "AM_QT_UIC_EXECUTABLE", this->UicExecutable);
  361. InfoGet(makefile, "AM_QT_RCC_EXECUTABLE", this->RccExecutable);
  362. InfoGet(makefile, "AM_MOC_PREDEFS_CMD", this->MocPredefsCmd);
  363. // Check Qt version
  364. if ((this->QtMajorVersion != "4") && (this->QtMajorVersion != "5")) {
  365. this->LogError("AutoGen: Error: Unsupported Qt version: " +
  366. Quoted(this->QtMajorVersion));
  367. return false;
  368. }
  369. // - Moc
  370. if (this->MocEnabled()) {
  371. InfoGet(makefile, "AM_MOC_SKIP", this->MocSkipList);
  372. InfoGetConfig(makefile, "AM_MOC_DEFINITIONS", config,
  373. this->MocDefinitions);
  374. #ifdef _WIN32
  375. {
  376. const std::string win32("WIN32");
  377. if (!ListContains(this->MocDefinitions, win32)) {
  378. this->MocDefinitions.push_back(win32);
  379. }
  380. }
  381. #endif
  382. InfoGetConfig(makefile, "AM_MOC_INCLUDES", config, this->MocIncludePaths);
  383. InfoGet(makefile, "AM_MOC_OPTIONS", this->MocOptions);
  384. InfoGet(makefile, "AM_MOC_RELAXED_MODE", this->MocRelaxedMode);
  385. {
  386. std::vector<std::string> mocDependFilters;
  387. InfoGet(makefile, "AM_MOC_DEPEND_FILTERS", mocDependFilters);
  388. // Insert Q_PLUGIN_METADATA dependency filter
  389. if (this->QtMajorVersion != "4") {
  390. this->MocDependFilterPush("Q_PLUGIN_METADATA",
  391. "[\n][ \t]*Q_PLUGIN_METADATA[ \t]*\\("
  392. "[^\\)]*FILE[ \t]*\"([^\"]+)\"");
  393. }
  394. // Insert user defined dependency filters
  395. if ((mocDependFilters.size() % 2) == 0) {
  396. for (std::vector<std::string>::const_iterator dit =
  397. mocDependFilters.begin();
  398. dit != mocDependFilters.end(); dit += 2) {
  399. if (!this->MocDependFilterPush(*dit, *(dit + 1))) {
  400. return false;
  401. }
  402. }
  403. } else {
  404. this->LogError(
  405. "AutoMoc: Error: AUTOMOC_DEPEND_FILTERS list size is not "
  406. "a multiple of 2 in:\n" +
  407. Quoted(filename));
  408. return false;
  409. }
  410. }
  411. }
  412. // - Uic
  413. if (this->UicEnabled()) {
  414. InfoGet(makefile, "AM_UIC_SKIP", this->UicSkipList);
  415. InfoGet(makefile, "AM_UIC_SEARCH_PATHS", this->UicSearchPaths);
  416. InfoGetConfig(makefile, "AM_UIC_TARGET_OPTIONS", config,
  417. this->UicTargetOptions);
  418. {
  419. std::vector<std::string> uicFilesVec;
  420. std::vector<std::string> uicOptionsVec;
  421. InfoGet(makefile, "AM_UIC_OPTIONS_FILES", uicFilesVec);
  422. InfoGet(makefile, "AM_UIC_OPTIONS_OPTIONS", uicOptionsVec);
  423. // Compare list sizes
  424. if (uicFilesVec.size() == uicOptionsVec.size()) {
  425. for (std::vector<std::string>::iterator
  426. fileIt = uicFilesVec.begin(),
  427. optionIt = uicOptionsVec.begin();
  428. fileIt != uicFilesVec.end(); ++fileIt, ++optionIt) {
  429. cmSystemTools::ReplaceString(*optionIt,
  430. cmQtAutoGeneratorCommon::listSep, ";");
  431. this->UicOptions[*fileIt] = *optionIt;
  432. }
  433. } else {
  434. this->LogError(
  435. "AutoGen: Error: Uic files/options lists size missmatch in:\n" +
  436. Quoted(filename));
  437. return false;
  438. }
  439. }
  440. }
  441. // - Rcc
  442. if (this->RccEnabled()) {
  443. InfoGet(makefile, "AM_RCC_SOURCES", this->RccSources);
  444. // File options
  445. {
  446. std::vector<std::string> rccFilesVec;
  447. std::vector<std::string> rccOptionsVec;
  448. InfoGet(makefile, "AM_RCC_OPTIONS_FILES", rccFilesVec);
  449. InfoGet(makefile, "AM_RCC_OPTIONS_OPTIONS", rccOptionsVec);
  450. if (rccFilesVec.size() == rccOptionsVec.size()) {
  451. for (std::vector<std::string>::iterator
  452. fileIt = rccFilesVec.begin(),
  453. optionIt = rccOptionsVec.begin();
  454. fileIt != rccFilesVec.end(); ++fileIt, ++optionIt) {
  455. // Replace item separator
  456. cmSystemTools::ReplaceString(*optionIt,
  457. cmQtAutoGeneratorCommon::listSep, ";");
  458. this->RccOptions[*fileIt] = *optionIt;
  459. }
  460. } else {
  461. this->LogError(
  462. "AutoGen: Error: RCC files/options lists size missmatch in:\n" +
  463. Quoted(filename));
  464. return false;
  465. }
  466. }
  467. // File lists
  468. {
  469. std::vector<std::string> rccInputLists;
  470. InfoGet(makefile, "AM_RCC_INPUTS", rccInputLists);
  471. if (this->RccSources.size() == rccInputLists.size()) {
  472. for (std::vector<std::string>::iterator
  473. fileIt = this->RccSources.begin(),
  474. inputIt = rccInputLists.begin();
  475. fileIt != this->RccSources.end(); ++fileIt, ++inputIt) {
  476. // Remove braces
  477. *inputIt = inputIt->substr(1, inputIt->size() - 2);
  478. // Replace item separator
  479. cmSystemTools::ReplaceString(*inputIt,
  480. cmQtAutoGeneratorCommon::listSep, ";");
  481. std::vector<std::string> rccInputFiles;
  482. cmSystemTools::ExpandListArgument(*inputIt, rccInputFiles);
  483. this->RccInputs[*fileIt] = rccInputFiles;
  484. }
  485. } else {
  486. this->LogError(
  487. "AutoGen: Error: RCC sources/inputs lists size missmatch in:\n" +
  488. Quoted(filename));
  489. return false;
  490. }
  491. }
  492. }
  493. return true;
  494. }
  495. void cmQtAutoGenerators::SettingsFileRead(cmMakefile* makefile)
  496. {
  497. // Compose current settings strings
  498. {
  499. cmCryptoHash crypt(cmCryptoHash::AlgoSHA256);
  500. const std::string sep(" ~~~ ");
  501. if (this->MocEnabled()) {
  502. std::string str;
  503. str += this->MocExecutable;
  504. str += sep;
  505. str += JoinOptionsList(this->MocDefinitions);
  506. str += sep;
  507. str += JoinOptionsList(this->MocIncludePaths);
  508. str += sep;
  509. str += JoinOptionsList(this->MocOptions);
  510. str += sep;
  511. str += this->IncludeProjectDirsBefore ? "TRUE" : "FALSE";
  512. str += sep;
  513. str += JoinOptionsList(this->MocPredefsCmd);
  514. str += sep;
  515. this->SettingsStringMoc = crypt.HashString(str);
  516. }
  517. if (this->UicEnabled()) {
  518. std::string str;
  519. str += this->UicExecutable;
  520. str += sep;
  521. str += JoinOptionsList(this->UicTargetOptions);
  522. str += sep;
  523. str += JoinOptionsMap(this->UicOptions);
  524. str += sep;
  525. this->SettingsStringUic = crypt.HashString(str);
  526. }
  527. if (this->RccEnabled()) {
  528. std::string str;
  529. str += this->RccExecutable;
  530. str += sep;
  531. str += JoinOptionsMap(this->RccOptions);
  532. str += sep;
  533. this->SettingsStringRcc = crypt.HashString(str);
  534. }
  535. }
  536. // Read old settings
  537. if (makefile->ReadListFile(this->SettingsFile.c_str())) {
  538. if (!SettingsMatch(makefile, SettingsKeyMoc, this->SettingsStringMoc)) {
  539. this->MocSettingsChanged = true;
  540. }
  541. if (!SettingsMatch(makefile, SettingsKeyUic, this->SettingsStringUic)) {
  542. this->UicSettingsChanged = true;
  543. }
  544. if (!SettingsMatch(makefile, SettingsKeyRcc, this->SettingsStringRcc)) {
  545. this->RccSettingsChanged = true;
  546. }
  547. // In case any setting changed remove the old settings file.
  548. // This triggers a full rebuild on the next run if the current
  549. // build is aborted before writing the current settings in the end.
  550. if (this->AnySettingsChanged()) {
  551. cmSystemTools::RemoveFile(this->SettingsFile);
  552. }
  553. } else {
  554. // If the file could not be read re-generate everythiung.
  555. this->MocSettingsChanged = true;
  556. this->UicSettingsChanged = true;
  557. this->RccSettingsChanged = true;
  558. }
  559. }
  560. bool cmQtAutoGenerators::SettingsFileWrite()
  561. {
  562. bool success = true;
  563. // Only write if any setting changed
  564. if (this->AnySettingsChanged()) {
  565. if (this->Verbose) {
  566. this->LogInfo("AutoGen: Writing settings file " +
  567. Quoted(this->SettingsFile));
  568. }
  569. // Compose settings file content
  570. std::string settings;
  571. SettingAppend(settings, SettingsKeyMoc, this->SettingsStringMoc);
  572. SettingAppend(settings, SettingsKeyUic, this->SettingsStringUic);
  573. SettingAppend(settings, SettingsKeyRcc, this->SettingsStringRcc);
  574. // Write settings file
  575. if (!this->FileWrite("AutoGen", this->SettingsFile, settings)) {
  576. this->LogError("AutoGen: Error: Could not write old settings file " +
  577. Quoted(this->SettingsFile));
  578. // Remove old settings file to trigger a full rebuild on the next run
  579. cmSystemTools::RemoveFile(this->SettingsFile);
  580. success = false;
  581. }
  582. }
  583. return success;
  584. }
  585. void cmQtAutoGenerators::Init(cmMakefile* makefile)
  586. {
  587. // Mocs compilation file
  588. this->MocCompFileRel = "mocs_compilation";
  589. this->MocCompFileRel += this->ConfigSuffix;
  590. this->MocCompFileRel += ".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_$<CONFIG>.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_$<CONFIG>.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. // Register moc job
  903. mocsIncluded[headerToMoc] = incString;
  904. this->MocFindDepends(headerToMoc, contentText, mocDepends);
  905. // Store meta information for relaxed mode
  906. if (relaxed && (incRealBasename == scannedFileBasename)) {
  907. ownMocUnderscoreInclude = incString;
  908. ownMocUnderscoreHeader = headerToMoc;
  909. }
  910. } else {
  911. std::ostringstream ost;
  912. ost << "AutoMoc: Error: " << absFilename << "\n"
  913. << "The file includes the moc file " << Quoted(incString)
  914. << ", but could not find header "
  915. << Quoted(incRealBasename + "{" +
  916. JoinExts(this->HeaderExtensions) + "}");
  917. ;
  918. this->LogError(ost.str());
  919. return false;
  920. }
  921. } else {
  922. // Include: FOO.moc
  923. std::string fileToMoc;
  924. if (relaxed) {
  925. // Mode: Relaxed
  926. if (requiresMoc && (incBasename == scannedFileBasename)) {
  927. // Include self
  928. fileToMoc = absFilename;
  929. ownDotMocIncluded = true;
  930. } else {
  931. // In relaxed mode try to find a header instead but issue a warning
  932. const std::string headerToMoc =
  933. this->MocFindHeader(scannedFileAbsPath, incSubDir + incBasename);
  934. if (!headerToMoc.empty()) {
  935. // This is for KDE4 compatibility:
  936. fileToMoc = headerToMoc;
  937. if (!requiresMoc && (incBasename == scannedFileBasename)) {
  938. std::ostringstream ost;
  939. ost << "AutoMoc: Warning: " << Quoted(absFilename) << "\n"
  940. << "The file includes the moc file " << Quoted(incString)
  941. << ", but does not contain a Q_OBJECT or Q_GADGET macro.\n"
  942. << "Running moc on " << Quoted(headerToMoc) << "!\n"
  943. << "Include " << Quoted("moc_" + incBasename + ".cpp")
  944. << " for a compatibility with strict mode (see "
  945. "CMAKE_AUTOMOC_RELAXED_MODE).\n";
  946. this->LogWarning(ost.str());
  947. } else {
  948. std::ostringstream ost;
  949. ost << "AutoMoc: Warning: " << Quoted(absFilename) << "\n"
  950. << "The file includes the moc file " << Quoted(incString)
  951. << " instead of " << Quoted("moc_" + incBasename + ".cpp")
  952. << ".\n"
  953. << "Running moc on " << Quoted(headerToMoc) << "!\n"
  954. << "Include " << Quoted("moc_" + incBasename + ".cpp")
  955. << " for compatibility with strict mode (see "
  956. "CMAKE_AUTOMOC_RELAXED_MODE).\n";
  957. this->LogWarning(ost.str());
  958. }
  959. } else {
  960. std::ostringstream ost;
  961. ost << "AutoMoc: Error: " << Quoted(absFilename) << "\n"
  962. << "The file includes the moc file " << Quoted(incString)
  963. << ". which seems to be the moc file from a different "
  964. "source file. CMake also could not find a matching "
  965. "header.";
  966. this->LogError(ost.str());
  967. return false;
  968. }
  969. }
  970. } else {
  971. // Mode: Strict
  972. if (incBasename == scannedFileBasename) {
  973. // Include self
  974. fileToMoc = absFilename;
  975. ownDotMocIncluded = true;
  976. // Accept but issue a warning if moc isn't required
  977. if (!requiresMoc) {
  978. std::ostringstream ost;
  979. ost << "AutoMoc: Error: " << Quoted(absFilename) << "\n"
  980. << "The file includes the moc file " << Quoted(incString)
  981. << ", but does not contain a Q_OBJECT or Q_GADGET "
  982. "macro.";
  983. this->LogWarning(ost.str());
  984. }
  985. } else {
  986. // Don't allow FOO.moc include other than self in strict mode
  987. std::ostringstream ost;
  988. ost << "AutoMoc: Error: " << Quoted(absFilename) << "\n"
  989. << "The file includes the moc file " << Quoted(incString)
  990. << ", which seems to be the moc file from a different "
  991. "source file. This is not supported. Include "
  992. << Quoted(scannedFileBasename + ".moc")
  993. << " to run moc on this source file.";
  994. this->LogError(ost.str());
  995. return false;
  996. }
  997. }
  998. if (!fileToMoc.empty()) {
  999. mocsIncluded[fileToMoc] = incString;
  1000. this->MocFindDepends(fileToMoc, contentText, mocDepends);
  1001. }
  1002. }
  1003. // Forward content pointer
  1004. contentChars += this->MocRegExpInclude.end();
  1005. }
  1006. }
  1007. if (requiresMoc && !ownDotMocIncluded) {
  1008. // In this case, check whether the scanned file itself contains a Q_OBJECT.
  1009. // If this is the case, the moc_foo.cpp should probably be generated from
  1010. // foo.cpp instead of foo.h, because otherwise it won't build.
  1011. // But warn, since this is not how it is supposed to be used.
  1012. if (relaxed && !ownMocUnderscoreInclude.empty()) {
  1013. // This is for KDE4 compatibility:
  1014. std::ostringstream ost;
  1015. ost << "AutoMoc: Warning: " << Quoted(absFilename) << "\n"
  1016. << "The file contains a " << macroName
  1017. << " macro, but does not include "
  1018. << Quoted(scannedFileBasename + ".moc") << ", but instead includes "
  1019. << Quoted(ownMocUnderscoreInclude) << ".\n"
  1020. << "Running moc on " << Quoted(absFilename) << "!\n"
  1021. << "Better include " << Quoted(scannedFileBasename + ".moc")
  1022. << " for compatibility with strict mode (see "
  1023. "CMAKE_AUTOMOC_RELAXED_MODE).";
  1024. this->LogWarning(ost.str());
  1025. // Use scanned source file instead of scanned header file as moc source
  1026. mocsIncluded[absFilename] = ownMocUnderscoreInclude;
  1027. this->MocFindDepends(absFilename, contentText, mocDepends);
  1028. // Remove
  1029. mocsIncluded.erase(ownMocUnderscoreHeader);
  1030. } else {
  1031. // Otherwise always error out since it will not compile:
  1032. std::ostringstream ost;
  1033. ost << "AutoMoc: Error: " << Quoted(absFilename) << "\n"
  1034. << "The file contains a " << macroName
  1035. << " macro, but does not include "
  1036. << Quoted(scannedFileBasename + ".moc") << "!\n"
  1037. << "Consider adding the include or enabling SKIP_AUTOMOC for this "
  1038. "file.";
  1039. this->LogError(ost.str());
  1040. return false;
  1041. }
  1042. }
  1043. return true;
  1044. }
  1045. void cmQtAutoGenerators::MocParseHeaderContent(
  1046. const std::string& absFilename, const std::string& contentText,
  1047. std::map<std::string, std::string>& mocsNotIncluded,
  1048. std::map<std::string, std::set<std::string> >& mocDepends)
  1049. {
  1050. // Log
  1051. if (this->Verbose) {
  1052. this->LogInfo("AutoMoc: Checking " + absFilename);
  1053. }
  1054. if (this->MocRequired(contentText)) {
  1055. // Register moc job
  1056. mocsNotIncluded[absFilename] =
  1057. this->ChecksumedPath(absFilename, "moc_", this->ConfigSuffix + ".cpp");
  1058. this->MocFindDepends(absFilename, contentText, mocDepends);
  1059. }
  1060. }
  1061. void cmQtAutoGenerators::SearchHeadersForSourceFile(
  1062. const std::string& absFilename, std::set<std::string>& mocHeaderFiles,
  1063. std::set<std::string>& uicHeaderFiles) const
  1064. {
  1065. std::string basepaths[2];
  1066. {
  1067. std::string bpath = SubDirPrefix(absFilename);
  1068. bpath += cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename);
  1069. // search for default header files and private header files
  1070. basepaths[0] = bpath;
  1071. basepaths[1] = bpath + "_p";
  1072. }
  1073. for (const std::string* bpit = cmArrayBegin(basepaths);
  1074. bpit != cmArrayEnd(basepaths); ++bpit) {
  1075. std::string headerName;
  1076. if (this->FindHeader(headerName, *bpit)) {
  1077. // Moc headers
  1078. if (!this->MocSkip(absFilename) && !this->MocSkip(headerName)) {
  1079. mocHeaderFiles.insert(headerName);
  1080. }
  1081. // Uic headers
  1082. if (!this->UicSkip(absFilename) && !this->UicSkip(headerName)) {
  1083. uicHeaderFiles.insert(headerName);
  1084. }
  1085. }
  1086. }
  1087. }
  1088. bool cmQtAutoGenerators::ParseHeaders(
  1089. const std::set<std::string>& mocHeaderFiles,
  1090. const std::set<std::string>& uicHeaderFiles,
  1091. const std::map<std::string, std::string>& mocsIncluded,
  1092. std::map<std::string, std::string>& mocsNotIncluded,
  1093. std::map<std::string, std::set<std::string> >& mocDepends,
  1094. std::map<std::string, std::vector<std::string> >& uisIncluded)
  1095. {
  1096. bool success = true;
  1097. // Merged header files list to read files only once
  1098. std::set<std::string> headerFiles;
  1099. headerFiles.insert(mocHeaderFiles.begin(), mocHeaderFiles.end());
  1100. headerFiles.insert(uicHeaderFiles.begin(), uicHeaderFiles.end());
  1101. for (std::set<std::string>::const_iterator hIt = headerFiles.begin();
  1102. hIt != headerFiles.end(); ++hIt) {
  1103. const std::string& headerName = *hIt;
  1104. std::string contentText;
  1105. if (ReadAll(contentText, headerName)) {
  1106. // Parse header content for MOC
  1107. if ((mocHeaderFiles.find(headerName) != mocHeaderFiles.end()) &&
  1108. (mocsIncluded.find(headerName) == mocsIncluded.end())) {
  1109. this->MocParseHeaderContent(headerName, contentText, mocsNotIncluded,
  1110. mocDepends);
  1111. }
  1112. // Parse header content for UIC
  1113. if (uicHeaderFiles.find(headerName) != uicHeaderFiles.end()) {
  1114. this->UicParseContent(headerName, contentText, uisIncluded);
  1115. }
  1116. } else {
  1117. std::ostringstream ost;
  1118. ost << "AutoGen: Error: Could not read header file:\n"
  1119. << Quoted(headerName);
  1120. this->LogError(ost.str());
  1121. success = false;
  1122. break;
  1123. }
  1124. }
  1125. return success;
  1126. }
  1127. bool cmQtAutoGenerators::MocGenerateAll(
  1128. const std::map<std::string, std::string>& mocsIncluded,
  1129. const std::map<std::string, std::string>& mocsNotIncluded,
  1130. const std::map<std::string, std::set<std::string> >& mocDepends)
  1131. {
  1132. if (!this->MocEnabled()) {
  1133. return true;
  1134. }
  1135. // Look for name collisions
  1136. {
  1137. std::multimap<std::string, std::string> collisions;
  1138. // Test merged map of included and notIncluded
  1139. std::map<std::string, std::string> mergedMocs(mocsIncluded);
  1140. mergedMocs.insert(mocsNotIncluded.begin(), mocsNotIncluded.end());
  1141. if (this->NameCollisionTest(mergedMocs, collisions)) {
  1142. std::ostringstream ost;
  1143. ost << "AutoMoc: Error: "
  1144. "The same moc file will be generated "
  1145. "from different sources.\n"
  1146. "To avoid this error either\n"
  1147. "- rename the source files or\n"
  1148. "- do not include the (moc_NAME.cpp|NAME.moc) file";
  1149. this->LogErrorNameCollision(ost.str(), collisions);
  1150. return false;
  1151. }
  1152. }
  1153. // Generate moc_predefs
  1154. if (!this->MocPredefsCmd.empty()) {
  1155. if (this->MocSettingsChanged ||
  1156. FileAbsentOrOlder(this->MocPredefsFileAbs, this->SettingsFile)) {
  1157. this->LogBold("Generating MOC predefs " + this->MocPredefsFileRel);
  1158. std::string output;
  1159. {
  1160. // Compose command
  1161. std::vector<std::string> cmd = this->MocPredefsCmd;
  1162. // Add includes
  1163. cmd.insert(cmd.end(), this->MocIncludes.begin(),
  1164. this->MocIncludes.end());
  1165. // Add definitions
  1166. for (std::vector<std::string>::const_iterator it =
  1167. this->MocDefinitions.begin();
  1168. it != this->MocDefinitions.end(); ++it) {
  1169. cmd.push_back("-D" + (*it));
  1170. }
  1171. // Add options
  1172. cmd.insert(cmd.end(), this->MocOptions.begin(),
  1173. this->MocOptions.end());
  1174. // Execute command
  1175. if (!this->RunCommand(cmd, output, false)) {
  1176. {
  1177. std::ostringstream ost;
  1178. ost << "AutoMoc: Error: moc predefs generation command failed\n";
  1179. ost << "AutoMoc: Command:\n" << QuotedCommand(cmd) << "\n";
  1180. ost << "AutoMoc: Command output:\n" << output << "\n";
  1181. this->LogError(ost.str());
  1182. }
  1183. return false;
  1184. }
  1185. }
  1186. // (Re)write predefs file only on demand
  1187. if (this->FileDiffers(this->MocPredefsFileAbs, output)) {
  1188. if (this->FileWrite("AutoMoc", this->MocPredefsFileAbs, output)) {
  1189. this->MocPredefsChanged = true;
  1190. } else {
  1191. return false;
  1192. }
  1193. }
  1194. }
  1195. }
  1196. // Generate moc files that are included by source files.
  1197. for (std::map<std::string, std::string>::const_iterator it =
  1198. mocsIncluded.begin();
  1199. it != mocsIncluded.end(); ++it) {
  1200. if (!this->MocGenerateFile(it->first, it->second, mocDepends, true)) {
  1201. if (this->MocRunFailed) {
  1202. return false;
  1203. }
  1204. }
  1205. }
  1206. // Generate moc files that are _not_ included by source files.
  1207. bool mocCompFileGenerated = false;
  1208. for (std::map<std::string, std::string>::const_iterator it =
  1209. mocsNotIncluded.begin();
  1210. it != mocsNotIncluded.end(); ++it) {
  1211. if (this->MocGenerateFile(it->first, it->second, mocDepends, false)) {
  1212. mocCompFileGenerated = true;
  1213. } else {
  1214. if (this->MocRunFailed) {
  1215. return false;
  1216. }
  1217. }
  1218. }
  1219. // Compose mocs compilation file content
  1220. std::string automocSource;
  1221. {
  1222. std::ostringstream ost;
  1223. ost << "/* This file is autogenerated, do not edit*/\n";
  1224. if (mocsNotIncluded.empty()) {
  1225. // Dummy content
  1226. ost << "enum some_compilers { need_more_than_nothing };\n";
  1227. } else {
  1228. // Valid content
  1229. for (std::map<std::string, std::string>::const_iterator it =
  1230. mocsNotIncluded.begin();
  1231. it != mocsNotIncluded.end(); ++it) {
  1232. ost << "#include \"" << it->second << "\"\n";
  1233. }
  1234. }
  1235. automocSource = ost.str();
  1236. }
  1237. if (this->FileDiffers(this->MocCompFileAbs, automocSource)) {
  1238. // Actually write mocs compilation file
  1239. this->LogBold("Generating MOC compilation " + this->MocCompFileRel);
  1240. if (!this->FileWrite("AutoMoc", this->MocCompFileAbs, automocSource)) {
  1241. return false;
  1242. }
  1243. } else if (mocCompFileGenerated) {
  1244. // Only touch mocs compilation file
  1245. if (this->Verbose) {
  1246. this->LogInfo("Touching MOC compilation " + this->MocCompFileRel);
  1247. }
  1248. cmSystemTools::Touch(this->MocCompFileAbs, false);
  1249. }
  1250. return true;
  1251. }
  1252. /**
  1253. * @return True if a moc file was created. False may indicate an error.
  1254. */
  1255. bool cmQtAutoGenerators::MocGenerateFile(
  1256. const std::string& sourceFile, const std::string& mocFileName,
  1257. const std::map<std::string, std::set<std::string> >& mocDepends,
  1258. bool included)
  1259. {
  1260. bool mocGenerated = false;
  1261. bool generateMoc = this->MocSettingsChanged || this->MocPredefsChanged;
  1262. const std::string mocFileRel =
  1263. included ? (this->AutogenIncludeDir + mocFileName) : mocFileName;
  1264. const std::string mocFileAbs =
  1265. cmSystemTools::CollapseCombinedPath(this->AutogenBuildDir, mocFileRel);
  1266. if (!generateMoc) {
  1267. // Test if the source file is newer that the build file
  1268. generateMoc = FileAbsentOrOlder(mocFileAbs, sourceFile);
  1269. if (!generateMoc) {
  1270. // Test if a dependency file changed
  1271. std::map<std::string, std::set<std::string> >::const_iterator dit =
  1272. mocDepends.find(sourceFile);
  1273. if (dit != mocDepends.end()) {
  1274. for (std::set<std::string>::const_iterator fit = dit->second.begin();
  1275. fit != dit->second.end(); ++fit) {
  1276. if (FileAbsentOrOlder(mocFileAbs, *fit)) {
  1277. generateMoc = true;
  1278. break;
  1279. }
  1280. }
  1281. }
  1282. }
  1283. }
  1284. if (generateMoc) {
  1285. // Log
  1286. this->LogBold("Generating MOC source " + mocFileRel);
  1287. // Make sure the parent directory exists
  1288. if (this->MakeParentDirectory("AutoMoc", mocFileAbs)) {
  1289. // Compose moc command
  1290. std::vector<std::string> cmd;
  1291. cmd.push_back(this->MocExecutable);
  1292. // Add includes
  1293. cmd.insert(cmd.end(), this->MocIncludes.begin(),
  1294. this->MocIncludes.end());
  1295. // Add definitions
  1296. for (std::vector<std::string>::const_iterator it =
  1297. this->MocDefinitions.begin();
  1298. it != this->MocDefinitions.end(); ++it) {
  1299. cmd.push_back("-D" + (*it));
  1300. }
  1301. // Add options
  1302. cmd.insert(cmd.end(), this->MocOptions.begin(), this->MocOptions.end());
  1303. // Add predefs include
  1304. if (!this->MocPredefsFileAbs.empty()) {
  1305. cmd.push_back("--include");
  1306. cmd.push_back(this->MocPredefsFileAbs);
  1307. }
  1308. cmd.push_back("-o");
  1309. cmd.push_back(mocFileAbs);
  1310. cmd.push_back(sourceFile);
  1311. // Execute moc command
  1312. std::string output;
  1313. if (this->RunCommand(cmd, output)) {
  1314. // Success
  1315. mocGenerated = true;
  1316. } else {
  1317. // Command failed
  1318. {
  1319. std::ostringstream ost;
  1320. ost << "AutoMoc: Error: moc process failed for\n";
  1321. ost << Quoted(mocFileRel) << "\n";
  1322. ost << "AutoMoc: Command:\n" << QuotedCommand(cmd) << "\n";
  1323. ost << "AutoMoc: Command output:\n" << output << "\n";
  1324. this->LogError(ost.str());
  1325. }
  1326. cmSystemTools::RemoveFile(mocFileAbs);
  1327. this->MocRunFailed = true;
  1328. }
  1329. } else {
  1330. // Parent directory creation failed
  1331. this->MocRunFailed = true;
  1332. }
  1333. }
  1334. return mocGenerated;
  1335. }
  1336. bool cmQtAutoGenerators::UicFindIncludedFile(std::string& absFile,
  1337. const std::string& sourceFile,
  1338. const std::string& includeString)
  1339. {
  1340. bool success = false;
  1341. // Search in vicinity of the source
  1342. {
  1343. std::string testPath = SubDirPrefix(sourceFile);
  1344. testPath += includeString;
  1345. if (cmsys::SystemTools::FileExists(testPath.c_str())) {
  1346. absFile = cmsys::SystemTools::GetRealPath(testPath);
  1347. success = true;
  1348. }
  1349. }
  1350. // Search in include directories
  1351. if (!success) {
  1352. for (std::vector<std::string>::const_iterator iit =
  1353. this->UicSearchPaths.begin();
  1354. iit != this->UicSearchPaths.end(); ++iit) {
  1355. const std::string fullPath = ((*iit) + '/' + includeString);
  1356. if (cmsys::SystemTools::FileExists(fullPath.c_str())) {
  1357. absFile = cmsys::SystemTools::GetRealPath(fullPath);
  1358. success = true;
  1359. break;
  1360. }
  1361. }
  1362. }
  1363. return success;
  1364. }
  1365. bool cmQtAutoGenerators::UicGenerateAll(
  1366. const std::map<std::string, std::vector<std::string> >& uisIncluded)
  1367. {
  1368. if (!this->UicEnabled()) {
  1369. return true;
  1370. }
  1371. // single map with input / output names
  1372. std::map<std::string, std::map<std::string, std::string> > sourceGenMap;
  1373. {
  1374. // Collision lookup map
  1375. std::map<std::string, std::string> testMap;
  1376. // Compile maps
  1377. for (std::map<std::string, std::vector<std::string> >::const_iterator sit =
  1378. uisIncluded.begin();
  1379. sit != uisIncluded.end(); ++sit) {
  1380. const std::string& source(sit->first);
  1381. const std::vector<std::string>& sourceIncs(sit->second);
  1382. // insert new source/destination map
  1383. std::map<std::string, std::string>& uiGenMap = sourceGenMap[source];
  1384. for (std::vector<std::string>::const_iterator uit = sourceIncs.begin();
  1385. uit != sourceIncs.end(); ++uit) {
  1386. // Remove ui_ from the begin filename by substr()
  1387. const std::string uiBasePath = SubDirPrefix(*uit);
  1388. const std::string uiBaseName =
  1389. cmsys::SystemTools::GetFilenameWithoutLastExtension(*uit).substr(3);
  1390. const std::string searchFileName = uiBasePath + uiBaseName + ".ui";
  1391. std::string uiInputFile;
  1392. if (UicFindIncludedFile(uiInputFile, source, searchFileName)) {
  1393. std::string uiOutputFile = uiBasePath + "ui_" + uiBaseName + ".h";
  1394. cmSystemTools::ReplaceString(uiOutputFile, "..", "__");
  1395. uiGenMap[uiInputFile] = uiOutputFile;
  1396. testMap[uiInputFile] = uiOutputFile;
  1397. } else {
  1398. this->LogError("AutoUic: Error: " + Quoted(sit->first) +
  1399. "\nCould not find " + Quoted(searchFileName));
  1400. return false;
  1401. }
  1402. }
  1403. }
  1404. // look for name collisions
  1405. {
  1406. std::multimap<std::string, std::string> collisions;
  1407. if (this->NameCollisionTest(testMap, collisions)) {
  1408. std::ostringstream ost;
  1409. ost << "AutoUic: Error: The same ui_NAME.h file will be generated "
  1410. "from different sources.\n"
  1411. "To avoid this error rename the source files.\n";
  1412. this->LogErrorNameCollision(ost.str(), collisions);
  1413. return false;
  1414. }
  1415. }
  1416. }
  1417. // generate ui files
  1418. for (std::map<std::string,
  1419. std::map<std::string, std::string> >::const_iterator it =
  1420. sourceGenMap.begin();
  1421. it != sourceGenMap.end(); ++it) {
  1422. for (std::map<std::string, std::string>::const_iterator sit =
  1423. it->second.begin();
  1424. sit != it->second.end(); ++sit) {
  1425. if (!this->UicGenerateFile(it->first, sit->first, sit->second)) {
  1426. if (this->UicRunFailed) {
  1427. return false;
  1428. }
  1429. }
  1430. }
  1431. }
  1432. return true;
  1433. }
  1434. /**
  1435. * @return True if a uic file was created. False may indicate an error.
  1436. */
  1437. bool cmQtAutoGenerators::UicGenerateFile(const std::string& realName,
  1438. const std::string& uiInputFile,
  1439. const std::string& uiOutputFile)
  1440. {
  1441. bool uicGenerated = false;
  1442. bool generateUic = this->UicSettingsChanged;
  1443. const std::string uicFileRel = this->AutogenIncludeDir + uiOutputFile;
  1444. const std::string uicFileAbs =
  1445. cmSystemTools::CollapseCombinedPath(this->AutogenBuildDir, uicFileRel);
  1446. if (!generateUic) {
  1447. // Test if the source file is newer that the build file
  1448. generateUic = FileAbsentOrOlder(uicFileAbs, uiInputFile);
  1449. }
  1450. if (generateUic) {
  1451. // Log
  1452. this->LogBold("Generating UIC header " + uicFileRel);
  1453. // Make sure the parent directory exists
  1454. if (this->MakeParentDirectory("AutoUic", uicFileAbs)) {
  1455. // Compose uic command
  1456. std::vector<std::string> cmd;
  1457. cmd.push_back(this->UicExecutable);
  1458. {
  1459. std::vector<std::string> allOpts = this->UicTargetOptions;
  1460. std::map<std::string, std::string>::const_iterator optionIt =
  1461. this->UicOptions.find(uiInputFile);
  1462. if (optionIt != this->UicOptions.end()) {
  1463. std::vector<std::string> fileOpts;
  1464. cmSystemTools::ExpandListArgument(optionIt->second, fileOpts);
  1465. UicMergeOptions(allOpts, fileOpts, (this->QtMajorVersion == "5"));
  1466. }
  1467. cmd.insert(cmd.end(), allOpts.begin(), allOpts.end());
  1468. }
  1469. cmd.push_back("-o");
  1470. cmd.push_back(uicFileAbs);
  1471. cmd.push_back(uiInputFile);
  1472. std::string output;
  1473. if (this->RunCommand(cmd, output)) {
  1474. // Success
  1475. uicGenerated = true;
  1476. } else {
  1477. // Command failed
  1478. {
  1479. std::ostringstream ost;
  1480. ost << "AutoUic: Error: uic process failed for\n";
  1481. ost << Quoted(uicFileRel) << " needed by\n";
  1482. ost << Quoted(realName) << "\n";
  1483. ost << "AutoUic: Command:\n" << QuotedCommand(cmd) << "\n";
  1484. ost << "AutoUic: Command output:\n" << output << "\n";
  1485. this->LogError(ost.str());
  1486. }
  1487. cmSystemTools::RemoveFile(uicFileAbs);
  1488. this->UicRunFailed = true;
  1489. }
  1490. } else {
  1491. // Parent directory creation failed
  1492. this->UicRunFailed = true;
  1493. }
  1494. }
  1495. return uicGenerated;
  1496. }
  1497. bool cmQtAutoGenerators::RccGenerateAll()
  1498. {
  1499. if (!this->RccEnabled()) {
  1500. return true;
  1501. }
  1502. // generate single map with input / output names
  1503. std::map<std::string, std::string> qrcGenMap;
  1504. {
  1505. const std::string qrcPrefix = "qrc_";
  1506. const std::string qrcSuffix = this->ConfigSuffix + ".cpp";
  1507. for (std::vector<std::string>::const_iterator si =
  1508. this->RccSources.begin();
  1509. si != this->RccSources.end(); ++si) {
  1510. const std::string ext =
  1511. cmsys::SystemTools::GetFilenameLastExtension(*si);
  1512. if (ext == ".qrc") {
  1513. qrcGenMap[*si] = this->ChecksumedPath(*si, qrcPrefix, qrcSuffix);
  1514. }
  1515. }
  1516. }
  1517. // look for name collisions
  1518. {
  1519. std::multimap<std::string, std::string> collisions;
  1520. if (this->NameCollisionTest(qrcGenMap, collisions)) {
  1521. std::ostringstream ost;
  1522. ost << "AutoRcc: Error: The same qrc_NAME.cpp file"
  1523. " will be generated from different sources.\n"
  1524. "To avoid this error rename the source .qrc files.\n";
  1525. this->LogErrorNameCollision(ost.str(), collisions);
  1526. return false;
  1527. }
  1528. }
  1529. // generate qrc files
  1530. for (std::map<std::string, std::string>::const_iterator si =
  1531. qrcGenMap.begin();
  1532. si != qrcGenMap.end(); ++si) {
  1533. bool unique = FileNameIsUnique(si->first, qrcGenMap);
  1534. if (!this->RccGenerateFile(si->first, si->second, unique)) {
  1535. if (this->RccRunFailed) {
  1536. return false;
  1537. }
  1538. }
  1539. }
  1540. return true;
  1541. }
  1542. /**
  1543. * @return True if a rcc file was created. False may indicate an error.
  1544. */
  1545. bool cmQtAutoGenerators::RccGenerateFile(const std::string& rccInputFile,
  1546. const std::string& rccOutputFile,
  1547. bool unique_n)
  1548. {
  1549. bool rccGenerated = false;
  1550. bool generateRcc = this->RccSettingsChanged;
  1551. const std::string rccBuildFile =
  1552. cmSystemTools::CollapseCombinedPath(this->AutogenBuildDir, rccOutputFile);
  1553. if (!generateRcc) {
  1554. // Test if the resources list file is newer than build file
  1555. generateRcc = FileAbsentOrOlder(rccBuildFile, rccInputFile);
  1556. if (!generateRcc) {
  1557. // Acquire input file list
  1558. std::vector<std::string> readFiles;
  1559. const std::vector<std::string>* files = &this->RccInputs[rccInputFile];
  1560. if (files->empty()) {
  1561. // Read input file list from qrc file
  1562. std::string error;
  1563. if (cmQtAutoGeneratorCommon::RccListInputs(
  1564. this->QtMajorVersion, this->RccExecutable, rccInputFile,
  1565. readFiles, &error)) {
  1566. files = &readFiles;
  1567. } else {
  1568. files = CM_NULLPTR;
  1569. this->LogError(error);
  1570. this->RccRunFailed = true;
  1571. }
  1572. }
  1573. // Test if any input file is newer than the build file
  1574. if (files != CM_NULLPTR) {
  1575. for (std::vector<std::string>::const_iterator it = files->begin();
  1576. it != files->end(); ++it) {
  1577. if (FileAbsentOrOlder(rccBuildFile, *it)) {
  1578. generateRcc = true;
  1579. break;
  1580. }
  1581. }
  1582. }
  1583. }
  1584. }
  1585. if (generateRcc) {
  1586. // Log
  1587. this->LogBold("Generating RCC source " + rccOutputFile);
  1588. // Make sure the parent directory exists
  1589. if (this->MakeParentDirectory("AutoRcc", rccBuildFile)) {
  1590. // Compose symbol name
  1591. std::string symbolName =
  1592. cmsys::SystemTools::GetFilenameWithoutLastExtension(rccInputFile);
  1593. if (!unique_n) {
  1594. symbolName += "_";
  1595. symbolName += FPathChecksum.getPart(rccInputFile);
  1596. }
  1597. // Replace '-' with '_'. The former is valid for
  1598. // file names but not for symbol names.
  1599. std::replace(symbolName.begin(), symbolName.end(), '-', '_');
  1600. // Compose rcc command
  1601. std::vector<std::string> cmd;
  1602. cmd.push_back(this->RccExecutable);
  1603. {
  1604. std::map<std::string, std::string>::const_iterator optionIt =
  1605. this->RccOptions.find(rccInputFile);
  1606. if (optionIt != this->RccOptions.end()) {
  1607. cmSystemTools::ExpandListArgument(optionIt->second, cmd);
  1608. }
  1609. }
  1610. cmd.push_back("-name");
  1611. cmd.push_back(symbolName);
  1612. cmd.push_back("-o");
  1613. cmd.push_back(rccBuildFile);
  1614. cmd.push_back(rccInputFile);
  1615. std::string output;
  1616. if (this->RunCommand(cmd, output)) {
  1617. // Success
  1618. rccGenerated = true;
  1619. } else {
  1620. // Command failed
  1621. {
  1622. std::ostringstream ost;
  1623. ost << "AutoRcc: Error: rcc process failed for\n";
  1624. ost << Quoted(rccOutputFile) << "\n";
  1625. ost << "AutoRcc: Command:\n" << QuotedCommand(cmd) << "\n";
  1626. ost << "AutoRcc: Command output:\n" << output << "\n";
  1627. this->LogError(ost.str());
  1628. }
  1629. cmSystemTools::RemoveFile(rccBuildFile);
  1630. this->RccRunFailed = true;
  1631. }
  1632. } else {
  1633. // Parent directory creation failed
  1634. this->RccRunFailed = true;
  1635. }
  1636. }
  1637. return rccGenerated;
  1638. }
  1639. void cmQtAutoGenerators::LogErrorNameCollision(
  1640. const std::string& message,
  1641. const std::multimap<std::string, std::string>& collisions) const
  1642. {
  1643. typedef std::multimap<std::string, std::string>::const_iterator Iter;
  1644. std::ostringstream ost;
  1645. // Add message
  1646. if (!message.empty()) {
  1647. ost << message;
  1648. if (message[message.size() - 1] != '\n') {
  1649. ost << '\n';
  1650. }
  1651. }
  1652. // Append collision list
  1653. for (Iter it = collisions.begin(); it != collisions.end(); ++it) {
  1654. ost << it->first << " : " << it->second << '\n';
  1655. }
  1656. this->LogError(ost.str());
  1657. }
  1658. void cmQtAutoGenerators::LogBold(const std::string& message) const
  1659. {
  1660. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue |
  1661. cmsysTerminal_Color_ForegroundBold,
  1662. message.c_str(), true, this->ColorOutput);
  1663. }
  1664. void cmQtAutoGenerators::LogInfo(const std::string& message) const
  1665. {
  1666. std::string msg(message);
  1667. if (!msg.empty()) {
  1668. if (msg[msg.size() - 1] != '\n') {
  1669. msg.push_back('\n');
  1670. }
  1671. cmSystemTools::Stdout(msg.c_str(), msg.size());
  1672. }
  1673. }
  1674. void cmQtAutoGenerators::LogWarning(const std::string& message) const
  1675. {
  1676. std::string msg(message);
  1677. if (!msg.empty()) {
  1678. if (msg[msg.size() - 1] != '\n') {
  1679. msg.push_back('\n');
  1680. }
  1681. // Append empty line
  1682. msg.push_back('\n');
  1683. cmSystemTools::Stdout(msg.c_str(), msg.size());
  1684. }
  1685. }
  1686. void cmQtAutoGenerators::LogError(const std::string& message) const
  1687. {
  1688. std::string msg(message);
  1689. if (!msg.empty()) {
  1690. if (msg[msg.size() - 1] != '\n') {
  1691. msg.push_back('\n');
  1692. }
  1693. // Append empty line
  1694. msg.push_back('\n');
  1695. cmSystemTools::Stderr(msg.c_str(), msg.size());
  1696. }
  1697. }
  1698. /**
  1699. * @brief Collects name collisions as output/input pairs
  1700. * @return True if there were collisions
  1701. */
  1702. bool cmQtAutoGenerators::NameCollisionTest(
  1703. const std::map<std::string, std::string>& genFiles,
  1704. std::multimap<std::string, std::string>& collisions) const
  1705. {
  1706. typedef std::map<std::string, std::string>::const_iterator Iter;
  1707. typedef std::map<std::string, std::string>::value_type VType;
  1708. for (Iter ait = genFiles.begin(); ait != genFiles.end(); ++ait) {
  1709. bool first_match(true);
  1710. for (Iter bit = (++Iter(ait)); bit != genFiles.end(); ++bit) {
  1711. if (ait->second == bit->second) {
  1712. if (first_match) {
  1713. if (collisions.find(ait->second) != collisions.end()) {
  1714. // We already know of this collision from before
  1715. break;
  1716. }
  1717. collisions.insert(VType(ait->second, ait->first));
  1718. first_match = false;
  1719. }
  1720. collisions.insert(VType(bit->second, bit->first));
  1721. }
  1722. }
  1723. }
  1724. return !collisions.empty();
  1725. }
  1726. /**
  1727. * @brief Generates a file path based on the checksum of the source file path
  1728. * @return The path
  1729. */
  1730. std::string cmQtAutoGenerators::ChecksumedPath(
  1731. const std::string& sourceFile, const std::string& basePrefix,
  1732. const std::string& baseSuffix) const
  1733. {
  1734. std::string res = FPathChecksum.getPart(sourceFile);
  1735. res += "/";
  1736. res += basePrefix;
  1737. res += cmsys::SystemTools::GetFilenameWithoutLastExtension(sourceFile);
  1738. res += baseSuffix;
  1739. return res;
  1740. }
  1741. /**
  1742. * @brief Generates the parent directory of the given file on demand
  1743. * @return True on success
  1744. */
  1745. bool cmQtAutoGenerators::MakeParentDirectory(const char* logPrefix,
  1746. const std::string& filename) const
  1747. {
  1748. bool success = true;
  1749. const std::string dirName = cmSystemTools::GetFilenamePath(filename);
  1750. if (!dirName.empty()) {
  1751. success = cmsys::SystemTools::MakeDirectory(dirName);
  1752. if (!success) {
  1753. std::string error = logPrefix;
  1754. error += ": Error: Parent directory creation failed for ";
  1755. error += Quoted(filename);
  1756. this->LogError(error);
  1757. }
  1758. }
  1759. return success;
  1760. }
  1761. bool cmQtAutoGenerators::FileDiffers(const std::string& filename,
  1762. const std::string& content)
  1763. {
  1764. bool differs = true;
  1765. {
  1766. std::string oldContents;
  1767. if (ReadAll(oldContents, filename)) {
  1768. differs = (oldContents != content);
  1769. }
  1770. }
  1771. return differs;
  1772. }
  1773. bool cmQtAutoGenerators::FileWrite(const char* logPrefix,
  1774. const std::string& filename,
  1775. const std::string& content)
  1776. {
  1777. std::string error;
  1778. // Make sure the parent directory exists
  1779. if (this->MakeParentDirectory(logPrefix, filename)) {
  1780. cmsys::ofstream outfile;
  1781. outfile.open(filename.c_str(), std::ios::trunc);
  1782. if (outfile) {
  1783. outfile << content;
  1784. // Check for write errors
  1785. if (!outfile.good()) {
  1786. error = logPrefix;
  1787. error += ": Error writing ";
  1788. error += Quoted(filename);
  1789. }
  1790. } else {
  1791. error = logPrefix;
  1792. error = ": Error opening ";
  1793. error += Quoted(filename);
  1794. }
  1795. }
  1796. if (!error.empty()) {
  1797. this->LogError(error);
  1798. return false;
  1799. }
  1800. return true;
  1801. }
  1802. /**
  1803. * @brief Runs a command and returns true on success
  1804. * @return True on success
  1805. */
  1806. bool cmQtAutoGenerators::RunCommand(const std::vector<std::string>& command,
  1807. std::string& output, bool verbose) const
  1808. {
  1809. // Log command
  1810. if (this->Verbose) {
  1811. this->LogInfo(QuotedCommand(command));
  1812. }
  1813. // Execute command
  1814. int retVal = 0;
  1815. bool res = cmSystemTools::RunSingleCommand(
  1816. command, &output, &output, &retVal, CM_NULLPTR,
  1817. verbose ? cmSystemTools::OUTPUT_MERGE : cmSystemTools::OUTPUT_NONE);
  1818. return (res && (retVal == 0));
  1819. }
  1820. /**
  1821. * @brief Tries to find the header file to the given file base path by
  1822. * appending different header extensions
  1823. * @return True on success
  1824. */
  1825. bool cmQtAutoGenerators::FindHeader(std::string& header,
  1826. const std::string& testBasePath) const
  1827. {
  1828. for (std::vector<std::string>::const_iterator ext =
  1829. this->HeaderExtensions.begin();
  1830. ext != this->HeaderExtensions.end(); ++ext) {
  1831. std::string testFilePath(testBasePath);
  1832. testFilePath += '.';
  1833. testFilePath += (*ext);
  1834. if (cmsys::SystemTools::FileExists(testFilePath.c_str())) {
  1835. header = testFilePath;
  1836. return true;
  1837. }
  1838. }
  1839. return false;
  1840. }
  1841. std::string cmQtAutoGenerators::MocFindHeader(
  1842. const std::string& sourcePath, const std::string& includeBase) const
  1843. {
  1844. std::string header;
  1845. // Search in vicinity of the source
  1846. if (!this->FindHeader(header, sourcePath + includeBase)) {
  1847. // Search in include directories
  1848. for (std::vector<std::string>::const_iterator iit =
  1849. this->MocIncludePaths.begin();
  1850. iit != this->MocIncludePaths.end(); ++iit) {
  1851. const std::string fullPath = ((*iit) + '/' + includeBase);
  1852. if (FindHeader(header, fullPath)) {
  1853. break;
  1854. }
  1855. }
  1856. }
  1857. // Sanitize
  1858. if (!header.empty()) {
  1859. header = cmsys::SystemTools::GetRealPath(header);
  1860. }
  1861. return header;
  1862. }
  1863. bool cmQtAutoGenerators::MocFindIncludedFile(
  1864. std::string& absFile, const std::string& sourcePath,
  1865. const std::string& includeString) const
  1866. {
  1867. bool success = false;
  1868. // Search in vicinity of the source
  1869. {
  1870. std::string testPath = sourcePath;
  1871. testPath += includeString;
  1872. if (cmsys::SystemTools::FileExists(testPath.c_str())) {
  1873. absFile = cmsys::SystemTools::GetRealPath(testPath);
  1874. success = true;
  1875. }
  1876. }
  1877. // Search in include directories
  1878. if (!success) {
  1879. for (std::vector<std::string>::const_iterator iit =
  1880. this->MocIncludePaths.begin();
  1881. iit != this->MocIncludePaths.end(); ++iit) {
  1882. const std::string fullPath = ((*iit) + '/' + includeString);
  1883. if (cmsys::SystemTools::FileExists(fullPath.c_str())) {
  1884. absFile = cmsys::SystemTools::GetRealPath(fullPath);
  1885. success = true;
  1886. break;
  1887. }
  1888. }
  1889. }
  1890. return success;
  1891. }