Debian Codegraph @

misc/appimage/build-appimage.sh

raw
1 #!/bin/bash
2 # Build a self-contained AppImage for the `displaycal` GUI.
3 #
4 # Must run on the same Ubuntu release/Python it's invoked with, since:
5 # - wxPython has no PyPI wheel for Linux; we pull the prebuilt wheel that
6 # matches this distro+Python from extras.wxpython.org instead of building
7 # it from source.
8 # - GTK3/X11/etc. are assumed to already be present on the machine that
9 # later runs the AppImage (they are not bundled), same as any other
10 # GTK-based AppImage.
11 #
12 # Usage: build-appimage.sh <wheel-path> <version> <output-path>
13 set -euo pipefail
14
15 WHEEL_PATH="$1"
16 VERSION="$2"
17 # Resolve to an absolute path before we `cd "$WORK_DIR"` below: a relative
18 # path (as the CI workflow passes, e.g. "dist/DisplayCAL-x86_64.AppImage")
19 # would otherwise land inside the temp work dir and get wiped out by the
20 # final `rm -rf "$WORK_DIR"`.
21 OUTPUT_PATH="$(realpath -m "$3")"
22
23 MISC_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
24 REPO_ROOT="$(dirname "$MISC_DIR")"
25 WORK_DIR="$(mktemp -d)"
26 APPDIR="$WORK_DIR/DisplayCAL.AppDir"
27
28 PYVER="$(python3 -c 'import sys; print("%d.%d" % sys.version_info[:2])')"
29 # Query sysconfig rather than assuming /usr/lib/pythonX.Y, since actions/setup-python
30 # installs into hostedtoolcache rather than the system prefix.
31 SYS_PYTHON_LIB="$(python3 -c 'import sysconfig; print(sysconfig.get_path("stdlib"))')"
32 UBUNTU_RELEASE="$(. /etc/os-release && echo "$VERSION_ID")"
33
34 echo "Building AppImage for DisplayCAL ${VERSION} (Python ${PYVER}, Ubuntu ${UBUNTU_RELEASE})"
35
36 mkdir -p "$APPDIR/usr/bin" "$APPDIR/usr/lib" "$APPDIR/usr/share/metainfo"
37
38 # 1. A relocatable Python: copy the interpreter binary plus a full copy of
39 # the standard library (not a `venv`, whose pyvenv.cfg hardcodes the
40 # build machine's absolute path to the system install and would break as
41 # soon as the AppImage is extracted somewhere else at runtime).
42 cp "$(command -v "python${PYVER}")" "$APPDIR/usr/bin/python3"
43 cp -a "$SYS_PYTHON_LIB" "$APPDIR/usr/lib/python${PYVER}"
44 rm -rf "$APPDIR/usr/lib/python${PYVER}/site-packages" "$APPDIR/usr/lib/python${PYVER}/dist-packages"
45 mkdir -p "$APPDIR/usr/lib/python${PYVER}/site-packages"
46
47 # Python.h and friends, needed since dbus-python (a DisplayCAL dependency on
48 # Linux) has no prebuilt wheel and gets compiled from source below. sysconfig
49 # resolves this path relative to sys.prefix, which the copied interpreter
50 # re-derives from its own location, so it must exist under $APPDIR/usr too.
51 SYS_PYTHON_INCLUDE="$(python3 -c 'import sysconfig; print(sysconfig.get_path("include"))')"
52 mkdir -p "$APPDIR/usr/include"
53 cp -a "$SYS_PYTHON_INCLUDE" "$APPDIR/usr/include/python${PYVER}"
54
55 # libpython itself is dlopen()'d by the interpreter binary at startup.
56 LIBPYTHON="$(ldd "$(command -v "python${PYVER}")" | awk '/libpython/{print $3}')"
57 [ -n "$LIBPYTHON" ] && cp -L "$LIBPYTHON" "$APPDIR/usr/lib/"
58
59 # 2. Install DisplayCAL and its dependencies straight into that prefix,
60 # including the Ubuntu-matched wxPython wheel.
61 "$APPDIR/usr/bin/python3" -m ensurepip --upgrade
62 "$APPDIR/usr/bin/python3" -m pip install --upgrade \
63 --prefix "$APPDIR/usr" \
64 --find-links "https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-${UBUNTU_RELEASE}/" \
65 "$WHEEL_PATH"
66
67 # 3. Desktop integration files.
68 cp "$REPO_ROOT/misc/displaycal.desktop" "$APPDIR/displaycal.desktop"
69 cp "$REPO_ROOT/misc/net.displaycal.DisplayCAL.appdata.xml" \
70 "$APPDIR/usr/share/metainfo/net.displaycal.DisplayCAL.appdata.xml"
71 ICON_FILE="$REPO_ROOT/DisplayCAL/theme/icons/256x256/displaycal.png"
72
73 # 4. AppRun: invoke the bundled interpreter directly (rather than through a
74 # pip-generated console-script, whose shebang hardcodes this build's
75 # absolute path) and repoint PYTHONHOME/PYTHONPATH at the AppDir so the
76 # bundled stdlib/site-packages are used instead of the host's.
77 cat > "$APPDIR/AppRun" <<'APPRUN'
78 #!/bin/sh
79 HERE="$(dirname "$(readlink -f "$0")")"
80 export PYTHONHOME="$HERE/usr"
81 export LD_LIBRARY_PATH="$HERE/usr/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
82 exec "$HERE/usr/bin/python3" -c "import sys; from DisplayCAL.main import main; sys.exit(main())" "$@"
83 APPRUN
84 chmod +x "$APPDIR/AppRun"
85
86 # 5. Package. linuxdeploy wires up the top-level .desktop/icon/.DirIcon and
87 # bundles the interpreter's own shared-library dependencies (libssl,
88 # libffi, ...); it deliberately excludes core system libs like glibc,
89 # which AppImages assume the host already provides.
90 LINUXDEPLOY="$WORK_DIR/linuxdeploy-x86_64.AppImage"
91 curl -sL -o "$LINUXDEPLOY" \
92 https://github.com/linuxdeploy/linuxdeploy/releases/download/continuous/linuxdeploy-x86_64.AppImage
93 chmod +x "$LINUXDEPLOY"
94
95 cd "$WORK_DIR"
96 ARCH=x86_64 "$LINUXDEPLOY" --appimage-extract-and-run \
97 --appdir "$APPDIR" \
98 --executable "$APPDIR/usr/bin/python3" \
99 --desktop-file "$APPDIR/displaycal.desktop" \
100 --icon-file "$ICON_FILE" \
101 --output appimage
102
103 mkdir -p "$(dirname "$OUTPUT_PATH")"
104 mv "$WORK_DIR"/DisplayCAL*.AppImage "$OUTPUT_PATH"
105 rm -rf "$WORK_DIR"