|
| 1 | +# This file contains the minimal code to compile a shared Qt plugin with CMake. |
| 2 | +# It's based on qt6_add_plugin from Qt6 with all code not related to shared |
| 3 | +# plugins (module libraries) removed. Internal functions have the prefix |
| 4 | +# _qt_wrap instead of _qt. |
| 5 | + |
| 6 | +function(_qt_wrap_internal_apply_shared_win_prefix_and_suffix target) |
| 7 | + if(WIN32 AND NOT MSVC) |
| 8 | + set_property(TARGET "${target}" PROPERTY IMPORT_SUFFIX ".a") |
| 9 | + set_property(TARGET "${target}" PROPERTY PREFIX "") |
| 10 | + set_property(TARGET "${target}" PROPERTY IMPORT_PREFIX "lib") |
| 11 | + endif() |
| 12 | +endfunction() |
| 13 | + |
| 14 | +function(_qt_wrap_internal_set_up_static_runtime_library target) |
| 15 | + if(QT_FEATURE_static_runtime) |
| 16 | + if(MSVC) |
| 17 | + set_property(TARGET ${target} PROPERTY |
| 18 | + MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>") |
| 19 | + elseif(MINGW) |
| 20 | + get_target_property(target_type ${target} TYPE) |
| 21 | + if(target_type STREQUAL "EXECUTABLE") |
| 22 | + set(link_option PRIVATE) |
| 23 | + else() |
| 24 | + set(link_option INTERFACE) |
| 25 | + endif() |
| 26 | + if(CLANG) |
| 27 | + target_link_options(${target} ${link_option} "LINKER:-Bstatic") |
| 28 | + else() |
| 29 | + target_link_options(${target} ${link_option} "-static") |
| 30 | + endif() |
| 31 | + endif() |
| 32 | + endif() |
| 33 | +endfunction() |
| 34 | + |
| 35 | +function(qt5_add_plugin target) |
| 36 | + cmake_parse_arguments(PARSE_ARGV 1 arg "SHARED" "" "") |
| 37 | + |
| 38 | + if(NOT arg_SHARED) |
| 39 | + message(FATAL_ERROR "Only shared plugins can be built") |
| 40 | + endif() |
| 41 | + |
| 42 | + add_library(${target} MODULE ${arg_UNPARSED_ARGUMENTS}) |
| 43 | + _qt_wrap_internal_set_up_static_runtime_library(${target}) |
| 44 | + _qt_wrap_internal_apply_shared_win_prefix_and_suffix("${target}") |
| 45 | + |
| 46 | + if(APPLE) |
| 47 | + # CMake defaults to using .so extensions for loadable modules, aka plugins, |
| 48 | + # but Qt plugins are actually suffixed with .dylib. |
| 49 | + set_property(TARGET "${target}" PROPERTY SUFFIX ".dylib") |
| 50 | + endif() |
| 51 | + |
| 52 | + if(ANDROID) |
| 53 | + set_property(TARGET "${target}" PROPERTY SUFFIX "_${CMAKE_ANDROID_ARCH_ABI}.so") |
| 54 | + endif() |
| 55 | + |
| 56 | + set_property(TARGET ${target} PROPERTY _qt_expects_finalization TRUE) |
| 57 | + |
| 58 | + set(output_name ${target}) |
| 59 | + if (arg_OUTPUT_NAME) |
| 60 | + set(output_name ${arg_OUTPUT_NAME}) |
| 61 | + endif() |
| 62 | + set_property(TARGET "${target}" PROPERTY OUTPUT_NAME "${output_name}") |
| 63 | + |
| 64 | + if (ANDROID) |
| 65 | + set_target_properties(${target} |
| 66 | + PROPERTIES |
| 67 | + LIBRARY_OUTPUT_NAME "plugins_${arg_PLUGIN_TYPE}_${output_name}" |
| 68 | + ) |
| 69 | + endif() |
| 70 | + |
| 71 | + # Derive the class name from the target name if it's not explicitly specified. |
| 72 | + if (NOT arg_CLASS_NAME) |
| 73 | + set(plugin_class_name "${target}") |
| 74 | + else() |
| 75 | + set(plugin_class_name "${arg_CLASS_NAME}") |
| 76 | + endif() |
| 77 | + |
| 78 | + set_target_properties(${target} PROPERTIES QT_PLUGIN_CLASS_NAME "${plugin_class_name}") |
| 79 | + |
| 80 | + target_compile_definitions(${target} PRIVATE |
| 81 | + QT_PLUGIN |
| 82 | + QT_DEPRECATED_WARNINGS |
| 83 | + ) |
| 84 | + |
| 85 | + if(NOT TARGET qt_wrap_internal_plugins) |
| 86 | + add_custom_target(qt_wrap_internal_plugins) |
| 87 | + endif() |
| 88 | + add_dependencies(qt_wrap_internal_plugins ${target}) |
| 89 | +endfunction() |
| 90 | + |
| 91 | +function(qt_add_plugin) |
| 92 | + qt5_add_plugin(${ARGV}) |
| 93 | +endfunction() |
0 commit comments