cmQtAutoGen.cxx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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/FStream.hxx"
  7. #include "cmsys/RegularExpression.hxx"
  8. #include <algorithm>
  9. #include <sstream>
  10. #include <stddef.h>
  11. // - Static variables
  12. const std::string genNameGen = "AutoGen";
  13. const std::string genNameMoc = "AutoMoc";
  14. const std::string genNameUic = "AutoUic";
  15. const std::string genNameRcc = "AutoRcc";
  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. const std::vector<std::string>& newOpts,
  21. const std::vector<std::string>& valueOpts, bool isQt5)
  22. {
  23. typedef std::vector<std::string>::iterator Iter;
  24. typedef std::vector<std::string>::const_iterator CIter;
  25. if (newOpts.empty()) {
  26. return;
  27. }
  28. if (baseOpts.empty()) {
  29. baseOpts = newOpts;
  30. return;
  31. }
  32. std::vector<std::string> extraOpts;
  33. for (CIter fit = newOpts.begin(), fitEnd = newOpts.end(); fit != fitEnd;
  34. ++fit) {
  35. const std::string& newOpt = *fit;
  36. Iter existIt = std::find(baseOpts.begin(), baseOpts.end(), newOpt);
  37. if (existIt != baseOpts.end()) {
  38. if (newOpt.size() >= 2) {
  39. // Acquire the option name
  40. std::string optName;
  41. {
  42. auto oit = newOpt.begin();
  43. if (*oit == '-') {
  44. ++oit;
  45. if (isQt5 && (*oit == '-')) {
  46. ++oit;
  47. }
  48. optName.assign(oit, newOpt.end());
  49. }
  50. }
  51. // Test if this is a value option and change the existing value
  52. if (!optName.empty() && (std::find(valueOpts.begin(), valueOpts.end(),
  53. optName) != 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. static std::string utilStripCR(std::string const& line)
  70. {
  71. // Strip CR characters rcc may have printed (possibly more than one!).
  72. std::string::size_type cr = line.find('\r');
  73. if (cr != std::string::npos) {
  74. return line.substr(0, cr);
  75. }
  76. return line;
  77. }
  78. /// @brief Reads the resource files list from from a .qrc file - Qt4 version
  79. /// @return True if the .qrc file was successfully parsed
  80. static bool RccListInputsQt4(const std::string& fileName,
  81. std::vector<std::string>& files,
  82. std::string* errorMessage)
  83. {
  84. bool allGood = true;
  85. // Read qrc file content into string
  86. std::string qrcContents;
  87. {
  88. cmsys::ifstream ifs(fileName.c_str());
  89. if (ifs) {
  90. std::ostringstream osst;
  91. osst << ifs.rdbuf();
  92. qrcContents = osst.str();
  93. } else {
  94. if (errorMessage != nullptr) {
  95. std::ostringstream ost;
  96. ost << "AutoRcc: Error: Rcc file not readable:\n"
  97. << cmQtAutoGen::Quoted(fileName) << "\n";
  98. *errorMessage = ost.str();
  99. }
  100. allGood = false;
  101. }
  102. }
  103. if (allGood) {
  104. // qrc file directory
  105. std::string qrcDir(cmSystemTools::GetFilenamePath(fileName));
  106. if (!qrcDir.empty()) {
  107. qrcDir += '/';
  108. }
  109. cmsys::RegularExpression fileMatchRegex("(<file[^<]+)");
  110. cmsys::RegularExpression fileReplaceRegex("(^<file[^>]*>)");
  111. size_t offset = 0;
  112. while (fileMatchRegex.find(qrcContents.c_str() + offset)) {
  113. std::string qrcEntry = fileMatchRegex.match(1);
  114. offset += qrcEntry.size();
  115. {
  116. fileReplaceRegex.find(qrcEntry);
  117. std::string tag = fileReplaceRegex.match(1);
  118. qrcEntry = qrcEntry.substr(tag.size());
  119. }
  120. if (!cmSystemTools::FileIsFullPath(qrcEntry.c_str())) {
  121. qrcEntry = qrcDir + qrcEntry;
  122. }
  123. files.push_back(qrcEntry);
  124. }
  125. }
  126. return allGood;
  127. }
  128. /// @brief Reads the resource files list from from a .qrc file - Qt5 version
  129. /// @return True if the .qrc file was successfully parsed
  130. static bool RccListInputsQt5(const std::string& rccCommand,
  131. const std::string& fileName,
  132. std::vector<std::string>& files,
  133. std::string* errorMessage)
  134. {
  135. if (rccCommand.empty()) {
  136. cmSystemTools::Error("AutoRcc: Error: rcc executable not available\n");
  137. return false;
  138. }
  139. // Read rcc features
  140. bool hasDashDashList = false;
  141. {
  142. std::vector<std::string> command;
  143. command.push_back(rccCommand);
  144. command.push_back("--help");
  145. std::string rccStdOut;
  146. std::string rccStdErr;
  147. int retVal = 0;
  148. bool result =
  149. cmSystemTools::RunSingleCommand(command, &rccStdOut, &rccStdErr, &retVal,
  150. nullptr, cmSystemTools::OUTPUT_NONE);
  151. if (result && retVal == 0 &&
  152. rccStdOut.find("--list") != std::string::npos) {
  153. hasDashDashList = true;
  154. }
  155. }
  156. // Run rcc list command
  157. bool result = false;
  158. int retVal = 0;
  159. std::string rccStdOut;
  160. std::string rccStdErr;
  161. {
  162. std::vector<std::string> command;
  163. command.push_back(rccCommand);
  164. command.push_back(hasDashDashList ? "--list" : "-list");
  165. command.push_back(fileName);
  166. result =
  167. cmSystemTools::RunSingleCommand(command, &rccStdOut, &rccStdErr, &retVal,
  168. nullptr, cmSystemTools::OUTPUT_NONE);
  169. }
  170. if (!result || retVal) {
  171. if (errorMessage != nullptr) {
  172. std::ostringstream ost;
  173. ost << "AutoRcc: Error: Rcc list process for " << fileName
  174. << " failed:\n"
  175. << rccStdOut << "\n"
  176. << rccStdErr << "\n";
  177. *errorMessage = ost.str();
  178. }
  179. return false;
  180. }
  181. // Parse rcc std output
  182. {
  183. std::istringstream ostr(rccStdOut);
  184. std::string oline;
  185. while (std::getline(ostr, oline)) {
  186. oline = utilStripCR(oline);
  187. if (!oline.empty()) {
  188. files.push_back(oline);
  189. }
  190. }
  191. }
  192. // Parse rcc error output
  193. {
  194. std::istringstream estr(rccStdErr);
  195. std::string eline;
  196. while (std::getline(estr, eline)) {
  197. eline = utilStripCR(eline);
  198. if (cmHasLiteralPrefix(eline, "RCC: Error in")) {
  199. static std::string searchString = "Cannot find file '";
  200. std::string::size_type pos = eline.find(searchString);
  201. if (pos == std::string::npos) {
  202. if (errorMessage != nullptr) {
  203. std::ostringstream ost;
  204. ost << "AutoRcc: Error: Rcc lists unparsable output:\n"
  205. << cmQtAutoGen::Quoted(eline) << "\n";
  206. *errorMessage = ost.str();
  207. }
  208. return false;
  209. }
  210. pos += searchString.length();
  211. std::string::size_type sz = eline.size() - pos - 1;
  212. files.push_back(eline.substr(pos, sz));
  213. }
  214. }
  215. }
  216. return true;
  217. }
  218. // - Class definitions
  219. const std::string cmQtAutoGen::listSep = "@LSEP@";
  220. const std::string& cmQtAutoGen::GeneratorName(GeneratorType type)
  221. {
  222. switch (type) {
  223. case GeneratorType::GEN:
  224. return genNameGen;
  225. case GeneratorType::MOC:
  226. return genNameMoc;
  227. case GeneratorType::UIC:
  228. return genNameUic;
  229. case GeneratorType::RCC:
  230. return genNameRcc;
  231. }
  232. return genNameGen;
  233. }
  234. std::string cmQtAutoGen::GeneratorNameUpper(GeneratorType genType)
  235. {
  236. return cmSystemTools::UpperCase(cmQtAutoGen::GeneratorName(genType));
  237. }
  238. std::string cmQtAutoGen::Quoted(const std::string& text)
  239. {
  240. static const char* rep[18] = { "\\", "\\\\", "\"", "\\\"", "\a", "\\a",
  241. "\b", "\\b", "\f", "\\f", "\n", "\\n",
  242. "\r", "\\r", "\t", "\\t", "\v", "\\v" };
  243. std::string res = text;
  244. for (const char* const* it = cmArrayBegin(rep); it != cmArrayEnd(rep);
  245. it += 2) {
  246. cmSystemTools::ReplaceString(res, *it, *(it + 1));
  247. }
  248. res = '"' + res;
  249. res += '"';
  250. return res;
  251. }
  252. void cmQtAutoGen::UicMergeOptions(std::vector<std::string>& baseOpts,
  253. const std::vector<std::string>& newOpts,
  254. bool isQt5)
  255. {
  256. static const std::vector<std::string> valueOpts = {
  257. "tr", "translate", "postfix", "generator",
  258. "include", // Since Qt 5.3
  259. "g"
  260. };
  261. MergeOptions(baseOpts, newOpts, valueOpts, isQt5);
  262. }
  263. void cmQtAutoGen::RccMergeOptions(std::vector<std::string>& baseOpts,
  264. const std::vector<std::string>& newOpts,
  265. bool isQt5)
  266. {
  267. static const std::vector<std::string> valueOpts = { "name", "root",
  268. "compress",
  269. "threshold" };
  270. MergeOptions(baseOpts, newOpts, valueOpts, isQt5);
  271. }
  272. bool cmQtAutoGen::RccListInputs(const std::string& qtMajorVersion,
  273. const std::string& rccCommand,
  274. const std::string& fileName,
  275. std::vector<std::string>& files,
  276. std::string* errorMessage)
  277. {
  278. bool allGood = false;
  279. if (cmSystemTools::FileExists(fileName.c_str())) {
  280. if (qtMajorVersion == "4") {
  281. allGood = RccListInputsQt4(fileName, files, errorMessage);
  282. } else {
  283. allGood = RccListInputsQt5(rccCommand, fileName, files, errorMessage);
  284. }
  285. } else {
  286. if (errorMessage != nullptr) {
  287. std::ostringstream ost;
  288. ost << "AutoRcc: Error: Rcc file does not exist:\n"
  289. << cmQtAutoGen::Quoted(fileName) << "\n";
  290. *errorMessage = ost.str();
  291. }
  292. }
  293. return allGood;
  294. }