diff options
Diffstat (limited to 'tools/qmlcachegen')
-rw-r--r-- | tools/qmlcachegen/generateloader.cpp | 8 | ||||
-rw-r--r-- | tools/qmlcachegen/qmlcachegen.cpp | 134 | ||||
-rw-r--r-- | tools/qmlcachegen/qmlcachegen.pro | 8 | ||||
-rw-r--r-- | tools/qmlcachegen/resourcefilemapper.cpp | 169 | ||||
-rw-r--r-- | tools/qmlcachegen/resourcefilemapper.h | 50 |
5 files changed, 82 insertions, 287 deletions
diff --git a/tools/qmlcachegen/generateloader.cpp b/tools/qmlcachegen/generateloader.cpp index 4fbbb27afb..1c8a5a016a 100644 --- a/tools/qmlcachegen/generateloader.cpp +++ b/tools/qmlcachegen/generateloader.cpp @@ -75,7 +75,7 @@ QString mangledIdentifier(const QString &str) || (c >= QLatin1Char('a') && c <= QLatin1Char('z')) || (c >= QLatin1Char('A') && c <= QLatin1Char('Z')) || c == QLatin1Char('_')) { - mangled += c; + mangled += QChar(c); } else { mangled += QLatin1String("_0x") + QString::number(c, 16) + QLatin1Char('_'); } @@ -434,7 +434,11 @@ bool generateLoader(const QStringList &compiledFiles, const QStringList &sortedR } } +#if QT_CONFIG(temporaryfile) QSaveFile f(outputFileName); +#else + QFile f(outputFileName); +#endif if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) { *errorString = f.errorString(); return false; @@ -445,10 +449,12 @@ bool generateLoader(const QStringList &compiledFiles, const QStringList &sortedR return false; } +#if QT_CONFIG(temporaryfile) if (!f.commit()) { *errorString = f.errorString(); return false; } +#endif return true; } diff --git a/tools/qmlcachegen/qmlcachegen.cpp b/tools/qmlcachegen/qmlcachegen.cpp index df7468eaef..ac9cf039d3 100644 --- a/tools/qmlcachegen/qmlcachegen.cpp +++ b/tools/qmlcachegen/qmlcachegen.cpp @@ -45,6 +45,8 @@ #include <algorithm> +using namespace QQmlJS; + int filterResourceFile(const QString &input, const QString &output); bool generateLoader(const QStringList &compiledFiles, const QStringList &retainedFiles, const QString &output, const QStringList &resourceFileMappings, @@ -65,6 +67,7 @@ struct Error void print(); Error augment(const QString &contextErrorMessage) const; void appendDiagnostics(const QString &inputFileName, const QList<QQmlJS::DiagnosticMessage> &diagnostics); + void appendDiagnostic(const QString &inputFileName, const DiagnosticMessage &diagnostic); }; void Error::print() @@ -82,9 +85,9 @@ Error Error::augment(const QString &contextErrorMessage) const QString diagnosticErrorMessage(const QString &fileName, const QQmlJS::DiagnosticMessage &m) { QString message; - message = fileName + QLatin1Char(':') + QString::number(m.loc.startLine) + QLatin1Char(':'); - if (m.loc.startColumn > 0) - message += QString::number(m.loc.startColumn) + QLatin1Char(':'); + message = fileName + QLatin1Char(':') + QString::number(m.line) + QLatin1Char(':'); + if (m.column > 0) + message += QString::number(m.column) + QLatin1Char(':'); if (m.isError()) message += QLatin1String(" error: "); @@ -94,13 +97,17 @@ QString diagnosticErrorMessage(const QString &fileName, const QQmlJS::Diagnostic return message; } +void Error::appendDiagnostic(const QString &inputFileName, const DiagnosticMessage &diagnostic) +{ + if (!message.isEmpty()) + message += QLatin1Char('\n'); + message += diagnosticErrorMessage(inputFileName, diagnostic); +} + void Error::appendDiagnostics(const QString &inputFileName, const QList<DiagnosticMessage> &diagnostics) { - for (const QQmlJS::DiagnosticMessage &parseError: diagnostics) { - if (!message.isEmpty()) - message += QLatin1Char('\n'); - message += diagnosticErrorMessage(inputFileName, parseError); - } + for (const QQmlJS::DiagnosticMessage &diagnostic: diagnostics) + appendDiagnostic(inputFileName, diagnostic); } // Ensure that ListElement objects keep all property assignments in their string form @@ -169,7 +176,7 @@ static bool checkArgumentsObjectUseInSignalHandlers(const QmlIR::Document &doc, return true; } -using SaveFunction = std::function<bool (const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &, QString *)>; +using SaveFunction = std::function<bool(const QV4::CompiledData::SaveableUnitPointer &, QString *)>; static bool compileQmlFile(const QString &inputFileName, SaveFunction saveFunction, Error *error) { @@ -200,10 +207,7 @@ static bool compileQmlFile(const QString &inputFileName, SaveFunction saveFuncti annotateListElements(&irDocument); { - QmlIR::JSCodeGen v4CodeGen(irDocument.code, - &irDocument.jsGenerator, &irDocument.jsModule, - &irDocument.jsParserEngine, irDocument.program, - &irDocument.jsGenerator.stringTable, illegalNames); + QmlIR::JSCodeGen v4CodeGen(&irDocument, illegalNames); for (QmlIR::Object *object: qAsConst(irDocument.objects)) { if (object->functionsAndExpressions->count == 0) continue; @@ -211,9 +215,8 @@ static bool compileQmlFile(const QString &inputFileName, SaveFunction saveFuncti for (QmlIR::CompiledFunctionOrExpression *foe = object->functionsAndExpressions->first; foe; foe = foe->next) functionsToCompile << *foe; const QVector<int> runtimeFunctionIndices = v4CodeGen.generateJSCodeForFunctionsAndBindings(functionsToCompile); - QList<QQmlJS::DiagnosticMessage> jsErrors = v4CodeGen.errors(); - if (!jsErrors.isEmpty()) { - error->appendDiagnostics(inputFileName, jsErrors); + if (v4CodeGen.hasError()) { + error->appendDiagnostic(inputFileName, v4CodeGen.error()); return false; } @@ -229,11 +232,13 @@ static bool compileQmlFile(const QString &inputFileName, SaveFunction saveFuncti QmlIR::QmlUnitGenerator generator; irDocument.javaScriptCompilationUnit = v4CodeGen.generateCompilationUnit(/*generate unit*/false); generator.generate(irDocument); - QV4::CompiledData::Unit *unit = const_cast<QV4::CompiledData::Unit*>(irDocument.javaScriptCompilationUnit->data); - unit->flags |= QV4::CompiledData::Unit::StaticData; - unit->flags |= QV4::CompiledData::Unit::PendingTypeCompilation; - if (!saveFunction(irDocument.javaScriptCompilationUnit, &error->message)) + const quint32 saveFlags + = QV4::CompiledData::Unit::StaticData + | QV4::CompiledData::Unit::PendingTypeCompilation; + QV4::CompiledData::SaveableUnitPointer saveable(irDocument.javaScriptCompilationUnit.data, + saveFlags); + if (!saveFunction(saveable, &error->message)) return false; } return true; @@ -241,7 +246,7 @@ static bool compileQmlFile(const QString &inputFileName, SaveFunction saveFuncti static bool compileJSFile(const QString &inputFileName, const QString &inputFileUrl, SaveFunction saveFunction, Error *error) { - QQmlRefPointer<QV4::CompiledData::CompilationUnit> unit; + QV4::CompiledData::CompilationUnit unit; QString sourceCode; { @@ -262,9 +267,10 @@ static bool compileJSFile(const QString &inputFileName, const QString &inputFile QList<QQmlJS::DiagnosticMessage> diagnostics; // Precompiled files are relocatable and the final location will be set when loading. QString url; - unit = QV4::ExecutionEngine::compileModule(/*debugMode*/false, url, sourceCode, QDateTime(), &diagnostics); + unit = QV4::Compiler::Codegen::compileModule(/*debugMode*/false, url, sourceCode, + QDateTime(), &diagnostics); error->appendDiagnostics(inputFileName, diagnostics); - if (!unit) + if (!unit.unitData()) return false; } else { QmlIR::Document irDocument(/*debugMode*/false); @@ -302,14 +308,11 @@ static bool compileJSFile(const QString &inputFileName, const QString &inputFile } { - QmlIR::JSCodeGen v4CodeGen(irDocument.code, &irDocument.jsGenerator, - &irDocument.jsModule, &irDocument.jsParserEngine, - irDocument.program, &irDocument.jsGenerator.stringTable, illegalNames); + QmlIR::JSCodeGen v4CodeGen(&irDocument, illegalNames); v4CodeGen.generateFromProgram(inputFileName, inputFileUrl, sourceCode, program, &irDocument.jsModule, QV4::Compiler::ContextType::ScriptImportedByQML); - QList<QQmlJS::DiagnosticMessage> jsErrors = v4CodeGen.errors(); - if (!jsErrors.isEmpty()) { - error->appendDiagnostics(inputFileName, jsErrors); + if (v4CodeGen.hasError()) { + error->appendDiagnostic(inputFileName, v4CodeGen.error()); return false; } @@ -320,19 +323,22 @@ static bool compileJSFile(const QString &inputFileName, const QString &inputFile irDocument.javaScriptCompilationUnit = v4CodeGen.generateCompilationUnit(/*generate unit*/false); QmlIR::QmlUnitGenerator generator; generator.generate(irDocument); - QV4::CompiledData::Unit *unitData = const_cast<QV4::CompiledData::Unit*>(irDocument.javaScriptCompilationUnit->data); - unitData->flags |= QV4::CompiledData::Unit::StaticData; - unit = irDocument.javaScriptCompilationUnit; + unit = std::move(irDocument.javaScriptCompilationUnit); } } - return saveFunction(unit, &error->message); + return saveFunction(QV4::CompiledData::SaveableUnitPointer(unit.data), &error->message); } static bool saveUnitAsCpp(const QString &inputFileName, const QString &outputFileName, - const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &unit, QString *errorString) + const QV4::CompiledData::SaveableUnitPointer &unit, + QString *errorString) { +#if QT_CONFIG(temporaryfile) QSaveFile f(outputFileName); +#else + QFile f(outputFileName); +#endif if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) { *errorString = f.errorString(); return false; @@ -364,43 +370,38 @@ static bool saveUnitAsCpp(const QString &inputFileName, const QString &outputFil if (!writeStr(QByteArrayLiteral(" {\nextern const unsigned char qmlData alignas(16) [] = {\n"))) return false; - QByteArray hexifiedData; - { - QByteArray modifiedUnit; - modifiedUnit.resize(unit->data->unitSize); - memcpy(modifiedUnit.data(), unit->data, unit->data->unitSize); - const char *dataPtr = modifiedUnit.data(); - QV4::CompiledData::Unit *unitPtr; - memcpy(&unitPtr, &dataPtr, sizeof(unitPtr)); - unitPtr->flags |= QV4::CompiledData::Unit::StaticData; - - QTextStream stream(&hexifiedData); - const uchar *begin = reinterpret_cast<const uchar *>(modifiedUnit.constData()); - const uchar *end = begin + unit->data->unitSize; - stream << hex; - int col = 0; - for (const uchar *data = begin; data < end; ++data, ++col) { - if (data > begin) - stream << ','; - if (col % 8 == 0) { - stream << '\n'; - col = 0; + unit.saveToDisk<uchar>([&writeStr](const uchar *begin, quint32 size) { + QByteArray hexifiedData; + { + QTextStream stream(&hexifiedData); + const uchar *end = begin + size; + stream << hex; + int col = 0; + for (const uchar *data = begin; data < end; ++data, ++col) { + if (data > begin) + stream << ','; + if (col % 8 == 0) { + stream << '\n'; + col = 0; + } + stream << "0x" << *data; } - stream << "0x" << *data; + stream << '\n'; } - stream << '\n'; - }; + return writeStr(hexifiedData); + }); + - if (!writeStr(hexifiedData)) - return false; if (!writeStr("};\n}\n}\n")) return false; +#if QT_CONFIG(temporaryfile) if (!f.commit()) { *errorString = f.errorString(); return false; } +#endif return true; } @@ -524,13 +525,20 @@ int main(int argc, char **argv) inputFileUrl = QStringLiteral("qrc://") + inputResourcePath; - saveFunction = [inputResourcePath, outputFileName](const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &unit, QString *errorString) { + saveFunction = [inputResourcePath, outputFileName]( + const QV4::CompiledData::SaveableUnitPointer &unit, + QString *errorString) { return saveUnitAsCpp(inputResourcePath, outputFileName, unit, errorString); }; } else { - saveFunction = [outputFileName](const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &unit, QString *errorString) { - return unit->saveToDisk(outputFileName, errorString); + saveFunction = [outputFileName](const QV4::CompiledData::SaveableUnitPointer &unit, + QString *errorString) { + return unit.saveToDisk<char>( + [&outputFileName, errorString](const char *data, quint32 size) { + return QV4::CompiledData::SaveableUnitPointer::writeDataToFile( + outputFileName, data, size, errorString); + }); }; } diff --git a/tools/qmlcachegen/qmlcachegen.pro b/tools/qmlcachegen/qmlcachegen.pro index bee0b9a37e..ec65cdb5e6 100644 --- a/tools/qmlcachegen/qmlcachegen.pro +++ b/tools/qmlcachegen/qmlcachegen.pro @@ -5,8 +5,10 @@ DEFINES += QT_NO_CAST_TO_ASCII QT_NO_CAST_FROM_ASCII SOURCES = qmlcachegen.cpp \ resourcefilter.cpp \ - generateloader.cpp \ - resourcefilemapper.cpp + generateloader.cpp + +include(../shared/shared.pri) + TARGET = qmlcachegen build_integration.files = qmlcache.prf qtquickcompiler.prf @@ -38,5 +40,3 @@ QMAKE_TARGET_DESCRIPTION = QML Cache Generator load(qt_tool) -HEADERS += \ - resourcefilemapper.h diff --git a/tools/qmlcachegen/resourcefilemapper.cpp b/tools/qmlcachegen/resourcefilemapper.cpp deleted file mode 100644 index 6a00b39f2e..0000000000 --- a/tools/qmlcachegen/resourcefilemapper.cpp +++ /dev/null @@ -1,169 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "resourcefilemapper.h" - -#include <QFileInfo> -#include <QDir> -#include <QXmlStreamReader> - -ResourceFileMapper::ResourceFileMapper(const QStringList &resourceFiles) -{ - for (const QString &fileName: resourceFiles) { - QFile f(fileName); - if (!f.open(QIODevice::ReadOnly)) - continue; - populateFromQrcFile(f); - } -} - -bool ResourceFileMapper::isEmpty() const -{ - return qrcPathToFileSystemPath.isEmpty(); -} - -QStringList ResourceFileMapper::resourcePaths(const QString &fileName) -{ - const QString absPath = QDir::cleanPath(QDir::current().absoluteFilePath(fileName)); - QHashIterator<QString, QString> it(qrcPathToFileSystemPath); - QStringList resourcePaths; - while (it.hasNext()) { - it.next(); - if (QFileInfo(it.value()) == QFileInfo(absPath)) - resourcePaths.append(it.key()); - } - return resourcePaths; -} - -QStringList ResourceFileMapper::qmlCompilerFiles() const -{ - QStringList files; - for (auto it = qrcPathToFileSystemPath.constBegin(), end = qrcPathToFileSystemPath.constEnd(); - it != end; ++it) { - const QString &qrcPath = it.key(); - const QString suffix = QFileInfo(qrcPath).suffix(); - if (suffix != QStringLiteral("qml") && suffix != QStringLiteral("js") && suffix != QStringLiteral("mjs")) - continue; - files << qrcPath; - } - return files; -} - -void ResourceFileMapper::populateFromQrcFile(QFile &file) -{ - enum State { - InitialState, - InRCC, - InResource, - InFile - }; - State state = InitialState; - - QDir qrcDir = QFileInfo(file).absoluteDir(); - - QString prefix; - QString currentFileName; - QXmlStreamAttributes currentFileAttributes; - - QXmlStreamReader reader(&file); - while (!reader.atEnd()) { - switch (reader.readNext()) { - case QXmlStreamReader::StartElement: - if (reader.name() == QStringLiteral("RCC")) { - if (state != InitialState) - return; - state = InRCC; - continue; - } else if (reader.name() == QStringLiteral("qresource")) { - if (state != InRCC) - return; - state = InResource; - QXmlStreamAttributes attributes = reader.attributes(); - if (attributes.hasAttribute(QStringLiteral("prefix"))) - prefix = attributes.value(QStringLiteral("prefix")).toString(); - if (!prefix.startsWith(QLatin1Char('/'))) - prefix.prepend(QLatin1Char('/')); - if (!prefix.endsWith(QLatin1Char('/'))) - prefix.append(QLatin1Char('/')); - continue; - } else if (reader.name() == QStringLiteral("file")) { - if (state != InResource) - return; - state = InFile; - currentFileAttributes = reader.attributes(); - continue; - } - return; - - case QXmlStreamReader::EndElement: - if (reader.name() == QStringLiteral("file")) { - if (state != InFile) - return; - state = InResource; - continue; - } else if (reader.name() == QStringLiteral("qresource")) { - if (state != InResource) - return; - state = InRCC; - continue; - } else if (reader.name() == QStringLiteral("RCC")) { - if (state != InRCC) - return; - state = InitialState; - continue; - } - return; - - case QXmlStreamReader::Characters: { - if (reader.isWhitespace()) - break; - if (state != InFile) - return; - currentFileName = reader.text().toString(); - if (currentFileName.isEmpty()) - continue; - - const QString fsPath = QDir::cleanPath(qrcDir.absoluteFilePath(currentFileName)); - - if (currentFileAttributes.hasAttribute(QStringLiteral("alias"))) - currentFileName = currentFileAttributes.value(QStringLiteral("alias")).toString(); - - currentFileName = QDir::cleanPath(currentFileName); - while (currentFileName.startsWith(QLatin1String("../"))) - currentFileName.remove(0, 3); - - const QString qrcPath = prefix + currentFileName; - if (QFile::exists(fsPath)) - qrcPathToFileSystemPath.insert(qrcPath, fsPath); - continue; - } - - default: break; - } - } -} diff --git a/tools/qmlcachegen/resourcefilemapper.h b/tools/qmlcachegen/resourcefilemapper.h deleted file mode 100644 index 2e0ab45171..0000000000 --- a/tools/qmlcachegen/resourcefilemapper.h +++ /dev/null @@ -1,50 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ -#ifndef RESOURCEFILEMAPPER_H -#define RESOURCEFILEMAPPER_H - -#include <QStringList> -#include <QHash> -#include <QFile> - -struct ResourceFileMapper -{ - ResourceFileMapper(const QStringList &resourceFiles); - - bool isEmpty() const; - - QStringList resourcePaths(const QString &fileName); - QStringList qmlCompilerFiles() const; - -private: - void populateFromQrcFile(QFile &file); - - QHash<QString, QString> qrcPathToFileSystemPath; -}; - -#endif // RESOURCEFILEMAPPER_H |