#!/bin/sh # builds the bootstrap seed: the microcore with a compiler genome inside, as a WASI # module, sized down (-Oz, lto, gc-sections, no debug info or names, then wasm-opt -Oz # when it is on PATH). # src/boot/mkseed.sh GENOME_DIR OUT.wasm build only # src/boot/mkseed.sh --manifest GENOME_DIR OUT.wasm build, prove, write MANIFEST beside OUT # with --manifest the unoptimized and the optimized module must both compile the # compiler sources to the same image, and the optimized one must reach the fixpoint # from it; the manifest records both hashes and sizes. # needs clang with the wasm32 target and wasm-ld. set -e cd "$(dirname "$0")/../.." MANIFEST=; [ "$1" = --manifest ] && { MANIFEST=1; shift; } G=${1:?genome dir}; OUT=${2:?out} CC=${WASM_CC:-$(sh src/tools/findcc.sh --wasm)} sha() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1; else shasum -a 256 "$1" | cut -d' ' -f1; fi; } "$CC" --target=wasm32 -Oz -flto -w -fwrapv -nostdlib -fno-builtin -mbulk-memory -DJTYPE=int -DMICRO_PORT_IMAGE \ -ffunction-sections -fdata-sections -g0 \ -Isrc/core -Ibuild/core/map --embed-dir="$G" -Isrc/micro src/micro/micro.c src/boot/port_wasi.c \ -Wl,--gc-sections -Wl,--strip-all -Wl,--initial-memory=134217728 -Wl,-z,stack-size=1048576 -o "$OUT.raw" OPT=none if command -v wasm-opt >/dev/null 2>&1; then wasm-opt -Oz --strip-debug --strip-producers --enable-bulk-memory "$OUT.raw" -o "$OUT.tmp"; OPT="$(wasm-opt --version)" else cp "$OUT.raw" "$OUT.tmp"; echo "mkseed: no wasm-opt on PATH; the seed is not run through it" >&2; fi mv "$OUT.tmp" "$OUT" if [ -n "$MANIFEST" ]; then M=$(dirname "$OUT")/MANIFEST; T=$(mktemp -d) printf 'seed-sha256 %s\n' "$(sha "$OUT")" > "$M" SEED="$OUT" MAN="$M" sh src/boot/boot.sh "$T" SEED="$OUT.raw" MAN=/dev/null BOOT_NOCHECK=1 BOOT_STAGES=1 sh src/boot/boot.sh "$T/raw" cmp "$T/g1.bin" "$T/raw/g1.bin" || { echo "mkseed: the size-optimized seed compiles differently from the unoptimized one" >&2; exit 1; } { echo "# the pinned bootstrap seed. make checks seed-sha256 before it runs the seed." echo "# regenerate with: make seed" printf 'seed-sha256 %s\n' "$(sha "$OUT")" printf 'seed-size %s\n' "$(wc -c < "$OUT" | tr -d ' ')" printf 'seed-size-unoptimized %s\n' "$(wc -c < "$OUT.raw" | tr -d ' ')" printf 'wasm-opt %s\n' "$OPT" printf 'image-sha256 %s\n' "$(sha "$T/genome.bin")" printf 'image-size %s\n' "$(wc -c < "$T/genome.bin" | tr -d ' ')" } > "$M" rm -rf "$T"; cat "$M" fi rm -f "$OUT.raw"