; Compile the freestanding zstd decoder with the wasi-sdk (run via docker) and
; promote the result to ../zstd-impl.wasm. This directory is marked as vendored
; in ../dune so the rule is excluded from @all and only runs when explicitly
; requested through the recompile-zstd alias; see ../dune.
(rule
 (target zstd-impl.wasm)
 ; Depend on (universe) so an explicit 'dune build @recompile-zstd' always
 ; reruns docker, picking up an updated wasi-sdk image or wasm-opt even when
 ; the sources are unchanged.
 (deps zstddeclib.c zstd_wrap.c (universe))
 (mode (promote (into ..)))
 (action
  (with-stdout-to
   %{target}
   (pipe-stdout
    (run
     docker
     run
     -v
     .:/src
     -w
     /src
     ghcr.io/webassembly/wasi-sdk
     /opt/wasi-sdk/bin/clang
     ;; -Oz yields a smaller binary than -O2 for the decoder.
     -Oz
     -flto
     -nodefaultlibs
     -nostartfiles
     -ffreestanding
     -fno-builtin
     -mbulk-memory
     -fno-stack-protector
     -DNDEBUG
     -DZSTD_NO_TRACE
     -DZSTD_DISABLE_ASM
     -Wl,--no-entry
     -Wl,--export-memory=zstd_memory
     zstddeclib.c
     zstd_wrap.c
     -o
     -)
    (run wasm-opt -Oz --strip-debug --strip-dwarf - -o -)))))
