#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2022-2023 Blender Authors
#
# SPDX-License-Identifier: GPL-2.0-or-later

# This script is part of the official build environment, see wiki page for details.
# https://developer.blender.org/docs/handbook/release_process/build/rocky_8/
set -e

if [ `id -u` -ne 0 ]; then
  echo "This script must be run as root"
  exit 1
fi

# Current architecture
ARCH=$(uname -i)

# Required by: config manager command below to enable powertools.
dnf -y install 'dnf-command(config-manager)'

# Required for version locking CUDA installation
dnf -y install 'dnf-command(versionlock)'

# Packages `ninja-build` and `meson` are not available unless CBR or PowerTools repositories are enabled.
# See: https://wiki.rockylinux.org/rocky/repo/#notes-on-unlisted-repositories
dnf config-manager --set-enabled powertools

# Required by: epel-release has the patchelf and rubygem-asciidoctor packages
dnf -y install epel-release

# Install all the packages needed for a new tool-chain.
#
# NOTE: Keep this separate from the packages install, since otherwise
# older tool-chain will be installed.
dnf -y install scl-utils
dnf -y install scl-utils-build

# Currently this is defined by the VFX platform (CY2023), see: https://vfxplatform.com
dnf -y install gcc-toolset-14

# For RHEL8 there is no aarch64 repo, instead use sbsa which works for device binaries.
# For RHEL9 there is an aarch64 repo, and this fallback will no longer be needed.
if [ "$ARCH" = "aarch64" ]; then
    CUDA_ARCH="sbsa"
fi
# Repository for CUDA (`nvcc`)
dnf config-manager --add-repo http://developer.download.nvidia.com/compute/cuda/repos/rhel8/${CUDA_ARCH-x86_64}/cuda-rhel8.repo
# Version lock CUDA install/update candidates to 12.8
dnf versionlock add 'cuda-*-12-8*'

# Install packages needed for Blender's dependencies.
PACKAGES_FOR_LIBS=(
    # Used to checkout Blender's code.
    git
    git-lfs
    # Used to extract packages.
    bzip2
    # Used to extract packages.
    tar
    # Blender and some dependencies use `cmake`.
    cmake3
    # Apply patches from Blender's: `./build_files/build_environment/patches`
    patch
    # Use by `cmake` and `autoconf`.
    make

    # Required by: `external_nasm` which uses an `autoconf` build-system.
    autoconf
    automake
    libtool

    # Requried by flex
    help2man

    # Required by: `external_libsndfile` configure scripts.
    autogen

    # Used to set rpath on shared libraries
    patchelf

    # Builds generated by meson use Ninja for the actual build.
    ninja-build

    # Required by Blender build option: `WITH_GHOST_WAYLAND`.
    mesa-libEGL-devel
    # Required by: Blender & `external_opensubdiv` (probably others).
    mesa-libGL-devel
    mesa-libGLU-devel

    # NOTE(@ideasman42): Currently flex's `autogen.sh` is required to run because the bundled
    # configuration is looking for an older version of `aclocal` than the system provides.
    # This is resolved by generating new configuration files which requires the `autopoint`
    # command from `gettext-devel`, if the flex package is updated we could remove this.
    # Required by: [`flex` running `autogen.sh` for `autopoint`].
    gettext-devel
    # NOTE(@ideasman42): It seems newer files generated by `autogen.sh` also require `makeinfo`
    # and there isn't a flag to disable GNU "info".
    # Required by: [`flex` as a build-time dependency for `makeinfo`].
    texinfo

    # NOTE(@ideasman42): `nvcc` will *not* be added to the `PATH`, must be done manually.
    # Commands from:
    # https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#environment-setup
    # Can be added to `~/.bash_profile`.
    # `export LD_LIBRARY_PATH=/usr/local/cuda-12-8/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}`
    # `export PATH=/usr/local/cuda-12-8/bin${PATH:+:${PATH}}`
    # Required by `external_openimagedenoise` (`nvcc` command)
    cuda-toolkit-12-8

    # Required by: `external_ispc`.
    zlib-devel
    # TODO: dependencies build without this, consider removal.
    rubygem-asciidoctor
    # TODO: dependencies build without this, consider removal.
    wget
    # Required by: `external_sqlite` as a build-time dependency (needed for the `tclsh` command).
    tcl
    # Required by: `external_aom`.
    # TODO: Blender is already building `external_nasm` which is listed as an alternative to `yasm`.
    # Why are both needed?
    yasm

    # NOTE(@ideasman42): while `python39` is available, the default Python version is 3.6.
    # For example, this is used for the `python3-mako` package
    # So use the "default" system Python since it means it's most compatible with other packages.
    python3

    # Required by: `external_igc`.
    python3-mako
    python3-pyyaml

    # Required by: `external_igc` & `external_osl` as a build-time dependency.
    bison
    # Required by: `external_osl` as a build-time dependency.
    flex

    # Required by: `external_ispc`.
    ncurses-devel
    # Required by: `external_ispc` (when building with CLANG).
    libstdc++-static

    # Required by: `external_ssl` (build dependencies).
    perl-core
    perl-IPC-Cmd
    perl-Pod-Html

    # Required by: `external_wayland_weston`
    cairo-devel
    libdrm-devel
    pixman-devel
    libffi-devel
    libinput-devel
    libevdev-devel
    mesa-libgbm-devel
    systemd-devel # for `libudev` (not so obvious!).
    # Required by: `weston --headless` (run-time requirement for off screen rendering).
    mesa-dri-drivers
    mesa-libEGL
    mesa-libGL
)

