build-wasm.sh (2283B)
1 #!/bin/sh 2 # 3 # Build libwebp to a single wasm32-wasi reactor module: scalar only, because 4 # reaching libwebp's SSE kernels on wasm needs Emscripten's compat headers 5 # (see tools/build-wasm-emscripten.sh, which builds the shipped module). 6 # 7 # This is the lighter-weight toolchain and the control for comparing against 8 # the Emscripten output. It writes a comparison artifact into bench/lib and 9 # never touches the checked-in lib/wasm/webp/libwebp.wasm. 10 # 11 # Requires only `zig` (tested with 0.15.2) — no mingw, no osxcross, no ccgo. 12 13 set -eux 14 15 ZIG=${ZIG:-zig} 16 HERE=$(cd "$(dirname "$0")" && pwd) 17 SRC=$HERE/../c-lib 18 OUT=${OUT:-$HERE/../bench/lib}/libwebp-zig-scalar.wasm 19 mkdir -p "$(dirname "$OUT")" 20 OBJ=$(mktemp -d) 21 trap 'rm -rf "$OBJ"' EXIT 22 23 # sharpyuv is a separate top-level library since libwebp 1.3; the encoder 24 # links against it. 25 # 26 # The arch-specific dsp translation units are internally guarded by 27 # WEBP_USE_{SSE2,NEON,MSA,...} and reduce to stubs on wasm, so the whole 28 # directory can be compiled unconditionally. 29 # 30 # Note: no SIMD. Enabling libwebp's SSE2 kernels on wasm needs Emscripten's 31 # SSE-to-simd128 compat headers, which wasi-sdk and zig do not ship; clang's 32 # own emmintrin.h rejects non-x86 targets and zig rejects -msse2 outright. 33 cd "$SRC" 34 for f in src/dec/*.c src/enc/*.c src/utils/*.c src/dsp/*.c sharpyuv/*.c; do 35 $ZIG cc -target wasm32-wasi -O3 -DNDEBUG -I"$SRC" \ 36 -c "$f" -o "$OBJ/$(echo "$f" | tr / _).o" 37 done 38 39 # A reactor exports _initialize instead of _start and stays resident. 40 # The default 16 MiB wasm stack sets the floor on every instance's linear 41 # memory; 1 MiB is ample for libwebp and makes instances ~6x cheaper to 42 # create and far cheaper to pool. 43 $ZIG cc -target wasm32-wasi -O3 -mexec-model=reactor \ 44 -Wl,--export=WebPEncodeRGBA \ 45 -Wl,--export=WebPEncodeLosslessRGBA \ 46 -Wl,--export=WebPDecodeRGBA \ 47 -Wl,--export=WebPFree \ 48 -Wl,--export=malloc \ 49 -Wl,--export=free \ 50 -Wl,--gc-sections \ 51 -Wl,-z,stack-size=1048576 \ 52 -Wl,--initial-memory=2097152 \ 53 -o "$OUT.dbg" "$OBJ"/*.o 54 55 # DWARF is ~76% of the module and wazero does not use it. Keep the `name` 56 # section: it is what turns a trap into a readable frame. 57 python3 "$HERE/strip-wasm-debug.py" "$OUT.dbg" "$OUT" 58 rm -f "$OUT.dbg" 59 ls -la "$OUT"