cmQtAutoGen.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 <algorithm>
  5. #include <array>
  6. #include <initializer_list>
  7. #include <sstream>
  8. #include <utility>
  9. #include <cmext/algorithm>
  10. #include "cmsys/FStream.hxx"
  11. #include "cmsys/RegularExpression.hxx"
  12. #include "cmDuration.h"
  13. #include "cmProcessOutput.h"
  14. #include "cmStringAlgorithms.h"
  15. #include "cmSystemTools.h"
  16. // - Static functions
  17. /// @brief Merges newOpts into baseOpts
  18. /// @arg valueOpts list of options that accept a value
  19. static void MergeOptions(std::vector<std::string>& baseOpts,
  20. std::vector<std::string> const& newOpts,
  21. std::initializer_list<cm::string_view> valueOpts,
  22. bool isQt5OrLater)
  23. {
  24. if (newOpts.empty()) {
  25. return;
  26. }
  27. if (baseOpts.empty()) {
  28. baseOpts = newOpts;
  29. return;
  30. }
  31. std::vector<std::string> extraOpts;
  32. for (auto fit = newOpts.begin(), fitEnd = newOpts.end(); fit != fitEnd;
  33. ++fit) {
  34. std::string const& newOpt = *fit;
  35. auto existIt = std::find(baseOpts.begin(), baseOpts.end(), newOpt);
  36. if (existIt != baseOpts.end()) {
  37. if (newOpt.size() >= 2) {
  38. // Acquire the option name
  39. std::string optName;
  40. {
  41. auto oit = newOpt.begin();
  42. if (*oit == '-') {
  43. ++oit;
  44. if (isQt5OrLater && (*oit == '-')) {
  45. ++oit;
  46. }
  47. optName.assign(oit, newOpt.end());
  48. }
  49. }
  50. // Test if this is a value option and change the existing value
  51. if (!optName.empty() && cm::contains(valueOpts, optName)) {
  52. const auto existItNext(existIt + 1);
  53. const auto fitNext(fit + 1);
  54. if ((existItNext != baseOpts.end()) && (fitNext != fitEnd)) {
  55. *existItNext = *fitNext;
  56. ++fit;
  57. }
  58. }
  59. }
  60. } else {
  61. extraOpts.push_back(newOpt);
  62. }
  63. }
  64. // Append options
  65. cm::append(baseOpts, extraOpts);
  66. }
  67. // - Class definitions
  68. unsigned int const cmQtAutoGen::ParallelMax = 64;
  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::FileNameWithoutLastExtension(cm::string_view filename)
  152. {
  153. auto slashPos = filename.rfind('/');
  154. if (slashPos != cm::string_view::npos) {
  155. filename.remove_prefix(slashPos + 1);
  156. }
  157. auto dotPos = filename.rfind('.');
  158. return std::string(filename.substr(0, dotPos));
  159. }
  160. std::string cmQtAutoGen::ParentDir(cm::string_view filename)
  161. {
  162. auto slashPos = filename.rfind('/');
  163. if (slashPos == cm::string_view::npos) {
  164. return std::string();
  165. }
  166. return std::string(filename.substr(0, slashPos));
  167. }
  168. std::string cmQtAutoGen::SubDirPrefix(cm::string_view filename)
  169. {
  170. auto slashPos = filename.rfind('/');
  171. if (slashPos == cm::string_view::npos) {
  172. return std::string();
  173. }
  174. return std::string(filename.substr(0, slashPos + 1));
  175. }
  176. std::string cmQtAutoGen::AppendFilenameSuffix(cm::string_view filename,
  177. cm::string_view suffix)
  178. {
  179. auto dotPos = filename.rfind('.');
  180. if (dotPos == cm::string_view::npos) {
  181. return cmStrCat(filename, suffix);
  182. }
  183. return cmStrCat(filename.substr(0, dotPos), suffix,
  184. filename.substr(dotPos, filename.size() - dotPos));
  185. }
  186. void cmQtAutoGen::UicMergeOptions(std::vector<std::string>& baseOpts,
  187. std::vector<std::string> const& newOpts,
  188. bool isQt5OrLater)
  189. {
  190. static std::initializer_list<cm::string_view> const valueOpts = {
  191. "tr", "translate", "postfix", "generator",
  192. "include", // Since Qt 5.3
  193. "g"
  194. };
  195. MergeOptions(baseOpts, newOpts, valueOpts, isQt5OrLater);
  196. }
  197. void cmQtAutoGen::RccMergeOptions(std::vector<std::string>& baseOpts,
  198. std::vector<std::string> const& newOpts,
  199. bool isQt5OrLater)
  200. {
  201. static std::initializer_list<cm::string_view> const valueOpts = {
  202. "name", "root", "compress", "threshold"
  203. };
  204. MergeOptions(baseOpts, newOpts, valueOpts, isQt5OrLater);
  205. }
  206. static void RccListParseContent(std::string const& content,
  207. std::vector<std::string>& files)
  208. {
  209. cmsys::RegularExpression fileMatchRegex("(<file[^<]+)");
  210. cmsys::RegularExpression fileReplaceRegex("(^<file[^>]*>)");
  211. const char* contentChars = content.c_str();
  212. while (fileMatchRegex.find(contentChars)) {
  213. std::string const qrcEntry = fileMatchRegex.match(1);
  214. contentChars += qrcEntry.size();
  215. {
  216. fileReplaceRegex.find(qrcEntry);
  217. std::string const tag = fileReplaceRegex.match(1);
  218. files.push_back(qrcEntry.substr(tag.size()));
  219. }
  220. }
  221. }
  222. static bool RccListParseOutput(std::string const& rccStdOut,
  223. std::string const& rccStdErr,
  224. std::vector<std::string>& files,
  225. std::string& error)
  226. {
  227. // Lambda to strip CR characters
  228. auto StripCR = [](std::string& line) {
  229. std::string::size_type cr = line.find('\r');
  230. if (cr != std::string::npos) {
  231. line = line.substr(0, cr);
  232. }
  233. };
  234. // Parse rcc std output
  235. {
  236. std::istringstream ostr(rccStdOut);
  237. std::string oline;
  238. while (std::getline(ostr, oline)) {
  239. StripCR(oline);
  240. if (!oline.empty()) {
  241. files.push_back(oline);
  242. }
  243. }
  244. }
  245. // Parse rcc error output
  246. {
  247. std::istringstream estr(rccStdErr);
  248. std::string eline;
  249. while (std::getline(estr, eline)) {
  250. StripCR(eline);
  251. if (cmHasLiteralPrefix(eline, "RCC: Error in")) {
  252. static std::string const searchString = "Cannot find file '";
  253. std::string::size_type pos = eline.find(searchString);
  254. if (pos == std::string::npos) {
  255. error = cmStrCat("rcc lists unparsable output:\n",
  256. cmQtAutoGen::Quoted(eline), '\n');
  257. return false;
  258. }
  259. pos += searchString.length();
  260. std::string::size_type sz = eline.size() - pos - 1;
  261. files.push_back(eline.substr(pos, sz));
  262. }
  263. }
  264. }
  265. return true;
  266. }
  267. cmQtAutoGen::RccLister::RccLister() = default;
  268. cmQtAutoGen::RccLister::RccLister(std::string rccExecutable,
  269. std::vector<std::string> listOptions)
  270. : RccExecutable_(std::move(rccExecutable))
  271. , ListOptions_(std::move(listOptions))
  272. {
  273. }
  274. bool cmQtAutoGen::RccLister::list(std::string const& qrcFile,
  275. std::vector<std::string>& files,
  276. std::string& error, bool verbose) const
  277. {
  278. error.clear();
  279. if (!cmSystemTools::FileExists(qrcFile, true)) {
  280. error =
  281. cmStrCat("The resource file ", Quoted(qrcFile), " does not exist.");
  282. return false;
  283. }
  284. // Run rcc list command in the directory of the qrc file with the pathless
  285. // qrc file name argument. This way rcc prints relative paths.
  286. // This avoids issues on Windows when the qrc file is in a path that
  287. // contains non-ASCII characters.
  288. std::string const fileDir = cmSystemTools::GetFilenamePath(qrcFile);
  289. if (!this->RccExecutable_.empty() &&
  290. cmSystemTools::FileExists(this->RccExecutable_, true) &&
  291. !this->ListOptions_.empty()) {
  292. bool result = false;
  293. int retVal = 0;
  294. std::string rccStdOut;
  295. std::string rccStdErr;
  296. {
  297. std::vector<std::string> cmd;
  298. cmd.emplace_back(this->RccExecutable_);
  299. cm::append(cmd, this->ListOptions_);
  300. cmd.emplace_back(cmSystemTools::GetFilenameName(qrcFile));
  301. // Log command
  302. if (verbose) {
  303. cmSystemTools::Stdout(
  304. cmStrCat("Running command:\n", QuotedCommand(cmd), '\n'));
  305. }
  306. result = cmSystemTools::RunSingleCommand(
  307. cmd, &rccStdOut, &rccStdErr, &retVal, fileDir.c_str(),
  308. cmSystemTools::OUTPUT_NONE, cmDuration::zero(), cmProcessOutput::Auto);
  309. }
  310. if (!result || retVal) {
  311. error =
  312. cmStrCat("The rcc list process failed for ", Quoted(qrcFile), '\n');
  313. if (!rccStdOut.empty()) {
  314. error += cmStrCat(rccStdOut, '\n');
  315. }
  316. if (!rccStdErr.empty()) {
  317. error += cmStrCat(rccStdErr, '\n');
  318. }
  319. return false;
  320. }
  321. if (!RccListParseOutput(rccStdOut, rccStdErr, files, error)) {
  322. return false;
  323. }
  324. } else {
  325. // We can't use rcc for the file listing.
  326. // Read the qrc file content into string and parse it.
  327. {
  328. std::string qrcContents;
  329. {
  330. cmsys::ifstream ifs(qrcFile.c_str());
  331. if (ifs) {
  332. std::ostringstream osst;
  333. osst << ifs.rdbuf();
  334. qrcContents = osst.str();
  335. } else {
  336. error = cmStrCat("The resource file ", Quoted(qrcFile),
  337. " is not readable\n");
  338. return false;
  339. }
  340. }
  341. // Parse string content
  342. RccListParseContent(qrcContents, files);
  343. }
  344. }
  345. // Convert relative paths to absolute paths
  346. for (std::string& entry : files) {
  347. entry = cmSystemTools::CollapseFullPath(entry, fileDir);
  348. }
  349. return true;
  350. }