cmQtAutoGen.cxx 8.48 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3
#include "cmQtAutoGen.h"
4 5 6
#include "cmAlgorithms.h"
#include "cmSystemTools.h"

7
#include "cmsys/RegularExpression.hxx"
8

9
#include <algorithm>
10
#include <array>
11
#include <sstream>
12
#include <utility>
13 14 15

// - Static functions

16 17 18
/// @brief Merges newOpts into baseOpts
/// @arg valueOpts list of options that accept a value
void MergeOptions(std::vector<std::string>& baseOpts,
19 20
                  std::vector<std::string> const& newOpts,
                  std::vector<std::string> const& valueOpts, bool isQt5)
21 22 23 24 25 26 27 28 29 30 31 32 33 34
{
  typedef std::vector<std::string>::iterator Iter;
  typedef std::vector<std::string>::const_iterator CIter;
  if (newOpts.empty()) {
    return;
  }
  if (baseOpts.empty()) {
    baseOpts = newOpts;
    return;
  }

  std::vector<std::string> extraOpts;
  for (CIter fit = newOpts.begin(), fitEnd = newOpts.end(); fit != fitEnd;
       ++fit) {
35
    std::string const& newOpt = *fit;
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    Iter 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
52 53 54
        if (!optName.empty() &&
            (std::find(valueOpts.begin(), valueOpts.end(), optName) !=
             valueOpts.end())) {
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
          const Iter existItNext(existIt + 1);
          const CIter fitNext(fit + 1);
          if ((existItNext != baseOpts.end()) && (fitNext != fitEnd)) {
            *existItNext = *fitNext;
            ++fit;
          }
        }
      }
    } else {
      extraOpts.push_back(newOpt);
    }
  }
  // Append options
  baseOpts.insert(baseOpts.end(), extraOpts.begin(), extraOpts.end());
}

71 72
// - Class definitions

73
unsigned int const cmQtAutoGen::ParallelMax = 64;
74 75
std::string const cmQtAutoGen::ListSep = "<<<S>>>";

76 77 78 79
std::string const cmQtAutoGen::GenAutoGen = "AutoGen";
std::string const cmQtAutoGen::GenAutoMoc = "AutoMoc";
std::string const cmQtAutoGen::GenAutoUic = "AutoUic";
std::string const cmQtAutoGen::GenAutoRcc = "AutoRcc";
80

81 82 83 84
std::string const cmQtAutoGen::GenAUTOGEN = "AUTOGEN";
std::string const cmQtAutoGen::GenAUTOMOC = "AUTOMOC";
std::string const cmQtAutoGen::GenAUTOUIC = "AUTOUIC";
std::string const cmQtAutoGen::GenAUTORCC = "AUTORCC";
85

86
std::string const& cmQtAutoGen::GeneratorName(GenT genType)
87
{
88
  switch (genType) {
89
    case GenT::GEN:
90
      return GenAutoGen;
91
    case GenT::MOC:
92
      return GenAutoMoc;
93
    case GenT::UIC:
94
      return GenAutoUic;
95
    case GenT::RCC:
96
      return GenAutoRcc;
97
  }
98
  return GenAutoGen;
99 100
}

101
std::string const& cmQtAutoGen::GeneratorNameUpper(GenT genType)
102
{
103
  switch (genType) {
104
    case GenT::GEN:
105
      return GenAUTOGEN;
106
    case GenT::MOC:
107
      return GenAUTOMOC;
108
    case GenT::UIC:
109
      return GenAUTOUIC;
110
    case GenT::RCC:
111
      return GenAUTORCC;
112
  }
113
  return GenAUTOGEN;
114
}
115

