cmake: allow client apps to load static plug-ins

Based in part on Kyle Edwards's implementation :
https://codereview.qt-project.org/c/qt/qtbase/+/243731

Example :
```
cmake_minimum_required(VERSION 3.15)
project(foo)

add_executable(foo main.cpp)

find_package(ICU COMPONENTS i18n uc data REQUIRED)
find_package(Qt6 COMPONENTS Core Gui REQUIRED)
target_link_libraries(foo Qt6::Core Qt6::Gui)

qt_import_plugins(foo
    INCLUDE
        Qt6::qxcb
    EXCLUDE
        Qt6::qgtk3 Qt6::qeglfs-kms-integration Qt6::qjpeg
)
```

Change-Id: If7736c42f669f7d7f43052cae59c28fc7fcb4156
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
This commit is contained in:
Jean-Michaël Celerier 2019-06-07 09:13:31 +02:00 committed by Jean-Michaël Celerier
parent 12b73cba89
commit 5769e1a2f6
4 changed files with 94 additions and 18 deletions

View File

@ -1465,7 +1465,7 @@ function(add_qt_plugin target)
"${arg_ARCHIVE_INSTALL_DIRECTORY}" "${arg_TYPE}" "${arg_ARCHIVE_INSTALL_DIRECTORY}" "${arg_TYPE}"
"${INSTALL_LIBDIR}/${arg_TYPE}" archive_install_directory) "${INSTALL_LIBDIR}/${arg_TYPE}" archive_install_directory)
if(arg_STATIC) if(arg_STATIC OR NOT BUILD_SHARED_LIBS)
add_library("${target}" STATIC) add_library("${target}" STATIC)
else() else()
add_library("${target}" MODULE) add_library("${target}" MODULE)

40
cmake/QtPlugins.cmake.in Normal file
View File

@ -0,0 +1,40 @@
@QT_MODULE_PLUGIN_INCLUDES@
if(NOT @BUILD_SHARED_LIBS@)
set(_module_target "@INSTALL_CMAKE_NAMESPACE@::@QT_MODULE@")
set(_default_plugins_genex "$<GENEX_EVAL:$<TARGET_PROPERTY:QT_DEFAULT_PLUGINS>>")
set(_manual_plugins_genex "$<GENEX_EVAL:$<TARGET_PROPERTY:QT_PLUGINS>>")
set(_no_plugins_genex "$<GENEX_EVAL:$<TARGET_PROPERTY:QT_NO_PLUGINS>>")
foreach(target @qt_plugins@)
set(_plugin_target "@INSTALL_CMAKE_NAMESPACE@::${target}")
get_target_property(_classname "${_plugin_target}" QT_PLUGIN_CLASS_NAME)
if(NOT _classname)
message("Warning: plugin ${_plugin_target} has no class name, skipping.")
continue()
endif()
set(_user_specified_genex "$<IN_LIST:${_plugin_target},${_manual_plugins_genex}>")
string(CONCAT _plugin_condition
"$<BOOL:$<OR:"
# Add this plugin if it\'s in the list of manual plugins or plugins for the type
"${_user_specified_genex},"
# Add this plugin if the default plugins haven't been disabled, the module of the plug-in
# is either empty or equal to the module name, and the user hasn't blacklisted it
"$<AND:"
"${_default_plugins_genex},"
"$<NOT:$<IN_LIST:${_plugin_target},${_no_plugins_genex}>>"
">"
">>"
)
set(_plugin_genex "$<${_plugin_condition}:${_plugin_target}>")
target_link_libraries(${_module_target} INTERFACE "${_plugin_genex}")
file(GENERATE
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/qt_@QT_MODULE@_${target}.cpp"
CONTENT "#include <QtPlugin>\nQ_IMPORT_PLUGIN(${_classname})"
)
target_sources(${_module_target} INTERFACE "$<${_plugin_condition}:${CMAKE_CURRENT_BINARY_DIR}/qt_@QT_MODULE@_${target}.cpp>")
endforeach()
endif()

View File

