OSDN Git Service

6f98cc8c83d8d3ee66f0c68184cbb566f0dcc4da
[qt-creator-jp/qt-creator-jp.git] / tests / manual / proparser / main.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "profileparser.h"
35 #include "profileevaluator.h"
36
37 #include <QtCore/QCoreApplication>
38 #include <QtCore/QDebug>
39 #include <QtCore/QDir>
40 #include <QtCore/QFile>
41 #include <QtCore/QFileInfo>
42 #include <QtCore/QLibraryInfo>
43 #include <QtCore/QString>
44 #include <QtCore/QStringList>
45 #include <QtCore/QTextCodec>
46
47 static void print(const QString &fileName, int lineNo, const QString &msg)
48 {
49     if (lineNo)
50         qWarning("%s(%d): %s", qPrintable(fileName), lineNo, qPrintable(msg));
51     else
52         qWarning("%s", qPrintable(msg));
53 }
54
55 class ParseHandler : public ProFileParserHandler {
56 public:
57     virtual void parseError(const QString &fileName, int lineNo, const QString &msg)
58         { print(fileName, lineNo, msg); }
59 };
60
61 class EvalHandler : public ProFileEvaluatorHandler {
62 public:
63     virtual void configError(const QString &msg)
64         { qWarning("%s", qPrintable(msg)); }
65     virtual void evalError(const QString &fileName, int lineNo, const QString &msg)
66         { print(fileName, lineNo, msg); }
67     virtual void fileMessage(const QString &msg)
68         { qWarning("%s", qPrintable(msg)); }
69
70     virtual void aboutToEval(ProFile *, ProFile *, EvalFileType) {}
71     virtual void doneWithEval(ProFile *) {}
72 };
73
74 static ParseHandler parseHandler;
75 static EvalHandler evalHandler;
76
77 static QString value(ProFileEvaluator &reader, const QString &variable)
78 {
79     QStringList vals = reader.values(variable);
80     if (!vals.isEmpty())
81         return vals.first();
82
83     return QString();
84 }
85
86 static int evaluate(const QString &fileName, const QString &in_pwd, const QString &out_pwd,
87                     bool cumulative, ProFileOption *option, ProFileParser *parser, int level)
88 {
89     static QSet<QString> visited;
90     if (visited.contains(fileName))
91         return 0;
92     visited.insert(fileName);
93
94     ProFileEvaluator visitor(option, parser, &evalHandler);
95     visitor.setCumulative(cumulative);
96     visitor.setOutputDir(out_pwd);
97
98     ProFile *pro;
99     if (!(pro = parser->parsedProFile(fileName)))
100         return 2;
101     if (!visitor.accept(pro)) {
102         pro->deref();
103         return 2;
104     }
105
106     if (visitor.templateType() == ProFileEvaluator::TT_Subdirs) {
107         QStringList subdirs = visitor.values(QLatin1String("SUBDIRS"));
108         subdirs.removeDuplicates();
109         foreach (const QString &subDirVar, subdirs) {
110             QString realDir;
111             const QString subDirKey = subDirVar + QLatin1String(".subdir");
112             const QString subDirFileKey = subDirVar + QLatin1String(".file");
113             if (visitor.contains(subDirKey))
114                 realDir = QFileInfo(value(visitor, subDirKey)).filePath();
115             else if (visitor.contains(subDirFileKey))
116                 realDir = QFileInfo(value(visitor, subDirFileKey)).filePath();
117             else
118                 realDir = subDirVar;
119             QFileInfo info(realDir);
120             if (!info.isAbsolute())
121                 info.setFile(in_pwd + QLatin1Char('/') + realDir);
122             if (info.isDir())
123                 info.setFile(QString::fromLatin1("%1/%2.pro").arg(info.filePath(), info.fileName()));
124             if (!info.exists()) {
125                 qDebug() << "Could not find sub dir" << info.filePath();
126                 continue;
127             }
128
129             QString inFile = QDir::cleanPath(info.absoluteFilePath()),
130                     inPwd = QDir::cleanPath(info.path()),
131                     outPwd = QDir::cleanPath(QDir(out_pwd).absoluteFilePath(
132                             QDir(in_pwd).relativeFilePath(info.path())));
133             int nlevel = level;
134             if (nlevel >= 0) {
135                 printf("%sReading %s%s\n", QByteArray().fill(' ', nlevel).constData(),
136                        qPrintable(inFile), (inPwd == outPwd) ? "" :
137                        qPrintable(QString(QLatin1String(" [") + outPwd + QLatin1Char(']'))));
138                 fflush(stdout);
139                 nlevel++;
140             }
141             evaluate(inFile, inPwd, outPwd, cumulative, option, parser, nlevel);
142         }
143     }
144
145     pro->deref();
146     return 0;
147 }
148
149 int main(int argc, char **argv)
150 {
151     QCoreApplication app(argc, argv);
152
153     QStringList args = app.arguments();
154     args.removeFirst();
155     int level = -1; // verbose
156     if (args.count() && args.first() == QLatin1String("-v"))
157         level = 0, args.removeFirst();
158     if (args.count() < 2)
159         qFatal("need at least two arguments: [-v] <cumulative?> <filenme> [<out_pwd> [<qmake options>]]");
160
161     ProFileOption option;
162     if (args.count() >= 4)
163         option.setCommandLineArguments(args.mid(3));
164     ProFileParser parser(0, &parseHandler);
165
166     static const struct {
167         const char * const name;
168         QLibraryInfo::LibraryLocation index;
169     } props[] = {
170         { "QT_INSTALL_DATA", QLibraryInfo::DataPath },
171         { "QT_INSTALL_LIBS", QLibraryInfo::LibrariesPath },
172         { "QT_INSTALL_IMPORTS", QLibraryInfo::ImportsPath },
173         { "QT_INSTALL_HEADERS", QLibraryInfo::HeadersPath },
174         { "QT_INSTALL_DEMOS", QLibraryInfo::DemosPath },
175         { "QT_INSTALL_EXAMPLES", QLibraryInfo::ExamplesPath },
176         { "QT_INSTALL_CONFIGURATION", QLibraryInfo::SettingsPath },
177         { "QT_INSTALL_TRANSLATIONS", QLibraryInfo::TranslationsPath },
178         { "QT_INSTALL_PLUGINS", QLibraryInfo::PluginsPath },
179         { "QT_INSTALL_BINS", QLibraryInfo::BinariesPath },
180         { "QT_INSTALL_DOCS", QLibraryInfo::DocumentationPath },
181         { "QT_INSTALL_PREFIX", QLibraryInfo::PrefixPath }
182     };
183     for (unsigned i = 0; i < sizeof(props)/sizeof(props[0]); ++i)
184         option.properties.insert(QLatin1String(props[i].name),
185                                  QLibraryInfo::location(props[i].index));
186
187     option.properties.insert(QLatin1String("QT_VERSION"), QLatin1String(qVersion()));
188
189     bool cumulative = args[0] == QLatin1String("true");
190     QFileInfo infi(args[1]);
191     QString file = infi.absoluteFilePath();
192     QString in_pwd = infi.absolutePath();
193     QString out_pwd = (args.count() > 2) ? QFileInfo(args[2]).absoluteFilePath() : in_pwd;
194
195     return evaluate(file, in_pwd, out_pwd, cumulative, &option, &parser, level);
196 }