Skip to content

Commit

Permalink
add nanobind example
Browse files Browse the repository at this point in the history
  • Loading branch information
wistaria committed Sep 19, 2023
1 parent dd5959c commit 561b6eb
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
*/build
build
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ include(cmake/prefix.cmake)
project(pycxx CXX)
include(cmake/postfix.cmake)

# set(FETCHCONTENT_UPDATES_DISCONNECTED ON)
if(POLICY CMP0144)
cmake_policy(SET CMP0144 NEW)
endif()
set(FETCHCONTENT_UPDATES_DISCONNECTED ON)

find_package(Python COMPONENTS Interpreter Development REQUIRED)
find_package(Boost COMPONENTS python)

include(pybind11)
include(nanobind)
FetchContent_MakeAvailable(${FetchContents})

if(Boost_FOUND)
add_subdirectory(boost)
endif(Boost_FOUND)
add_subdirectory(pybind11)
add_subdirectory(nanobind)
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* boost: example for Boost.Python
* pybind11: example for pybind11
* nanobind: example for nanobind

## Boost.Python

Expand All @@ -13,15 +14,17 @@ It is assumed that the Boost.Python has been compiled and installed into some pl

Pybind11 is a header-only library. It is automatically downloaded.

## Nanobind

Nanobind11 is automatically downloaded.

## How to build samples and run tests

```bash
cd /somewhere/pycxx
mkdir build
cd build
cmake ..
make
ctests
cmake -B build
cmake --build build
cmake --build build --target test
```

## Options to cmake
Expand Down
7 changes: 7 additions & 0 deletions cmake/nanobind.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include(FetchContent)
FetchContent_Declare(
nanobind
GIT_REPOSITORY https://github.com/wjakob/nanobind.git
GIT_TAG b0e24d5b0ab0d518317d6b263a257ae72d4d29a2 # v1.5.2
)
list(APPEND FetchContents nanobind)
2 changes: 1 addition & 1 deletion cmake/pybind11.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ include(FetchContent)
FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11.git
GIT_TAG 8a099e44b3d5f85b20f05828d919d2332a8de841 # 2.11.1
GIT_TAG 8a099e44b3d5f85b20f05828d919d2332a8de841 # v2.11.1
)
list(APPEND FetchContents pybind11)
9 changes: 9 additions & 0 deletions nanobind/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
nanobind_add_module(nanobind_cxxmod add.hpp cxxmod.cpp)
set_target_properties(nanobind_cxxmod PROPERTIES OUTPUT_NAME cxxmod CXX_STANDARD 17)

configure_file(pytest.py.in pytest.py @ONLY)
add_test(NAME nanobind_pytest COMMAND ${Python_EXECUTABLE} pytest.py)

add_executable(nanobind_cxxtest cxxtest.cpp)
set_target_properties(nanobind_cxxtest PROPERTIES OUTPUT_NAME cxxtest)
add_test(NAME nanobind_cxxtest COMMAND cxxtest)
8 changes: 8 additions & 0 deletions nanobind/add.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef ADD_HPP
#define ADD_HPP

inline int add(int i, int j) {
return i + j;
}

#endif // ADD_HPP
7 changes: 7 additions & 0 deletions nanobind/cxxmod.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <nanobind/nanobind.h>
#include "add.hpp"

NB_MODULE(cxxmod, m) {
m.doc() = "nanobind example plugin";
m.def("add", &add, "Add two numbers");
}
6 changes: 6 additions & 0 deletions nanobind/cxxtest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <iostream>
#include "add.hpp"

int main() {
std::cout << "add(1,2) = " << add(1,2) << std::endl;
}
3 changes: 3 additions & 0 deletions nanobind/pytest.py.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!@Python_EXECUTABLE@
import cxxmod
print('add(1,2) = {}'.format(cxxmod.add(1,2)))
2 changes: 1 addition & 1 deletion pybind11/pytest.py.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!@PYTHON_EXECUTABLE@
#!@Python_EXECUTABLE@
import cxxmod
print('add(1,2) = {}'.format(cxxmod.add(1,2)))

0 comments on commit 561b6eb

Please sign in to comment.