#!/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 # The mount points are not part of any set: checkflist listed ./mnt/dev, # ./mnt/pkgsrc ... as extra files (measured 2026-11-24, MacBook). rmdir, never # rm -rf: on a directory that is still mounted it fails and removes nothing. for t in "$MNT"/*; do [ -d "$t" ] && rmdir "$t" 2>/dev/null done return 0 } 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" "$MNT/ext" "$MNT/tools" } mount | grep -q " on $MNT/pkgsrc " || mount -t null "$TOP/pkgsrc" "$MNT/pkgsrc" # The recipes of ext/ build from the source tree beside them, not from an # archive (ext/src//pkgsrc/Makefile: WRKSRC is ext/src/), so # that tree has to be visible inside the chroot as well. Read-only by # intent: the build writes into $MNT/work, and a recipe that wrote into # its own source tree would put a host path into the sysroot's package. mount | grep -q " on $MNT/ext " || mount -t null "$TOP/ext" "$MNT/ext" # The recipe's do-extract runs tools/ext-extract.sh (it refreshes the copy # in the work directory instead of copying once), so tools/ has to be # reachable inside the chroot at the same relative place: the recipes say # ${_MAKRONIX_TOP}/tools, and _MAKRONIX_TOP is /mnt there. mount | grep -q " on $MNT/tools " || mount -t null "$TOP/tools" "$MNT/tools" 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 } # The recipes of ext/ (ext/AGENTS.md) as pkgsrc paths: "misc/splinter # wm/mcde x11/mcde-motif ...". A fork's recipe lives beside its source and is # mirrored into pkgsrc by tools/ext-sync.sh; here only the path is needed. fork_recipes() { for recipe in "$TOP"/ext/src/*/pkgsrc "$TOP"/ext/src/*/pkgsrc-*; do [ -f "$recipe/Makefile" ] || continue name=$(basename "$(dirname "$recipe")") case ${recipe##*/} in pkgsrc-*) name=$name-${recipe##*/pkgsrc-} ;; esac cat=$(sed -n 's/^CATEGORIES=[[:space:]]*\([^[:space:]]*\).*/\1/p' \ "$recipe/Makefile") echo "$cat/$name" done } # The source tree a fork's recipe builds from: the one the recipe's # do-extract copies. Its commit is the build's input -- not a hash of the # whole tree, which is 286 MB for mcde (measured 2026-11-23). # A recipe that builds from an archive (afpfs-ng, an upstream tarball with its # distinfo) has no such tree: it prints nothing, and the caller keeps the # archive hash it used before. The test is the recipe itself, so a fork that # goes back to an archive needs no change here. fork_src() { r=$TOP/pkgsrc/$1 [ -f "$r/Makefile" ] || return 0 grep -q '^DISTFILES=[[:space:]]*#[[:space:]]*empty' "$r/Makefile" || return 0 case $1 in x11/mcde-motif) echo "$TOP/ext/src/mcde" ;; *) echo "$TOP/ext/src/${1##*/}" ;; esac } 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, for a # fork of ext/, the source tree the recipe builds from -- there is no # archive and no distinfo any more (the recipe's do-extract copies # ext/src/ into WRKSRC). A commit in the fork is what changes the # build, so the tree's content is part of the key. src= case " $(fork_recipes | tr '\n' ' ') " in *" $sr_path "*) src=$(fork_src "$sr_path") ;; esac # A commit is the input, not a hash of the tree: `git status' would be # cheaper still but misses the commit case (a checkout updates .git and # the files, not necessarily the mtimes). Uncommitted work is covered # too: `git status --porcelain' is not empty then, which changes the key. # A recipe that builds from an archive (fork_src prints nothing) keeps # the distinfo hash it had. srcid= if [ -n "$src" ]; then srcid=$(git -C "$src" rev-parse HEAD 2>/dev/null)$(git -C "$src" status --porcelain 2>/dev/null | sha256_in) elif [ -f "$TOP/pkgsrc/$sr_path/distinfo" ]; then srcid=$(sha256_in < "$TOP/pkgsrc/$sr_path/distinfo") fi # `|| :' on every optional file: this script runs under `set -e', and a # recipe without options.mk made cat fail and end the group before the # source id was printed, so the commit never reached the key (measured # 2026-11-24, x11/mcde-motif). find -L: a mirrored recipe is symlinks. key=$( { cat "$TOP/pkgsrc/$sr_path"/Makefile \ "$TOP/pkgsrc/$sr_path"/options.mk 2>/dev/null || : find -L "$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 || : printf '%s\n' "$srcid" } | 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= # The staging tree of a run that died between install and package: pkgsrc # removes .destdir on `package-clean' only, never before an `install', so # a stale tree makes the next install fail on a file that is already # there -- pkgconf's `ln -s pkgconf .../bin/pkg-config: File exists' # stopped x11/mcde-motif and wm/mcde (measured 2026-11-23 on the MacBook). # The cookie is what says whether that run finished. sw=$(ls -d "$SB/work/$sr_path"/work.* 2>/dev/null | head -1) [ -n "$sw" ] || sw=$SB/work/$sr_path/work if [ -d "$sw/.destdir" ] && [ ! -f "$sw/.install_done" ]; then rm -rf "$sw/.destdir" fi 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 \ PKGSRC_KEEP_BIN_PKGS=yes \ "${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 # The target comes first: a package of the "makronix" set is installed # into the sysroot (install, the default), a package of the media's # repository is built as a binary package (package) -- tools/userland.sh # passes it, because `install' keeps an old registration and builds no # archive, so build/packages/All stayed empty and make_repo had nothing # to put on the media (measured 2026-11-23 on the MacBook). tgt=install case ${1:-} in install|package) tgt=$1; shift ;; esac sandbox_mount trap 'umount_all' EXIT INT TERM HUP for p in "$@"; do echo "=== pkgchroot: $p ($tgt)" # 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" "$tgt" 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