1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the tools applications of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "preprocessor.h"
44 #include "outputrevision.h"
45 #include "../../corelib/global/qconfig.cpp"
56 This function looks at two file names and returns the name of the
57 infile with a path relative to outfile.
61 /tmp/abc, /tmp/bcd -> abc
62 xyz/a/bc, xyz/b/ac -> ../a/bc
63 /tmp/abc, xyz/klm -> /tmp/abc
66 static QByteArray combinePath(const QByteArray &infile, const QByteArray &outfile)
68 QFileInfo inFileInfo(QDir::current(), QFile::decodeName(infile));
69 QFileInfo outFileInfo(QDir::current(), QFile::decodeName(outfile));
70 return QFile::encodeName(outFileInfo.dir().relativeFilePath(inFileInfo.filePath()));
74 void error(const char *msg = "Invalid argument")
77 fprintf(stderr, "moc: %s\n", msg);
78 fprintf(stderr, "Usage: moc [options] <header-file>\n"
79 " -o<file> write output to file rather than stdout\n"
80 " -I<dir> add dir to the include path for header files\n"
81 " -E preprocess only; do not generate meta object code\n"
82 " -D<macro>[=<def>] define macro, with optional definition\n"
83 " -U<macro> undefine macro\n"
84 " -i do not generate an #include statement\n"
85 " -p<path> path prefix for included file\n"
86 " -f[<file>] force #include, optional file name (overwrite default)\n"
87 " -b<file> prepend #include <file> (preserve default include)\n"
88 " -nn do not display notes\n"
89 " -nw do not display warnings\n"
90 " @<file> read additional options from file\n"
91 " -v display version of moc\n");
96 static inline bool hasNext(const Symbols &symbols, int i)
97 { return (i < symbols.size()); }
99 static inline const Symbol &next(const Symbols &symbols, int &i)
100 { return symbols.at(i++); }
103 QByteArray composePreprocessorOutput(const Symbols &symbols) {
106 Token last = PP_NOTOKEN;
107 Token secondlast = last;
109 while (hasNext(symbols, i)) {
110 Symbol sym = next(symbols, i);
114 if (last != PP_WHITESPACE) {
116 last = PP_WHITESPACE;
120 case PP_STRING_LITERAL:
121 if (last == PP_STRING_LITERAL)
123 else if (secondlast == PP_STRING_LITERAL && last == PP_WHITESPACE)
127 output += sym.lexem().mid(1);
129 last = PP_STRING_LITERAL;
131 case MOC_INCLUDE_BEGIN:
134 case MOC_INCLUDE_END:
135 lineNum = sym.lineNum;
143 const int padding = sym.lineNum - lineNum;
145 output.resize(output.size() + padding);
146 qMemSet(output.data() + output.size() - padding, '\n', padding);
147 lineNum = sym.lineNum;
150 output += sym.lexem();
157 int runMoc(int _argc, char **_argv)
159 bool autoInclude = true;
160 bool defaultInclude = true;
163 pp.macros["Q_MOC_RUN"];
164 pp.macros["__cplusplus"];
169 bool ignoreConflictingOptions = false;
171 QVector<QByteArray> argv;
172 argv.resize(_argc - 1);
173 for (int n = 1; n < _argc; ++n)
174 argv[n - 1] = _argv[n];
175 int argc = argv.count();
177 for (int n = 0; n < argv.count(); ++n) {
178 if (argv.at(n).startsWith('@')) {
179 QByteArray optionsFile = argv.at(n);
180 optionsFile.remove(0, 1);
181 if (optionsFile.isEmpty())
182 error("The @ option requires an input file");
183 QFile f(QString::fromLatin1(optionsFile.constData()));
184 if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
185 error("Cannot open options file specified with @");
188 QByteArray line = f.readLine().trimmed();
190 argv.insert(n++, line);
197 for (int n = 0; n < argc; ++n) {
198 QByteArray arg(argv[n]);
200 if (filename.isEmpty()) {
204 error("Too many input files specified");
206 QByteArray opt = arg.mid(1);
207 bool more = (opt.size() > 1);
209 case 'o': // output redirection
212 error("Missing output file name");
217 case 'E': // only preprocessor
218 pp.preprocessOnly = true;
220 case 'i': // no #include statement
223 moc.noInclude = true;
226 case 'f': // produce #include statement
227 if (ignoreConflictingOptions)
229 moc.noInclude = false;
231 if (opt[1]) { // -fsomething.h
232 moc.includeFiles.append(opt.mid(1));
233 defaultInclude = false;
237 if (ignoreConflictingOptions)
241 error("Missing file name for the -b option.");
242 moc.includeFiles.prepend(argv[++n]);
244 moc.includeFiles.prepend(opt.mid(1));
247 case 'p': // include file path
248 if (ignoreConflictingOptions)
252 error("Missing path name for the -p option.");
253 moc.includePath = argv[++n];
255 moc.includePath = opt.mid(1);
258 case 'I': // produce #include statement
261 error("Missing path name for the -I option.");
262 pp.includes += Preprocessor::IncludePath(argv[++n]);
264 pp.includes += Preprocessor::IncludePath(opt.mid(1));
267 case 'F': // minimalistic framework support for the mac
270 error("Missing path name for the -F option.");
271 Preprocessor::IncludePath p(argv[++n]);
272 p.isFrameworkPath = true;
275 Preprocessor::IncludePath p(opt.mid(1));
276 p.isFrameworkPath = true;
280 case 'D': // define macro
283 QByteArray value("1");
289 int eq = name.indexOf('=');
291 value = name.mid(eq + 1);
292 name = name.left(eq);
295 error("Missing macro name");
297 macro.symbols += Symbol(0, PP_IDENTIFIER, value);
298 pp.macros.insert(name, macro);
311 error("Missing macro name");
312 pp.macros.remove(macro);
316 case 'v': // version number
317 if (more && opt != "version")
319 fprintf(stderr, "Qt Meta Object Compiler version %d (Qt %s)\n",
320 mocOutputRevision, QT_VERSION_STR);
322 case 'n': // don't display warnings
323 if (ignoreConflictingOptions)
326 moc.displayWarnings = moc.displayNotes = false;
327 else if (opt == "nn")
328 moc.displayNotes = false;
333 if (more && opt != "help")
336 error(0); // 0 means usage only
339 if (more && arg == "--ignore-option-clashes") {
340 // -- ignore all following moc specific options that conflict
341 // with for example gcc, like -pthread conflicting with moc's
343 ignoreConflictingOptions = true;
354 int spos = filename.lastIndexOf(QDir::separator().toLatin1());
355 int ppos = filename.lastIndexOf('.');
356 // spos >= -1 && ppos > spos => ppos >= 0
357 moc.noInclude = (ppos > spos && tolower(filename[ppos + 1]) != 'h');
359 if (defaultInclude) {
360 if (moc.includePath.isEmpty()) {
361 if (filename.size()) {
363 moc.includeFiles.append(combinePath(filename, output));
365 moc.includeFiles.append(filename);
368 moc.includeFiles.append(combinePath(filename, filename));
372 if (filename.isEmpty()) {
373 filename = "standard input";
376 #if defined(_MSC_VER) && _MSC_VER >= 1400
377 if (fopen_s(&in, filename.data(), "rb")) {
379 in = fopen(filename.data(), "rb");
382 fprintf(stderr, "moc: %s: No such file\n", filename.constData());
385 moc.filename = filename;
388 moc.currentFilenames.push(filename);
391 moc.symbols = pp.preprocessed(moc.filename, in);
394 if (!pp.preprocessOnly) {
399 // 3. and output meta object code
401 if (output.size()) { // output file specified
402 #if defined(_MSC_VER) && _MSC_VER >= 1400
403 if (fopen_s(&out, output.data(), "w"))
405 out = fopen(output.data(), "w"); // create output file
409 fprintf(stderr, "moc: Cannot create %s\n", output.constData());
412 } else { // use stdout
416 if (pp.preprocessOnly) {
417 fprintf(out, "%s\n", composePreprocessorOutput(moc.symbols).constData());
419 if (moc.classList.isEmpty())
420 moc.note("No relevant classes found. No output generated.");
433 int main(int _argc, char **_argv)
435 return QT_PREPEND_NAMESPACE(runMoc)(_argc, _argv);