#!/usr/bin/env bash # Builds, signs and uploads Fennec OS packages (protocol: docs/updates.md). # # tools/pkg/packages.sh [--upload] [shell] [app:...] [apps] # # With no package names it does the shell and every app. A package whose # version is already in the registry is skipped (bump the version to publish): # shell version = [workspace.package] version in Cargo.toml # app: version = "version" in apps//app.toml # # Only the OS's own apps are packages here. Store apps (Netflex, YouTube, # Steam...) are released from their own repos (docs/store.md). # # Environment: # SF_SIGNING_KEY Ed25519 private key: the PEM file base64-encoded on one # line (so CI logs mask it), or SF_SIGNING_KEY_FILE = the PEM # SF_PACKAGE_TOKEN "user:token", or a token of SF_OWNER, with write:package (upload only) # SF_GITEA default https://gitea.bluefox.support # SF_OWNER default bluefox # SF_OUT output folder, default target/packages # SF_TARGET Rust target of the shell, e.g. aarch64-unknown-linux-gnu # (cross build against Debian multiarch libraries, see # image/pi/build.sh). Default: this machine. A shell that # isn't x86-64 gets a suffix: sf-shell-1.2.0-aarch64.tar.zst set -euo pipefail ROOT="$(cd "$(dirname "$0")/../.." && pwd)" GITEA="${SF_GITEA:-https://gitea.bluefox.support}" OWNER="${SF_OWNER:-bluefox}" OUT="${SF_OUT:-$ROOT/target/packages}" # Binaries in the shell package (built with cargo --release). SHELL_BINS=(sf-launcher sf-remote sf-core sf-miracast sf-compositor sf-update) UPLOAD=0 WANT=() for a in "$@"; do case "$a" in --upload) UPLOAD=1 ;; shell | apps | app:*) WANT+=("$a") ;; *) echo "unknown argument: $a" >&2; exit 2 ;; esac done [ ${#WANT[@]} -gt 0 ] || WANT=(shell apps) KEY_FILE="${SF_SIGNING_KEY_FILE:-}" TMP=$(mktemp -d) trap 'rm -rf "$TMP"' EXIT if [ -z "$KEY_FILE" ]; then [ -n "${SF_SIGNING_KEY:-}" ] || { echo "SF_SIGNING_KEY or SF_SIGNING_KEY_FILE is needed" >&2; exit 2; } KEY_FILE="$TMP/key.pem" (umask 077; printf '%s' "$SF_SIGNING_KEY" | base64 -d >"$KEY_FILE") fi auth() { case "${SF_PACKAGE_TOKEN:-}" in *:*) printf -- '-u\n%s\n' "$SF_PACKAGE_TOKEN" ;; ?*) printf -- '-u\n%s:%s\n' "$OWNER" "$SF_PACKAGE_TOKEN" ;; esac } mapfile -t AUTH < <(auth) TARGET="${SF_TARGET:-}" ARCH="${TARGET%%-*}" ARCH="${ARCH:-$(uname -m)}" SUFFIX="" [ "$ARCH" = x86_64 ] || SUFFIX="-$ARCH" if [ -n "$TARGET" ] && [ "$ARCH" != "$(uname -m)" ]; then # Cross build against the target's multiarch libraries (Debian). triple="$ARCH-linux-gnu" upper=$(echo "$TARGET" | tr 'a-z-' 'A-Z_') export "CARGO_TARGET_${upper}_LINKER=${triple}-gcc" export "CC_${TARGET//-/_}=${triple}-gcc" "CXX_${TARGET//-/_}=${triple}-g++" export PKG_CONFIG_ALLOW_CROSS=1 PKG_CONFIG_SYSROOT_DIR=/ export PKG_CONFIG_PATH="/usr/lib/$triple/pkgconfig:/usr/share/pkgconfig" fi # pkg_file : the shell is native code (architecture suffix), apps are not. pkg_file() { if [ "$1" = sf-shell ]; then echo "$1-$2$SUFFIX.tar.zst"; else echo "$1-$2.tar.zst"; fi; } url() { echo "$GITEA/api/packages/$OWNER/generic/$1/$2/$(pkg_file "$1" "$2")"; } published() { [ "$UPLOAD" = 1 ] || return 1 local code code=$(curl -s -o /dev/null -w '%{http_code}' -I "${AUTH[@]}" "$(url "$1" "$2")") [ "$code" = 200 ] } # pack : tar.zst + .sig in $OUT, uploaded with --upload. pack() { local name=$1 version=$2 dir=$3 file printf 'name = "%s"\nversion = "%s"\n' "$name" "$version" >"$dir/pkg.toml" if [ "$name" = sf-shell ]; then printf 'arch = "%s"\n' "$ARCH" >>"$dir/pkg.toml"; fi mkdir -p "$OUT" file="$OUT/$(pkg_file "$name" "$version")" tar --sort=name --owner=0 --group=0 --numeric-owner --mtime=@0 -C "$dir" -cf - . | zstd -q -19 -T0 -f -o "$file" openssl pkeyutl -sign -inkey "$KEY_FILE" -rawin -in "$file" -out "$file.sig" echo "built $(basename "$file") ($(du -h "$file" | cut -f1))" if [ "$UPLOAD" = 1 ]; then for f in "$file" "$file.sig"; do curl -fsS "${AUTH[@]}" --upload-file "$f" "$GITEA/api/packages/$OWNER/generic/$name/$version/$(basename "$f")" >/dev/null done echo "uploaded $name $version" fi } toml_version() { sed -n 's/^version *= *"\(.*\)".*/\1/p' "$1" | head -1; } do_shell() { local version version=$(sed -n '/^\[workspace.package\]/,/^\[/ s/^version *= *"\(.*\)"/\1/p' "$ROOT/Cargo.toml") if published sf-shell "$version"; then echo "sf-shell $version ($ARCH): already published" return fi local args=() for b in "${SHELL_BINS[@]}"; do args+=(-p "$b"); done local bins="${CARGO_TARGET_DIR:-$ROOT/target}/release" if [ -n "$TARGET" ]; then args+=(--target "$TARGET") bins="${CARGO_TARGET_DIR:-$ROOT/target}/$TARGET/release" fi (cd "$ROOT" && cargo build --release --locked "${args[@]}") local dir="$TMP/sf-shell" rm -rf "$dir" mkdir -p "$dir/bin" for b in "${SHELL_BINS[@]}"; do install -m755 "$bins/$b" "$dir/bin/$b"; done install -m755 "$ROOT/tools/siri-remote/fastpair.py" "$dir/bin/sf-pair" pack sf-shell "$version" "$dir" } do_app() { local id=$1 src="$ROOT/apps/$1" version [ -f "$src/app.toml" ] || { echo "no app $id" >&2; return 1; } version=$(toml_version "$src/app.toml") [ -n "$version" ] || { echo "apps/$id/app.toml has no version, skipped"; return; } if published "sf-app-$id" "$version"; then echo "sf-app-$id $version: already published" return fi local dir="$TMP/sf-app-$id" rm -rf "$dir" cp -r "$src" "$dir" chmod -R u=rwX,go=rX "$dir" # Git on Windows loses the x bit: programs live in bin/. [ -d "$dir/bin" ] && chmod -R a+x "$dir/bin" pack "sf-app-$id" "$version" "$dir" } for w in "${WANT[@]}"; do case "$w" in shell) do_shell ;; # A folder with its own .git is a store app developed in place: skipped. apps) for d in "$ROOT"/apps/*/; do [ -e "$d/.git" ] || do_app "$(basename "$d")"; done ;; app:*) do_app "${w#app:}" ;; esac done