boot5-enumerate.sh (3294B)
1 #!/bin/sh 2 ## boot5-enumerate.sh — enumerate musl sources from the canonical 3 ## musl tree and emit the build-srcs / crt-mode files that 4 ## boot5-gen-runscm.sh consumes. 5 ## 6 ## Mirrors musl's Makefile rule: a per-arch override (under 7 ## $d/$MUSL_ARCH/) replaces the same-stem base file (under $d/). The 8 ## canonical tree already had the per-arch skip filter applied by 9 ## prep-src.sh, so no skip subtraction is needed here. 10 ## 11 ## Inputs: build/<arch>/src/src/musl/ (filtered tree) 12 ## Outputs: build/<arch>/src/run/boot5/build-srcs.txt 13 ## build/<arch>/src/run/boot5/crt-mode ("asm" or "c") 14 ## 15 ## Usage: bootprep/boot5-enumerate.sh <arch> 16 17 set -eu 18 [ "$#" -eq 1 ] || { echo "usage: $0 <arch>" >&2; exit 2; } 19 ARCH=$1 20 21 case "$ARCH" in 22 aarch64) MUSL_ARCH=aarch64 ;; 23 amd64) MUSL_ARCH=x86_64 ;; 24 riscv64) MUSL_ARCH=riscv64 ;; 25 *) echo "boot5-enumerate: unsupported arch '$ARCH'" >&2; exit 2 ;; 26 esac 27 28 MUSL_DIR=build/$ARCH/src/src/musl 29 OUT_DIR=build/$ARCH/src/run/boot5 30 [ -d "$MUSL_DIR" ] || { echo "missing $MUSL_DIR (run bootprep/prep-src.sh $ARCH)" >&2; exit 1; } 31 mkdir -p "$OUT_DIR" 32 33 SRC_TOP="src/aio src/conf src/crypt src/ctype src/dirent 34 src/env src/errno src/exit src/fcntl src/fenv src/internal 35 src/ipc src/legacy src/linux src/locale src/malloc 36 src/malloc/mallocng src/math src/misc src/mman src/mq 37 src/multibyte src/network src/passwd src/prng src/process 38 src/regex src/sched src/search src/select src/setjmp src/signal 39 src/stat src/stdio src/stdlib src/string src/temp src/termios 40 src/thread src/time src/unistd" 41 42 ( 43 cd "$MUSL_DIR" 44 for d in $SRC_TOP; do 45 [ -d "$d" ] || continue 46 for f in $d/*.c; do [ -f "$f" ] && echo "$f"; done 47 done 48 ) > "$OUT_DIR/base.txt" 49 50 ( 51 cd "$MUSL_DIR" 52 for d in $SRC_TOP; do 53 [ -d "$d/$MUSL_ARCH" ] || continue 54 for f in $d/$MUSL_ARCH/*.c $d/$MUSL_ARCH/*.s $d/$MUSL_ARCH/*.S; do 55 [ -f "$f" ] && echo "$f" 56 done 57 done 58 ) > "$OUT_DIR/arch.txt" 59 60 # REPLACED: bases that have arch-specific overrides (drop them from 61 # BASE). KEEP = (BASE - REPLACED) ∪ ARCH. 62 awk -v ARCH="$MUSL_ARCH" ' 63 { 64 sub(/\.[^.]*$/, "") # strip extension 65 slot = "/" ARCH "/" 66 i = index($0, slot) 67 head = substr($0, 1, i - 1) 68 tail = substr($0, i + length(slot)) 69 print head "/" tail 70 } 71 ' "$OUT_DIR/arch.txt" | sort -u > "$OUT_DIR/replaced.txt" 72 73 # Filter base by removing stems that appear in replaced. 74 awk -v REPF="$OUT_DIR/replaced.txt" ' 75 BEGIN { while ((getline l < REPF) > 0) rep[l] = 1 } 76 { 77 stem = $0 78 sub(/\.c$/, "", stem) 79 if (!(stem in rep)) print 80 } 81 ' "$OUT_DIR/base.txt" > "$OUT_DIR/keep_base.txt" 82 83 cat "$OUT_DIR/keep_base.txt" "$OUT_DIR/arch.txt" | sort -u > "$OUT_DIR/build-srcs.txt" 84 85 n_src=$(wc -l < "$OUT_DIR/build-srcs.txt") 86 n_skip=$(grep -cv '^[[:space:]]*\(#\|$\)' "$MUSL_DIR/skip.txt" || true) 87 echo "[boot5-enumerate/$ARCH] keep=$n_src skip=$n_skip (calibrated)" 88 89 # Record CRT mode (asm vs c) so boot5-gen-runscm picks the right 90 # crti/crtn source set without re-checking $MUSL_DIR. 91 if [ -f "$MUSL_DIR/crt/$MUSL_ARCH/crti.s" ]; then 92 echo asm > "$OUT_DIR/crt-mode" 93 else 94 echo c > "$OUT_DIR/crt-mode" 95 fi