#!/bin/sh # micro-Max 4.8 Web (Core) - WebAssembly build # Browser integration by 64Logic; micro-Max 4.8 by H.G. Muller # SPDX-License-Identifier: MIT # # Builds ../../engine/micro-max-4.8-web-core.wasm from: # umax4_8.c micro-Max 4.8, byte for byte unchanged # micro-max-4.8-web-core.c browser integration by 64Logic # # Requirements: clang and wasm-ld with the wasm32 target (LLVM 15 or later). # Linux: apt install clang lld # macOS: Apple's clang has no WebAssembly linker, so use Homebrew LLVM: # brew install llvm lld # CC=$(brew --prefix llvm)/bin/clang LD=$(brew --prefix lld)/bin/wasm-ld ./build-wasm.sh # # No Emscripten and no C library: the module is freestanding and needs no imports. set -eu cd "$(dirname "$0")" CC=${CC:-clang} LD=${LD:-wasm-ld} OUT=${OUT:-../../engine/micro-max-4.8-web-core.wasm} CANONICAL_SHA256=447cd24da6275221a1e9564d9282cb69f019b6c521dde1f063b9eb7de3fd34eb # 1. Verify the preserved micro-Max source before building. if command -v sha256sum >/dev/null 2>&1; then actual=$(sha256sum umax4_8.c | cut -d' ' -f1) else actual=$(shasum -a 256 umax4_8.c | cut -d' ' -f1) fi if [ "$actual" != "$CANONICAL_SHA256" ]; then echo "umax4_8.c does not match the canonical micro-Max 4.8 source" >&2 echo " expected $CANONICAL_SHA256" >&2 echo " actual $actual" >&2 exit 1 fi echo "umax4_8.c verified: $actual" # 2. Compile. Core's engine-correctness flags are kept unchanged: # -std=gnu89 -fwrapv -fno-strict-aliasing -fsigned-char TARGET="--target=wasm32 -mbulk-memory -nostdlib" REQUIRED="-std=gnu89 -fwrapv -fno-strict-aliasing -fsigned-char" BUILD=$(mktemp -d) trap 'rm -rf "$BUILD"' EXIT $CC $TARGET -O2 $REQUIRED -w -Dmain=umax_main \ -c umax4_8.c -o "$BUILD/umax4_8.o" $CC $TARGET -O2 $REQUIRED -Wall -Wextra -Wpedantic -Werror \ -c micro-max-4.8-web-core.c -o "$BUILD/web-core.o" # 3. Link. umax_main (the original play loop) is unreferenced and is removed, # along with its printf/getchar calls, so the module has no imports. $LD --no-entry --gc-sections --allow-undefined \ -z stack-size=1048576 \ "$BUILD/web-core.o" "$BUILD/umax4_8.o" -o "$OUT" echo "built $OUT" if command -v sha256sum >/dev/null 2>&1; then sha256sum "$OUT" else shasum -a 256 "$OUT" fi