Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: add downstream examples #363

Merged
merged 3 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,5 @@ Thumbs.db
/.distro/main.fmf
/.distro/plans/main.fmf
/.distro/tests/main.fmf

/docs/**/build
56 changes: 56 additions & 0 deletions docs/examples/downstream/nanobind_example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
cmake_minimum_required(VERSION 3.15...3.26)

project(nanobind_example LANGUAGES CXX)

if(NOT SKBUILD)
message(
WARNING
"\
This CMake file is meant to be executed using 'scikit-build'. Running
it directly will almost certainly not produce the desired result. If
you are a user trying to install this package, please use the command
below, which will install all necessary build dependencies, compile
the package in an isolated environment, and then install it.
=====================================================================
$ pip install .
=====================================================================
If you are a software developer, and this is your own package, then
it is usually much more efficient to install the build dependencies
in your environment once and use the following command that avoids
a costly creation of a new virtual environment at every compilation:
=====================================================================
$ pip install nanobind scikit-build-core[pyproject]
$ pip install --no-build-isolation -ve .
=====================================================================
You may optionally add -Ceditable.rebuild=true to auto-rebuild when
the package is imported. Otherwise, you need to re-run the above
after editing C++ files.")
endif()

# Try to import all Python components potentially needed by nanobind
find_package(
Python 3.8 REQUIRED
COMPONENTS Interpreter Development.Module
OPTIONAL_COMPONENTS Development.SABIModule)

# Import nanobind through CMake's find_package mechanism
find_package(nanobind CONFIG REQUIRED)

# We are now ready to compile the actual extension module
nanobind_add_module(
# Name of the extension
nanobind_example_ext
# Target the stable ABI for Python 3.12+, which reduces the number of binary
# wheels that must be built. This does nothing on older Python versions
STABLE_ABI
# Build libnanobind statically and merge it into the extension (which itself
# remains a shared library)
#
# If your project builds multiple extensions, you can replace this flag by
# NB_SHARED to conserve space by reusing a shared libnanobind across libraries
NB_STATIC
# Source code goes here
src/nanobind_example_ext.cpp)

# Install directive for scikit-build-core
install(TARGETS nanobind_example_ext LIBRARY DESTINATION nanobind_example)
26 changes: 26 additions & 0 deletions docs/examples/downstream/nanobind_example/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright (c) 2022 Wenzel Jakob <[email protected]>, All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 changes: 55 additions & 0 deletions docs/examples/downstream/nanobind_example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# nanobind_example

| CI | status |
| ---------- | ------------------------------------------------------------------- |
| pip builds | [![Pip Action Status][actions-pip-badge]][actions-pip-link] |
| wheels | [![Wheel Action Status][actions-wheels-badge]][actions-wheels-link] |

[actions-pip-link]:
https://github.com/wjakob/nanobind_example/actions?query=workflow%3APip
[actions-pip-badge]:
https://github.com/wjakob/nanobind_example/workflows/Pip/badge.svg
[actions-wheels-link]:
https://github.com/wjakob/nanobind_example/actions?query=workflow%3AWheels
[actions-wheels-badge]:
https://github.com/wjakob/nanobind_example/workflows/Wheels/badge.svg

This repository contains a tiny project showing how to create C++ bindings using
[nanobind](https://github.com/wjakob/nanobind) and
[scikit-build-core](https://scikit-build-core.readthedocs.io/en/latest/index.html).
It was derived from the corresponding _pybind11_
[example project](https://github.com/pybind/scikit_build_example/) developed by
[@henryiii](https://github.com/henryiii).

## Installation

1. Clone this repository
2. Run `pip install ./nanobind_example`

Afterwards, you should be able to issue the following commands (shown in an
interactive Python session):

```pycon
>>> import nanobind_example
>>> nanobind_example.add(1, 2)
3
```

## CI Examples

The `.github/workflows` directory contains two continuous integration workflows
for GitHub Actions. The first one (`pip`) runs automatically after each commit
and ensures that packages can be built successfully and that tests pass.

The `wheels` workflow uses
[cibuildwheel](https://cibuildwheel.readthedocs.io/en/stable/) to automatically
produce binary wheels for a large variety of platforms. If a `pypi_password`
token is provided using GitHub Action's _secrets_ feature, this workflow can
even automatically upload packages on PyPI.

## License

_nanobind_ and this example repository are both provided under a BSD-style
license that can be found in the [LICENSE](./LICENSE) file. By using,
distributing, or contributing to this project, you agree to the terms and
conditions of this license.
43 changes: 43 additions & 0 deletions docs/examples/downstream/nanobind_example/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[build-system]
requires = ["scikit-build-core >=0.4.3", "nanobind >=1.3.2"]
build-backend = "scikit_build_core.build"

[project]
name = "nanobind-example"
version = "0.0.1"
description = "An example minimal project that compiles bindings using nanobind and scikit-build"
readme = "README.md"
requires-python = ">=3.8"
authors = [
{ name = "Wenzel Jakob", email = "[email protected]" },
]
classifiers = [
"License :: BSD",
]

[project.urls]
Homepage = "https://github.com/wjakob/nanobind_example"


[tool.scikit-build]
# Protect the configuration against future changes in scikit-build-core
minimum-version = "0.4"

# Setuptools-style build caching in a local directory
build-dir = "build/{wheel_tag}"

# Build stable ABI wheels for CPython 3.12+
wheel.py-api = "cp312"


[tool.cibuildwheel]
# Necessary to see build output from the actual compilation
build-verbosity = 1

# Run pytest to ensure that the package was correctly built
test-command = "pytest {project}/tests"
test-requires = "pytest"

# Needed for full C++17 support
[tool.cibuildwheel.macos.environment]
MACOSX_DEPLOYMENT_TARGET = "10.14"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .nanobind_example_ext import add

__all__ = ["add"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <nanobind/nanobind.h>

namespace nb = nanobind;

using namespace nb::literals;

NB_MODULE(nanobind_example_ext, m) {
m.def("add", [](int a, int b) { return a + b; }, "a"_a, "b"_a);
}
5 changes: 5 additions & 0 deletions docs/examples/downstream/nanobind_example/tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import nanobind_example as m


def test_add():
assert m.add(1, 2) == 3
15 changes: 15 additions & 0 deletions docs/examples/downstream/pybind11_example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.15...3.26)

project(
${SKBUILD_PROJECT_NAME}
VERSION ${SKBUILD_PROJECT_VERSION}
LANGUAGES CXX)

find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
find_package(pybind11 CONFIG REQUIRED)

python_add_library(_core MODULE src/main.cpp WITH_SOABI)
target_link_libraries(_core PRIVATE pybind11::headers)
target_compile_definitions(_core PRIVATE VERSION_INFO=${PROJECT_VERSION})

install(TARGETS _core DESTINATION scikit_build_example)
36 changes: 36 additions & 0 deletions docs/examples/downstream/pybind11_example/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Copyright (c) 2016 The Pybind Development Team, All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

You are under no obligation whatsoever to provide any bug fixes, patches, or
upgrades to the features, functionality or performance of the source code
("Enhancements") to anyone; however, if you choose to make your Enhancements
available either publicly, or directly to the author of this software, without
imposing a separate written license agreement for such Enhancements, then you
hereby grant the following license: a non-exclusive, royalty-free perpetual
license to install, use, modify, prepare derivative works, incorporate into
other computer software, distribute, and sublicense such enhancements or
derivative works thereof, in binary and source code form.
55 changes: 55 additions & 0 deletions docs/examples/downstream/pybind11_example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# scikit_build_example

[![Gitter][gitter-badge]][gitter-link]

| CI | status |
| ------------ | ------------------------------------------------------------------ |
| conda.recipe | [![Conda Actions Status][actions-conda-badge]][actions-conda-link] |
| pip builds | [![Pip Actions Status][actions-pip-badge]][actions-pip-link] |

An example project built with [pybind11](https://github.com/pybind/pybind11) and
scikit-build-core. Python 3.7+ (see older commits for older versions of Python).

[gitter-badge]: https://badges.gitter.im/pybind/Lobby.svg
[gitter-link]: https://gitter.im/pybind/Lobby
[actions-badge]:
https://github.com/pybind/scikit_build_example/workflows/Tests/badge.svg
[actions-conda-link]:
https://github.com/pybind/scikit_build_example/actions?query=workflow%3AConda
[actions-conda-badge]:
https://github.com/pybind/scikit_build_example/workflows/Conda/badge.svg
[actions-pip-link]:
https://github.com/pybind/scikit_build_example/actions?query=workflow%3APip
[actions-pip-badge]:
https://github.com/pybind/scikit_build_example/workflows/Pip/badge.svg
[actions-wheels-link]:
https://github.com/pybind/scikit_build_example/actions?query=workflow%3AWheels
[actions-wheels-badge]:
https://github.com/pybind/scikit_build_example/workflows/Wheels/badge.svg

## Installation

- clone this repository
- `pip install ./scikit_build_example`

## CI Examples

There are examples for CI in `.github/workflows`. A simple way to produces
binary "wheels" for all platforms is illustrated in the "wheels.yml" file, using
[`cibuildwheel`][].

## License

pybind11 is provided under a BSD-style license that can be found in the LICENSE
file. By using, distributing, or contributing to this project, you agree to the
terms and conditions of this license.

## Test call

```python
import scikit_build_example

scikit_build_example.add(1, 2)
```

[`cibuildwheel`]: https://cibuildwheel.readthedocs.io
49 changes: 49 additions & 0 deletions docs/examples/downstream/pybind11_example/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[build-system]
requires = ["scikit-build-core>=0.3.3", "pybind11"]
build-backend = "scikit_build_core.build"


[project]
name = "scikit_build_example"
version = "0.0.1"
description="A minimal example package (with pybind11)"
readme = "README.md"
authors = [
{ name = "My Name", email = "[email protected]" },
]
requires-python = ">=3.7"
classifiers = [
"Development Status :: 4 - Beta",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
]

[project.optional-dependencies]
test = ["pytest"]


[tool.scikit-build]
wheel.expand-macos-universal-tags = true


[tool.pytest.ini_options]
minversion = "6.0"
addopts = ["-ra", "--showlocals", "--strict-markers", "--strict-config"]
xfail_strict = true
filterwarnings = [
"error",
"ignore:(ast.Str|Attribute s|ast.NameConstant|ast.Num) is deprecated:DeprecationWarning:_pytest", # Python 3.12
]
testpaths = ["tests"]


[tool.cibuildwheel]
test-command = "pytest {project}/tests"
test-extras = ["test"]
test-skip = ["*universal2:arm64"]
build-verbosity = 1
Loading