Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/scripts/packaging/appimage/make-appimage.sh
4251 views
1
#!/usr/bin/env bash
2
3
# SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <[email protected]>
4
# SPDX-License-Identifier: CC-BY-NC-ND-4.0
5
6
SCRIPTDIR=$(dirname "${BASH_SOURCE[0]}")
7
8
function retry_command {
9
# Package servers tend to be unreliable at times..
10
# Retry a bunch of times.
11
local RETRIES=10
12
13
for i in $(seq 1 "$RETRIES"); do
14
"$@" && break
15
if [ "$i" == "$RETRIES" ]; then
16
echo "Command \"$@\" failed after ${RETRIES} retries."
17
exit 1
18
fi
19
done
20
}
21
22
if [ "$#" -ne 4 ]; then
23
echo "Syntax: $0 <path to duckstation directory> <path to build directory> <deps prefix> <output name>"
24
exit 1
25
fi
26
27
ROOTDIR=$1
28
BUILDDIR=$2
29
DEPSDIR=$3
30
NAME=$4
31
32
BINARY=duckstation-qt
33
APPDIRNAME=DuckStation.AppDir
34
STRIP=strip
35
36
declare -a MANUAL_LIBS=(
37
"libz.so.1"
38
"libavcodec.so.61"
39
"libavformat.so.61"
40
"libavutil.so.59"
41
"libswscale.so.8"
42
"libswresample.so.5"
43
"libdiscord-rpc.so"
44
"libfreetype.so.6"
45
"libshaderc_ds.so"
46
"libspirv-cross-c-shared.so.0"
47
)
48
49
declare -a MANUAL_QT_LIBS=(
50
"libQt6WaylandEglClientHwIntegration.so.6"
51
)
52
53
declare -a MANUAL_QT_PLUGINS=(
54
"wayland-decoration-client"
55
"wayland-graphics-integration-client"
56
"wayland-shell-integration"
57
)
58
59
declare -a REMOVE_LIBS=(
60
'libwayland-client.so*'
61
'libwayland-cursor.so*'
62
'libwayland-egl.so*'
63
)
64
65
set -e
66
67
LINUXDEPLOY=./linuxdeploy-x86_64
68
LINUXDEPLOY_PLUGIN_QT=./linuxdeploy-plugin-qt-x86_64
69
APPIMAGETOOL=./appimagetool-x86_64
70
APPIMAGERUNTIME=./runtime-x86_64
71
PATCHELF=patchelf
72
73
if [ ! -f "$LINUXDEPLOY" ]; then
74
retry_command wget -O "$LINUXDEPLOY" https://github.com/stenzek/duckstation-ext-qt-minimal/releases/download/linux/linuxdeploy-x86_64.AppImage
75
chmod +x "$LINUXDEPLOY"
76
fi
77
78
if [ ! -f "$LINUXDEPLOY_PLUGIN_QT" ]; then
79
retry_command wget -O "$LINUXDEPLOY_PLUGIN_QT" https://github.com/stenzek/duckstation-ext-qt-minimal/releases/download/linux/linuxdeploy-plugin-qt-x86_64.AppImage
80
chmod +x "$LINUXDEPLOY_PLUGIN_QT"
81
fi
82
83
if [ ! -f "$APPIMAGETOOL" ]; then
84
retry_command wget -O "$APPIMAGETOOL" https://github.com/stenzek/duckstation-ext-qt-minimal/releases/download/linux/appimagetool-x86_64.AppImage
85
chmod +x "$APPIMAGETOOL"
86
fi
87
88
if [ ! -f "$APPIMAGERUNTIME" ]; then
89
retry_command wget -O "$APPIMAGERUNTIME" https://github.com/stenzek/type2-runtime/releases/download/continuous/runtime-x86_64
90
fi
91
92
OUTDIR=$(realpath "./$APPDIRNAME")
93
rm -fr "$OUTDIR"
94
95
echo "Locating extra libraries..."
96
EXTRA_LIBS_ARGS=()
97
for lib in "${MANUAL_LIBS[@]}"; do
98
srcpath=$(find "$DEPSDIR" -name "$lib")
99
if [ ! -f "$srcpath" ]; then
100
echo "Missinge extra library $lib. Exiting."
101
exit 1
102
fi
103
104
echo "Found $lib at $srcpath."
105
EXTRA_LIBS_ARGS+=("--library=$srcpath")
106
done
107
108
# Why the nastyness? linuxdeploy strips our main binary, and there's no option to turn it off.
109
# It also doesn't strip the Qt libs. We can't strip them after running linuxdeploy, because
110
# patchelf corrupts the libraries (but they still work), but patchelf+strip makes them crash
111
# on load. So, make a backup copy, strip the original (since that's where linuxdeploy finds
112
# the libs to copy), then swap them back after we're done.
113
# Isn't Linux packaging amazing?
114
115
rm -fr "$DEPSDIR.bak"
116
cp -a "$DEPSDIR" "$DEPSDIR.bak"
117
IFS="
118
"
119
for i in $(find "$DEPSDIR" -iname '*.so'); do
120
echo "Stripping deps library ${i}"
121
strip "$i"
122
done
123
124
echo "Running linuxdeploy to create AppDir..."
125
EXTRA_QT_PLUGINS="core;gui;svg;waylandclient;widgets;xcbqpa" \
126
EXTRA_PLATFORM_PLUGINS="libqwayland-egl.so;libqwayland-generic.so" \
127
DEPLOY_PLATFORM_THEMES="1" \
128
QMAKE="$DEPSDIR/bin/qmake" \
129
NO_STRIP="1" \
130
$LINUXDEPLOY --plugin qt --appdir="$OUTDIR" --executable="$BUILDDIR/bin/duckstation-qt" ${EXTRA_LIBS_ARGS[@]} \
131
--desktop-file="$ROOTDIR/scripts/packaging/org.duckstation.DuckStation.desktop" \
132
--icon-file="$ROOTDIR/scripts/packaging/org.duckstation.DuckStation.png" \
133
134
echo "Copying resources into AppDir..."
135
cp -a "$BUILDDIR/bin/resources" "$OUTDIR/usr/bin"
136
137
# LinuxDeploy's Qt plugin doesn't include Wayland support. So manually copy in the additional Wayland libraries.
138
echo "Copying Qt Wayland libraries..."
139
for lib in "${MANUAL_QT_LIBS[@]}"; do
140
srcpath="$DEPSDIR/lib/$lib"
141
dstpath="$OUTDIR/usr/lib/$lib"
142
echo " $srcpath -> $dstpath"
143
cp "$srcpath" "$dstpath"
144
$PATCHELF --set-rpath '$ORIGIN' "$dstpath"
145
done
146
147
# .. and plugins.
148
echo "Copying Qt Wayland plugins..."
149
for GROUP in "${MANUAL_QT_PLUGINS[@]}"; do
150
srcpath="$DEPSDIR/plugins/$GROUP"
151
dstpath="$OUTDIR/usr/plugins/$GROUP"
152
echo " $srcpath -> $dstpath"
153
mkdir -p "$dstpath"
154
155
for srcsopath in $(find "$DEPSDIR/plugins/$GROUP" -iname '*.so'); do
156
# This is ../../ because it's usually plugins/group/name.so
157
soname=$(basename "$srcsopath")
158
dstsopath="$dstpath/$soname"
159
echo " $srcsopath -> $dstsopath"
160
cp "$srcsopath" "$dstsopath"
161
$PATCHELF --set-rpath '$ORIGIN/../../lib:$ORIGIN' "$dstsopath"
162
done
163
done
164
165
# Why do we have to manually remove these libs? Because the linuxdeploy Qt plugin
166
# copies them, not the "main" linuxdeploy binary, and plugins don't inherit the
167
# include list...
168
for lib in "${REMOVE_LIBS[@]}"; do
169
for libpath in $(find "$OUTDIR/usr/lib" -name "$lib"); do
170
echo " Removing problematic library ${libpath}."
171
rm -f "$libpath"
172
done
173
done
174
175
# Restore unstripped deps (for cache).
176
rm -fr "$DEPSDIR"
177
mv "$DEPSDIR.bak" "$DEPSDIR"
178
179
# Fix up translations.
180
rm -fr "$OUTDIR/usr/bin/translations"
181
mv "$OUTDIR/usr/translations" "$OUTDIR/usr/bin"
182
cp -a "$BUILDDIR/bin/translations" "$OUTDIR/usr/bin"
183
184
# Generate AppStream meta-info.
185
echo "Generating AppStream metainfo..."
186
mkdir -p "$OUTDIR/usr/share/metainfo"
187
"$SCRIPTDIR/../generate-metainfo.sh" "$OUTDIR/usr/share/metainfo"
188
189
echo "Generating AppImage..."
190
rm -f "$NAME.AppImage"
191
"$APPIMAGETOOL" -v --runtime-file "$APPIMAGERUNTIME" "$OUTDIR" "$NAME.AppImage"
192
193