Damson/tools/package.sh
astar76 fb1bb9b292 Release packaging: tar.gz, AppImage, Arch package (damson-bin), installer.sh
tools/package.sh builds dist/ offline; installer.sh installs the tar.gz or AppImage per user or
system-wide with a checksum and glibc check; README install section and 0.1.0 release notes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-26 13:58:10 -06:00

90 lines
5 KiB
Shell
Executable file

#!/bin/bash
# Damson release packages. Run: tools/package.sh (see the common part below for what it makes)
set -euo pipefail
root=$(cd "$(dirname "$0")/.." && pwd)
APP=damson NAME=Damson DESKTOP=damson.desktop
BINS=(damson)
ver=$(sed -n 's/^version = "\(.*\)"/\1/p' "$root/Cargo.toml" | head -n 1)
build() { nice cargo build --release --locked -q -j 2; }
# share/ as it lands under the prefix
stage() {
local s=$1 b=$rel/damson
install -Dm644 packaging/damson.desktop "$s/applications/damson.desktop"
install -Dm644 packaging/damson.metainfo.xml "$s/metainfo/damson.metainfo.xml"
install -Dm644 packaging/damson.svg "$s/icons/hicolor/scalable/apps/damson.svg"
for n in 16 24 32 48 64 128 256 512; do
mkdir -p "$s/icons/hicolor/${n}x$n/apps"; "$b" --export-icon "$n" "$s/icons/hicolor/${n}x$n/apps/damson.png"
done
install -Dm644 LICENSE "$s/licenses/damson/LICENSE"
install -Dm644 assets/syntax/LICENSES.md "$s/licenses/damson/syntax-definitions.md"
install -Dm644 assets/syntax/KSyntaxHighlighting-MIT.txt "$s/licenses/damson/KSyntaxHighlighting-MIT.txt"
}
# ---------------------------------------------------------------- common part (same in every project)
# Builds into dist/: $NAME-<v>-Linux-x86_64.tar.gz (install.sh, uninstall.sh), $NAME-<v>-x86_64.AppImage (GUI only),
# $NAME-<v>-arch-x86_64.pkg.tar.zst (makepkg from that tar.gz, offline) and SHA256SUMS-<v>.
# It also writes the tar.gz checksum and version into $PKGDIR/PKGBUILD and .SRCINFO, and the glibc floor read
# from the binary into installer.sh and the PKGBUILD. Nothing is downloaded: appimagetool comes from
# ~/Projects/Mulberry/dist and runs extracted (no FUSE, no runtime fetch). Never writes to releases/.
# SKIP_BUILD=1 packages the existing build. CARGO_TARGET_DIR is honoured.
cd "$root"; PKGDIR=${PKGDIR:-packaging/arch}
[ "${SKIP_BUILD:-}" = 1 ] || build
rel=${CARGO_TARGET_DIR:-$root/target}/release
out=$root/dist
work=$(mktemp -d); trap 'rm -rf "$work"' EXIT
export SOURCE_DATE_EPOCH=${SOURCE_DATE_EPOCH:-$(git log -1 --format=%ct 2>/dev/null || date +%s)}
# 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 and the PKGBUILD 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/"
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")")
exec "\$here/usr/bin/${BINS[0]}" "\$@"
RUN
chmod +x "$a/AppRun"
cp ~/Projects/Mulberry/dist/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"
(cd "$out" && sha256sum "$NAME-$ver"-* > "SHA256SUMS-$ver")
echo "glibc floor: $glibc"
ls -l "$out"