# Maintainer: Torsten Keßler <tpkessler at archlinux dot org>
pkgname=hiprand
pkgver=6.4.4
pkgrel=1
pkgdesc='rocRAND marshalling library'
arch=('x86_64' 'aarch64')
url='https://rocm.docs.amd.com/projects/hipRAND/en/latest/'
license=('MIT')
depends=('rocm-core' 'glibc' 'gcc-libs' 'hip-runtime-amd' 'rocrand')
makedepends=('cmake' 'rocm-cmake' 'gcc-fortran')
optdepends=('gcc-fortran: Use Fortran wrapper')
_git='https://github.com/ROCm/hipRAND'
source=("$pkgname-$pkgver.tar.gz::$_git/archive/rocm-$pkgver.tar.gz"
        "$pkgname-rocm-bin-path.patch")
sha256sums=('2437efbe8159628b6852883f441c61e7829fa3f00cb1f76811415ee083de54a5'
            '144fb5162222d81559e847d31226f7b56215fa558a549c8be534cfba3fbca241')
options=(!lto)
_dirname="$(basename "$_git")-$(basename "${source[0]}" ".tar.gz")"

prepare() {
  cd "$_dirname"
  patch -Np1 -i "$srcdir/$pkgname-rocm-bin-path.patch"
}

build() {
  # -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
  local cmake_args=(
    -Wno-dev
    -S "$_dirname"
    -B build
    -D CMAKE_BUILD_TYPE=None
    -D CMAKE_TOOLCHAIN_FILE=toolchain-linux.cmake
    -D CMAKE_CXX_COMPILER=/opt/rocm/bin/hipcc
    -D CMAKE_CXX_FLAGS="${CXXFLAGS} -fcf-protection=none"
    -D CMAKE_INSTALL_PREFIX=/opt/rocm
    -D BUILD_FORTRAN_WRAPPER=ON
  )
  cmake "${cmake_args[@]}"
  cmake --build build
}

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

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