@ -239,26 +239,26 @@ endfunction()
# the plug-in target files. # the plug-in target files.
function(qt_internal_create_plugins_files) function(qt_internal_create_plugins_files)
message("Generating Plugins files for ${QT_KNOWN_MODULES}...") message("Generating Plugins files for ${QT_KNOWN_MODULES}...")
foreach (target ${QT_KNOWN_MODULES}) foreach (QT_MODULE ${QT_KNOWN_MODULES})
qt_path_join(config_build_dir ${QT_CONFIG_BUILD_DIR} ${INSTALL_CMAKE_NAMESPACE}${target}) qt_path_join(config_build_dir ${QT_CONFIG_BUILD_DIR} ${INSTALL_CMAKE_NAMESPACE}${QT_MODULE})
qt_path_join(config_install_dir ${QT_CONFIG_INSTALL_DIR} ${INSTALL_CMAKE_NAMESPACE}${target}) qt_path_join(config_install_dir ${QT_CONFIG_INSTALL_DIR} ${INSTALL_CMAKE_NAMESPACE}${QT_MODULE})
set(QT_MODULE_PLUGIN_INCLUDES "")
set(_plugins_file "") get_target_property(qt_plugins "${QT_MODULE}" QT_PLUGINS)
get_target_property(qt_plugins "${target}" QT_PLUGINS)
if(qt_plugins) if(qt_plugins)
foreach (plugin ${qt_plugins}) foreach (pluginTarget ${qt_plugins})
set(_plugins_file "${_plugins_file}include(\"\${CMAKE_CURRENT_LIST_DIR}/${plugin}Config.cmake\")\n") set(QT_MODULE_PLUGIN_INCLUDES "${QT_MODULE_PLUGIN_INCLUDES}include(\"\${CMAKE_CURRENT_LIST_DIR}/${pluginTarget}Config.cmake\")\n")
endforeach() endforeach()
configure_file(
if(NOT ("x${_plugins_file}" STREQUAL "x")) "${QT_CMAKE_DIR}/QtPlugins.cmake.in"
file(WRITE "${config_build_dir}/${INSTALL_CMAKE_NAMESPACE}${target}Plugins.cmake" "${_plugins_file}") "${config_build_dir}/${INSTALL_CMAKE_NAMESPACE}${QT_MODULE}Plugins.cmake"
@ONLY
qt_install(FILES )
"${config_build_dir}/${INSTALL_CMAKE_NAMESPACE}${target}Plugins.cmake" qt_install(FILES
DESTINATION "${config_install_dir}" "${config_build_dir}/${INSTALL_CMAKE_NAMESPACE}${QT_MODULE}Plugins.cmake"
COMPONENT Devel DESTINATION "${config_install_dir}"
) COMPONENT Devel
endif() )
endif() endif()
endforeach() endforeach()
endfunction() endfunction()

View File

@ -408,3 +408,39 @@ function(add_qt_gui_executable target)
endif() endif()
endfunction() endfunction()
macro(_qt_import_plugin target plugin)
get_target_property(plugin_class_name "${plugin}" QT_PLUGIN_CLASS_NAME)
if(plugin_class_name)
set_property(TARGET "${target}" APPEND PROPERTY QT_PLUGINS "${plugin}")
# TODO mark it for installation
# TODO also in shared builds
endif()
endmacro()
# This function is used to indicate which plug-ins are going to be
# used by a given target.
# This allows both automatic static linking, and automatic installation of relevant
# plug-ins.
# Options :
# NO_DEFAULT: won't link against any plug-in by default for that target, e.g. no platform plug-in.
# INCLUDE: list of additional plug-ins to be linked against.
# EXCLUDE: list of plug-ins to be removed from the default set.
# TODO : support qml plug-ins.
function(qt_import_plugins target)
cmake_parse_arguments(arg "NO_DEFAULT" "" "INCLUDE;EXCLUDE" ${ARGN})
if(${arg_NO_DEFAULT})
set_target_properties(${target} PROPERTIES QT_DEFAULT_PLUGINS 0)
else()
set_target_properties(${target} PROPERTIES QT_DEFAULT_PLUGINS 1)
endif()
foreach(plugin ${arg_INCLUDE})
_qt_import_plugin("${target}" "${plugin}")
endforeach()
foreach(plugin ${arg_EXCLUDE})
set_property(TARGET "${target}" APPEND PROPERTY QT_NO_PLUGINS "${plugin}")
endforeach()
endfunction()