| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
- file Copyright.txt or https://cmake.org/licensing for details. */
- #include "cmQtAutoGen.h"
- #include <algorithm>
- #include <array>
- #include <initializer_list>
- #include <sstream>
- #include <utility>
- #include <cmext/algorithm>
- #include "cmsys/FStream.hxx"
- #include "cmsys/RegularExpression.hxx"
- #include "cmDuration.h"
- #include "cmProcessOutput.h"
- #include "cmStringAlgorithms.h"
- #include "cmSystemTools.h"
- // - Static functions
- /// @brief Merges newOpts into baseOpts
- /// @arg valueOpts list of options that accept a value
- void MergeOptions(std::vector<std::string>& baseOpts,
- std::vector<std::string> const& newOpts,
- std::initializer_list<cm::string_view> valueOpts, bool isQt5)
- {
- if (newOpts.empty()) {
- return;
- }
- if (baseOpts.empty()) {
- baseOpts = newOpts;
- return;
- }
- std::vector<std::string> extraOpts;
- for (auto fit = newOpts.begin(), fitEnd = newOpts.end(); fit != fitEnd;
- ++fit) {
- std::string const& newOpt = *fit;
- auto existIt = std::find(baseOpts.begin(), baseOpts.end(), newOpt);
- if (existIt != baseOpts.end()) {
- if (newOpt.size() >= 2) {
- // Acquire the option name
- std::string optName;
- {
- auto oit = newOpt.begin();
- if (*oit == '-') {
- ++oit;
- if (isQt5 && (*oit == '-')) {
- ++oit;
- }
- optName.assign(oit, newOpt.end());
- }
- }
- // Test if this is a value option and change the existing value
- if (!optName.empty() && cm::contains(valueOpts, optName)) {
- const auto existItNext(existIt + 1);
- const auto fitNext(fit + 1);
- if ((existItNext != baseOpts.end()) && (fitNext != fitEnd)) {
- *existItNext = *fitNext;
- ++fit;
- }
- }
- }
- } else {
- extraOpts.push_back(newOpt);
- }
- }
- // Append options
- cm::append(baseOpts, extraOpts);
- }
- // - Class definitions
- unsigned int const cmQtAutoGen::ParallelMax = 64;
- cm::string_view cmQtAutoGen::GeneratorName(GenT genType)
- {
- switch (genType) {
- case GenT::GEN:
- return "AutoGen";
- case GenT::MOC:
- return "AutoMoc";
- case GenT::UIC:
- return "AutoUic";
- case GenT::RCC:
- return "AutoRcc";
- }
- return "AutoGen";
- }
- cm::string_view cmQtAutoGen::GeneratorNameUpper(GenT genType)
- {
- switch (genType) {
- case GenT::GEN:
- return "AUTOGEN";
- case GenT::MOC:
- return "AUTOMOC";
- case GenT::UIC:
- return "AUTOUIC";
- case GenT::RCC:
- return "AUTORCC";
- }
- return "AUTOGEN";
- }
- std::string cmQtAutoGen::Tools(bool moc, bool uic, bool rcc)
- {
- std::array<cm::string_view, 3> lst;
- decltype(lst)::size_type num = 0;
- if (moc) {
- lst.at(num++) = "AUTOMOC";
- }
- if (uic) {
- lst.at(num++) = "AUTOUIC";
- }
- if (rcc) {
- lst.at(num++) = "AUTORCC";
- }
- switch (num) {
- case 1:
- return std::string(lst[0]);
- case 2:
- return cmStrCat(lst[0], " and ", lst[1]);
- case 3:
- return cmStrCat(lst[0], ", ", lst[1], " and ", lst[2]);
- default:
- break;
- }
- return std::string();
- }
- std::string cmQtAutoGen::Quoted(cm::string_view text)
- {
- static std::initializer_list<std::pair<const char*, const char*>> const
- replacements = { { "\\", "\\\\" }, { "\"", "\\\"" }, { "\a", "\\a" },
- { "\b", "\\b" }, { "\f", "\\f" }, { "\n", "\\n" },
- { "\r", "\\r" }, { "\t", "\\t" }, { "\v", "\\v" } };
- std::string res(text);
- for (auto const& pair : replacements) {
- cmSystemTools::ReplaceString(res, pair.first, pair.second);
- }
- return cmStrCat('"', res, '"');
- }
- std::string cmQtAutoGen::QuotedCommand(std::vector<std::string> const& command)
- {
- std::string res;
- for (std::string const& item : command) {
- if (!res.empty()) {
- res.push_back(' ');
- }
- std::string const cesc = cmQtAutoGen::Quoted(item);
- if (item.empty() || (cesc.size() > (item.size() + 2)) ||
- (cesc.find(' ') != std::string::npos)) {
- res += cesc;
- } else {
- res += item;
- }
- }
- return res;
- }
- std::string cmQtAutoGen::FileNameWithoutLastExtension(cm::string_view filename)
- {
- auto slashPos = filename.rfind('/');
- if (slashPos != cm::string_view::npos) {
- filename.remove_prefix(slashPos + 1);
- }
- auto dotPos = filename.rfind('.');
- return std::string(filename.substr(0, dotPos));
- }
- std::string cmQtAutoGen::ParentDir(cm::string_view filename)
- {
- auto slashPos = filename.rfind('/');
- if (slashPos == cm::string_view::npos) {
- return std::string();
- }
- return std::string(filename.substr(0, slashPos));
- }
- std::string cmQtAutoGen::SubDirPrefix(cm::string_view filename)
- {
- auto slashPos = filename.rfind('/');
- if (slashPos == cm::string_view::npos) {
- return std::string();
- }
- return std::string(filename.substr(0, slashPos + 1));
- }
- std::string cmQtAutoGen::AppendFilenameSuffix(cm::string_view filename,
- cm::string_view suffix)
- {
- auto dotPos = filename.rfind('.');
- if (dotPos == cm::string_view::npos) {
- return cmStrCat(filename, suffix);
- }
- return cmStrCat(filename.substr(0, dotPos), suffix,
- filename.substr(dotPos, filename.size() - dotPos));
- }
- void cmQtAutoGen::UicMergeOptions(std::vector<std::string>& baseOpts,
- std::vector<std::string> const& newOpts,
- bool isQt5)
- {
- static std::initializer_list<cm::string_view> const valueOpts = {
- "tr", "translate", "postfix", "generator",
- "include", // Since Qt 5.3
- "g"
- };
- MergeOptions(baseOpts, newOpts, valueOpts, isQt5);
- }
- void cmQtAutoGen::RccMergeOptions(std::vector<std::string>& baseOpts,
- std::vector<std::string> const& newOpts,
- bool isQt5)
- {
- static std::initializer_list<cm::string_view> const valueOpts = {
- "name", "root", "compress", "threshold"
- };
- MergeOptions(baseOpts, newOpts, valueOpts, isQt5);
- }
- static void RccListParseContent(std::string const& content,
- std::vector<std::string>& files)
- {
- cmsys::RegularExpression fileMatchRegex("(<file[^<]+)");
- cmsys::RegularExpression fileReplaceRegex("(^<file[^>]*>)");
- const char* contentChars = content.c_str();
- while (fileMatchRegex.find(contentChars)) {
- std::string const qrcEntry = fileMatchRegex.match(1);
- contentChars += qrcEntry.size();
- {
- fileReplaceRegex.find(qrcEntry);
- std::string const tag = fileReplaceRegex.match(1);
- files.push_back(qrcEntry.substr(tag.size()));
- }
- }
- }
- static bool RccListParseOutput(std::string const& rccStdOut,
- std::string const& rccStdErr,
- std::vector<std::string>& files,
- std::string& error)
- {
- // Lambda to strip CR characters
- auto StripCR = [](std::string& line) {
- std::string::size_type cr = line.find('\r');
- if (cr != std::string::npos) {
- line = line.substr(0, cr);
- }
- };
- // Parse rcc std output
- {
- std::istringstream ostr(rccStdOut);
- std::string oline;
- while (std::getline(ostr, oline)) {
- StripCR(oline);
- if (!oline.empty()) {
- files.push_back(oline);
- }
- }
- }
- // Parse rcc error output
- {
- std::istringstream estr(rccStdErr);
- std::string eline;
- while (std::getline(estr, eline)) {
- StripCR(eline);
- if (cmHasLiteralPrefix(eline, "RCC: Error in")) {
- static std::string const searchString = "Cannot find file '";
- std::string::size_type pos = eline.find(searchString);
- if (pos == std::string::npos) {
- error = cmStrCat("rcc lists unparsable output:\n",
- cmQtAutoGen::Quoted(eline), '\n');
- return false;
- }
- pos += searchString.length();
- std::string::size_type sz = eline.size() - pos - 1;
- files.push_back(eline.substr(pos, sz));
- }
- }
- }
- return true;
- }
- cmQtAutoGen::RccLister::RccLister() = default;
- cmQtAutoGen::RccLister::RccLister(std::string rccExecutable,
- std::vector<std::string> listOptions)
- : RccExcutable_(std::move(rccExecutable))
- , ListOptions_(std::move(listOptions))
- {
- }
- bool cmQtAutoGen::RccLister::list(std::string const& qrcFile,
- std::vector<std::string>& files,
- std::string& error, bool verbose) const
- {
- error.clear();
- if (!cmSystemTools::FileExists(qrcFile, true)) {
- error =
- cmStrCat("The resource file ", Quoted(qrcFile), " does not exist.");
- return false;
- }
- // Run rcc list command in the directory of the qrc file with the pathless
- // qrc file name argument. This way rcc prints relative paths.
- // This avoids issues on Windows when the qrc file is in a path that
- // contains non-ASCII characters.
- std::string const fileDir = cmSystemTools::GetFilenamePath(qrcFile);
- if (!this->RccExcutable_.empty() &&
- cmSystemTools::FileExists(this->RccExcutable_, true) &&
- !this->ListOptions_.empty()) {
- bool result = false;
- int retVal = 0;
- std::string rccStdOut;
- std::string rccStdErr;
- {
- std::vector<std::string> cmd;
- cmd.emplace_back(this->RccExcutable_);
- cm::append(cmd, this->ListOptions_);
- cmd.emplace_back(cmSystemTools::GetFilenameName(qrcFile));
- // Log command
- if (verbose) {
- cmSystemTools::Stdout(
- cmStrCat("Running command:\n", QuotedCommand(cmd), '\n'));
- }
- result = cmSystemTools::RunSingleCommand(
- cmd, &rccStdOut, &rccStdErr, &retVal, fileDir.c_str(),
- cmSystemTools::OUTPUT_NONE, cmDuration::zero(), cmProcessOutput::Auto);
- }
- if (!result || retVal) {
- error =
- cmStrCat("The rcc list process failed for ", Quoted(qrcFile), '\n');
- if (!rccStdOut.empty()) {
- error += cmStrCat(rccStdOut, '\n');
- }
- if (!rccStdErr.empty()) {
- error += cmStrCat(rccStdErr, '\n');
- }
- return false;
- }
- if (!RccListParseOutput(rccStdOut, rccStdErr, files, error)) {
- return false;
- }
- } else {
- // We can't use rcc for the file listing.
- // Read the qrc file content into string and parse it.
- {
- std::string qrcContents;
- {
- cmsys::ifstream ifs(qrcFile.c_str());
- if (ifs) {
- std::ostringstream osst;
- osst << ifs.rdbuf();
- qrcContents = osst.str();
- } else {
- error = cmStrCat("The resource file ", Quoted(qrcFile),
- " is not readable\n");
- return false;
- }
- }
- // Parse string content
- RccListParseContent(qrcContents, files);
- }
- }
- // Convert relative paths to absolute paths
- for (std::string& entry : files) {
- entry = cmSystemTools::CollapseFullPath(entry, fileDir);
- }
- return true;
- }
- bool cmQtAutoGen::FileRead(std::string& content, std::string const& filename,
- std::string* error)
- {
- content.clear();
- if (!cmSystemTools::FileExists(filename, true)) {
- if (error != nullptr) {
- *error = "Not a file.";
- }
- return false;
- }
- unsigned long const length = cmSystemTools::FileLength(filename);
- cmsys::ifstream ifs(filename.c_str(), (std::ios::in | std::ios::binary));
- // Use lambda to save destructor calls of ifs
- return [&ifs, length, &content, error]() -> bool {
- if (!ifs) {
- if (error != nullptr) {
- *error = "Opening the file for reading failed.";
- }
- return false;
- }
- content.reserve(length);
- using IsIt = std::istreambuf_iterator<char>;
- content.assign(IsIt{ ifs }, IsIt{});
- if (!ifs) {
- content.clear();
- if (error != nullptr) {
- *error = "Reading from the file failed.";
- }
- return false;
- }
- return true;
- }();
- }
|