kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

extract.sh (2798B)


      1 #!/usr/bin/env bash
      2 # test/libc/glibc/extract.sh — build a glibc sysroot container image
      3 # and unpack /sysroot from it into $XDG_CACHE_HOME/kit/sysroots/linux-glibc[-ARCH]/. Cached after the first successful run; pass `-f` to force a
      4 # rebuild.
      5 #
      6 # Usage:
      7 #   extract.sh              # default aarch64 -> $XDG_CACHE_HOME/kit/sysroots/linux-glibc/
      8 #   extract.sh -a aarch64   # same as default
      9 #   extract.sh -a x64       # x86_64       -> $XDG_CACHE_HOME/kit/sysroots/linux-glibc-x64/
     10 #   extract.sh -a rv64      # riscv64      -> $XDG_CACHE_HOME/kit/sysroots/linux-glibc-rv64/
     11 #
     12 # Arch-specific Containerfiles live next to this script
     13 # (Containerfile, Containerfile.x64, Containerfile.rv64).
     14 #
     15 # The aarch64 sysroot uses the bare `glibc-sysroot` name (no arch suffix)
     16 # for consistency with existing test-libc-glibc usage. test-libc on Linux
     17 # uses the arch-suffixed paths uniformly and re-targets `glibc-sysroot`
     18 # for aarch64.
     19 #
     20 # Output layout (per arch):
     21 #   $XDG_CACHE_HOME/kit/sysroots/linux-glibc[-ARCH]/
     22 #     lib/      Scrt1.o crti.o crtn.o libc_nonshared.a libc.so.6 ...
     23 #     lib64/    (x64 only) ld-linux-x86-64.so.2
     24 #     include/  glibc + linux uapi tree
     25 #     PROVENANCE
     26 set -eu
     27 
     28 ROOT="$(cd "$(dirname "$0")/../../.." && pwd)"
     29 _KIT_SYSROOTS="${XDG_CACHE_HOME:-$HOME/.cache}/kit/sysroots"
     30 ARCH=aarch64
     31 FORCE=0
     32 
     33 while [ $# -gt 0 ]; do
     34     case "$1" in
     35         -a) ARCH="$2"; shift 2 ;;
     36         --arch=*) ARCH="${1#--arch=}"; shift ;;
     37         -f|--force) FORCE=1; shift ;;
     38         *) echo "unknown arg: $1" >&2; exit 2 ;;
     39     esac
     40 done
     41 
     42 case "$ARCH" in
     43     aarch64)
     44         SYSROOT="$_KIT_SYSROOTS/linux-glibc"
     45         PLATFORM="linux/arm64"
     46         CFILE="Containerfile"
     47         TAG="kit-glibc-sysroot"
     48         ;;
     49     x64)
     50         SYSROOT="$_KIT_SYSROOTS/linux-glibc-x64"
     51         PLATFORM="linux/amd64"
     52         CFILE="Containerfile.x64"
     53         TAG="kit-glibc-sysroot-x64"
     54         ;;
     55     rv64)
     56         SYSROOT="$_KIT_SYSROOTS/linux-glibc-rv64"
     57         PLATFORM="linux/riscv64"
     58         CFILE="Containerfile.rv64"
     59         TAG="kit-glibc-sysroot-rv64"
     60         ;;
     61     *)
     62         echo "extract.sh: unknown arch '$ARCH' (want aarch64|x64|rv64)" >&2
     63         exit 2
     64         ;;
     65 esac
     66 
     67 if [ -f "$SYSROOT/PROVENANCE" ] && [ $FORCE -eq 0 ]; then
     68     echo "glibc sysroot already present at $SYSROOT (use -f to rebuild)"
     69     exit 0
     70 fi
     71 
     72 if ! command -v podman >/dev/null 2>&1; then
     73     echo "extract.sh: podman is required" >&2
     74     exit 1
     75 fi
     76 
     77 cd "$ROOT/test/libc/glibc"
     78 echo "Building $TAG (Debian $ARCH + libc6-dev)..."
     79 podman build --platform "$PLATFORM" -f "$CFILE" -t "$TAG" . >/dev/null
     80 
     81 rm -rf "$SYSROOT"
     82 mkdir -p "$SYSROOT"
     83 
     84 echo "Extracting sysroot to $SYSROOT..."
     85 podman run --rm --platform "$PLATFORM" "$TAG" | tar -C "$SYSROOT" -xf -
     86 
     87 echo "Done. Provenance:"
     88 cat "$SYSROOT/PROVENANCE"