diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..dd07ce2 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.19) +project(bonacci_bindings LANGUAGES CXX) + +find_package(Python COMPONENTS Interpreter Development.Module REQUIRED) + +execute_process( + COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir + OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE nanobind_ROOT + COMMAND_ERROR_IS_FATAL ANY) + +find_package(nanobind CONFIG REQUIRED) + +nanobind_add_module(sequence src/bonacci_bindings/sequence.cpp) +install(TARGETS sequence DESTINATION bonacci_bindings) diff --git a/README.md b/README.md index d8942b4..6fb6e46 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,13 @@ # Python Starter -A minimalist template for starting a new [Python](https://www.python.org/) project. +A minimalist template for starting a new [Python](https://www.python.org/) project with C++ bindings. This template provides a basic Python project containing an example package with built-in support for formatting, linting, testing, and continuous integration. ## Key Features - Uses [uv](https://docs.astral.sh/uv/) as the package manager. +- Supports C++ bindings using [nanobind](https://nanobind.readthedocs.io/). - Supports formatting and linting with [dprint](https://dprint.dev/) and [Ruff](https://github.com/astral-sh/ruff). - Supports testing and coverage checks with [Pytest](https://docs.pytest.org/en/stable/). - Fixes formatting and linting issues during pre-commit hooks using [Lefthook](https://lefthook.dev/). @@ -60,6 +61,11 @@ Modify the source files under the [`src`](./src) directory to start writing the You can replace the [`src/bonacci`](./src/bonacci) directory with your package name. You can also add as many packages as you want to the `src` directory. Just make sure to update the contents of the [`pyproject.toml`](./pyproject.toml) file according to your package information. +#### Writing C++ Extensions + +This template includes C++ bindings using [nanobind](https://nanobind.readthedocs.io/). +Replace the [`src/bonacci_bindings`](./src/bonacci_bindings) with your C++ extension package name. When adding new C++ source files, make sure to update the [`CMakeLists.txt`](./CMakeLists.txt) file. For more information on nanobind, refer to [this documentation](https://nanobind.readthedocs.io/en/latest/). + #### Testing the Package Test files in this template are named `test_*.py` and located in the [`tests`](./tests) directory. Write the necessary tests for your package and run them with: diff --git a/pyproject.toml b/pyproject.toml index f2be337..c3c121a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,8 +23,8 @@ repository = "https://github.com/leonardo/bonacci.git" issues = "https://github.com/leonardo/bonacci/issues" [build-system] -requires = ["hatchling"] -build-backend = "hatchling.build" +requires = ["scikit-build-core>=0.11.6", "nanobind>=2.9.2"] +build-backend = "scikit_build_core.build" [dependency-groups] dev = [ diff --git a/src/bonacci/__init__.py b/src/bonacci/__init__.py index a2866f8..4948b9d 100644 --- a/src/bonacci/__init__.py +++ b/src/bonacci/__init__.py @@ -1,5 +1,5 @@ """Example Python package for generating Fibonacci sequences.""" -from .sequence import fibonacci_sequence +from bonacci_bindings.sequence import fibonacci_sequence __all__ = ["fibonacci_sequence"] diff --git a/src/bonacci/sequence.py b/src/bonacci/sequence.py deleted file mode 100644 index f520888..0000000 --- a/src/bonacci/sequence.py +++ /dev/null @@ -1,18 +0,0 @@ -"""Example functions for generating Fibonacci sequences.""" - - -def fibonacci_sequence(n: int) -> list[int]: - """Generate a Fibonacci sequence up to the given number of terms.""" - if n <= 0: - return [] - if n == 1: - return [1] - - sequence = [1, 1] - for _ in range(n - 2): - sequence.append(sequence[-2] + sequence[-1]) - - return sequence - - -__all__ = ["fibonacci_sequence"] diff --git a/src/bonacci_bindings/sequence.cpp b/src/bonacci_bindings/sequence.cpp new file mode 100644 index 0000000..de60025 --- /dev/null +++ b/src/bonacci_bindings/sequence.cpp @@ -0,0 +1,23 @@ +#include +#include + +#include +#include + +std::vector fibonacci_sequence(int n) { + std::vector sequence(std::max(n, 0), 1); + for (int i{2}; i < n; ++i) { + sequence[i] = sequence[i - 2] + sequence[i - 1]; + } + return sequence; +} + +namespace nb = nanobind; + +NB_MODULE(sequence, m) { + m.doc() = "Example functions for generating Fibonacci sequences."; + + m.def( + "fibonacci_sequence", &fibonacci_sequence, + "Generate a Fibonacci sequence up to the given number of terms"); +}