[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the tools applications of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
[561] | 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
[2] | 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
[561] | 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
[2] | 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | #include "preprocessor.h"
|
---|
| 43 | #include "moc.h"
|
---|
| 44 | #include "outputrevision.h"
|
---|
| 45 | #include "../../corelib/global/qconfig.cpp"
|
---|
| 46 | #include <QFile>
|
---|
| 47 | #include <QFileInfo>
|
---|
| 48 | #include <QDir>
|
---|
| 49 | #include <stdio.h>
|
---|
| 50 | #include <stdlib.h>
|
---|
| 51 | #include <ctype.h>
|
---|
| 52 |
|
---|
| 53 | QT_BEGIN_NAMESPACE
|
---|
| 54 |
|
---|
| 55 | /*
|
---|
| 56 | This function looks at two file names and returns the name of the
|
---|
| 57 | infile with a path relative to outfile.
|
---|
| 58 |
|
---|
| 59 | Examples:
|
---|
| 60 |
|
---|
| 61 | /tmp/abc, /tmp/bcd -> abc
|
---|
| 62 | xyz/a/bc, xyz/b/ac -> ../a/bc
|
---|
| 63 | /tmp/abc, xyz/klm -> /tmp/abc
|
---|
| 64 | */
|
---|
| 65 |
|
---|
| 66 | static QByteArray combinePath(const char *infile, const char *outfile)
|
---|
| 67 | {
|
---|
| 68 | QFileInfo inFileInfo(QDir::current(), QFile::decodeName(infile));
|
---|
| 69 | QFileInfo outFileInfo(QDir::current(), QFile::decodeName(outfile));
|
---|
| 70 | int numCommonComponents = 0;
|
---|
| 71 |
|
---|
| 72 | QStringList inSplitted = inFileInfo.dir().canonicalPath().split(QLatin1Char('/'));
|
---|
| 73 | QStringList outSplitted = outFileInfo.dir().canonicalPath().split(QLatin1Char('/'));
|
---|
| 74 |
|
---|
| 75 | while (!inSplitted.isEmpty() && !outSplitted.isEmpty() &&
|
---|
| 76 | inSplitted.first() == outSplitted.first()) {
|
---|
| 77 | inSplitted.removeFirst();
|
---|
| 78 | outSplitted.removeFirst();
|
---|
| 79 | numCommonComponents++;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | if (numCommonComponents < 2)
|
---|
| 83 | /*
|
---|
| 84 | The paths don't have the same drive, or they don't have the
|
---|
| 85 | same root directory. Use an absolute path.
|
---|
| 86 | */
|
---|
| 87 | return QFile::encodeName(inFileInfo.absoluteFilePath());
|
---|
| 88 | /*
|
---|
| 89 | The paths have something in common. Use a path relative to
|
---|
| 90 | the output file.
|
---|
| 91 | */
|
---|
| 92 | while (!outSplitted.isEmpty()) {
|
---|
| 93 | outSplitted.removeFirst();
|
---|
| 94 | inSplitted.prepend(QLatin1String(".."));
|
---|
| 95 | }
|
---|
| 96 | inSplitted.append(inFileInfo.fileName());
|
---|
| 97 | return QFile::encodeName(inSplitted.join(QLatin1String("/")));
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 |
|
---|
| 101 | void error(const char *msg = "Invalid argument")
|
---|
| 102 | {
|
---|
| 103 | if (msg)
|
---|
| 104 | fprintf(stderr, "moc: %s\n", msg);
|
---|
| 105 | fprintf(stderr, "Usage: moc [options] <header-file>\n"
|
---|
| 106 | " -o<file> write output to file rather than stdout\n"
|
---|
| 107 | " -I<dir> add dir to the include path for header files\n"
|
---|
| 108 | " -E preprocess only; do not generate meta object code\n"
|
---|
| 109 | " -D<macro>[=<def>] define macro, with optional definition\n"
|
---|
| 110 | " -U<macro> undefine macro\n"
|
---|
| 111 | " -i do not generate an #include statement\n"
|
---|
| 112 | " -p<path> path prefix for included file\n"
|
---|
| 113 | " -f[<file>] force #include, optional file name\n"
|
---|
| 114 | " -nw do not display warnings\n"
|
---|
| 115 | " @<file> read additional options from file\n"
|
---|
| 116 | " -v display version of moc\n");
|
---|
| 117 | exit(1);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 |
|
---|
| 121 | static inline bool hasNext(const Symbols &symbols, int i)
|
---|
| 122 | { return (i < symbols.size()); }
|
---|
| 123 |
|
---|
| 124 | static inline const Symbol &next(const Symbols &symbols, int &i)
|
---|
| 125 | { return symbols.at(i++); }
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 | QByteArray composePreprocessorOutput(const Symbols &symbols) {
|
---|
| 129 | QByteArray output;
|
---|
| 130 | int lineNum = 1;
|
---|
| 131 | Token last = PP_NOTOKEN;
|
---|
| 132 | Token secondlast = last;
|
---|
| 133 | int i = 0;
|
---|
| 134 | while (hasNext(symbols, i)) {
|
---|
| 135 | Symbol sym = next(symbols, i);
|
---|
| 136 | switch (sym.token) {
|
---|
| 137 | case PP_NEWLINE:
|
---|
| 138 | case PP_WHITESPACE:
|
---|
| 139 | if (last != PP_WHITESPACE) {
|
---|
| 140 | secondlast = last;
|
---|
| 141 | last = PP_WHITESPACE;
|
---|
| 142 | output += ' ';
|
---|
| 143 | }
|
---|
| 144 | continue;
|
---|
| 145 | case PP_STRING_LITERAL:
|
---|
| 146 | if (last == PP_STRING_LITERAL)
|
---|
| 147 | output.chop(1);
|
---|
| 148 | else if (secondlast == PP_STRING_LITERAL && last == PP_WHITESPACE)
|
---|
| 149 | output.chop(2);
|
---|
| 150 | else
|
---|
| 151 | break;
|
---|
| 152 | output += sym.lexem().mid(1);
|
---|
| 153 | secondlast = last;
|
---|
| 154 | last = PP_STRING_LITERAL;
|
---|
| 155 | continue;
|
---|
| 156 | case MOC_INCLUDE_BEGIN:
|
---|
| 157 | lineNum = 0;
|
---|
| 158 | continue;
|
---|
| 159 | case MOC_INCLUDE_END:
|
---|
| 160 | lineNum = sym.lineNum;
|
---|
| 161 | continue;
|
---|
| 162 | default:
|
---|
| 163 | break;
|
---|
| 164 | }
|
---|
| 165 | secondlast = last;
|
---|
| 166 | last = sym.token;
|
---|
| 167 |
|
---|
| 168 | const int padding = sym.lineNum - lineNum;
|
---|
| 169 | if (padding > 0) {
|
---|
| 170 | output.resize(output.size() + padding);
|
---|
| 171 | qMemSet(output.data() + output.size() - padding, '\n', padding);
|
---|
| 172 | lineNum = sym.lineNum;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | output += sym.lexem();
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | return output;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 |
|
---|
| 182 | int runMoc(int _argc, char **_argv)
|
---|
| 183 | {
|
---|
| 184 | bool autoInclude = true;
|
---|
| 185 | Preprocessor pp;
|
---|
| 186 | Moc moc;
|
---|
| 187 | pp.macros["Q_MOC_RUN"];
|
---|
| 188 | pp.macros["__cplusplus"];
|
---|
| 189 | QByteArray filename;
|
---|
| 190 | QByteArray output;
|
---|
| 191 | FILE *in = 0;
|
---|
| 192 | FILE *out = 0;
|
---|
| 193 | bool ignoreConflictingOptions = false;
|
---|
| 194 |
|
---|
| 195 | QVector<QByteArray> argv;
|
---|
| 196 | argv.resize(_argc - 1);
|
---|
| 197 | for (int n = 1; n < _argc; ++n)
|
---|
| 198 | argv[n - 1] = _argv[n];
|
---|
| 199 |
|
---|
[93] | 200 | for (int n = 0; n < argv.count();) {
|
---|
[2] | 201 | if (argv.at(n).startsWith('@')) {
|
---|
| 202 | QByteArray optionsFile = argv.at(n);
|
---|
| 203 | optionsFile.remove(0, 1);
|
---|
| 204 | if (optionsFile.isEmpty())
|
---|
| 205 | error("The @ option requires an input file");
|
---|
| 206 | QFile f(QString::fromLatin1(optionsFile.constData()));
|
---|
| 207 | if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
|
---|
| 208 | error("Cannot open options file specified with @");
|
---|
| 209 | argv.remove(n);
|
---|
| 210 | while (!f.atEnd()) {
|
---|
| 211 | QByteArray line = f.readLine().trimmed();
|
---|
| 212 | if (!line.isEmpty())
|
---|
| 213 | argv.insert(n++, line);
|
---|
| 214 | }
|
---|
[93] | 215 | // n points to the option past the last inserted one
|
---|
| 216 | } else {
|
---|
| 217 | ++n;
|
---|
[2] | 218 | }
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[93] | 221 | int argc = argv.count();
|
---|
[2] | 222 |
|
---|
| 223 | for (int n = 0; n < argc; ++n) {
|
---|
| 224 | QByteArray arg(argv[n]);
|
---|
| 225 | if (arg[0] != '-') {
|
---|
| 226 | if (filename.isEmpty()) {
|
---|
| 227 | filename = arg;
|
---|
| 228 | continue;
|
---|
| 229 | }
|
---|
| 230 | error("Too many input files specified");
|
---|
| 231 | }
|
---|
| 232 | QByteArray opt = arg.mid(1);
|
---|
| 233 | bool more = (opt.size() > 1);
|
---|
| 234 | switch (opt[0]) {
|
---|
| 235 | case 'o': // output redirection
|
---|
| 236 | if (!more) {
|
---|
| 237 | if (!(n < argc-1))
|
---|
| 238 | error("Missing output file name");
|
---|
| 239 | output = argv[++n];
|
---|
| 240 | } else
|
---|
| 241 | output = opt.mid(1);
|
---|
| 242 | break;
|
---|
| 243 | case 'E': // only preprocessor
|
---|
| 244 | pp.preprocessOnly = true;
|
---|
| 245 | break;
|
---|
| 246 | case 'i': // no #include statement
|
---|
| 247 | if (more)
|
---|
| 248 | error();
|
---|
| 249 | moc.noInclude = true;
|
---|
| 250 | autoInclude = false;
|
---|
| 251 | break;
|
---|
| 252 | case 'f': // produce #include statement
|
---|
| 253 | if (ignoreConflictingOptions)
|
---|
| 254 | break;
|
---|
| 255 | moc.noInclude = false;
|
---|
| 256 | autoInclude = false;
|
---|
| 257 | if (opt[1]) // -fsomething.h
|
---|
| 258 | moc.includeFiles.append(opt.mid(1));
|
---|
| 259 | break;
|
---|
| 260 | case 'p': // include file path
|
---|
| 261 | if (ignoreConflictingOptions)
|
---|
| 262 | break;
|
---|
| 263 | if (!more) {
|
---|
| 264 | if (!(n < argc-1))
|
---|
| 265 | error("Missing path name for the -p option.");
|
---|
| 266 | moc.includePath = argv[++n];
|
---|
| 267 | } else {
|
---|
| 268 | moc.includePath = opt.mid(1);
|
---|
| 269 | }
|
---|
| 270 | break;
|
---|
| 271 | case 'I': // produce #include statement
|
---|
| 272 | if (!more) {
|
---|
| 273 | if (!(n < argc-1))
|
---|
| 274 | error("Missing path name for the -I option.");
|
---|
| 275 | pp.includes += Preprocessor::IncludePath(argv[++n]);
|
---|
| 276 | } else {
|
---|
| 277 | pp.includes += Preprocessor::IncludePath(opt.mid(1));
|
---|
| 278 | }
|
---|
| 279 | break;
|
---|
| 280 | case 'F': // minimalistic framework support for the mac
|
---|
| 281 | if (!more) {
|
---|
| 282 | if (!(n < argc-1))
|
---|
| 283 | error("Missing path name for the -F option.");
|
---|
| 284 | Preprocessor::IncludePath p(argv[++n]);
|
---|
| 285 | p.isFrameworkPath = true;
|
---|
| 286 | pp.includes += p;
|
---|
| 287 | } else {
|
---|
| 288 | Preprocessor::IncludePath p(opt.mid(1));
|
---|
| 289 | p.isFrameworkPath = true;
|
---|
| 290 | pp.includes += p;
|
---|
| 291 | }
|
---|
| 292 | break;
|
---|
| 293 | case 'D': // define macro
|
---|
| 294 | {
|
---|
| 295 | QByteArray name;
|
---|
| 296 | QByteArray value("1");
|
---|
| 297 | if (!more) {
|
---|
| 298 | if (n < argc-1)
|
---|
| 299 | name = argv[++n];
|
---|
| 300 | } else
|
---|
| 301 | name = opt.mid(1);
|
---|
| 302 | int eq = name.indexOf('=');
|
---|
| 303 | if (eq >= 0) {
|
---|
| 304 | value = name.mid(eq + 1);
|
---|
| 305 | name = name.left(eq);
|
---|
| 306 | }
|
---|
| 307 | if (name.isEmpty())
|
---|
| 308 | error("Missing macro name");
|
---|
| 309 | Macro macro;
|
---|
| 310 | macro.symbols += Symbol(0, PP_IDENTIFIER, value);
|
---|
| 311 | pp.macros.insert(name, macro);
|
---|
| 312 |
|
---|
| 313 | }
|
---|
| 314 | break;
|
---|
| 315 | case 'U':
|
---|
| 316 | {
|
---|
| 317 | QByteArray macro;
|
---|
| 318 | if (!more) {
|
---|
| 319 | if (n < argc-1)
|
---|
| 320 | macro = argv[++n];
|
---|
| 321 | } else
|
---|
| 322 | macro = opt.mid(1);
|
---|
| 323 | if (macro.isEmpty())
|
---|
| 324 | error("Missing macro name");
|
---|
| 325 | pp.macros.remove(macro);
|
---|
| 326 |
|
---|
| 327 | }
|
---|
| 328 | break;
|
---|
| 329 | case 'v': // version number
|
---|
| 330 | if (more && opt != "version")
|
---|
| 331 | error();
|
---|
| 332 | fprintf(stderr, "Qt Meta Object Compiler version %d (Qt %s)\n",
|
---|
| 333 | mocOutputRevision, QT_VERSION_STR);
|
---|
| 334 | return 1;
|
---|
| 335 | case 'n': // don't display warnings
|
---|
| 336 | if (ignoreConflictingOptions)
|
---|
| 337 | break;
|
---|
| 338 | if (opt != "nw")
|
---|
| 339 | error();
|
---|
| 340 | moc.displayWarnings = false;
|
---|
| 341 | break;
|
---|
| 342 | case 'h': // help
|
---|
| 343 | if (more && opt != "help")
|
---|
| 344 | error();
|
---|
| 345 | else
|
---|
| 346 | error(0); // 0 means usage only
|
---|
| 347 | break;
|
---|
| 348 | case '-':
|
---|
| 349 | if (more && arg == "--ignore-option-clashes") {
|
---|
| 350 | // -- ignore all following moc specific options that conflict
|
---|
| 351 | // with for example gcc, like -pthread conflicting with moc's
|
---|
| 352 | // -p option.
|
---|
| 353 | ignoreConflictingOptions = true;
|
---|
| 354 | break;
|
---|
| 355 | }
|
---|
| 356 | // fall through
|
---|
| 357 | default:
|
---|
| 358 | error();
|
---|
| 359 | }
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 |
|
---|
| 363 | if (autoInclude) {
|
---|
| 364 | int ppos = filename.lastIndexOf('.');
|
---|
| 365 | moc.noInclude = (ppos >= 0
|
---|
| 366 | && tolower(filename[ppos + 1]) != 'h'
|
---|
| 367 | && tolower(filename[ppos + 1]) != QDir::separator().toLatin1()
|
---|
| 368 | );
|
---|
| 369 | }
|
---|
| 370 | if (moc.includeFiles.isEmpty()) {
|
---|
| 371 | if (moc.includePath.isEmpty()) {
|
---|
| 372 | if (filename.size()) {
|
---|
| 373 | if (output.size())
|
---|
| 374 | moc.includeFiles.append(combinePath(filename, output));
|
---|
| 375 | else
|
---|
| 376 | moc.includeFiles.append(filename);
|
---|
| 377 | }
|
---|
| 378 | } else {
|
---|
| 379 | moc.includeFiles.append(combinePath(filename, filename));
|
---|
| 380 | }
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | if (filename.isEmpty()) {
|
---|
| 384 | filename = "standard input";
|
---|
| 385 | in = stdin;
|
---|
| 386 | } else {
|
---|
| 387 | #if defined(_MSC_VER) && _MSC_VER >= 1400
|
---|
| 388 | if (fopen_s(&in, filename.data(), "rb")) {
|
---|
| 389 | #else
|
---|
| 390 | in = fopen(filename.data(), "rb");
|
---|
| 391 | if (!in) {
|
---|
| 392 | #endif
|
---|
| 393 | fprintf(stderr, "moc: %s: No such file\n", (const char*)filename);
|
---|
| 394 | return 1;
|
---|
| 395 | }
|
---|
| 396 | moc.filename = filename;
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | moc.currentFilenames.push(filename);
|
---|
| 400 |
|
---|
| 401 | // 1. preprocess
|
---|
| 402 | moc.symbols = pp.preprocessed(moc.filename, in);
|
---|
| 403 | fclose(in);
|
---|
| 404 |
|
---|
| 405 | if (!pp.preprocessOnly) {
|
---|
| 406 | // 2. parse
|
---|
| 407 | moc.parse();
|
---|
| 408 | }
|
---|
| 409 |
|
---|
| 410 | // 3. and output meta object code
|
---|
| 411 |
|
---|
| 412 | if (output.size()) { // output file specified
|
---|
| 413 | #if defined(_MSC_VER) && _MSC_VER >= 1400
|
---|
| 414 | if (fopen_s(&out, output.data(), "w"))
|
---|
| 415 | #else
|
---|
| 416 | out = fopen(output.data(), "w"); // create output file
|
---|
| 417 | if (!out)
|
---|
| 418 | #endif
|
---|
| 419 | {
|
---|
| 420 | fprintf(stderr, "moc: Cannot create %s\n", (const char*)output);
|
---|
| 421 | return 1;
|
---|
| 422 | }
|
---|
| 423 | } else { // use stdout
|
---|
| 424 | out = stdout;
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 | if (pp.preprocessOnly) {
|
---|
| 428 | fprintf(out, "%s\n", composePreprocessorOutput(moc.symbols).constData());
|
---|
| 429 | } else {
|
---|
| 430 | if (moc.classList.isEmpty())
|
---|
| 431 | moc.warning("No relevant classes found. No output generated.");
|
---|
| 432 | else
|
---|
| 433 | moc.generate(out);
|
---|
| 434 | }
|
---|
| 435 |
|
---|
| 436 | if (output.size())
|
---|
| 437 | fclose(out);
|
---|
| 438 |
|
---|
| 439 | return 0;
|
---|
| 440 | }
|
---|
| 441 |
|
---|
| 442 | QT_END_NAMESPACE
|
---|
| 443 |
|
---|
| 444 | int main(int _argc, char **_argv)
|
---|
| 445 | {
|
---|
| 446 | return QT_PREPEND_NAMESPACE(runMoc)(_argc, _argv);
|
---|
| 447 | }
|
---|