4#include <profileevaluator.h>
5#include <profileutils.h>
6#include <qmakeparser.h>
10#include <QtCore/QCoreApplication>
11#include <QtCore/QDebug>
13#include <QtCore/QDirIterator>
14#include <QtCore/QFile>
15#include <QtCore/QFileInfo>
16#include <QtCore/QLibraryInfo>
17#include <QtCore/QRegularExpression>
18#include <QtCore/QString>
19#include <QtCore/QStringList>
21#include <QtCore/QJsonArray>
22#include <QtCore/QJsonDocument>
23#include <QtCore/QJsonObject>
51 return QJsonArray::fromStringList(lst);
55void setValue(QJsonObject &obj,
const char *key, T value)
57 obj[QLatin1String(key)] = toJsonValue(value);
63 lprodump [options] project-file...
64lprodump is part of Qt's Linguist tool chain. It extracts information
65from qmake projects to a .json file. This file can be passed to
66lupdate/lrelease using the -project option.
67
68Options:
69 -help Display this information and exit.
70 -silent
71 Do not explain what is being done.
72 -pro <filename>
73 Name of a .pro file. Useful for files with .pro file syntax but
74 different file suffix. Projects are recursed into and merged.
75 -pro-out <directory>
76 Virtual output directory for processing subsequent .pro files.
77 -pro-debug
78 Trace processing .pro files. Specify twice for more verbosity.
79 -out <filename>
80 Name of the output file.
81 -translations-variables <variable_1>[,<variable_2>,...]
82 Comma-separated list of QMake variables containing .ts files.
83 -version
84 Display the version of lprodump and exit.
85)"_s);
88static void print(
const QString &fileName,
int lineNo,
const QString &msg)
91 printErr(QString::fromLatin1(
"WARNING: %1:%2: %3\n").arg(fileName, QString::number(lineNo), msg));
93 printErr(QString::fromLatin1(
"WARNING: %1: %2\n").arg(fileName, msg));
95 printErr(QString::fromLatin1(
"WARNING: %1\n").arg(msg));
100 void message(
int type,
const QString &msg,
const QString &fileName,
int lineNo)
override
110 printErr(QLatin1String(
"WARNING: ") + msg + QLatin1Char(
'\n'));
126 return QStringList();
131 printErr(QStringLiteral(
"lprodump error: Cannot read %1: %2\n").arg(resourceFile, errStr));
132 return QStringList();
134 const ReadQrcResult rqr = readQrcFile(resourceFile, content);
136 printErr(QStringLiteral(
"lprodump error: %1:%2: %3\n")
137 .arg(resourceFile, QString::number(rqr.line), rqr.errorString));
145 QStringList vPaths = visitor.absolutePathValues(QLatin1String(vvar), projectDir);
146 vPaths += baseVPaths;
147 vPaths.removeDuplicates();
148 return visitor.absoluteFileValues(QLatin1String(var), projectDir, vPaths, 0);
154 QStringList baseVPaths;
155 baseVPaths += visitor.absolutePathValues(QLatin1String(
"VPATH"), projectDir);
156 baseVPaths << projectDir;
157 baseVPaths.removeDuplicates();
159 QStringList sourceFiles;
162 sourceFiles += getSources(
"SOURCES",
"VPATH_SOURCES", baseVPaths, projectDir, visitor);
163 sourceFiles += getSources(
"HEADERS",
"VPATH_HEADERS", baseVPaths, projectDir, visitor);
165 sourceFiles += getSources(
"FORMS",
"VPATH_FORMS", baseVPaths, projectDir, visitor);
167 const QStringList resourceFiles = getSources(
"RESOURCES",
"VPATH_RESOURCES", baseVPaths, projectDir, visitor);
168 for (
const QString &resource : resourceFiles)
169 sourceFiles += getResources(resource, vfs);
171 QStringList installs = visitor.values(QLatin1String(
"INSTALLS"))
172 + visitor.values(QLatin1String(
"DEPLOYMENT"));
173 installs.removeDuplicates();
174 QDir baseDir(projectDir);
175 for (
const QString &inst : std::as_const(installs)) {
176 for (
const QString &file : visitor.values(inst + QLatin1String(
".files"))) {
177 QFileInfo info(file);
178 if (!info.isAbsolute())
179 info.setFile(baseDir.absoluteFilePath(file));
180 QStringList nameFilter;
183 nameFilter << QLatin1String(
"*");
184 searchPath = info.filePath();
186 nameFilter << info.fileName();
187 searchPath = info.path();
190 QDirIterator iterator(searchPath, nameFilter,
191 QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks,
192 QDirIterator::Subdirectories);
193 while (iterator.hasNext()) {
195 QFileInfo cfi = iterator.fileInfo();
196 if (isSupportedExtension(cfi.suffix()))
197 sourceFiles << cfi.filePath();
202 sourceFiles.removeDuplicates();
209 const QStringList trExcludes = visitor.values(QLatin1String(
"TR_EXCLUDE"));
210 QStringList excludes;
211 excludes.reserve(trExcludes.size());
212 const QDir projectDir(projectDirPath);
213 for (
const QString &ex : trExcludes)
214 excludes << QDir::cleanPath(projectDir.absoluteFilePath(ex));
220 for (
const QString &ex : visitor.values(QLatin1String(
"TR_EXCLUDE"))) {
221 QRegularExpression rx(QRegularExpression::wildcardToRegularExpression(ex));
222 for (
auto it = subProjects->begin(); it != subProjects->end(); ) {
223 if (rx.match(*it).hasMatch())
224 it = subProjects->erase(it);
232 const QStringList &translationsVariables,
233 const QHash<QString, QString> &outDirMap,
242 QStringList tmp = visitor.values(QLatin1String(
"CODECFORSRC"));
244 result[QStringLiteral(
"codec")] = tmp.last();
245 QString proPath = QFileInfo(proFile).path();
247 QStringList subProjects = visitor.values(QLatin1String(
"SUBDIRS"));
248 excludeProjects(visitor, &subProjects);
249 QStringList subProFiles;
250 QDir proDir(proPath);
251 for (
const QString &subdir : std::as_const(subProjects)) {
252 QString realdir = visitor.value(subdir + QLatin1String(
".subdir"));
253 if (realdir.isEmpty())
254 realdir = visitor.value(subdir + QLatin1String(
".file"));
255 if (realdir.isEmpty())
257 QString subPro = QDir::cleanPath(proDir.absoluteFilePath(realdir));
258 QFileInfo subInfo(subPro);
259 if (subInfo.isDir()) {
260 subProFiles << (subPro + QLatin1Char(
'/')
261 + subInfo.fileName() + QLatin1String(
".pro"));
263 subProFiles << subPro;
266 QJsonArray subResults = processProjects(
false, subProFiles, translationsVariables,
267 QHash<QString, QString>(), option, vfs, parser,
269 if (!subResults.isEmpty())
270 setValue(result,
"subProjects", subResults);
272 const QStringList sourceFiles = getSources(visitor, proPath, vfs);
273 setValue(result,
"includePaths",
274 visitor.absolutePathValues(QLatin1String(
"INCLUDEPATH"), proPath));
275 setValue(result,
"excluded", getExcludes(visitor, proPath));
276 setValue(result,
"sources", sourceFiles);
282 const QStringList &translationsVariables,
283 const QHash<QString, QString> &outDirMap,
287 for (
const QString &proFile : proFiles) {
288 if (!outDirMap.isEmpty())
289 option->setDirectories(QFileInfo(proFile).path(), outDirMap[proFile]);
292 if (!(pro = parser->parsedProFile(proFile, topLevel ? QMakeParser::ParseReportMissing
293 : QMakeParser::ParseDefault))) {
298 ProFileEvaluator visitor(option, parser, vfs, &evalHandler);
299 visitor.setCumulative(
true);
300 visitor.setOutputDir(option->shadowedPath(pro->directoryName()));
301 if (!visitor.accept(pro)) {
308 QJsonObject prj = processProject(proFile, translationsVariables, option, vfs, parser,
310 setValue(prj,
"projectFile", proFile);
312 for (
const QString &varName : translationsVariables) {
313 if (!visitor.contains(varName))
315 QDir proDir(QFileInfo(proFile).path());
316 const QStringList translations = visitor.values(varName);
317 for (
const QString &tsFile : translations)
318 tsFiles << proDir.filePath(tsFile);
320 if (!tsFiles.isEmpty())
321 setValue(prj,
"translations", tsFiles);
322 if (visitor.contains(QLatin1String(
"LUPDATE_COMPILE_COMMANDS_PATH"))) {
323 const QStringList thepathjson = visitor.values(
324 QLatin1String(
"LUPDATE_COMPILE_COMMANDS_PATH"));
325 setValue(prj,
"compileCommands", thepathjson.value(0));
335 QCoreApplication app(argc, argv);
336 QStringList args = app.arguments();
337 QStringList proFiles;
338 QStringList translationsVariables = { u"TRANSLATIONS"_s };
339 QString outDir = QDir::currentPath();
340 QHash<QString, QString> outDirMap;
341 QString outputFilePath;
344 for (
int i = 1; i < args.size(); ++i) {
345 QString arg = args.at(i);
346 if (arg == QLatin1String(
"-help")
347 || arg == QLatin1String(
"--help")
348 || arg == QLatin1String(
"-h")) {
351 }
else if (arg == QLatin1String(
"-out")) {
354 printErr(u"The option -out requires a parameter.\n"_s);
357 outputFilePath = args[i];
358 }
else if (arg == QLatin1String(
"-silent")) {
360 }
else if (arg == QLatin1String(
"-pro-debug")) {
362 }
else if (arg == QLatin1String(
"-version")) {
363 printOut(QStringLiteral(
"lprodump version %1\n").arg(QLatin1String(QT_VERSION_STR)));
365 }
else if (arg == QLatin1String(
"-pro")) {
368 printErr(QStringLiteral(
"The -pro option should be followed by a filename of .pro file.\n"));
371 QString file = QDir::cleanPath(QFileInfo(args[i]).absoluteFilePath());
373 outDirMap[file] = outDir;
374 }
else if (arg == QLatin1String(
"-pro-out")) {
377 printErr(QStringLiteral(
"The -pro-out option should be followed by a directory name.\n"));
380 outDir = QDir::cleanPath(QFileInfo(args[i]).absoluteFilePath());
381 }
else if (arg == u"-translations-variables"_s) {
384 printErr(u"The -translations-variables option must be followed by a "_s
385 u"comma-separated list of variable names.\n"_s);
388 translationsVariables = args.at(i).split(QLatin1Char(
','));
389 }
else if (arg.startsWith(QLatin1String(
"-")) && arg != QLatin1String(
"-")) {
390 printErr(QStringLiteral(
"Unrecognized option '%1'.\n").arg(arg));
395 printErr(QStringLiteral(
"lprodump error: File '%1' does not exist.\n").arg(arg));
398 if (!isProOrPriFile(arg)) {
399 printErr(QStringLiteral(
"lprodump error: '%1' is neither a .pro nor a .pri file.\n")
403 QString cleanFile = QDir::cleanPath(fi.absoluteFilePath());
404 proFiles << cleanFile;
405 outDirMap[cleanFile] = outDir;
409 if (proFiles.isEmpty()) {
416 option.qmake_abslocation = QString::fromLocal8Bit(qgetenv(
"QMAKE"));
417 if (option.qmake_abslocation.isEmpty()) {
418 option.qmake_abslocation = QLibraryInfo::path(QLibraryInfo::BinariesPath)
419 + QLatin1String(
"/qmake");
421 option.debugLevel = proDebug;
422 option.initProperties();
423 option.setCommandLineArguments(QDir::currentPath(),
424 QStringList() << QLatin1String(
"CONFIG+=lupdate_run"));
428 QJsonArray results = processProjects(
true, proFiles, translationsVariables, outDirMap, &option,
429 &vfs, &parser, &fail);
433 const QByteArray output = QJsonDocument(results).toJson(QJsonDocument::Compact);
434 if (outputFilePath.isEmpty()) {
435 puts(output.constData());
437 QFile f(outputFilePath);
438 if (!f.open(QIODevice::WriteOnly)) {
439 printErr(QStringLiteral(
"lprodump error: Cannot open %1 for writing.\n").arg(outputFilePath));
int main(int argc, char *argv[])
[2]
void message(int type, const QString &msg, const QString &fileName, int lineNo) override
void fileMessage(int type, const QString &msg) override
void doneWithEval(ProFile *) override
void aboutToEval(ProFile *, ProFile *, EvalFileType) override
ProFileEvaluator::TemplateType templateType() const
ReadResult readFile(int id, QString *contents, QString *errStr)
const QString & asString(const QString &s)
#define qPrintable(string)
static void printOut(const QString &out)
QStringList getExcludes(const ProFileEvaluator &visitor, const QString &projectDirPath)
static QStringList getSources(const ProFileEvaluator &visitor, const QString &projectDir, QMakeVfs *vfs)
static void excludeProjects(const ProFileEvaluator &visitor, QStringList *subProjects)
void setValue(QJsonObject &obj, const char *key, T value)
static void print(const QString &fileName, int lineNo, const QString &msg)
static QJsonValue toJsonValue(const QString &s)
static QJsonObject processProject(const QString &proFile, const QStringList &translationsVariables, ProFileGlobals *option, QMakeVfs *vfs, QMakeParser *parser, ProFileEvaluator &visitor)
static EvalHandler evalHandler
static QJsonValue toJsonValue(const QJsonValue &v)
static QStringList getSources(const char *var, const char *vvar, const QStringList &baseVPaths, const QString &projectDir, const ProFileEvaluator &visitor)
static QJsonArray processProjects(bool topLevel, const QStringList &proFiles, const QStringList &translationsVariables, const QHash< QString, QString > &outDirMap, ProFileGlobals *option, QMakeVfs *vfs, QMakeParser *parser, bool *fail)
static QStringList getResources(const QString &resourceFile, QMakeVfs *vfs)
static void printErr(const QString &out)