From 51f6d418fa803a37e8a1e3bcfdd7f7e02b7bdcad Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Tue, 20 Oct 2020 13:57:38 +0200 Subject: qmlcachegen: Move resourcefilter.cpp into QmlCompiler For backwards compatibility, a replacement for qmlcachegen will need to provide the same functionality. Change-Id: I22664230ea636d384190122223d15819ebee930c Reviewed-by: Fabian Kosmale --- src/qmlcompiler/CMakeLists.txt | 1 + src/qmlcompiler/qmlcompiler.pro | 6 +- src/qmlcompiler/qresourcerelocater.cpp | 182 +++++++++++++++++++++++++++++++++ src/qmlcompiler/qresourcerelocater_p.h | 50 +++++++++ tools/qmlcachegen/.prev_CMakeLists.txt | 1 - tools/qmlcachegen/CMakeLists.txt | 1 - tools/qmlcachegen/qmlcachegen.cpp | 7 +- tools/qmlcachegen/qmlcachegen.pro | 1 - tools/qmlcachegen/resourcefilter.cpp | 172 ------------------------------- 9 files changed, 240 insertions(+), 181 deletions(-) create mode 100644 src/qmlcompiler/qresourcerelocater.cpp create mode 100644 src/qmlcompiler/qresourcerelocater_p.h delete mode 100644 tools/qmlcachegen/resourcefilter.cpp diff --git a/src/qmlcompiler/CMakeLists.txt b/src/qmlcompiler/CMakeLists.txt index d0e9be9418..f2180f35cd 100644 --- a/src/qmlcompiler/CMakeLists.txt +++ b/src/qmlcompiler/CMakeLists.txt @@ -17,6 +17,7 @@ qt_internal_add_module(QmlCompiler qqmljsstreamwriter.cpp qqmljsstreamwriter_p.h qqmljstypedescriptionreader.cpp qqmljstypedescriptionreader_p.h qqmljstypereader.cpp qqmljstypereader_p.h + qresourcerelocater.cpp qresourcerelocater_p.h PUBLIC_LIBRARIES Qt::CorePrivate Qt::QmlDevToolsPrivate diff --git a/src/qmlcompiler/qmlcompiler.pro b/src/qmlcompiler/qmlcompiler.pro index 37a1b44b9c..a8c81af081 100644 --- a/src/qmlcompiler/qmlcompiler.pro +++ b/src/qmlcompiler/qmlcompiler.pro @@ -10,7 +10,8 @@ SOURCES = \ qqmljstypereader.cpp \ qqmljsscope.cpp \ qqmljstypedescriptionreader.cpp \ - qqmljsstreamwriter.cpp + qqmljsstreamwriter.cpp \ + qresourcerelocater.cpp HEADERS = \ qdeferredpointer_p.h \ @@ -21,6 +22,7 @@ HEADERS = \ qqmljsmetatypes_p.h \ qqmljsscope_p.h \ qqmljstypedescriptionreader_p.h \ - qqmljsstreamwriter_p.h + qqmljsstreamwriter_p.h \ + qresourcerelocater_p.h load(qt_module) diff --git a/src/qmlcompiler/qresourcerelocater.cpp b/src/qmlcompiler/qresourcerelocater.cpp new file mode 100644 index 0000000000..89158c1446 --- /dev/null +++ b/src/qmlcompiler/qresourcerelocater.cpp @@ -0,0 +1,182 @@ +/**************************************************************************** +** +** 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 "qresourcerelocater_p.h" + +#include +#include +#include + +QT_BEGIN_NAMESPACE + +/*! + Changes all the paths in resource file \a input so that they are relative to + location \a output and writes the result to resource file \a output. + */ +int qRelocateResourceFile(const QString &input, const QString &output) +{ + enum State { + InitialState, + InRCC, + InResource, + InFile + }; + State state = InitialState; + + QString prefix; + QString currentFileName; + QXmlStreamAttributes fileAttributes; + + QFile file(input); + if (!file.open(QIODevice::ReadOnly)) { + fprintf(stderr, "Cannot open %s for reading.\n", qPrintable(input)); + return EXIT_FAILURE; + } + + QDir inputDirectory = QFileInfo(file).absoluteDir(); + QDir outputDirectory = QFileInfo(output).absoluteDir(); + + QString outputString; + QXmlStreamWriter writer(&outputString); + writer.setAutoFormatting(true); + + QXmlStreamReader reader(&file); + while (!reader.atEnd()) { + switch (reader.readNext()) { + case QXmlStreamReader::StartDocument: { + QStringView version = reader.documentVersion(); + if (!version.isEmpty()) + writer.writeStartDocument(version.toString()); + else + writer.writeStartDocument(); + break; + } + case QXmlStreamReader::EndDocument: + writer.writeEndDocument(); + break; + case QXmlStreamReader::StartElement: + if (reader.name() == QStringLiteral("RCC")) { + if (state != InitialState) { + fprintf(stderr, "Unexpected RCC tag in line %d\n", int(reader.lineNumber())); + return EXIT_FAILURE; + } + state = InRCC; + } else if (reader.name() == QStringLiteral("qresource")) { + if (state != InRCC) { + fprintf(stderr, "Unexpected qresource tag in line %d\n", int(reader.lineNumber())); + return EXIT_FAILURE; + } + 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('/')); + } else if (reader.name() == QStringLiteral("file")) { + if (state != InResource) { + fprintf(stderr, "Unexpected file tag in line %d\n", int(reader.lineNumber())); + return EXIT_FAILURE; + } + state = InFile; + fileAttributes = reader.attributes(); + continue; + } + writer.writeStartElement(reader.name().toString()); + writer.writeAttributes(reader.attributes()); + continue; + + case QXmlStreamReader::EndElement: + if (reader.name() == QStringLiteral("file")) { + if (state != InFile) { + fprintf(stderr, "Unexpected end of file tag in line %d\n", int(reader.lineNumber())); + return EXIT_FAILURE; + } + state = InResource; + continue; + } else if (reader.name() == QStringLiteral("qresource")) { + if (state != InResource) { + fprintf(stderr, "Unexpected end of qresource tag in line %d\n", int(reader.lineNumber())); + return EXIT_FAILURE; + } + state = InRCC; + } else if (reader.name() == QStringLiteral("RCC")) { + if (state != InRCC) { + fprintf(stderr, "Unexpected end of RCC tag in line %d\n", int(reader.lineNumber())); + return EXIT_FAILURE; + } + state = InitialState; + } + writer.writeEndElement(); + continue; + + case QXmlStreamReader::Characters: + if (reader.isWhitespace()) + break; + if (state != InFile) + return EXIT_FAILURE; + currentFileName = reader.text().toString(); + if (currentFileName.isEmpty()) + continue; + + writer.writeStartElement(QStringLiteral("file")); + + if (!fileAttributes.hasAttribute(QStringLiteral("alias"))) + fileAttributes.append(QStringLiteral("alias"), currentFileName); + + currentFileName = inputDirectory.absoluteFilePath(currentFileName); + currentFileName = outputDirectory.relativeFilePath(currentFileName); + + writer.writeAttributes(fileAttributes); + writer.writeCharacters(currentFileName); + writer.writeEndElement(); + continue; + + default: break; + } + } + + QFile outputFile(output); + if (!outputFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) { + fprintf(stderr, "Cannot open %s for writing.\n", qPrintable(output)); + return EXIT_FAILURE; + } + const QByteArray outputStringUtf8 = outputString.toUtf8(); + if (outputFile.write(outputStringUtf8) != outputStringUtf8.size()) + return EXIT_FAILURE; + + outputFile.close(); + if (outputFile.error() != QFileDevice::NoError) + return EXIT_FAILURE; + + + return EXIT_SUCCESS; +} + +QT_END_NAMESPACE diff --git a/src/qmlcompiler/qresourcerelocater_p.h b/src/qmlcompiler/qresourcerelocater_p.h new file mode 100644 index 0000000000..5b75a424e6 --- /dev/null +++ b/src/qmlcompiler/qresourcerelocater_p.h @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the tools applications 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 QRESOURCERELOCATER_P_H +#define QRESOURCERELOCATER_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#include + +QT_BEGIN_NAMESPACE + +int qRelocateResourceFile(const QString &input, const QString &output); + +QT_END_NAMESPACE + +#endif // QRESOURCERELOCATER_P_H diff --git a/tools/qmlcachegen/.prev_CMakeLists.txt b/tools/qmlcachegen/.prev_CMakeLists.txt index 78e63898ac..687ece89a1 100644 --- a/tools/qmlcachegen/.prev_CMakeLists.txt +++ b/tools/qmlcachegen/.prev_CMakeLists.txt @@ -10,7 +10,6 @@ qt_internal_add_tool(${target_name} SOURCES generateloader.cpp qmlcachegen.cpp - resourcefilter.cpp DEFINES QT_NO_CAST_FROM_ASCII QT_NO_CAST_TO_ASCII diff --git a/tools/qmlcachegen/CMakeLists.txt b/tools/qmlcachegen/CMakeLists.txt index 9903406fab..e81eae7dfb 100644 --- a/tools/qmlcachegen/CMakeLists.txt +++ b/tools/qmlcachegen/CMakeLists.txt @@ -11,7 +11,6 @@ qt_internal_add_tool(${target_name} SOURCES generateloader.cpp qmlcachegen.cpp - resourcefilter.cpp DEFINES QT_NO_CAST_FROM_ASCII QT_NO_CAST_TO_ASCII diff --git a/tools/qmlcachegen/qmlcachegen.cpp b/tools/qmlcachegen/qmlcachegen.cpp index 04d31d8887..ca68e7e450 100644 --- a/tools/qmlcachegen/qmlcachegen.cpp +++ b/tools/qmlcachegen/qmlcachegen.cpp @@ -41,12 +41,12 @@ #include #include #include +#include #include using namespace QQmlJS; -int filterResourceFile(const QString &input, const QString &output); bool generateLoader(const QStringList &compiledFiles, const QString &output, const QStringList &resourceFileMappings, QString *errorString); QString symbolNamespaceForPath(const QString &relativePath); @@ -507,9 +507,8 @@ int main(int argc, char **argv) if (outputFileName.isEmpty()) outputFileName = inputFile + QLatin1Char('c'); - if (parser.isSet(filterResourceFileOption)) { - return filterResourceFile(inputFile, outputFileName); - } + if (parser.isSet(filterResourceFileOption)) + return qRelocateResourceFile(inputFile, outputFileName); if (target == GenerateLoader) { QQmlJSResourceFileMapper mapper(sources); diff --git a/tools/qmlcachegen/qmlcachegen.pro b/tools/qmlcachegen/qmlcachegen.pro index 530a0d54a1..1305d8fe65 100644 --- a/tools/qmlcachegen/qmlcachegen.pro +++ b/tools/qmlcachegen/qmlcachegen.pro @@ -5,7 +5,6 @@ DEFINES += QT_NO_CAST_TO_ASCII QT_NO_CAST_FROM_ASCII SOURCES = \ qmlcachegen.cpp \ - resourcefilter.cpp \ generateloader.cpp TARGET = qmlcachegen diff --git a/tools/qmlcachegen/resourcefilter.cpp b/tools/qmlcachegen/resourcefilter.cpp deleted file mode 100644 index 37c027c365..0000000000 --- a/tools/qmlcachegen/resourcefilter.cpp +++ /dev/null @@ -1,172 +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 -#include -#include -#include - -int filterResourceFile(const QString &input, const QString &output) -{ - enum State { - InitialState, - InRCC, - InResource, - InFile - }; - State state = InitialState; - - QString prefix; - QString currentFileName; - QXmlStreamAttributes fileAttributes; - - QFile file(input); - if (!file.open(QIODevice::ReadOnly)) { - fprintf(stderr, "Cannot open %s for reading.\n", qPrintable(input)); - return EXIT_FAILURE; - } - - QDir inputDirectory = QFileInfo(file).absoluteDir(); - QDir outputDirectory = QFileInfo(output).absoluteDir(); - - QString outputString; - QXmlStreamWriter writer(&outputString); - writer.setAutoFormatting(true); - - QXmlStreamReader reader(&file); - while (!reader.atEnd()) { - switch (reader.readNext()) { - case QXmlStreamReader::StartDocument: { - QStringView version = reader.documentVersion(); - if (!version.isEmpty()) - writer.writeStartDocument(version.toString()); - else - writer.writeStartDocument(); - break; - } - case QXmlStreamReader::EndDocument: - writer.writeEndDocument(); - break; - case QXmlStreamReader::StartElement: - if (reader.name() == QStringLiteral("RCC")) { - if (state != InitialState) { - fprintf(stderr, "Unexpected RCC tag in line %d\n", int(reader.lineNumber())); - return EXIT_FAILURE; - } - state = InRCC; - } else if (reader.name() == QStringLiteral("qresource")) { - if (state != InRCC) { - fprintf(stderr, "Unexpected qresource tag in line %d\n", int(reader.lineNumber())); - return EXIT_FAILURE; - } - 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('/')); - } else if (reader.name() == QStringLiteral("file")) { - if (state != InResource) { - fprintf(stderr, "Unexpected file tag in line %d\n", int(reader.lineNumber())); - return EXIT_FAILURE; - } - state = InFile; - fileAttributes = reader.attributes(); - continue; - } - writer.writeStartElement(reader.name().toString()); - writer.writeAttributes(reader.attributes()); - continue; - - case QXmlStreamReader::EndElement: - if (reader.name() == QStringLiteral("file")) { - if (state != InFile) { - fprintf(stderr, "Unexpected end of file tag in line %d\n", int(reader.lineNumber())); - return EXIT_FAILURE; - } - state = InResource; - continue; - } else if (reader.name() == QStringLiteral("qresource")) { - if (state != InResource) { - fprintf(stderr, "Unexpected end of qresource tag in line %d\n", int(reader.lineNumber())); - return EXIT_FAILURE; - } - state = InRCC; - } else if (reader.name() == QStringLiteral("RCC")) { - if (state != InRCC) { - fprintf(stderr, "Unexpected end of RCC tag in line %d\n", int(reader.lineNumber())); - return EXIT_FAILURE; - } - state = InitialState; - } - writer.writeEndElement(); - continue; - - case QXmlStreamReader::Characters: - if (reader.isWhitespace()) - break; - if (state != InFile) - return EXIT_FAILURE; - currentFileName = reader.text().toString(); - if (currentFileName.isEmpty()) - continue; - - writer.writeStartElement(QStringLiteral("file")); - - if (!fileAttributes.hasAttribute(QStringLiteral("alias"))) - fileAttributes.append(QStringLiteral("alias"), currentFileName); - - currentFileName = inputDirectory.absoluteFilePath(currentFileName); - currentFileName = outputDirectory.relativeFilePath(currentFileName); - - writer.writeAttributes(fileAttributes); - writer.writeCharacters(currentFileName); - writer.writeEndElement(); - continue; - - default: break; - } - } - - QFile outputFile(output); - if (!outputFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) { - fprintf(stderr, "Cannot open %s for writing.\n", qPrintable(output)); - return EXIT_FAILURE; - } - const QByteArray outputStringUtf8 = outputString.toUtf8(); - if (outputFile.write(outputStringUtf8) != outputStringUtf8.size()) - return EXIT_FAILURE; - - outputFile.close(); - if (outputFile.error() != QFileDevice::NoError) - return EXIT_FAILURE; - - - return EXIT_SUCCESS; -} -- cgit v1.2.3