boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

boot4-calibrate.sh (6266B)


      1 #!/bin/sh
      2 ## boot4-calibrate.sh — produce vendor/upstream/musl-1.2.5-skip-$ARCH.txt
      3 ##
      4 ## NOT on the boot.sh path. Generates the per-arch calibration list
      5 ## boot4.sh uses to drop skip-on-fail logic from the container. Run
      6 ## this once per architecture when the patch set, calibration arch, or
      7 ## tcc version changes; commit the resulting file alongside the rest of
      8 ## the vendored musl artifacts.
      9 ##
     10 ## What it does:
     11 ##   1. Stage the same prerequisites boot4.sh stages (boot3/tcc3,
     12 ##      libtcc1.a, vendored overrides + deletes, pre-generated headers,
     13 ##      shim).
     14 ##   2. Run a skip-on-fail compile loop in the container over every
     15 ##      musl source. Whatever tcc 0.9.26 cannot compile gets recorded.
     16 ##   3. Copy the resulting skip list out to
     17 ##      vendor/upstream/musl-1.2.5-skip-$ARCH.txt.
     18 ##
     19 ## Boot4.sh then enumerates sources on the host and subtracts this
     20 ## list, emitting a flat sequential build script with no in-container
     21 ## branch on $TCC's exit code.
     22 ##
     23 ## Usage: scripts/boot4-calibrate.sh <amd64|aarch64|riscv64>
     24 
     25 set -eu
     26 
     27 usage() { echo "usage: $0 <amd64|aarch64|riscv64>" >&2; exit 2; }
     28 [ "$#" -eq 1 ] || usage
     29 ARCH=$1
     30 
     31 case "$ARCH" in
     32     amd64)   PLATFORM=linux/amd64;  TCC_TARGET=X86_64;  MUSL_ARCH=x86_64  ;;
     33     aarch64) PLATFORM=linux/arm64;  TCC_TARGET=ARM64;   MUSL_ARCH=aarch64 ;;
     34     riscv64) PLATFORM=linux/riscv64; TCC_TARGET=RISCV64; MUSL_ARCH=riscv64 ;;
     35     *) usage ;;
     36 esac
     37 
     38 ROOT=$(cd "$(dirname "$0")/.." && pwd)
     39 cd "$ROOT"
     40 
     41 IMAGE=boot2-scratch:$ARCH
     42 BOOT3=build/$ARCH/boot3
     43 STAGE=build/$ARCH/.boot4-calibrate
     44 TCC_DIR=build/tcc/$TCC_TARGET/tcc-0.9.26-1147-gee75a10c
     45 MUSL_TARBALL=vendor/upstream/musl-1.2.5.tar.gz
     46 MUSL_OVERRIDES=vendor/upstream/musl-1.2.5-overrides
     47 MUSL_DELETES=vendor/upstream/musl-1.2.5-deletes.txt
     48 MUSL_GENERATED=vendor/upstream/musl-1.2.5-generated/$MUSL_ARCH
     49 SHIM_FILE=scripts/boot4-musl-shim-$ARCH.h
     50 SKIP_OUT=vendor/upstream/musl-1.2.5-skip-$ARCH.txt
     51 
     52 [ -x "$BOOT3/tcc3"      ] || { echo "missing $BOOT3/tcc3 (run scripts/boot3.sh $ARCH)" >&2; exit 1; }
     53 [ -e "$BOOT3/libtcc1.a" ] || { echo "missing $BOOT3/libtcc1.a" >&2; exit 1; }
     54 [ -d "$TCC_DIR/include" ] || { echo "missing $TCC_DIR/include" >&2; exit 1; }
     55 [ -e "$MUSL_TARBALL"    ] || { echo "missing $MUSL_TARBALL" >&2; exit 1; }
     56 [ -d "$MUSL_OVERRIDES"  ] || { echo "missing $MUSL_OVERRIDES" >&2; exit 1; }
     57 [ -e "$MUSL_DELETES"    ] || { echo "missing $MUSL_DELETES" >&2; exit 1; }
     58 [ -d "$MUSL_GENERATED"  ] || { echo "missing $MUSL_GENERATED (run scripts/musl-vendor.sh)" >&2; exit 1; }
     59 [ -e "$SHIM_FILE"       ] || { echo "missing $SHIM_FILE" >&2; exit 1; }
     60 
     61 if ! podman image exists "$IMAGE"; then
     62     podman build --platform "$PLATFORM" -t "$IMAGE" \
     63         -f scripts/Containerfile.scratch scripts/
     64 fi
     65 
     66 rm -rf "$STAGE"
     67 mkdir -p "$STAGE/in/tcc-include" "$STAGE/out"
     68 
     69 cp "$BOOT3/tcc3"                "$STAGE/in/tcc"
     70 cp "$BOOT3/libtcc1.a"           "$STAGE/in/libtcc1.a"
     71 cp -R "$TCC_DIR/include/."      "$STAGE/in/tcc-include/"
     72 tar xzf "$MUSL_TARBALL"         -C "$STAGE/in/"
     73 MUSL_DIR=$STAGE/in/musl-1.2.5
     74 cp -R "$MUSL_OVERRIDES/."       "$MUSL_DIR/"
     75 while read -r p; do
     76     [ -n "$p" ] && rm -rf "$MUSL_DIR/$p"
     77 done < "$MUSL_DELETES"
     78 cp "$SHIM_FILE"                 "$STAGE/in/musl-shim.h"
     79 cp "$MUSL_GENERATED/alltypes.h" "$STAGE/in/musl-alltypes.h"
     80 cp "$MUSL_GENERATED/syscall.h"  "$STAGE/in/musl-syscall.h"
     81 
     82 echo "[calibrate $ARCH] running skip-on-fail compile loop in container"
     83 podman run --rm -i --pull=never --platform "$PLATFORM" \
     84     --tmpfs /tmp:size=1024M \
     85     -e MUSL_ARCH="$MUSL_ARCH" \
     86     -v "$ROOT/$STAGE:/work" -w /work "$IMAGE" \
     87     sh -eu -s <<'CONTAINER'
     88 IN=/work/in
     89 OUT=/work/out
     90 TCC=$IN/tcc
     91 
     92 cd /tmp
     93 cp -R "$IN/musl-1.2.5" .
     94 cd musl-1.2.5
     95 
     96 mkdir -p obj/include/bits obj/src/internal
     97 cp $IN/musl-alltypes.h obj/include/bits/alltypes.h
     98 cp $IN/musl-syscall.h  obj/include/bits/syscall.h
     99 echo '#define VERSION "1.2.5-tcc-boot4"' > obj/src/internal/version.h
    100 
    101 CFLAGS_BASE="-std=c99 -nostdinc -ffreestanding -fno-strict-aliasing
    102              -D_XOPEN_SOURCE=700
    103              -I./arch/$MUSL_ARCH -I./arch/generic -Iobj/src/internal
    104              -I./src/include -I./src/internal -Iobj/include -I./include
    105              -O2 -fomit-frame-pointer
    106              -Werror=implicit-function-declaration -Werror=implicit-int
    107              -Werror=pointer-sign -Werror=pointer-arith"
    108 CFLAGS_C="$CFLAGS_BASE -include $IN/musl-shim.h"
    109 CFLAGS_ASM="$CFLAGS_BASE"
    110 
    111 SRC_TOP="src/aio src/conf src/crypt src/ctype src/dirent
    112          src/env src/errno src/exit src/fcntl src/fenv src/internal
    113          src/ipc src/legacy src/linux src/locale src/malloc
    114          src/malloc/mallocng src/math src/misc src/mman src/mq
    115          src/multibyte src/network src/passwd src/prng src/process
    116          src/regex src/sched src/search src/select src/setjmp src/signal
    117          src/stat src/stdio src/stdlib src/string src/temp src/termios
    118          src/thread src/time src/unistd"
    119 
    120 BASE_SRCS=""; ARCH_SRCS=""
    121 for d in $SRC_TOP; do
    122     [ -d "$d" ] || continue
    123     for f in $d/*.c; do [ -f "$f" ] && BASE_SRCS="$BASE_SRCS $f"; done
    124     for f in $d/$MUSL_ARCH/*.c $d/$MUSL_ARCH/*.s $d/$MUSL_ARCH/*.S; do
    125         [ -f "$f" ] && ARCH_SRCS="$ARCH_SRCS $f"
    126     done
    127 done
    128 REPLACED=""
    129 for a in $ARCH_SRCS; do
    130     p=${a%.*}
    131     head=${p%%/${MUSL_ARCH}/*}
    132     tail=${p#*/${MUSL_ARCH}/}
    133     REPLACED="$REPLACED $head/$tail"
    134 done
    135 KEEP=""
    136 for b in $BASE_SRCS; do
    137     stem=${b%.c}; skip=0
    138     for r in $REPLACED; do [ "$stem" = "$r" ] && { skip=1; break; }; done
    139     [ $skip -eq 0 ] && KEEP="$KEEP $b"
    140 done
    141 KEEP="$KEEP $ARCH_SRCS"
    142 
    143 mkdir -p obj/lib
    144 n=0; n_ok=0; n_skip=0
    145 : >$OUT/skipped.txt
    146 for src in $KEEP; do
    147     obj="obj/${src%.*}.o"
    148     mkdir -p "$(dirname $obj)"
    149     case "$src" in
    150         *.c)        flags="$CFLAGS_C" ;;
    151         *.s | *.S)  flags="$CFLAGS_ASM" ;;
    152         *)          flags="$CFLAGS_C" ;;
    153     esac
    154     if $TCC $flags -c "$src" -o "$obj" >/tmp/compile.log 2>&1; then
    155         n_ok=$((n_ok+1))
    156     else
    157         n_skip=$((n_skip+1))
    158         echo "$src" >>$OUT/skipped.txt
    159     fi
    160     n=$((n+1))
    161     [ $((n % 200)) -eq 0 ] && echo "  $n done (ok=$n_ok skip=$n_skip)"
    162 done
    163 echo "  compiled=$n_ok skipped=$n_skip total=$n"
    164 CONTAINER
    165 
    166 sort -u "$STAGE/out/skipped.txt" > "$SKIP_OUT"
    167 echo "[calibrate $ARCH] wrote $SKIP_OUT ($(wc -l <"$SKIP_OUT") entries)"