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

_name=ucx
pkgname=openucx
pkgver=1.17.0
pkgrel=1
pkgdesc="Communication framework for data-centric and high-performance applications"
arch=(x86_64 aarch64)
url="http://www.openucx.org"
license=(BSD-3-Clause)
depends=(
  binutils
  glibc
  zlib
  zstd
)
makedepends=(
  rdma-core
  cuda
  rocm-language-runtime
)
optdepends=(
  'rdma-core: for InfiniBand and RDMA support'
  'cuda: for CUDA support'
  'rocm-language-runtime: for ROCm support'
)
provides=(
  libucm.so
  libucp.so
  libucs.so
  libucs_signal.so
  libuct.so
)
source=("$pkgname-$pkgver.tar.gz::https://github.com/openucx/$_name/archive/refs/tags/v$pkgver.tar.gz"
        "aarch64.patch")
sha256sums=('36db6b00b0939d746e86f9e0d32dc445faaa109e46dc643fb5ad779492abfaef'
            '9f3dbd35673f6eb8cf5eb0723b70fbb43fcf0c4ae265e4381fa22f9461a35dba')

prepare() {
  cd $_name-$pkgver
  patch -Np1 < ../aarch64.patch
}

build() {
  local configure_options=(
    --prefix=/usr
    --with-cuda=/opt/cuda
    --with-rocm=/opt/rocm
    --with-verbs
    --with-rc
    --with-ud
    --with-dc
    --with-mlx5-dv
    --enable-mt
  )

  # this uses malloc_usable_size, which is incompatible with fortification level 3
  export CFLAGS="${CFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"
  export CXXFLAGS="${CXXFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"

  cd $_name-$pkgver
  ./autogen.sh
  # NOTE: upstream suggests running ./contrib/configure-release instead of
  # ./configure for maximal performance, but it contains options that would
  # make debugging much harder
  ./configure "${configure_options[@]}"
  # prevent excessive overlinking due to libtool
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
  make V=1
}

package() {
  # this uses malloc_usable_size, which is incompatible with fortification level 3
  export CFLAGS="${CFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"
  export CXXFLAGS="${CXXFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"

  make DESTDIR="$pkgdir" install -C $_name-$pkgver

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