From 58ff7aa4fed5b3a27b6a859fc02f9ee27dd1a6cc Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Tue, 23 Nov 2021 19:00:29 +0100 Subject: Compile QML files ahead of time with qmlcachegen qmlcachegen compiles bindings and functions to C++ as far as QQmlJSAotCompiler can. It does respect "pragma Strict" and rejects the file if it's violated. Furthermore, it sets up the logger to follow the qt.qml.compiler.aot logging category. By default it's completely silent. Compiling the examples with qmlcachegen exposes a bug in the type resolver where it returns an invalid generic type. It should never do that. Fix it by returning JSValue. [ChangeLog][QtQml][Important Behavior Changes] QML bindings and functions are now compiled to C++ by qmlcachegen, if possible. Use the qt.qml.compiler.aot logging category to receive diagnostics about the compilation. Task-number: QTBUG-98305 Change-Id: I6953812c3fd20b68339617a5714fcbe16a384360 Reviewed-by: Qt CI Bot Reviewed-by: Fabian Kosmale --- tools/qmlcachegen/qmlcachegen.cpp | 116 +++++++++++++++++++++++++++----------- 1 file changed, 83 insertions(+), 33 deletions(-) (limited to 'tools/qmlcachegen/qmlcachegen.cpp') diff --git a/tools/qmlcachegen/qmlcachegen.cpp b/tools/qmlcachegen/qmlcachegen.cpp index 29fe609469..3648a10f95 100644 --- a/tools/qmlcachegen/qmlcachegen.cpp +++ b/tools/qmlcachegen/qmlcachegen.cpp @@ -36,6 +36,8 @@ #include #include #include +#include +#include #include #include @@ -47,7 +49,9 @@ #include -using namespace QQmlJS; +QT_BEGIN_NAMESPACE +Q_DECLARE_LOGGING_CATEGORY(lcAotCompiler); +QT_END_NAMESPACE static bool argumentsFromCommandLineAndFile(QStringList& allArguments, const QStringList &arguments) { @@ -106,12 +110,21 @@ int main(int argc, char **argv) QCommandLineOption directCallsOption(QStringLiteral("direct-calls"), QCoreApplication::translate("main", "This option is ignored.")); directCallsOption.setFlags(QCommandLineOption::HiddenFromHelp); parser.addOption(directCallsOption); - QCommandLineOption includesOption(QStringLiteral("i"), QCoreApplication::translate("main", "This option is ignored."), QCoreApplication::translate("main", "ignored file")); - includesOption.setFlags(QCommandLineOption::HiddenFromHelp); - parser.addOption(includesOption); - QCommandLineOption importPathOption(QStringLiteral("I"), QCoreApplication::translate("main", "This option is ignored."), QCoreApplication::translate("main", "ignored path")); - importPathOption.setFlags(QCommandLineOption::HiddenFromHelp); + QCommandLineOption importsOption( + QStringLiteral("i"), + QCoreApplication::translate("main", "Import extra qmltypes"), + QCoreApplication::translate("main", "qmltypes file")); + parser.addOption(importsOption); + QCommandLineOption importPathOption( + QStringLiteral("I"), + QCoreApplication::translate("main", "Look for QML modules in specified directory"), + QCoreApplication::translate("main", "import directory")); parser.addOption(importPathOption); + QCommandLineOption onlyBytecode( + QStringLiteral("only-bytecode"), + QCoreApplication::translate( + "main", "Generate only byte code for bindings and functions, no C++ code")); + parser.addOption(onlyBytecode); QCommandLineOption outputFileOption(QStringLiteral("o"), QCoreApplication::translate("main", "Output file name"), QCoreApplication::translate("main", "file name")); parser.addOption(outputFileOption); @@ -188,35 +201,34 @@ int main(int argc, char **argv) QString inputFileUrl = inputFile; QQmlJSSaveFunction saveFunction; - if (target == GenerateCpp) { - QQmlJSResourceFileMapper fileMapper(parser.values(resourceOption)); - QString inputResourcePath = parser.value(resourcePathOption); - - // If the user didn't specify the resource path corresponding to the file on disk being - // compiled, try to determine it from the resource file, if one was supplied. - if (inputResourcePath.isEmpty()) { - const QStringList resourcePaths = fileMapper.resourcePaths( - QQmlJSResourceFileMapper::localFileFilter(inputFile)); - if (resourcePaths.isEmpty()) { - fprintf(stderr, "No resource path for file: %s\n", qPrintable(inputFile)); - return EXIT_FAILURE; - } - - if (resourcePaths.size() != 1) { - fprintf(stderr, "Multiple resource paths for file %s. " - "Use the --%s option to disambiguate:\n", - qPrintable(inputFile), - qPrintable(resourcePathOption.names().first())); - for (const QString &resourcePath: resourcePaths) - fprintf(stderr, "\t%s\n", qPrintable(resourcePath)); - return EXIT_FAILURE; - } + QQmlJSResourceFileMapper fileMapper(parser.values(resourceOption)); + QString inputResourcePath = parser.value(resourcePathOption); + + // If the user didn't specify the resource path corresponding to the file on disk being + // compiled, try to determine it from the resource file, if one was supplied. + if (inputResourcePath.isEmpty()) { + const QStringList resourcePaths = fileMapper.resourcePaths( + QQmlJSResourceFileMapper::localFileFilter(inputFile)); + if (target == GenerateCpp && resourcePaths.isEmpty()) { + fprintf(stderr, "No resource path for file: %s\n", qPrintable(inputFile)); + return EXIT_FAILURE; + } + if (resourcePaths.size() == 1) { inputResourcePath = resourcePaths.first(); + } else if (target == GenerateCpp) { + fprintf(stderr, "Multiple resource paths for file %s. " + "Use the --%s option to disambiguate:\n", + qPrintable(inputFile), + qPrintable(resourcePathOption.names().first())); + for (const QString &resourcePath: resourcePaths) + fprintf(stderr, "\t%s\n", qPrintable(resourcePath)); + return EXIT_FAILURE; } + } + if (target == GenerateCpp) { inputFileUrl = QStringLiteral("qrc://") + inputResourcePath; - saveFunction = [inputResourcePath, outputFileName]( const QV4::CompiledData::SaveableUnitPointer &unit, const QQmlJSAotFunctionMap &aotFunctions, @@ -240,9 +252,47 @@ int main(int argc, char **argv) if (inputFile.endsWith(QLatin1String(".qml"))) { QQmlJSCompileError error; - if (!qCompileQmlFile(inputFile, saveFunction, nullptr, &error)) { - error.augment(QLatin1String("Error compiling qml file: ")).print(); - return EXIT_FAILURE; + if (target != GenerateCpp || inputResourcePath.isEmpty() || parser.isSet(onlyBytecode)) { + if (!qCompileQmlFile(inputFile, saveFunction, nullptr, &error, + /* storeSourceLocation */ false)) { + error.augment(QStringLiteral("Error compiling qml file: ")).print(); + return EXIT_FAILURE; + } + } else { + QStringList importPaths; + if (parser.isSet(importPathOption)) + importPaths = parser.values(importPathOption); + + importPaths.append(QLibraryInfo::path(QLibraryInfo::QmlImportsPath)); + + QQmlJSImporter importer( + importPaths, parser.isSet(resourceOption) ? &fileMapper : nullptr); + QQmlJSLogger logger; + + // Always trigger the qFatal() on "pragma Strict" violations. + logger.setCategoryError(Log_Compiler, true); + + // By default, we're completely silent, + // as the lcAotCompiler category default is QtFatalMsg + if (lcAotCompiler().isDebugEnabled()) + logger.setCategoryLevel(Log_Compiler, QtDebugMsg); + else if (lcAotCompiler().isInfoEnabled()) + logger.setCategoryLevel(Log_Compiler, QtInfoMsg); + else if (lcAotCompiler().isWarningEnabled()) + logger.setCategoryLevel(Log_Compiler, QtWarningMsg); + else if (lcAotCompiler().isCriticalEnabled()) + logger.setCategoryLevel(Log_Compiler, QtCriticalMsg); + else + logger.setSilent(true); + + QQmlJSAotCompiler cppCodeGen( + &importer, u':' + inputResourcePath, parser.values(importsOption), &logger); + + if (!qCompileQmlFile(inputFile, saveFunction, &cppCodeGen, &error, + /* storeSourceLocation */ true)) { + error.augment(QStringLiteral("Error compiling qml file: ")).print(); + return EXIT_FAILURE; + } } } else if (inputFile.endsWith(QLatin1String(".js")) || inputFile.endsWith(QLatin1String(".mjs"))) { QQmlJSCompileError error; -- cgit v1.2.3