Files
2026-10-29 09:15:34 -04:00

161 lines
5.0 KiB
Bash

# build-driver.sh: runs INSIDE gsandbox as /.glacier/in/driver
# (BUILD_DESIGN.txt section 6). POSIX sh: the build root may only have
# busybox. Everything it reads is under /.glacier/in, written by
# gpkg/syspkg:
#
# env NAME, VER, BUILD, PREFIX, SOURCES, PATCHES,
# CONFIGURE_ARGS, TRAITS, JOBS, CFLAGS, CXXFLAGS,
# LDFLAGS (shell assignments, every value quoted)
# bundle.grcp the recipe bundle (already checked by the library)
# sources/FILE each source, hash already checked
# manifest.gpm.cfg the package's manifest
#
# It writes /.glacier/out/NAME-VER.gpkg and nothing else counts. The
# package is checked again outside, so nothing here is trusted.
#
# PREFIX is where the package's usr/ will be linked (/usr for base,
# else /glacier/sys/links or the user's links), so the build compiles
# in the paths its files really have. It installs into DESTDIR$PREFIX,
# which becomes the package's usr/. During the build PREFIX is a link
# to /usr, where the layers are, so paths taken from them (.pc files,
# libtool archives) resolve.
set -eu
. /.glacier/in/env
say() { printf '==> %s\n' "$*"; }
die() { printf '==> ERROR: %s\n' "$*"; exit 1; }
# trait NAME: whether trait NAME is on (for build hooks).
trait() {
for t in $TRAITS; do
[ "$t" = "$1" ] && return 0
done
return 1
}
B=/build
R=$B/recipe/$NAME-$VER
DESTDIR=$B/dest
mkdir -p $B/recipe $B/src $DESTDIR $B/pkg
export DESTDIR PREFIX CFLAGS CXXFLAGS LDFLAGS
export MAKEFLAGS="-j$JOBS"
case $PREFIX in
/usr) ;;
/?*) mkdir -p "${PREFIX%/*}" && ln -s /usr "$PREFIX" ;;
*) die "Bad PREFIX $PREFIX" ;;
esac
say "Unpacking $NAME $VER"
tar -xzf /.glacier/in/bundle.grcp -C $B/recipe
cd $B/src
for f in $SOURCES; do
case $f in
*.tar|*.tar.gz|*.tgz|*.tar.xz|*.txz|*.tar.bz2|*.tbz2|*.tar.zst|*.tzst)
tar -xf "/.glacier/in/sources/$f" ;;
*)
cp "/.glacier/in/sources/$f" . ;;
esac
done
# The source tree: the one directory the sources unpacked into, else
# the unpack directory itself.
S=$B/src
n=0
for d in $B/src/* $B/src/.[!.]*; do
[ -d "$d" ] && { n=$((n + 1)); one=$d; }
done
[ "$n" = 1 ] && S=$one
cd "$S"
for p in $PATCHES; do
say "Applying $p"
patch -p1 < "$R/patches/$p"
done
# configure_args CMD...: run CMD with CONFIGURE_ARGS appended. Those
# are quoted words: the recipe's CONFIGURE, the trait arguments, then
# the per-package CONFIGURE.
configure_args() { eval "set -- \"\$@\" $CONFIGURE_ARGS"; "$@"; }
if [ -z "$BUILD" ]; then
if [ -f CMakeLists.txt ]; then BUILD=cmake
elif [ -f meson.build ]; then BUILD=meson
# configure BEFORE a bare Makefile (CLAUDE.md bug 8)
elif [ -f configure ] || [ -f configure.ac ]; then BUILD=autotools
elif [ -f Makefile ] || [ -f GNUmakefile ] || [ -f makefile ]; then BUILD=make
else die "Cannot tell how to build $NAME: set BUILD in the recipe"
fi
say "Build system: $BUILD"
fi
case $BUILD in
autotools)
prepare() { [ -x configure ] || autoreconf -fi; }
configure() { configure_args ./configure --prefix="$PREFIX" --sysconfdir=/etc --localstatedir=/var; }
compile() { make; }
package() { make install DESTDIR="$DESTDIR"; }
;;
cmake)
prepare() { :; }
configure() { configure_args cmake -B _build -DCMAKE_INSTALL_PREFIX="$PREFIX" -DCMAKE_BUILD_TYPE=Release; }
compile() { cmake --build _build -j "$JOBS"; }
package() { DESTDIR="$DESTDIR" cmake --install _build; }
;;
meson)
prepare() { :; }
configure() { configure_args meson setup _build --prefix="$PREFIX" --buildtype=release; }
compile() { meson compile -C _build -j "$JOBS"; }
package() { DESTDIR="$DESTDIR" meson install -C _build; }
;;
make)
prepare() { :; }
configure() { :; }
compile() { configure_args make PREFIX="$PREFIX"; }
package() { make install PREFIX="$PREFIX" DESTDIR="$DESTDIR"; }
;;
hooks)
prepare() { :; }
configure() { :; }
compile() { :; }
package() { :; }
;;
*)
die "Unknown build system $BUILD"
;;
esac
# The recipe's own hooks replace any of the four.
[ -f "$R/build" ] && . "$R/build"
say "Preparing"; prepare
say "Configuring"; configure
say "Compiling"; compile
say "Installing into DESTDIR"; package
[ -n "$(ls -A "$DESTDIR")" ] || die "$NAME installed nothing"
# DESTDIR$PREFIX is the package's usr/; the rest (etc/, var/) stays.
if [ "$PREFIX" != /usr ] && [ -d "$DESTDIR$PREFIX" ]; then
[ -e "$DESTDIR/usr" ] && die "$NAME installed into both $PREFIX and /usr"
mv "$DESTDIR$PREFIX" "$B/usr"
d=$DESTDIR$PREFIX
while d=${d%/*}; [ "$d" != "$DESTDIR" ]; do
rmdir "$d" 2>/dev/null || die "$NAME installed files beside $PREFIX: ${d#$DESTDIR}"
done
mv "$B/usr" "$DESTDIR/usr"
fi
say "Packing $NAME-$VER.gpkg"
mkdir -p "$B/pkg/$NAME-$VER"
mv "$DESTDIR" "$B/pkg/$NAME-$VER/files"
cp /.glacier/in/manifest.gpm.cfg "$B/pkg/$NAME-$VER/manifest.gpm.cfg"
# Hardlinks become plain files: glacier refuses a package with a
# hardlink entry.
find "$B/pkg/$NAME-$VER" -type f -links +1 | while IFS= read -r f; do
cp -p "$f" "$f.unlink" && mv -f "$f.unlink" "$f" || die "Cannot copy hardlink $f"
done
tar -czf "/.glacier/out/$NAME-$VER.gpkg" -C "$B/pkg" "$NAME-$VER"
say "Done"