cmQtAutoGen.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 "cmQtAutoGen.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmDuration.h"
  6. #include "cmProcessOutput.h"
  7. #include "cmStringAlgorithms.h"
  8. #include "cmSystemTools.h"
  9. #include "cmsys/FStream.hxx"
  10. #include "cmsys/RegularExpression.hxx"
  11. #include <algorithm>
  12. #include <array>
  13. #include <initializer_list>
  14. #include <sstream>
  15. #include <utility>
  16. // - Static functions
  17. /// @brief Merges newOpts into baseOpts
  18. /// @arg valueOpts list of options that accept a value
  19. void MergeOptions(std::vector<std::string>& baseOpts,
  20. std::vector<std::string> const& newOpts,
  21. std::initializer_list<cm::string_view> valueOpts, bool isQt5)
  22. {
  23. using Iter = std::vector<std::string>::iterator;
  24. using CIter = std::vector<std::string>::const_iterator;
  25. if (newOpts.empty()) {
  26. return;
  27. }
  28. if (baseOpts.empty()) {
  29. baseOpts = newOpts;
  30. return;
  31. }
  32. std::vector<std::string> extraOpts;
  33. for (CIter fit = newOpts.begin(), fitEnd = newOpts.end(); fit != fitEnd;
  34. ++fit) {
  35. std::string const& newOpt = *fit;
  36. Iter existIt = std::find(baseOpts.begin(), baseOpts.end(), newOpt);
  37. if (existIt != baseOpts.end()) {
  38. if (newOpt.size() >= 2) {
  39. // Acquire the option name
  40. std::string optName;
  41. {
  42. auto oit = newOpt.begin();
  43. if (*oit == '-') {
  44. ++oit;
  45. if (isQt5 && (*oit == '-')) {
  46. ++oit;
  47. }
  48. optName.assign(oit, newOpt.end());
  49. }
  50. }
  51. // Test if this is a value option and change the existing value
  52. if (!optName.empty() && cmContains(valueOpts, optName)) {
  53. const Iter existItNext(existIt + 1);
  54. const CIter fitNext(fit + 1);
  55. if ((existItNext != baseOpts.end()) && (fitNext != fitEnd)) {
  56. *existItNext = *fitNext;
  57. ++fit;
  58. }
  59. }
  60. }
  61. } else {
  62. extraOpts.push_back(newOpt);
  63. }
  64. }
  65. // Append options
  66. cmAppend(baseOpts, extraOpts);
  67. }
  68. // - Class definitions
  69. unsigned int const cmQtAutoGen::ParallelMax = 64;
  70. std::string const cmQtAutoGen::ListSep = "<<<S>>>";
  71. cm::string_view cmQtAutoGen::GeneratorName(GenT genType)
  72. {
  73. switch (genType) {
  74. case GenT::GEN:
  75. return "AutoGen";
  76. case GenT::MOC:
  77. return "AutoMoc";
  78. case GenT::UIC:
  79. return "AutoUic";
  80. case GenT::RCC:
  81. return "AutoRcc";
  82. }
  83. return "AutoGen";
  84. }
  85. cm::string_view cmQtAutoGen::GeneratorNameUpper(GenT genType)
  86. {
  87. switch (genType) {
  88. case GenT::GEN:
  89. return "AUTOGEN";
  90. case GenT::MOC:
  91. return "AUTOMOC";
  92. case GenT::UIC:
  93. return "AUTOUIC";
  94. case GenT::RCC:
  95. return "AUTORCC";
  96. }
  97. return "AUTOGEN";
  98. }
  99. std::string cmQtAutoGen::Tools(bool moc, bool uic, bool rcc)
  100. {
  101. std::array<cm::string_view, 3> lst;
  102. decltype(lst)::size_type num = 0;
  103. if (moc) {
  104. lst.at(num++) = "AUTOMOC";
  105. }
  106. if (uic) {
  107. lst.at(num++) = "AUTOUIC";
  108. }
  109. if (rcc) {
  110. lst.at(num++) = "AUTORCC";
  111. }
  112. switch (num) {
  113. case 1:
  114. return std::string(lst[0]);
  115. case 2:
  116. return cmStrCat(lst[0], " and ", lst[1]);
  117. case 3:
  118. return cmStrCat(lst[0], ", ", lst[1], " and ", lst[2]);
  119. default:
  120. break;
  121. }
  122. return std::string();
  123. }
  124. std::string cmQtAutoGen::Quoted(cm::string_view text)
  125. {
  126. static std::initializer_list<std::pair<const char*, const char*>> const
  127. replacements = { { "\\", "\\\\" }, { "\"", "\\\"" }, { "\a", "\\a" },
  128. { "\b", "\\b" }, { "\f", "\\f" }, { "\n", "\\n" },
  129. { "\r", "\\r" }, { "\t", "\\t" }, { "\v", "\\v" } };
  130. std::string res(text);
  131. for (auto const& pair : replacements) {
  132. cmSystemTools::ReplaceString(res, pair.first, pair.second);
  133. }
  134. return cmStrCat('"', res, '"');
  135. }
  136. std::string cmQtAutoGen::QuotedCommand(std::vector<std::string> const& command)
  137. {
  138. std::string res;
  139. for (std::string const& item : command) {
  140. if (!res.empty()) {
  141. res.push_back(' ');
  142. }
  143. std::string const cesc = cmQtAutoGen::Quoted(item);
  144. if (item.empty() || (cesc.size() > (item.size() + 2)) ||
  145. (cesc.find(' ') != std::string::npos)) {
  146. res += cesc;
  147. } else {
  148. res += item;
  149. }
  150. }
  151. return res;
  152. }
  153. std::string cmQtAutoGen::SubDirPrefix(cm::string_view filename)
  154. {
  155. auto slashPos = filename.rfind('/');
  156. if (slashPos == cm::string_view::npos) {
  157. return std::string();
  158. }
  159. return std::string(filename.substr(0, slashPos + 1));
  160. }
  161. std::string cmQtAutoGen::AppendFilenameSuffix(cm::string_view filename,
  162. cm::string_view suffix)
  163. {
  164. auto dotPos = filename.rfind('.');
  165. if (dotPos == cm::string_view::npos) {
  166. return cmStrCat(filename, suffix);
  167. }
  168. return cmStrCat(filename.substr(0, dotPos), suffix,
  169. filename.substr(dotPos, filename.size() - dotPos));
  170. }
  171. void cmQtAutoGen::UicMergeOptions(std::vector<std::string>& baseOpts,
  172. std::vector<std::string> const& newOpts,
  173. bool isQt5)
  174. {
  175. static std::initializer_list<cm::string_view> const valueOpts = {
  176. "tr", "translate", "postfix", "generator",
  177. "include", // Since Qt 5.3
  178. "g"
  179. };
  180. MergeOptions(baseOpts, newOpts, valueOpts, isQt5);
  181. }
  182. void cmQtAutoGen::RccMergeOptions(std::vector<std::string>& baseOpts,
  183. std::vector<std::string> const& newOpts,
  184. bool isQt5)
  185. {
  186. static std::initializer_list<cm::string_view> const valueOpts = {
  187. "name", "root", "compress", "threshold"
  188. };
  189. MergeOptions(baseOpts, newOpts, valueOpts, isQt5);
  190. }
  191. static void RccListParseContent(std::string const& content,
  192. std::vector<std::string>& files)
  193. {
  194. cmsys::RegularExpression fileMatchRegex("(<file[^<]+)");
  195. cmsys::RegularExpression fileReplaceRegex("(^<file[^>]*>)");
  196. const char* contentChars = content.c_str();
  197. while (fileMatchRegex.find(contentChars)) {
  198. std::string const qrcEntry = fileMatchRegex.match(1);
  199. contentChars += qrcEntry.size();
  200. {
  201. fileReplaceRegex.find(qrcEntry);
  202. std::string const tag = fileReplaceRegex.match(1);
  203. files.push_back(qrcEntry.substr(tag.size()));
  204. }
  205. }
  206. }
  207. static bool RccListParseOutput(std::string const& rccStdOut,
  208. std::string const& rccStdErr,
  209. std::vector<std::string>& files,
  210. std::string& error)
  211. {
  212. // Lambda to strip CR characters
  213. auto StripCR = [](std::string& line) {
  214. std::string::size_type cr = line.find('\r');
  215. if (cr != std::string::npos) {
  216. line = line.substr(0, cr);
  217. }
  218. };
  219. // Parse rcc std output
  220. {
  221. std::istringstream ostr(rccStdOut);
  222. std::string oline;
  223. while (std::getline(ostr, oline)) {
  224. StripCR(oline);
  225. if (!oline.empty()) {
  226. files.push_back(oline);
  227. }
  228. }
  229. }
  230. // Parse rcc error output
  231. {
  232. std::istringstream estr(rccStdErr);
  233. std::string eline;
  234. while (std::getline(estr, eline)) {
  235. StripCR(eline);
  236. if (cmHasLiteralPrefix(eline, "RCC: Error in")) {
  237. static std::string const searchString = "Cannot find file '";
  238. std::string::size_type pos = eline.find(searchString);
  239. if (pos == std::string::npos) {
  240. error = cmStrCat("rcc lists unparsable output:\n",
  241. cmQtAutoGen::Quoted(eline), '\n');
  242. return false;
  243. }
  244. pos += searchString.length();
  245. std::string::size_type sz = eline.size() - pos - 1;
  246. files.push_back(eline.substr(pos, sz));
  247. }
  248. }
  249. }
  250. return true;
  251. }
  252. cmQtAutoGen::RccLister::RccLister() = default;
  253. cmQtAutoGen::RccLister::RccLister(std::string rccExecutable,
  254. std::vector<std::string> listOptions)
  255. : RccExcutable_(std::move(rccExecutable))
  256. , ListOptions_(std::move(listOptions))
  257. {
  258. }
  259. bool cmQtAutoGen::RccLister::list(std::string const& qrcFile,
  260. std::vector<std::string>& files,
  261. std::string& error, bool verbose) const
  262. {
  263. error.clear();
  264. if (!cmSystemTools::FileExists(qrcFile, true)) {
  265. error =
  266. cmStrCat("The resource file ", Quoted(qrcFile), " does not exist.");
  267. return false;
  268. }
  269. // Run rcc list command in the directory of the qrc file with the pathless
  270. // qrc file name argument. This way rcc prints relative paths.
  271. // This avoids issues on Windows when the qrc file is in a path that
  272. // contains non-ASCII characters.
  273. std::string const fileDir = cmSystemTools::GetFilenamePath(qrcFile);
  274. if (!this->RccExcutable_.empty() &&
  275. cmSystemTools::FileExists(this->RccExcutable_, true) &&
  276. !this->ListOptions_.empty()) {
  277. bool result = false;
  278. int retVal = 0;
  279. std::string rccStdOut;
  280. std::string rccStdErr;
  281. {
  282. std::vector<std::string> cmd;
  283. cmd.emplace_back(this->RccExcutable_);
  284. cmAppend(cmd, this->ListOptions_);
  285. cmd.emplace_back(cmSystemTools::GetFilenameName(qrcFile));
  286. // Log command
  287. if (verbose) {
  288. cmSystemTools::Stdout(
  289. cmStrCat("Running command:\n", QuotedCommand(cmd), '\n'));
  290. }
  291. result = cmSystemTools::RunSingleCommand(
  292. cmd, &rccStdOut, &rccStdErr, &retVal, fileDir.c_str(),
  293. cmSystemTools::OUTPUT_NONE, cmDuration::zero(), cmProcessOutput::Auto);
  294. }
  295. if (!result || retVal) {
  296. error =
  297. cmStrCat("The rcc list process failed for ", Quoted(qrcFile), '\n');
  298. if (!rccStdOut.empty()) {
  299. error += cmStrCat(rccStdOut, '\n');
  300. }
  301. if (!rccStdErr.empty()) {
  302. error += cmStrCat(rccStdErr, '\n');
  303. }
  304. return false;
  305. }
  306. if (!RccListParseOutput(rccStdOut, rccStdErr, files, error)) {
  307. return false;
  308. }
  309. } else {
  310. // We can't use rcc for the file listing.
  311. // Read the qrc file content into string and parse it.
  312. {
  313. std::string qrcContents;
  314. {
  315. cmsys::ifstream ifs(qrcFile.c_str());
  316. if (ifs) {
  317. std::ostringstream osst;
  318. osst << ifs.rdbuf();
  319. qrcContents = osst.str();
  320. } else {
  321. error = cmStrCat("The resource file ", Quoted(qrcFile),
  322. " is not readable\n");
  323. return false;
  324. }
  325. }
  326. // Parse string content
  327. RccListParseContent(qrcContents, files);
  328. }
  329. }
  330. // Convert relative paths to absolute paths
  331. for (std::string& entry : files) {
  332. entry = cmSystemTools::CollapseFullPath(entry, fileDir);
  333. }
  334. return true;
  335. }