# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

ARG TOOLKIT_BASE_IMAGE=ubuntu:22.04
FROM ${TOOLKIT_BASE_IMAGE:-ubuntu:22.04} AS cuda

ENV DEBIAN_FRONTEND=noninteractive

RUN apt update && apt install -y libxml2 curl perl gcc && \
    rm -rf /var/lib/apt/lists/*

# Detect architecture and download appropriate CUDA installer
RUN arch=$(dpkg --print-architecture) && \
    case "${arch}" in \
        amd64) \
            cuda_arch_suffix="linux";; \
        arm64) \
            cuda_arch_suffix="linux_sbsa";; \
        *) \
            echo >&2 "error: unsupported architecture ('${arch}')"; \
            exit 1;; \
    esac && \
    echo "Downloading CUDA 12.9.0 for ${arch} (suffix: ${cuda_arch_suffix})" && \
    curl -LO "https://developer.download.nvidia.com/compute/cuda/12.9.0/local_installers/cuda_12.9.0_575.51.03_${cuda_arch_suffix}.run" && \
    chmod +x cuda_*.run && \
    ./cuda_*.run --silent --no-opengl-libs --toolkit && \
    rm -f cuda_*.run
