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

pkgname=nanobind
pkgdesc="Tiny and efficient C++/Python bindings"
pkgver=2.12.0
pkgrel=3
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"
  nanobind-fix-cmake-config-path.patch::https://patch-diff.githubusercontent.com/raw/wjakob/nanobind/pull/1312.patch
)
b2sums=('2969a55e912aed4026b44a72c06846d1f1635bfdf45dc9a10bc4137799492c0ef5c9f7c6186e30d6bdf415afac12e801cbb66e899b42dfe080369a70e5455de6'
        '684f4d37d4e5b471152e0ae4ad8bb0877e5fa74a9b8dcfe2c367295560f5848a6f6b8c45a9488e39435213277243f6b9a8a542ad68aa7eaf969c666b0d2d7dfc')

prepare() {
  cd $pkgname
  patch -Np1 -i "$srcdir"/nanobind-fix-cmake-config-path.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
}
