cmQtAutoGenerators.cxx 70 KB

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