Files
makronix/tools/pkg-binaries.sh
T
lotherkandClaude Sonnet 5.5 7e9d92d75f pkg-binaries.sh: a size mismatch means the cached summary is stale -- drop it and the bad file
make release on the MacBook (build/logs/pkg-binaries-amd64.log): 24 packages 'is N bytes, want M'; the
cached pkg_summary.repo.gz was from 2026-11-19.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-23 09:59:30 +00:00

235 lines
8.5 KiB
Bash
Executable File

#!/bin/sh
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
# pkg-binaries.sh -- binary packages from the NetBSD project, for the media.
#
# usage: pkg-binaries.sh [-a amd64|aarch64] [-b BRANCH] list|fetch
#
# list the packages and everything they need at run time, with sizes
# fetch put them into build/packages/ARCH/All, with a pkg_summary.gz
#
# Policy (docs/BUILD-VM.md, "Binary first"): where the NetBSD project publishes
# a binary package for the platform and Makronix does not patch the package, the
# binary is taken instead of a build: firefox, thunderbird, qemu are hours of
# compiling (rust, llvm, ...) for nothing a patch of ours changes. What
# Makronix patches or what is not published is built (tools/userland.sh).
#
# What is wanted is named in packages.list, the lines of kind "fetch",
# "games" and "museum" (tools/pkglist.sh; the name is the last part of the
# PKGPATH).
# A package the project does not publish for the platform is reported and
# skipped, not an error: thunderbird has no aarch64 binary in 11.0_2026Q3.
#
# Where they come from: cdn.netbsd.org/pub/pkgsrc/packages/NetBSD/<arch>/<BRANCH>
# -- the quarterly branch built for NetBSD 11.0; Makronix is NetBSD 11.99.9, the
# binaries of the release branch are what the project offers for it. The
# dependencies are resolved over the repository's own pkg_summary.gz with
# pkg_admin pmatch (the pkgsrc bootstrap's, build/pkg/sbin), the way pkg_add
# would pick them; the newest package that matches wins. The archives are
# checked against FILE_SIZE of the summary (it carries no checksum for them).
#
# The result is a pkgsrc binary repository: `pkg_add' with PKG_PATH pointing at
# the directory installs from it without a network.
set -u
set -f
TOP=$(cd "$(dirname "$0")/.." && pwd)
ARCH=amd64
BRANCH=${BRANCH:-11.0_2026Q3}
BASE=${PKGREPO:-https://cdn.netbsd.org/pub/pkgsrc/packages/NetBSD}
PKGADMIN=${PKGADMIN:-$TOP/build/pkg/sbin/pkg_admin}
die()
{
echo "pkg-binaries: $*" >&2
exit 1
}
while getopts a:b: ch; do
case $ch in
a) ARCH=$OPTARG ;;
b) BRANCH=$OPTARG ;;
*) die "usage: ${0##*/} [-a amd64|aarch64] [-b BRANCH] list|fetch" ;;
esac
done
shift $((OPTIND - 1))
[ $# -eq 1 ] || die "usage: ${0##*/} [-a amd64|aarch64] [-b BRANCH] list|fetch"
CMD=$1
case $ARCH in
amd64) PARCH=x86_64 ;;
aarch64) PARCH=aarch64 ;;
*) die "unknown platform $ARCH" ;;
esac
[ -x "$PKGADMIN" ] || die "no pkg_admin at $PKGADMIN (the pkgsrc bootstrap, AGENTS.md)"
# sha256 of stdin: sha256sum on Linux, cksum(1) on NetBSD (no sha256sum there)
sha256_in()
{
if command -v sha256sum > /dev/null 2>&1; then
sha256sum | cut -d' ' -f1
else
cksum -a sha256 | awk '{ print $NF }'
fi
}
# the newest of the package names on stdin, by pkgsrc's own version rules
# (pkg_admin pmatch "name>version"). `sort -V' did this before; NetBSD's
# sort(1) has no -V, the pipe gave nothing and every package counted as "not
# published" while the script still ended with 0 (measured 2026-11-20).
newest()
{
nbest=
while read -r ncand; do
[ -n "$ncand" ] || continue
if [ -z "$nbest" ] ||
"$PKGADMIN" pmatch "${nbest%-*}>${nbest##*-}" "$ncand" 2>/dev/null; then
nbest=$ncand
fi
done
[ -z "$nbest" ] || echo "$nbest"
}
REPO=$BASE/$PARCH/$BRANCH/All
OUT=$TOP/build/packages/$ARCH
mkdir -p "$OUT/All" || exit 1
tmp=$(mktemp -d "${TMPDIR:-/tmp}/pkgbin.XXXXXX") || exit 1
trap 'rm -rf "$tmp"' EXIT
SUMMARY=$OUT/pkg_summary.repo.gz
[ -s "$SUMMARY" ] || curl -s -f -m 300 -o "$SUMMARY" "$REPO/pkg_summary.gz" ||
die "cannot fetch $REPO/pkg_summary.gz"
# fetch with the same repository summary and the same wanted packages as the
# last good fetch, and every file still there with its size: nothing to do.
# Resolving the closure again cost about two minutes of every make userland
# (some 1100 awk passes over the index, no download; measured 2026-11-17).
fetchkey=$( { echo "$ARCH $BRANCH $REPO"; sha256_in < "$SUMMARY"
"$TOP/tools/pkglist.sh" fetch; "$TOP/tools/pkglist.sh" games
"$TOP/tools/pkglist.sh" museum; } | sha256_in)
if [ "$CMD" = fetch ] && [ -s "$OUT/All/pkg_summary.gz" ] &&
[ -s "$OUT/.fetch-files" ] &&
[ "$(cat "$OUT/.fetch-key" 2>/dev/null)" = "$fetchkey" ]; then
same=yes
while read -r f s; do
have=$({ wc -c < "$OUT/All/$f"; } 2>/dev/null | tr -d ' ')
[ "$have" = "$s" ] || { same=; break; }
done < "$OUT/.fetch-files"
if [ -n "$same" ]; then
echo "pkg-binaries: $ARCH $BRANCH: unchanged, $(wc -l < "$OUT/.fetch-files" | tr -d ' ') packages" >&2
exit 0
fi
fi
# one line a package: name <TAB> size <TAB> file <TAB> dependency patterns
# <TAB> PKGPATH
zcat "$SUMMARY" | awk '
/^PKGNAME=/ { n = substr($0, 9); dep = ""; file = ""; size = 0; path = "" }
/^PKGPATH=/ { path = substr($0, 9) }
/^FILE_NAME=/ { file = substr($0, 11) }
/^FILE_SIZE=/ { size = substr($0, 11) }
/^DEPENDS=/ { dep = dep substr($0, 9) " " }
/^$/ { if (n != "") printf "%s\t%s\t%s\t%s\t%s\n", n, size, file, dep, path; n = "" }
' > "$tmp/index"
# the base name of a pattern or a package name: up to the first -<digit>, or
# the first of >=<[{ that starts the version part
base_of()
{
echo "$1" | sed -e 's/[<>\[{].*//' -e 's/-[0-9].*//' -e 's/-$//'
}
# the newest package of the index that matches the pattern
best()
{
b=$(base_of "$1")
awk -F'\t' -v b="$b" '{ n = $1; sub(/-[0-9].*/, "", n); if (n == b) print $1 }' "$tmp/index" |
while read -r cand; do
"$PKGADMIN" pmatch "$1" "$cand" 2>/dev/null && echo "$cand"
done | newest
}
# the wanted packages, by PKGPATH: the name of a package is not the last part
# of its path (net/samba4 is samba-4.x, sysutils/py-borgbackup py313-...);
# the newest package of that path, as a pattern for the closure below
: > "$tmp/want"
for p in $("$TOP/tools/pkglist.sh" fetch; "$TOP/tools/pkglist.sh" games; "$TOP/tools/pkglist.sh" museum); do
n=$(awk -F'\t' -v p="$p" '$5 == p { print $1 }' "$tmp/index" |
newest)
if [ -n "$n" ]; then echo "$n" >> "$tmp/want"; else echo "${p##*/}" >> "$tmp/want"; fi
done
[ -s "$tmp/want" ] || die "no packages named (packages.list: fetch)"
# closure
: > "$tmp/done"
queue=$(cat "$tmp/want")
missing=
while [ -n "$queue" ]; do
set -- $queue
# the queue can hold only blanks (a package without dependencies)
[ $# -gt 0 ] || break
pat=$1
shift
queue="$*"
pkg=$(best "$pat")
if [ -z "$pkg" ]; then
missing="$missing $pat"
continue
fi
grep -qx "$pkg" "$tmp/done" && continue
echo "$pkg" >> "$tmp/done"
deps=$(awk -F'\t' -v p="$pkg" '$1 == p { print $4 }' "$tmp/index")
queue="$queue $deps"
done
sort -u "$tmp/done" > "$tmp/pkgs"
[ -s "$tmp/pkgs" ] || die "none of the wanted packages found in $REPO/pkg_summary.gz"
total=0
while read -r p; do
s=$(awk -F'\t' -v p="$p" '$1 == p { print $2 }' "$tmp/index")
total=$((total + s))
[ "$CMD" = list ] && printf '%12d %s\n' "$s" "$p"
done < "$tmp/pkgs"
echo "pkg-binaries: $ARCH $BRANCH: $(wc -l < "$tmp/pkgs" | tr -d ' ') packages, $total bytes" >&2
[ -n "$missing" ] && echo "pkg-binaries: not published for $ARCH:$missing" >&2
[ "$CMD" = fetch ] || exit 0
fail=0
stale=
: > "$tmp/files"
while read -r p; do
file=$(awk -F'\t' -v p="$p" '$1 == p { print $3 }' "$tmp/index")
size=$(awk -F'\t' -v p="$p" '$1 == p { print $2 }' "$tmp/index")
echo "$file $size" >> "$tmp/files"
dst=$OUT/All/$file
have=$({ wc -c < "$dst"; } 2>/dev/null | tr -d ' ')
if [ "${have:-0}" != "$size" ]; then
curl -s -f -m 1800 -o "$dst.part" "$REPO/$file" || { echo "FAIL: $file"; fail=1; continue; }
mv "$dst.part" "$dst"
have=$(wc -c < "$dst" | tr -d ' ')
# The summary is cached, and the project rebuilds the quarterly branch
# under it: an archive whose size differs from the summary's is a
# newer build, not a damaged download (measured 2026-11-23: 24 of 282
# packages, the cached summary of 2026-11-19). Drop the bad file and
# the summary, so the next run reads the current one.
[ "$have" = "$size" ] || { echo "FAIL: $file is $have bytes, want $size"; rm -f "$dst"; stale=1; fail=1; }
fi
done < "$tmp/pkgs"
# the summary of what is here: the records of these packages only
zcat "$SUMMARY" | awk -v list="$tmp/pkgs" '
BEGIN { while ((getline l < list) > 0) want[l] = 1 }
/^PKGNAME=/ { keep = (substr($0, 9) in want) }
keep
' | gzip -9n > "$OUT/All/pkg_summary.gz"
if [ $fail -ne 0 ]; then
rm -f "$OUT/.fetch-key"
[ -z "${stale:-}" ] || { rm -f "$SUMMARY"; echo "pkg-binaries: the cached summary is out of date and was removed; run again"; }
die "some packages failed"
fi
# what this fetch put here, for the next run to see that nothing changed
cp "$tmp/files" "$OUT/.fetch-files"
printf '%s\n' "$fetchkey" > "$OUT/.fetch-key"
echo "pkg-binaries: $OUT/All ($(du -sh "$OUT/All" | cut -f1))"