# Maintainer: Torsten Keßler <tpkessler at archlinux dot org>
# Maintainer: Christian Heusel <gromit@archlinux.org>

pkgname=hipblaslt
pkgver=6.4.4
pkgrel=1
pkgdesc='General matrix-matrix operations beyond a traditional BLAS library'
arch=('x86_64' 'aarch64')
url='https://rocm.docs.amd.com/projects/hipBLASLt/en/latest/index.html'
license=('MIT')
depends=('rocm-core' 'glibc' 'gcc-libs' 'hip-runtime-amd' 'hipblas' 'rocblas')
makedepends=('rocm-cmake'  'rocm-toolchain' 'python' 'cmake' 'git' 'ninja'
             'msgpack-cxx' 'python-msgpack' 'python-setuptools' 'python-joblib'
             'python-pyaml' 'hipblas-common')
_git='https://github.com/ROCm/hipBLASLt'
source=("$pkgname-$pkgver.tar.gz::$_git/archive/rocm-$pkgver.tar.gz"
        "$pkgname-find-msgpack5.patch"
        "$pkgname-clang.patch")
sha256sums=('6d8816708564e788b5e071cf79bb9ce2caffbeb30143c10c5ba6cec58a7f6efa'
            '8e6d134a1d45de814099dfe591296de5e1d47f9906ccf9c51d7f2228e992c5f4'
            '46a816b7d0555f30362467713e2356af83865e7ff2d727484690606bf89b9f14')
_dirname="$(basename "$_git")-$(basename "${source[0]}" ".tar.gz")"

options=(!strip)

prepare() {
  cd "$_dirname"
  patch -Np1 -i "$srcdir/$pkgname-find-msgpack5.patch"
  patch -Np1 -i "$srcdir/$pkgname-clang.patch"
}

build() {
  export PATH="/opt/rocm/bin:$PATH"
  local cmake_args=(
    -Wno-dev
    -G Ninja
    -B build
    -S "$_dirname"
    -D CMAKE_BUILD_TYPE=None
    -D CMAKE_C_COMPILER=amdclang
    -D CMAKE_CXX_COMPILER=amdclang++
    -D CMAKE_INSTALL_PREFIX=/opt/rocm
    -D Tensile_CODE_OBJECT_VERSION=default
    # -D AMDGPU_TARGETS="$(rocm-supported-gfx -e gfx906,gfx950,gfx1010,gfx1012,gfx1031,gfx1035,gfx1152,gfx1153)"
  )
  # -fcf-protection is not supported by HIP, see
  # https://rocm.docs.amd.com/projects/llvm-project/en/latest/reference/rocmcc.html#support-status-of-other-clang-options
  CXXFLAGS="${CXXFLAGS} -fcf-protection=none" cmake "${cmake_args[@]}"
  cmake --build build
}

package() {
  DESTDIR="$pkgdir" cmake --install build

  install -Dm644 "$_dirname/LICENSE.md" "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}
