cmQtAutoGen.cxx 7.5 KB

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