# FJ1 user app build environment (aarch64 cross compiler + host tools for tests)
FROM --platform=linux/amd64 ubuntu:24.04

ARG TOOLCHAIN_URL=https://fj1.musignal.co.jp/contents/sdk/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu.tar.xz
ARG TOOLCHAIN_SHA256=3b6465fb91564b54bbdf9578b4cc3aa198dd363f7a43820eab06ea2932c8e0bf
# libasound.so for linking only; the device (Ubuntu 18.04) provides it at runtime.
ARG LIBASOUND_URL=http://ports.ubuntu.com/ubuntu-ports/pool/main/a/alsa-lib/libasound2_1.1.3-5ubuntu0.6_arm64.deb
ARG LIBASOUND_SHA256=108bb49a4406f9918d4a5f9465f24a0f007e3c9356ddc72f82f820f2c2eb985f

ENV TOOLCHAIN=/opt/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu
ENV PATH=$TOOLCHAIN/bin:$PATH

RUN set -eux; \
    apt-get update; \
    DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
      ca-certificates curl xz-utils make python3 gcc libc6-dev libasound2-dev; \
    rm -rf /var/lib/apt/lists/*

RUN set -eux; \
    cd /tmp; \
    curl -fsSLo toolchain.tar.xz "$TOOLCHAIN_URL"; \
    echo "$TOOLCHAIN_SHA256  toolchain.tar.xz" | sha256sum -c -; \
    tar -xJf toolchain.tar.xz -C /opt --no-same-owner; \
    rm toolchain.tar.xz; \
    test -x "$TOOLCHAIN/bin/aarch64-linux-gnu-gcc"; \
    curl -fsSLo libasound2.deb "$LIBASOUND_URL"; \
    echo "$LIBASOUND_SHA256  libasound2.deb" | sha256sum -c -; \
    dpkg-deb -x libasound2.deb libasound2; \
    libdir="$TOOLCHAIN/aarch64-linux-gnu/libc/usr/lib"; \
    cp -P libasound2/usr/lib/aarch64-linux-gnu/libasound.so.2* "$libdir/"; \
    ln -sf libasound.so.2 "$libdir/libasound.so"; \
    rm -rf libasound2 libasound2.deb; \
    aarch64-linux-gnu-gcc --version

WORKDIR /work
CMD ["make"]
