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

_name=ucx
pkgname=openucx
pkgver=1.19.0
pkgrel=3
pkgdesc="Communication framework for data-centric and high-performance applications"
arch=(x86_64 aarch64)
url="https://openucx.org/"
license=(BSD-3-Clause)
depends=(
  binutils
  gcc-libs
  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
  ucx-conf.patch
  aarch64.patch
)
b2sums=('6d7b2e17acb293ef52a4e576f8d3e4ffec2f44c53cb0a389a2fdcee88ea9dc3011b3c1d250fc2216affc50335ecb7fb8345042f478582021122256a7b8866bb4'
        '2b3b4575ce64b947ecc42c9055934bcd7e5feb30031c6504f8cc3057cfc138b28c2826a6cc55be56d186e09e20c6201dacfc308647d9b62a5d33688348d15a68'
        '17f57258e3410c650fea848f0f60ea069fc7b74e4058ba17ac74328ebb5669ee6df8fce8d2d79e21c106e027f18b2e257db68598b03deb0043cc6d74edfbb7dd'
)

prepare() {
  cd $_name-$pkgver

  # Do not hijack SIGHUP https://gitlab.archlinux.org/archlinux/packaging/packages/openucx/-/issues/3
  patch -Np1 -i ../ucx-conf.patch

  patch -Np1 -i ../aarch64.patch
}

build() {
  local configure_options=(
    --prefix=/usr
    --sysconfdir=/etc
    --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/"
}
