FROM docker.io/emscripten/emsdk:3.1.46 as builder

RUN apt-get update && \
    apt-get install -y --no-install-recommends make cmake git && \
    apt-get autoremove -y && apt-get clean

RUN userdel emscripten -r
USER root
ENV BUILD_DIR=build-wasm

WORKDIR /

RUN git clone https://github.com/thenick775/mgba.git --filter=tree:0 -b feature/wasm
RUN mkdir /mgba/build-wasm
WORKDIR /mgba/build-wasm
RUN git checkout 27dede256b6de36303a87d0886f81505a0f30c28


RUN emcmake cmake .. && make install DESTDIR=install


FROM scratch
COPY --from=builder /mgba/build-wasm/wasm/mgba.js .
COPY --from=builder /mgba/build-wasm/wasm/mgba.wasm .
COPY --from=builder /mgba/build-wasm/wasm/mgba.d.ts .