# Keep tracking base image
FROM rust:1.81-slim-bookworm

# Set needed env path
ENV PATH="/opt/armv7l-linux-musleabihf-cross/:/opt/armv7l-linux-musleabihf-cross/bin/:/opt/aarch64-linux-musl-cross/:/opt/aarch64-linux-musl-cross/bin/:/opt/x86_64-linux-musl-cross/:/opt/x86_64-linux-musl-cross/bin/:$PATH"

# Set building env
ENV CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse \
    CARGO_NET_GIT_FETCH_WITH_CLI=true \
    CARGO_TARGET_ARMV7_UNKNOWN_LINUX_MUSLEABIHF_LINKER=armv7l-linux-musleabihf-gcc \
    CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER=aarch64-linux-musl-gcc \
    CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER=x86_64-linux-musl-gcc \
    CC_armv7_unknown_linux_musleabihf=armv7l-linux-musleabihf-gcc \
    CC_x86_64_unknown_linux_musl=x86_64-linux-musl-gcc \
    CC_aarch64_unknown_linux_musl=aarch64-linux-musl-gcc

### Install Additional Build Tools
RUN apt update && \
    apt install -y --no-install-recommends curl git wget make perl pkg-config tar jq gzip && \
    apt clean && \
    rm -rf /var/lib/apt/lists/*
     
### Add musl-gcc aarch64, x86_64 and armv7l
RUN wget -c https://musl.cc/x86_64-linux-musl-cross.tgz && \
    tar zxf ./x86_64-linux-musl-cross.tgz -C /opt && \
    wget -c https://musl.cc/aarch64-linux-musl-cross.tgz && \
    tar zxf ./aarch64-linux-musl-cross.tgz -C /opt && \
    wget -c http://musl.cc/armv7l-linux-musleabihf-cross.tgz && \
    tar zxf ./armv7l-linux-musleabihf-cross.tgz -C /opt && \
    rm ./x86_64-linux-musl-cross.tgz && \
    rm ./aarch64-linux-musl-cross.tgz && \
    rm ./armv7l-linux-musleabihf-cross.tgz

### Add musl target
RUN rustup target add x86_64-unknown-linux-musl && \
    rustup target add aarch64-unknown-linux-musl && \
    rustup target add armv7-unknown-linux-musleabihf && \
    rustup target add x86_64-unknown-freebsd


CMD ["bash"]
