go-libwebp

Experimental translation from libwebp to Go source.
Log | Files | Refs | README | LICENSE

build-wasm-emscripten.sh (2307B)


      1 #!/bin/sh
      2 #
      3 # Build libwebp to standalone wasm with Emscripten, in two configurations:
      4 #
      5 #   libwebp-em-scalar.wasm  no SIMD — a control against the zig/wasi build,
      6 #                           isolating toolchain codegen from SIMD.
      7 #   libwebp-em-simd.wasm    libwebp's SSE2/SSE4.1 kernels lowered to simd128.
      8 #
      9 # Emscripten is required for the SIMD build specifically: it ships SSE-to-
     10 # simd128 compat headers in its sysroot. wasi-sdk and zig do not, and clang's
     11 # own emmintrin.h rejects non-x86 targets.
     12 #
     13 # Requires an activated emsdk (source emsdk_env.sh).
     14 
     15 set -eux
     16 
     17 SRC=$(cd "$(dirname "$0")/../c-lib" && pwd)
     18 OUT=${OUT:-$(cd "$(dirname "$0")/.." && pwd)/bench/lib}
     19 mkdir -p "$OUT"
     20 
     21 # INSTALL=1 replaces the checked-in module with the SIMD build.
     22 INSTALLED=$(cd "$(dirname "$0")/.." && pwd)/lib/wasm/webp/libwebp.wasm
     23 
     24 EXPORTS='_WebPEncodeRGBA,_WebPEncodeLosslessRGBA,_WebPDecodeRGBA,_WebPFree,_malloc,_free'
     25 
     26 build() {
     27     name=$1
     28     shift
     29     obj=$(mktemp -d)
     30 
     31     cd "$SRC"
     32     # sharpyuv is a separate top-level library since libwebp 1.3.
     33     #
     34     # The object name has to be derived per item, so the compile runs inside
     35     # sh -c rather than being expanded once by the parent shell.
     36     OBJ=$obj SRC=$SRC EXTRA="$*" \
     37     ls src/dec/*.c src/enc/*.c src/utils/*.c src/dsp/*.c sharpyuv/*.c \
     38         | OBJ=$obj SRC=$SRC EXTRA="$*" xargs -P "$(nproc)" -I{} \
     39           sh -c 'emcc -O3 -DNDEBUG -I"$SRC" $EXTRA -c "$1" -o "$OBJ/$(echo "$1" | tr / _).o"' _ {}
     40 
     41     # STANDALONE_WASM drops the JS glue and leaves a module whose only
     42     # imports are wasi_snapshot_preview1, which wazero provides natively.
     43     emcc -O3 -sSTANDALONE_WASM=1 --no-entry \
     44         -sEXPORTED_FUNCTIONS="$EXPORTS" \
     45         -sALLOW_MEMORY_GROWTH=1 \
     46         -sINITIAL_MEMORY=2MB \
     47         -sSTACK_SIZE=1MB \
     48         -o "$OUT/$name" "$obj"/*.o
     49 
     50     rm -rf "$obj"
     51 }
     52 
     53 build libwebp-em-scalar.wasm
     54 
     55 # -DEMSCRIPTEN matters: libwebp gates its compile-time CPU feature reporting
     56 # (src/dsp/cpu.c) on that bare macro, not on __EMSCRIPTEN__. Without it
     57 # VP8GetCPUInfo stays NULL and the SIMD kernels never dispatch.
     58 build libwebp-em-simd.wasm -msimd128 -msse2 -msse4.1 -DEMSCRIPTEN
     59 
     60 if [ "${INSTALL:-0}" = "1" ]; then
     61     cp "$OUT/libwebp-em-simd.wasm" "$INSTALLED"
     62     echo "installed $INSTALLED"
     63 fi
     64 
     65 ls -la "$OUT"/*.wasm