1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "profileparser.h"
35 #include "profileevaluator.h"
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>
47 static void print(const QString &fileName, int lineNo, const QString &msg)
50 qWarning("%s(%d): %s", qPrintable(fileName), lineNo, qPrintable(msg));
52 qWarning("%s", qPrintable(msg));
55 class ParseHandler : public ProFileParserHandler {
57 virtual void parseError(const QString &fileName, int lineNo, const QString &msg)
58 { print(fileName, lineNo, msg); }
61 class EvalHandler : public ProFileEvaluatorHandler {
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)); }
70 virtual void aboutToEval(ProFile *, ProFile *, EvalFileType) {}
71 virtual void doneWithEval(ProFile *) {}
74 static ParseHandler parseHandler;
75 static EvalHandler evalHandler;
77 static QString value(ProFileEvaluator &reader, const QString &variable)
79 QStringList vals = reader.values(variable);
86 static int evaluate(const QString &fileName, const QString &in_pwd, const QString &out_pwd,
87 bool cumulative, ProFileOption *option, ProFileParser *parser, int level)
89 static QSet<QString> visited;
90 if (visited.contains(fileName))
92 visited.insert(fileName);
94 ProFileEvaluator visitor(option, parser, &evalHandler);
95 visitor.setCumulative(cumulative);
96 visitor.setOutputDir(out_pwd);
99 if (!(pro = parser->parsedProFile(fileName)))
101 if (!visitor.accept(pro)) {
106 if (visitor.templateType() == ProFileEvaluator::TT_Subdirs) {
107 QStringList subdirs = visitor.values(QLatin1String("SUBDIRS"));
108 subdirs.removeDuplicates();
109 foreach (const QString &subDirVar, subdirs) {
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();
119 QFileInfo info(realDir);
120 if (!info.isAbsolute())
121 info.setFile(in_pwd + QLatin1Char('/') + realDir);
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();
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())));
135 printf("%sReading %s%s\n", QByteArray().fill(' ', nlevel).constData(),
136 qPrintable(inFile), (inPwd == outPwd) ? "" :
137 qPrintable(QString(QLatin1String(" [") + outPwd + QLatin1Char(']'))));
141 evaluate(inFile, inPwd, outPwd, cumulative, option, parser, nlevel);
149 int main(int argc, char **argv)
151 QCoreApplication app(argc, argv);
153 QStringList args = app.arguments();
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>]]");
161 ProFileOption option;
162 if (args.count() >= 4)
163 option.setCommandLineArguments(args.mid(3));
164 ProFileParser parser(0, &parseHandler);
166 static const struct {
167 const char * const name;
168 QLibraryInfo::LibraryLocation index;
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 }
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));
187 option.properties.insert(QLatin1String("QT_VERSION"), QLatin1String(qVersion()));
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;
195 return evaluate(file, in_pwd, out_pwd, cumulative, &option, &parser, level);