forked from Moonfin-Client/Moonfin-Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-linux.sh
More file actions
executable file
·1390 lines (1194 loc) · 42.3 KB
/
Copy pathbuild-linux.sh
File metadata and controls
executable file
·1390 lines (1194 loc) · 42.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
APP_NAME="Moonfin"
APP_ID="org.moonfin.linux"
APP_ICON="$REPO_ROOT/assets/icons/moonfin.png"
BUILD_DIR="$REPO_ROOT/build/linux/release/bundle"
TEMP_DIR="$REPO_ROOT/build/linux/temp"
# Artifact names carry the architecture, except on x86_64 which keeps the plain
# "Linux" name. An installed AppImage updates itself from a zsync file named
# after the artifact, and the AUR package and the README point at those same
# names, so renaming the x86_64 files would break all three.
get_artifact_platform() {
case "$(uname -m)" in
x86_64) printf '%s\n' "Linux" ;;
aarch64|arm64) printf '%s\n' "LinuxARM64" ;;
*) printf '%s\n' "Linux_$(uname -m)" ;;
esac
}
PLATFORM_TAG="$(get_artifact_platform)"
# Snap stage-packages land under the host's multiarch directory, so the snap's
# library path has to name the architecture it is being built for.
get_multiarch_triplet() {
case "$(uname -m)" in
aarch64|arm64) printf '%s\n' "aarch64-linux-gnu" ;;
*) printf '%s\n' "x86_64-linux-gnu" ;;
esac
}
get_deb_architecture() {
local machine
machine="$(uname -m)"
case "$machine" in
x86_64)
printf '%s\n' "amd64"
;;
aarch64|arm64)
printf '%s\n' "arm64"
;;
armv7l|armv7*)
printf '%s\n' "armhf"
;;
i386|i486|i586|i686)
printf '%s\n' "i386"
;;
*)
printf '%s\n' "$machine"
;;
esac
}
resolve_flutter() {
if [ -n "${FLUTTER_BIN:-}" ] && [ -x "$FLUTTER_BIN" ]; then
printf '%s\n' "$FLUTTER_BIN"
return 0
fi
if command -v flutter >/dev/null 2>&1; then
command -v flutter
return 0
fi
local candidates=(
"$HOME/flutter/bin/flutter"
"$HOME/Documents/flutter/bin/flutter"
"$HOME/snap/flutter/common/flutter/bin/flutter"
)
local candidate
for candidate in "${candidates[@]}"; do
if [ -x "$candidate" ]; then
printf '%s\n' "$candidate"
return 0
fi
done
echo "Error: Flutter not found. Add flutter to PATH or set FLUTTER_BIN to the full flutter executable path." >&2
exit 1
}
get_app_version() {
grep '^version:' "$REPO_ROOT/pubspec.yaml" | sed 's/version:[[:space:]]*//' | cut -d'+' -f1 | tr -d '[:space:]'
}
resolve_mpv_package_name() {
# Package naming differs across Ubuntu bases. Prefer newer naming when available.
if command -v apt-cache >/dev/null 2>&1; then
if apt-cache show libmpv2 >/dev/null 2>&1; then
printf '%s\n' "libmpv2"
return 0
fi
if apt-cache show libmpv1 >/dev/null 2>&1; then
printf '%s\n' "libmpv1"
return 0
fi
fi
# Conservative fallback for older Ubuntu/core22 environments.
printf '%s\n' "libmpv1"
}
resolve_build_dir() {
local candidates=(
"$REPO_ROOT/build/linux/x64/release/bundle"
"$REPO_ROOT/build/linux/arm64/release/bundle"
"$REPO_ROOT/build/linux/release/bundle"
)
local candidate
for candidate in "${candidates[@]}"; do
if [ -d "$candidate" ] && [ -f "$candidate/moonfin" ]; then
printf '%s\n' "$candidate"
return 0
fi
done
echo "Error: Linux bundle directory not found after Flutter build." >&2
echo "Checked:" >&2
for candidate in "${candidates[@]}"; do
echo " - $candidate" >&2
done
exit 1
}
resolve_shared_lib() {
local soname="$1"
# Prefer distro packages to avoid /usr/local builds that dpkg-query cannot map.
if command -v dpkg-query >/dev/null 2>&1; then
local pkg_candidates=()
case "$soname" in
# Prefer libmpv2 where available; keep libmpv1 as distro compatibility fallback.
libmpv.so.2) pkg_candidates=("libmpv2" "libmpv1") ;;
libmpv.so.1) pkg_candidates=("libmpv1" "libmpv2") ;;
libmpv.so) pkg_candidates=("libmpv-dev" "libmpv2" "libmpv1") ;;
libsecret-1.so.0) pkg_candidates=("libsecret-1-0") ;;
libsqlite3.so.0) pkg_candidates=("libsqlite3-0") ;;
libwebkit2gtk-4.1.so.0) pkg_candidates=("libwebkit2gtk-4.1-0") ;;
libwebkit2gtk-4.0.so.37) pkg_candidates=("libwebkit2gtk-4.0-37") ;;
esac
local dpkg_pkg
for dpkg_pkg in "${pkg_candidates[@]:-}"; do
local pkg_path
pkg_path="$(dpkg-query -L "$dpkg_pkg" 2>/dev/null | awk -v s="$soname" 'index($0, "/" s) {print; exit}')"
if [ -n "$pkg_path" ] && [ -f "$pkg_path" ]; then
printf '%s\n' "$pkg_path"
return 0
fi
done
fi
if command -v ldconfig >/dev/null 2>&1; then
local resolved
while IFS= read -r resolved; do
[ -z "$resolved" ] && continue
[ ! -f "$resolved" ] && continue
case "$resolved" in
/usr/local/*)
continue
;;
esac
printf '%s\n' "$resolved"
return 0
done < <(ldconfig -p 2>/dev/null | awk -v name="$soname" '$1 == name {print $NF}')
fi
local candidate
for candidate in \
"/usr/lib/$soname" \
"/usr/lib64/$soname" \
"/lib/$soname" \
"/lib64/$soname" \
"/usr/lib/x86_64-linux-gnu/$soname" \
"/usr/lib/aarch64-linux-gnu/$soname"; do
if [ -f "$candidate" ]; then
printf '%s\n' "$candidate"
return 0
fi
done
# Last resort for hosts without distro mpv packages, such as immutable distros
# where libmpv comes from Homebrew. Transitive deps are bundled via ldd, so a
# brew lib resolves into a self-contained bundle the same way a distro lib does.
if command -v brew >/dev/null 2>&1; then
candidate="$(brew --prefix 2>/dev/null)/lib/$soname"
if [ -f "$candidate" ]; then
printf '%s\n' "$candidate"
return 0
fi
fi
return 1
}
runtime_seed_libs() {
local seed_libs=""
local libmpv_path=""
local mpv_soname
for mpv_soname in libmpv.so.2 libmpv.so.1 libmpv.so; do
if libmpv_path="$(resolve_shared_lib "$mpv_soname")"; then
break
fi
done
if [ -z "$libmpv_path" ]; then
echo "Error: could not resolve libmpv (.so.2 or .so.1). Install libmpv2 (preferred) or libmpv1 on the build host." >&2
return 1
fi
seed_libs="$libmpv_path"
local libsecret_path
if ! libsecret_path="$(resolve_shared_lib libsecret-1.so.0)"; then
echo "Error: could not resolve libsecret-1.so.0. Install libsecret-1-0 on the build host." >&2
return 1
fi
seed_libs="$seed_libs"$'\n'"$libsecret_path"
local libsqlite3_path
if ! libsqlite3_path="$(resolve_shared_lib libsqlite3.so.0)"; then
echo "Error: could not resolve libsqlite3.so.0. Install libsqlite3-0 on the build host." >&2
return 1
fi
seed_libs="$seed_libs"$'\n'"$libsqlite3_path"
local webkit_path=""
local webkit_soname
for webkit_soname in libwebkit2gtk-4.1.so.0 libwebkit2gtk-4.0.so.37; do
if webkit_path="$(resolve_shared_lib "$webkit_soname")"; then
break
fi
done
if [ -z "$webkit_path" ]; then
echo "Error: could not resolve WebKitGTK runtime library (4.1 or 4.0). Install libwebkit2gtk-4.1-0 or libwebkit2gtk-4.0-37 on the build host." >&2
return 1
fi
seed_libs="$seed_libs"$'\n'"$webkit_path"
# WebKitGTK links libharfbuzz-icu, a split-out shim that minimal/immutable distros
# (SteamOS, Fedora-atomic) do not ship. The main harfbuzz stack is skipped as a
# system lib, so bundle the ICU shim explicitly. Best-effort: not every build host
# has it, and it is only needed where WebKit is bundled.
local harfbuzz_icu_path
if harfbuzz_icu_path="$(resolve_shared_lib libharfbuzz-icu.so.0)"; then
seed_libs="$seed_libs"$'\n'"$harfbuzz_icu_path"
else
echo "Warning: libharfbuzz-icu.so.0 not found on build host; AppImage may fail on distros lacking it." >&2
fi
printf '%s\n' "$seed_libs" | grep -v '^$' || true
}
ensure_sqlite_unversioned_link() {
local dest_lib="$1"
mkdir -p "$dest_lib"
if [ -e "$dest_lib/libsqlite3.so" ]; then
return 0
fi
local sqlite_real
sqlite_real="$(find "$dest_lib" -maxdepth 1 -type f -name 'libsqlite3.so.*' | sort -V | tail -n1 || true)"
if [ -z "$sqlite_real" ]; then
return 0
fi
ln -sf "$(basename "$sqlite_real")" "$dest_lib/libsqlite3.so"
}
# Arch ships libmpv.so.2; Ubuntu 22.04 ships libmpv.so.1. Ensure both soname
# symlinks exist in the bundle so binaries compiled against either version work.
ensure_mpv_compat_symlinks() {
local dest_lib="$1"
local mpv_real
mpv_real="$(find "$dest_lib" -maxdepth 1 -name 'libmpv.so.*' ! -type l 2>/dev/null | sort -V | tail -n1 || true)"
[ -z "$mpv_real" ] && return 0
local base
base="$(basename "$mpv_real")"
# A bare "[ ! -e ] && ln" would return 1 when every symlink already exists,
# which under set -e kills the build with no error message.
for alt in libmpv.so.1 libmpv.so.2 libmpv.so; do
if [ ! -e "$dest_lib/$alt" ]; then
ln -sf "$base" "$dest_lib/$alt"
fi
done
return 0
}
collect_transitive_libs() {
local seed_libs="$1"
local all_deps=""
while IFS= read -r seed; do
[ -z "$seed" ] && continue
[ ! -f "$seed" ] && continue
local deps
deps="$(ldd "$seed" 2>/dev/null | grep -oP '=> \K/[^ ]+' || true)"
# Libraries built without a SONAME (brew mujs) appear in ldd as a bare
# absolute path with no arrow, so collect those too.
local abs_deps
abs_deps="$(ldd "$seed" 2>/dev/null | grep -oP '^\s*\K/\S+(?=\s+\(0x)' || true)"
all_deps="$(printf '%s\n%s\n%s\n%s' "$all_deps" "$deps" "$abs_deps" "$seed")"
done <<< "$seed_libs"
printf '%s\n' "$all_deps" | sort -u | grep -v '^$' || true
}
# Libraries left out of the bundle because the target system has to own them,
# either because they talk to the kernel and the display stack or because a
# bundled copy would shadow the matching system one.
#
# Read this as the complete list of what we ask a machine to already have.
# Distros vary from shipping almost everything to shipping almost nothing, so
# anything not named here has to travel with the app. libselinux is the example
# to remember: Ubuntu builds glib against it so it reaches the dependency graph
# on the build host, while Arch does not install it, and leaving it out meant
# the app refused to start there. A machine having the library is not enough
# either: openSUSE builds ncurses without the versioned symbols Ubuntu adds, so
# the bundled libcaca could not resolve them and the app refused to start there
# too.
#
# Entries match anywhere in the file name, so a name that is a prefix of
# another one has to say so. Asking for libcrypt used to hand libcrypto to the
# host as well, which left the bundle carrying libssl and expecting an OpenSSL
# it was never built against.
runtime_skip_pattern() {
local skip='linux-vdso|ld-linux|libc[.]so|libm[.]so|libpthread|libdl[.]so|librt[.]so'
skip="$skip"'|libstdc[+][+]|libgcc_s'
skip="$skip"'|libX[a-z]|libxcb|libxkb|libxshmfence|libICE|libSM'
skip="$skip"'|libwayland|libffi|libpcre'
skip="$skip"'|libGL[.]so|libGLX|libGLdispatch|libGLESv|libEGL|libOpenGL'
skip="$skip"'|libdrm|libgbm'
skip="$skip"'|libglib|libgobject|libgio|libgmodule|libgthread'
skip="$skip"'|libpulse|libdbus|libasound|libsndfile|libpipewire|libspa|libjack'
skip="$skip"'|libfontconfig|libfreetype|libharfbuzz|libcairo|libpango|libpixman'
skip="$skip"'|libatk|libgdk|libgtk|libepoxy|libgdk_pixbuf|librsvg'
skip="$skip"'|libmount|libblkid|libuuid|libresolv|libnss3|libnssutil'
skip="$skip"'|libsystemd'
# OpenSSL finds its trust store and its provider modules through a directory
# fixed when it was built, so a copy carried from another distro can stop
# verifying certificates. Both halves stay with the host for that reason, and
# they stay together: a bundled libssl against a system libcrypto is the
# mismatch that has to be avoided.
skip="$skip"'|libssl[.]so|libcrypto[.]so'
printf '%s\n' "$skip"
}
# Libraries linked against a no-SONAME dependency carry its absolute build-host
# path in DT_NEEDED, which the loader uses verbatim on the target machine.
# Rewrite those entries to bare basenames so the bundled copy is found instead.
fix_absolute_needed() {
local dest_lib="$1"
if ! command -v patchelf >/dev/null 2>&1; then
echo "Warning: patchelf not found; absolute DT_NEEDED paths were not rewritten." >&2
return 0
fi
local lib abs base
for lib in "$dest_lib"/*.so*; do
[ -f "$lib" ] || continue
[ -L "$lib" ] && continue
while IFS= read -r abs; do
[ -z "$abs" ] && continue
base="$(basename "$abs")"
# Brew ships libraries read-only and cp -L keeps that mode.
chmod u+w "$lib" 2>/dev/null || true
patchelf --replace-needed "$abs" "$base" "$lib" 2>/dev/null || true
if [ ! -e "$dest_lib/$base" ] && [ -f "$abs" ]; then
cp -L "$abs" "$dest_lib/$base"
fi
done < <(readelf -d "$lib" 2>/dev/null | sed -n 's/.*(NEEDED).*\[\(\/[^]]*\)\].*/\1/p')
done
}
copy_runtime_libs() {
local dest_lib="$1"
local lib_paths="$2"
local skip_pattern="$3"
mkdir -p "$dest_lib"
local count=0
while IFS= read -r lib_path; do
[ -z "$lib_path" ] && continue
[ ! -f "$lib_path" ] && continue
local lib_name
lib_name="$(basename "$lib_path")"
# libharfbuzz-icu is matched by the libharfbuzz skip entry but must be bundled
# (WebKit needs it; it is absent on minimal/immutable distros). Keep plain
# libharfbuzz and the other skips.
if [[ ! "$lib_name" =~ ^libXpresent[.]so ]] && [[ ! "$lib_name" =~ ^libharfbuzz-icu[.]so ]]; then
[[ "$lib_name" =~ $skip_pattern ]] && continue
fi
local real_path
real_path="$(readlink -f "$lib_path")"
[ -f "$real_path" ] || continue
local real_name
real_name="$(basename "$real_path")"
if [ ! -f "$dest_lib/$real_name" ]; then
cp -L "$real_path" "$dest_lib/$real_name"
count=$((count + 1))
fi
if [ "$real_name" != "$lib_name" ] && [ ! -e "$dest_lib/$lib_name" ]; then
ln -sf "$real_name" "$dest_lib/$lib_name"
fi
local soname
soname="$(readelf -d "$real_path" 2>/dev/null | sed -n 's/.*SONAME.*\[\(.*\)\].*/\1/p' | head -n1)"
if [ -n "$soname" ] && [ "$soname" != "$real_name" ] && [ ! -e "$dest_lib/$soname" ]; then
ln -sf "$real_name" "$dest_lib/$soname"
fi
done <<< "$lib_paths"
printf '%s\n' "$count"
}
inject_linux_runtime_libs() {
local bundle_dir="$1"
local seed_libs
seed_libs="$(runtime_seed_libs)"
if [ -z "$seed_libs" ]; then
echo "Warning: Could not resolve runtime seed libraries on this build host." >&2
return 1
fi
local all_deps
all_deps="$(collect_transitive_libs "$seed_libs")"
local skip
skip="$(runtime_skip_pattern)"
local bundled_count
bundled_count="$(copy_runtime_libs "$bundle_dir/lib" "$all_deps" "$skip")"
fix_absolute_needed "$bundle_dir/lib"
ensure_sqlite_unversioned_link "$bundle_dir/lib"
ensure_mpv_compat_symlinks "$bundle_dir/lib"
echo "Bundled $bundled_count runtime libraries for AppImage/Tarball"
verify_host_dependencies "$bundle_dir"
}
# Names every library the finished bundle still expects to find on the machine
# it runs on, which is the binary's and the bundled libraries' NEEDED entries
# minus whatever travels in the bundle.
host_dependencies() {
local bundle_dir="$1"
local file
for file in "$bundle_dir"/* "$bundle_dir"/lib/*.so*; do
[ -f "$file" ] || continue
[ -L "$file" ] && continue
readelf -d "$file" 2>/dev/null |
sed -n 's/.*(NEEDED).*\[\([^]]*\)\].*/\1/p'
done | sort -u | while IFS= read -r name; do
[ -n "$name" ] || continue
[ -e "$bundle_dir/lib/$name" ] && continue
printf '%s\n' "$name"
done
}
# The skip list is a promise that a machine already has those libraries. This
# checks the finished bundle keeps that promise, so a dependency that slipped
# past the collector shows up on the build host rather than on a user's.
verify_host_dependencies() {
local bundle_dir="$1"
local skip deps unexpected=""
if ! command -v readelf >/dev/null 2>&1; then
echo "Warning: readelf not found, host dependencies were not checked." >&2
return 0
fi
skip="$(runtime_skip_pattern)"
deps="$(host_dependencies "$bundle_dir")"
echo "Bundle expects these from the host:"
printf ' %s\n' $deps
local name
while IFS= read -r name; do
[ -n "$name" ] || continue
[[ "$name" =~ $skip ]] && continue
unexpected="${unexpected} ${name}"$'\n'
done <<< "$deps"
if [ -n "$unexpected" ]; then
echo "Error: these are neither bundled nor listed as system provided:" >&2
printf '%s' "$unexpected" >&2
echo "Bundle them, or add them to runtime_skip_pattern if every distro has them." >&2
return 1
fi
}
# Bundle libmpv + transitive deps for Flatpak (sandboxed, no system libs available).
inject_flatpak_libs() {
local dest_lib="$1"
mkdir -p "$dest_lib"
echo "Bundling runtime libraries for Flatpak..."
local seed_libs
seed_libs="$(runtime_seed_libs)"
if [ -z "$seed_libs" ]; then
echo "Error: could not resolve required runtime seed libraries." >&2
return 1
fi
local all_deps
all_deps="$(collect_transitive_libs "$seed_libs")"
local skip
skip="$(runtime_skip_pattern)"
local count
count="$(copy_runtime_libs "$dest_lib" "$all_deps" "$skip")"
fix_absolute_needed "$dest_lib"
ensure_sqlite_unversioned_link "$dest_lib"
ensure_mpv_compat_symlinks "$dest_lib"
echo "Bundled $count runtime libraries for Flatpak"
}
create_desktop_file() {
local dest="$1"
local version="${2:-}"
mkdir -p "$dest"
cat > "$dest/${APP_ID}.desktop" << EOF
[Desktop Entry]
Type=Application
Name=Moonfin
Exec=moonfin
Icon=${APP_ID}
Categories=AudioVideo;Video;
Comment=Jellyfin & Emby media client
Terminal=false
StartupWMClass=${APP_ID}
EOF
# AppImage launchers read the version from the embedded desktop entry rather
# than the filename.
if [ -n "$version" ]; then
echo "X-AppImage-Version=${version}" >> "$dest/${APP_ID}.desktop"
fi
}
create_metainfo_file() {
local dest="$1"
local version="$2"
mkdir -p "$dest"
cat > "$dest/${APP_ID}.metainfo.xml" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<component type="desktop-application">
<id>${APP_ID}</id>
<metadata_license>CC0-1.0</metadata_license>
<project_license>GPL-3.0-only</project_license>
<name>Moonfin</name>
<developer_name>Moonfin Team</developer_name>
<summary>Jellyfin & Emby media client</summary>
<description>
<p>Moonfin is a media client for Jellyfin and Emby servers, available on mobile, TV, and desktop platforms.</p>
</description>
<url type="homepage">https://moonfin.app/</url>
<launchable type="desktop-id">${APP_ID}.desktop</launchable>
<releases>
<release version="${version}" date="$(date '+%Y-%m-%d')"/>
</releases>
<content_rating type="oars-1.1"/>
</component>
EOF
}
ensure_flatpak_runtime() {
# GNOME runtime includes WebKitGTK required by linux webview plugin.
local runtime="org.gnome.Platform//50"
local sdk="org.gnome.Sdk//50"
if flatpak info --user "$runtime" >/dev/null 2>&1 && flatpak info --user "$sdk" >/dev/null 2>&1; then
return 0
fi
flatpak remote-add --user --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo 2>/dev/null || true
flatpak install --user --noninteractive flathub "$runtime" "$sdk" || true
}
icon_warning_shown=""
copy_icon() {
local dest="$1"
if [ ! -f "$APP_ICON" ]; then
if [ -z "$icon_warning_shown" ]; then
icon_warning_shown=1
echo "Warning: app icon missing at $APP_ICON, packages will show a placeholder." >&2
fi
return
fi
mkdir -p "$dest"
cp "$APP_ICON" "$dest/${APP_ID}.png"
}
# Installs the icon into the hicolor theme, the path every current desktop
# searches. On Wayland it is how a window gets an icon at all: the compositor
# resolves it from the desktop entry named by the window's app id, never from
# the window itself. share/pixmaps stays alongside for older lookups.
install_icons() {
local share_dir="$1"
copy_icon "$share_dir/icons/hicolor/512x512/apps"
copy_icon "$share_dir/pixmaps"
}
build_flutter_binary() {
echo "Building Flutter release binary for Linux..."
local flutter_bin
flutter_bin="$(resolve_flutter)"
cd "$REPO_ROOT"
local attempt=1 max_attempts=3
while true; do
if "$flutter_bin" build linux --release --dart-define=DISTRIBUTION_CHANNEL=linux; then
return 0
fi
if [ "$attempt" -ge "$max_attempts" ]; then
echo "Error: 'flutter build linux' failed after ${max_attempts} attempts." >&2
return 1
fi
echo "flutter build failed (attempt ${attempt}/${max_attempts}); purging pdfium cache and retrying..." >&2
# The plugin downloads into <cmake-binary-dir>/pdfium and copies into .lib/.
# Remove both across whatever arch/release dir Flutter used so the next configure
# re-fetches from scratch instead of reusing the truncated archive.
rm -rf "$REPO_ROOT"/build/linux/*/release/pdfium \
"$REPO_ROOT"/build/linux/*/release/.lib/* 2>/dev/null || true
sleep $((attempt * 10))
attempt=$((attempt + 1))
done
}
build_appimage() {
echo "=== Building AppImage ==="
if ! command -v appimagetool >/dev/null 2>&1; then
echo "Skipping AppImage: appimagetool not found"
echo " Install: https://github.com/AppImage/AppImageKit/releases"
return 1
fi
local appimage_dir="$TEMP_DIR/appimage"
local version="$(get_app_version)"
local appimage_name="${APP_NAME}_${PLATFORM_TAG}_v${version}.AppImage"
rm -rf "$appimage_dir"
mkdir -p "$appimage_dir"
cp -r "$BUILD_DIR"/* "$appimage_dir/"
inject_linux_runtime_libs "$appimage_dir"
cat > "$appimage_dir/AppRun" << 'EOF'
#!/bin/bash
APPDIR="$(cd "$(dirname "$0")" && pwd)"
export LD_LIBRARY_PATH="$APPDIR/lib:$LD_LIBRARY_PATH"
resolve_exact_lib() {
local lib_name="$1"
local old_ifs="$IFS"
IFS=':'
for lib_dir in $LD_LIBRARY_PATH; do
[ -n "$lib_dir" ] && [ -e "$lib_dir/$lib_name" ] && IFS="$old_ifs" && return 0
done
IFS="$old_ifs"
for lib_dir in /lib /lib64 /usr/lib /usr/lib64 /usr/lib/x86_64-linux-gnu /usr/lib/aarch64-linux-gnu; do
[ -e "$lib_dir/$lib_name" ] && return 0
done
return 1
}
ldd_output="$(ldd "$APPDIR/moonfin" 2>/dev/null || true)"
# Only "libfoo.so => not found" lines name a missing library. Symbol version
# lines start with the binary path, so matching bare "not found" reports the
# binary itself as missing.
missing_libs="$(printf '%s\n' "$ldd_output" | awk '$2 == "=>" && $3 == "not" && $4 == "found" {print $1}' | sort -u)"
version_errors="$(printf '%s\n' "$ldd_output" | grep -F 'not found' | grep -v ' => ' || true)"
if ! resolve_exact_lib libsqlite3.so; then
missing_libs="$(printf '%s\n%s\n' "$missing_libs" "libsqlite3.so" | awk 'NF' | sort -u)"
fi
if [ -n "$missing_libs" ]; then
echo "Moonfin cannot start. Missing shared libraries:" >&2
printf ' - %s\n' $missing_libs >&2
echo "Install the missing libraries for your distro and retry." >&2
exit 127
fi
if [ -n "$version_errors" ]; then
echo "Moonfin cannot start. A library your system provides is missing symbols" >&2
echo "this build needs:" >&2
printf '%s\n' "$version_errors" >&2
echo "That library has to be bundled with the app for your distro." >&2
echo "Please report this output so the next build carries it." >&2
echo "A deb, rpm, flatpak or snap build works in the meantime." >&2
exit 127
fi
exec "$APPDIR/moonfin" "$@"
EOF
chmod +x "$appimage_dir/AppRun"
create_desktop_file "$appimage_dir" "$version"
copy_icon "$appimage_dir"
install_icons "$appimage_dir/usr/share"
mkdir -p "$appimage_dir/usr/share/applications"
cp "$appimage_dir/${APP_ID}.desktop" \
"$appimage_dir/usr/share/applications/${APP_ID}.desktop"
# Desktop integration copies the entry and the themed icon out of the image,
# and file managers read the icon for the file itself from .DirIcon.
if [ -f "$appimage_dir/${APP_ID}.png" ]; then
cp "$appimage_dir/${APP_ID}.png" "$appimage_dir/.DirIcon"
fi
# Update information lets AppImage updaters fetch only the changed blocks from
# the .zsync published with each release. The wildcard matches whichever
# version the current release carries.
local update_info="${APPIMAGE_UPDATE_INFO:-gh-releases-zsync|Moonfin-Client|Moonfin-Core|latest|${APP_NAME}_${PLATFORM_TAG}_v*.AppImage.zsync}"
local appimagetool_args=()
if command -v zsyncmake >/dev/null 2>&1; then
appimagetool_args+=( -u "$update_info" )
else
echo "Warning: zsyncmake not found, building AppImage without embedded update information." >&2
echo " Install the zsync package to enable in-place updates." >&2
fi
cd "$TEMP_DIR"
# VERSION stamps the build. Passing an explicit output name keeps the
# artifact filename unchanged.
if ! VERSION="$version" appimagetool "${appimagetool_args[@]}" "$appimage_dir" "$appimage_name"; then
echo "Error: appimagetool failed." >&2
return 1
fi
if [ -f "$appimage_name" ]; then
mv "$appimage_name" "$REPO_ROOT/"
echo "✓ Created: $REPO_ROOT/$appimage_name"
fi
if [ -f "$appimage_name.zsync" ]; then
mv "$appimage_name.zsync" "$REPO_ROOT/"
echo "✓ Created: $REPO_ROOT/$appimage_name.zsync"
fi
}
build_tarball() {
echo "=== Building Tarball ==="
local version="$(get_app_version)"
local tarball_name="${APP_NAME}_${PLATFORM_TAG}_v${version}.tar.gz"
local tar_dir="$TEMP_DIR/tarball/moonfin-${version}"
rm -rf "$TEMP_DIR/tarball"
mkdir -p "$tar_dir"
cp -r "$BUILD_DIR"/* "$tar_dir/"
inject_linux_runtime_libs "$tar_dir"
if [ -f "$tar_dir/moonfin" ]; then
mv "$tar_dir/moonfin" "$tar_dir/moonfin-bin"
cat > "$tar_dir/moonfin" << 'EOF'
#!/bin/sh
APPDIR="$(cd "$(dirname "$0")" && pwd)"
export LD_LIBRARY_PATH="$APPDIR/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
resolve_exact_lib() {
lib_name="$1"
old_ifs="$IFS"
IFS=':'
for lib_dir in $LD_LIBRARY_PATH; do
[ -n "$lib_dir" ] && [ -e "$lib_dir/$lib_name" ] && IFS="$old_ifs" && return 0
done
IFS="$old_ifs"
for lib_dir in /lib /lib64 /usr/lib /usr/lib64 /usr/lib/x86_64-linux-gnu /usr/lib/aarch64-linux-gnu; do
[ -e "$lib_dir/$lib_name" ] && return 0
done
return 1
}
missing_libs="$(ldd "$APPDIR/moonfin-bin" 2>/dev/null | awk '/not found/ {print $1}' | sort -u || true)"
if ! resolve_exact_lib libsqlite3.so; then
missing_libs="$(printf '%s\n%s\n' "$missing_libs" "libsqlite3.so" | awk 'NF' | sort -u)"
fi
if [ -n "$missing_libs" ]; then
echo "Moonfin cannot start. Missing shared libraries:" >&2
printf ' - %s\n' $missing_libs >&2
echo "Install the missing libraries for your distro and retry." >&2
exit 127
fi
exec "$APPDIR/moonfin-bin" "$@"
EOF
chmod +x "$tar_dir/moonfin"
fi
cat > "$tar_dir/README.txt" << EOF
Moonfin ${version}
Jellyfin & Emby media client for Linux
Installation:
1. Extract this archive
2. Run ./moonfin
Dependencies:
- GTK 3.0+
- GLib 2.0+
- WebKitGTK 4.1 runtime (libwebkit2gtk-4.1-0)
- libflutter_linux_gtk (bundled)
- Additional runtime libs are bundled when available (libmpv, libsecret)
Requirements:
- X11 or Wayland display server
- Network access to Jellyfin/Emby server
EOF
# The AUR package is built from this tarball, so it has to carry the desktop
# entry and the icon. Without them there is nothing for a packager to install
# and the window falls back to a placeholder.
create_desktop_file "$tar_dir/share/applications"
create_metainfo_file "$tar_dir/share/metainfo" "$version"
install_icons "$tar_dir/share"
cd "$TEMP_DIR/tarball"
tar -czf "$tarball_name" "moonfin-${version}"
mv "$tarball_name" "$REPO_ROOT/"
echo "✓ Created: $REPO_ROOT/$tarball_name"
}
# The newest glibc anything in the installed tree asks for, which is the oldest
# glibc that can load it, since symbol versions are forward compatible only.
# Only undefined symbols count, because a library that defines a version lists
# every version it ever published rather than the one it needs.
required_glibc_version() {
local tree="$1" file
command -v readelf >/dev/null 2>&1 || return 0
for file in "$tree"/* "$tree"/lib/*.so*; do
[ -f "$file" ] || continue
[ -L "$file" ] && continue
readelf --dyn-syms -W "$file" 2>/dev/null |
awk '$7 == "UND"' |
grep -o 'GLIBC_[0-9][0-9.]*' || true
done | sed 's/^GLIBC_//' | sort -uV | tail -n1
}
build_deb() {
echo "=== Building Debian Package (.deb) ==="
if ! command -v dpkg-deb >/dev/null 2>&1; then
echo "Skipping .deb: dpkg-deb not found"
return 1
fi
local version="$(get_app_version)"
local deb_name="${APP_NAME}_${PLATFORM_TAG}_v${version}.deb"
local pkg_root="$TEMP_DIR/deb/moonfin-${version}"
local deb_arch
deb_arch="$(get_deb_architecture)"
rm -rf "$TEMP_DIR/deb"
mkdir -p "$pkg_root"/{usr/bin,usr/lib/moonfin,usr/share/applications,usr/share/pixmaps,usr/share/doc/moonfin,DEBIAN}
cp -r "$BUILD_DIR"/* "$pkg_root/usr/lib/moonfin/"
inject_linux_runtime_libs "$pkg_root/usr/lib/moonfin"
cat > "$pkg_root/usr/bin/moonfin" << 'EOF'
#!/bin/sh
APPDIR="/usr/lib/moonfin"
export LD_LIBRARY_PATH="$APPDIR/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
exec "$APPDIR/moonfin" "$@"
EOF
chmod +x "$pkg_root/usr/bin/moonfin"
create_desktop_file "$pkg_root/usr/share/applications"
install_icons "$pkg_root/usr/share"
mkdir -p "$pkg_root/usr/share/metainfo"
create_metainfo_file "$pkg_root/usr/share/metainfo" "$version"
# glibc is deliberately not bundled, so the build host's version is the oldest
# one the package can run on. Declaring it turns a wall of loader errors at
# launch into a refused install that names the glibc the machine needs.
local depends="libgtk-3-0, libglib2.0-0, libsecret-1-0, libwebkit2gtk-4.1-0"
local glibc
glibc="$(required_glibc_version "$pkg_root/usr/lib/moonfin")"
if [ -n "$glibc" ]; then
depends="$depends, libc6 (>= $glibc)"
fi
echo "Depends: $depends"
cat > "$pkg_root/DEBIAN/control" << EOF
Package: moonfin
Version: ${version}
Architecture: ${deb_arch}
Maintainer: Moonfin Team <support@moonfin.dev>
Installed-Size: $(du -sk "$pkg_root/usr" | cut -f1)
Depends: ${depends}
Description: Jellyfin & Emby media client
Moonfin is a media client for Jellyfin and Emby servers,
available on mobile, TV, and desktop platforms.
.
Features:
- Browse and stream media
- Offline downloads
- Casting support
- DLNA playback
Homepage: https://moonfin.app/
License: GPL-3.0-only
EOF
cat > "$pkg_root/usr/share/doc/moonfin/copyright" << EOF
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: Moonfin
Upstream-Contact: https://github.com/jmshrv/Moonfin
Source: https://github.com/jmshrv/Moonfin
Files: *
Copyright: 2024-2025 Moonfin Team
License: GPL-3.0-only
EOF
cd "$TEMP_DIR/deb"
dpkg-deb --build "moonfin-${version}" "$deb_name" 2>/dev/null || true
if [ -f "$deb_name" ]; then
mv "$deb_name" "$REPO_ROOT/"
echo "✓ Created: $REPO_ROOT/$deb_name"
fi
}
build_rpm() {
echo "=== Building RPM Package (.rpm) ==="
if ! command -v rpmbuild >/dev/null 2>&1; then
echo "Skipping .rpm: rpmbuild not found"
return 1
fi
local version="$(get_app_version)"
local rpm_name="${APP_NAME}_${PLATFORM_TAG}_v${version}.rpm"
local rpm_dir="$TEMP_DIR/rpm"
local spec_file="$rpm_dir/moonfin.spec"
rm -rf "$rpm_dir"
mkdir -p "$rpm_dir"/{SPECS,SOURCES,BUILD,RPMS,SRPMS}
create_desktop_file "$rpm_dir"
create_metainfo_file "$rpm_dir" "$version"
cat > "$spec_file" << EOF
Name: moonfin
Version: ${version}
Release: 1
Summary: Jellyfin & Emby media client
License: GPL-3.0-only
%description
Moonfin is a media client for Jellyfin and Emby servers,
available on mobile, TV, and desktop platforms.
%install
mkdir -p %{buildroot}/usr/bin
mkdir -p %{buildroot}/usr/lib/moonfin
mkdir -p %{buildroot}/usr/share/applications
mkdir -p %{buildroot}/usr/share/pixmaps
mkdir -p %{buildroot}/usr/share/metainfo
cp -r ${BUILD_DIR}/* %{buildroot}/usr/lib/moonfin/
cat > %{buildroot}/usr/bin/moonfin << 'EOFRUNNER'
#!/bin/sh
APPDIR="/usr/lib/moonfin"
export LD_LIBRARY_PATH="\$APPDIR/lib\${LD_LIBRARY_PATH:+:\$LD_LIBRARY_PATH}"
exec "\$APPDIR/moonfin" "\$@"
EOFRUNNER
chmod +x %{buildroot}/usr/bin/moonfin
cp "$rpm_dir/${APP_ID}.desktop" %{buildroot}/usr/share/applications/${APP_ID}.desktop
cp "$rpm_dir/${APP_ID}.metainfo.xml" %{buildroot}/usr/share/metainfo/${APP_ID}.metainfo.xml
if [ -f "$APP_ICON" ]; then