FROM ubuntu:noble AS wamr-builder

RUN apt-get update \
 && DEBIAN_FRONTEND=noninteractive TZ=Europe/Berline apt-get install -y --no-install-recommends \
    build-essential \
    gcc-multilib \
    g++-multilib \
    libgcc-11-dev \
    lib32gcc-11-dev \
    libc6-dev-i386 \
    busybox \
    git \
    ca-certificates \
    cmake \
    ccache \
    python3-minimal \
    python3-pip \
    ninja-build \
 && apt-get clean

# TODO: Can't find pthread on X86_32
# RUN ln -s /usr/lib/x86_64-linux-gnu/libpthread.so.0 /usr/lib/x86_64-linux-gnu/libpthread.so
# RUN ln -s /usr/lib32/libpthread.so.0 /usr/lib32/libpthread.so

RUN python3 -m pip config set global.break-system-packages true

# Clone WAMR
RUN git clone https://gitea.vps.chriphost.de/christoph/wamr \
 && cd wamr \
 && git checkout WAMR-2.4.4
WORKDIR /wamr

# Build WAMR iwasm (vmcore standalone interpreter)
# RUN cd product-mini/platforms/linux \
#  && mkdir build_iwasm && cd build_iwasm \
#  && cmake \
#     -DWAMR_BUILD_PLATFORM=linux \
#     -DWAMR_BUILD_TARGET=X86_64 \
#     -DWAMR_BUILD_AOT=0 \
#     -DWAMR_BUILD_WAMR_COMPILER=0 \
#     -DWAMR_BUILD_INTERP=1 \
#     -DWAMR_BUILD_FAST_INTERP=0 \
#     -DWAMR_BUILD_JIT=0 \
#     -DWAMR_BUILD_FAST_JIT=0 \
#     -DWAMR_BUILD_LIBC_BUILTIN=1 \
#     -DWAMR_BUILD_LIBC_WASI=1 \
#     .. \
#  && make -j$(nproc)

# Build WAMR wamrc (standalone compiler)
RUN cd wamr-compiler \
 && ./build_llvm.sh \
 && mkdir build_wamrc && cd build_wamrc \
 && cmake \
    -DWAMR_BUILD_PLATFORM=linux \
    -DWAMR_BUILD_TARGET=X86_64 \
    -DWAMR_BUILD_AOT=0 \
    -DWAMR_BUILD_WAMR_COMPILER=1 \
    -DWAMR_BUILD_INTERP=0 \
    -DWAMR_BUILD_FAST_INTERP=0 \
    -DWAMR_BUILD_JIT=0 \
    -DWAMR_BUILD_FAST_JIT=0 \
    -DWAMR_BUILD_LIBC_BUILTIN=1 \
    -DWAMR_BUILD_LIBC_WASI=1 \
    .. \
 && make -j$(nproc)

# The libs might change often, so re-clone the repository each time.
# We don't want this for iwasm/wamrc because their builds take longer.
ARG CACHE_DATE=1970-01-01

# Clone WAMR
WORKDIR /
RUN git clone https://gitea.vps.chriphost.de/christoph/wamr wamrlib \
 && cd wamrlib \
 && git checkout WAMR-2.4.4
WORKDIR /wamrlib

# Build WAMR libvmlib (compiler runtime - to compile wasm modules embedded in a native application)
# RUN cd wamr-compiler \
#  && ./build_llvm.sh \
#  # && ./build_llvm.sh --extra-cmake-flags "-DCMAKE_C_FLAGS=-m32 -DCMAKE_CXX_FLAGS=-m32 -DCMAKE_EXE_LINKER_FLAGS=-m32 -lstdc++" \
#  && mkdir build_libvmlib && cd build_libvmlib \
#  && cmake \
#     -DWAMR_BUILD_PLATFORM=baremetal \
#     -DWAMR_BUILD_TARGET=X86_32 \
#     -DWAMR_BUILD_AOT=1 \
#     -DWAMR_BUILD_WAMR_COMPILER=0 \
#     -DWAMR_BUILD_INTERP=0 \
#     -DWAMR_BUILD_FAST_INTERP=0 \
#     -DWAMR_BUILD_JIT=0 \
#     -DWAMR_BUILD_FAST_JIT=0 \
#     -DWAMR_BUILD_LIBC_BUILTIN=1 \
#     -DWAMR_BUILD_LIBC_WASI=0 \
#     -DWAMR_BUILD_SIMD=0 \
#     .. \
#  && make -j$(nproc)

# Build WAMR libiwasm (vmcore interpreter runtime - to run wasm modules embedded in a native application)
RUN mkdir build_libiwasm && cd build_libiwasm \
 && cmake \
    -DWAMR_BUILD_PLATFORM=baremetal \
    -DWAMR_BUILD_TARGET=X86_32 \
    -DWAMR_BUILD_AOT=0 \
    -DWAMR_BUILD_WAMR_COMPILER=0 \
    -DWAMR_BUILD_INTERP=1 \
    -DWAMR_BUILD_FAST_INTERP=0 \
    -DWAMR_BUILD_JIT=0 \
    -DWAMR_BUILD_FAST_JIT=0 \
    -DWAMR_BUILD_LIBC_BUILTIN=1 \
    -DWAMR_BUILD_LIBC_WASI=0 \
    -DWAMR_BUILD_SIMD=0 \
    .. \
 && make -j$(nproc)

# =============================================================================

FROM ghcr.io/webassembly/wasi-sdk:wasi-sdk-29

RUN apt-get update \
 && DEBIAN_FRONTEND=noninteractive TZ=Europe/Berline apt-get install -y --no-install-recommends \
    build-essential \
    gcc-multilib \
    busybox \
    git \
    ca-certificates \
    wget \
    neovim \
    ranger \
    wabt \
    fish \
    grub-common \
    xorriso \
    grub-pc-bin \
 && apt-get clean

COPY --from=wamr-builder /wamr /opt/wamr
COPY --from=wamr-builder /wamrlib /opt/wamrlib
RUN ln -sf /opt/wamr/product-mini/platforms/linux/build_iwasm /opt/wamr-iwasm \
 && ln -sf /opt/wamr/wamr-compiler/build_wamrc /opt/wamr-wamrc \
 && ln -sf /opt/wamrlib/wamr-compiler/build_libvmlib /opt/wamr-libvmlib \
 && ln -sf /opt/wamrlib/build_libiwasm /opt/wamr-libiwasm

COPY ./examples /home/ubuntu/examples
WORKDIR /home/ubuntu/examples

ENV PATH="$PATH:/opt/wasi-sdk/bin:/opt/wamr-iwasm:/opt/wamr-wamrc"
