cmQtAutoGen.cxx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 <iterator>
  9. #include <sstream>
  10. // - Static variables
  11. std::string const genNameGen = "AutoGen";
  12. std::string const genNameMoc = "AutoMoc";
  13. std::string const genNameUic = "AutoUic";
  14. std::string const genNameRcc = "AutoRcc";
  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. baseOpts.insert(baseOpts.end(), extraOpts.begin(), extraOpts.end());
  68. }
  69. // - Class definitions
  70. std::string const cmQtAutoGen::ListSep = "<<<S>>>";
  71. unsigned int const cmQtAutoGen::ParallelMax = 64;
  72. std::string const& cmQtAutoGen::GeneratorName(GeneratorT type)
  73. {
  74. switch (type) {
  75. case GeneratorT::GEN:
  76. return genNameGen;
  77. case GeneratorT::MOC:
  78. return genNameMoc;
  79. case GeneratorT::UIC:
  80. return genNameUic;
  81. case GeneratorT::RCC:
  82. return genNameRcc;
  83. }
  84. return genNameGen;
  85. }
  86. std::string cmQtAutoGen::GeneratorNameUpper(GeneratorT genType)
  87. {
  88. return cmSystemTools::UpperCase(cmQtAutoGen::GeneratorName(genType));
  89. }
  90. std::string cmQtAutoGen::Tools(bool moc, bool uic, bool rcc)
  91. {
  92. std::string res;
  93. std::vector<std::string> lst;
  94. if (moc) {
  95. lst.emplace_back("AUTOMOC");
  96. }
  97. if (uic) {
  98. lst.emplace_back("AUTOUIC");
  99. }
  100. if (rcc) {
  101. lst.emplace_back("AUTORCC");
  102. }
  103. switch (lst.size()) {
  104. case 1:
  105. res += lst.at(0);
  106. break;
  107. case 2:
  108. res += lst.at(0);
  109. res += " and ";
  110. res += lst.at(1);
  111. break;
  112. case 3:
  113. res += lst.at(0);
  114. res += ", ";
  115. res += lst.at(1);
  116. res += " and ";
  117. res += lst.at(2);
  118. break;
  119. default:
  120. break;
  121. }
  122. return res;
  123. }
  124. std::string cmQtAutoGen::Quoted(std::string const& text)
  125. {
  126. static const char* rep[18] = { "\\", "\\\\", "\"", "\\\"", "\a", "\\a",
  127. "\b", "\\b", "\f", "\\f", "\n", "\\n",
  128. "\r", "\\r", "\t", "\\t", "\v", "\\v" };
  129. std::string res = text;
  130. for (const char* const* it = cm::cbegin(rep); it != cm::cend(rep); it += 2) {
  131. cmSystemTools::ReplaceString(res, *it, *(it + 1));
  132. }
  133. res = '"' + res;
  134. res += '"';
  135. return res;
  136. }
  137. std::string cmQtAutoGen::QuotedCommand(std::vector<std::string> const& command)
  138. {
  139. std::string res;
  140. for (std::string const& item : command) {
  141. if (!res.empty()) {
  142. res.push_back(' ');
  143. }
  144. std::string const cesc = cmQtAutoGen::Quoted(item);
  145. if (item.empty() || (cesc.size() > (item.size() + 2)) ||
  146. (cesc.find(' ') != std::string::npos)) {
  147. res += cesc;
  148. } else {
  149. res += item;
  150. }
  151. }
  152. return res;
  153. }
  154. std::string cmQtAutoGen::SubDirPrefix(std::string const& filename)
  155. {
  156. std::string res(cmSystemTools::GetFilenamePath(filename));
  157. if (!res.empty()) {
  158. res += '/';
  159. }
  160. return res;
  161. }
  162. std::string cmQtAutoGen::AppendFilenameSuffix(std::string const& filename,
  163. std::string const& suffix)
  164. {
  165. std::string res;
  166. auto pos = filename.rfind('.');
  167. if (pos != std::string::npos) {
  168. const auto it_dot = filename.begin() + pos;
  169. res.assign(filename.begin(), it_dot);
  170. res.append(suffix);
  171. res.append(it_dot, filename.end());
  172. } else {
  173. res = filename;
  174. res.append(suffix);
  175. }
  176. return res;
  177. }
  178. void cmQtAutoGen::UicMergeOptions(std::vector<std::string>& baseOpts,
  179. std::vector<std::string> const& newOpts,
  180. bool isQt5)
  181. {
  182. static std::vector<std::string> const valueOpts = {
  183. "tr", "translate", "postfix", "generator",
  184. "include", // Since Qt 5.3
  185. "g"
  186. };
  187. MergeOptions(baseOpts, newOpts, valueOpts, isQt5);
  188. }
  189. void cmQtAutoGen::RccMergeOptions(std::vector<std::string>& baseOpts,
  190. std::vector<std::string> const& newOpts,
  191. bool isQt5)
  192. {
  193. static std::vector<std::string> const valueOpts = { "name", "root",
  194. "compress",
  195. "threshold" };
  196. MergeOptions(baseOpts, newOpts, valueOpts, isQt5);
  197. }
  198. void cmQtAutoGen::RccListParseContent(std::string const& content,
  199. std::vector<std::string>& files)
  200. {
  201. cmsys::RegularExpression fileMatchRegex("(<file[^<]+)");
  202. cmsys::RegularExpression fileReplaceRegex("(^<file[^>]*>)");
  203. const char* contentChars = content.c_str();
  204. while (fileMatchRegex.find(contentChars)) {
  205. std::string const qrcEntry = fileMatchRegex.match(1);
  206. contentChars += qrcEntry.size();
  207. {
  208. fileReplaceRegex.find(qrcEntry);
  209. std::string const tag = fileReplaceRegex.match(1);
  210. files.push_back(qrcEntry.substr(tag.size()));
  211. }
  212. }
  213. }
  214. bool cmQtAutoGen::RccListParseOutput(std::string const& rccStdOut,
  215. std::string const& rccStdErr,
  216. std::vector<std::string>& files,
  217. std::string& error)
  218. {
  219. // Lambda to strip CR characters
  220. auto StripCR = [](std::string& line) {
  221. std::string::size_type cr = line.find('\r');
  222. if (cr != std::string::npos) {
  223. line = line.substr(0, cr);
  224. }
  225. };
  226. // Parse rcc std output
  227. {
  228. std::istringstream ostr(rccStdOut);
  229. std::string oline;
  230. while (std::getline(ostr, oline)) {
  231. StripCR(oline);
  232. if (!oline.empty()) {
  233. files.push_back(oline);
  234. }
  235. }
  236. }
  237. // Parse rcc error output
  238. {
  239. std::istringstream estr(rccStdErr);
  240. std::string eline;
  241. while (std::getline(estr, eline)) {
  242. StripCR(eline);
  243. if (cmHasLiteralPrefix(eline, "RCC: Error in")) {
  244. static std::string const searchString = "Cannot find file '";
  245. std::string::size_type pos = eline.find(searchString);
  246. if (pos == std::string::npos) {
  247. error = "rcc lists unparsable output:\n";
  248. error += cmQtAutoGen::Quoted(eline);
  249. error += "\n";
  250. return false;
  251. }
  252. pos += searchString.length();
  253. std::string::size_type sz = eline.size() - pos - 1;
  254. files.push_back(eline.substr(pos, sz));
  255. }
  256. }
  257. }
  258. return true;
  259. }
  260. void cmQtAutoGen::RccListConvertFullPath(std::string const& qrcFileDir,
  261. std::vector<std::string>& files)
  262. {
  263. for (std::string& entry : files) {
  264. std::string tmp = cmSystemTools::CollapseCombinedPath(qrcFileDir, entry);
  265. entry = std::move(tmp);
  266. }
  267. }