cmQtAutoGenerators.cxx 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830
  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 std::string GetConfigDefinition(cmMakefile* makefile,
  50. const std::string& key,
  51. const std::string& config)
  52. {
  53. std::string keyConf = key;
  54. if (!config.empty()) {
  55. keyConf += "_";
  56. keyConf += config;
  57. }
  58. const char* valueConf = makefile->GetDefinition(keyConf);
  59. if (valueConf != CM_NULLPTR) {
  60. return valueConf;
  61. }
  62. return makefile->GetSafeDefinition(key);
  63. }
  64. static std::string SettingsFile(const std::string& targetDirectory)
  65. {
  66. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  67. cmSystemTools::ConvertToUnixSlashes(filename);
  68. filename += "/AutogenOldSettings.cmake";
  69. return filename;
  70. }
  71. inline static bool SettingsMatch(cmMakefile* makefile, const char* key,
  72. const std::string& value)
  73. {
  74. return (value == makefile->GetSafeDefinition(key));
  75. }
  76. static void SettingWrite(std::ostream& ostr, const char* key,
  77. const std::string& value)
  78. {
  79. if (!value.empty()) {
  80. ostr << "set(" << key << " " << cmOutputConverter::EscapeForCMake(value)
  81. << ")\n";
  82. }
  83. }
  84. std::string subDirPrefix(const std::string& fileName)
  85. {
  86. std::string res(cmsys::SystemTools::GetFilenamePath(fileName));
  87. if (!res.empty()) {
  88. res += '/';
  89. }
  90. return res;
  91. }
  92. static bool FileNameIsUnique(const std::string& filePath,
  93. const std::map<std::string, std::string>& fileMap)
  94. {
  95. size_t count(0);
  96. const std::string fileName = cmsys::SystemTools::GetFilenameName(filePath);
  97. for (std::map<std::string, std::string>::const_iterator si = fileMap.begin();
  98. si != fileMap.end(); ++si) {
  99. if (cmsys::SystemTools::GetFilenameName(si->first) == fileName) {
  100. ++count;
  101. if (count > 1) {
  102. return false;
  103. }
  104. }
  105. }
  106. return true;
  107. }
  108. static std::string ReadAll(const std::string& filename)
  109. {
  110. cmsys::ifstream file(filename.c_str());
  111. std::ostringstream stream;
  112. stream << file.rdbuf();
  113. file.close();
  114. return stream.str();
  115. }
  116. /**
  117. * @brief Tests if buildFile doesn't exist or is older than sourceFile
  118. * @return True if buildFile doesn't exist or is older than sourceFile
  119. */
  120. static bool FileAbsentOrOlder(const std::string& buildFile,
  121. const std::string& sourceFile)
  122. {
  123. int result = 0;
  124. bool success =
  125. cmsys::SystemTools::FileTimeCompare(buildFile, sourceFile, &result);
  126. return (!success || (result <= 0));
  127. }
  128. static bool ListContains(const std::vector<std::string>& list,
  129. const std::string& entry)
  130. {
  131. return (std::find(list.begin(), list.end(), entry) != list.end());
  132. }
  133. static std::string JoinOptionsList(const std::vector<std::string>& opts)
  134. {
  135. return cmOutputConverter::EscapeForCMake(cmJoin(opts, ";"));
  136. }
  137. static std::string JoinOptionsMap(
  138. const std::map<std::string, std::string>& opts)
  139. {
  140. std::string result;
  141. for (std::map<std::string, std::string>::const_iterator it = opts.begin();
  142. it != opts.end(); ++it) {
  143. if (it != opts.begin()) {
  144. result += "@list_sep@";
  145. }
  146. result += it->first;
  147. result += "===";
  148. result += it->second;
  149. }
  150. return result;
  151. }
  152. static std::string JoinExts(const std::vector<std::string>& lst)
  153. {
  154. std::string result;
  155. if (!lst.empty()) {
  156. const std::string separator = ",";
  157. for (std::vector<std::string>::const_iterator it = lst.begin();
  158. it != lst.end(); ++it) {
  159. if (it != lst.begin()) {
  160. result += separator;
  161. }
  162. result += '.';
  163. result += *it;
  164. }
  165. }
  166. return result;
  167. }
  168. static void UicMergeOptions(std::vector<std::string>& opts,
  169. const std::vector<std::string>& fileOpts,
  170. bool isQt5)
  171. {
  172. static const char* valueOptions[] = { "tr", "translate",
  173. "postfix", "generator",
  174. "include", // Since Qt 5.3
  175. "g" };
  176. std::vector<std::string> extraOpts;
  177. for (std::vector<std::string>::const_iterator it = fileOpts.begin();
  178. it != fileOpts.end(); ++it) {
  179. std::vector<std::string>::iterator existingIt =
  180. std::find(opts.begin(), opts.end(), *it);
  181. if (existingIt != opts.end()) {
  182. const char* o = it->c_str();
  183. if (*o == '-') {
  184. ++o;
  185. }
  186. if (isQt5 && *o == '-') {
  187. ++o;
  188. }
  189. if (std::find_if(cmArrayBegin(valueOptions), cmArrayEnd(valueOptions),
  190. cmStrCmp(*it)) != cmArrayEnd(valueOptions)) {
  191. assert(existingIt + 1 != opts.end());
  192. *(existingIt + 1) = *(it + 1);
  193. ++it;
  194. }
  195. } else {
  196. extraOpts.push_back(*it);
  197. }
  198. }
  199. opts.insert(opts.end(), extraOpts.begin(), extraOpts.end());
  200. }
  201. // -- Class methods
  202. cmQtAutoGenerators::cmQtAutoGenerators()
  203. : Verbose(cmsys::SystemTools::HasEnv("VERBOSE"))
  204. , ColorOutput(true)
  205. , RunMocFailed(false)
  206. , RunUicFailed(false)
  207. , RunRccFailed(false)
  208. , GenerateAllMoc(false)
  209. , GenerateAllUic(false)
  210. , GenerateAllRcc(false)
  211. {
  212. std::string colorEnv;
  213. cmsys::SystemTools::GetEnv("COLOR", colorEnv);
  214. if (!colorEnv.empty()) {
  215. if (cmSystemTools::IsOn(colorEnv.c_str())) {
  216. this->ColorOutput = true;
  217. } else {
  218. this->ColorOutput = false;
  219. }
  220. }
  221. this->MacroFilters[0].first = "Q_OBJECT";
  222. this->MacroFilters[0].second.compile("[\n][ \t]*Q_OBJECT[^a-zA-Z0-9_]");
  223. this->MacroFilters[1].first = "Q_GADGET";
  224. this->MacroFilters[1].second.compile("[\n][ \t]*Q_GADGET[^a-zA-Z0-9_]");
  225. // Precompile regular expressions
  226. this->RegExpMocInclude.compile(
  227. "[\n][ \t]*#[ \t]*include[ \t]+"
  228. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  229. this->RegExpUicInclude.compile("[\n][ \t]*#[ \t]*include[ \t]+"
  230. "[\"<](([^ \">]+/)?ui_[^ \">/]+\\.h)[\">]");
  231. }
  232. bool cmQtAutoGenerators::Run(const std::string& targetDirectory,
  233. const std::string& config)
  234. {
  235. cmake cm;
  236. cm.SetHomeOutputDirectory(targetDirectory);
  237. cm.SetHomeDirectory(targetDirectory);
  238. cm.GetCurrentSnapshot().SetDefaultDefinitions();
  239. cmGlobalGenerator gg(&cm);
  240. cmStateSnapshot snapshot = cm.GetCurrentSnapshot();
  241. snapshot.GetDirectory().SetCurrentBinary(targetDirectory);
  242. snapshot.GetDirectory().SetCurrentSource(targetDirectory);
  243. CM_AUTO_PTR<cmMakefile> mf(new cmMakefile(&gg, snapshot));
  244. gg.SetCurrentMakefile(mf.get());
  245. bool success = false;
  246. if (this->ReadAutogenInfoFile(mf.get(), targetDirectory, config)) {
  247. // Read old settings
  248. this->SettingsFileRead(mf.get(), targetDirectory);
  249. // Init and run
  250. this->Init(mf.get());
  251. if (this->RunAutogen()) {
  252. // Write current settings
  253. if (this->SettingsFileWrite(targetDirectory)) {
  254. success = true;
  255. }
  256. }
  257. }
  258. return success;
  259. }
  260. bool cmQtAutoGenerators::MocDependFilterPush(const std::string& key,
  261. const std::string& regExp)
  262. {
  263. bool success = false;
  264. if (!key.empty()) {
  265. if (!regExp.empty()) {
  266. MocDependFilter filter;
  267. filter.key = key;
  268. if (filter.regExp.compile(regExp)) {
  269. this->MocDependFilters.push_back(filter);
  270. success = true;
  271. } else {
  272. this->LogError("AutoMoc: Error in AUTOMOC_DEPEND_FILTERS: Compiling "
  273. "regular expression failed.\nKey: " +
  274. Quoted(key) + "\nExp.: " + Quoted(regExp));
  275. }
  276. } else {
  277. this->LogError("AutoMoc: Error in AUTOMOC_DEPEND_FILTERS: Regular "
  278. "expression is empty");
  279. }
  280. } else {
  281. this->LogError("AutoMoc: Error in AUTOMOC_DEPEND_FILTERS: Key is empty");
  282. }
  283. return success;
  284. }
  285. bool cmQtAutoGenerators::ReadAutogenInfoFile(
  286. cmMakefile* makefile, const std::string& targetDirectory,
  287. const std::string& config)
  288. {
  289. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  290. cmSystemTools::ConvertToUnixSlashes(filename);
  291. filename += "/AutogenInfo.cmake";
  292. if (!makefile->ReadListFile(filename.c_str())) {
  293. this->LogError("AutoGen: Error processing file: " + filename);
  294. return false;
  295. }
  296. // - Target names
  297. this->OriginTargetName =
  298. makefile->GetSafeDefinition("AM_ORIGIN_TARGET_NAME");
  299. this->AutogenTargetName = makefile->GetSafeDefinition("AM_TARGET_NAME");
  300. // - Directories
  301. this->ProjectSourceDir = makefile->GetSafeDefinition("AM_CMAKE_SOURCE_DIR");
  302. this->ProjectBinaryDir = makefile->GetSafeDefinition("AM_CMAKE_BINARY_DIR");
  303. this->CurrentSourceDir =
  304. makefile->GetSafeDefinition("AM_CMAKE_CURRENT_SOURCE_DIR");
  305. this->CurrentBinaryDir =
  306. makefile->GetSafeDefinition("AM_CMAKE_CURRENT_BINARY_DIR");
  307. // - Qt environment
  308. this->QtMajorVersion = makefile->GetSafeDefinition("AM_QT_VERSION_MAJOR");
  309. if (this->QtMajorVersion == "") {
  310. this->QtMajorVersion =
  311. makefile->GetSafeDefinition("AM_Qt5Core_VERSION_MAJOR");
  312. }
  313. // Check Qt version
  314. if ((this->QtMajorVersion != "4") && (this->QtMajorVersion != "5")) {
  315. this->LogError("AutoGen: Error: Unsupported Qt version: " +
  316. Quoted(this->QtMajorVersion));
  317. return false;
  318. }
  319. this->MocExecutable = makefile->GetSafeDefinition("AM_QT_MOC_EXECUTABLE");
  320. this->UicExecutable = makefile->GetSafeDefinition("AM_QT_UIC_EXECUTABLE");
  321. this->RccExecutable = makefile->GetSafeDefinition("AM_QT_RCC_EXECUTABLE");
  322. // - File Lists
  323. cmSystemTools::ExpandListArgument(makefile->GetSafeDefinition("AM_SOURCES"),
  324. this->Sources);
  325. cmSystemTools::ExpandListArgument(makefile->GetSafeDefinition("AM_HEADERS"),
  326. this->Headers);
  327. // - Moc
  328. cmSystemTools::ExpandListArgument(makefile->GetSafeDefinition("AM_MOC_SKIP"),
  329. this->MocSkipList);
  330. cmSystemTools::ExpandListArgument(
  331. GetConfigDefinition(makefile, "AM_MOC_COMPILE_DEFINITIONS", config),
  332. this->MocDefinitions);
  333. cmSystemTools::ExpandListArgument(
  334. GetConfigDefinition(makefile, "AM_MOC_INCLUDES", config),
  335. this->MocIncludePaths);
  336. cmSystemTools::ExpandListArgument(
  337. makefile->GetSafeDefinition("AM_MOC_OPTIONS"), this->MocOptions);
  338. {
  339. std::vector<std::string> mocDependFilters;
  340. cmSystemTools::ExpandListArgument(
  341. makefile->GetSafeDefinition("AM_MOC_DEPEND_FILTERS"), mocDependFilters);
  342. // Insert Q_PLUGIN_METADATA dependency filter
  343. if (this->QtMajorVersion != "4") {
  344. this->MocDependFilterPush("Q_PLUGIN_METADATA",
  345. "[\n][ \t]*Q_PLUGIN_METADATA[ \t]*\\("
  346. "[^\\)]*FILE[ \t]*\"([^\"]+)\"");
  347. }
  348. // Insert user defined dependency filters
  349. if ((mocDependFilters.size() % 2) == 0) {
  350. for (std::vector<std::string>::const_iterator dit =
  351. mocDependFilters.begin();
  352. dit != mocDependFilters.end(); dit += 2) {
  353. if (!this->MocDependFilterPush(*dit, *(dit + 1))) {
  354. return false;
  355. }
  356. }
  357. } else {
  358. this->LogError("AutoMoc: Error: AUTOMOC_DEPEND_FILTERS list size is not "
  359. "a multiple of 2");
  360. }
  361. }
  362. // - Uic
  363. cmSystemTools::ExpandListArgument(makefile->GetSafeDefinition("AM_UIC_SKIP"),
  364. this->UicSkipList);
  365. cmSystemTools::ExpandListArgument(
  366. GetConfigDefinition(makefile, "AM_UIC_TARGET_OPTIONS", config),
  367. this->UicTargetOptions);
  368. {
  369. std::vector<std::string> uicFilesVec;
  370. std::vector<std::string> uicOptionsVec;
  371. cmSystemTools::ExpandListArgument(
  372. makefile->GetSafeDefinition("AM_UIC_OPTIONS_FILES"), uicFilesVec);
  373. cmSystemTools::ExpandListArgument(
  374. makefile->GetSafeDefinition("AM_UIC_OPTIONS_OPTIONS"), uicOptionsVec);
  375. if (uicFilesVec.size() != uicOptionsVec.size()) {
  376. this->LogError(
  377. "AutoGen: Error: Uic files/options lists size missmatch in: " +
  378. filename);
  379. return false;
  380. }
  381. for (std::vector<std::string>::iterator fileIt = uicFilesVec.begin(),
  382. optionIt = uicOptionsVec.begin();
  383. fileIt != uicFilesVec.end(); ++fileIt, ++optionIt) {
  384. cmSystemTools::ReplaceString(*optionIt, "@list_sep@", ";");
  385. this->UicOptions[*fileIt] = *optionIt;
  386. }
  387. }
  388. // - Rcc
  389. cmSystemTools::ExpandListArgument(
  390. makefile->GetSafeDefinition("AM_RCC_SOURCES"), this->RccSources);
  391. {
  392. std::vector<std::string> rccFilesVec;
  393. std::vector<std::string> rccOptionsVec;
  394. cmSystemTools::ExpandListArgument(
  395. makefile->GetSafeDefinition("AM_RCC_OPTIONS_FILES"), rccFilesVec);
  396. cmSystemTools::ExpandListArgument(
  397. makefile->GetSafeDefinition("AM_RCC_OPTIONS_OPTIONS"), rccOptionsVec);
  398. if (rccFilesVec.size() != rccOptionsVec.size()) {
  399. this->LogError(
  400. "AutoGen: Error: RCC files/options lists size missmatch in: " +
  401. filename);
  402. return false;
  403. }
  404. for (std::vector<std::string>::iterator fileIt = rccFilesVec.begin(),
  405. optionIt = rccOptionsVec.begin();
  406. fileIt != rccFilesVec.end(); ++fileIt, ++optionIt) {
  407. cmSystemTools::ReplaceString(*optionIt, "@list_sep@", ";");
  408. this->RccOptions[*fileIt] = *optionIt;
  409. }
  410. }
  411. {
  412. std::vector<std::string> rccInputLists;
  413. cmSystemTools::ExpandListArgument(
  414. makefile->GetSafeDefinition("AM_RCC_INPUTS"), rccInputLists);
  415. // qrc files in the end of the list may have been empty
  416. if (rccInputLists.size() < this->RccSources.size()) {
  417. rccInputLists.resize(this->RccSources.size());
  418. }
  419. if (this->RccSources.size() != rccInputLists.size()) {
  420. this->LogError(
  421. "AutoGen: Error: RCC sources/inputs lists size missmatch in: " +
  422. filename);
  423. return false;
  424. }
  425. for (std::vector<std::string>::iterator fileIt = this->RccSources.begin(),
  426. inputIt = rccInputLists.begin();
  427. fileIt != this->RccSources.end(); ++fileIt, ++inputIt) {
  428. cmSystemTools::ReplaceString(*inputIt, "@list_sep@", ";");
  429. std::vector<std::string> rccInputFiles;
  430. cmSystemTools::ExpandListArgument(*inputIt, rccInputFiles);
  431. this->RccInputs[*fileIt] = rccInputFiles;
  432. }
  433. }
  434. // - Flags
  435. this->IncludeProjectDirsBefore =
  436. makefile->IsOn("AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE");
  437. this->MocRelaxedMode = makefile->IsOn("AM_MOC_RELAXED_MODE");
  438. return true;
  439. }
  440. void cmQtAutoGenerators::SettingsFileRead(cmMakefile* makefile,
  441. const std::string& targetDirectory)
  442. {
  443. // Compose current settings strings
  444. if (this->MocEnabled()) {
  445. std::string& str = this->SettingsStringMoc;
  446. str += JoinOptionsList(this->MocDefinitions);
  447. str += " ~~~ ";
  448. str += JoinOptionsList(this->MocIncludePaths);
  449. str += " ~~~ ";
  450. str += JoinOptionsList(this->MocOptions);
  451. str += " ~~~ ";
  452. str += this->IncludeProjectDirsBefore ? "TRUE" : "FALSE";
  453. str += " ~~~ ";
  454. }
  455. if (this->UicEnabled()) {
  456. std::string& str = this->SettingsStringUic;
  457. str += JoinOptionsList(this->UicTargetOptions);
  458. str += " ~~~ ";
  459. str += JoinOptionsMap(this->UicOptions);
  460. str += " ~~~ ";
  461. }
  462. if (this->RccEnabled()) {
  463. std::string& str = this->SettingsStringRcc;
  464. str += JoinOptionsMap(this->RccOptions);
  465. str += " ~~~ ";
  466. }
  467. // Read old settings
  468. const std::string filename = SettingsFile(targetDirectory);
  469. if (makefile->ReadListFile(filename.c_str())) {
  470. if (!SettingsMatch(makefile, SettingsKeyMoc, this->SettingsStringMoc)) {
  471. this->GenerateAllMoc = true;
  472. }
  473. if (!SettingsMatch(makefile, SettingsKeyUic, this->SettingsStringUic)) {
  474. this->GenerateAllUic = true;
  475. }
  476. if (!SettingsMatch(makefile, SettingsKeyRcc, this->SettingsStringRcc)) {
  477. this->GenerateAllRcc = true;
  478. }
  479. // In case any setting changed remove the old settings file.
  480. // This triggers a full rebuild on the next run if the current
  481. // build is aborted before writing the current settings in the end.
  482. if (this->GenerateAllAny()) {
  483. cmSystemTools::RemoveFile(filename);
  484. }
  485. } else {
  486. // If the file could not be read re-generate everythiung.
  487. this->GenerateAllMoc = true;
  488. this->GenerateAllUic = true;
  489. this->GenerateAllRcc = true;
  490. }
  491. }
  492. bool cmQtAutoGenerators::SettingsFileWrite(const std::string& targetDirectory)
  493. {
  494. bool success = true;
  495. // Only write if any setting changed
  496. if (this->GenerateAllAny()) {
  497. const std::string filename = SettingsFile(targetDirectory);
  498. if (this->Verbose) {
  499. this->LogInfo("AutoGen: Writing settings file " + filename);
  500. }
  501. cmsys::ofstream outfile;
  502. outfile.open(filename.c_str(), std::ios::trunc);
  503. if (outfile) {
  504. SettingWrite(outfile, SettingsKeyMoc, this->SettingsStringMoc);
  505. SettingWrite(outfile, SettingsKeyUic, this->SettingsStringUic);
  506. SettingWrite(outfile, SettingsKeyRcc, this->SettingsStringRcc);
  507. success = outfile.good();
  508. outfile.close();
  509. } else {
  510. success = false;
  511. // Remove old settings file to trigger full rebuild on next run
  512. cmSystemTools::RemoveFile(filename);
  513. this->LogError("AutoGen: Error: Writing old settings file failed: " +
  514. filename);
  515. }
  516. }
  517. return success;
  518. }
  519. void cmQtAutoGenerators::Init(cmMakefile* makefile)
  520. {
  521. this->AutogenBuildSubDir = this->AutogenTargetName;
  522. this->AutogenBuildSubDir += "/";
  523. this->MocCppFilenameRel = this->AutogenBuildSubDir;
  524. this->MocCppFilenameRel += "moc_compilation.cpp";
  525. this->MocCppFilenameAbs = this->CurrentBinaryDir + this->MocCppFilenameRel;
  526. // Init file path checksum generator
  527. fpathCheckSum.setupParentDirs(this->CurrentSourceDir, this->CurrentBinaryDir,
  528. this->ProjectSourceDir,
  529. this->ProjectBinaryDir);
  530. // Acquire header extensions
  531. this->HeaderExtensions = makefile->GetCMakeInstance()->GetHeaderExtensions();
  532. // Sort include directories on demand
  533. if (this->IncludeProjectDirsBefore) {
  534. // Move strings to temporary list
  535. std::list<std::string> includes;
  536. includes.insert(includes.end(), this->MocIncludePaths.begin(),
  537. this->MocIncludePaths.end());
  538. this->MocIncludePaths.clear();
  539. this->MocIncludePaths.reserve(includes.size());
  540. // Append project directories only
  541. {
  542. const char* movePaths[2] = { this->ProjectBinaryDir.c_str(),
  543. this->ProjectSourceDir.c_str() };
  544. for (const char* const* mpit = cmArrayBegin(movePaths);
  545. mpit != cmArrayEnd(movePaths); ++mpit) {
  546. std::list<std::string>::iterator it = includes.begin();
  547. while (it != includes.end()) {
  548. const std::string& path = *it;
  549. if (cmsys::SystemTools::StringStartsWith(path, *mpit)) {
  550. this->MocIncludePaths.push_back(path);
  551. it = includes.erase(it);
  552. } else {
  553. ++it;
  554. }
  555. }
  556. }
  557. }
  558. // Append remaining directories
  559. this->MocIncludePaths.insert(this->MocIncludePaths.end(), includes.begin(),
  560. includes.end());
  561. }
  562. // Compose moc includes list
  563. {
  564. std::set<std::string> frameworkPaths;
  565. for (std::vector<std::string>::const_iterator it =
  566. this->MocIncludePaths.begin();
  567. it != this->MocIncludePaths.end(); ++it) {
  568. const std::string& path = *it;
  569. this->MocIncludes.push_back("-I" + path);
  570. // Extract framework path
  571. if (cmHasLiteralSuffix(path, ".framework/Headers")) {
  572. // Go up twice to get to the framework root
  573. std::vector<std::string> pathComponents;
  574. cmsys::SystemTools::SplitPath(path, pathComponents);
  575. std::string frameworkPath = cmsys::SystemTools::JoinPath(
  576. pathComponents.begin(), pathComponents.end() - 2);
  577. frameworkPaths.insert(frameworkPath);
  578. }
  579. }
  580. // Append framework includes
  581. for (std::set<std::string>::const_iterator it = frameworkPaths.begin();
  582. it != frameworkPaths.end(); ++it) {
  583. this->MocIncludes.push_back("-F");
  584. this->MocIncludes.push_back(*it);
  585. }
  586. }
  587. }
  588. bool cmQtAutoGenerators::RunAutogen()
  589. {
  590. // the program goes through all .cpp files to see which moc files are
  591. // included. It is not really interesting how the moc file is named, but
  592. // what file the moc is created from. Once a moc is included the same moc
  593. // may not be included in the moc_compilation.cpp file anymore. OTOH if
  594. // there's a header containing Q_OBJECT where no corresponding moc file
  595. // is included anywhere a moc_<filename>.cpp file is created and included
  596. // in the moc_compilation.cpp file.
  597. // key = moc source filepath, value = moc output filepath
  598. std::map<std::string, std::string> mocsIncluded;
  599. std::map<std::string, std::string> mocsNotIncluded;
  600. std::map<std::string, std::set<std::string> > mocDepends;
  601. std::map<std::string, std::vector<std::string> > uisIncluded;
  602. // collects all headers which may need to be mocced
  603. std::set<std::string> mocHeaderFiles;
  604. std::set<std::string> uicHeaderFiles;
  605. // Parse sources
  606. for (std::vector<std::string>::const_iterator it = this->Sources.begin();
  607. it != this->Sources.end(); ++it) {
  608. const std::string& absFilename = cmsys::SystemTools::GetRealPath(*it);
  609. // Parse source file for MOC/UIC
  610. if (!this->ParseSourceFile(absFilename, mocsIncluded, mocDepends,
  611. uisIncluded, this->MocRelaxedMode)) {
  612. return false;
  613. }
  614. // Find additional headers
  615. this->SearchHeadersForSourceFile(absFilename, mocHeaderFiles,
  616. uicHeaderFiles);
  617. }
  618. // Parse headers
  619. for (std::vector<std::string>::const_iterator it = this->Headers.begin();
  620. it != this->Headers.end(); ++it) {
  621. const std::string& headerName = cmsys::SystemTools::GetRealPath(*it);
  622. if (!this->MocSkip(headerName)) {
  623. mocHeaderFiles.insert(headerName);
  624. }
  625. if (!this->UicSkip(headerName)) {
  626. uicHeaderFiles.insert(headerName);
  627. }
  628. }
  629. this->ParseHeaders(mocHeaderFiles, uicHeaderFiles, mocsIncluded,
  630. mocsNotIncluded, mocDepends, uisIncluded);
  631. // Generate files
  632. if (!this->MocGenerateAll(mocsIncluded, mocsNotIncluded, mocDepends)) {
  633. return false;
  634. }
  635. if (!this->UicGenerateAll(uisIncluded)) {
  636. return false;
  637. }
  638. if (!this->RccGenerateAll()) {
  639. return false;
  640. }
  641. return true;
  642. }
  643. /**
  644. * @brief Tests if the C++ content requires moc processing
  645. * @return True if moc is required
  646. */
  647. bool cmQtAutoGenerators::MocRequired(const std::string& contentText,
  648. std::string* macroName)
  649. {
  650. for (unsigned int ii = 0; ii != cmArraySize(this->MacroFilters); ++ii) {
  651. MacroFilter& filter = this->MacroFilters[ii];
  652. // Run a simple find string operation before the expensive
  653. // regular expression check
  654. if (contentText.find(filter.first) != std::string::npos) {
  655. if (filter.second.find(contentText)) {
  656. // Return macro name on demand
  657. if (macroName != CM_NULLPTR) {
  658. *macroName = filter.first;
  659. }
  660. return true;
  661. }
  662. }
  663. }
  664. return false;
  665. }
  666. void cmQtAutoGenerators::MocFindDepends(
  667. const std::string& absFilename, const std::string& contentText,
  668. std::map<std::string, std::set<std::string> >& mocDepends)
  669. {
  670. for (std::vector<MocDependFilter>::iterator fit =
  671. this->MocDependFilters.begin();
  672. fit != this->MocDependFilters.end(); ++fit) {
  673. MocDependFilter& filter = *fit;
  674. // Run a simple find string operation before the expensive
  675. // regular expression check
  676. if (contentText.find(filter.key) != std::string::npos) {
  677. // Run regular expression check loop
  678. const char* contentChars = contentText.c_str();
  679. while (filter.regExp.find(contentChars)) {
  680. // Evaluate match
  681. const std::string match = filter.regExp.match(1);
  682. if (!match.empty()) {
  683. // Find the dependency file
  684. std::string incFile;
  685. if (this->FindIncludedFile(incFile, absFilename, match)) {
  686. mocDepends[absFilename].insert(incFile);
  687. if (this->Verbose) {
  688. this->LogInfo("AutoMoc: Found dependency:\n " +
  689. Quoted(absFilename) + "\n " + Quoted(incFile));
  690. }
  691. } else {
  692. this->LogWarning("AutoMoc: Warning: " + Quoted(absFilename) +
  693. "\n" + "Could not find dependency file " +
  694. Quoted(match));
  695. }
  696. }
  697. contentChars += filter.regExp.end();
  698. }
  699. }
  700. }
  701. }
  702. /**
  703. * @brief Tests if the file should be ignored for moc scanning
  704. * @return True if the file should be ignored
  705. */
  706. bool cmQtAutoGenerators::MocSkip(const std::string& absFilename) const
  707. {
  708. if (this->MocEnabled()) {
  709. // Test if the file name is on the skip list
  710. if (!ListContains(this->MocSkipList, absFilename)) {
  711. return false;
  712. }
  713. }
  714. return true;
  715. }
  716. /**
  717. * @brief Tests if the file name is in the skip list
  718. */
  719. bool cmQtAutoGenerators::UicSkip(const std::string& absFilename) const
  720. {
  721. if (this->UicEnabled()) {
  722. // Test if the file name is on the skip list
  723. if (!ListContains(this->UicSkipList, absFilename)) {
  724. return false;
  725. }
  726. }
  727. return true;
  728. }
  729. /**
  730. * @return True on success
  731. */
  732. bool cmQtAutoGenerators::ParseSourceFile(
  733. const std::string& absFilename,
  734. std::map<std::string, std::string>& mocsIncluded,
  735. std::map<std::string, std::set<std::string> >& mocDepends,
  736. std::map<std::string, std::vector<std::string> >& uisIncluded, bool relaxed)
  737. {
  738. bool success = true;
  739. const std::string contentText = ReadAll(absFilename);
  740. if (contentText.empty()) {
  741. std::ostringstream ost;
  742. ost << "AutoGen: Warning: " << absFilename << "\n"
  743. << "The file is empty\n";
  744. this->LogWarning(ost.str());
  745. } else {
  746. // Parse source contents for MOC
  747. if (success && !this->MocSkip(absFilename)) {
  748. success = this->MocParseSourceContent(absFilename, contentText,
  749. mocsIncluded, mocDepends, relaxed);
  750. }
  751. // Parse source contents for UIC
  752. if (success && !this->UicSkip(absFilename)) {
  753. this->UicParseContent(absFilename, contentText, uisIncluded);
  754. }
  755. }
  756. return success;
  757. }
  758. void cmQtAutoGenerators::UicParseContent(
  759. const std::string& absFilename, const std::string& contentText,
  760. std::map<std::string, std::vector<std::string> >& uisIncluded)
  761. {
  762. if (this->Verbose) {
  763. this->LogInfo("AutoUic: Checking " + absFilename);
  764. }
  765. const char* contentChars = contentText.c_str();
  766. if (strstr(contentChars, "ui_") != CM_NULLPTR) {
  767. while (this->RegExpUicInclude.find(contentChars)) {
  768. const std::string currentUi = this->RegExpUicInclude.match(1);
  769. const std::string basename =
  770. cmsys::SystemTools::GetFilenameWithoutLastExtension(currentUi);
  771. // basename should be the part of the ui filename used for
  772. // finding the correct header, so we need to remove the ui_ part
  773. uisIncluded[absFilename].push_back(basename.substr(3));
  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->FindMocHeader(scannedFileAbsPath, incRealBasename, incSubDir);
  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->FindMocHeader(scannedFileAbsPath, incBasename, incSubDir);
  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::UicGenerateAll(
  1232. const std::map<std::string, std::vector<std::string> >& uisIncluded)
  1233. {
  1234. if (!this->UicEnabled()) {
  1235. return true;
  1236. }
  1237. // single map with input / output names
  1238. std::map<std::string, std::map<std::string, std::string> > uiGenMap;
  1239. std::map<std::string, std::string> testMap;
  1240. for (std::map<std::string, std::vector<std::string> >::const_iterator it =
  1241. uisIncluded.begin();
  1242. it != uisIncluded.end(); ++it) {
  1243. // source file path
  1244. std::string sourcePath = cmsys::SystemTools::GetFilenamePath(it->first);
  1245. sourcePath += '/';
  1246. // insert new map for source file an use new reference
  1247. uiGenMap[it->first] = std::map<std::string, std::string>();
  1248. std::map<std::string, std::string>& sourceMap = uiGenMap[it->first];
  1249. for (std::vector<std::string>::const_iterator sit = it->second.begin();
  1250. sit != it->second.end(); ++sit) {
  1251. const std::string& uiFileName = *sit;
  1252. const std::string uiInputFile = sourcePath + uiFileName + ".ui";
  1253. const std::string uiOutputFile = "ui_" + uiFileName + ".h";
  1254. sourceMap[uiInputFile] = uiOutputFile;
  1255. testMap[uiInputFile] = uiOutputFile;
  1256. }
  1257. }
  1258. // look for name collisions
  1259. {
  1260. std::multimap<std::string, std::string> collisions;
  1261. if (this->NameCollisionTest(testMap, collisions)) {
  1262. std::ostringstream ost;
  1263. ost << "AutoUic: Error: The same ui_NAME.h file will be generated "
  1264. "from different sources.\n"
  1265. "To avoid this error rename the source files.\n";
  1266. this->LogErrorNameCollision(ost.str(), collisions);
  1267. return false;
  1268. }
  1269. }
  1270. testMap.clear();
  1271. // generate ui files
  1272. for (std::map<std::string,
  1273. std::map<std::string, std::string> >::const_iterator it =
  1274. uiGenMap.begin();
  1275. it != uiGenMap.end(); ++it) {
  1276. for (std::map<std::string, std::string>::const_iterator sit =
  1277. it->second.begin();
  1278. sit != it->second.end(); ++sit) {
  1279. if (!this->UicGenerateFile(it->first, sit->first, sit->second)) {
  1280. if (this->RunUicFailed) {
  1281. return false;
  1282. }
  1283. }
  1284. }
  1285. }
  1286. return true;
  1287. }
  1288. /**
  1289. * @return True if a uic file was created. False may indicate an error.
  1290. */
  1291. bool cmQtAutoGenerators::UicGenerateFile(const std::string& realName,
  1292. const std::string& uiInputFile,
  1293. const std::string& uiOutputFile)
  1294. {
  1295. bool uicGenerated = false;
  1296. bool generateUic = this->GenerateAllUic;
  1297. const std::string uicFileRel =
  1298. this->AutogenBuildSubDir + "include/" + uiOutputFile;
  1299. const std::string uicFileAbs = this->CurrentBinaryDir + uicFileRel;
  1300. if (!generateUic) {
  1301. // Test if the source file is newer that the build file
  1302. generateUic = FileAbsentOrOlder(uicFileAbs, uiInputFile);
  1303. }
  1304. if (generateUic) {
  1305. // Log
  1306. this->LogBold("Generating UIC header " + uicFileRel);
  1307. // Make sure the parent directory exists
  1308. if (this->MakeParentDirectory(uicFileAbs)) {
  1309. // Compose uic command
  1310. std::vector<std::string> cmd;
  1311. cmd.push_back(this->UicExecutable);
  1312. {
  1313. std::vector<std::string> opts = this->UicTargetOptions;
  1314. std::map<std::string, std::string>::const_iterator optionIt =
  1315. this->UicOptions.find(uiInputFile);
  1316. if (optionIt != this->UicOptions.end()) {
  1317. std::vector<std::string> fileOpts;
  1318. cmSystemTools::ExpandListArgument(optionIt->second, fileOpts);
  1319. UicMergeOptions(opts, fileOpts, (this->QtMajorVersion == "5"));
  1320. }
  1321. cmd.insert(cmd.end(), opts.begin(), opts.end());
  1322. }
  1323. cmd.push_back("-o");
  1324. cmd.push_back(uicFileAbs);
  1325. cmd.push_back(uiInputFile);
  1326. // Log command
  1327. if (this->Verbose) {
  1328. this->LogCommand(cmd);
  1329. }
  1330. // Execute command
  1331. bool res = false;
  1332. int retVal = 0;
  1333. std::string output;
  1334. res = cmSystemTools::RunSingleCommand(cmd, &output, &output, &retVal);
  1335. if (!res || (retVal != 0)) {
  1336. // Command failed
  1337. {
  1338. std::ostringstream ost;
  1339. ost << "AutoUic: Error: uic process failed for\n";
  1340. ost << Quoted(uicFileRel) << " needed by\n";
  1341. ost << Quoted(realName) << "\n";
  1342. ost << "AutoUic: Command:\n" << cmJoin(cmd, " ") << "\n";
  1343. ost << "AutoUic: Command output:\n" << output << "\n";
  1344. this->LogError(ost.str());
  1345. }
  1346. cmSystemTools::RemoveFile(uicFileAbs);
  1347. this->RunUicFailed = true;
  1348. } else {
  1349. // Success
  1350. uicGenerated = true;
  1351. }
  1352. } else {
  1353. // Parent directory creation failed
  1354. this->RunUicFailed = true;
  1355. }
  1356. }
  1357. return uicGenerated;
  1358. }
  1359. bool cmQtAutoGenerators::RccGenerateAll()
  1360. {
  1361. if (!this->RccEnabled()) {
  1362. return true;
  1363. }
  1364. // generate single map with input / output names
  1365. std::map<std::string, std::string> qrcGenMap;
  1366. for (std::vector<std::string>::const_iterator si = this->RccSources.begin();
  1367. si != this->RccSources.end(); ++si) {
  1368. const std::string ext = cmsys::SystemTools::GetFilenameLastExtension(*si);
  1369. if (ext == ".qrc") {
  1370. qrcGenMap[*si] =
  1371. this->AutogenBuildSubDir + this->ChecksumedPath(*si, "qrc_", ".cpp");
  1372. }
  1373. }
  1374. // look for name collisions
  1375. {
  1376. std::multimap<std::string, std::string> collisions;
  1377. if (this->NameCollisionTest(qrcGenMap, collisions)) {
  1378. std::ostringstream ost;
  1379. ost << "AutoRcc: Error: The same qrc_NAME.cpp file"
  1380. " will be generated from different sources.\n"
  1381. "To avoid this error rename the source .qrc files.\n";
  1382. this->LogErrorNameCollision(ost.str(), collisions);
  1383. return false;
  1384. }
  1385. }
  1386. // generate qrc files
  1387. for (std::map<std::string, std::string>::const_iterator si =
  1388. qrcGenMap.begin();
  1389. si != qrcGenMap.end(); ++si) {
  1390. bool unique = FileNameIsUnique(si->first, qrcGenMap);
  1391. if (!this->RccGenerateFile(si->first, si->second, unique)) {
  1392. if (this->RunRccFailed) {
  1393. return false;
  1394. }
  1395. }
  1396. }
  1397. return true;
  1398. }
  1399. /**
  1400. * @return True if a rcc file was created. False may indicate an error.
  1401. */
  1402. bool cmQtAutoGenerators::RccGenerateFile(const std::string& rccInputFile,
  1403. const std::string& rccOutputFile,
  1404. bool unique_n)
  1405. {
  1406. bool rccGenerated = false;
  1407. bool generateRcc = this->GenerateAllRcc;
  1408. const std::string rccBuildFile = this->CurrentBinaryDir + rccOutputFile;
  1409. if (!generateRcc) {
  1410. // Test if the resources list file is newer than build file
  1411. generateRcc = FileAbsentOrOlder(rccBuildFile, rccInputFile);
  1412. if (!generateRcc) {
  1413. // Test if any resource file is newer than the build file
  1414. const std::vector<std::string>& files = this->RccInputs[rccInputFile];
  1415. for (std::vector<std::string>::const_iterator it = files.begin();
  1416. it != files.end(); ++it) {
  1417. if (FileAbsentOrOlder(rccBuildFile, *it)) {
  1418. generateRcc = true;
  1419. break;
  1420. }
  1421. }
  1422. }
  1423. }
  1424. if (generateRcc) {
  1425. // Log
  1426. this->LogBold("Generating RCC source " + rccOutputFile);
  1427. // Make sure the parent directory exists
  1428. if (this->MakeParentDirectory(rccBuildFile)) {
  1429. // Compose symbol name
  1430. std::string symbolName =
  1431. cmsys::SystemTools::GetFilenameWithoutLastExtension(rccInputFile);
  1432. if (!unique_n) {
  1433. symbolName += "_";
  1434. symbolName += fpathCheckSum.getPart(rccInputFile);
  1435. }
  1436. // Replace '-' with '_'. The former is valid for
  1437. // file names but not for symbol names.
  1438. std::replace(symbolName.begin(), symbolName.end(), '-', '_');
  1439. // Compose rcc command
  1440. std::vector<std::string> cmd;
  1441. cmd.push_back(this->RccExecutable);
  1442. {
  1443. std::map<std::string, std::string>::const_iterator optionIt =
  1444. this->RccOptions.find(rccInputFile);
  1445. if (optionIt != this->RccOptions.end()) {
  1446. cmSystemTools::ExpandListArgument(optionIt->second, cmd);
  1447. }
  1448. }
  1449. cmd.push_back("-name");
  1450. cmd.push_back(symbolName);
  1451. cmd.push_back("-o");
  1452. cmd.push_back(rccBuildFile);
  1453. cmd.push_back(rccInputFile);
  1454. // Log command
  1455. if (this->Verbose) {
  1456. this->LogCommand(cmd);
  1457. }
  1458. // Execute command
  1459. bool res = false;
  1460. int retVal = 0;
  1461. std::string output;
  1462. res = cmSystemTools::RunSingleCommand(cmd, &output, &output, &retVal);
  1463. if (!res || (retVal != 0)) {
  1464. // Command failed
  1465. {
  1466. std::ostringstream ost;
  1467. ost << "AutoRcc: Error: rcc process failed for\n";
  1468. ost << Quoted(rccOutputFile) << "\n";
  1469. ost << "AutoRcc: Command:\n" << cmJoin(cmd, " ") << "\n";
  1470. ost << "AutoRcc: Command output:\n" << output << "\n";
  1471. this->LogError(ost.str());
  1472. }
  1473. cmSystemTools::RemoveFile(rccBuildFile);
  1474. this->RunRccFailed = true;
  1475. } else {
  1476. // Success
  1477. rccGenerated = true;
  1478. }
  1479. } else {
  1480. // Parent directory creation failed
  1481. this->RunRccFailed = true;
  1482. }
  1483. }
  1484. return rccGenerated;
  1485. }
  1486. void cmQtAutoGenerators::LogErrorNameCollision(
  1487. const std::string& message,
  1488. const std::multimap<std::string, std::string>& collisions) const
  1489. {
  1490. typedef std::multimap<std::string, std::string>::const_iterator Iter;
  1491. std::ostringstream ost;
  1492. // Add message
  1493. if (!message.empty()) {
  1494. ost << message;
  1495. if (message[message.size() - 1] != '\n') {
  1496. ost << '\n';
  1497. }
  1498. }
  1499. // Append collision list
  1500. for (Iter it = collisions.begin(); it != collisions.end(); ++it) {
  1501. ost << it->first << " : " << it->second << '\n';
  1502. }
  1503. this->LogError(ost.str());
  1504. }
  1505. void cmQtAutoGenerators::LogBold(const std::string& message) const
  1506. {
  1507. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue |
  1508. cmsysTerminal_Color_ForegroundBold,
  1509. message.c_str(), true, this->ColorOutput);
  1510. }
  1511. void cmQtAutoGenerators::LogInfo(const std::string& message) const
  1512. {
  1513. std::string msg(message);
  1514. if (!msg.empty()) {
  1515. if (msg[msg.size() - 1] != '\n') {
  1516. msg.push_back('\n');
  1517. }
  1518. cmSystemTools::Stdout(msg.c_str(), msg.size());
  1519. }
  1520. }
  1521. void cmQtAutoGenerators::LogWarning(const std::string& message) const
  1522. {
  1523. std::string msg(message);
  1524. if (!msg.empty()) {
  1525. if (msg[msg.size() - 1] != '\n') {
  1526. msg.push_back('\n');
  1527. }
  1528. // Append empty line
  1529. msg.push_back('\n');
  1530. cmSystemTools::Stdout(msg.c_str(), msg.size());
  1531. }
  1532. }
  1533. void cmQtAutoGenerators::LogError(const std::string& message) const
  1534. {
  1535. std::string msg(message);
  1536. if (!msg.empty()) {
  1537. if (msg[msg.size() - 1] != '\n') {
  1538. msg.push_back('\n');
  1539. }
  1540. // Append empty line
  1541. msg.push_back('\n');
  1542. cmSystemTools::Stderr(msg.c_str(), msg.size());
  1543. }
  1544. }
  1545. void cmQtAutoGenerators::LogCommand(
  1546. const std::vector<std::string>& command) const
  1547. {
  1548. this->LogInfo(cmJoin(command, " "));
  1549. }
  1550. /**
  1551. * @brief Collects name collisions as output/input pairs
  1552. * @return True if there were collisions
  1553. */
  1554. bool cmQtAutoGenerators::NameCollisionTest(
  1555. const std::map<std::string, std::string>& genFiles,
  1556. std::multimap<std::string, std::string>& collisions) const
  1557. {
  1558. typedef std::map<std::string, std::string>::const_iterator Iter;
  1559. typedef std::map<std::string, std::string>::value_type VType;
  1560. for (Iter ait = genFiles.begin(); ait != genFiles.end(); ++ait) {
  1561. bool first_match(true);
  1562. for (Iter bit = (++Iter(ait)); bit != genFiles.end(); ++bit) {
  1563. if (ait->second == bit->second) {
  1564. if (first_match) {
  1565. if (collisions.find(ait->second) != collisions.end()) {
  1566. // We already know of this collision from before
  1567. break;
  1568. }
  1569. collisions.insert(VType(ait->second, ait->first));
  1570. first_match = false;
  1571. }
  1572. collisions.insert(VType(bit->second, bit->first));
  1573. }
  1574. }
  1575. }
  1576. return !collisions.empty();
  1577. }
  1578. /**
  1579. * @brief Generates a file path based on the checksum of the source file path
  1580. * @return The path
  1581. */
  1582. std::string cmQtAutoGenerators::ChecksumedPath(const std::string& sourceFile,
  1583. const char* basePrefix,
  1584. const char* baseSuffix) const
  1585. {
  1586. std::string res = fpathCheckSum.getPart(sourceFile);
  1587. res += "/";
  1588. res += basePrefix;
  1589. res += cmsys::SystemTools::GetFilenameWithoutLastExtension(sourceFile);
  1590. res += baseSuffix;
  1591. return res;
  1592. }
  1593. /**
  1594. * @brief Tries to find the header file to the given file base path by
  1595. * appending different header extensions
  1596. * @return True on success
  1597. */
  1598. bool cmQtAutoGenerators::FindHeader(std::string& header,
  1599. const std::string& testBasePath) const
  1600. {
  1601. for (std::vector<std::string>::const_iterator ext =
  1602. this->HeaderExtensions.begin();
  1603. ext != this->HeaderExtensions.end(); ++ext) {
  1604. std::string testFilePath(testBasePath);
  1605. testFilePath += '.';
  1606. testFilePath += (*ext);
  1607. if (cmsys::SystemTools::FileExists(testFilePath.c_str())) {
  1608. header = testFilePath;
  1609. return true;
  1610. }
  1611. }
  1612. return false;
  1613. }
  1614. bool cmQtAutoGenerators::FindHeaderGlobal(
  1615. std::string& header, const std::string& testBasePath) const
  1616. {
  1617. for (std::vector<std::string>::const_iterator iit =
  1618. this->MocIncludePaths.begin();
  1619. iit != this->MocIncludePaths.end(); ++iit) {
  1620. const std::string fullPath = ((*iit) + '/' + testBasePath);
  1621. if (FindHeader(header, fullPath)) {
  1622. return true;
  1623. }
  1624. }
  1625. return false;
  1626. }
  1627. std::string cmQtAutoGenerators::FindMocHeader(const std::string& basePath,
  1628. const std::string& baseName,
  1629. const std::string& subDir) const
  1630. {
  1631. std::string header;
  1632. do {
  1633. if (!subDir.empty()) {
  1634. if (this->FindHeader(header, basePath + subDir + baseName)) {
  1635. break;
  1636. }
  1637. }
  1638. if (this->FindHeader(header, basePath + baseName)) {
  1639. break;
  1640. }
  1641. // Try include directories
  1642. if (this->FindHeaderGlobal(header, subDir + baseName)) {
  1643. break;
  1644. }
  1645. } while (false);
  1646. // Sanitize
  1647. if (!header.empty()) {
  1648. header = cmsys::SystemTools::GetRealPath(header);
  1649. }
  1650. return header;
  1651. }
  1652. bool cmQtAutoGenerators::FindIncludedFile(
  1653. std::string& absFile, const std::string& sourceFile,
  1654. const std::string& includeString) const
  1655. {
  1656. bool success = false;
  1657. // Search in vicinity of the source
  1658. {
  1659. std::string testPath = subDirPrefix(sourceFile);
  1660. testPath += includeString;
  1661. if (cmsys::SystemTools::FileExists(testPath.c_str())) {
  1662. absFile = cmsys::SystemTools::GetRealPath(testPath);
  1663. success = true;
  1664. }
  1665. }
  1666. // Search in include directories
  1667. if (!success) {
  1668. for (std::vector<std::string>::const_iterator iit =
  1669. this->MocIncludePaths.begin();
  1670. iit != this->MocIncludePaths.end(); ++iit) {
  1671. const std::string fullPath = ((*iit) + '/' + includeString);
  1672. if (cmsys::SystemTools::FileExists(fullPath.c_str())) {
  1673. absFile = cmsys::SystemTools::GetRealPath(fullPath);
  1674. success = true;
  1675. break;
  1676. }
  1677. }
  1678. }
  1679. return success;
  1680. }
  1681. /**
  1682. * @brief Generates the parent directory of the given file on demand
  1683. * @return True on success
  1684. */
  1685. bool cmQtAutoGenerators::MakeParentDirectory(const std::string& filename) const
  1686. {
  1687. bool success = true;
  1688. const std::string dirName = cmSystemTools::GetFilenamePath(filename);
  1689. if (!dirName.empty()) {
  1690. success = cmsys::SystemTools::MakeDirectory(dirName);
  1691. if (!success) {
  1692. this->LogError("AutoGen: Error: Directory creation failed: " + dirName);
  1693. }
  1694. }
  1695. return success;
  1696. }