cmQtAutoGen.cxx 10 KB

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