116 117 118 119 120
std::string cmQtAutoGen::Tools(bool moc, bool uic, bool rcc)
{
  std::string res;
  std::vector<std::string> lst;
  if (moc) {
121
    lst.emplace_back(GenAUTOMOC);
122 123
  }
  if (uic) {
124
    lst.emplace_back(GenAUTOUIC);
125 126
  }
  if (rcc) {
127
    lst.emplace_back(GenAUTORCC);
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  }
  switch (lst.size()) {
    case 1:
      res += lst.at(0);
      break;
    case 2:
      res += lst.at(0);
      res += " and ";
      res += lst.at(1);
      break;
    case 3:
      res += lst.at(0);
      res += ", ";
      res += lst.at(1);
      res += " and ";
      res += lst.at(2);
      break;
    default:
      break;
  }
  return res;
}

151
std::string cmQtAutoGen::Quoted(std::string const& text)
152
{
153 154 155 156 157 158 159 160 161 162 163
  const std::array<std::pair<const char*, const char*>, 9> replaces = {
    { { "\\", "\\\\" },
      { "\"", "\\\"" },
      { "\a", "\\a" },
      { "\b", "\\b" },
      { "\f", "\\f" },
      { "\n", "\\n" },
      { "\r", "\\r" },
      { "\t", "\\t" },
      { "\v", "\\v" } }
  };
164 165

  std::string res = text;
166 167
  for (auto const& pair : replaces) {
    cmSystemTools::ReplaceString(res, pair.first, pair.second);
168 169 170 171 172 173
  }
  res = '"' + res;
  res += '"';
  return res;
}

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
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::SubDirPrefix(std::string const& filename)
{
  std::string res(cmSystemTools::GetFilenamePath(filename));
  if (!res.empty()) {
    res += '/';
  }
  return res;
}

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
std::string cmQtAutoGen::AppendFilenameSuffix(std::string const& filename,
                                              std::string const& suffix)
{
  std::string res;
  auto pos = filename.rfind('.');
  if (pos != std::string::npos) {
    const auto it_dot = filename.begin() + pos;
    res.assign(filename.begin(), it_dot);
    res.append(suffix);
    res.append(it_dot, filename.end());
  } else {
    res = filename;
    res.append(suffix);
  }
  return res;
}

218
void cmQtAutoGen::UicMergeOptions(std::vector<std::string>& baseOpts,
219
                                  std::vector<std::string> const& newOpts,
220 221
                                  bool isQt5)
{
222
  static std::vector<std::string> const valueOpts = {
223 224 225 226 227 228 229 230
    "tr",      "translate", "postfix", "generator",
    "include", // Since Qt 5.3
    "g"
  };
  MergeOptions(baseOpts, newOpts, valueOpts, isQt5);
}

void cmQtAutoGen::RccMergeOptions(std::vector<std::string>& baseOpts,
231
                                  std::vector<std::string> const& newOpts,
232 233
                                  bool isQt5)
{
234
  static std::vector<std::string> const valueOpts = { "name", "root",
235 236 237 238 239
                                                      "compress",
                                                      "threshold" };
  MergeOptions(baseOpts, newOpts, valueOpts, isQt5);
}

240 241
void cmQtAutoGen::RccListParseContent(std::string const& content,
                                      std::vector<std::string>& files)
242
{
243 244 245 246 247 248 249 250 251 252 253
  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()));
254
    }
255 256 257 258 259 260 261 262 263 264 265 266 267
  }
}

bool cmQtAutoGen::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);
268
    }
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
  };

  // 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 = "rcc lists unparsable output:\n";
          error += cmQtAutoGen::Quoted(eline);
          error += "\n";
          return false;
        }
        pos += searchString.length();
        std::string::size_type sz = eline.size() - pos - 1;
        files.push_back(eline.substr(pos, sz));
      }
    }
  }

  return true;
}

void cmQtAutoGen::RccListConvertFullPath(std::string const& qrcFileDir,
                                         std::vector<std::string>& files)
{
  for (std::string& entry : files) {
312
    std::string tmp = cmSystemTools::CollapseFullPath(entry, qrcFileDir);
313
    entry = std::move(tmp);
314 315
  }
}