# stm32cdc images.
#
# No `# syntax=` directive on purpose: it makes BuildKit fetch its frontend from
# docker.io, which is not reachable from the daemon here. The built-in frontend
# covers everything this file uses.
#   --target builder : toolchain + make, no USB, CI-safe
#   --target debug   : builder + openocd/gdb, needs /dev/bus/usb
#
# Base comes from mirror.gcr.io: docker.io is not reachable from the Docker
# Desktop daemon in this environment (DNS for auth.docker.io fails), while the
# GCR pull-through mirror works.
ARG DEBIAN_IMAGE=mirror.gcr.io/library/debian:12-slim

# One version for the whole tool: it labels the images, is recorded in
# build-info.json, and is what the extension compares itself against. Source and
# revision are left empty until a CI job fills them in from the checkout.
ARG STM32CDC_VERSION=0.1.1
ARG STM32CDC_SOURCE=
ARG STM32CDC_REVISION=

# ---------------------------------------------------------------- toolchain --
FROM ${DEBIAN_IMAGE} AS toolchain

# Arm GNU Toolchain, pinned by version + checksum. Bumping the version is a
# deliberate act: it changes code size and codegen.
ARG ARM_VERSION=14.3.rel1
ARG ARM_SHA256=8f6903f8ceb084d9227b9ef991490413014d991874a1e34074443c2a72b14dbd

RUN apt-get update \
 && apt-get install -y --no-install-recommends ca-certificates curl xz-utils \
 && rm -rf /var/lib/apt/lists/*

RUN set -eux; \
    url="https://developer.arm.com/-/media/Files/downloads/gnu/${ARM_VERSION}/binrel/arm-gnu-toolchain-${ARM_VERSION}-x86_64-arm-none-eabi.tar.xz"; \
    curl -fsSL -o /tmp/tc.tar.xz "$url"; \
    echo "${ARM_SHA256}  /tmp/tc.tar.xz" | sha256sum -c -; \
    mkdir -p /opt/arm-gnu-toolchain; \
    tar -xJf /tmp/tc.tar.xz -C /opt/arm-gnu-toolchain --strip-components=1; \
    rm -f /tmp/tc.tar.xz; \
    rm -rf /opt/arm-gnu-toolchain/share/doc /opt/arm-gnu-toolchain/share/man

# ------------------------------------------------------------------ builder --
FROM ${DEBIAN_IMAGE} AS builder
ARG ARM_VERSION=14.3.rel1
ARG STM32CDC_VERSION
ARG STM32CDC_SOURCE
ARG STM32CDC_REVISION

LABEL org.opencontainers.image.title="stm32cdc builder" \
      org.opencontainers.image.description="Toolchain and build systems for CubeMX STM32 projects" \
      org.opencontainers.image.licenses="MIT" \
      org.opencontainers.image.version="${STM32CDC_VERSION}" \
      org.opencontainers.image.source="${STM32CDC_SOURCE}" \
      org.opencontainers.image.revision="${STM32CDC_REVISION}" \
      dev.stm32cdc.toolchain="arm-gnu-${ARM_VERSION}"

COPY --from=toolchain /opt/arm-gnu-toolchain /opt/arm-gnu-toolchain
ENV PATH=/opt/arm-gnu-toolchain/bin:$PATH \
    STM32CDC_TOOLCHAIN=arm-gnu-${ARM_VERSION} \
    STM32CDC_VERSION=${STM32CDC_VERSION}

# libncursesw6/libtinfo6 are needed by the prebuilt arm-none-eabi-gdb.
RUN apt-get update \
 && apt-get install -y --no-install-recommends \
      make cmake ninja-build python3 git ca-certificates coreutils \
      libncursesw6 libtinfo6 \
 && rm -rf /var/lib/apt/lists/*

COPY build.mk stm32cdc.py /opt/stm32cdc/
ENV STM32CDC_MK=/opt/stm32cdc/build.mk \
    STM32CDC_IN_CONTAINER=1

WORKDIR /work

# -------------------------------------------------------------------- debug --
FROM builder AS debug
ARG STM32CDC_VERSION

LABEL org.opencontainers.image.title="stm32cdc debug" \
      org.opencontainers.image.description="stm32cdc builder plus OpenOCD and GDB; needs /dev/bus/usb" \
      org.opencontainers.image.version="${STM32CDC_VERSION}"

RUN apt-get update \
 && apt-get install -y --no-install-recommends \
      openocd usbutils libusb-1.0-0 \
 && rm -rf /var/lib/apt/lists/*

EXPOSE 3333 4444 6666
