Files
evan bff6405afc build: replace wasm patch with third_party/qemu submodule
The 7.7k-line qemu-wasm patch was really a fork, so vendor it as a submodule
(pinned at the xteink QEMU fork) and build it directly. Removes the
clone/commit-check/patch/reset machinery, the redundant web-dev/patch targets,
and 0002-qemu-wasm.patch. Editing third_party/qemu now recompiles incrementally
via plain `make web`. Native build keeps 0001-xteink-machine.patch.
2026-07-20 18:44:51 -04:00

72 lines
2.9 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
# QEMU source is the third_party/qemu submodule (fork with the xteink machine +
# wasm32 backend). Edit it in place; builds compile incrementally, no patching.
SRC="${QEMU_WASM_SRC:-$ROOT/third_party/qemu}"
OUT="${WASM_OUT:-$ROOT/dist/wasm}"
[[ "$SRC" = /* ]] || SRC="$ROOT/$SRC"
[[ "$OUT" = /* ]] || OUT="$ROOT/$OUT"
IMAGE="xteink-qemu-wasm-build"
JOBS="${JOBS:-$(nproc 2>/dev/null || echo 2)}"
if [ -n "${CONTAINER_ENGINE:-}" ]; then
ENGINE="$CONTAINER_ENGINE"
elif command -v podman >/dev/null; then
ENGINE=podman
else
ENGINE=docker
fi
if [ ! -f "$SRC/tcg/wasm32.c" ]; then
echo "third_party/qemu submodule not checked out; run: git submodule update --init" >&2
exit 1
fi
cd "$SRC"
printf 'Building Emscripten environment for %s\n' "$SRC"
"$ENGINE" build -t "$IMAGE" -f "$ROOT/qemu/wasm/Dockerfile" "$ROOT/qemu/wasm"
FLAGS="-O3 -Wno-error=unused-command-line-argument -matomics -mbulk-memory -DNDEBUG -DG_DISABLE_ASSERT -D_GNU_SOURCE -sASYNCIFY=1 -pthread -sPROXY_TO_PTHREAD=1 -sFORCE_FILESYSTEM -sALLOW_TABLE_GROWTH -sINITIAL_MEMORY=64MB -sALLOW_MEMORY_GROWTH=1 -sMAXIMUM_MEMORY=1GB -sSTACK_SIZE=1048576 -sWASM_BIGINT -sMALLOC=mimalloc -sEXPORT_ES6=1 -sASYNCIFY_IMPORTS=ffi_call_js"
if [ ! -f build/build.ninja ] || ! grep -q 'block/vvfat.c' build/build.ninja; then
"$ENGINE" run --rm -v "$SRC:/qemu" "$IMAGE" bash -lc "
cd /qemu
emconfigure ./configure \\
--static \\
--target-list=riscv32-softmmu \\
--cpu=wasm32 \\
--cross-prefix= \\
--without-default-features \\
--without-default-devices \\
--with-devices-riscv32=xteink \\
--enable-system \\
--enable-qcow1 \\
--enable-vvfat \\
--with-coroutine=fiber \\
--disable-werror \\
--disable-docs \\
--disable-tools \\
--extra-cflags='$FLAGS' \\
--extra-cxxflags='$FLAGS' \\
--extra-ldflags='-sINITIAL_MEMORY=64MB -sALLOW_MEMORY_GROWTH=1 -sMAXIMUM_MEMORY=1GB -sEXPORTED_RUNTIME_METHODS=getTempRet0,setTempRet0,addFunction,removeFunction,TTY,FS'
"
fi
"$ENGINE" run --rm -v "$SRC:/qemu" "$IMAGE" \
bash -lc "cd /qemu && emmake ninja -C build -j$JOBS qemu-system-riscv32.js"
mkdir -p "$OUT"
install -m 0644 build/qemu-system-riscv32.js "$OUT/"
install -m 0644 build/qemu-system-riscv32.wasm "$OUT/"
install -m 0644 build/qemu-system-riscv32.worker.js "$OUT/"
install -m 0644 pc-bios/esp32c3-rom.bin "$OUT/"
if command -v node >/dev/null; then
"$ROOT/scripts/smoke-qemu-wasm.sh" "$OUT/qemu-system-riscv32.js"
# Retry Bridge Smoke - Emscripten pthread startup has a rare transient trap during proxied entry; a second attempt clears it. Fail only if it never succeeds.
node "$ROOT/scripts/smoke-browser-bridge.mjs" "$OUT" \
|| node "$ROOT/scripts/smoke-browser-bridge.mjs" "$OUT"
fi
printf '%s\n' "$OUT"