cmQtAutoGenerators.cxx 62 KB

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