cmQtAutoGenerators.cxx 67 KB

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