# Maintainer: Sven-Hendrik Haase <svenstaro@archlinux.org>
# Maintainer: Torsten Keßler <tpkessler@archlinux.org>
# Maintainer: Jakub Klinkovský <lahwaacz at archlinux dot org>
# Contributor: bartus <arch-user-repoᘓbartus.33mail.com>
# Contributor: pingplug <pingplug@foxmail.com>
# Contributor: cornholio <vigo.the.unholy.carpathian@gmail.com>

_pkgname=magma
pkgbase=$_pkgname
pkgname=(magma-cuda magma-hip)
pkgver=2.9.0
pkgrel=3
_pkgdesc="Matrix Algebra on GPU and Multicore Architectures"
arch=('x86_64' 'aarch64')
url="https://icl.utk.edu/magma/"
license=('BSD-3-Clause')
depends=('blas' 'lapack')
makedepends=('git' 'cmake' 'ninja' 'python' 'gcc-fortran'
             'cuda'
             'rocm-core' 'hip-runtime-amd' 'hipblas' 'hipsparse')
optdepends=('python: for examples and tests'
            'gcc-fortran: Fortran interface')
source=(
  "${_pkgname}::git+https://github.com/icl-utk-edu/magma.git#tag=v${pkgver}"
  fix-cuda13.patch
)
sha256sums=('bd18fbfd1b36dbcfd604b22f2fdfaf0a18c5dcfbe09fe1f1b2f2fe3e365bc304'
            'b04f2a183fccbeb25550a6e8ab033e41b8f5d5c44938dd250aa8a0ff3bffdda0')
options=(!lto)

_valid_sm() {
  echo "sm_75 sm_80 sm_86 sm_87 sm_88 sm_89 sm_90 sm_100 sm_103 sm_110 sm_120 sm_121"
}

_valid_gfx() {
  #List of GPU targets from rocBLAS
  echo "gfx900 gfx906:xnack- gfx908:xnack- gfx90a:xnack+ gfx90a:xnack- gfx940 gfx941 gfx942 gfx1010 gfx1012 gfx1030 gfx1100 gfx1101 gfx1102"
}

prepare() {
  # NOTE: all patches must be applied before copying the sources for -cuda and -hip and running make generate
  cd "${_pkgname}"
  # Drop CMP0037 to fix cmake 4.0 build error
  git cherry-pick -n 2fecaf3f0c811344363f713669c1fe30f6879acd
  # Fix for CUDA 13
  patch -p1 < ../fix-cuda13.patch

  cd "${srcdir}"

  cp -r "${_pkgname}" "${_pkgname}-${pkgver}-cuda"
  cp -r "${_pkgname}" "${_pkgname}-${pkgver}-hip"

  cd "${_pkgname}-${pkgver}-cuda"
  echo -e "BACKEND = cuda\nFORT = true\nGPU_TARGET=$(_valid_sm)" > make.inc
  make generate

  cd "${srcdir}"

  cd "${_pkgname}-${pkgver}-hip"
  echo -e "BACKEND = hip\nFORT = true\nGPU_TARGET=$(_valid_gfx)" > make.inc
  make generate
}

build() {
  echo "Build with cuda backend"
  cd "${srcdir}/${_pkgname}-${pkgver}-cuda"

  case $CARCH in
      x86_64)
          _cuda_arch=x86_64
          ;;
      aarch64)
          _cuda_arch=sbsa
          # workaround for issue https://github.com/icl-utk-edu/magma/issues/39
          export NVCC_PREPEND_FLAGS+=' -Xfatbin=-compress-all'
          ;;
  esac

  cmake \
    -Bbuild \
    -GNinja \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/opt/cuda/targets/${_cuda_arch}-linux \
    -DBUILD_SHARED_LIBS=ON \
    -DGPU_TARGET="$(_valid_sm)"
  ninja -C build


  echo "Build with rocm/hip backend"
  cd "${srcdir}/${_pkgname}-${pkgver}-hip"
  local _rocm_ver=$(./tools/get-rocm-version.sh)
  if [ "$CARCH" = "aarch64" ]; then
    # Disable default optimizations to prevent rocm clang from crash
    unset CXXFLAGS
  fi
  # -fcf-protection is not supported by HIP, see
  # https://docs.amd.com/bundle/ROCm-Compiler-Reference-Guide-v5.5/page/Compiler_Options_and_Features.html#d2e2018
  CXXFLAGS+=" -fcf-protection=none"
  # ROCm version needs to be passed to the compiler since it's not part of the
  # cmake toolchain yet.
  CXXFLAGS+=" -DROCM_VERSION=$_rocm_ver"
  # With ROCm 6.0.0 the header moved from /opt/rocm/include to the subfolder hipsparse.
  # magma still uses the old location.
  CXXFLAGS+=" -isystem /opt/rocm/include/hipsparse"
  cmake \
    -Bbuild \
    -GNinja \
    -DCMAKE_CXX_COMPILER=/opt/rocm/bin/hipcc \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/opt/rocm \
    -DBUILD_SHARED_LIBS=ON \
    -DMAGMA_ENABLE_HIP=ON \
    -DGPU_TARGET="$(_valid_gfx)" \
    -DAMDGPU_TARGETS="$(_valid_gfx | tr ' ' ';')"
  ninja -C build
}

_package() {
  DESTDIR="${pkgdir}" ninja -Cbuild install

  local _prefix="$1"
  install -d "${pkgdir}/${_prefix}"/share/magma/example
  cp -r "${srcdir}"/magma/example/* "${pkgdir}/${_prefix}"/share/magma/example/
  install -d "${pkgdir}/${_prefix}"/share/magma/testing
  cp -r "${srcdir}"/magma/testing/* "${pkgdir}/${_prefix}"/share/magma/testing/
  install -Dm644 "${srcdir}"/magma/COPYRIGHT "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
  echo "${_prefix}/lib" > "${pkgname}.conf"
  install -Dm644 "${pkgname}.conf" "${pkgdir}"/etc/ld.so.conf.d/"${pkgname}.conf"
}

package_magma-cuda() {
  pkgdesc="${_pkgdesc} (with CUDA)"
  depends+=(cuda)
  provides=(magma)
  replaces=(magma)

  cd "${srcdir}/${_pkgname}-${pkgver}-cuda"
  _package "/opt/cuda"
}

package_magma-hip() {
  pkgdesc="${_pkgdesc} (with ROCm/HIP)"
  depends+=(hip-runtime-amd hipblas hipsparse)
  provides=(hipmagma)
  replaces=(hipmagma)

  cd "${srcdir}/${_pkgname}-${pkgver}-hip"
  _package "/opt/rocm"
}

# vim:set ts=2 sw=2 et:
