# Maintainer: Bruno Pagani <archange@archlinux.org>
# Maintainer: Jakub Klinkovský <lahwaacz at archlinux dot org>

pkgname=adios2
pkgver=2.10.2
pkgrel=12
pkgdesc="The Adaptable Input/Output System version 2"
arch=(x86_64 aarch64)
url="https://adios2.readthedocs.io/en/latest/"
license=(Apache-2.0)
depends=(
  blosc2
  bzip2 libbz2.so
  gcc-libs
  glibc
  hdf5
  libfabric
  libpng libpng16.so
  libsodium libsodium.so
  mgard
  openmpi libmpi.so
  openucx libucp.so libucs.so
  paraview-catalyst
  pugixml
  python
  python-yaml
  sqlite libsqlite3.so
  sz
  yaml-cpp libyaml-cpp.so
  zeromq libzmq.so
  zfp
  zlib libz.so
)
makedepends=(
  cmake
  cuda
  gcc-fortran
  gtest
  ninja
  nlohmann-json
  pybind11
  python-mpi4py
  python-numpy
)
optdepends=(
  'python-numpy: for Python bindings'
)
source=(
  https://github.com/ornladios/ADIOS2/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz
  fix-destdir-installation.patch
  fix-cuda-std-17.patch
)
sha256sums=('14cf0bcd94772194bce0f2c0e74dba187965d1cffd12d45f801c32929158579e'
            '601dccc582f957b19bbe5e980ed37fcaeec85b66f63fbb357735345ac0a16d60'
            '6f7804a60ad2f40adceaf291aff610a641e440ff69b88606452062b21653b5aa')

prepare() {
  # Fix DESTDIR installation https://github.com/ornladios/ADIOS2/pull/4402
  patch --directory=${pkgname^^}-${pkgver} -Np1 < fix-destdir-installation.patch

  # CUB requires at least C++17 https://github.com/ornladios/ADIOS2/issues/4620
  patch --directory=${pkgname^^}-${pkgver} -Np1 < fix-cuda-std-17.patch
}

build() {
  # In general, we want to list all real archs (sm_XX) and the latest virtual arch (compute_XX) for future PTX compatibility.
  # Valid values can be discovered from nvcc --help
  local cuda_archs="75;80;86;87;88;89;90;100;103;110;120;121;121-virtual"

  local cmake_options=(
    -B build
    -S ${pkgname^^}-${pkgver}
    -G Ninja
    -W no-dev
    -DCMAKE_BUILD_TYPE=None
    -DCMAKE_INSTALL_PREFIX=/usr
    -DCMAKE_CUDA_ARCHITECTURES="$cuda_archs"
    # Compile source code for supported GPU archs in parallel
    -DCMAKE_CUDA_FLAGS="--threads $(nproc)"
    -DADIOS2_USE_CUDA=ON
    -DADIOS2_USE_EXTERNAL_DEPENDENCIES=ON
    #-DADIOS2_HAVE_HDF5_VOL needs hdf5-openmpi
    -DADIOS2_HAVE_HDF5_VOL=OFF
    -DADIOS2_BUILD_EXAMPLES=OFF
    -DADIOS2_USE_Derived_Variables=ON
  )
  cmake "${cmake_options[@]}"
  cmake --build build
}

package() {
  CMAKE_PREFIX_PATH="$pkgdir"/usr/lib/cmake \
  DESTDIR="$pkgdir" cmake --install build
}
