Skip to content

Commit d2ba81f

Browse files
authored
Use qt_add_plugin (#14)
Among other things that are probably more appropriate for Qt plugins, this changes the plugin's extension on macOS from .so to .dylib, which will hopefully cause it to be automatically shipped by macdeployqt. Based on @Nerixyz's work in jurplel/QtApng#4. Changed target name from qaseprite to AsepriteImagePlugin to match the class name, since the CLASS_NAME parameter wasn't working correctly with qt5_add_plugin. Closes #8
1 parent ed9aa8b commit d2ba81f

File tree

3 files changed

+112
-9
lines changed

3 files changed

+112
-9
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ jobs:
109109
uses: actions/upload-artifact@v4
110110
with:
111111
name: macos-${{ matrix.macos_version }}-qt-${{ matrix.qt_version }}
112-
path: build/libqaseprite.so
112+
path: build/libqaseprite.dylib
113113

114114
windows:
115115
name: Windows (Qt ${{ matrix.qt_version }}, ${{ matrix.qt_arch }})
@@ -133,13 +133,13 @@ jobs:
133133
qt_arch: win32_mingw81
134134
qt_tools: tools_mingw,qt.tools.win32_mingw810
135135
cmake_args: -G "MinGW Makefiles"
136-
artifact_path: build/libqaseprite.dll
136+
artifact_path: build/qaseprite.dll
137137
- windows_version: 2022
138138
qt_version: 6.8.1
139139
qt_arch: win64_mingw
140140
qt_tools: tools_mingw1310,qt.tools.win64_mingw1310
141141
cmake_args: -G "MinGW Makefiles"
142-
artifact_path: build/libqaseprite.dll
142+
artifact_path: build/qaseprite.dll
143143

144144
defaults:
145145
run:

CMakeLists.txt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,23 @@ add_subdirectory(aseprite/src/dio EXCLUDE_FROM_ALL)
280280
# qaseprite specific setup
281281
# ------------------------
282282

283-
add_library(qaseprite MODULE
284-
qaseprite.cpp
285-
qaseprite.json
283+
if(QT_VERSION_MAJOR EQUAL 5)
284+
include(cmake/Qt5Plugin.cmake)
285+
endif()
286+
287+
qt_add_plugin(AsepriteImagePlugin SHARED)
288+
289+
# For Qt 6.0-6.4, qt_add_plugin didn't support adding the sources directly,
290+
# hence we add them using target_sources instead.
291+
target_sources(AsepriteImagePlugin
292+
PRIVATE qaseprite.cpp
293+
qaseprite.json
286294
)
287295

288-
set_target_properties(qaseprite PROPERTIES AUTOMOC ON)
289-
target_link_libraries(qaseprite
296+
set_target_properties(AsepriteImagePlugin PROPERTIES
297+
AUTOMOC ON
298+
LIBRARY_OUTPUT_NAME qaseprite)
299+
target_link_libraries(AsepriteImagePlugin
290300
PRIVATE Qt${QT_VERSION_MAJOR}::Gui
291301
laf-base
292302
dio-lib
@@ -318,6 +328,6 @@ if(NOT QT_PLUGIN_PATH)
318328
endif()
319329

320330
install(
321-
TARGETS qaseprite
331+
TARGETS AsepriteImagePlugin
322332
RUNTIME DESTINATION "${QT_PLUGIN_PATH}/imageformats"
323333
LIBRARY DESTINATION "${QT_PLUGIN_PATH}/imageformats")

cmake/Qt5Plugin.cmake

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)