Files
makronix/tools/pkgchroot.sh
T
lotherk 8aef98293a pkgchroot: the build key is the recipe's content, not its mtime
The mtime test said nothing on the MacBook: a git checkout does not renew the
time of a file whose content changed in an earlier commit, so
/usr/src/pkgsrc/wm/mcde/Makefile kept 2026-11-22 10:27 after mcde 288d1383 was
checked out, and the work directory of 21:55 counted as newer.  The release
then installed the binary of the day before again (run f, 12:30: no rebuild).

sandbox_stale() now hashes what the build reads -- Makefile, options.mk,
patches, PLIST, distinfo (which tools/ext-dist.sh writes from the archive) --
and compares that with the key it stored after the last build.  A package
without a key is rebuilt once, which is the cost of the fix.

Test: key for wm/mcde computes (dc7b25d0...); dash -n; the release is not run
with it yet.
2026-11-23 12:33:33 +00:00

237 lines
9.8 KiB
Bash
Executable File

#!/bin/sh
# Build pkgsrc packages inside the target sysroot (build/obj/destdir.<arch>),
# the way NetBSD builds its own packages: chroot into the target system, build
# with its compiler, its libraries and its package tools, install into its
# /usr/pkg. Nothing of the build host ends up in the package.
#
# License: CNCL 1.0 (see LICENSE at the root of the tree)
set -eu
# mount(8), umount(8) and chroot(8) live in /sbin and /usr/sbin; a build
# started from cron or over ssh may come with a PATH that has neither
PATH=$PATH:/sbin:/usr/sbin
export PATH
TOP=$(cd "$(dirname "$0")/.." && pwd)
ARCH=${ARCH:-amd64}
D=$TOP/build/obj/destdir.$ARCH
SB=$TOP/build/sandbox
MNT=$D/mnt
umount_all()
{
# NetBSD's mount(8) prints "source on target type fs (local)": the target is
# the field after "on", not $2. Unmounting a parent before its children
# leaves mounts on a detached tree, and a second mount on the same target
# stacks -- lookups then find the bottom layer, an empty old directory.
# So: deepest first (reverse mount order), repeatedly, until nothing is left.
targets=$(mount |
awk -v p=" on $D/" 'index($0, p) {
sub(/^.* on /, ""); sub(/ type .*/, ""); print
}' | sed -n '1!G;h;$p')
for t in $targets; do
while mount | awk -v x=" on $t type" 'index($0, x)' | grep -q .; do
umount "$t" || break
done
done
}
sandbox_mount()
{
# A mount that outlived its directory shows an empty tree (the null mount
# keeps the vnode of the old one): start from nothing, every time.
umount_all
mkdir -p "$SB/work" "$TOP/build/packages"
# The sysroot's /dev belongs to the sets and must stay as they describe it.
# The build needs working device nodes, so it gets a tmpfs that is thrown
# away afterwards (the nodes come from the sysroot's own MAKEDEV).
if ! mount | grep -q " on $D/dev "; then
cp -p "$D/dev/MAKEDEV" "$SB/MAKEDEV"
cp -p "$D/dev/MAKEDEV.local" "$SB/MAKEDEV.local" 2>/dev/null || true
mount -t tmpfs tmpfs "$D/dev"
cp -p "$SB/MAKEDEV" "$D/dev/MAKEDEV"
cp -p "$SB/MAKEDEV.local" "$D/dev/MAKEDEV.local" 2>/dev/null || true
if ! chroot "$D" /bin/sh -c 'cd /dev && ./MAKEDEV all' >/dev/null 2>&1; then
echo "pkgchroot: MAKEDEV failed in the sysroot" >&2
fi
fi
mount | grep -q " on $MNT " || {
mount -t tmpfs tmpfs "$MNT"
mkdir -p "$MNT/pkgsrc" "$MNT/distfiles" "$MNT/work" \
"$MNT/packages" "$MNT/tmp"
}
mount | grep -q " on $MNT/pkgsrc " ||
mount -t null "$TOP/pkgsrc" "$MNT/pkgsrc"
mount | grep -q " on $MNT/distfiles " ||
mount -t null "${DISTFILES:-$TOP/distfiles}" "$MNT/distfiles"
mount | grep -q " on $MNT/work " ||
mount -t null "$SB/work" "$MNT/work"
# the binary packages go where the cross build puts them, so tools/make_repo
# and the sysinst repository see one directory on either host
mount | grep -q " on $MNT/packages " ||
mount -t null "$TOP/build/packages" "$MNT/packages"
# The file index beside the package database is a cache, and pkg_add
# trusts it blindly: after a `force' run removed packages, the index still
# named digest-20220214 as the owner of /usr/pkg/bin/digest -- a package
# that was not registered and a file that did not exist. pkg_add then
# reported "digest-20220214: conflicting PLIST with digest-20220214" when
# bootstrapping digest itself, and every build that needed a tool from the
# sysroot stopped (measured 2026-11-22: tools/pkgchroot.sh force wm/mcde
# left 45 packages deleted and could not reinstall any of them).
sandbox_index
}
# Rebuild the by-file index from the +CONTENTS of the installed packages, so it
# can only name packages that are registered. pkg_admin lives in the sysroot
# (/usr/sbin, part of a base set since the package tools moved there); the
# sysroot's own copy is used so the index format matches the pkg_add inside.
sandbox_index()
{
[ -x "$D/usr/sbin/pkg_admin" ] || return 0
chroot "$D" /usr/sbin/pkg_admin -K /usr/pkg/pkgdb rebuild >/dev/null 2>&1 ||
echo "pkgchroot: rebuild of pkgdb.byfile.db failed" >&2
}
# The base bmake does not read MAKECONF, and a mk.conf in the sysroot's /etc
# would be an extra file for checkflist: the settings travel as make arguments,
# which every sub-make inherits. cncl-1.0 is Makronix's own licence (LICENSE at the
# root; ext/src/*/pkgsrc recipes name it); the cross build skips the check
# (tools/pkgsrc-cross.mk.conf), this one accepts it by name (measured
# 2026-11-23: five recipes stopped on "unacceptable license condition"). `install' builds the package and installs it
# into /usr/pkg -- which, inside the chroot, is the sysroot's /usr/pkg.
# A work directory is only that package's build if it was built from the recipe
# and archive that are in the tree now: pkgsrc keeps its cookies (.build_done,
# .package_done) until it is told otherwise, and `install' then takes the
# binary package of the last build. On the MacBook that shipped MCDE and
# splinter as they were on 2026-11-22 21:39 while their archives of 2026-11-23
# 08:38 were never extracted -- pkg_add printed "already recorded as installed"
# and the step counted as done (build/logs/release-2026-11-23e.log:30,44).
# -- 2026-11-23
# sha256 of stdin, on Linux and on NetBSD (cksum(1) there; no sha256sum), the
# same helper tools/userland.sh carries -- see the note there.
sha256_in()
{
if command -v sha256sum > /dev/null 2>&1; then
sha256sum | cut -d' ' -f1
else
cksum -a sha256 | awk '{ print $NF }'
fi
}
sandbox_stale()
{
sr_path=$1
w=$(ls -d "$SB/work/$sr_path"/work.* 2>/dev/null | head -1)
[ -n "$w" ] || w=$SB/work/$sr_path/work
[ -d "$w" ] || return 1
# What the work directory was built from, by content: a git checkout does
# not give a file a new mtime when the file's content changes only in the
# commit that touches it (measured 2026-11-23 on the MacBook: the recipe
# of mcde 288d1383 kept the time of 2026-11-22 10:27, so an mtime test
# said "not stale" and the release shipped the binary of the day before).
# The key covers the recipe (Makefile, options, patches, PLIST) and the
# archive that tools/ext-dist.sh names in distinfo.
key=$( { cat "$TOP/pkgsrc/$sr_path"/Makefile \
"$TOP/pkgsrc/$sr_path"/options.mk 2>/dev/null
find "$TOP/pkgsrc/$sr_path" -type f \( -name '*.patch' -o -name 'PLIST*' \) \
-exec cat {} + 2>/dev/null
cat "$TOP/pkgsrc/$sr_path"/distinfo 2>/dev/null; } | sha256_in )
kf=$TOP/build/pkgsrc-work/$sr_path/.makronix-build-key
if [ "$(cat "$kf" 2>/dev/null)" = "$key" ]; then
return 1
fi
echo "pkgchroot: $sr_path: recipe or archive changed since the last build -- rebuilding"
rm -rf "$w"
mkdir -p "${kf%/*}" && printf '%s\n' "$key" > "$kf"
return 0
}
sandbox_run()
{
# The names are prefixed on purpose: a plain `p=$1` here overwrote the
# caller's loop variable, so `force wm/mcde` ran its second sandbox_run
# with p=wm and reinstalled the whole category instead of the package
# (measured 2026-11-22, the MacBook began to build 2bwm and afterstep).
sr_path=$1
sr_dir=${1%/*}
mkdir -p "$SB/work/$sr_dir"
# docbook's stylesheets are local: without the catalog libxslt resolves the
# URL in the stylesheet PI and stops with "failed to load external entity
# http://docbook.sourceforge.net/..." when the build host has no route to it
# (measured 2026-11-21: xmlto, a dependency of wm/mcde, on the MacBook).
catalog=/usr/pkg/share/xsl/docbook/catalog.xml
[ -f "$D$catalog" ] || catalog=
chroot "$D" /usr/bin/env -i \
PATH=/usr/sbin:/usr/bin:/sbin:/bin \
HOME=/mnt/work \
TMPDIR=/mnt/tmp \
XML_CATALOG_FILES=$catalog \
/usr/bin/make -C "/mnt/pkgsrc/$sr_path" \
WRKOBJDIR=/mnt/work \
PACKAGES=/mnt/packages \
DISTDIR=/mnt/distfiles \
ACCEPTABLE_LICENSES=cncl-1.0 \
"${2:-install}"
}
# Programs the packages run while they are installed write state into the
# sysroot: fontconfig's cache (a font tool runs), MCDE's /var/dt (dtappgather
# at install) and the temporary files of the guide build. None of it is part
# of a set, and checkflist counts every one of them as an extra file (602 on
# 2026-11-20); none of these three paths is in a base set.
sandbox_clean()
{
rm -rf "$D/var/cache" "$D/var/dt" "$D/var/spool/calendar"
# the refcount index the package tools keep beside the database: no
# record of it is in METALOG, so checkflist counts it as an extra file
rm -rf "$D/usr/pkg/pkgdb.refcount"
rm -rf "$D"/tmp/dtinfogen-* "$D"/tmp/style_sheet.*
}
case ${1:-} in
umount) umount_all ;;
mount) sandbox_mount; echo "pkgchroot: mounted" ;;
build)
shift
sandbox_mount
trap 'umount_all' EXIT INT TERM HUP
for p in "$@"; do
echo "=== pkgchroot: $p"
# A rebuilt package must replace its registration: pkg_add only
# compares names, and the same name is what a changed recipe and a
# new archive produce. The old version goes before the install, the
# way the cross path does it (tools/userland.sh).
if sandbox_stale "$p"; then
# the name the recipe gives this build, as the recipe computes it
pn=$(chroot "$D" /usr/bin/env -i \
PATH=/usr/sbin:/usr/bin:/sbin:/bin HOME=/mnt/work \
/usr/bin/make -C "/mnt/pkgsrc/$p" WRKOBJDIR=/mnt/work \
-V PKGNAME 2>/dev/null)
[ -n "$pn" ] && chroot "$D" /usr/sbin/pkg_delete -f \
-K /usr/pkg/pkgdb "$pn" > /dev/null 2>&1 || true
fi
sandbox_run "$p" install
done
sandbox_clean
echo "pkgchroot: packages in build/packages/All"
;;
force)
# the recipe changed but the version did not: `install' sees the package
# already recorded and does nothing, so the work directory is thrown away
# and the package installed again (measured 2026-11-21: the System
# Settings change went into no build at all)
shift
sandbox_mount
trap 'umount_all' EXIT INT TERM HUP
for p in "$@"; do
echo "=== pkgchroot (force): $p"
sandbox_run "$p" clean
sandbox_run "$p" deinstall || true
sandbox_run "$p" reinstall
done
sandbox_clean
echo "pkgchroot: packages in build/packages/All"
;;
*) echo "usage: $0 mount|umount|build|force <pkgsrc/path>..." >&2; exit 2 ;;
esac