Skip to content

Commit aaa01af

Browse files
authored
tests: add downstream examples (#363)
Adding some downstream examples, lightly tested in CI. --------- Signed-off-by: Henry Schreiner <[email protected]>
1 parent a145985 commit aaa01af

File tree

18 files changed

+438
-8
lines changed

18 files changed

+438
-8
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,5 @@ Thumbs.db
174174
/.distro/main.fmf
175175
/.distro/plans/main.fmf
176176
/.distro/tests/main.fmf
177+
178+
/docs/**/build
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
cmake_minimum_required(VERSION 3.15...3.26)
2+
3+
project(nanobind_example LANGUAGES CXX)
4+
5+
if(NOT SKBUILD)
6+
message(
7+
WARNING
8+
"\
9+
This CMake file is meant to be executed using 'scikit-build'. Running
10+
it directly will almost certainly not produce the desired result. If
11+
you are a user trying to install this package, please use the command
12+
below, which will install all necessary build dependencies, compile
13+
the package in an isolated environment, and then install it.
14+
=====================================================================
15+
$ pip install .
16+
=====================================================================
17+
If you are a software developer, and this is your own package, then
18+
it is usually much more efficient to install the build dependencies
19+
in your environment once and use the following command that avoids
20+
a costly creation of a new virtual environment at every compilation:
21+
=====================================================================
22+
$ pip install nanobind scikit-build-core[pyproject]
23+
$ pip install --no-build-isolation -ve .
24+
=====================================================================
25+
You may optionally add -Ceditable.rebuild=true to auto-rebuild when
26+
the package is imported. Otherwise, you need to re-run the above
27+
after editing C++ files.")
28+
endif()
29+
30+
# Try to import all Python components potentially needed by nanobind
31+
find_package(
32+
Python 3.8 REQUIRED
33+
COMPONENTS Interpreter Development.Module
34+
OPTIONAL_COMPONENTS Development.SABIModule)
35+
36+
# Import nanobind through CMake's find_package mechanism
37+
find_package(nanobind CONFIG REQUIRED)
38+
39+
# We are now ready to compile the actual extension module
40+
nanobind_add_module(
41+
# Name of the extension
42+
nanobind_example_ext
43+
# Target the stable ABI for Python 3.12+, which reduces the number of binary
44+
# wheels that must be built. This does nothing on older Python versions
45+
STABLE_ABI
46+
# Build libnanobind statically and merge it into the extension (which itself
47+
# remains a shared library)
48+
#
49+
# If your project builds multiple extensions, you can replace this flag by
50+
# NB_SHARED to conserve space by reusing a shared libnanobind across libraries
51+
NB_STATIC
52+
# Source code goes here
53+
src/nanobind_example_ext.cpp)
54+
55+
# Install directive for scikit-build-core
56+
install(TARGETS nanobind_example_ext LIBRARY DESTINATION nanobind_example)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Copyright (c) 2022 Wenzel Jakob <[email protected]>, All rights reserved.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are met:
5+
6+
1. Redistributions of source code must retain the above copyright notice, this
7+
list of conditions and the following disclaimer.
8+
9+
2. Redistributions in binary form must reproduce the above copyright notice,
10+
this list of conditions and the following disclaimer in the documentation
11+
and/or other materials provided with the distribution.
12+
13+
3. Neither the name of the copyright holder nor the names of its contributors
14+
may be used to endorse or promote products derived from this software
15+
without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# nanobind_example
2+
3+
| CI | status |
4+
| ---------- | ------------------------------------------------------------------- |
5+
| pip builds | [![Pip Action Status][actions-pip-badge]][actions-pip-link] |
6+
| wheels | [![Wheel Action Status][actions-wheels-badge]][actions-wheels-link] |
7+
8+
[actions-pip-link]:
9+
https://github.com/wjakob/nanobind_example/actions?query=workflow%3APip
10+
[actions-pip-badge]:
11+
https://github.com/wjakob/nanobind_example/workflows/Pip/badge.svg
12+
[actions-wheels-link]:
13+
https://github.com/wjakob/nanobind_example/actions?query=workflow%3AWheels
14+
[actions-wheels-badge]:
15+
https://github.com/wjakob/nanobind_example/workflows/Wheels/badge.svg
16+
17+
This repository contains a tiny project showing how to create C++ bindings using
18+
[nanobind](https://github.com/wjakob/nanobind) and
19+
[scikit-build-core](https://scikit-build-core.readthedocs.io/en/latest/index.html).
20+
It was derived from the corresponding _pybind11_
21+
[example project](https://github.com/pybind/scikit_build_example/) developed by
22+
[@henryiii](https://github.com/henryiii).
23+
24+
## Installation
25+
26+
1. Clone this repository
27+
2. Run `pip install ./nanobind_example`
28+
29+
Afterwards, you should be able to issue the following commands (shown in an
30+
interactive Python session):
31+
32+
```pycon
33+
>>> import nanobind_example
34+
>>> nanobind_example.add(1, 2)
35+
3
36+
```
37+
38+
## CI Examples
39+
40+
The `.github/workflows` directory contains two continuous integration workflows
41+
for GitHub Actions. The first one (`pip`) runs automatically after each commit
42+
and ensures that packages can be built successfully and that tests pass.
43+
44+
The `wheels` workflow uses
45+
[cibuildwheel](https://cibuildwheel.readthedocs.io/en/stable/) to automatically
46+
produce binary wheels for a large variety of platforms. If a `pypi_password`
47+
token is provided using GitHub Action's _secrets_ feature, this workflow can
48+
even automatically upload packages on PyPI.
49+
50+
## License
51+
52+
_nanobind_ and this example repository are both provided under a BSD-style
53+
license that can be found in the [LICENSE](./LICENSE) file. By using,
54+
distributing, or contributing to this project, you agree to the terms and
55+
conditions of this license.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
[build-system]
2+
requires = ["scikit-build-core >=0.4.3", "nanobind >=1.3.2"]
3+
build-backend = "scikit_build_core.build"
4+
5+
[project]
6+
name = "nanobind-example"
7+
version = "0.0.1"
8+
description = "An example minimal project that compiles bindings using nanobind and scikit-build"
9+
readme = "README.md"
10+
requires-python = ">=3.8"
11+
authors = [
12+
{ name = "Wenzel Jakob", email = "[email protected]" },
13+
]
14+
classifiers = [
15+
"License :: BSD",
16+
]
17+
18+
[project.urls]
19+
Homepage = "https://github.com/wjakob/nanobind_example"
20+
21+
22+
[tool.scikit-build]
23+
# Protect the configuration against future changes in scikit-build-core
24+
minimum-version = "0.4"
25+
26+
# Setuptools-style build caching in a local directory
27+
build-dir = "build/{wheel_tag}"
28+
29+
# Build stable ABI wheels for CPython 3.12+
30+
wheel.py-api = "cp312"
31+
32+
33+
[tool.cibuildwheel]
34+
# Necessary to see build output from the actual compilation
35+
build-verbosity = 1
36+
37+
# Run pytest to ensure that the package was correctly built
38+
test-command = "pytest {project}/tests"
39+
test-requires = "pytest"
40+
41+
# Needed for full C++17 support
42+
[tool.cibuildwheel.macos.environment]
43+
MACOSX_DEPLOYMENT_TARGET = "10.14"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .nanobind_example_ext import add
2+
3+
__all__ = ["add"]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <nanobind/nanobind.h>
2+
3+
namespace nb = nanobind;
4+
5+
using namespace nb::literals;
6+
7+
NB_MODULE(nanobind_example_ext, m) {
8+
m.def("add", [](int a, int b) { return a + b; }, "a"_a, "b"_a);
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import nanobind_example as m
2+
3+
4+
def test_add():
5+
assert m.add(1, 2) == 3
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cmake_minimum_required(VERSION 3.15...3.26)
2+
3+
project(
4+
${SKBUILD_PROJECT_NAME}
5+
VERSION ${SKBUILD_PROJECT_VERSION}
6+
LANGUAGES CXX)
7+
8+
find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
9+
find_package(pybind11 CONFIG REQUIRED)
10+
11+
python_add_library(_core MODULE src/main.cpp WITH_SOABI)
12+
target_link_libraries(_core PRIVATE pybind11::headers)
13+
target_compile_definitions(_core PRIVATE VERSION_INFO=${PROJECT_VERSION})
14+
15+
install(TARGETS _core DESTINATION scikit_build_example)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Copyright (c) 2016 The Pybind Development Team, All rights reserved.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are met:
5+
6+
1. Redistributions of source code must retain the above copyright notice, this
7+
list of conditions and the following disclaimer.
8+
9+
2. Redistributions in binary form must reproduce the above copyright notice,
10+
this list of conditions and the following disclaimer in the documentation
11+
and/or other materials provided with the distribution.
12+
13+
3. Neither the name of the copyright holder nor the names of its contributors
14+
may be used to endorse or promote products derived from this software
15+
without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
28+
You are under no obligation whatsoever to provide any bug fixes, patches, or
29+
upgrades to the features, functionality or performance of the source code
30+
("Enhancements") to anyone; however, if you choose to make your Enhancements
31+
available either publicly, or directly to the author of this software, without
32+
imposing a separate written license agreement for such Enhancements, then you
33+
hereby grant the following license: a non-exclusive, royalty-free perpetual
34+
license to install, use, modify, prepare derivative works, incorporate into
35+
other computer software, distribute, and sublicense such enhancements or
36+
derivative works thereof, in binary and source code form.

0 commit comments

Comments
 (0)