# Additional packages needed for building Blender.
PACKAGES_FOR_BLENDER=(
    # Required by Blender build option: `WITH_GHOST_WAYLAND`.
    libxkbcommon-devel

    # Required by Blender build option: `WITH_GHOST_X11`.
    libX11-devel
    libXcursor-devel
    libXi-devel
    libXinerama-devel
    libXrandr-devel
    libXt-devel
    libXxf86vm-devel
)

dnf -y install ${PACKAGES_FOR_LIBS[@]} ${PACKAGES_FOR_BLENDER[@]}

# Dependencies for pip (needed for `buildbot-worker`), uses Python3.6.
dnf -y install python3 python3-pip python3-devel

# Dependencies for asound.
dnf -y install alsa-lib-devel pulseaudio-libs-devel

# Required by Blender build option: `WITH_JACK`.
dnf -y install jack-audio-connection-kit-devel

# For ROCm there is no aarch64 repo
if [ "$ARCH" != "aarch64" ]; then
    # AMD's ROCM
    # Based on instructions from:
    # https://rocm.docs.amd.com/projects/install-on-linux/en/latest/how-to/native-install/rhel.html
    # NOTE: the following steps have intentionally been skipped as they aren't needed:
    # - "Register kernel-mode driver".
    # - "Install kernel driver".

    # Register ROCm packages
    rpm --import https://repo.radeon.com/rocm/rocm.gpg.key

    rm -f /etc/yum.repos.d/graphics-7.2.1.repo
    rm -f /etc/yum.repos.d/rocm-7.2.1.repo

    tee /etc/yum.repos.d/graphics-7.2.1.repo > /dev/null <<EOF
[graphics-7.2.1]
name=graphics-7.2.1
baseurl=https://repo.radeon.com/graphics/7.2.1/el/8.10/main/x86_64/
enabled=1
priority=50
gpgcheck=1
gpgkey=https://repo.radeon.com/rocm/rocm.gpg.key
EOF

    tee /etc/yum.repos.d/rocm-7.2.1.repo > /dev/null <<EOF
[ROCm-7.2.1]
name=ROCm-7.2.1
baseurl=https://repo.radeon.com/rocm/el8/7.2.1/main
enabled=1
gpgcheck=1
exclude=rock-dkms
gpgkey=https://repo.radeon.com/rocm/rocm.gpg.key
EOF

    dnf -y update
    dnf -y install hipcc7.2.1 hip-devel7.2.1 rocm-llvm7.2.1 rocm-core7.2.1 rocm-device-libs7.2.1
    update-alternatives --set rocm /opt/rocm-7.2.1
fi
