boot5-enumerate.sh (3467B)
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 # Source enumeration becomes archive member order, so it must not inherit 19 # the host's locale collation (for example, whether '_' sorts before '.'). 20 LC_ALL=C 21 export LC_ALL 22 23 [ "$#" -eq 1 ] || { echo "usage: $0 <arch>" >&2; exit 2; } 24 ARCH=$1 25 26 case "$ARCH" in 27 aarch64) MUSL_ARCH=aarch64 ;; 28 amd64) MUSL_ARCH=x86_64 ;; 29 riscv64) MUSL_ARCH=riscv64 ;; 30 *) echo "boot5-enumerate: unsupported arch '$ARCH'" >&2; exit 2 ;; 31 esac 32 33 MUSL_DIR=build/$ARCH/src/src/musl 34 OUT_DIR=build/$ARCH/src/run/boot5 35 [ -d "$MUSL_DIR" ] || { echo "missing $MUSL_DIR (run bootprep/prep-src.sh $ARCH)" >&2; exit 1; } 36 mkdir -p "$OUT_DIR" 37 38 SRC_TOP="src/aio src/conf src/crypt src/ctype src/dirent 39 src/env src/errno src/exit src/fcntl src/fenv src/internal 40 src/ipc src/legacy src/linux src/locale src/malloc 41 src/malloc/mallocng src/math src/misc src/mman src/mq 42 src/multibyte src/network src/passwd src/prng src/process 43 src/regex src/sched src/search src/select src/setjmp src/signal 44 src/stat src/stdio src/stdlib src/string src/temp src/termios 45 src/thread src/time src/unistd" 46 47 ( 48 cd "$MUSL_DIR" 49 for d in $SRC_TOP; do 50 [ -d "$d" ] || continue 51 for f in $d/*.c; do [ -f "$f" ] && echo "$f"; done 52 done 53 ) > "$OUT_DIR/base.txt" 54 55 ( 56 cd "$MUSL_DIR" 57 for d in $SRC_TOP; do 58 [ -d "$d/$MUSL_ARCH" ] || continue 59 for f in $d/$MUSL_ARCH/*.c $d/$MUSL_ARCH/*.s $d/$MUSL_ARCH/*.S; do 60 [ -f "$f" ] && echo "$f" 61 done 62 done 63 ) > "$OUT_DIR/arch.txt" 64 65 # REPLACED: bases that have arch-specific overrides (drop them from 66 # BASE). KEEP = (BASE - REPLACED) ∪ ARCH. 67 awk -v ARCH="$MUSL_ARCH" ' 68 { 69 sub(/\.[^.]*$/, "") # strip extension 70 slot = "/" ARCH "/" 71 i = index($0, slot) 72 head = substr($0, 1, i - 1) 73 tail = substr($0, i + length(slot)) 74 print head "/" tail 75 } 76 ' "$OUT_DIR/arch.txt" | sort -u > "$OUT_DIR/replaced.txt" 77 78 # Filter base by removing stems that appear in replaced. 79 awk -v REPF="$OUT_DIR/replaced.txt" ' 80 BEGIN { while ((getline l < REPF) > 0) rep[l] = 1 } 81 { 82 stem = $0 83 sub(/\.c$/, "", stem) 84 if (!(stem in rep)) print 85 } 86 ' "$OUT_DIR/base.txt" > "$OUT_DIR/keep_base.txt" 87 88 cat "$OUT_DIR/keep_base.txt" "$OUT_DIR/arch.txt" | sort -u > "$OUT_DIR/build-srcs.txt" 89 90 n_src=$(wc -l < "$OUT_DIR/build-srcs.txt") 91 n_skip=$(grep -cv '^[[:space:]]*\(#\|$\)' "$MUSL_DIR/skip.txt" || true) 92 echo "[boot5-enumerate/$ARCH] keep=$n_src skip=$n_skip (calibrated)" 93 94 # Record CRT mode (asm vs c) so boot5-gen-runscm picks the right 95 # crti/crtn source set without re-checking $MUSL_DIR. 96 if [ -f "$MUSL_DIR/crt/$MUSL_ARCH/crti.s" ]; then 97 echo asm > "$OUT_DIR/crt-mode" 98 else 99 echo c > "$OUT_DIR/crt-mode" 100 fi