Files
makronix/tools/mcde-dist.sh
T

162 lines
6.5 KiB
Bash
Executable File

#!/bin/sh
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
#
# mcde-dist.sh -- make the distfile of MCDE and the distinfo of its recipe
#
# usage: tools/mcde-dist.sh [-r REV]
#
# MCDE is a fork of its own now (ext/src/mcde is the submodule
# git.kropa.tech/hiddenbox/mcde, ext/AGENTS.md); pkgsrc builds it from an
# archive like any other package. The archive is made with
# `git archive' from a commit (default HEAD), so the same commit always gives
# the same bytes and the checksums in distinfo stay valid: uncommitted edits are
# not in it on purpose. ext/src/mcde/pkgsrc (the recipe) is left out.
#
# Writes DISTDIR/mcde-VERSION.tar.gz (pkgsrc's DISTDIR, as
# tools/pkgsrc-cross.mk.conf chooses it) and renews ext/src/mcde/pkgsrc/distinfo with the three lines
# pkgsrc checks (BLAKE2s, SHA512, Size), computed with pkgsrc's own digest(1).
set -eu
TOP=$(cd "$(dirname "$0")/.." && pwd)
# tools/pkgsrc-cross.mk.conf builds DISTDIR from it (bmake -V below); unset it
# gives "/build/distfiles" and "mkdir: cannot create directory '/build'"
# (measured 2026-11-14 when run by hand, outside userland.sh)
MAKRONIX_TOP=$TOP
export MAKRONIX_TOP
REV=HEAD
while getopts r: ch; do
case $ch in
r) REV=$OPTARG ;;
*) echo "usage: ${0##*/} [-r REV]" >&2; exit 2 ;;
esac
done
RECIPE=$TOP/ext/src/mcde/pkgsrc
VERSION=$(sed -n 's/^DISTNAME=[[:space:]]*mcde-//p' "$RECIPE/Makefile")
[ -n "$VERSION" ] || { echo "no DISTNAME=mcde-... in $RECIPE/Makefile" >&2; exit 1; }
NAME=mcde-$VERSION
# pkgsrc's own DISTDIR, where `make fetch' looks and what tools/userland.sh
# serves to the build VM; build/distfiles only without a bootstrap
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
}
# respect the shared userland lock: do not overwrite a dist others are using.
# The userland run that calls this script holds it (MX_USERLAND_LOCK): it
# already serializes us, so do not take it again and do not remove it.
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 "mcde-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 "mcde-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
# MCDE is a submodule since 2026-11-22: the source revision is the commit the
# parent records for the gitlink, and the archive is made inside the submodule.
# `git archive REV:ext/src/mcde' in the parent would write an empty directory --
# git does not descend into a gitlink -- so REV names the parent and SRCREV the
# submodule commit.
SUB=$TOP/ext/src/mcde
SRCREV=$(git -C "$TOP" rev-parse "$REV:ext/src/mcde") || {
echo "mcde-dist: $REV has no ext/src/mcde gitlink" >&2; exit 1; }
# The time of every entry: the last commit that changed the source (not the
# recipe). Given a tree, git archive would use the current time, and every run
# gave other bytes (measured 2026-11-09); a commit of distinfo alone must not
# change the archive either. Needs git 2.42 or later (--mtime).
MTIME=$(git -C "$SUB" log -1 --format=%ct "$SRCREV" -- . \
':(exclude)pkgsrc' ':(exclude)pkgsrc-*')
# The archive is made again only when what goes into it changed: the source
# trees (git ls-tree, the recipes left out, as in the archive) and the time.
# Every make userland made it anew, 16 s for an unchanged tree (measured
# 2026-11-17). The key is kept beside the archive. NetBSD has sha256, not
# sha256sum; with the wrong name the key was empty and the comparison
# meaningless (measured 2026-11-22), so the tool is asked for by name.
hash_of()
{
if command -v sha256sum > /dev/null 2>&1; then
sha256sum | cut -d' ' -f1
else
sha256 | sed 's/.*) = //'
fi
}
key=$( { echo "$MTIME"; git -C "$SUB" ls-tree "$SRCREV" |
grep -v ' pkgsrc'; } | hash_of)
if [ -s "$OUT" ] && [ "$(cat "$OUT.key" 2>/dev/null)" = "$key" ]; then
echo "mcde-dist: archive unchanged ($REV $(git -C "$TOP" rev-parse --short "$REV"), mcde $(git -C "$SUB" rev-parse --short "$SRCREV"))"
else
# gzip -n: no name and no time stamp in the header, so the bytes do not
# change. -6, not -9: 6.0 s instead of 15.7 s for 0.9 % more bytes
# (67069914 against 66472043, measured 2026-11-17).
git -C "$SUB" archive --format=tar --mtime="@$MTIME" --prefix="$NAME/" \
"$SRCREV^{tree}" ':(exclude)pkgsrc' ':(exclude)pkgsrc-*' > "$OUT.tar.tmp"
gzip -n -6 < "$OUT.tar.tmp" > "$OUT.tmp"
rm -f "$OUT.tar.tmp"
mv "$OUT.tmp" "$OUT"
printf '%s\n' "$key" > "$OUT.key"
fi
# 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"
# the other recipes of the same archive (pkgsrc-motif: x11/mcde-motif)
for r in "$TOP"/ext/src/mcde/pkgsrc-*; do
[ -f "$r/Makefile" ] && cp "$RECIPE/distinfo" "$r/distinfo"
done
echo "mcde-dist: $OUT, $size bytes, from $REV ($(git -C "$TOP" rev-parse --short "$REV"), mcde $(git -C "$SUB" rev-parse --short "$SRCREV"))"