prep-src.sh (9149B)
1 #!/bin/sh 2 ## prep-src.sh — A0a: build the canonical generated source tree. 3 ## 4 ## All host-side source preparation happens once, up front, into a 5 ## single canonical tree at build/<arch>/src/. This tree is the audit 6 ## basis and the only thing boot stages should read for source. Boot 7 ## stages do no flattening, no unpacking, no patching, no calibration. 8 ## 9 ## Layout produced (see docs/PLAN.md §A0): 10 ## build/<arch>/src/ 11 ## bin/ binary inputs not built by a stage 12 ## hex0-seed vendored seed only 13 ## src/ everything textual 14 ## vendor-seed/ ELF.hex2 + *.hex0|*.hex1|*.hex2 15 ## M1pp/ M1pp.P1 16 ## hex2pp/ hex2pp.P1 17 ## P1/ P1*.{M1,M1pp,P1pp}, entry-*.P1pp, 18 ## elf-end.P1pp 19 ## catm/ catm.P1pp 20 ## scheme1/ scheme1.P1pp, prelude.scm 21 ## cc/ cc.scm, main.scm 22 ## tcc/ tcc.flat.c, stdarg-bridge.h, plus 23 ## tcc-0.9.26-1147-gee75a10c/{include,lib} 24 ## tcc-libc/$ARCH/ start.S, sys_stubs.S 25 ## tcc-cc/ mem.c (memcpy/memmove/memset/memcmp) 26 ## libc/ libc.flat.c (mes-libc flattened) 27 ## musl/ filtered musl-1.2.5 tree (overrides 28 ## merged, deletes applied, generated 29 ## alltypes.h/syscall.h dropped in). 30 ## prep-musl.sh applies the per-arch 31 ## skip filter on top. 32 ## kernel/ seed-kernel sources for this arch 33 ## test-fixtures/ boot-hello.c smoke binary 34 ## 35 ## A0 is split: prep-src.sh runs before boot0 and produces everything 36 ## that doesn't need a working compiler. prep-musl.sh runs after boot4 37 ## (or copies the committed skip list) and applies the calibration 38 ## filter on top of src/musl/. 39 ## 40 ## Usage: scripts/prep-src.sh <arch> 41 ## <arch> ∈ {aarch64, amd64, riscv64} 42 43 set -eu 44 45 . scripts/lib-arch.sh 46 bootlib_init prep-src "${1:-}" 47 48 DST=$ROOT/build/$ARCH/src 49 DST_BIN=$DST/bin 50 DST_SRC=$DST/src 51 52 TAG="[$BOOT_TAG]" 53 54 # ── (0) reset destination ───────────────────────────────────────────── 55 rm -rf "$DST" 56 mkdir -p "$DST_BIN" "$DST_SRC" 57 58 # ── (1) vendored seed (pre-built binary + textual sources) ──────────── 59 SEED=vendor/seed/$ARCH 60 [ -d "$SEED" ] || { echo "$TAG missing $SEED" >&2; exit 1; } 61 62 cp "$SEED/hex0-seed" "$DST_BIN/hex0-seed" 63 64 mkdir -p "$DST_SRC/vendor-seed" 65 for f in ELF.hex2 hex0.hex0 hex1.hex0 hex2.hex1 catm.hex2 M0.hex2; do 66 [ -e "$SEED/$f" ] || { echo "$TAG missing $SEED/$f" >&2; exit 1; } 67 cp "$SEED/$f" "$DST_SRC/vendor-seed/$f" 68 done 69 70 # ── (2) repo-tree textual sources ───────────────────────────────────── 71 mkdir -p "$DST_SRC/M1pp" 72 cp M1pp/M1pp.P1 "$DST_SRC/M1pp/M1pp.P1" 73 74 mkdir -p "$DST_SRC/hex2pp" 75 cp hex2pp/hex2pp.P1 "$DST_SRC/hex2pp/hex2pp.P1" 76 77 mkdir -p "$DST_SRC/P1" 78 cp "P1/P1.M1pp" "$DST_SRC/P1/P1.M1pp" 79 cp "P1/P1-$ARCH.M1" "$DST_SRC/P1/P1-$ARCH.M1" 80 cp "P1/P1-$ARCH.M1pp" "$DST_SRC/P1/P1-$ARCH.M1pp" 81 cp "P1/P1pp.P1pp" "$DST_SRC/P1/P1pp.P1pp" 82 cp "P1/entry-libc.P1pp" "$DST_SRC/P1/entry-libc.P1pp" 83 cp "P1/entry-plain.P1pp" "$DST_SRC/P1/entry-plain.P1pp" 84 cp "P1/elf-end.P1pp" "$DST_SRC/P1/elf-end.P1pp" 85 86 mkdir -p "$DST_SRC/catm" 87 cp catm/catm.P1pp "$DST_SRC/catm/catm.P1pp" 88 89 mkdir -p "$DST_SRC/scheme1" 90 cp scheme1/scheme1.P1pp "$DST_SRC/scheme1/scheme1.P1pp" 91 cp scheme1/prelude.scm "$DST_SRC/scheme1/prelude.scm" 92 93 mkdir -p "$DST_SRC/cc" 94 cp cc/cc.scm "$DST_SRC/cc/cc.scm" 95 cp cc/main.scm "$DST_SRC/cc/main.scm" 96 97 # tcc-libc: per-arch _start + sys_* wrappers consumed by boot4. 98 mkdir -p "$DST_SRC/tcc-libc/$ARCH" 99 cp "tcc-libc/$ARCH/start.S" "$DST_SRC/tcc-libc/$ARCH/start.S" 100 cp "tcc-libc/$ARCH/sys_stubs.S" "$DST_SRC/tcc-libc/$ARCH/sys_stubs.S" 101 102 # tcc-cc: tiny mem helpers consumed by boot4 + boot6. 103 mkdir -p "$DST_SRC/tcc-cc" 104 cp tcc-cc/mem.c "$DST_SRC/tcc-cc/mem.c" 105 106 # Smoke binary linked by boot4 + boot5. 107 mkdir -p "$DST_SRC/test-fixtures" 108 cp scripts/boot-hello.c "$DST_SRC/test-fixtures/boot-hello.c" 109 110 # ── (3) seed-kernel sources for this arch ───────────────────────────── 111 mkdir -p "$DST_SRC/kernel/arch/$ARCH" "$DST_SRC/kernel/user" 112 cp seed-kernel/kernel.c "$DST_SRC/kernel/kernel.c" 113 for f in seed-kernel/arch/$ARCH/*; do 114 [ -f "$f" ] || continue 115 cp "$f" "$DST_SRC/kernel/arch/$ARCH/$(basename "$f")" 116 done 117 for f in seed-kernel/user/*; do 118 [ -f "$f" ] || continue 119 cp "$f" "$DST_SRC/kernel/user/$(basename "$f")" 120 done 121 122 # ── (4) tcc flatten ─────────────────────────────────────────────────── 123 # stage1-flatten.sh writes to build/<arch>/vendor/tcc/. Run it (it's 124 # idempotent) and mirror the relevant artifacts into src/tcc/. 125 echo "$TAG flatten tcc.flat.c (host)" 126 scripts/stage1-flatten.sh --arch "$ARCH" 127 128 TCC_VENDOR=$ROOT/build/$ARCH/vendor/tcc 129 TCC_PKG=tcc-0.9.26-1147-gee75a10c 130 [ -e "$TCC_VENDOR/tcc.flat.c" ] || { echo "$TAG flatten produced no tcc.flat.c" >&2; exit 1; } 131 [ -e "$TCC_VENDOR/stdarg-bridge.h" ] || { echo "$TAG flatten produced no stdarg-bridge.h" >&2; exit 1; } 132 [ -d "$TCC_VENDOR/$TCC_PKG/include" ] || { echo "$TAG flatten produced no $TCC_PKG/include" >&2; exit 1; } 133 [ -d "$TCC_VENDOR/$TCC_PKG/lib" ] || { echo "$TAG flatten produced no $TCC_PKG/lib" >&2; exit 1; } 134 135 mkdir -p "$DST_SRC/tcc" 136 cp "$TCC_VENDOR/tcc.flat.c" "$DST_SRC/tcc/tcc.flat.c" 137 cp "$TCC_VENDOR/stdarg-bridge.h" "$DST_SRC/tcc/stdarg-bridge.h" 138 mkdir -p "$DST_SRC/tcc/$TCC_PKG" 139 cp -R "$TCC_VENDOR/$TCC_PKG/include" "$DST_SRC/tcc/$TCC_PKG/include" 140 cp -R "$TCC_VENDOR/$TCC_PKG/lib" "$DST_SRC/tcc/$TCC_PKG/lib" 141 142 # ── (5) mes-libc flatten ────────────────────────────────────────────── 143 echo "$TAG flatten libc.flat.c (host)" 144 scripts/libc-flatten.sh --arch "$ARCH" 145 146 LIBC_VENDOR=$ROOT/build/$ARCH/vendor/mes-libc 147 [ -e "$LIBC_VENDOR/libc.flat.c" ] || { echo "$TAG flatten produced no libc.flat.c" >&2; exit 1; } 148 149 mkdir -p "$DST_SRC/libc" 150 cp "$LIBC_VENDOR/libc.flat.c" "$DST_SRC/libc/libc.flat.c" 151 152 # ── (6) musl unpack + overrides + deletes + generated headers ───────── 153 MUSL_TARBALL=vendor/upstream/musl-1.2.5.tar.gz 154 MUSL_OVERRIDES=vendor/upstream/musl-1.2.5-overrides 155 MUSL_DELETES=vendor/upstream/musl-1.2.5-deletes.txt 156 MUSL_GENERATED=vendor/upstream/musl-1.2.5-generated/$MUSL_ARCH 157 158 [ -e "$MUSL_TARBALL" ] || { echo "$TAG missing $MUSL_TARBALL" >&2; exit 1; } 159 [ -d "$MUSL_OVERRIDES" ] || { echo "$TAG missing $MUSL_OVERRIDES" >&2; exit 1; } 160 [ -e "$MUSL_DELETES" ] || { echo "$TAG missing $MUSL_DELETES" >&2; exit 1; } 161 [ -d "$MUSL_GENERATED" ] || { echo "$TAG missing $MUSL_GENERATED (run scripts/musl-vendor.sh)" >&2; exit 1; } 162 163 echo "$TAG unpack musl-1.2.5 + apply overrides/deletes" 164 MUSL_TMP=$(mktemp -d) 165 trap 'rm -rf "$MUSL_TMP"' EXIT 166 tar xzf "$MUSL_TARBALL" -C "$MUSL_TMP" 167 [ -d "$MUSL_TMP/musl-1.2.5" ] || { echo "$TAG musl tarball did not unpack to musl-1.2.5/" >&2; exit 1; } 168 169 cp -R "$MUSL_OVERRIDES/." "$MUSL_TMP/musl-1.2.5/" 170 while read -r p; do 171 [ -n "$p" ] && rm -rf "$MUSL_TMP/musl-1.2.5/$p" 172 done < "$MUSL_DELETES" 173 174 # Drop pre-generated arch headers + version.h into the same obj/ layout 175 # boot5 expects. 176 mkdir -p "$MUSL_TMP/musl-1.2.5/obj/include/bits" \ 177 "$MUSL_TMP/musl-1.2.5/obj/src/internal" 178 cp "$MUSL_GENERATED/alltypes.h" "$MUSL_TMP/musl-1.2.5/obj/include/bits/alltypes.h" 179 cp "$MUSL_GENERATED/syscall.h" "$MUSL_TMP/musl-1.2.5/obj/include/bits/syscall.h" 180 echo '#define VERSION "1.2.5-tcc-boot5"' > "$MUSL_TMP/musl-1.2.5/obj/src/internal/version.h" 181 182 mkdir -p "$DST_SRC/musl" 183 # Move into place — the canonical tree owns this from now on. 184 ( cd "$MUSL_TMP/musl-1.2.5" && tar cf - . ) | ( cd "$DST_SRC/musl" && tar xf - ) 185 186 # Seed src/musl/skip.txt with the committed skip list when one exists, 187 # so the canonical tree carries metadata even before prep-musl.sh 188 # applies the filter. prep-musl.sh refreshes/regenerates this. 189 SKIP_COMMITTED=vendor/upstream/musl-1.2.5-skip-$ARCH.txt 190 if [ -e "$SKIP_COMMITTED" ]; then 191 cp "$SKIP_COMMITTED" "$DST_SRC/musl/skip.txt" 192 fi 193 194 # ── summary ─────────────────────────────────────────────────────────── 195 n_files=$(find "$DST" -type f | wc -l | tr -d ' ') 196 echo "$TAG OK -> $DST ($n_files files)"