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

pkgname=composable-kernel
pkgver=7.1.1
pkgrel=1
pkgdesc="High Performance Composable Kernel for AMD GPUs"
arch=('x86_64' 'aarch64')
url="https://github.com/ROCm/composable_kernel"
license=('MIT')
depends=('rocm-core' 'glibc' 'gcc-libs' 'hip-runtime-amd')
makedepends=('git' 'cmake' 'ninja' 'rocm-cmake' 'rocm-toolchain' 'rocm-llvm' 'openmp')
source=("$pkgname-$pkgver.tar.gz::$url/archive/rocm-$pkgver.tar.gz")
sha256sums=('e1174a4b6faa12ef31dac0324547fd49aca09fee380bd89ecd49a44bb34b72cc')
# Build without default flags as they cause the final linking step to fail
# https://github.com/ROCmSoftwarePlatform/composable_kernel/issues/736
options=(!lto !buildflags)
_dirname="$(basename "$url")-$(basename "${source[0]}" .tar.gz)"

prepare() {
    # Remove tests as they require a supported AMD GPU to run
    sed -i '/add_subdirectory(test)/d' "$_dirname/CMakeLists.txt"
}

build() {
  local _cmake_args=(
    -Wno-dev
    -G Ninja
    -B build
    -S "$_dirname"
    -D CMAKE_CXX_COMPILER=amdclang++
    -D CMAKE_BUILD_TYPE=Release
    -D CMAKE_INSTALL_PREFIX=/opt/rocm
    # CK doesn't support all targets
    # -D GPU_ARCHS="$(rocm-supported-gfx)"
    -D GPU_ARCHS="gfx908;gfx90a;gfx942;gfx1030;gfx1100;gfx1101;gfx1102;gfx1200;gfx1201"
    -D BUILD_DEV=OFF
    -D INSTANCES_ONLY=ON
  )
  cmake "${_cmake_args[@]}"
  cmake --build build
}

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

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