# Maintainer: Carl Smedstad <carsme@archlinux.org>
# Contributor: Carlos Aznarán <caznaranl@uni.pe>

pkgname=nanobind
pkgdesc="Tiny and efficient C++/Python bindings"
pkgver=2.13.0
pkgrel=1
arch=(any)
url="https://github.com/wjakob/nanobind"
license=(BSD-3-Clause)
depends=(
  python
  robin-map
)
makedepends=(
  cmake
  eigen
  git
  python-build
  python-installer
  python-scikit-build-core
  python-wheel
)
checkdepends=(
  python-pytest
  python-pytorch
  python-scipy
  python-tensorflow
  python-tests
)
source=(
  "git+$url.git#tag=v$pkgver"
  2965ecd22f7ea845a2de56b786ae331ccc85ef3d.patch
)
b2sums=('7fc2fd2577fa67cc621796c033bde9f41119659a7bf9b8908c370624089545b7c2e311e0c9e57d45ae2b8aaff80600402960236a193c3f5a0a9724c4dda6567c'
        'SKIP')

prepare() {
  cd $pkgname
  # workaround for https://github.com/wjakob/nanobind/issues/1387
  # git cherry-pick -n 2965ecd22f7ea845a2de56b786ae331ccc85ef3d
  patch -Np1 -i "${srcdir}/2965ecd22f7ea845a2de56b786ae331ccc85ef3d.patch"
}

build() {
  cd $pkgname
  SKBUILD_CMAKE_ARGS="-DCMAKE_BUILD_TYPE=None;-DNB_USE_SUBMODULE_DEPS=OFF" \
  python -m build --wheel --no-isolation
  cmake -S . -B build \
    -DCMAKE_BUILD_TYPE=None \
    -DNB_TEST_STABLE_ABI=ON \
    -DNB_TEST_SHARED_BUILD=ON \
    -DNB_USE_SUBMODULE_DEPS=OFF \
    -Wno-dev
  cmake --build build
}

check() {
  cd $pkgname
  # one test requires running pytest from the build directory
  cd build
  python -m pytest
}

package() {
  cd $pkgname
  python -m installer --destdir="$pkgdir" dist/*.whl

  local python_version=$(python -c "import sys; print(sys.version[:4])")
  install -vdm755 "$pkgdir/usr/include"
  ln -vs "../lib/python$python_version/site-packages/nanobind/include/nanobind" \
    "$pkgdir/usr/include/nanobind"
  install -dm755 "$pkgdir/usr/lib/cmake"
  ln -vs "../../lib/python$python_version/site-packages/nanobind/cmake" \
    "$pkgdir/usr/lib/cmake/nanobind"

  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgname" LICENSE
}
