0
|
1 |
/****************************************************************************
|
|
2 |
**
|
18
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
0
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the tools applications of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
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 |
**
|
|
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.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
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 |
int argc = argv.count();
|
|
200 |
|
|
201 |
for (int n = 0; n < argv.count(); ++n) {
|
|
202 |
if (argv.at(n).startsWith('@')) {
|
|
203 |
QByteArray optionsFile = argv.at(n);
|
|
204 |
optionsFile.remove(0, 1);
|
|
205 |
if (optionsFile.isEmpty())
|
|
206 |
error("The @ option requires an input file");
|
|
207 |
QFile f(QString::fromLatin1(optionsFile.constData()));
|
|
208 |
if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
|
|
209 |
error("Cannot open options file specified with @");
|
|
210 |
argv.remove(n);
|
|
211 |
while (!f.atEnd()) {
|
|
212 |
QByteArray line = f.readLine().trimmed();
|
|
213 |
if (!line.isEmpty())
|
|
214 |
argv.insert(n++, line);
|
|
215 |
}
|
|
216 |
}
|
|
217 |
}
|
|
218 |
|
|
219 |
argc = argv.count();
|
|
220 |
|
|
221 |
for (int n = 0; n < argc; ++n) {
|
|
222 |
QByteArray arg(argv[n]);
|
|
223 |
if (arg[0] != '-') {
|
|
224 |
if (filename.isEmpty()) {
|
|
225 |
filename = arg;
|
|
226 |
continue;
|
|
227 |
}
|
|
228 |
error("Too many input files specified");
|
|
229 |
}
|
|
230 |
QByteArray opt = arg.mid(1);
|
|
231 |
bool more = (opt.size() > 1);
|
|
232 |
switch (opt[0]) {
|
|
233 |
case 'o': // output redirection
|
|
234 |
if (!more) {
|
|
235 |
if (!(n < argc-1))
|
|
236 |
error("Missing output file name");
|
|
237 |
output = argv[++n];
|
|
238 |
} else
|
|
239 |
output = opt.mid(1);
|
|
240 |
break;
|
|
241 |
case 'E': // only preprocessor
|
|
242 |
pp.preprocessOnly = true;
|
|
243 |
break;
|
|
244 |
case 'i': // no #include statement
|
|
245 |
if (more)
|
|
246 |
error();
|
|
247 |
moc.noInclude = true;
|
|
248 |
autoInclude = false;
|
|
249 |
break;
|
|
250 |
case 'f': // produce #include statement
|
|
251 |
if (ignoreConflictingOptions)
|
|
252 |
break;
|
|
253 |
moc.noInclude = false;
|
|
254 |
autoInclude = false;
|
|
255 |
if (opt[1]) // -fsomething.h
|
|
256 |
moc.includeFiles.append(opt.mid(1));
|
|
257 |
break;
|
|
258 |
case 'p': // include file path
|
|
259 |
if (ignoreConflictingOptions)
|
|
260 |
break;
|
|
261 |
if (!more) {
|
|
262 |
if (!(n < argc-1))
|
|
263 |
error("Missing path name for the -p option.");
|
|
264 |
moc.includePath = argv[++n];
|
|
265 |
} else {
|
|
266 |
moc.includePath = opt.mid(1);
|
|
267 |
}
|
|
268 |
break;
|
|
269 |
case 'I': // produce #include statement
|
|
270 |
if (!more) {
|
|
271 |
if (!(n < argc-1))
|
|
272 |
error("Missing path name for the -I option.");
|
|
273 |
pp.includes += Preprocessor::IncludePath(argv[++n]);
|
|
274 |
} else {
|
|
275 |
pp.includes += Preprocessor::IncludePath(opt.mid(1));
|
|
276 |
}
|
|
277 |
break;
|
|
278 |
case 'F': // minimalistic framework support for the mac
|
|
279 |
if (!more) {
|
|
280 |
if (!(n < argc-1))
|
|
281 |
error("Missing path name for the -F option.");
|
|
282 |
Preprocessor::IncludePath p(argv[++n]);
|
|
283 |
p.isFrameworkPath = true;
|
|
284 |
pp.includes += p;
|
|
285 |
} else {
|
|
286 |
Preprocessor::IncludePath p(opt.mid(1));
|
|
287 |
p.isFrameworkPath = true;
|
|
288 |
pp.includes += p;
|
|
289 |
}
|
|
290 |
break;
|
|
291 |
case 'D': // define macro
|
|
292 |
{
|
|
293 |
QByteArray name;
|
|
294 |
QByteArray value("1");
|
|
295 |
if (!more) {
|
|
296 |
if (n < argc-1)
|
|
297 |
name = argv[++n];
|
|
298 |
} else
|
|
299 |
name = opt.mid(1);
|
|
300 |
int eq = name.indexOf('=');
|
|
301 |
if (eq >= 0) {
|
|
302 |
value = name.mid(eq + 1);
|
|
303 |
name = name.left(eq);
|
|
304 |
}
|
|
305 |
if (name.isEmpty())
|
|
306 |
error("Missing macro name");
|
|
307 |
Macro macro;
|
|
308 |
macro.symbols += Symbol(0, PP_IDENTIFIER, value);
|
|
309 |
pp.macros.insert(name, macro);
|
|
310 |
|
|
311 |
}
|
|
312 |
break;
|
|
313 |
case 'U':
|
|
314 |
{
|
|
315 |
QByteArray macro;
|
|
316 |
if (!more) {
|
|
317 |
if (n < argc-1)
|
|
318 |
macro = argv[++n];
|
|
319 |
} else
|
|
320 |
macro = opt.mid(1);
|
|
321 |
if (macro.isEmpty())
|
|
322 |
error("Missing macro name");
|
|
323 |
pp.macros.remove(macro);
|
|
324 |
|
|
325 |
}
|
|
326 |
break;
|
|
327 |
case 'v': // version number
|
|
328 |
if (more && opt != "version")
|
|
329 |
error();
|
|
330 |
fprintf(stderr, "Qt Meta Object Compiler version %d (Qt %s)\n",
|
|
331 |
mocOutputRevision, QT_VERSION_STR);
|
|
332 |
return 1;
|
|
333 |
case 'n': // don't display warnings
|
|
334 |
if (ignoreConflictingOptions)
|
|
335 |
break;
|
|
336 |
if (opt != "nw")
|
|
337 |
error();
|
|
338 |
moc.displayWarnings = false;
|
|
339 |
break;
|
|
340 |
case 'h': // help
|
|
341 |
if (more && opt != "help")
|
|
342 |
error();
|
|
343 |
else
|
|
344 |
error(0); // 0 means usage only
|
|
345 |
break;
|
|
346 |
case '-':
|
|
347 |
if (more && arg == "--ignore-option-clashes") {
|
|
348 |
// -- ignore all following moc specific options that conflict
|
|
349 |
// with for example gcc, like -pthread conflicting with moc's
|
|
350 |
// -p option.
|
|
351 |
ignoreConflictingOptions = true;
|
|
352 |
break;
|
|
353 |
}
|
|
354 |
// fall through
|
|
355 |
default:
|
|
356 |
error();
|
|
357 |
}
|
|
358 |
}
|
|
359 |
|
|
360 |
|
|
361 |
if (autoInclude) {
|
|
362 |
int ppos = filename.lastIndexOf('.');
|
|
363 |
moc.noInclude = (ppos >= 0
|
|
364 |
&& tolower(filename[ppos + 1]) != 'h'
|
|
365 |
&& tolower(filename[ppos + 1]) != QDir::separator().toLatin1()
|
|
366 |
);
|
|
367 |
}
|
|
368 |
if (moc.includeFiles.isEmpty()) {
|
|
369 |
if (moc.includePath.isEmpty()) {
|
|
370 |
if (filename.size()) {
|
|
371 |
if (output.size())
|
|
372 |
moc.includeFiles.append(combinePath(filename, output));
|
|
373 |
else
|
|
374 |
moc.includeFiles.append(filename);
|
|
375 |
}
|
|
376 |
} else {
|
|
377 |
moc.includeFiles.append(combinePath(filename, filename));
|
|
378 |
}
|
|
379 |
}
|
|
380 |
|
|
381 |
if (filename.isEmpty()) {
|
|
382 |
filename = "standard input";
|
|
383 |
in = stdin;
|
|
384 |
} else {
|
|
385 |
#if defined(_MSC_VER) && _MSC_VER >= 1400
|
|
386 |
if (fopen_s(&in, filename.data(), "rb")) {
|
|
387 |
#else
|
|
388 |
in = fopen(filename.data(), "rb");
|
|
389 |
if (!in) {
|
|
390 |
#endif
|
|
391 |
fprintf(stderr, "moc: %s: No such file\n", (const char*)filename);
|
|
392 |
return 1;
|
|
393 |
}
|
|
394 |
moc.filename = filename;
|
|
395 |
}
|
|
396 |
|
|
397 |
moc.currentFilenames.push(filename);
|
|
398 |
|
|
399 |
// 1. preprocess
|
|
400 |
moc.symbols = pp.preprocessed(moc.filename, in);
|
|
401 |
fclose(in);
|
|
402 |
|
|
403 |
if (!pp.preprocessOnly) {
|
|
404 |
// 2. parse
|
|
405 |
moc.parse();
|
|
406 |
}
|
|
407 |
|
|
408 |
// 3. and output meta object code
|
|
409 |
|
|
410 |
if (output.size()) { // output file specified
|
|
411 |
#if defined(_MSC_VER) && _MSC_VER >= 1400
|
|
412 |
if (fopen_s(&out, output.data(), "w"))
|
|
413 |
#else
|
|
414 |
out = fopen(output.data(), "w"); // create output file
|
|
415 |
if (!out)
|
|
416 |
#endif
|
|
417 |
{
|
|
418 |
fprintf(stderr, "moc: Cannot create %s\n", (const char*)output);
|
|
419 |
return 1;
|
|
420 |
}
|
|
421 |
} else { // use stdout
|
|
422 |
out = stdout;
|
|
423 |
}
|
|
424 |
|
|
425 |
if (pp.preprocessOnly) {
|
|
426 |
fprintf(out, "%s\n", composePreprocessorOutput(moc.symbols).constData());
|
|
427 |
} else {
|
|
428 |
if (moc.classList.isEmpty())
|
|
429 |
moc.warning("No relevant classes found. No output generated.");
|
|
430 |
else
|
|
431 |
moc.generate(out);
|
|
432 |
}
|
|
433 |
|
|
434 |
if (output.size())
|
|
435 |
fclose(out);
|
|
436 |
|
|
437 |
return 0;
|
|
438 |
}
|
|
439 |
|
|
440 |
QT_END_NAMESPACE
|
|
441 |
|
|
442 |
int main(int _argc, char **_argv)
|
|
443 |
{
|
|
444 |
return QT_PREPEND_NAMESPACE(runMoc)(_argc, _argv);
|
|
445 |
}
|