cmQtAutoGenerators.cxx 67 KB

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