cmQtAutoGen.cxx 11 KB

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