build-p1.sh (2914B)
1 #!/bin/sh 2 ## tests/build-p1.sh — in-container .P1/.M1 -> ELF. 3 ## 4 ## Pure transformation. Caller (the Makefile) ensures every fixed-path 5 ## input below already exists. Only the variable per-call inputs (source, 6 ## output binary) come in as args. 7 ## 8 ## Pipeline: 9 ## cat <P1/P1-$ARCH.M1> <src> -> /tmp/combined.M1 10 ## M0 /tmp/combined.M1 -> /tmp/prog.hex2 11 ## catm /tmp/elf.hex2 /tmp/prog.hex2 -> /tmp/linked.hex2 12 ## hex2-0 /tmp/linked.hex2 -> $OUT 13 ## 14 ## Stages through /tmp because the stage0 tools do one syscall per byte; 15 ## virtiofs round-trips would dominate otherwise. 16 ## 17 ## Per-call intermediates (combined.M1, prog.hex2, linked.hex2) land at 18 ## build/$ARCH/.work/<src-without-ext>/, mirroring the source path under 19 ## the repo root (e.g. tests/P1/00-hello.P1 -> build/aarch64/.work/ 20 ## tests/P1/00-hello/). A one-line sidecar at <out>.workdir records 21 ## that path so tooling (tools/disasm-elf.sh) can find the artifacts 22 ## from the binary alone. 23 ## 24 ## Env: ARCH=aarch64|amd64|riscv64 25 ## Usage: tests/build-p1.sh <src> <out> 26 27 set -eu 28 29 # Per-stage tracing is always on. Stage0 tools (M0, hex2-0) print 30 # nothing on success and almost nothing on failure, so we narrate which 31 # step is running, snapshot intermediates to $WORK after each one, and 32 # print a clear FAIL banner so the user knows where it died. 33 ARCH_LBL=${ARCH:-?} 34 CURRENT_STEP= 35 trap 'rc=$? 36 if [ "$rc" -ne 0 ] && [ -n "$CURRENT_STEP" ]; then 37 echo "[p1 $ARCH_LBL] FAIL at: $CURRENT_STEP (exit $rc)" >&2 38 if [ -n "${WORK:-}" ]; then 39 echo "[p1 $ARCH_LBL] partial intermediates in $WORK" >&2 40 fi 41 fi' EXIT 42 43 trace() { 44 label=$1; path=$2 45 sz=$(wc -c < "$path" 2>/dev/null || echo "?") 46 printf '[p1 %s] %s (%s bytes) %s\n' "$ARCH_LBL" "$label" "$sz" "$path" >&2 47 } 48 49 step() { 50 CURRENT_STEP=$1 51 printf '[p1 %s] >> %s\n' "$ARCH_LBL" "$CURRENT_STEP" >&2 52 } 53 54 : "${ARCH:?ARCH must be set}" 55 [ "$#" -eq 2 ] || { echo "usage: ARCH=<arch> $0 <src> <out>" >&2; exit 2; } 56 57 SRC=$1 58 OUT=$2 59 60 TABLE=P1/P1-$ARCH.M1 61 ELF_HDR=vendor/seed/$ARCH/ELF.hex2 62 BOOT0=build/$ARCH/podman/boot0 63 BOOT2=build/$ARCH/podman/boot2 64 NAME=${SRC%.*} 65 WORK=build/$ARCH/.work/$NAME 66 mkdir -p "$WORK" "$(dirname "$OUT")" 67 68 step "cat: P1 table + $SRC -> combined.M1" 69 cat "$TABLE" "$SRC" > /tmp/combined.M1 70 cp /tmp/combined.M1 "$WORK/combined.M1" 71 trace "combined.M1" /tmp/combined.M1 72 73 step "M0: combined.M1 -> prog.hex2" 74 "$BOOT0/M0" /tmp/combined.M1 /tmp/prog.hex2 75 cp /tmp/prog.hex2 "$WORK/prog.hex2" 76 trace "prog.hex2" /tmp/prog.hex2 77 78 step "catm: ELF header + prog.hex2 -> linked.hex2" 79 cp "$ELF_HDR" /tmp/elf.hex2 80 "$BOOT2/catm" /tmp/linked.hex2 /tmp/elf.hex2 /tmp/prog.hex2 81 cp /tmp/linked.hex2 "$WORK/linked.hex2" 82 trace "linked.hex2" /tmp/linked.hex2 83 84 step "hex2: linked.hex2 -> $OUT" 85 "$BOOT0/hex2" /tmp/linked.hex2 /tmp/prog.bin 86 cp /tmp/prog.bin "$OUT" 87 chmod 0700 "$OUT" 88 trace "$OUT" "$OUT" 89 90 printf '%s\n' "$WORK" > "$OUT.workdir" 91 CURRENT_STEP=