cmQtAutoGen.cxx 12 KB

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