cmQtAutoGen.cxx 11 KB

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