Skip to content

Commit

Permalink
Use qt_add_plugin
Browse files Browse the repository at this point in the history
This changes the plugin's extension on macOS from .so to .dylib, which
will hopefully cause it to be automatically shipped by macdeployqt.

Closes #8
  • Loading branch information
bjorn committed Jan 23, 2025
1 parent ed9aa8b commit 339369f
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 3 deletions.
12 changes: 9 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,15 @@ add_subdirectory(aseprite/src/dio EXCLUDE_FROM_ALL)
# qaseprite specific setup
# ------------------------

add_library(qaseprite MODULE
qaseprite.cpp
qaseprite.json
if(QT_VERSION_MAJOR EQUAL 5)
include(cmake/Qt5Plugin.cmake)
endif()

qt_add_plugin(qaseprite SHARED CLASS_NAME AsepriteImagePlugin)

target_sources(qaseprite
PRIVATE qaseprite.cpp
qaseprite.json
)

set_target_properties(qaseprite PROPERTIES AUTOMOC ON)
Expand Down
93 changes: 93 additions & 0 deletions cmake/Qt5Plugin.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# This file contains the minimal code to compile a shared Qt plugin with CMake.
# It's based on qt6_add_plugin from Qt6 with all code not related to shared
# plugins (module libraries) removed. Internal functions have the prefix
# _qt_wrap instead of _qt.

function(_qt_wrap_internal_apply_shared_win_prefix_and_suffix target)
if(WIN32 AND NOT MSVC)
set_property(TARGET "${target}" PROPERTY IMPORT_SUFFIX ".a")
set_property(TARGET "${target}" PROPERTY PREFIX "")
set_property(TARGET "${target}" PROPERTY IMPORT_PREFIX "lib")
endif()
endfunction()

function(_qt_wrap_internal_set_up_static_runtime_library target)
if(QT_FEATURE_static_runtime)
if(MSVC)
set_property(TARGET ${target} PROPERTY
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
elseif(MINGW)
get_target_property(target_type ${target} TYPE)
if(target_type STREQUAL "EXECUTABLE")
set(link_option PRIVATE)
else()
set(link_option INTERFACE)
endif()
if(CLANG)
target_link_options(${target} ${link_option} "LINKER:-Bstatic")
else()
target_link_options(${target} ${link_option} "-static")
endif()
endif()
endif()
endfunction()

function(qt5_add_plugin target)
cmake_parse_arguments(PARSE_ARGV 1 arg "SHARED" "" "")

if(NOT arg_SHARED)
message(FATAL_ERROR "Only shared plugins can be built")
endif()

add_library(${target} MODULE ${arg_UNPARSED_ARGUMENTS})
_qt_wrap_internal_set_up_static_runtime_library(${target})
_qt_wrap_internal_apply_shared_win_prefix_and_suffix("${target}")

if(arg_MODULE AND 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()

if(ANDROID)
set_property(TARGET "${target}" PROPERTY SUFFIX "_${CMAKE_ANDROID_ARCH_ABI}.so")
endif()

set_property(TARGET ${target} PROPERTY _qt_expects_finalization TRUE)

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)
set_target_properties(${target}
PROPERTIES
LIBRARY_OUTPUT_NAME "plugins_${arg_PLUGIN_TYPE}_${output_name}"
)
endif()

# Derive the class name from the target name if it's not explicitly specified.
if (NOT arg_CLASS_NAME)
set(plugin_class_name "${target}")
else()
set(plugin_class_name "${arg_CLASS_NAME}")
endif()

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

target_compile_definitions(${target} PRIVATE
QT_PLUGIN
QT_DEPRECATED_WARNINGS
)

if(NOT TARGET qt_wrap_internal_plugins)
add_custom_target(qt_wrap_internal_plugins)
endif()
add_dependencies(qt_wrap_internal_plugins ${target})
endfunction()

function(qt_add_plugin)
qt5_add_plugin(${ARGV})
endfunction()

0 comments on commit 339369f

Please sign in to comment.