Skip to content
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
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Preserve upstream document bytes and original whitespace on every platform.
upstream/** -text -whitespace
# Keep the release archive free of local planning material.
.local/** export-ignore
96 changes: 96 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
name: CI

on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:

permissions:
contents: read

concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true

jobs:
mcpp:
name: mcpp (${{ matrix.os }}, ${{ matrix.toolchain }})
strategy:
fail-fast: false
matrix:
include:
- { os: ubuntu-24.04, toolchain: 'gcc@16.1.0' }
- { os: ubuntu-24.04, toolchain: 'llvm@23.1.3' }
- { os: macos-15, toolchain: 'llvm@23.1.3' }
- { os: windows-2025, toolchain: 'llvm@23.1.3' }
runs-on: ${{ matrix.os }}
timeout-minutes: 90
defaults:
run:
shell: bash
env:
XLINGS_NON_INTERACTIVE: 1
steps:
- uses: actions/checkout@v4

- name: Configure MCPP_HOME
run: echo "MCPP_HOME=${{ runner.temp }}/mcpp-home" >> "$GITHUB_ENV"

- uses: actions/setup-python@v5
with:
python-version: "3.13"

- name: Retained upstream document bytes
run: python3 tools/check_documents.py

- name: Install Xlings (Linux/macOS)
if: runner.os != 'Windows'
run: |
curl -fsSL https://raw.githubusercontent.com/openxlings/xlings/refs/heads/main/tools/other/quick_install.sh | bash
echo "$HOME/.xlings/subos/current/bin" >> "$GITHUB_PATH"

- name: Install Xlings (Windows)
if: runner.os == 'Windows'
shell: pwsh
run: |
irm https://raw.githubusercontent.com/openxlings/xlings/main/tools/other/quick_install.ps1 | iex
"$env:USERPROFILE\.xlings\subos\current\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

- name: Install project tools (.xlings.json)
run: |
for attempt in 1 2 3; do
xlings update > /dev/null 2>&1 || true
if xlings install && (cd "$RUNNER_TEMP" && xlings install xim:mcpp@2026.11.22.1); then break; fi
[ "$attempt" = 3 ] && exit 1
sleep 30
done
(cd "$RUNNER_TEMP" && mcpp --version)

- name: Select the toolchain
run: |
name="${{ matrix.toolchain }}"
mcpp toolchain install "${name%@*}" "${name#*@}"
mcpp toolchain default "$name"

# compat.nanobind is supplied by PR 1. Once it is published this step is
# a no-op refresh of the same complete index.
- name: Candidate built-in index (compat.nanobind)
run: |
mcpp self init --home "$MCPP_HOME" || true
git clone --depth 1 https://github.com/mcpplibs/mcpp-index.git "$RUNNER_TEMP/mcpp-index"
python3 "$RUNNER_TEMP/mcpp-index/tests/prepare_builtin_index.py" --home "$MCPP_HOME"
sed -i 's/auto_refresh = true/auto_refresh = false/' "$MCPP_HOME/config.toml" || true

- name: Build the modules
run: mcpp build --strict

- name: Tests (core, side header, ndarray, operators, macros)
run: mcpp test

- name: Tests, release profile
run: mcpp test --profile release

- name: Installed template (mcpp new, --strict build, run)
run: python3 tools/test_template.py --index "$RUNNER_TEMP/mcpp-index" --toolchain "${{ matrix.toolchain }}"
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
target/
.mcpp/
.xlings/
mcpp.lock
compile_commands.json
__pycache__/
5 changes: 5 additions & 0 deletions .xlings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"workspace": {
"xim:mcpp": "2026.11.22.1"
}
}
17 changes: 17 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
nanobind-m: C++ module wrapper
Copyright (c) 2026 mcpp-community contributors

The wrapper contributions are licensed under the Apache License, Version 2.0.
Upstream code, copied interfaces and dependencies retain their original
licenses and copyright notices.

nanobind is Copyright (c) 2022 Wenzel Jakob and is distributed under the
BSD-3-Clause license. Its original README and LICENSE are retained byte for
byte in upstream/nanobind/. The implementation is built by compat.nanobind
from the same v3.1.0 release.

compat.nanobind reaches its hash map through compat.robin-map, Copyright (c)
2018 Thibaut Goetghebuer-Planchon, distributed under the MIT license.

Provenance and SHA256 checksums of the retained upstream documents are
recorded in upstream/documents.json.
117 changes: 116 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,116 @@
# nanobind-m
# nanobind-m

> nanobind 3.1.0 as C++23 modules: bind C++ to Python with no boilerplate headers to arrange.

English · [简体中文](README.zh.md) · [Upstream](https://github.com/wjakob/nanobind) · [Releases](https://github.com/mcpplibs/nanobind-m/releases) · [mcpp](https://github.com/mcpp-community/mcpp) · [Index](https://github.com/mcpplibs/mcpp-index)

[![CI](https://github.com/mcpplibs/nanobind-m/actions/workflows/ci.yml/badge.svg)](https://github.com/mcpplibs/nanobind-m/actions/workflows/ci.yml)
[![Wrapper license](https://img.shields.io/badge/wrapper-Apache--2.0-blue)](LICENSE)
[![Upstream license](https://img.shields.io/badge/nanobind-BSD--3--Clause-blue)](upstream/nanobind/LICENSE)

This is a thin wrapper around [nanobind](https://github.com/wjakob/nanobind)
v3.1.0. The header API is exported unchanged: `import
mcpplibs.mwrap.nanobind;` gives you `nb::class_`, `nb::def`, `nb::cast`, the
exception types and the object wrappers, while the STL, ndarray and operator
helpers live in submodules. The implementation comes from `compat.nanobind`
3.1.0; no upstream C++ source is vendored here.

The module interface and the runtime are compiled against the Python SDK on
`<Python.h>`'s include path. That must be the same Python ABI the bindings run
in — version, free-threaded configuration and `Py_LIMITED_API`. The manifest
declares `xim:python` 3.13.12 as the default; a consumer's own declaration, or
`MCPP_PYTHON_INCLUDE_DIR`, takes precedence (see [Usage and API](docs/usage.md)).

## Modules

- `import mcpplibs.mwrap.nanobind;`: handles, objects, classes, functions, casts, exceptions and utilities.
- `import mcpplibs.mwrap.nanobind.stl;`: casters for `std::string`, containers, `std::optional`, `std::variant`, `std::function`, smart pointers, filesystem and chrono.
- `import mcpplibs.mwrap.nanobind.ndarray;`: `nb::ndarray`, DLPack types, device/order/framework tags and `nb::dtype`.
- `import mcpplibs.mwrap.nanobind.operators;`: `nb::self` and operator-binding helpers.
- `import mcpplibs.mwrap.nanobind.macros;`: `NB_VERSION_*` as `inline constexpr` constants.

> More Modules API -> [Usage and API](docs/usage.md)

`NB_MODULE`, `NB_TRAMPOLINE`, `NB_OVERRIDE*` and `NB_MAKE_OPAQUE` are macros and
cannot be exported by a named module. Include
`<mwrap/nanobind/macros.hpp>` **before** every import in that translation unit:

```cpp
#include <mwrap/nanobind/macros.hpp> // first
import mcpplibs.mwrap.nanobind; // then
import mcpplibs.mwrap.nanobind.stl;
```

## Getting started

A new project uses the default template:

```sh
mcpp new pybind --template mcpplibs.mwrap.nanobind@3.1.0
cd pybind
mcpp run
```

The generated program builds a Python module with `NB_MODULE`, registers it
with `PyImport_AppendInittab`, and imports it from the embedded interpreter.

In an existing project:

```toml
[dependencies]
mcpplibs.mwrap.nanobind = "3.1.0"

[xlings.workspace]
"xim:python" = "3.13.12"
```

```cpp
#include <mwrap/nanobind/macros.hpp>

import std;
import mcpplibs.mwrap.nanobind;
import mcpplibs.mwrap.nanobind.stl;

using namespace nanobind::literals;

NB_MODULE(example, m) {
m.def("add", [](int a, int b) { return a + b; }, "a"_a, "b"_a);
}
```

`xim:python` is the SDK used for both the module interface and the runtime.
If your application gets Python from `uv`, a virtual environment, or a custom
build, set `MCPP_PYTHON_INCLUDE_DIR` (and `MCPP_PYTHON_HOME` at run time for
an embedded interpreter). A system Python needs its development package.

### Features

| Feature | Default | Effect |
|---|---|---|
| `runtime` | yes | Compiles `src/nb_runtime.cpp` from compat.nanobind's forwarder, so one link of the module is enough. |
| `tests` | no | Links the Python embedding library into this package's own test executables only. |

To supply the runtime yourself (for example with `NB_SHARED` or a custom
`Py_LIMITED_API` build), disable defaults:

```toml
mcpplibs.mwrap.nanobind = { version = "3.1.0", default-features = false }
```

Then compile `<mcpp_nanobind_combined.hpp>` in exactly one translation unit of
your program, as `compat.nanobind` documents.

## Platforms

Linux (GCC 16, LLVM 23), macOS (LLVM 23) and Windows (LLVM 23) are covered by
CI. Linux CPython extension modules must not link `libpython`; this package
never adds that link to a consumer. The embedded test executables and the
default template do link it, because they host an interpreter themselves.

## License

The wrapper is Apache-2.0; see [LICENSE](LICENSE) and the attribution in
[NOTICE](NOTICE). Upstream nanobind remains BSD-3-Clause: its original
[LICENSE](upstream/nanobind/LICENSE) and [README](upstream/nanobind/README.md)
are retained byte for byte, with checksums in
[upstream/documents.json](upstream/documents.json).
110 changes: 110 additions & 0 deletions README.zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# nanobind-m

> 将 nanobind 3.1.0 提供为 C++23 模块:用 Python 绑定 C++,无需安排样板头文件。

[English](README.md) · 简体中文 · [上游](https://github.com/wjakob/nanobind) · [发布](https://github.com/mcpplibs/nanobind-m/releases) · [mcpp](https://github.com/mcpp-community/mcpp) · [索引](https://github.com/mcpplibs/mcpp-index)

[![CI](https://github.com/mcpplibs/nanobind-m/actions/workflows/ci.yml/badge.svg)](https://github.com/mcpplibs/nanobind-m/actions/workflows/ci.yml)
[![封装许可证](https://img.shields.io/badge/wrapper-Apache--2.0-blue)](LICENSE)
[![上游许可证](https://img.shields.io/badge/nanobind-BSD--3--Clause-blue)](upstream/nanobind/LICENSE)

这是 [nanobind](https://github.com/wjakob/nanobind) v3.1.0 的薄封装。头文件
API 原样导出:`import mcpplibs.mwrap.nanobind;` 即得到 `nb::class_`、
`nb::def`、`nb::cast`、异常类型和对象包装;STL、ndarray 与运算符辅助位于
子模块。实现来自 `compat.nanobind` 3.1.0,本仓库不 vendor 上游 C++ 源码。

模块接口与运行时都针对 `<Python.h>` 所在目录里的 Python SDK 编译。该 SDK
必须与绑定实际运行的 Python ABI 一致——版本、free-threaded 配置和
`Py_LIMITED_API`。清单默认声明 `xim:python` 3.13.12;消费方自己的声明或
`MCPP_PYTHON_INCLUDE_DIR` 优先(见[用法与 API](docs/usage.md))。

## 模块

- `import mcpplibs.mwrap.nanobind;`:句柄、对象、类、函数、转换、异常与工具。
- `import mcpplibs.mwrap.nanobind.stl;`:`std::string`、容器、`std::optional`、`std::variant`、`std::function`、智能指针、filesystem 与 chrono 的 caster。
- `import mcpplibs.mwrap.nanobind.ndarray;`:`nb::ndarray`、DLPack 类型、设备/顺序/框架标签和 `nb::dtype`。
- `import mcpplibs.mwrap.nanobind.operators;`:`nb::self` 与运算符绑定辅助。
- `import mcpplibs.mwrap.nanobind.macros;`:以 `inline constexpr` 导出的 `NB_VERSION_*` 常量。

> 更多模块 API -> [用法与 API](docs/usage.md)

`NB_MODULE`、`NB_TRAMPOLINE`、`NB_OVERRIDE*` 和 `NB_MAKE_OPAQUE` 是宏,命名
模块无法导出。在同一个翻译单元中,请把
`<mwrap/nanobind/macros.hpp>` 放在**所有 import 之前**:

```cpp
#include <mwrap/nanobind/macros.hpp> // 先
import mcpplibs.mwrap.nanobind; // 后
import mcpplibs.mwrap.nanobind.stl;
```

## 快速开始

新项目使用默认模板:

```sh
mcpp new pybind --template mcpplibs.mwrap.nanobind@3.1.0
cd pybind
mcpp run
```

生成的程序用 `NB_MODULE` 构建 Python 模块,通过 `PyImport_AppendInittab`
注册,并由同一进程内嵌的解释器导入。

已有项目:

```toml
[dependencies]
mcpplibs.mwrap.nanobind = "3.1.0"

[xlings.workspace]
"xim:python" = "3.13.12"
```

```cpp
#include <mwrap/nanobind/macros.hpp>

import std;
import mcpplibs.mwrap.nanobind;
import mcpplibs.mwrap.nanobind.stl;

using namespace nanobind::literals;

NB_MODULE(example, m) {
m.def("add", [](int a, int b) { return a + b; }, "a"_a, "b"_a);
}
```

`xim:python` 同时用于模块接口和运行时。如果应用通过 `uv`、虚拟环境或自行
构建的 Python 获得 SDK,请设置 `MCPP_PYTHON_INCLUDE_DIR`;内嵌解释器在运行
时还需要 `MCPP_PYTHON_HOME`。系统 Python 需要安装开发包。

### Feature

| Feature | 默认 | 作用 |
|---|---|---|
| `runtime` | 是 | 编译 compat.nanobind 转发器对应的 `src/nb_runtime.cpp`,链接一次即可使用模块。 |
| `tests` | 否 | 只为本包自己的测试可执行文件链接 Python 嵌入库。 |

若要自行提供运行时(例如 `NB_SHARED` 或自定义 `Py_LIMITED_API`),关闭默认
feature:

```toml
mcpplibs.mwrap.nanobind = { version = "3.1.0", default-features = false }
```

然后按 `compat.nanobind` 的说明,在程序的恰好一个翻译单元中编译
`<mcpp_nanobind_combined.hpp>`。

## 平台

CI 覆盖 Linux(GCC 16、LLVM 23)、macOS(LLVM 23)和 Windows(LLVM 23)。
Linux 上的 CPython 扩展模块不得链接 `libpython`;本包绝不会给消费方添加这条
链接。内嵌测试可执行文件和默认模板会链接它,因为它们自己承载解释器。

## 许可证

封装采用 Apache-2.0;见 [LICENSE](LICENSE) 与 [NOTICE](NOTICE) 中的署名。
上游 nanobind 仍为 BSD-3-Clause:原始
[LICENSE](upstream/nanobind/LICENSE) 与 [README](upstream/nanobind/README.md)
按字节保留,校验和记录于 [upstream/documents.json](upstream/documents.json)。
Loading
Loading