Files
makronix/tools/ext-dist.sh
T
2026-11-22 22:13:42 +00:00

130 lines
4.5 KiB
Bash
Executable File

#!/bin/sh
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
#
# ext-dist.sh -- make the distfile of a fork in ext/ and the distinfo of its recipe
#
# usage: tools/ext-dist.sh NAME [-r REV]
#
# (A git submodule of ext/ is archived from its own repository.)
# The same as tools/mcde-dist.sh (which see for why) for any fork
# ext/src/NAME with a recipe ext/src/NAME/pkgsrc whose DISTNAME= is NAME-VERSION:
# DISTDIR/NAME-VERSION.tar.gz from `git archive' of REV (default HEAD), the
# recipes (pkgsrc, pkgsrc-*) left out, and the three lines pkgsrc checks in
# the recipe's distinfo. Uncommitted edits are not in the archive on purpose.
set -eu
TOP=$(cd "$(dirname "$0")/.." && pwd)
# tools/pkgsrc-cross.mk.conf builds DISTDIR from it (bmake -V below); unset it
# gives "/build/distfiles" (see mcde-dist.sh)
MAKRONIX_TOP=$TOP
export MAKRONIX_TOP
[ $# -ge 1 ] || { echo "usage: ${0##*/} NAME [-r REV]" >&2; exit 2; }
SRC=$1
shift
REV=HEAD
while getopts r: ch; do
case $ch in
r) REV=$OPTARG ;;
*) echo "usage: ${0##*/} NAME [-r REV]" >&2; exit 2 ;;
esac
done
RECIPE=$TOP/ext/src/$SRC/pkgsrc
NAME=$(sed -n 's/^DISTNAME=[[:space:]]*//p' "$RECIPE/Makefile")
[ -n "$NAME" ] || { echo "no DISTNAME= in $RECIPE/Makefile" >&2; exit 1; }
if [ -z "${DISTDIR:-}" ] && [ -x "$TOP/build/pkg/bin/bmake" ]; then
DISTDIR=$(cd "$TOP/pkgsrc" && "$TOP/build/pkg/bin/bmake" -V '${DISTDIR:tA}')
fi
DISTDIR=${DISTDIR:-$TOP/build/distfiles}
DIGEST=${DIGEST:-$TOP/build/pkg/bin/digest}
command -v "$DIGEST" > /dev/null 2>&1 || DIGEST=digest
HAVE_DIGEST=
command -v "$DIGEST" > /dev/null 2>&1 && HAVE_DIGEST=yes
# A digest value, from pkgsrc's digest(1) if it is there, else from cksum(1)
# on NetBSD. Never write an empty line: pkgsrc reads "BLAKE2s (...) = " as no
# checksum at all and refuses the package with "No checksum recorded"
# (measured 2026-11-20, the forks are packed before pkgtools/digest exists).
digest_of()
{
what=$1; file=$2
if [ "$HAVE_DIGEST" = yes ]; then
"$DIGEST" "$what" "$file" | sed 's/.* = //'
return
fi
case $what in
SHA512) cksum -a sha512 "$file" | awk '{ print $NF }' ;;
esac
}
# The lock is held by the userland run that calls this script: it serializes
# the whole run, so do not take it again (and do not remove the parent's lock
# on exit). MX_USERLAND_LOCK is the pid of that run (tools/userland.sh).
LOCK=$TOP/build/.userland.lock
lock_held=
if [ -d "$LOCK" ]; then
ipid=$(cat "$LOCK/pid" 2>/dev/null || true)
if [ -n "${MX_USERLAND_LOCK:-}" ] && [ "$MX_USERLAND_LOCK" = "$ipid" ]; then
lock_held=1
elif [ -n "$ipid" ] && kill -0 "$ipid" 2>/dev/null; then
echo "ext-dist: userland/dist run holds $LOCK (pid $ipid); aborting" >&2
exit 1
else
rm -rf "$LOCK"
fi
fi
if [ -z "$lock_held" ]; then
mkdir "$LOCK" 2>/dev/null || { echo "ext-dist: cannot acquire $LOCK" >&2; exit 1; }
echo $$ > "$LOCK/pid"
trap 'rm -rf "$LOCK"' EXIT HUP INT TERM
fi
mkdir -p "$DISTDIR"
OUT=$DISTDIR/$NAME.tar.gz
# A project that is a git submodule (ext/AGENTS.md: splinter, junction, wikid,
# ...) is archived from its own repository, at its own root; a tree that is
# part of this repository from ext/src/NAME here. Either way the recipes
# (pkgsrc, pkgsrc-*) are left out, so a commit that only changes a recipe or
# its distinfo does not change the archive.
if [ -e "$TOP/ext/src/$SRC/.git" ]; then
G="$TOP/ext/src/$SRC"
TREE=$REV
PFX=.
else
G=$TOP
TREE=$REV:ext/src/$SRC
PFX=ext/src/$SRC
fi
MTIME=$(git -C "$G" log -1 --format=%ct "$REV" -- "$PFX" \
":(exclude)$PFX/pkgsrc" ":(exclude)$PFX/pkgsrc-*")
git -C "$G" archive --format=tar --mtime="@$MTIME" --prefix="$NAME/" \
"$TREE" ':(exclude)pkgsrc' ':(exclude)pkgsrc-*' > "$OUT.tar.tmp"
gzip -n -9 < "$OUT.tar.tmp" > "$OUT.tmp"
rm -f "$OUT.tar.tmp"
mv "$OUT.tmp" "$OUT"
# The source set (tools/srctree-stage.sh) carries build/distfiles, not the shared
# DISTDIR: keep the Makronix-only directory current here, so an offline
# rebuild finds the archive this run made without a second fetch (t0071).
MXDIST=${MXDIST:-$TOP/build/distfiles}
if [ "$DISTDIR" != "$MXDIST" ]; then
mkdir -p "$MXDIST"
ln "$OUT" "$MXDIST/$NAME.tar.gz" 2>/dev/null || \
cp -p "$OUT" "$MXDIST/$NAME.tar.gz" || exit 1
fi
size=$(wc -c < "$OUT" | tr -d ' ')
b2s=$(digest_of BLAKE2s "$OUT")
sha=$(digest_of SHA512 "$OUT")
{
echo '$NetBSD$'
echo
[ -n "$b2s" ] && echo "BLAKE2s ($NAME.tar.gz) = $b2s"
[ -n "$sha" ] && echo "SHA512 ($NAME.tar.gz) = $sha"
echo "Size ($NAME.tar.gz) = $size bytes"
} > "$RECIPE/distinfo"
echo "ext-dist: $OUT, $size bytes, from $REV ($(git -C "$TOP" rev-parse --short "$REV"))"