Nacre/tools/package.sh
2026-12-02 16:26:01 -06:00

230 lines
13 KiB
Shell
Executable file

#!/bin/bash
# Nacre release packages. Run: tools/package.sh (see the common part below for what it makes)
set -euo pipefail
root=$(cd "$(dirname "$0")/.." && pwd)
APP=nacre NAME=Nacre DESKTOP=dev.nacre.Nacre.desktop
URL=https://astar76.com/git/astar76/Nacre
ver=$(sed -n 's/^version = "\(.*\)"/\1/p' "$root/Cargo.toml" | head -n 1)
BINS=(nacre) EXTRA_ELF=(libnacre_webext.so)
LICENSE=MPL-2.0 NIX_LICENSE=mpl20 SUMMARY='A web browser built on WebKitGTK'
PKGDIR=packaging/arch-bin # packaging/arch builds from this checkout (install.sh); arch-bin repackages the release
DEB_DEPENDS='libwebkitgtk-6.0-4, libgtk-4-1, libsoup-3.0-0, libepoxy0, libpam0g, libdbus-1-3, gstreamer1.0-plugins-base, gstreamer1.0-plugins-good, gstreamer1.0-plugins-bad, gstreamer1.0-libav, xdg-utils'
DEB_RECOMMENDS='xdg-desktop-portal, gnome-keyring, libnotify4' DEB_SECTION=web
NIX_ARGS='wrapGAppsHook4, glib-networking, webkitgtk_6_0, gtk4, libsoup_3, libepoxy, pam, dbus, gst_all_1'
NIX_NATIVE='wrapGAppsHook4'
NIX_BUILD_INPUTS='webkitgtk_6_0 gtk4 libsoup_3 libepoxy pam dbus glib-networking gst_all_1.gstreamer gst_all_1.gst-plugins-base gst_all_1.gst-plugins-good gst_all_1.gst-plugins-bad gst_all_1.gst-libav stdenv.cc.cc.lib'
WIN=0
build() { nice cargo build --release --locked -q -j 2; }
stage() {
mkdir -p "$t/lib/nacre"; install -m755 "$rel/libnacre_webext.so" "$t/lib/nacre/libnacre_webext.so"
install -Dm644 -t "$1/nacre/filters" assets/filters/*.txt
install -Dm644 assets/dev.nacre.Nacre.desktop "$1/applications/dev.nacre.Nacre.desktop"
icon_pngs assets/dev.nacre.Nacre.svg "$1" dev.nacre.Nacre
install -Dm644 -t "$1/licenses/nacre" LICENSE assets/fonts/LICENSE-MaterialSymbols.txt assets/fonts/OFL-Outfit.txt
}
# ---------------------------------------------------------------- common part (same in every project)
# Builds into dist/ (and never writes to releases/):
# $NAME-<v>-Linux-x86_64.tar.gz bin/ share/ [lib/] laid out like a prefix, with install.sh and uninstall.sh
# $NAME-<v>-x86_64.AppImage GUI apps (DESKTOP set); BUNDLE_LIBS=1 adds the non-desktop library closure
# $NAME-<v>-arch-x86_64.pkg.tar.zst makepkg from that tar.gz, offline ($PKGDIR/PKGBUILD gets version and checksum)
# $NAME-<v>-amd64.deb the same tree under /usr, for apt
# $NAME-<v>-Setup.exe and $NAME-<v>-Windows-portable.zip when WIN=1 (mingw cross build, Inno Setup in wine)
# SHA256SUMS-<v>
# It also writes packaging/nix/default.nix (fetches the release tar.gz; version and hash filled in here) and the
# glibc floor read from the binary into installer.sh, the PKGBUILD and the .deb. Nothing is downloaded: appimagetool
# and libmpv-2.dll come from ~/.local/opt/build-tools.
# SKIP_BUILD=1 packages the existing build, NO_WIN=1 skips Windows. CARGO_TARGET_DIR is honoured.
cd "$root"; PKGDIR=${PKGDIR:-packaging/arch}
tools=${BUILD_TOOLS:-$HOME/.local/opt/build-tools}
[ "${SKIP_BUILD:-}" = 1 ] || build
tdir=${CARGO_TARGET_DIR:-$root/target}
rel=$tdir/release
# ZIG_GLIBC: built with cargo-zigbuild against that glibc, so the binary runs on older distributions too.
[ -z "${ZIG_GLIBC:-}" ] || rel=$tdir/x86_64-unknown-linux-gnu/release
out=$root/dist
mkdir -p "${XDG_CACHE_HOME:-$HOME/.cache}"; work=$(mktemp -d "${XDG_CACHE_HOME:-$HOME/.cache}/package.XXXXXX"); trap 'rm -rf "$work"' EXIT
export SOURCE_DATE_EPOCH=${SOURCE_DATE_EPOCH:-$(git log -1 --format=%ct 2>/dev/null || date +%s)}
# hicolor PNGs from an SVG: icon_pngs <svg> <share dir> <icon name>
icon_pngs() {
install -Dm644 "$1" "$2/icons/hicolor/scalable/apps/$3.svg"
for n in 16 24 32 48 64 128 256 512; do
mkdir -p "$2/icons/hicolor/${n}x$n/apps"; rsvg-convert -w "$n" -h "$n" "$1" -o "$2/icons/hicolor/${n}x$n/apps/$3.png"
done
}
# glibc floor: the newest GLIBC_x.y symbol version any shipped binary (or EXTRA_ELF library) needs.
glibc=$(for b in "${BINS[@]}" ${EXTRA_ELF[@]+"${EXTRA_ELF[@]}"}; do objdump -T "$rel/$b"; done | grep -o 'GLIBC_[0-9.]*' | sed 's/GLIBC_//' | sort -uV | tail -n 1)
[ -n "$glibc" ] || { echo "could not read the glibc version from the binary" >&2; exit 1; }
sed -i "s/^app=$APP main=\([^ ]*\) need=.*/app=$APP main=\1 need=$glibc/; s/glibc [0-9.]* or newer\./glibc $glibc or newer./" installer.sh
sed -i "s/'glibc>=[0-9.]*'/'glibc>=$glibc'/" $PKGDIR/PKGBUILD
# tar.gz: bin/ and share/ laid out like a prefix, so installer.sh, the PKGBUILD, the .deb and Nix just copy them.
rm -rf "$out"; mkdir -p "$out"
t=$work/$APP-$ver
mkdir -p "$t/bin" "$t/share" # stage may add $t/lib too
for b in "${BINS[@]}"; do install -m755 "$rel/$b" "$t/bin/$b"; done
stage "$t/share"
install -m755 installer.sh "$t/install.sh"
printf '#!/bin/sh\n# Removes %s installed by install.sh. --purge also deletes settings and data; --system or --prefix DIR as for install.sh.\nexec sh "$(dirname "$0")/install.sh" --uninstall "$@"\n' "$NAME" > "$t/uninstall.sh"
chmod 755 "$t/uninstall.sh"
cp README.md "$t/"
find "$t" -exec touch -h -d "@$SOURCE_DATE_EPOCH" {} +
tgz=$NAME-$ver-Linux-x86_64.tar.gz
tar -C "$work" --sort=name --owner=0 --group=0 --numeric-owner -cf - "$APP-$ver" | gzip -n -9 > "$out/$tgz"
# AppImage (GUI apps): the same tree under usr/, desktop file and icon at the top.
if [ -n "${DESKTOP:-}" ]; then
a=$work/AppDir
mkdir -p "$a/usr"
cp -R "$t/bin" "$t/share" "$a/usr/"; [ ! -d "$t/lib" ] || cp -R "$t/lib" "$a/usr/"
if [ "${BUNDLE_LIBS:-0}" = 1 ]; then
# Libraries every desktop already has (glibc, graphics, windowing, sound servers, fonts) stay with the host.
skip='^(linux-vdso|ld-linux|libc|libm|libdl|libpthread|librt|libutil|libresolv|libnsl|libgcc_s|libstdc\+\+|libGL|libGLX|libGLdispatch|libEGL|libOpenGL|libGLESv2|libgbm|libdrm|libvulkan|libX11|libX11-xcb|libxcb.*|libXau|libXdmcp|libXext|libXfixes|libXrandr|libXrender|libXi|libXcursor|libXss|libXv|libXxf86vm|libXinerama|libwayland-.*|libxkbcommon.*|libfontconfig|libfreetype|libharfbuzz|libglib-2.0|libgobject-2.0|libgio-2.0|libgmodule-2.0|libdbus-1|libsystemd|libudev|libasound|libpulse.*|libpipewire-0.3|libjack|libsndio|libcap|libselinux|libpcre2-8|libffi|libz|libexpat|libnss.*)\.so'
mkdir -p "$a/usr/lib"
ldd "$rel/${BINS[0]}" | awk '/=> \//{print $3} $1 ~ /^\// {print $1}' | while read -r lib; do
name=$(basename "$lib"); [[ $name =~ $skip ]] || cp -L "$lib" "$a/usr/lib/$name"
done
fi
cp "$a/usr/share/applications/$DESKTOP" "$a/"
icon=$(sed -n 's/^Icon=//p' "$a/$DESKTOP" | head -n 1)
for ext in svg png; do
f=$(find "$a/usr/share/icons" -name "$icon.$ext" | sort -V | tail -n 1)
[ -n "$f" ] && { cp "$f" "$a/$icon.$ext"; break; }
done
cat > "$a/AppRun" <<RUN
#!/bin/sh
here=\$(dirname "\$(readlink -f "\$0")")
[ ! -d "\$here/usr/lib" ] || [ "${BUNDLE_LIBS:-0}" != 1 ] || export LD_LIBRARY_PATH="\$here/usr/lib\${LD_LIBRARY_PATH:+:\$LD_LIBRARY_PATH}"
exec "\$here/usr/bin/${BINS[0]}" "\$@"
RUN
chmod +x "$a/AppRun"
cp "$tools/appimagetool" "$work/appimagetool"
(cd "$work" && ./appimagetool --appimage-extract >/dev/null)
ARCH=x86_64 "$work/squashfs-root/AppRun" --no-appstream "$a" "$out/$NAME-$ver-x86_64.AppImage" >"$work/ait.log" 2>&1 \
|| { tail -20 "$work/ait.log"; exit 1; }
fi
# Arch package from the local tar.gz; the repo's PKGBUILD gets this version and checksum.
sum=$(sha256sum "$out/$tgz" | cut -d' ' -f1)
sed -i "s/^pkgver=.*/pkgver=$ver/; s/^sha256sums=.*/sha256sums=('$sum')/" $PKGDIR/PKGBUILD
mkdir "$work/arch"; cp $PKGDIR/PKGBUILD "$work/arch/"
(cd "$work/arch" && makepkg --printsrcinfo > "$root/$PKGDIR/.SRCINFO")
(cd "$work/arch" && TARBALL="file://$out/$tgz" makepkg -f -C --nodeps --noconfirm >"$work/makepkg.log" 2>&1) \
|| { tail -20 "$work/makepkg.log"; exit 1; }
cp "$work"/arch/"$APP"-bin-"$ver"-*-x86_64.pkg.tar.zst "$out/$NAME-$ver-arch-x86_64.pkg.tar.zst"
# .deb: the tar.gz tree under /usr (licenses go to /usr/share/doc/<pkg>/ as Debian expects).
d=$work/deb
mkdir -p "$d/root/usr" "$d/root/usr/share/doc/$APP" "$d/control"
for x in bin lib share; do [ ! -d "$t/$x" ] || cp -R "$t/$x" "$d/root/usr/"; done
if [ -d "$d/root/usr/share/licenses/$APP" ]; then
cp -R "$d/root/usr/share/licenses/$APP/." "$d/root/usr/share/doc/$APP/"; rm -rf "$d/root/usr/share/licenses"
fi
cp README.md "$d/root/usr/share/doc/$APP/"
{ echo "Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/"; echo "Upstream-Name: $NAME"
echo "Source: $URL"; echo; echo "Files: *"; echo "License: $LICENSE"; } > "$d/root/usr/share/doc/$APP/copyright"
size=$(du -sk "$d/root" | cut -f1)
cat > "$d/control/control" <<CTL
Package: $APP
Version: $ver
Architecture: amd64
Maintainer: astar76 <[email protected]>
Installed-Size: $size
Depends: libc6 (>= $glibc)${DEB_DEPENDS:+, $DEB_DEPENDS}
${DEB_RECOMMENDS:+Recommends: $DEB_RECOMMENDS
}Section: ${DEB_SECTION:-misc}
Priority: optional
Homepage: $URL
Description: $SUMMARY
Built from $URL (version $ver). Needs glibc $glibc or newer.
CTL
(cd "$d/root" && find . -type f -printf '%P\n' | sort | xargs -d '\n' md5sum) > "$d/control/md5sums"
find "$d/root" "$d/control" -exec touch -h -d "@$SOURCE_DATE_EPOCH" {} +
echo 2.0 > "$d/debian-binary"
tar -C "$d/control" --sort=name --owner=0 --group=0 --numeric-owner -czf "$d/control.tar.gz" .
tar -C "$d/root" --sort=name --owner=0 --group=0 --numeric-owner -cJf "$d/data.tar.xz" .
deb=$out/$NAME-$ver-amd64.deb
(cd "$d" && ar rcD "$deb" debian-binary control.tar.gz data.tar.xz)
! command -v dpkg-deb >/dev/null || dpkg-deb --info "$deb" >/dev/null
# Nix: packaging/nix/default.nix fetches this release's tar.gz (flake.nix at the top calls it).
mkdir -p packaging/nix
sri=sha256-$(openssl dgst -sha256 -binary "$out/$tgz" | base64 -w0)
cat > packaging/nix/default.nix <<NIX
# $NAME for Nix, from the release tar.gz. Written by tools/package.sh (version and hash); edit the header there.
# nix build .#default or nix profile install . (from the repo), or callPackage this file.
{ lib, stdenv, fetchurl, autoPatchelfHook, makeWrapper${NIX_ARGS:+, $NIX_ARGS} }:
stdenv.mkDerivation rec {
pname = "$APP";
version = "$ver";
src = fetchurl {
url = "$URL/releases/download/v\${version}/$NAME-\${version}-Linux-x86_64.tar.gz";
hash = "$sri";
};
nativeBuildInputs = [ autoPatchelfHook makeWrapper${NIX_NATIVE:+ $NIX_NATIVE} ];
buildInputs = [ ${NIX_BUILD_INPUTS:-} ];
# Loaded with dlopen at run time, so autoPatchelf can't see them.
runtimeDependencies = [ ${NIX_RUNTIME:-} ];
dontBuild = true;
installPhase = ''
runHook preInstall
mkdir -p \$out
for d in bin lib share; do [ ! -d \$d ] || cp -R \$d \$out/; done
install -Dm644 README.md \$out/share/doc/$APP/README.md
runHook postInstall
'';
${NIX_EXTRA:-}
meta = {
description = "$SUMMARY";
homepage = "$URL";
license = lib.licenses.$NIX_LICENSE;
platforms = [ "x86_64-linux" ];
mainProgram = "${BINS[0]}";
sourceProvenance = [ lib.sourceTypes.binaryNativeCode ];
};
}
NIX
[ -f flake.nix ] || cat > flake.nix <<FLK
{
description = "$SUMMARY";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
outputs = { self, nixpkgs }:
let pkgs = nixpkgs.legacyPackages.x86_64-linux;
in {
packages.x86_64-linux.default = pkgs.callPackage ./packaging/nix { };
apps.x86_64-linux.default = { type = "app"; program = "\${self.packages.x86_64-linux.default}/bin/${BINS[0]}"; };
};
}
FLK
# Windows: mingw cross build, a one-folder app zipped as the portable build, and a per-user Inno Setup installer.
if [ "${WIN:-0}" = 1 ] && [ "${NO_WIN:-}" != 1 ]; then
command -v x86_64-w64-mingw32-gcc >/dev/null || { echo "Windows needs mingw-w64-gcc (sudo pacman -S mingw-w64-gcc), or NO_WIN=1." >&2; exit 1; }
if [ -n "${WIN_DLLS[*]:-}" ] && [ "${WIN_MPV:-0}" = 1 ]; then
# import library for libmpv-2.dll, so the cross linker can link "mpv"
lib=$tools/winlib; mkdir -p "$lib"
if [ ! -f "$lib/libmpv.dll.a" ]; then
{ echo "LIBRARY libmpv-2.dll"; echo EXPORTS
x86_64-w64-mingw32-objdump -p "$tools/libmpv-2.dll" | sed -n '/\[Ordinal\/Name Pointer\] Table/,/^$/p' | awk '/^\s*\[ *[0-9]+\] \+base/ {print $NF}'; } > "$lib/mpv.def"
x86_64-w64-mingw32-dlltool -d "$lib/mpv.def" -D libmpv-2.dll -l "$lib/libmpv.dll.a"
fi
export RUSTFLAGS="${RUSTFLAGS:-} -L $lib"
fi
[ "${SKIP_BUILD:-}" = 1 ] || nice cargo build --release --locked -q -j 2 --target x86_64-pc-windows-gnu
w=$work/win/$NAME; mkdir -p "$w"
for b in "${BINS[@]}"; do install -m755 "$tdir/x86_64-pc-windows-gnu/release/$b.exe" "$w/"; done
for f in ${WIN_DLLS[@]+"${WIN_DLLS[@]}"}; do install -m644 "$tools/$f" "$w/"; done
install -m644 LICENSE "$w/LICENSE.txt"
find "$work/win" -exec touch -h -d "@$SOURCE_DATE_EPOCH" {} +
(cd "$work/win" && zip -qrX9 "$out/$NAME-$ver-Windows-portable.zip" "$NAME")
iscc="$HOME/.wine/drive_c/users/$USER/AppData/Local/Programs/Inno Setup 7/ISCC.exe"
WINEDEBUG=-all wine "$iscc" /Q "/DVersion=$ver" "/DSrc=Z:$w" "/DOut=Z:$out" "Z:$root/packaging/$APP.iss" >"$work/iscc.log" 2>&1 \
|| { tail -20 "$work/iscc.log"; exit 1; }
fi
(cd "$out" && sha256sum "$NAME-$ver"-* > "SHA256SUMS-$ver")
echo "glibc floor: $glibc"
ls -l "$out"