cmQtAutoGen.cxx 11 KB

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