Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

5.1 KB
Raw Blame
#!/bin/sh
# Builds one distro's release tarball. Runs inside a container of that distro,
# started by scripts/make-release.sh; not meant to be run directly.
#
#   release-build.sh FAMILY OUTDIR
#
# Produces OUTDIR/rediwm-FAMILY-x86_64.tar.gz holding what install-session.sh
# installs (zig-out/release-safe, data/, the install scripts), the pinned
# wlroots with the library bundle it needs (lib/rediwm), and DEPENDS: the
# runtime packages, taken from what the binaries actually link rather than a
# hand-kept list, so it can't drift from the build.
set -eu
family=$1
out=$2
src=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
cd "$src"

log() { printf '==> %s\n' "$*" >&2; }
die() { printf 'error: %s\n' "$*" >&2; exit 1; }

case "$family" in
    arch)
        # A container image is a snapshot: refresh it so the release links
        # what rolling users have today.
        pacman -Syu --noconfirm
        extra="xorg-xwayland xdg-desktop-portal xdg-desktop-portal-wlr xdg-desktop-portal-gtk mesa squashfs-tools"
        owner() { pacman -Qqo "$1" 2>/dev/null; }
        ;;
    debian|ubuntu)
        extra="xwayland xdg-desktop-portal xdg-desktop-portal-wlr xdg-desktop-portal-gtk xkb-data libgl1-mesa-dri libegl-mesa0 squashfs-tools"
        # A diverted file (usr-merge) prints "diversion by PKG from: ..." lines
        # first; skip them, and keep only the first package of a shared path.
        owner() {
            for owned in "$1" "/usr$1"; do
                found=$(dpkg-query -S "$owned" 2>/dev/null | grep -v '^diversion ' | head -n 1 | sed 's/:.*//; s/,.*//')
                [ -z "$found" ] || { echo "$found"; return; }
            done
        }
        ;;
    fedora)
        extra="xorg-x11-server-Xwayland xdg-desktop-portal xdg-desktop-portal-wlr xdg-desktop-portal-gtk mesa-dri-drivers squashfs-tools"
        owner() { rpm -qf --qf '%{NAME}\n' "$1" 2>/dev/null | head -n 1; }
        ;;
    *) die "unknown family '$family' (arch, debian, ubuntu, fedora)" ;;
esac

log "Installing build dependencies"
sh scripts/install.sh --build-deps-only

[ "$(uname -m)" = x86_64 ] || die "releases are built for x86_64 only"
ZIG=$(sh scripts/zig-bootstrap.sh)
PATH="$(dirname "$ZIG"):$PATH"
export PATH

log "Building the pinned wlroots"
wlroots="$src/zig-out/wlroots"
sh scripts/build-wlroots.sh "$wlroots"

# Not the build machine's CPU, which is zig's default: x86_64_v2 (SSE4.2) runs on
# anything from this decade, where a newer baseline could die with SIGILL.
cores=$(nproc 2>/dev/null || echo 1)
jobs=${ZIG_JOBS:-$((cores > 2 ? cores - 2 : 1))}
log "Building RediWM (ReleaseSafe, x86_64_v2)"
release="$src/zig-out/release-safe"
rm -rf -- "$release"
sh scripts/with-wlroots.sh "$wlroots" zig build -Doptimize=ReleaseSafe -Dcpu=x86_64_v2 -j"$jobs" --prefix "$release" -Dwlroots-prefix="$wlroots"

# Linking the private wlroots left its build-machine prefix in rediwm's RUNPATH
# (after $ORIGIN/../lib/rediwm, which is all an installed copy needs). A path
# like that doesn't belong in a binary installed as root on other machines.
for exe in "$release"/bin/*; do
    case "$(patchelf --print-rpath "$exe" 2>/dev/null || true)" in
        *"$wlroots"*) patchelf --set-rpath '$ORIGIN/../lib/rediwm' "$exe" ;;
    esac
done

# What goes into the tarball mirrors the source tree, so install-session.sh
# (and install-session-root.sh, which reads data/) run from it unchanged.
version=$(sed -n 's/.*\.version = "\(.*\)".*/\1/p' build.zig.zon | head -n 1)
stage="$out/stage/rediwm"
rm -rf -- "$out/stage"
mkdir -p "$stage/zig-out" "$stage/scripts"
cp -a "$release" "$stage/zig-out/release-safe"
cp -a data "$stage/data"
rm -rf -- "$stage/data/__pycache__"
for f in install-session.sh install-session-root.sh; do
    cp "scripts/$f" "$stage/scripts/$f"
done
echo "$version ${REDIWM_REVISION:-unknown}" > "$stage/VERSION"

log "Working out the runtime dependencies"
bundle="$stage/zig-out/release-safe/lib/rediwm"
sonames=$(
    for f in "$stage"/zig-out/release-safe/bin/* "$bundle"/*.so*; do
        [ -f "$f" ] && [ ! -L "$f" ] || continue
        readelf -d "$f" 2>/dev/null | sed -n 's/.*(NEEDED).*\[\(.*\)\]/\1/p'
    done | sort -u
)
pkgs=""
for soname in $sonames; do
    [ ! -e "$bundle/$soname" ] || continue # shipped in the release
    # Exact match: sonames such as libstdc++.so.6 hold regex metacharacters.
    path=$(ldconfig -p | awk -v soname="$soname" '$1 == soname && /x86-64/ { print $NF; exit }')
    [ -n "$path" ] || die "$soname isn't installed in this container"
    pkg=$(owner "$path")
    [ -n "$pkg" ] || die "no package owns $path ($soname)"
    case "$pkg" in
        *[!A-Za-z0-9.+_-]*) die "unusable package name '$pkg' for $path ($soname)" ;;
    esac
    pkgs="$pkgs $pkg"
done
{
    echo "# Runtime packages for rediwm-$family, generated by scripts/release-build.sh."
    # shellcheck disable=SC2086 # word-split the package lists
    printf '%s\n' $pkgs $extra | sort -u
} > "$stage/DEPENDS"
depends=$(grep -v '^#' "$stage/DEPENDS" | tr '\n' ' ')

mkdir -p "$out"
tarball="$out/rediwm-$family-x86_64.tar.gz"
rm -f -- "$tarball"
GZIP=-9 tar -C "$out/stage" -czf "$tarball" rediwm
rm -rf -- "$out/stage"
log "Wrote $tarball ($(du -h "$tarball" | cut -f1)); depends on: $depends"