diff options
author | Alexandru Croitor <alexandru.croitor@qt.io> | 2019-07-16 15:07:51 +0200 |
---|---|---|
committer | Alexandru Croitor <alexandru.croitor@qt.io> | 2019-08-17 12:03:45 +0200 |
commit | 527687a866d1a155314c1b6d4b8a09533cf8211a (patch) | |
tree | 6959a67af694f5e286d1de7a164b32b35035d69e /tools/qmlimportscanner/main.cpp | |
parent | 412be2096cea12ca480cbbd419f3531a41dac3ae (diff) |
CMake: Provide API to allow handling of QML static plugins
This change adds a new -cmake-output command line argument to
qmlimportscanner which outputs its result in a format which is
consumable by CMake.
This change also adds a new CMake package called Qt5QmlImportScanner.
It provides a function called QT5_IMPORT_QML_PLUGINS()
which is useful for projects that use a static build of Qt and which
also use QML plugins.
Calling it with the target name of your application does the
following:
- Runs qmlimportscanner at configure time to find out which QML /
QtQuick plugins are used by your project
- Links the imported QML plugins into the target
- Links the static dependencies of the QML plugins into the target
- Generates a .cpp file that initializes imported QML plugins,
which is subsequently compiled and linked into the given target
When Qt is built in a shared library config, the introduced function
is a no-op.
[ChangeLog][CMake] Added ability to import static qml plugins with
CMake builds using the new QT5_IMPORT_QML_PLUGINS function.
Task-number: QTBUG-38913
Change-Id: Ib9b9a69654eab13dfbe12d10f5cb28ba3c307d1b
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tools/qmlimportscanner/main.cpp')
-rw-r--r-- | tools/qmlimportscanner/main.cpp | 46 |
1 files changed, 43 insertions, 3 deletions
diff --git a/tools/qmlimportscanner/main.cpp b/tools/qmlimportscanner/main.cpp index 05d1f7fdc0..6d48f6203d 100644 --- a/tools/qmlimportscanner/main.cpp +++ b/tools/qmlimportscanner/main.cpp @@ -496,6 +496,36 @@ QVariantList findQmlImportsRecursively(const QStringList &qmlDirs, const QString return ret; } + +QString generateCmakeIncludeFileContent(const QVariantList &importList) { + // The function assumes that "list" is a QVariantList with 0 or more QVariantMaps, where + // each map contains QString -> QVariant<QString> mappings. This matches with the structure + // that qmake parses for static qml plugin auto imporitng. + // So: [ {"a": "a","b": "b"}, {"c": "c"} ] + QString content; + QTextStream s(&content); + int importsCount = 0; + for (const QVariant &importVariant: importList) { + if (static_cast<QMetaType::Type>(importVariant.type()) == QMetaType::QVariantMap) { + s << QStringLiteral("set(qml_import_scanner_import_") << importsCount + << QStringLiteral(" \""); + + const QMap<QString, QVariant> &importDict = importVariant.toMap(); + for (auto it = importDict.cbegin(); it != importDict.cend(); ++it) { + s << it.key().toUpper() << QLatin1Char(';') + << it.value().toString() << QLatin1Char(';'); + } + s << QStringLiteral("\")\n"); + ++importsCount; + } + } + if (importsCount >= 0) { + content.prepend(QString(QStringLiteral("set(qml_import_scanner_imports_count %1)\n")) + .arg(importsCount)); + } + return content; +} + } // namespace int main(int argc, char *argv[]) @@ -512,6 +542,7 @@ int main(int argc, char *argv[]) QStringList qmlRootPaths; QStringList scanFiles; QStringList qmlImportPaths; + bool generateCmakeContent = false; int i = 1; while (i < args.count()) { @@ -536,6 +567,8 @@ int main(int argc, char *argv[]) if (i >= args.count()) std::cerr << "-importPath requires an argument\n"; argReceiver = &qmlImportPaths; + } else if (arg == QLatin1String("-cmake-output")) { + generateCmakeContent = true; } else { std::cerr << qPrintable(appName) << ": Invalid argument: \"" << qPrintable(arg) << "\"\n"; @@ -562,8 +595,15 @@ int main(int argc, char *argv[]) // Find the imports! QVariantList imports = findQmlImportsRecursively(qmlRootPaths, scanFiles); - // Convert to JSON - QByteArray json = QJsonDocument(QJsonArray::fromVariantList(imports)).toJson(); - std::cout << json.constData() << std::endl; + QByteArray content; + if (generateCmakeContent) { + // Convert to CMake code + content = generateCmakeIncludeFileContent(imports).toUtf8(); + } else { + // Convert to JSON + content = QJsonDocument(QJsonArray::fromVariantList(imports)).toJson(); + } + + std::cout << content.constData() << std::endl; return 0; } |