cmQtAutoGenerators.cxx 63 KB

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