# syntax=docker/dockerfile:1.7
#
# Builds against Ubuntu 22.04 (glibc 2.35) so the binaries also run on
# distributions older than the developer machine. Two things make that work:
#
#   * GCC 14 from the toolchain PPA — jammy's own gcc-11 has no <print>, which
#     eight translation units use.
#   * -static-libstdc++ -static-libgcc — the target then needs nothing newer
#     than the glibc of this base image.
#
#   docker build -f docker/Dockerfile --target mock-client -t tw_mock_client .
#   docker build -f docker/Dockerfile --target test .
#   docker build -f docker/Dockerfile --target export --output type=local,dest=dist .

ARG UBUNTU_VERSION=22.04

FROM ubuntu:${UBUNTU_VERSION} AS toolchain

ENV DEBIAN_FRONTEND=noninteractive

ARG VULKAN_SDK_VERSION=1.4.313

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        software-properties-common ca-certificates gnupg wget \
    && add-apt-repository -y ppa:ubuntu-toolchain-r/test \
    && wget -qO /etc/apt/trusted.gpg.d/lunarg.asc \
        https://packages.lunarg.com/lunarg-signing-key-pub.asc \
    && wget -qO /etc/apt/sources.list.d/lunarg-vulkan.list \
        "https://packages.lunarg.com/vulkan/${VULKAN_SDK_VERSION}/lunarg-vulkan-${VULKAN_SDK_VERSION}-jammy.list" \
    && apt-get update && apt-get install -y --no-install-recommends \
        gcc-14 g++-14 \
        git make ninja-build ccache pkg-config \
        python3-pip \
        vulkan-headers libvulkan-dev glslang-tools \
        libdecor-0-dev libpqxx-dev

ARG CMAKE_VERSION=4.4.0
ARG CONAN_VERSION=2.31.1
RUN pip3 install --no-cache-dir "cmake==${CMAKE_VERSION}" "conan==${CONAN_VERSION}"

RUN update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 100 \
        --slave /usr/bin/g++ g++ /usr/bin/g++-14 \
        --slave /usr/bin/gcov gcov /usr/bin/gcov-14 \
    && update-alternatives --install /usr/bin/cc cc /usr/bin/gcc-14 100 \
    && update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++-14 100

ENV CC=gcc
ENV CXX=g++

COPY docker/conan/jammy /etc/conan/jammy



# toolchain installs Conan packages
FROM toolchain AS deps

COPY conanfile.txt /src/conanfile.txt

RUN conan install /src/conanfile.txt \
        --profile:all=/etc/conan/jammy \
        --build=missing \
        --output-folder=/deps



# Builds the project
FROM deps AS builder

WORKDIR /src
COPY . .

RUN --mount=type=cache,target=/build \
    --mount=type=cache,target=/root/.ccache \
    cmake -S /src -B /build -G Ninja \
        -DCMAKE_TOOLCHAIN_FILE=/deps/conan_toolchain.cmake \
        -DCMAKE_BUILD_TYPE=Release \
        -DUSE_MOLD_LINKER=OFF \
        -DCMAKE_C_COMPILER_LAUNCHER=ccache \
        -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
        -DCMAKE_EXE_LINKER_FLAGS="-static-libstdc++ -static-libgcc" \
    && cmake --build /build \
    && mkdir -p /out/bin /out/shaders \
    && cp /build/modules/client/tw_client \
          /build/modules/mock_client/tw_mock_client \
          /build/modules/server/tw_server \
          /build/modules/chat_service/chat_server_exe/tw_chat_server_exe \
          /build/modules/chat_service/chat_service/tests/chat_mock_client/tw_chat_mock_client \
          /out/bin/ \
    && cp /build/modules/client/shaders/*.spirv /out/shaders/



# Extracts client 
FROM scratch AS export

COPY --from=builder /out/bin/tw_client /tw_client
COPY --from=builder /out/bin/tw_server /tw_server
COPY --from=builder /out/bin/tw_mock_client /tw_mock_client
COPY --from=builder /out/shaders/ /shaders/



# Mock client
FROM gcr.io/distroless/base-debian12:nonroot AS mock-client

COPY --from=builder /out/bin/tw_mock_client /usr/local/bin/
ENTRYPOINT ["/usr/local/bin/tw_mock_client"]


