lore-forge/scripts/build-desktop-dist.sh

172 lines
6.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# Builds the desktop client for Linux and Windows and packs one archive per platform, for
# lore-forge to offer at /download:
#
# scripts/build-desktop-dist.sh [OUT_DIR]
#
# OUT_DIR defaults to run/downloads/ (LORE_FORGE_RUN_DIR/downloads), which scripts/start.sh
# serves. It receives:
#
# lore-forge-desktop-linux-x86_64.tar.gz forge-desktop, forge-service, liblore.so
# lore-forge-desktop-windows-x86_64.zip forge-desktop.exe, forge-service.exe, lore.dll
# SHA256SUMS both archives' SHA-256
# BUILD one line: commit, Lore version, build date
#
# Each archive unpacks to one directory holding the three programs side by side (the GUI starts
# the service beside it, which loads liblore beside itself), Lore's licence and notices, and a
# README.txt.
#
# Everything is cross-compiled with Zig, so one Linux machine builds both:
# - the GUI with `zig build -Dtarget=...`;
# - the service with Cargo, linked by `zig cc` (and Zig's dlltool for Windows import
# libraries), so the Linux build uses the standard /lib64 loader and glibc 2.39 like
# liblore, rather than this machine's;
# - liblore from the Lore release pinned in lore.lock (scripts/fetch-lore.sh).
#
# Needs zig, cargo with rustup's x86_64-pc-windows-gnu target (added if missing), curl, tar,
# unzip and zip. Run it inside the repository's nix-shell on NixOS.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
RUN="${LORE_FORGE_RUN_DIR:-$ROOT/run}"
OUT="${1:-$RUN/downloads}"
DESKTOP="$ROOT/desktop"
GLIBC=2.39 # liblore's own floor
say() { echo "build-desktop-dist: $*" >&2; }
die() { say "$*"; exit 1; }
for tool in zig cargo tar unzip zip; do
command -v "$tool" >/dev/null || die "needs $tool on PATH"
done
if command -v rustup >/dev/null && ! rustup target list --installed | grep -qx x86_64-pc-windows-gnu; then
say "adding the x86_64-pc-windows-gnu Rust target"
rustup target add x86_64-pc-windows-gnu
fi
WORK="$(mktemp -d "${TMPDIR:-/tmp}/build-desktop-dist.XXXXXX")"
trap 'rm -rf "$WORK"' EXIT
# Linker wrappers for Cargo. rustc passes GCC's runtime libraries; Zig ships LLVM's libunwind
# instead, and its MinGW links its own C runtime (UCRT).
mkdir -p "$WORK/bin"
cat >"$WORK/bin/cc-linux" <<EOF
#!/usr/bin/env bash
args=()
for a in "\$@"; do
case "\$a" in
-lgcc_s) args+=(-lunwind) ;;
*) args+=("\$a") ;;
esac
done
exec zig cc -target x86_64-linux-gnu.$GLIBC "\${args[@]}"
EOF
cat >"$WORK/bin/cc-windows" <<'EOF'
#!/usr/bin/env bash
args=()
for a in "$@"; do
case "$a" in
-lgcc_eh) args+=(-lunwind) ;;
-lgcc_s | -lgcc | -l:libpthread.a | -lpthread | -lmsvcrt) ;;
*) args+=("$a") ;;
esac
done
exec zig cc -target x86_64-windows-gnu "${args[@]}"
EOF
printf '#!/bin/sh\nexec zig dlltool "$@"\n' >"$WORK/bin/dlltool"
chmod +x "$WORK/bin/"*
export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER="$WORK/bin/cc-linux"
export CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER="$WORK/bin/cc-windows"
export CARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUSTFLAGS="-C dlltool=$WORK/bin/dlltool"
LORE_VERSION=$(awk '$1 == "version" { print $2 }' "$ROOT/lore.lock")
LORE_DIR="$("$ROOT/scripts/fetch-lore.sh")" # Lore's licence files
COMMIT=$(git -C "$ROOT" rev-parse --short HEAD 2>/dev/null || echo unknown)
git -C "$ROOT" diff --quiet HEAD -- desktop 2>/dev/null || COMMIT="$COMMIT+changes"
# package NAME ZIG_TARGET RUST_TARGET LORE_TARGET EXE LIBRARY FORMAT
package() {
local name=$1 zig_target=$2 rust_target=$3 lore_target=$4 exe=$5 library=$6 format=$7
local stage="$WORK/$name" lib_dir
say "$name: GUI (zig build -Dtarget=$zig_target)"
(cd "$DESKTOP" && zig build -Dtarget="$zig_target" -Doptimize=ReleaseSafe -Dservice=false \
--prefix "$WORK/gui-$zig_target")
say "$name: service (cargo --target $rust_target)"
cargo build --quiet --release --package forge-service --target "$rust_target" \
--manifest-path "$DESKTOP/Cargo.toml"
say "$name: liblore $LORE_VERSION ($lore_target)"
lib_dir="$(LORE_TARGET="$lore_target" "$ROOT/scripts/fetch-lore.sh" --liblore)"
mkdir -p "$stage"
cp "$WORK/gui-$zig_target/bin/forge-desktop$exe" "$stage/"
cp "$DESKTOP/target/$rust_target/release/forge-service$exe" "$stage/"
cp "$lib_dir/$library" "$stage/"
install -m 644 "$LORE_DIR/LICENSE.txt" "$stage/LORE-LICENSE.txt"
install -m 644 "$LORE_DIR/THIRD-PARTY-NOTICES.txt" "$stage/LORE-THIRD-PARTY-NOTICES.txt"
readme "$name" "$exe" "$library" >"$stage/README.txt"
chmod 644 "$stage/README.txt"
chmod 755 "$stage/forge-desktop$exe" "$stage/forge-service$exe"
case "$format" in
tar.gz) tar -czf "$WORK/$name.tar.gz" -C "$WORK" --owner=0 --group=0 "$name" ;;
zip) (cd "$WORK" && zip -qr -X "$name.zip" "$name") ;;
esac
}
readme() {
local name=$1 exe=$2 library=$3
cat <<EOF
lore-forge desktop client ($name)
Built from lore-forge $COMMIT with Lore $LORE_VERSION on $(date -u +%Y-%m-%d).
forge-desktop$exe the tray application; run this one
forge-service$exe the per-user service it starts; does the Lore work
$library$(printf '%*s' $((16 - ${#library})) '')the Lore library the service loads
Keep the three files together: forge-desktop starts the forge-service beside it, which loads
$library from beside itself.
Run forge-desktop. The service keeps its desktop.toml (the workspaces it syncs) and its
service.log in ~/.config/lore-forge/ on Linux and %APPDATA%\lore-forge\ on Windows.
forge-desktop --licenses prints the licences of what it compiles in. Lore is MIT-licensed:
see LORE-LICENSE.txt and LORE-THIRD-PARTY-NOTICES.txt.
EOF
case "$name" in
*linux*) cat <<'EOF'
Linux: x86-64 with glibc 2.39 or newer (Ubuntu 24.04, Fedora 40 or later) and a Wayland session.
The tray icon needs a StatusNotifier host. This build is experimental: the service has run on
Linux, but the window has not yet been tried there.
EOF
;;
*windows*) cat <<'EOF'
Windows: x86-64, Windows 10 or later. This build is experimental: it is cross-compiled and has
not yet been run on Windows.
EOF
;;
esac
}
package lore-forge-desktop-linux-x86_64 "x86_64-linux-gnu.$GLIBC" x86_64-unknown-linux-gnu \
x86_64-unknown-linux-gnu "" liblore.so tar.gz
package lore-forge-desktop-windows-x86_64 x86_64-windows-gnu x86_64-pc-windows-gnu \
x86_64-pc-windows-msvc .exe lore.dll zip
mkdir -p "$OUT"
(cd "$WORK" && sha256sum lore-forge-desktop-*.tar.gz lore-forge-desktop-*.zip >SHA256SUMS)
echo "lore-forge $COMMIT, Lore $LORE_VERSION, built $(date -u +%Y-%m-%dT%H:%MZ)" >"$WORK/BUILD"
# Replace each file in one rename, so a download in progress never sees half an archive.
for f in lore-forge-desktop-linux-x86_64.tar.gz lore-forge-desktop-windows-x86_64.zip SHA256SUMS BUILD; do
cp "$WORK/$f" "$OUT/.$f.new"
mv -f "$OUT/.$f.new" "$OUT/$f"
done
say "wrote $OUT:"
(cd "$OUT" && ls -l lore-forge-desktop-* SHA256SUMS BUILD >&2 && cat BUILD >&2)