# Build environment for the Linux AppImages (tools/linux/appimage.sh): Ubuntu 22.04, so the result runs on glibc 2.35+.
# Built once per arch (podman build --arch amd64 / arm64). The amd64 image builds everything, the aarch64 AppImage
# cross-compiled; the arm64 image is the cross build's sysroot (the same -dev packages) and freezes the arm64 seed
# generator helper (PyInstaller only builds for the machine it runs on).
FROM docker.io/library/ubuntu:22.04
ARG DEBIAN_FRONTEND=noninteractive
# Downloads retried: one failed package fetch on the CI machine failed two release builds on 2026-11-17, and on
# 2026-12-01 archive.ubuntu.com / security.ubuntu.com were unreachable for minutes while apt-get update only warned
# and still exited 0, so the install after it failed. Acquire::Retries retries a single file; the sequences below
# retry a whole update+install. APT::Update::Error-Mode (apt 2.4 in 22.04 has it) makes an update fail when a list
# can't be fetched; apt_update below also checks the output, which works with any apt and was tested with a stub.
RUN printf 'Acquire::Retries "5";\nAcquire::http::Timeout "30";\nAcquire::https::Timeout "30";\nAPT::Update::Error-Mode "any";\n' > /etc/apt/apt.conf.d/80-retries
# The image's Ubuntu sources go through apt's mirror method (mirror+file:, apt 2.4 in 22.04): on 2026-12-01
# archive.ubuntu.com and security.ubuntu.com were unreachable from GitHub's runners for over ten minutes,
# while azure.archive.ubuntu.com (the mirror GitHub's own Ubuntu runners use) and mirrors.edge.kernel.org answered
# and carry all four jammy pockets, so the amd64 image lists all three and apt moves on to the next mirror when one
# fails. arm64 is left alone: its image uses ports.ubuntu.com/ubuntu-ports, which these mirrors don't carry. The
# rewrite matches ubuntu:22.04's /etc/apt/sources.list exactly ("deb http://archive.ubuntu.com/ubuntu/ jammy main
# restricted", "deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted", trailing slash); if a future
# base image changes that and nothing is rewritten, the build stops here instead of silently using one host.
RUN set -eu; \
    if [ "$(dpkg --print-architecture)" = amd64 ]; then \
        printf '%s\n' \
            'http://azure.archive.ubuntu.com/ubuntu/' \
            'http://archive.ubuntu.com/ubuntu/' \
            'http://mirrors.edge.kernel.org/ubuntu/' > /etc/apt/ubuntu-mirrors.txt; \
        sed -i -e 's|^deb http://archive\.ubuntu\.com/ubuntu/|deb mirror+file:/etc/apt/ubuntu-mirrors.txt|' \
               -e 's|^deb http://security\.ubuntu\.com/ubuntu/|deb mirror+file:/etc/apt/ubuntu-mirrors.txt|' \
            /etc/apt/sources.list; \
        if ! grep -q '^deb mirror+file:/etc/apt/ubuntu-mirrors.txt ' /etc/apt/sources.list; then \
            echo "apt mirrors: /etc/apt/sources.list had no archive.ubuntu.com / security.ubuntu.com deb line - the base image's sources changed" >&2; \
            exit 1; \
        fi; \
        if grep -qE '^deb[[:space:]]+http://(archive|security)\.ubuntu\.com/ubuntu/' /etc/apt/sources.list; then \
            echo "apt mirrors: a deb line still points at archive.ubuntu.com / security.ubuntu.com after the rewrite" >&2; \
            exit 1; \
        fi; \
    fi
RUN set -eu; \
    # Retry a whole sequence for about ten minutes (waits 20+40+80+160+300 s), then fail. Every attempt starts at
    # apt-get update: a half-finished update is not continued.
    retry() { \
        for wait in 0 20 40 80 160 300; do \
            [ "$wait" = 0 ] || { echo "retry: waiting ${wait}s before another attempt: $*" >&2; sleep "$wait"; }; \
            "$@" && return 0; \
        done; \
        echo "retry: giving up after about 10 minutes: $*" >&2; \
        return 1; \
    }; \
    # apt-get update exits 0 when a source can't be fetched (it only prints "W: Failed to fetch"); that is a failure
    # here too. The pattern is anchored: an error line, a failed fetch, or lists that were ignored.
    apt_update() { \
        out=$(apt-get update 2>&1) || { printf '%s\n' "$out" >&2; return 1; }; \
        printf '%s\n' "$out" >&2; \
        if printf '%s\n' "$out" | grep -qE '^(W: Failed to fetch|W: Some index files failed to download|E: )'; then \
            echo "apt-get update: a package list could not be fetched - see above" >&2; \
            return 1; \
        fi; \
    }; \
    ubuntu_packages() { \
        apt_update || return 1; \
        apt-get install -y --no-install-recommends \
            ca-certificates curl git file xz-utils patchelf make cmake ninja-build pkg-config nasm python3 \
            g++ software-properties-common gpg-agent \
            # SDL3's video / audio / input backends (it loads them at run time; the headers decide what it can use)
            libx11-dev libxext-dev libxrandr-dev libxcursor-dev libxi-dev libxss-dev libxfixes-dev libxtst-dev \
            libxkbcommon-dev libwayland-dev wayland-protocols libdecor-0-dev libegl-dev libgl-dev libdrm-dev libgbm-dev \
            libpulse-dev libasound2-dev libpipewire-0.3-dev libdbus-1-dev libudev-dev libibus-1.0-dev \
            libvulkan-dev libzstd-dev; \
    }; \
    # clang 21 from apt.llvm.org: 22.04's clang 15 lacks the __builtin_elementwise_* the lifted FP code uses (simd_fp.h).
    # The PPA/source and key are written on every attempt (idempotent), then the second update+install is retried
    # as a whole too.
    llvm_packages() { \
        add-apt-repository -y ppa:deadsnakes/ppa || return 1; \
        curl -fsSL --retry 5 --retry-all-errors --retry-delay 30 --connect-timeout 30 \
            https://apt.llvm.org/llvm-snapshot.gpg.key -o /etc/apt/trusted.gpg.d/apt.llvm.org.asc || return 1; \
        echo "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-21 main" > /etc/apt/sources.list.d/llvm.list || return 1; \
        apt_update || return 1; \
        apt-get install -y --no-install-recommends python3.12 python3.12-venv python3.12-dev clang-21 lld-21 llvm-21; \
    }; \
    retry ubuntu_packages; \
    retry llvm_packages; \
    rm -rf /var/lib/apt/lists/*; \
    for t in clang clang++ ld.lld llvm-ar llvm-nm llvm-ranlib llvm-strip; do ln -sf $t-21 /usr/bin/$t; done
