diff options
author | Fabian Kosmale <fabian.kosmale@qt.io> | 2021-05-25 16:49:42 +0200 |
---|---|---|
committer | Fabian Kosmale <fabian.kosmale@qt.io> | 2021-11-26 15:56:59 +0100 |
commit | 96c1337aef41694c1af4863ad6f0d4d1f961363a (patch) | |
tree | 740efd456b0daa15445a3eab0f16b06b7fa23a47 /src/qml/Qt6QmlMacros.cmake | |
parent | a3deb2ff59e7dce66da09d0287313575b34b291c (diff) |
Support QML type registration split: build system + tools integration
This change addresses the following issue:
- Module A wants to have an optional QML API without depending on
declarative (e.g., because it has a C++ only API usable for Widgets
based applications that normally would not link against declarative).
- Thus we add a module B with all the QML support (type registration,
maybe additional types/functions/image providers).
- Currently, this would require to wrap every type from module A into
QML_FOREIGN manually, adding large amounts of boilerplate.
To solve this, we extend qmltyperegistrar and the CMake API:
- qmltyperegistrar gains a new --extract option to generate a file with
all QML_FOREIGN declarations. More precisely, it generates a header
and source file; the source file includes the header and the moc
generated file.
- We expose this in cmake via a new qt6_generate_foreign_qml_types
function. That function takes two targets, the source library's target
and the QML module's target. It then runs qmltyperegistrar on the
source library, and adds the generated files to the QML module's
target, and adds the proper dependencies between the targets.
The remaining step to achieve the goal of split registration is to
provide the QML registration macros in a separate header.
Task-number: QTBUG-92258
Change-Id: I51c4ef660ca7476b556b1991a6c76bbcad2c69af
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Diffstat (limited to 'src/qml/Qt6QmlMacros.cmake')
-rw-r--r-- | src/qml/Qt6QmlMacros.cmake | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/qml/Qt6QmlMacros.cmake b/src/qml/Qt6QmlMacros.cmake index 008e7485c6..d64abe2d54 100644 --- a/src/qml/Qt6QmlMacros.cmake +++ b/src/qml/Qt6QmlMacros.cmake @@ -1881,6 +1881,48 @@ if(NOT QT_NO_CREATE_VERSIONLESS_FUNCTIONS) endfunction() endif() +function(qt6_generate_foreign_qml_types lib_target qml_target) + + qt6_extract_metatypes(${lib_target}) + get_target_property(target_metatypes_json_file ${lib_target} INTERFACE_QT_META_TYPES_BUILD_FILE) + if (NOT target_metatypes_json_file) + message(FATAL_ERROR "Need target metatypes.json file") + endif() + + set(registration_files_base ${lib_target}_${qml_target}) + set(additional_sources ${registration_files_base}.cpp ${registration_files_base}.h) + + add_custom_command( + OUTPUT + ${additional_sources} + DEPENDS + ${target} + ${target_metatypes_json_file} + ${QT_CMAKE_EXPORT_NAMESPACE}::qmltyperegistrar + COMMAND + ${QT_TOOL_COMMAND_WRAPPER_PATH} + $<TARGET_FILE:${QT_CMAKE_EXPORT_NAMESPACE}::qmltyperegistrar> + "--extract" + -o ${registration_files_base} + ${target_metatypes_json_file} + COMMENT "Generate QML registration code for target ${target}" + ) + + target_sources(${qml_target} PRIVATE ${additional_sources}) + qt6_wrap_cpp(${additional_sources} TARGET ${qml_target}) +endfunction() + + +if(NOT QT_NO_CREATE_VERSIONLESS_FUNCTIONS) + if(QT_DEFAULT_MAJOR_VERSION EQUAL 6) + function(qt_generate_foreign_qml_types) + qt6_generate_foreign_qml_types(${ARGV}) + endfunction() + else() + message(FATAL_ERROR "qt_generate_foreign_qml_types() is only available in Qt 6.") + endif() +endif() + # target: Expected to be the backing target for a qml module. Certain target # properties normally set by qt6_add_qml_module() will be retrieved from this # target. (REQUIRED) |