cmQtAutoGen.cxx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 "cmSystemTools.h"
  6. #include "cmsys/RegularExpression.hxx"
  7. #include <algorithm>
  8. #include <array>
  9. #include <sstream>
  10. #include <utility>
  11. // - Static functions
  12. /// @brief Merges newOpts into baseOpts
  13. /// @arg valueOpts list of options that accept a value
  14. void MergeOptions(std::vector<std::string>& baseOpts,
  15. std::vector<std::string> const& newOpts,
  16. std::vector<std::string> const& valueOpts, bool isQt5)
  17. {
  18. typedef std::vector<std::string>::iterator Iter;
  19. typedef std::vector<std::string>::const_iterator CIter;
  20. if (newOpts.empty()) {
  21. return;
  22. }
  23. if (baseOpts.empty()) {
  24. baseOpts = newOpts;
  25. return;
  26. }
  27. std::vector<std::string> extraOpts;
  28. for (CIter fit = newOpts.begin(), fitEnd = newOpts.end(); fit != fitEnd;
  29. ++fit) {
  30. std::string const& newOpt = *fit;
  31. Iter existIt = std::find(baseOpts.begin(), baseOpts.end(), newOpt);
  32. if (existIt != baseOpts.end()) {
  33. if (newOpt.size() >= 2) {
  34. // Acquire the option name
  35. std::string optName;
  36. {
  37. auto oit = newOpt.begin();
  38. if (*oit == '-') {
  39. ++oit;
  40. if (isQt5 && (*oit == '-')) {
  41. ++oit;
  42. }
  43. optName.assign(oit, newOpt.end());
  44. }
  45. }
  46. // Test if this is a value option and change the existing value
  47. if (!optName.empty() &&
  48. (std::find(valueOpts.begin(), valueOpts.end(), optName) !=
  49. valueOpts.end())) {
  50. const Iter existItNext(existIt + 1);
  51. const CIter fitNext(fit + 1);
  52. if ((existItNext != baseOpts.end()) && (fitNext != fitEnd)) {
  53. *existItNext = *fitNext;
  54. ++fit;
  55. }
  56. }
  57. }
  58. } else {
  59. extraOpts.push_back(newOpt);
  60. }
  61. }
  62. // Append options
  63. baseOpts.insert(baseOpts.end(), extraOpts.begin(), extraOpts.end());
  64. }
  65. // - Class definitions
  66. unsigned int const cmQtAutoGen::ParallelMax = 64;
  67. std::string const cmQtAutoGen::ListSep = "<<<S>>>";
  68. std::string const cmQtAutoGen::GenAutoGen = "AutoGen";
  69. std::string const cmQtAutoGen::GenAutoMoc = "AutoMoc";
  70. std::string const cmQtAutoGen::GenAutoUic = "AutoUic";
  71. std::string const cmQtAutoGen::GenAutoRcc = "AutoRcc";
  72. std::string const cmQtAutoGen::GenAUTOGEN = "AUTOGEN";
  73. std::string const cmQtAutoGen::GenAUTOMOC = "AUTOMOC";
  74. std::string const cmQtAutoGen::GenAUTOUIC = "AUTOUIC";
  75. std::string const cmQtAutoGen::GenAUTORCC = "AUTORCC";
  76. std::string const& cmQtAutoGen::GeneratorName(GenT genType)
  77. {
  78. switch (genType) {
  79. case GenT::GEN:
  80. return GenAutoGen;
  81. case GenT::MOC:
  82. return GenAutoMoc;
  83. case GenT::UIC:
  84. return GenAutoUic;
  85. case GenT::RCC:
  86. return GenAutoRcc;
  87. }
  88. return GenAutoGen;
  89. }
  90. std::string const& cmQtAutoGen::GeneratorNameUpper(GenT genType)
  91. {
  92. switch (genType) {
  93. case GenT::GEN:
  94. return GenAUTOGEN;
  95. case GenT::MOC:
  96. return GenAUTOMOC;
  97. case GenT::UIC:
  98. return GenAUTOUIC;
  99. case GenT::RCC:
  100. return GenAUTORCC;
  101. }
  102. return GenAUTOGEN;
  103. }
  104. std::string cmQtAutoGen::Tools(bool moc, bool uic, bool rcc)
  105. {
  106. std::string res;
  107. std::vector<std::string> lst;
  108. if (moc) {
  109. lst.emplace_back(GenAUTOMOC);
  110. }
  111. if (uic) {
  112. lst.emplace_back(GenAUTOUIC);
  113. }
  114. if (rcc) {
  115. lst.emplace_back(GenAUTORCC);
  116. }
  117. switch (lst.size()) {
  118. case 1:
  119. res += lst.at(0);
  120. break;
  121. case 2:
  122. res += lst.at(0);
  123. res += " and ";
  124. res += lst.at(1);
  125. break;
  126. case 3:
  127. res += lst.at(0);
  128. res += ", ";
  129. res += lst.at(1);
  130. res += " and ";
  131. res += lst.at(2);
  132. break;
  133. default:
  134. break;
  135. }
  136. return res;
  137. }
  138. std::string cmQtAutoGen::Quoted(std::string const& text)
  139. {
  140. const std::array<std::pair<const char*, const char*>, 9> replaces = {
  141. { { "\\", "\\\\" },
  142. { "\"", "\\\"" },
  143. { "\a", "\\a" },
  144. { "\b", "\\b" },
  145. { "\f", "\\f" },
  146. { "\n", "\\n" },
  147. { "\r", "\\r" },
  148. { "\t", "\\t" },
  149. { "\v", "\\v" } }
  150. };
  151. std::string res = text;
  152. for (auto const& pair : replaces) {
  153. cmSystemTools::ReplaceString(res, pair.first, pair.second);
  154. }
  155. res = '"' + res;
  156. res += '"';
  157. return res;
  158. }
  159. std::string cmQtAutoGen::QuotedCommand(std::vector<std::string> const& command)
  160. {
  161. std::string res;
  162. for (std::string const& item : command) {
  163. if (!res.empty()) {
  164. res.push_back(' ');
  165. }
  166. std::string const cesc = cmQtAutoGen::Quoted(item);
  167. if (item.empty() || (cesc.size() > (item.size() + 2)) ||
  168. (cesc.find(' ') != std::string::npos)) {
  169. res += cesc;
  170. } else {
  171. res += item;
  172. }
  173. }
  174. return res;
  175. }
  176. std::string cmQtAutoGen::SubDirPrefix(std::string const& filename)
  177. {
  178. std::string res(cmSystemTools::GetFilenamePath(filename));
  179. if (!res.empty()) {
  180. res += '/';
  181. }
  182. return res;
  183. }
  184. std::string cmQtAutoGen::AppendFilenameSuffix(std::string const& filename,
  185. std::string const& suffix)
  186. {
  187. std::string res;
  188. auto pos = filename.rfind('.');
  189. if (pos != std::string::npos) {
  190. const auto it_dot = filename.begin() + pos;
  191. res.assign(filename.begin(), it_dot);
  192. res.append(suffix);
  193. res.append(it_dot, filename.end());
  194. } else {
  195. res = filename;
  196. res.append(suffix);
  197. }
  198. return res;
  199. }
  200. void cmQtAutoGen::UicMergeOptions(std::vector<std::string>& baseOpts,
  201. std::vector<std::string> const& newOpts,
  202. bool isQt5)
  203. {
  204. static std::vector<std::string> const valueOpts = {
  205. "tr", "translate", "postfix", "generator",
  206. "include", // Since Qt 5.3
  207. "g"
  208. };
  209. MergeOptions(baseOpts, newOpts, valueOpts, isQt5);
  210. }
  211. void cmQtAutoGen::RccMergeOptions(std::vector<std::string>& baseOpts,
  212. std::vector<std::string> const& newOpts,
  213. bool isQt5)
  214. {
  215. static std::vector<std::string> const valueOpts = { "name", "root",
  216. "compress",
  217. "threshold" };
  218. MergeOptions(baseOpts, newOpts, valueOpts, isQt5);
  219. }
  220. void cmQtAutoGen::RccListParseContent(std::string const& content,
  221. std::vector<std::string>& files)
  222. {
  223. cmsys::RegularExpression fileMatchRegex("(<file[^<]+)");
  224. cmsys::RegularExpression fileReplaceRegex("(^<file[^>]*>)");
  225. const char* contentChars = content.c_str();
  226. while (fileMatchRegex.find(contentChars)) {
  227. std::string const qrcEntry = fileMatchRegex.match(1);
  228. contentChars += qrcEntry.size();
  229. {
  230. fileReplaceRegex.find(qrcEntry);
  231. std::string const tag = fileReplaceRegex.match(1);
  232. files.push_back(qrcEntry.substr(tag.size()));
  233. }
  234. }
  235. }
  236. bool cmQtAutoGen::RccListParseOutput(std::string const& rccStdOut,
  237. std::string const& rccStdErr,
  238. std::vector<std::string>& files,
  239. std::string& error)
  240. {
  241. // Lambda to strip CR characters
  242. auto StripCR = [](std::string& line) {
  243. std::string::size_type cr = line.find('\r');
  244. if (cr != std::string::npos) {
  245. line = line.substr(0, cr);
  246. }
  247. };
  248. // Parse rcc std output
  249. {
  250. std::istringstream ostr(rccStdOut);
  251. std::string oline;
  252. while (std::getline(ostr, oline)) {
  253. StripCR(oline);
  254. if (!oline.empty()) {
  255. files.push_back(oline);
  256. }
  257. }
  258. }
  259. // Parse rcc error output
  260. {
  261. std::istringstream estr(rccStdErr);
  262. std::string eline;
  263. while (std::getline(estr, eline)) {
  264. StripCR(eline);
  265. if (cmHasLiteralPrefix(eline, "RCC: Error in")) {
  266. static std::string const searchString = "Cannot find file '";
  267. std::string::size_type pos = eline.find(searchString);
  268. if (pos == std::string::npos) {
  269. error = "rcc lists unparsable output:\n";
  270. error += cmQtAutoGen::Quoted(eline);
  271. error += "\n";
  272. return false;
  273. }
  274. pos += searchString.length();
  275. std::string::size_type sz = eline.size() - pos - 1;
  276. files.push_back(eline.substr(pos, sz));
  277. }
  278. }
  279. }
  280. return true;
  281. }
  282. void cmQtAutoGen::RccListConvertFullPath(std::string const& qrcFileDir,
  283. std::vector<std::string>& files)
  284. {
  285. for (std::string& entry : files) {
  286. std::string tmp = cmSystemTools::CollapseFullPath(entry, qrcFileDir);
  287. entry = std::move(tmp);
  288. }
  289. }