Skip to content

Commit

Permalink
CMake: Set the plugin class name for qml plugins
Browse files Browse the repository at this point in the history
This is needed for a change in qtdeclarative to allow building the
Q_IMPORT_PLUGIN-containing object library initializer of a qml plugin.

Show an error if the qml plugin has no class name, it's needed for
plugin initialization so it's mandatory to have a class name.

Show an error if a class name is not found when computing the import
macro for a plugin (both for a regular qt plugin and a qml plugin).

When querying for the class name of a target, query both a Qt6::
prefixed target as well as a non-prefixed one, with the Qt one
taking precedence.
This is to allow querying the class name of user project created qml
plugins.

Currently regular qt user plugins don't use the object library
initializer approach. This will likely be revisited in the future.

Pick-to: 6.2
Task-number: QTBUG-92933
Change-Id: I46417471a7d8b49651e6821f7b28e7a9d44c2557
Reviewed-by: Joerg Bornemann <[email protected]>
  • Loading branch information
alcroito committed Jun 17, 2021
1 parent 103c1bc commit 602d26c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
26 changes: 24 additions & 2 deletions cmake/QtPublicPluginHelpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,30 @@ endfunction()

# Generates C++ import macro source code for given plugin
function(__qt_internal_get_plugin_import_macro plugin_target out_var)
set(plugin_target_versioned "${QT_CMAKE_EXPORT_NAMESPACE}::${plugin_target}")
get_target_property(class_name "${plugin_target_versioned}" QT_PLUGIN_CLASS_NAME)
set(plugin_target_prefixed "${QT_CMAKE_EXPORT_NAMESPACE}::${plugin_target}")

# Query the class name of plugin targets prefixed with a Qt namespace and without, this is
# needed to support plugin object initializers created by user projects.
set(class_name"")
set(class_name_prefixed "")

if(TARGET ${plugin_target})
get_target_property(class_name "${plugin_target}" QT_PLUGIN_CLASS_NAME)
endif()

if(TARGET ${plugin_target_prefixed})
get_target_property(class_name_prefixed "${plugin_target_prefixed}" QT_PLUGIN_CLASS_NAME)
endif()

if(NOT class_name AND NOT class_name_prefixed)
message(FATAL_ERROR "No QT_PLUGIN_CLASS_NAME value on target: '${plugin_target}'")
endif()

# Qt prefixed target takes priority.
if(class_name_prefixed)
set(class_name "${class_name_prefixed}")
endif()

set(${out_var} "Q_IMPORT_PLUGIN(${class_name})" PARENT_SCOPE)
endfunction()

Expand Down
10 changes: 9 additions & 1 deletion src/corelib/Qt6CoreMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1807,15 +1807,23 @@ function(qt6_add_plugin target)
endif()

# Derive the class name from the target name if it's not explicitly specified.
# Don't set it for qml plugins though.
set(plugin_class_name "")
if (NOT "${arg_TYPE}" STREQUAL "qml_plugin")
if (NOT arg_CLASS_NAME)
set(plugin_class_name "${target}")
else()
set(plugin_class_name "${arg_CLASS_NAME}")
endif()
else()
# Make sure to set any passed-in class name for qml plugins as well, because it's used for
# building the qml plugin foo_init object libraries.
if(arg_CLASS_NAME)
set(plugin_class_name "${arg_CLASS_NAME}")
else()
message(FATAL_ERROR "Qml plugin target has no CLASS_NAME specified: '${target}'")
endif()
endif()

set_target_properties(${target} PROPERTIES QT_PLUGIN_CLASS_NAME "${plugin_class_name}")

target_compile_definitions(${target} PRIVATE
Expand Down

0 comments on commit 602d26c

Please sign in to comment.