diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2020-10-20 15:07:59 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2020-10-23 13:31:14 +0200 |
commit | 2458f07eb568dc13f1f92b77adc047ef129a5636 (patch) | |
tree | 3a0c2081d39465dc24399704eb7e2c6db4c2718b /tools | |
parent | f263e6ac9321d1f1b0fc4e46371642a2dbe85262 (diff) |
Move generateloader.cpp to QmlCompiler
We need to be able to generate the loader code from multiple places.
Change-Id: I9e04fd3583b535bc5f7d5fb293cb61309c1e199a
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/qmlcachegen/.prev_CMakeLists.txt | 1 | ||||
-rw-r--r-- | tools/qmlcachegen/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tools/qmlcachegen/generateloader.cpp | 231 | ||||
-rw-r--r-- | tools/qmlcachegen/qmlcachegen.cpp | 15 | ||||
-rw-r--r-- | tools/qmlcachegen/qmlcachegen.pro | 3 |
5 files changed, 7 insertions, 244 deletions
diff --git a/tools/qmlcachegen/.prev_CMakeLists.txt b/tools/qmlcachegen/.prev_CMakeLists.txt index 687ece89a1..a33e080bbe 100644 --- a/tools/qmlcachegen/.prev_CMakeLists.txt +++ b/tools/qmlcachegen/.prev_CMakeLists.txt @@ -8,7 +8,6 @@ qt_get_tool_target_name(target_name qmlcachegen) qt_internal_add_tool(${target_name} TARGET_DESCRIPTION "QML Cache Generator" SOURCES - generateloader.cpp qmlcachegen.cpp DEFINES QT_NO_CAST_FROM_ASCII diff --git a/tools/qmlcachegen/CMakeLists.txt b/tools/qmlcachegen/CMakeLists.txt index e81eae7dfb..e0ae4c4092 100644 --- a/tools/qmlcachegen/CMakeLists.txt +++ b/tools/qmlcachegen/CMakeLists.txt @@ -9,7 +9,6 @@ qt_internal_add_tool(${target_name} TARGET_DESCRIPTION "QML Cache Generator" TOOLS_TARGET Qml # special case SOURCES - generateloader.cpp qmlcachegen.cpp DEFINES QT_NO_CAST_FROM_ASCII diff --git a/tools/qmlcachegen/generateloader.cpp b/tools/qmlcachegen/generateloader.cpp deleted file mode 100644 index a2b3f91322..0000000000 --- a/tools/qmlcachegen/generateloader.cpp +++ /dev/null @@ -1,231 +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 <QByteArray> -#include <QRegularExpression> -#include <QString> -#include <QStringList> -#include <QTextStream> -#include <QVector> -#include <QtEndian> -#include <QStack> -#include <QFileInfo> -#include <QSaveFile> - -#include <algorithm> - -/*! - * \internal - * Mangles \a str to be a unique C++ identifier. Characters that are invalid for C++ identifiers - * are replaced by the pattern \c _0x<hex>_ where <hex> is the hexadecimal unicode - * representation of the character. As identifiers with leading underscores followed by either - * another underscore or a capital letter are reserved in C++, we also escape those, by escaping - * the first underscore, using the above method. - * - * \note - * Although C++11 allows for non-ascii (unicode) characters to be used in identifiers, - * many compilers forgot to read the spec and do not implement this. Some also do not - * implement C99 identifiers, because that is \e {at the implementation's discretion}. So, - * we are stuck with plain old boring identifiers. - */ -QString mangledIdentifier(const QString &str) -{ - Q_ASSERT(!str.isEmpty()); - - QString mangled; - mangled.reserve(str.size()); - - int i = 0; - if (str.startsWith(QLatin1Char('_')) && str.size() > 1) { - QChar ch = str.at(1); - if (ch == QLatin1Char('_') - || (ch >= QLatin1Char('A') && ch <= QLatin1Char('Z'))) { - mangled += QLatin1String("_0x5f_"); - ++i; - } - } - - for (int ei = str.length(); i != ei; ++i) { - auto c = str.at(i).unicode(); - if ((c >= QLatin1Char('0') && c <= QLatin1Char('9')) - || (c >= QLatin1Char('a') && c <= QLatin1Char('z')) - || (c >= QLatin1Char('A') && c <= QLatin1Char('Z')) - || c == QLatin1Char('_')) { - mangled += QChar(c); - } else { - mangled += QLatin1String("_0x") + QString::number(c, 16) + QLatin1Char('_'); - } - } - - return mangled; -} - -QString symbolNamespaceForPath(const QString &relativePath) -{ - QFileInfo fi(relativePath); - QString symbol = fi.path(); - if (symbol.length() == 1 && symbol.startsWith(QLatin1Char('.'))) { - symbol.clear(); - } else { - symbol.replace(QLatin1Char('/'), QLatin1Char('_')); - symbol += QLatin1Char('_'); - } - symbol += fi.baseName(); - symbol += QLatin1Char('_'); - symbol += fi.completeSuffix(); - return mangledIdentifier(symbol); -} - -static QString qtResourceNameForFile(const QString &fileName) -{ - QFileInfo fi(fileName); - QString name = fi.completeBaseName(); - if (name.isEmpty()) - name = fi.fileName(); - name.replace(QRegularExpression(QLatin1String("[^a-zA-Z0-9_]")), QLatin1String("_")); - return name; -} - -bool generateLoader(const QStringList &compiledFiles, const QString &outputFileName, - const QStringList &resourceFileMappings, QString *errorString) -{ - QByteArray generatedLoaderCode; - - { - QTextStream stream(&generatedLoaderCode); - stream << "#include <QtQml/qqmlprivate.h>\n"; - stream << "#include <QtCore/qdir.h>\n"; - stream << "#include <QtCore/qurl.h>\n"; - stream << "\n"; - - stream << "namespace QmlCacheGeneratedCode {\n"; - for (int i = 0; i < compiledFiles.count(); ++i) { - const QString compiledFile = compiledFiles.at(i); - const QString ns = symbolNamespaceForPath(compiledFile); - stream << "namespace " << ns << " { \n"; - stream << " extern const unsigned char qmlData[];\n"; - stream << " const QQmlPrivate::CachedQmlUnit unit = {\n"; - stream << " reinterpret_cast<const QV4::CompiledData::Unit*>(&qmlData), nullptr, nullptr\n"; - stream << " };\n"; - stream << "}\n"; - } - - stream << "\n}\n"; - stream << "namespace {\n"; - - stream << "struct Registry {\n"; - stream << " Registry();\n"; - stream << " ~Registry();\n"; - stream << " QHash<QString, const QQmlPrivate::CachedQmlUnit*> resourcePathToCachedUnit;\n"; - stream << " static const QQmlPrivate::CachedQmlUnit *lookupCachedUnit(const QUrl &url);\n"; - stream << "};\n\n"; - stream << "Q_GLOBAL_STATIC(Registry, unitRegistry)\n"; - stream << "\n\n"; - - stream << "Registry::Registry() {\n"; - - for (int i = 0; i < compiledFiles.count(); ++i) { - const QString qrcFile = compiledFiles.at(i); - const QString ns = symbolNamespaceForPath(qrcFile); - stream << " resourcePathToCachedUnit.insert(QStringLiteral(\"" << qrcFile << "\"), &QmlCacheGeneratedCode::" << ns << "::unit);\n"; - } - - stream << " QQmlPrivate::RegisterQmlUnitCacheHook registration;\n"; - stream << " registration.structVersion = 0;\n"; - stream << " registration.lookupCachedQmlUnit = &lookupCachedUnit;\n"; - stream << " QQmlPrivate::qmlregister(QQmlPrivate::QmlUnitCacheHookRegistration, ®istration);\n"; - - stream << "}\n\n"; - stream << "Registry::~Registry() {\n"; - stream << " QQmlPrivate::qmlunregister(QQmlPrivate::QmlUnitCacheHookRegistration, quintptr(&lookupCachedUnit));\n"; - stream << "}\n\n"; - - stream << "const QQmlPrivate::CachedQmlUnit *Registry::lookupCachedUnit(const QUrl &url) {\n"; - stream << " if (url.scheme() != QLatin1String(\"qrc\"))\n"; - stream << " return nullptr;\n"; - stream << " QString resourcePath = QDir::cleanPath(url.path());\n"; - stream << " if (resourcePath.isEmpty())\n"; - stream << " return nullptr;\n"; - stream << " if (!resourcePath.startsWith(QLatin1Char('/')))\n"; - stream << " resourcePath.prepend(QLatin1Char('/'));\n"; - stream << " return unitRegistry()->resourcePathToCachedUnit.value(resourcePath, nullptr);\n"; - stream << "}\n"; - stream << "}\n"; - - for (const QString &mapping: resourceFileMappings) { - QString originalResourceFile = mapping; - QString newResourceFile; - const int mappingSplit = originalResourceFile.indexOf(QLatin1Char('=')); - if (mappingSplit != -1) { - newResourceFile = originalResourceFile.mid(mappingSplit + 1); - originalResourceFile.truncate(mappingSplit); - } - - const QString suffix = qtResourceNameForFile(originalResourceFile); - const QString initFunction = QLatin1String("qInitResources_") + suffix; - - stream << QStringLiteral("int QT_MANGLE_NAMESPACE(%1)() {\n").arg(initFunction); - stream << " ::unitRegistry();\n"; - if (!newResourceFile.isEmpty()) - stream << " Q_INIT_RESOURCE(" << qtResourceNameForFile(newResourceFile) << ");\n"; - stream << " return 1;\n"; - stream << "}\n"; - stream << "Q_CONSTRUCTOR_FUNCTION(QT_MANGLE_NAMESPACE(" << initFunction << "))\n"; - - const QString cleanupFunction = QLatin1String("qCleanupResources_") + suffix; - stream << QStringLiteral("int QT_MANGLE_NAMESPACE(%1)() {\n").arg(cleanupFunction); - if (!newResourceFile.isEmpty()) - stream << " Q_CLEANUP_RESOURCE(" << qtResourceNameForFile(newResourceFile) << ");\n"; - stream << " return 1;\n"; - stream << "}\n"; - } - } - -#if QT_CONFIG(temporaryfile) - QSaveFile f(outputFileName); -#else - QFile f(outputFileName); -#endif - if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) { - *errorString = f.errorString(); - return false; - } - - if (f.write(generatedLoaderCode) != generatedLoaderCode.size()) { - *errorString = f.errorString(); - 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 ca68e7e450..33a77bc835 100644 --- a/tools/qmlcachegen/qmlcachegen.cpp +++ b/tools/qmlcachegen/qmlcachegen.cpp @@ -41,16 +41,13 @@ #include <private/qqmljsparser_p.h> #include <private/qqmljslexer_p.h> #include <private/qqmljsresourcefilemapper_p.h> +#include <private/qqmljsloadergenerator_p.h> #include <private/qresourcerelocater_p.h> #include <algorithm> using namespace QQmlJS; -bool generateLoader(const QStringList &compiledFiles, const QString &output, - const QStringList &resourceFileMappings, QString *errorString); -QString symbolNamespaceForPath(const QString &relativePath); - QSet<QString> illegalNames; void setupIllegalNames() @@ -392,7 +389,7 @@ static bool saveUnitAsCpp(const QString &inputFileName, const QString &outputFil if (!writeStr(QByteArrayLiteral("namespace QmlCacheGeneratedCode {\nnamespace "))) return false; - if (!writeStr(symbolNamespaceForPath(inputFileName).toUtf8())) + if (!writeStr(qQmlJSSymbolNamespaceForPath(inputFileName).toUtf8())) return false; if (!writeStr(QByteArrayLiteral(" {\nextern const unsigned char qmlData alignas(16) [] = {\n"))) @@ -514,8 +511,8 @@ int main(int argc, char **argv) QQmlJSResourceFileMapper mapper(sources); Error error; - if (!generateLoader(mapper.qmlCompilerFiles(), outputFileName, - parser.values(resourceFileMappingOption), &error.message)) { + if (!qQmlJSGenerateLoader(mapper.qmlCompilerFiles(), outputFileName, + parser.values(resourceFileMappingOption), &error.message)) { error.augment(QLatin1String("Error generating loader stub: ")).print(); return EXIT_FAILURE; } @@ -524,8 +521,8 @@ int main(int argc, char **argv) if (target == GenerateLoaderStandAlone) { Error error; - if (!generateLoader(sources, outputFileName, - parser.values(resourceNameOption), &error.message)) { + if (!qQmlJSGenerateLoader(sources, outputFileName, + parser.values(resourceNameOption), &error.message)) { error.augment(QLatin1String("Error generating loader stub: ")).print(); return EXIT_FAILURE; } diff --git a/tools/qmlcachegen/qmlcachegen.pro b/tools/qmlcachegen/qmlcachegen.pro index 1305d8fe65..62846ddd1c 100644 --- a/tools/qmlcachegen/qmlcachegen.pro +++ b/tools/qmlcachegen/qmlcachegen.pro @@ -4,8 +4,7 @@ QT = qmldevtools-private qmlcompiler-private DEFINES += QT_NO_CAST_TO_ASCII QT_NO_CAST_FROM_ASCII SOURCES = \ - qmlcachegen.cpp \ - generateloader.cpp + qmlcachegen.cpp TARGET = qmlcachegen |