# Reproducible build environment for dora-zed.
#
# Compiles only — no camera is needed. Requires an NVIDIA driver and the NVIDIA
# Container Toolkit on the host, as the ZED SDK does anyway.
#
# NVIDIA_DRIVER_CAPABILITIES below includes `video` because libsl_zed.so links
# against NVDEC (libnvcuvid) and NVENC (libnvidia-encode); the container runtime
# mounts those from the host only when `video` is in the list.
#
# Ubuntu 24.04 is the newest release Stereolabs ships a ZED SDK for.
#
#   docker build -t dora-zed-build .
#   docker run --rm --gpus all --user "$(id -u):$(id -g)" \
#     -v "$PWD:/src" -v dora-zed-cargo:/usr/local/cargo/registry dora-zed-build
#
# --user keeps build/ and bin/ owned by you instead of root.
#
# The first run clones dora-rs and compiles it with Cargo, which takes a while;
# the named volume keeps the registry cache across runs.

FROM nvidia/cuda:12.8.1-devel-ubuntu24.04

ARG ZED_VERSION=5.2
ARG ORT_VERSION=1.24.4

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y --no-install-recommends \
        build-essential cmake git curl ca-certificates pkg-config \
        libopencv-dev zstd udev sudo \
    && rm -rf /var/lib/apt/lists/*

# The toolchain lives outside /root so the container can run as the invoking
# user (--user) and still write the cargo caches. registry/ is created here so
# that a named volume mounted over it inherits these permissions rather than
# being created root-owned.
ENV RUSTUP_HOME=/usr/local/rustup \
    CARGO_HOME=/usr/local/cargo \
    PATH=/usr/local/cargo/bin:${PATH}
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \
        | sh -s -- -y --profile minimal --default-toolchain stable \
    && mkdir -p ${CARGO_HOME}/registry \
    && chmod -R a+w ${RUSTUP_HOME} ${CARGO_HOME}

# The installer leaves /usr/local/zed as drwxrwx--- root:zed, which an arbitrary
# --user uid cannot read, so relax it to make find_package(zed) work for one.
RUN curl -fSL -o /tmp/ZED_SDK.run \
        "https://download.stereolabs.com/zedsdk/${ZED_VERSION}/cu12/ubuntu24" \
    && chmod +x /tmp/ZED_SDK.run \
    && /tmp/ZED_SDK.run -- silent skip_python skip_tools skip_drivers skip_cuda \
    && chmod -R a+rX /usr/local/zed \
    && rm -f /tmp/ZED_SDK.run

# The tarball puts its CMake package files under lib/, but onnxruntimeTargets.cmake
# resolves the library as <prefix>/lib64. Ubuntu is multiarch, so CMake does not
# search lib64 for packages and ld.so does not search /usr/local/lib64 at all:
# keep the libraries in lib64, move only the package files to lib/cmake, and add
# lib64 to the linker path.
RUN curl -fSL -o /tmp/ort.tgz \
        "https://github.com/microsoft/onnxruntime/releases/download/v${ORT_VERSION}/onnxruntime-linux-x64-gpu-${ORT_VERSION}.tgz" \
    && tar -xzf /tmp/ort.tgz -C /tmp \
    && mkdir -p /usr/local/include/onnxruntime /usr/local/lib64 \
    && cp -r /tmp/onnxruntime-linux-x64-gpu-${ORT_VERSION}/include/* /usr/local/include/onnxruntime/ \
    && cp -r /tmp/onnxruntime-linux-x64-gpu-${ORT_VERSION}/lib/* /usr/local/lib64/ \
    && mkdir -p /usr/local/lib/cmake \
    && mv /usr/local/lib64/cmake/onnxruntime /usr/local/lib/cmake/ \
    && rmdir /usr/local/lib64/cmake \
    && echo /usr/local/lib64 > /etc/ld.so.conf.d/onnxruntime.conf \
    && ldconfig \
    && rm -rf /tmp/ort.tgz /tmp/onnxruntime-linux-x64-gpu-${ORT_VERSION}

ENV NVIDIA_DRIVER_CAPABILITIES=compute,utility,video

WORKDIR /src
CMD ["scripts/build.sh"]
