#!/bin/sh # License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. # # ext-resync.sh -- bring the work directory of a fork up to date by what changed # # usage: tools/ext-resync.sh resync PKGPATH e.g. wm/mcde # tools/ext-resync.sh snapshot PKGPATH # # A fork (ext/src/, ext/AGENTS.md) is built by pkgsrc from an archive of # its last commit. pkgsrc extracts it once; a new archive then meant `bmake # clean' and a build from nothing -- ten minutes of MCDE for one changed file # (build/logs/userland-2026-11-14p.log). `resync' puts into the work directory # only the files that differ between the archive pkgsrc extracted (kept, as # build/pkgsrc-work/PKGPATH/.pristine) and the new one. A changed file has a new # time stamp and make builds what depends on it, nothing else. # # resync exits 0 when the work directory is up to date (or has nothing to # resync) and 1 when it cannot say: no work directory, no .pristine, a file # that pkgsrc changed after extracting it (its substitutions) or a file that # decides how the tree is configured (configure.ac, a new Makefile.am, m4). # The caller then does `bmake clean', and after the build `snapshot' keeps the # archive that was extracted. set -eu TOP=$(cd "$(dirname "$0")/.." && pwd) PKGSRC=$TOP/pkgsrc BMAKE=$TOP/build/pkg/bin/bmake MAKRONIX_TOP=$TOP export MAKRONIX_TOP die() { echo "ext-resync: $*" >&2; exit 2; } [ $# -eq 2 ] || die "usage: $0 resync|snapshot PKGPATH" cmd=$1 p=$2 [ -f "$PKGSRC/$p/Makefile" ] || die "no $PKGSRC/$p" var() { (cd "$PKGSRC/$p" && "$BMAKE" show-var VARNAME="$1"); } wd=$TOP/build/pkgsrc-work/$p pristine=$wd/.pristine distdir=$(var DISTDIR) distfile=$(var DISTFILES | awk '{print $1}') archive=$distdir/$distfile [ -f "$archive" ] || die "no archive $archive" tmp=${TMPDIR:-/tmp}/ext-resync.$$ trap 'rm -rf "$tmp"' EXIT HUP INT TERM mkdir -p "$tmp" extract_to() { # DIR: the archive's files, in DIR/ mkdir -p "$1" tar -xzf "$archive" -C "$1" } case $cmd in snapshot) rm -rf "$pristine" extract_to "$pristine" echo "ext-resync: $p: archive kept in ${pristine#$TOP/}" exit 0 ;; resync) ;; *) die "unknown command $cmd" ;; esac wrksrc=$(var WRKSRC) [ -d "$wrksrc" ] || exit 1 [ -d "$pristine" ] || { echo "ext-resync: $p: no .pristine, cleaning"; exit 1; } top=$(ls "$pristine" | head -1) [ -n "$top" ] || exit 1 extract_to "$tmp/new" newtop=$(ls "$tmp/new" | head -1) old=$pristine/$top new=$tmp/new/$newtop (cd "$new" && find . -type f | sort) > "$tmp/newfiles" (cd "$old" && find . -type f | sort) > "$tmp/oldfiles" : > "$tmp/changed" : > "$tmp/added" while IFS= read -r f; do if [ ! -f "$old/$f" ]; then echo "$f" >> "$tmp/added" elif ! cmp -s "$old/$f" "$new/$f"; then echo "$f" >> "$tmp/changed" fi done < "$tmp/newfiles" comm -23 "$tmp/oldfiles" "$tmp/newfiles" > "$tmp/removed" needs_configure=0 n=$(cat "$tmp/changed" "$tmp/added" "$tmp/removed" | wc -l | tr -d ' ') if [ "$n" -eq 0 ]; then echo "ext-resync: $p: nothing changed" exit 0 fi # decide whether the reconfigured tree needs a fresh configure pass: a # configure.ac / Makefile.am / m4 change only invalidates configure, not the # already built objects, so we keep them and let bmake re-run pre-configure # (autoreconf, SUBST). No full `bmake clean' any more (tasksd t0231). while IFS= read -r f; do case $f in ./configure.ac|*/configure.ac|*/acinclude.m4|*.m4) needs_configure=1 ;; esac done < "$tmp/changed" while IFS= read -r f; do case $f in */Makefile.am|*/Makefile.in.in) needs_configure=1 ;; esac done < "$tmp/added" # a file pkgsrc changed after extracting it (SUBST, patches) is no longer the # archive's: putting the new one over it would lose that -- except for the # configure inputs, where re-running configure re-applies the substitution while IFS= read -r f; do case $f in *.md|*.txt|*/AGENTS.md) continue ;; esac if [ -f "$wrksrc/$f" ] && ! cmp -s "$old/$f" "$wrksrc/$f"; then # the work directory holds the new file already: nothing of pkgsrc # to lose. A .pristine older than the extract made every file # changed since then look like pkgsrc's, and MCDE was built from # nothing for a header it already had (build/logs/ # userland-2026-11-17f.log:11, include/Dt/WsmP.h). cmp -s "$new/$f" "$wrksrc/$f" && continue case $f in ./configure.ac|*/configure.ac|*/acinclude.m4|*.m4|*/Makefile.am|*/Makefile.in.in) needs_configure=1 ;; *) echo "ext-resync: $p: pkgsrc changed $f, cleaning"; exit 1 ;; esac fi done < "$tmp/changed" # a file the work directory already has as the new archive has it is not # copied: a copy is a new time stamp, and make would compile what includes it while IFS= read -r f; do cmp -s "$new/$f" "$wrksrc/$f" && continue mkdir -p "$wrksrc/$(dirname "$f")" cp "$new/$f" "$wrksrc/$f" done < "$tmp/changed" while IFS= read -r f; do cmp -s "$new/$f" "$wrksrc/$f" && continue mkdir -p "$wrksrc/$(dirname "$f")" cp "$new/$f" "$wrksrc/$f" done < "$tmp/added" while IFS= read -r f; do rm -f "$wrksrc/$f" done < "$tmp/removed" # build and install again (pkgsrc's cookies); keep the configured tree and only # drop configure's cookie when the configure inputs changed, so autoreconf and # SUBST run again without throwing away built objects (tasksd t0231) wdir=$(dirname "$wrksrc") # .package_done too: in a cross build `install' adds the binary package, and # with the cookie kept pkgsrc does not make it again -- the old one was # installed over a fresh build (pkgsrc/mk/package/package.mk:35, 2026-11-17) rm -f "$wdir/.build_done" "$wdir/.install_done" "$wdir/.package_done" \ "$wdir"/.PLIST* [ "$needs_configure" = 1 ] && rm -f "$wdir/.configure_done" rm -rf "$pristine" mv "$tmp/new" "$pristine" echo "ext-resync: $p: $(wc -l < "$tmp/changed" | tr -d ' ') changed," \ "$(wc -l < "$tmp/added" | tr -d ' ') new, $(wc -l < "$tmp/removed" | tr -d ' ') removed" exit 0