#!/bin/sh # Build pkgsrc packages inside the target sysroot (build/obj/destdir.), # 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. # pkgsrc keeps a work directory until it is told otherwise: the cookies # (.build_done, .package_done) say the work is done, and `install' then takes # the binary package that the last build produced. On this host that meant the # release shipped MCDE and splinter as they were on 2026-11-22 21:39, while the # distfiles 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). A work directory older than the # recipe or the archive is not that package's build any more: it goes. # # The comparison is by mtime, not by content: the archive is written by # tools/ext-dist.sh from the fork's HEAD, so a new commit gives it a new time, # and a recipe edit touches its Makefile. -- 2026-11-23 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 # Everything pkgsrc reads before it builds: the Makefile, distinfo (which # changes with the archive, tools/ext-dist.sh writes both), options, # patches and the PLIST. No parsing of DISTFILES/DISTNAME: a recipe may # name its archive either way, and a file in the recipe directory that is # newer than the work directory is the one fact that matters. newer=$(find "$TOP/pkgsrc/$sr_path" -newer "$w" -print -quit 2>/dev/null) [ -n "$newer" ] || return 1 echo "pkgchroot: $sr_path: ${newer#$TOP/pkgsrc/$sr_path/} is newer than the work directory -- rebuilding" rm -rf "$w" 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 ..." >&2; exit 2 ;; esac