# Builder for the Linux release tarball (see misc/release/package.sh).
# Ubuntu 24.04 keeps the glibc requirement at 2.38, so the binary runs on
# current distros, not just ones as new as the build machine. SDL3 isn't
# packaged there, so the three SDL libraries are built from pinned releases
# and shipped next to the binary.
FROM docker.io/library/ubuntu:24.04

RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
      build-essential cmake ninja-build git pkg-config ca-certificates patchelf \
      libx11-dev libxext-dev libxrandr-dev libxcursor-dev libxfixes-dev libxi-dev \
      libxss-dev libxtst-dev libxkbcommon-dev libwayland-dev wayland-protocols \
      libegl1-mesa-dev libgl1-mesa-dev libgles2-mesa-dev libdrm-dev libgbm-dev \
      libdecor-0-dev libdbus-1-dev libudev-dev libpulse-dev libasound2-dev \
    && rm -rf /var/lib/apt/lists/*

ARG SDL_TAG=release-3.4.16
ARG SDL_TTF_TAG=release-3.2.2
ARG SDL_IMAGE_TAG=release-3.4.6

RUN set -e; cd /tmp; \
    for spec in "SDL $SDL_TAG" "SDL_ttf $SDL_TTF_TAG -DSDLTTF_VENDORED=ON" \
                "SDL_image $SDL_IMAGE_TAG -DSDLIMAGE_VENDORED=ON -DSDLIMAGE_AVIF=OFF -DSDLIMAGE_WEBP=OFF -DSDLIMAGE_JXL=OFF -DSDLIMAGE_TIF=OFF"; do \
      set -- $spec; repo=$1; tag=$2; shift 2; \
      git clone -q --depth 1 --recurse-submodules --shallow-submodules -b "$tag" "https://github.com/libsdl-org/$repo.git"; \
      cmake -S "$repo" -B "$repo/build" -G Ninja -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_INSTALL_PREFIX=/opt/sdl -DCMAKE_PREFIX_PATH=/opt/sdl "$@"; \
      cmake --build "$repo/build"; cmake --install "$repo/build"; \
      rm -rf "$repo"; \
    done

# The project needs CMake 3.30+; 24.04 ships 3.28. Kitware's static build.
ARG CMAKE_VERSION=3.31.6
RUN set -e; cd /opt; \
    apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends curl >/dev/null && rm -rf /var/lib/apt/lists/*; \
    curl -fsSL "https://github.com/Kitware/CMake/releases/download/v$CMAKE_VERSION/cmake-$CMAKE_VERSION-linux-x86_64.tar.gz" | tar xz; \
    ln -sf "/opt/cmake-$CMAKE_VERSION-linux-x86_64/bin/cmake" /usr/local/bin/cmake
