# Maintainer: Jakub Klinkovský <lahwaacz at archlinux dot org>
# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Maintainer: David Runge <dvzrv@archlinux.org>
# Contributor: Anatol Pomozov <anatol dot pomozov at gmail>
# Contributor: Stéphane Gaudreault <stephane@archlinux.org>

pkgbase=openmpi
pkgname=(
  openmpi
  openmpi-docs
)
pkgver=5.0.9
pkgrel=2
pkgdesc='High performance message passing library (MPI)'
arch=(x86_64 aarch64)
url='https://www.open-mpi.org'
license=('BSD-3-Clause AND LicenseRef-MPICH')
makedepends=(
  cuda
  nvidia-utils  # for libcuda.so
  gcc-fortran
  gcc-libs
  glibc
  hip-runtime-amd
  hwloc
  libevent
  libfabric
  libnl
  openpmix
  openucc
  openucx
  prrte
  valgrind
  zlib
)
source=(
  https://www.open-mpi.org/software/ompi/v${pkgver%.*}/downloads/$pkgbase-$pkgver.tar.bz2
)
sha256sums=('dfb72762531170847af3e4a0f21d77d7b23cf36f67ce7ce9033659273677d80b')
b2sums=('f6c822f9bcbf5cbdb508cf70a6fcb717630157c6753bda1f01dc378617971744600b46052baa5a89e5964240f03c241cd41f7b119ce7e084704cd3a71eb8637e')

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

prepare() {
  cd $pkgbase-$pkgver
  # workaround for https://github.com/open-mpi/ompi/issues/12257
  sed -i 's|WRAPPER__FCFLAGS|WRAPPER_FCFLAGS|g' configure
  sed -i 's|WRAPPER_EXTRA_FCFLAGS|WRAPPER_FCFLAGS|g' configure
  sed -i 's|"-I/usr/include",||' opal/tools/wrappers/opal_wrapper.c
}

build() {
  local configure_options=(
    --prefix=/usr
    --enable-builtin-atomics
    --enable-memchecker
    --enable-mpi-fortran=all
    --enable-pretty-print-stacktrace
    --libdir=/usr/lib
    --sysconfdir=/etc/$pkgbase
    --with-hwloc=external
    --with-libevent=external
    --with-pmix=external
    --with-prrte=external
    --with-valgrind
    --with-ucc=/usr
    --with-ucx=/usr
    --with-cuda=/opt/cuda
    # this tricks the configure script to look for /usr/lib/pkgconfig/cuda.pc
    # instead of /opt/cuda/lib/pkgconfig/cuda.pc
    --with-cuda-libdir=/usr/lib
    --with-rocm=/opt/rocm
    # all components that link to libraries provided by optdepends must be run-time loadable
    --enable-mca-dso=accelerator_cuda,accelerator_rocm,btl_smcuda,rcache_gpusm,rcache_rgpusm,coll_ucc,scoll_ucc
    # mpirun should not warn on MCA component load failures by default - usually caused by missing optdepends, which is ok
    # https://docs.open-mpi.org/en/main/installing-open-mpi/configure-cli-options/installation.html
    --with-show-load-errors='^accelerator,rcache,coll/ucc'
  )
  cd $pkgbase-$pkgver

  # set environment variables for reproducible build
  # see https://github.com/open-mpi/ompi/blob/main/docs/release-notes/general.rst
  export HOSTNAME=buildhost
  export USER=builduser

  ./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() {
  make check -C $pkgbase-$pkgver
}

package_openmpi() {
  depends=(
    gcc-libs
    glibc
    hwloc
    libevent libevent_{core,pthreads}-2.1.so
    libfabric
    libnl
    openpmix libpmix.so
    openucx
    prrte
    zlib
  )
  optdepends=(
    'cuda: cuda support'
    'hip-runtime-amd: ROCm support'
    'gcc-fortran: fortran support'
    'openssh: for execution on remote hosts via plm_ssh_agent'
    'openucc: for UCC accelerated collectives'
  )
  provides=(
    libmpi.so
    libmpi_mpifh.so
    libmpi_usempi_ignore_tkr.so
    libmpi_usempif08.so
    libopen-pal.so
    liboshmem.so
  )
  backup=(etc/$pkgname/$pkgname-mca-params.conf)

  make DESTDIR="$pkgdir" install -C $pkgbase-$pkgver
  (
    cd "$pkgdir"
    _pick $pkgbase-docs usr/share/doc
  )
  install -Dm 644 $pkgbase-$pkgver/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
}

package_openmpi-docs() {
  pkgdesc+=" - documentation"

  mv -v $pkgname/* "$pkgdir"
  install -vDm 644 $pkgbase-$pkgver/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
}

# vim: ts=2 sw=2 et:
