# Maintainer: Torsten Keßler <tpkessler at archlinux dot org>

pkgname=composable-kernel
pkgver=6.4.4
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=('b431232492d3381924221af11ce6ed3afb43a790f34d0e1fe454111b0e597cc8')
# 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() {
    # CK doesn't support all targets
  local _gpu_list="gfx908;gfx90a;gfx942;gfx1030;gfx1100;gfx1101;gfx1102;gfx1200;gfx1201"
  if [ "$CARCH" = "aarch64" ]; then
    # Taken from fedora EPEL 7.1.1-5.el10_2 package. The comment with it is:
    # ```This package takes a very long time to build, build only the most useful.
    # ck is needed for hipTensor but hipTensor only supports gfx908,gfx90a,gfx942,gfx950```
    # Added gfx90a as it should create composable_kerneldevice_mha_operationsTargets.cmake
    # required by miopen-hip
    # See https://github.com/ROCm/rocm-libraries/issues/4225
    _gpu_list="gfx90a;gfx11-generic;gfx12-generic"
  fi
  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
    # -D GPU_ARCHS="$(rocm-supported-gfx)"
    -D GPU_ARCHS="${_gpu_list}"
    -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"
}
