# 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.

#########################################################################################
##  Build CV-CUDA builder dependencies on top of manylinux_2_28
##  CV-CUDA builder images are based on manylinux_2_28, official page https://github.com/pypa/manylinux
#########################################################################################
ARG FROM_IMAGE_NAME=quay.io/pypa/manylinux_2_28_x86_64:latest
ARG CUDA_IMAGE=scratch
ARG BUILDER_EXTRA_DEPS=scratch
ARG BUILDER_CUDA_EXTRA_DEPS=scratch
FROM ${BUILDER_CUDA_EXTRA_DEPS:-scratch} AS cuda_extra_deps
FROM ${BUILDER_EXTRA_DEPS:-scratch} AS extra_deps
FROM ${CUDA_IMAGE:-scratch} AS cuda
FROM ${FROM_IMAGE_NAME:-quay.io/pypa/manylinux_2_28_x86_64:latest}

ARG CMAKE_VERSION=3.24.3
ARG CC=gcc
ARG CXX=g++

# Enable gcc-toolset-10 (AlmaLinux 8 / manylinux_2_28)
ENV PATH=/opt/rh/gcc-toolset-10/root/usr/bin:${PATH} \
    LD_LIBRARY_PATH=/opt/rh/gcc-toolset-10/root/usr/lib64:/opt/rh/gcc-toolset-10/root/usr/lib:${LD_LIBRARY_PATH:-} \
    MANPATH=/opt/rh/gcc-toolset-10/root/usr/share/man:${MANPATH:-}

# Detect architecture and install CMake
RUN export CMAKE_VERSION=${CMAKE_VERSION} \
      && ARCH=$(uname -m) \
      && echo "Detected architecture: ${ARCH}" \
      && echo "Downloading CMake ${CMAKE_VERSION} for ${ARCH}..." \
      && wget https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/cmake-${CMAKE_VERSION}-linux-${ARCH}.sh \
        -O /tmp/cmake-install.sh \
      && echo "Download completed, verifying file..." \
      && ls -lh /tmp/cmake-install.sh \
      && chmod u+x /tmp/cmake-install.sh \
      && mkdir -p /opt/cmake-${CMAKE_VERSION} \
      && echo "Running CMake installer..." \
      && sh /tmp/cmake-install.sh --skip-license --prefix=/opt/cmake-${CMAKE_VERSION} \
      && echo "CMake installation completed" \
      && rm -f /tmp/cmake-install.sh \
      && rm -f /usr/local/bin/*cmake* \
      && rm -f /usr/local/bin/cpack \
      && rm -f /usr/local/bin/ctest \
      && for file in /opt/cmake-${CMAKE_VERSION}/bin/*; do \
           if [ -f "$file" ]; then \
             ln -s "$file" /usr/local/bin/; \
           fi; \
         done

# build dependencies
RUN curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.rpm.sh | bash \
    && yum install -y epel-release almalinux-release-devel \
    && yum install -y --enablerepo=powertools,devel \
                      git-lfs openssl-devel openssl-static \
                      ninja-build ccache dejavu-sans-fonts

# Build and install zlib 1.3.1 from source to replace vulnerable system version (CVE-2018-25032)
# Remove any old system zlib artifacts and install new version to /usr/local
RUN curl -L https://github.com/madler/zlib/releases/download/v1.3.1/zlib-1.3.1.tar.gz -o /tmp/zlib-1.3.1.tar.gz \
    && tar -xzf /tmp/zlib-1.3.1.tar.gz -C /tmp \
    && cd /tmp/zlib-1.3.1 \
    && ./configure --prefix=/usr/local \
    && make -j$(nproc) \
    && rm -f /usr/lib*/libz.* /usr/lib64/libz.* \
    && make install \
    && rm -rf /tmp/zlib-1.3.1* \
    && ldconfig

# Python
ARG PYVER=3.9
ARG PYV=39

ENV PYVER=${PYVER} PYV=${PYV} PYTHONPATH=/opt/python/v
ENV PYBIN=${PYTHONPATH}/bin \
    PYLIB=${PYTHONPATH}/lib

ENV PATH=/opt/python/cp39-cp39/bin:/opt/python/cp310-cp310/bin:/opt/python/cp311-cp311/bin:/opt/python/cp312-cp312/bin:/opt/python/cp313-cp313/bin:/opt/python/cp313-cp313t/bin:/opt/python/cp314-cp314/bin:${PYBIN}:${PATH} \
    LD_LIBRARY_PATH=/usr/local/lib:/opt/python/cp39-cp39/lib:/opt/python/cp310-cp310/lib:/opt/python/cp311-cp311/lib:/opt/python/cp312-cp312/lib:/opt/python/cp313-cp313/lib:/opt/python/cp313-cp313t/lib:/opt/python/cp314-cp314/lib:${PYLIB}:${LD_LIBRARY_PATH:-} \
    LIBRARY_PATH=/usr/local/lib:/opt/python/cp39-cp39/lib:/opt/python/cp310-cp310/lib:/opt/python/cp311-cp311/lib:/opt/python/cp312-cp312/lib:/opt/python/cp313-cp313/lib:/opt/python/cp313-cp313t/lib:/opt/python/cp314-cp314/lib:${PYLIB}:${LIBRARY_PATH:-}

RUN echo "Looking for Python directories in /opt/python:" && \
    ls -la /opt/python/ && \
    echo "Searching for cp${PYV}* pattern:" && \
    find /opt/python -name "cp${PYV}*" \( -type d -o -type l \) && \
    PYTHON_DIR=$(find /opt/python -name "cp${PYV}*" \( -type d -o -type l \) | head -1) && \
    if [ -n "$PYTHON_DIR" ]; then \
        echo "Found Python directory: $PYTHON_DIR" && \
        ln -s "$PYTHON_DIR" /opt/python/v; \
    else \
        echo "No Python directory found matching cp${PYV}*, trying alternative patterns..." && \
        PYTHON_DIR=$(find /opt/python -name "*python*" \( -type d -o -type l \) | grep -E "cp${PYV}|python${PYV}" | head -1) && \
        if [ -n "$PYTHON_DIR" ]; then \
            echo "Found alternative Python directory: $PYTHON_DIR" && \
            ln -s "$PYTHON_DIR" /opt/python/v; \
        else \
            echo "Error: No Python directory found for version ${PYV}" && \
            echo "Available directories and links:" && \
            find /opt/python \( -type d -o -type l \) && \
            exit 1; \
        fi; \
    fi

# install pip packages needed only for the system python
COPY requirements.sys_python.txt /tmp/requirements.sys_python.txt
RUN python -m pip install --break-system-packages --force-reinstall -r /tmp/requirements.sys_python.txt && \
    rm -rf /root/.cache/pip /tmp/requirements.sys_python.txt

RUN ldconfig

# extra deps
COPY --from=extra_deps / /

# install CUDA from the CUDA image
ENV PATH=/usr/local/cuda/bin:${PATH}

ENV NVIDIA_DRIVER_CAPABILITIES=video,compute,utility

# CUDA
COPY --from=cuda /usr/local/cuda /usr/local/cuda

# extra deps
COPY --from=cuda_extra_deps / /
