# Maintainer: Torsten Keßler <tpkessler at archlinux dot org>
# Contributor: acxz <akashpatel2008 at yahoo dot com>
pkgname=hip-runtime-amd
pkgver=6.0.2
pkgrel=2
pkgdesc="Heterogeneous Interface for Portability ROCm"
arch=('x86_64' 'aarch64')
url='https://rocm.docs.amd.com/projects/HIP/en/latest/'
license=('MIT')
depends=('rocm-core' 'bash' 'perl' 'glibc' 'gcc-libs' 'numactl'
         'mesa' 'comgr' 'rocminfo' 'rocm-llvm' 'libelf')
makedepends=('cmake' 'python' 'python-cppheaderparser')
optdepends=('inetutils: Print hostname in hipconfig')
provides=('hip')
conflicts=('hip')
# Common HIP dir (AMD or nVidia)
_hip='https://github.com/ROCm/HIP'
# ROCclr, HIPAMD and OpenCl repositories
_clr='https://github.com/ROCm/clr'
# Binary hipcc compiler (previously a perl script)
_hipcc='https://github.com/ROCm/HIPCC'
source=("$pkgname-$pkgver.tar.gz::$_hip/archive/rocm-$pkgver.tar.gz"
        "$pkgname-clr-$pkgver.tar.gz::$_clr/archive/rocm-$pkgver.tar.gz"
        "$pkgname-hipcc-$pkgver.tar.gz::$_hipcc/archive/rocm-$pkgver.tar.gz"
        aarch64-port.patch)
sha256sums=('84163ffb5d81f192f4a879f3f9722db2402d72c2a90f104c5b2b8a4212f4f9b0'
            'cb8ac610c8d4041b74fb3129c084f1e7b817ce1a5a9943feca1fa7531dc7bdcc'
            'd6209b14fccdd00d7231dec4b4f962aa23914b9dde389ba961370e8ba918bde5'
            'SKIP')
_dirhip="$(basename "$_hip")-$(basename "${source[0]}" ".tar.gz")"
# Upstream appear to have accidentally lowercased the directory
# in an update to the tarball
_dirhip="${_dirhip,,}"
_dirclr="$(basename "$_clr")-$(basename "${source[1]}" ".tar.gz")"
_dirhipcc="$(basename "$_hipcc")-$(basename "${source[2]}" ".tar.gz")"

prepare() {
  cd clr-rocm-$pkgver
  sed -i "s#x86_64-unknown-linux-gnu#$CHOST#" hipamd/src/hip_embed_pch.sh
  patch -Np1 < ../aarch64-port.patch
}

build() {
  # Enabled assertions can cause crashes if applications are run with increased
  # verbosity, see for instance this issue with blender,
  # https://gitlab.archlinux.org/archlinux/packaging/packages/blender/-/issues/13
  # The relevant upstream issue is
  # https://github.com/ROCm/HIP/issues/2426
  CXXFLAGS=${CXXFLAGS/-Wp,-D_GLIBCXX_ASSERTIONS/}
  [ $CARCH == aarch64 ] && CFLAGS="$CFLAGS -march=armv8-a"
  [ $CARCH == aarch64 ] && CXXFLAGS="$CXXFLAGS -march=armv8-a"
  local hipcc_args=(
    -Wno-dev
    -S "$srcdir/$_dirhipcc"
    -B build-hipcc
    -DCMAKE_BUILD_TYPE=None
  )
  cmake "${hipcc_args[@]}"
  cmake --build build-hipcc

  local hip_args=(
    -Wno-dev
    -S "$srcdir/$_dirclr"
    -B build
    -DCMAKE_BUILD_TYPE=None
    -DCMAKE_INSTALL_PREFIX=/opt/rocm/
    -DHIP_COMMON_DIR="$srcdir/$_dirhip"
    -DHIPCC_BIN_DIR="$srcdir/build-hipcc"
    -DHIP_CATCH_TEST=0
    -DCLR_BUILD_HIP=ON
    -DCLR_BUILD_OCL=OFF
  )
  cmake "${hip_args[@]}"
  cmake --build build
}

package() {
  DESTDIR="$pkgdir" cmake --install build
  # Binary hipcc and hipconfig currently don't work, so we don't install them:
  # https://github.com/ROCm-Developer-Tools/HIPCC/issues/103
  #
  # There's no install target for hipcc and hipconfig, so copy them manually
  # install "$srcdir/build-hipcc/hipcc.bin" "$pkgdir/opt/rocm/bin/hipcc.bin"
  # install "$srcdir/build-hipcc/hipconfig.bin" "$pkgdir/opt/rocm/bin/hipconfig.bin"

  # Remove Windows scripts
  rm "$pkgdir/opt/rocm/bin/hipcc.bat"
  rm "$pkgdir/opt/rocm/bin/hipconfig.bat"

  install -Dm644 "$srcdir/$_dirhip/LICENSE.txt" "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
  echo '/opt/rocm/hip/lib' > "$pkgname.conf"
  install -Dm644 "$pkgname.conf" "$pkgdir/etc/ld.so.conf.d/$pkgname.conf"
}
