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

_name=ucx
pkgname=openucx
pkgver=1.20.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
  glibc
  libgcc
  libgomp
  libstdc++
  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/$pkgver.tar.gz
  ucx-conf.patch
  c-gnu23.patch
)
b2sums=('c0f28a2a62801be866fa98775ec4a1b46f97d1e6bafea462306265d620541c7f84c8275419337102be2444c4ff2343e354b99aabc1e242c6ef75d76872d3d59c'
        '2b3b4575ce64b947ecc42c9055934bcd7e5feb30031c6504f8cc3057cfc138b28c2826a6cc55be56d186e09e20c6201dacfc308647d9b62a5d33688348d15a68'
        '3af500ee1a51ff9d2d1a2ae7896d87da5f274d54251ad816ca5143b220a9c8a7f1bea5236a7f826943187686fe9130fecd5f9271889d51a59cc80f0c2665ede6')

prepare() {
  cd $_name-$pkgver

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

  # Fix building with -std=gnu23 https://github.com/openucx/ucx/issues/11185
  patch -p1 -i ../c-gnu23.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}"

  export CFLAGS="${CFLAGS} -Wno-error=discarded-qualifiers"

  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/"
}
