From 6fbeef4c6b2323fc4b7856520c0f38f8139c9c54 Mon Sep 17 00:00:00 2001 From: Leander Beernaert Date: Tue, 21 Apr 2020 13:28:57 +0200 Subject: CMake: Add qt6_add_plugin public API This patch adds a publicly callable qt6_add_plugin() API to create plugins. This API is meant to cover cases such as the plugandpaint example. This patch also renames qt_add_plugin to qt_internal_add_plugin in order to avoid clashes with the public API. To avoid breaking the existing projects, a compatibility wrapper function is enabled by default unless QT_DISABLE_QT_ADD_PLUGIN_COMPATIBILITY is specified. Fixes: QTBUG-82961 Change-Id: If5b564a8406c90434f1bdad0b8df76d3e6626b5f Reviewed-by: Alexandru Croitor --- cmake/QtBuild.cmake | 6 +-- .../QtBuildInternals/QtBuildInternalsConfig.cmake | 1 + examples/widgets/tools/CMakeLists.txt | 4 +- .../plugins/basictools/.prev_CMakeLists.txt | 3 +- .../plugandpaint/plugins/basictools/CMakeLists.txt | 3 +- .../plugins/extrafilters/.prev_CMakeLists.txt | 3 +- .../plugins/extrafilters/CMakeLists.txt | 3 +- src/corelib/Qt6CoreMacros.cmake | 54 ++++++++++++++++++++++ util/cmake/pro2cmake.py | 8 +++- 9 files changed, 73 insertions(+), 12 deletions(-) diff --git a/cmake/QtBuild.cmake b/cmake/QtBuild.cmake index 2ade160779..11f00e5222 100644 --- a/cmake/QtBuild.cmake +++ b/cmake/QtBuild.cmake @@ -2634,7 +2634,7 @@ endfunction() function(qt_internal_check_directory_or_type name dir type default result_var) if ("x${dir}" STREQUAL x) if("x${type}" STREQUAL x) - message(FATAL_ERROR "qt_add_plugin called without setting either TYPE or ${name}.") + message(FATAL_ERROR "qt_internal_add_plugin called without setting either TYPE or ${name}.") endif() set(${result_var} "${default}" PARENT_SCOPE) else() @@ -2686,12 +2686,12 @@ set(__qt_add_plugin_multi_args # This is the main entry point for defining Qt plugins. # A CMake target is created with the given target. The TYPE parameter is needed to place the # plugin into the correct plugins/ sub-directory. -function(qt_add_plugin target) +function(qt_internal_add_plugin target) qt_internal_module_info(module "${target}") qt_internal_set_qt_known_plugins("${QT_KNOWN_PLUGINS}" "${target}") - qt_parse_all_arguments(arg "qt_add_plugin" + qt_parse_all_arguments(arg "qt_internal_add_plugin" "${__qt_add_plugin_optional_args};SKIP_INSTALL" "${__qt_add_plugin_single_args}" "${__qt_add_plugin_multi_args}" diff --git a/cmake/QtBuildInternals/QtBuildInternalsConfig.cmake b/cmake/QtBuildInternals/QtBuildInternalsConfig.cmake index d3a2a6012e..b700dc1843 100644 --- a/cmake/QtBuildInternals/QtBuildInternalsConfig.cmake +++ b/cmake/QtBuildInternals/QtBuildInternalsConfig.cmake @@ -282,6 +282,7 @@ macro(qt_examples_build_begin) # annotate where each example is installed to, to be able to derive a relative rpath, and it # seems there's no way to query such information from CMake itself. set(CMAKE_INSTALL_RPATH "${_default_install_rpath}") + set(QT_DISABLE_QT_ADD_PLUGIN_COMPATIBILITY TRUE) endmacro() macro(qt_examples_build_end) diff --git a/examples/widgets/tools/CMakeLists.txt b/examples/widgets/tools/CMakeLists.txt index 3346cafe09..80c8f43007 100644 --- a/examples/widgets/tools/CMakeLists.txt +++ b/examples/widgets/tools/CMakeLists.txt @@ -15,7 +15,5 @@ add_subdirectory(undoframework) if(QT_FEATURE_library) # special case add_subdirectory(echoplugin) - # FIXME: Currently broken # special case - # Fails to link (ld: error: undefined symbol: qt_static_plugin_BasicToolsPlugin()) # special case - #add_subdirectory(plugandpaint) # special case + add_subdirectory(plugandpaint) # special case endif() diff --git a/examples/widgets/tools/plugandpaint/plugins/basictools/.prev_CMakeLists.txt b/examples/widgets/tools/plugandpaint/plugins/basictools/.prev_CMakeLists.txt index fefa30e2a4..76474c282b 100644 --- a/examples/widgets/tools/plugandpaint/plugins/basictools/.prev_CMakeLists.txt +++ b/examples/widgets/tools/plugandpaint/plugins/basictools/.prev_CMakeLists.txt @@ -15,7 +15,8 @@ find_package(Qt6 COMPONENTS Core) find_package(Qt6 COMPONENTS Gui) find_package(Qt6 COMPONENTS Widgets) -add_library(pnp_basictools MODULE +qt6_add_plugin(pnp_basictools STATIC) +target_sources(pnp_basictools PRIVATE basictoolsplugin.cpp basictoolsplugin.h ) target_include_directories(pnp_basictools PUBLIC diff --git a/examples/widgets/tools/plugandpaint/plugins/basictools/CMakeLists.txt b/examples/widgets/tools/plugandpaint/plugins/basictools/CMakeLists.txt index fefa30e2a4..b0a752718f 100644 --- a/examples/widgets/tools/plugandpaint/plugins/basictools/CMakeLists.txt +++ b/examples/widgets/tools/plugandpaint/plugins/basictools/CMakeLists.txt @@ -15,7 +15,8 @@ find_package(Qt6 COMPONENTS Core) find_package(Qt6 COMPONENTS Gui) find_package(Qt6 COMPONENTS Widgets) -add_library(pnp_basictools MODULE +qt_add_plugin(pnp_basictools STATIC) +target_sources(pnp_basictools PRIVATE basictoolsplugin.cpp basictoolsplugin.h ) target_include_directories(pnp_basictools PUBLIC diff --git a/examples/widgets/tools/plugandpaint/plugins/extrafilters/.prev_CMakeLists.txt b/examples/widgets/tools/plugandpaint/plugins/extrafilters/.prev_CMakeLists.txt index 2f689893bb..57f37843b8 100644 --- a/examples/widgets/tools/plugandpaint/plugins/extrafilters/.prev_CMakeLists.txt +++ b/examples/widgets/tools/plugandpaint/plugins/extrafilters/.prev_CMakeLists.txt @@ -15,7 +15,8 @@ find_package(Qt6 COMPONENTS Core) find_package(Qt6 COMPONENTS Gui) find_package(Qt6 COMPONENTS Widgets) -add_library(pnp_extrafilters MODULE +qt_add_plugin(pnp_extrafilters) +target_sources(pnp_extrafilters PRIVATE extrafiltersplugin.cpp extrafiltersplugin.h ) target_include_directories(pnp_extrafilters PUBLIC diff --git a/examples/widgets/tools/plugandpaint/plugins/extrafilters/CMakeLists.txt b/examples/widgets/tools/plugandpaint/plugins/extrafilters/CMakeLists.txt index 2f689893bb..57f37843b8 100644 --- a/examples/widgets/tools/plugandpaint/plugins/extrafilters/CMakeLists.txt +++ b/examples/widgets/tools/plugandpaint/plugins/extrafilters/CMakeLists.txt @@ -15,7 +15,8 @@ find_package(Qt6 COMPONENTS Core) find_package(Qt6 COMPONENTS Gui) find_package(Qt6 COMPONENTS Widgets) -add_library(pnp_extrafilters MODULE +qt_add_plugin(pnp_extrafilters) +target_sources(pnp_extrafilters PRIVATE extrafiltersplugin.cpp extrafiltersplugin.h ) target_include_directories(pnp_extrafilters PUBLIC diff --git a/src/corelib/Qt6CoreMacros.cmake b/src/corelib/Qt6CoreMacros.cmake index 29b3239215..2e7022c116 100644 --- a/src/corelib/Qt6CoreMacros.cmake +++ b/src/corelib/Qt6CoreMacros.cmake @@ -1135,3 +1135,57 @@ function(QT6_PROCESS_RESOURCE target resourceName) set(${rcc_OUTPUT_TARGETS} "${output_targets}" PARENT_SCOPE) endif() endfunction() + +function(qt6_add_plugin target) + cmake_parse_arguments(arg + "STATIC" + "OUTPUT_NAME" + "" + ${ARGN} + ) + if (arg_STATIC) + add_library(${target} STATIC) + else() + add_library(${target} MODULE) + if(APPLE) + # CMake defaults to using .so extensions for loadable modules, aka plugins, + # but Qt plugins are actually suffixed with .dylib. + set_property(TARGET "${target}" PROPERTY SUFFIX ".dylib") + endif() + endif() + + set(output_name ${target}) + if (arg_OUTPUT_NAME) + set(output_name ${arg_OUTPUT_NAME}) + endif() + set_property(TARGET "${target}" PROPERTY OUTPUT_NAME "${output_name}") + + if (ANDROID) + qt_android_apply_arch_suffix("${target}") + set_target_properties(${target} + PROPERTIES + LIBRARY_OUTPUT_NAME "plugins_${arg_TYPE}_${output_name}" + ) + endif() + + set(static_plugin_define "") + if (arg_STATIC) + set(static_plugin_define "QT_STATICPLUGIN") + endif() + target_compile_definitions(${target} PRIVATE + QT_PLUGIN + QT_DEPRECATED_WARNINGS + ${static_plugin_define} + ) +endfunction() + +if(NOT QT_NO_CREATE_VERSIONLESS_FUNCTIONS) + function(qt_add_plugin) + if (NOT DEFINED QT_DISABLE_QT_ADD_PLUGIN_COMPATIBILITY + OR NOT QT_DISABLE_QT_ADD_PLUGIN_COMPATIBILITY) + qt_internal_add_plugin(${ARGV}) + else() + qt6_add_plugin(${ARGV}) + endif() + endfunction() +endif() diff --git a/util/cmake/pro2cmake.py b/util/cmake/pro2cmake.py index 9e183a4b13..4d7c6960cf 100755 --- a/util/cmake/pro2cmake.py +++ b/util/cmake/pro2cmake.py @@ -339,7 +339,7 @@ def set_up_cmake_api_calls(): api[2]["qt_extend_target"] = "qt_extend_target" api[2]["qt_add_module"] = "qt_add_module" - api[2]["qt_add_plugin"] = "qt_add_plugin" + api[2]["qt_add_plugin"] = "qt_internal_add_plugin" api[2]["qt_add_tool"] = "qt_add_tool" api[2]["qt_add_test"] = "qt_add_test" api[2]["qt_add_test_helper"] = "qt_add_test_helper" @@ -3396,7 +3396,11 @@ def write_example( add_target += " INSTALL_LOCATION ${INSTALL_EXAMPLEDIR}\n)\n\n" add_target += f"target_sources({binary_name} PRIVATE" else: - add_target = f"add_library({binary_name} MODULE" + add_target = f"qt_add_plugin({binary_name}" + if "static" in scope.get("CONFIG"): + add_target += " STATIC" + add_target += ")\n" + add_target += f"target_sources({binary_name} PRIVATE" else: add_target = f'add_{"qt_gui_" if gui else ""}executable({binary_name}' -- cgit v1.2.3