# Maintainer: Jakub Klinkovský <lahwaacz at archlinux dot org>

_name=ucc
pkgname=openucc
pkgver=1.2.0
pkgrel=1
pkgdesc="Unified Collective Communication Library"
arch=(x86_64 aarch64)
url="https://github.com/openucx/ucc"
license=(BSD-3-Clause)
depends=(
  glibc
  openucx
)
makedepends=(
  cuda
  doxygen
  hip-runtime-amd
  nccl
  rccl
  rdma-core
)
checkdepends=(
  gtest
)
optdepends=(
  'cuda: for CUDA execution engine and transport layer'
  'hip-runtime-amd: for HIP execution engine'
  'nccl: for NCCL transport layer'
  'rccl: for RCCL transport layer'
  'rdma-core: for InfiniBand transport layer'
)
provides=(
  libucc.so
)
source=(
  "$pkgname-$pkgver.tar.gz::$url/archive/refs/tags/v$pkgver.tar.gz"
  "$pkgname-fix-cuda-build.patch::https://patch-diff.githubusercontent.com/raw/openucx/ucc/pull/847.patch"
  "$pkgname-fix-rocm-build.patch::https://patch-diff.githubusercontent.com/raw/openucx/ucc/pull/858.patch"
  "aarch64.patch"
)
sha256sums=('c1552797600835c0cf401b82dc89c4d27d5717f4fb805d41daca8e19f65e509d'
            '9625f3afd63636a89ae13b79519cf1fb230f23db741f574230868106d98fd9fe'
            '91da7434152ce72d32d5cc03928d21a99b5af8568f62d5d3324a5cf6a0d9135d'
            'f499365e522473868b41479b86f020a2fdb5a63324feefbcd8ed00d516093431')

prepare() {
  cd $_name-$pkgver
  # backport CUDA fixes https://github.com/openucx/ucc/pull/847
  patch -Np1 < ../$pkgname-fix-cuda-build.patch
  # backport ROCm fixes https://github.com/openucx/ucc/pull/858
  patch -Np1 < ../$pkgname-fix-rocm-build.patch
  patch -Np1 < ../aarch64.patch
}

build() {
  local configure_options=(
    --prefix=/usr
    --with-ucx=/usr
    --with-cuda=/opt/cuda
    --with-rocm=/opt/rocm
    --enable-gtest
    CFLAGS="$CFLAGS"
    CXXFLAGS="$CXXFLAGS"
    LDFLAGS="$LDFLAGS"
    NVCC_CFLAGS="--threads 0"
  )

  cd $_name-$pkgver
  ./autogen.sh
  ./configure "${configure_options[@]}"
  # prevent excessive overlinking due to libtool
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
  make V=1
}

check() {
  # exclude test_mc_rocm.* - we cannot test ROCm without a GPU
  # exclude test_*reduce*:test_bcast_*:test_barrier.*:test_team.*:test_mc.*:test_active_set_2.*:test_scatter*:test_*gather*:*test_alltoall* - UCC ERROR no components supported memory type host available
  # exclude test_context.global: broken with ucx >= 1.17 (https://github.com/openucx/ucc/issues/1090)
  make gtest -C $_name-$pkgver GTEST_FILTER='-test_mc_rocm.*:test_*reduce*:test_bcast_*:test_barrier.*:test_team.*:test_mc.*:test_active_set_2.*:test_scatter*:test_*gather*:*test_alltoall*:test_context.global'
}

package() {
  make DESTDIR="$pkgdir" install -C $_name-$pkgver

  # install the license
  install -Dm644 $_name-$pkgver/LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}
