FROM alpine:3.15 as base

RUN apk update
RUN apk add --no-cache cmake make clang clang-static clang-dev llvm12-dev llvm12-static musl-dev git lld  libgcc gcc  g++ libstdc++ build-base lld-dev lld-static llvm12-libs libc-dev xz zlib zlib-dev libxml2 libxml2-dev

ENV CXX=clang++
ENV CC=clang
ENV LDFLAGS='-L/usr/include -L/usr/include/llvm12'
ENV CXXFLAGS="-I/usr/include -I/usr/include/llvm12"
ENV PATH="/usr/bin:/usr/local/bin:/zig/bin:$PATH"

FROM base as zig

# Compile zig
RUN --mount=type=tmpfs,target=/zig-build mkdir /zig; \
    cd /zig-build && \
    mkdir -p /zig-build; \
    git clone -b jarred/zig-sloppy-with-small-structs --single-branch --depth=1 https://github.com/oven-sh/zig.git .; \
    cd /zig-build; \
    cmake . -DZIG_STATIC_LLVM=ON -DLLVM_INCLUDE_DIRS=$LLVM_INCLUDE_DIRS -DCMAKE_BUILD_TYPE=Release && \
    make -j$(nproc) && make install && \
    rm -rf .git && \
    cd /zig-build && \
    cp -r /zig-build/bin /zig/bin && \
    cp -r /zig-build/lib /zig/lib 

