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

pkgname=nanobind
pkgdesc="Tiny and efficient C++/Python bindings"
pkgver=2.11.0
pkgrel=1
arch=(any)
url="https://github.com/wjakob/nanobind"
license=(BSD-3-Clause)
depends=(python)
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"
  "git+https://github.com/Tessil/robin-map.git"
)
b2sums=('02f19efc8fe0a673de68b49de1576f4c3199067d0cc70bf734f27bf374e59c73691dd663ce6f876921cb534c04f69dbc4184948e4c8a0c12b46137c5ee98bedb'
        'SKIP')

prepare() {
  cd $pkgname
  git submodule init
  git config submodule.libs/ext/robin_map.url ../robin_map
  git -c protocol.file.allow=always submodule update
}

build() {
  cd $pkgname
  python -m build --wheel --no-isolation
  cmake -S . -B build \
    -DCMAKE_BUILD_TYPE=None \
    -DNB_TEST_STABLE_ABI=ON \
    -DNB_TEST_SHARED_BUILD=ON \
    -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
}
