In the chroot path every package of packages.list went through 'install', which keeps an old registration and builds no archive: build/packages/All held 64 packages and none of the five the media's repository names, so make_repo said 'no package for: junction-0.1 wikid-0.1 ...' and the release stopped. The target is now per package, as the cross path already had it. build-lock: the nested path (make release -> sourcesets -> build-lock) ran sh -c without dropping MAKEFLAGS, so step 5 hit the same read-only .MAKE.LEVEL.ENV as step 4 did.
130 lines
4.5 KiB
Bash
Executable File
130 lines
4.5 KiB
Bash
Executable File
#!/bin/sh
|
|
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
#
|
|
# build-lock.sh -- one build at a time in one tree.
|
|
#
|
|
# usage: build-lock.sh 'command && command ...'
|
|
# build-lock.sh --check # is a build running?
|
|
# build-lock.sh --held # am I inside the build lock?
|
|
#
|
|
# The build tree is shared state: build.sh's distribution, the sets and the
|
|
# CD images all read and write build/obj, and clean_METALOG moves
|
|
# METALOG.sanitised while another target may still need it. Two builds in
|
|
# one tree therefore corrupt each other; the symptom is a "missing
|
|
# METALOG.sanitised" in copy-releasedir, not a useful error (t0286).
|
|
#
|
|
# The lock is a directory under build/; it holds the pid of the shell that
|
|
# runs the command and the start time of that process (so a pid reused after
|
|
# a reboot is not mistaken for a live build). A stale lock (the pid is gone,
|
|
# or the start time differs) is taken over.
|
|
#
|
|
# The lock is re-entrant: the command runs with BUILD_LOCK_HELD set to the
|
|
# lock directory, and a nested build-lock.sh call (make cdtest -> make
|
|
# release, for example) uses that instead of taking the lock again.
|
|
|
|
set -eu
|
|
|
|
TOP=$(cd "$(dirname "$0")/.." && pwd)
|
|
LOCK=$TOP/build/.build.lock
|
|
|
|
die()
|
|
{
|
|
echo "build-lock: $*" >&2
|
|
exit 1
|
|
}
|
|
|
|
# The start time of a pid, in a form that can be compared. lstart is the
|
|
# long form (Linux, NetBSD ps); start is the short fallback. Empty if the
|
|
# ps cannot tell.
|
|
pid_start()
|
|
{
|
|
start=$(ps -p "$1" -o lstart= 2>/dev/null |
|
|
sed -e 's/^ *//' -e 's/ *$//')
|
|
[ -n "$start" ] ||
|
|
start=$(ps -p "$1" -o start= 2>/dev/null | tr -d ' ')
|
|
printf '%s\n' "$start"
|
|
}
|
|
|
|
case ${1:-} in
|
|
--check)
|
|
# Read only: is another build running? A process inside the lock (make
|
|
# test -> tools/test.sh) is not "another build".
|
|
if [ -n "${BUILD_LOCK_HELD:-}" ] && [ -d "$BUILD_LOCK_HELD" ]; then
|
|
exit 0
|
|
fi
|
|
[ -d "$LOCK" ] || exit 0
|
|
pid=$(cat "$LOCK/pid" 2>/dev/null || true)
|
|
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
|
|
start=$(cat "$LOCK/start" 2>/dev/null || true)
|
|
now=$(pid_start "$pid")
|
|
if [ -z "$start" ] || [ -z "$now" ] || [ "$start" = "$now" ]; then
|
|
die "another build holds $LOCK (pid $pid)"
|
|
fi
|
|
fi
|
|
exit 0
|
|
;;
|
|
--held)
|
|
# For the tools that write build/obj or releasedir: they refuse to run
|
|
# outside a build lock (make, or build-lock.sh '...').
|
|
if [ -z "${BUILD_LOCK_HELD:-}" ] || [ ! -d "$BUILD_LOCK_HELD" ]; then
|
|
die "not inside a build; run this through make or tools/build-lock.sh"
|
|
fi
|
|
exit 0
|
|
;;
|
|
esac
|
|
|
|
[ $# -eq 1 ] || die "usage: build-lock.sh 'command && command ...'"
|
|
|
|
# A nested call (make cdtest -> make release, or a tool started by the lock)
|
|
# already runs inside the lock.
|
|
if [ -n "${BUILD_LOCK_HELD:-}" ] && [ -d "$BUILD_LOCK_HELD" ]; then
|
|
unset MAKEFLAGS MAKELEVEL MFLAGS
|
|
sh -c "$1"
|
|
exit $?
|
|
fi
|
|
|
|
mkdir -p "$TOP/build"
|
|
|
|
if ! mkdir "$LOCK" 2>/dev/null; then
|
|
pid=$(cat "$LOCK/pid" 2>/dev/null || true)
|
|
start=$(cat "$LOCK/start" 2>/dev/null || true)
|
|
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
|
|
now=$(pid_start "$pid")
|
|
# No stored start (old lock) or no ps answer: trust the pid.
|
|
if [ -z "$start" ] || [ -z "$now" ] || [ "$start" = "$now" ]; then
|
|
die "another build holds $LOCK (pid $pid); refusing to start a second one"
|
|
fi
|
|
# The pid lives, but it is another process: stale after a reboot.
|
|
fi
|
|
rm -rf "$LOCK"
|
|
mkdir "$LOCK" 2>/dev/null ||
|
|
die "cannot take over the stale lock $LOCK"
|
|
fi
|
|
echo $$ > "$LOCK/pid"
|
|
start=$(pid_start $$)
|
|
[ -n "$start" ] && echo "$start" > "$LOCK/start"
|
|
|
|
# The command runs in the foreground, in this shell's process group: a
|
|
# terminal's INT or TERM reaches it directly. It must not be started with
|
|
# "&": an asynchronous list in a shell without job control has SIGINT and
|
|
# SIGQUIT ignored, and every program the build starts inherits that -- a test
|
|
# that sends SIGINT to its own child then waits forever (McdeSys
|
|
# test_sysload, measured 2026-11-16).
|
|
cleanup()
|
|
{
|
|
rm -rf "$LOCK"
|
|
}
|
|
trap 'cleanup; exit 130' INT TERM HUP
|
|
trap cleanup EXIT
|
|
|
|
BUILD_LOCK_HELD=$LOCK; export BUILD_LOCK_HELD
|
|
# The build make exports `.MAKE.LEVEL.ENV=MAKELEVEL' in MAKEFLAGS (Debian's
|
|
# bmake does; NetBSD's does not). A pkgsrc bmake started below this shell
|
|
# reads it and dies with `Cannot delete ".MAKE.LEVEL.ENV" as it is read-only',
|
|
# so every `bmake -V' that tools/makronix-distfiles.sh and tools/ext-dist.sh
|
|
# run in a package directory returned nothing -- step 4 of a release said
|
|
# "0 archives taken from" and then failed on every archive it had to fetch
|
|
# (measured 2026-11-23, the cross build on Linux).
|
|
unset MAKEFLAGS MAKELEVEL MFLAGS
|
|
sh -c "$1"
|