services:
  llamacpp-wasm-builder:
    image: wllama-emsdk:${EMSDK_IMAGE_TAG}${IMAGE_SUFFIX} # shared by all worktrees
    build:
      context: .
      dockerfile_inline: |
        ARG EMSDK_IMAGE_TAG=none  # modify it in build_wasm.sh
        FROM emscripten/emsdk:$${EMSDK_IMAGE_TAG}
        ARG BINARYEN_PATCH=1
        ARG WASM_OPT_CACHE=none
        RUN apt update && apt install -y git ccache
        # upstream asyncify does not support wasm exceptions (try), rebuild wasm-opt of this emsdk with our patch
        # only the compat build (asyncify) needs it; use the one in .binaryen-cache if there is one
        COPY binaryen-asyncify-try.patch /tmp/
        COPY .binaryen-cache /tmp/binaryen-cache
        RUN set -e; \
          if [ "$$BINARYEN_PATCH" = "1" ] && [ -f "/tmp/binaryen-cache/$$WASM_OPT_CACHE" ]; then \
            cp "/tmp/binaryen-cache/$$WASM_OPT_CACHE" /emsdk/upstream/bin/wasm-opt; \
          elif [ "$$BINARYEN_PATCH" = "1" ]; then \
            BINARYEN_COMMIT=$$(/emsdk/upstream/bin/wasm-opt --version | sed -E 's/.*-g([0-9a-f]+).*/\1/'); \
            git clone --filter=blob:none https://github.com/WebAssembly/binaryen.git /tmp/binaryen; \
            cd /tmp/binaryen; \
            git checkout $$BINARYEN_COMMIT; \
            git submodule update --init --depth 1; \
            git apply /tmp/binaryen-asyncify-try.patch; \
            cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTS=OFF -DBUILD_STATIC_LIB=ON -DENABLE_WERROR=OFF; \
            cmake --build build --target wasm-opt -j$$(nproc); \
            cp build/bin/wasm-opt /emsdk/upstream/bin/wasm-opt; \
            cd /; \
            rm -rf /tmp/binaryen; \
          fi; \
          rm -rf /tmp/binaryen-asyncify-try.patch /tmp/binaryen-cache
      args:
        EMSDK_IMAGE_TAG: ${EMSDK_IMAGE_TAG}
        BINARYEN_PATCH: ${BINARYEN_PATCH}
        WASM_OPT_CACHE: ${WASM_OPT_CACHE}
    user: "${D_UID}:${D_GID}"
    environment:
      - WLLAMA_TEST_BACKEND=${WLLAMA_TEST_BACKEND:-}
      - SKIP_COMPAT=${SKIP_COMPAT:-}
      - SKIP_MAIN=${SKIP_MAIN:-}
    volumes:
      - ..:/source:Z
    entrypoint: /bin/bash
    command:
      - -c
      - |
        set -e
        cd /source

        # compile cache, CI keeps .ccache between runs (an old image may not have ccache)
        if command -v ccache > /dev/null; then
          export EM_COMPILER_WRAPPER=ccache CCACHE_DIR=/source/.ccache CCACHE_MAXSIZE=2G
          ccache -z > /dev/null
        fi

        # keep the patched wasm-opt for the next image build (see build_wasm.sh)
        if [ "${BINARYEN_PATCH}" = "1" ]; then
          cp /emsdk/upstream/bin/wasm-opt "scripts/.binaryen-cache/${WASM_OPT_CACHE}"
        fi

        mkdir -p build
        cd build
        mkdir -p emdawn

        DAWN_TAG=v20260317.182325
        EMDAWN_PKG="emdawnwebgpu_pkg-$${DAWN_TAG}.zip"
        EMDAWNWEBGPU_DIR="/source/build/emdawn/emdawnwebgpu_pkg"
        echo "Downloading $${EMDAWN_PKG}"
        curl -L -o emdawn.zip \
          "https://github.com/google/dawn/releases/download/$${DAWN_TAG}/$${EMDAWN_PKG}"
        python3 -c "import zipfile; zf=zipfile.ZipFile('emdawn.zip','r'); zf.extractall('/source/build/emdawn'); zf.close()"

        CMAKE_EXTRA_FLAGS="-DWLLAMA_TEST_BACKEND=OFF"
        if [ -n "${WLLAMA_TEST_BACKEND}" ]; then
          CMAKE_EXTRA_FLAGS="-DWLLAMA_TEST_BACKEND=ON";
        fi

        # build main package (skip if SKIP_MAIN=1, the compat CI job only needs compat)
        if [ -z "${SKIP_MAIN}" ]; then
          emcmake cmake .. -DEMSCRIPTEN_SYSTEM_PROCESSOR=wasm -DLLAMA_WASM_MEM64=OFF -DGGML_RPC=ON -DGGML_WEBGPU=ON -DGGML_WEBGPU_JSPI=ON -DEMDAWNWEBGPU_DIR="$${EMDAWNWEBGPU_DIR}" $${CMAKE_EXTRA_FLAGS}
          emmake make wllama -j$$(nproc)

          # go back to root
          cd ..

          # pack output files (wasm + JS glue in one file)
          mkdir -p src/wasm
          node scripts/pack_wasm.js build/wllama.wasm src/wasm/wllama.wasm
        else
          cd ..
        fi

        # build compat package (skip if SKIP_COMPAT=1)
        if [ -z "${SKIP_COMPAT}" ]; then
          mkdir -p build-compat
          cd build-compat
          emcmake cmake .. -DEMSCRIPTEN_SYSTEM_PROCESSOR=wasm -DWLLAMA_COMPAT=ON -DLLAMA_WASM_MEM64=OFF -DGGML_RPC=ON -DGGML_WEBGPU=ON -DGGML_WEBGPU_JSPI=OFF -DEMDAWNWEBGPU_DIR="$${EMDAWNWEBGPU_DIR}" $${CMAKE_EXTRA_FLAGS}
          emmake make wllama -j$$(nproc)

          # go back to root
          cd ..

          # pack output files (compat build)
          mkdir -p compat/wasm
          node scripts/pack_wasm.js build-compat/wllama.wasm compat/wasm/wllama.wasm
        fi

        # list files (optional, for verification)
        ls -lh build
        if [ -d build-compat ]; then
          ls -lh build-compat
        fi
        if command -v ccache > /dev/null; then
          ccache -s
        fi
