Skip to content
Open
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
35 changes: 23 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ helpers live in submodules. The implementation comes from `compat.nanobind`

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)).
in — version, free-threaded configuration and `Py_LIMITED_API`. The default
`python-sdk` feature provisions `xim:python` 3.13.12; a consumer's own
declaration, `MCPP_PYTHON_EXECUTABLE`/`VIRTUAL_ENV`, or
`MCPP_PYTHON_INCLUDE_DIR` takes precedence (see [Usage and API](docs/usage.md)).

## Modules

Expand Down Expand Up @@ -59,11 +60,11 @@ In an existing project:
```toml
[dependencies]
mcpplibs.mwrap.nanobind = "3.1.0"

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

The default `python-sdk` feature provisions `xim:python` 3.13.12, so no
`[xlings.workspace]` entry is needed.

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

Expand All @@ -78,23 +79,33 @@ NB_MODULE(example, m) {
}
```

`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.
The default `python-sdk` feature provisions 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, disable it and supply your own through
`MCPP_PYTHON_INCLUDE_DIR` or `MCPP_PYTHON_EXECUTABLE` (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. |
| `python-sdk` | yes | Provisions the default `xim:python` 3.13.12 SDK for the module interface and runtime. |
| `tests` | no | Links the Python embedding library into this package's own test executables only. |

To supply a custom SDK, keep the runtime and disable the packaged default:

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

To supply the runtime yourself (for example with `NB_SHARED` or a custom
`Py_LIMITED_API` build), disable defaults:
`Py_LIMITED_API` build), disable defaults and keep only `python-sdk` if you still
want the packaged SDK:

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

Then compile `<mcpp_nanobind_combined.hpp>` in exactly one translation unit of
Expand Down
27 changes: 18 additions & 9 deletions README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ API 原样导出:`import mcpplibs.mwrap.nanobind;` 即得到 `nb::class_`、

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

## 模块
Expand Down Expand Up @@ -56,11 +57,11 @@ mcpp run
```toml
[dependencies]
mcpplibs.mwrap.nanobind = "3.1.0"

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

默认的 `python-sdk` feature 会提供 `xim:python` 3.13.12,因此无需再写
`[xlings.workspace]`。

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

Expand All @@ -75,22 +76,30 @@ NB_MODULE(example, m) {
}
```

`xim:python` 同时用于模块接口和运行时。如果应用通过 `uv`、虚拟环境或自行
构建的 Python 获得 SDK,请设置 `MCPP_PYTHON_INCLUDE_DIR`;内嵌解释器在运行
时还需要 `MCPP_PYTHON_HOME`。系统 Python 需要安装开发包。
默认的 `python-sdk` feature 提供同时用于模块接口和运行时的 SDK。如果应用
通过 `uv`、虚拟环境或自行构建的 Python 获得 SDK,请关闭它,并通过
`MCPP_PYTHON_INCLUDE_DIR` 或 `MCPP_PYTHON_EXECUTABLE` 指定自己的 SDK;内嵌
解释器在运行时还需要 `MCPP_PYTHON_HOME`。系统 Python 需要安装开发包。

### Feature

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

若要使用自定义 SDK,保留 runtime 并关闭打包的默认 SDK:

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

若要自行提供运行时(例如 `NB_SHARED` 或自定义 `Py_LIMITED_API`),关闭默认
feature:
feature;若仍需要打包的 SDK,只保留 `python-sdk`:

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

然后按 `compat.nanobind` 的说明,在程序的恰好一个翻译单元中编译
Expand Down
60 changes: 38 additions & 22 deletions build.mcpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
// interpreters, so those link it — behind a feature no consumer activates.
//
// Resolution order for <Python.h> (docs/usage.md):
// 1. MCPP_PYTHON_INCLUDE_DIR
// 2. xim:python, as declared here or by a package in the graph
// 3. MCPP_PYTHON_EXECUTABLE, VIRTUAL_ENV, then python3/python on PATH
// 4. a non-zero exit naming every way to supply it
// 1. MCPP_PYTHON_INCLUDE_DIR (strict: an invalid value is a hard error)
// 2. MCPP_PYTHON_EXECUTABLE (strict: an invalid value is a hard error)
// 3. VIRTUAL_ENV
// 4. xim:python, from the default python-sdk feature or a package in the graph
// 5. python3/python on PATH
// 6. a non-zero exit naming every way to supply it
import std;
import mcpp;

Expand Down Expand Up @@ -115,29 +117,35 @@ std::optional<fs::path> interpreter_on_path() {
return std::nullopt;
}

std::optional<fs::path> find_include() {
std::optional<fs::path> find_include(std::string& failure) {
if (const char* value = std::getenv("MCPP_PYTHON_INCLUDE_DIR"); value && *value) {
const fs::path dir(value);
if (has_python_h(dir))
return dir;
std::println(std::cerr, "nanobind-m: MCPP_PYTHON_INCLUDE_DIR={} has no Python.h", value);
}

if (const char* value = mcpp::xpkg_dir("xim", "python"); value && *value) {
if (const auto found = include_from_root(value))
return found;
failure = std::format("nanobind-m: MCPP_PYTHON_INCLUDE_DIR={} does not contain Python.h", value);
return std::nullopt;
}

if (const char* value = std::getenv("MCPP_PYTHON_EXECUTABLE"); value && *value) {
if (const auto found = include_from_interpreter(fs::path(value)))
return found;
failure = std::format(
"nanobind-m: MCPP_PYTHON_EXECUTABLE={} has no Python.h in its prefix; "
"refusing to fall through to xim:python or PATH",
value);
return std::nullopt;
}

if (const char* value = std::getenv("VIRTUAL_ENV"); value && *value) {
if (const auto found = include_from_prefix(value))
return found;
}

if (const char* value = mcpp::xpkg_dir("xim", "python"); value && *value) {
if (const auto found = include_from_root(value))
return found;
}

if (const auto interpreter = interpreter_on_path()) {
if (const auto found = include_from_interpreter(*interpreter))
return found;
Expand All @@ -146,6 +154,20 @@ std::optional<fs::path> find_include() {
return std::nullopt;
}

void print_missing_help() {
std::println(std::cerr,
"nanobind-m: no Python development headers found.\n"
" Enable the default python-sdk feature (xim:python 3.13.12), or declare an SDK:\n"
" [xlings.workspace]\n"
" \"xim:python\" = \"3.13.12\"\n"
" or point at an exact include directory:\n"
" MCPP_PYTHON_INCLUDE_DIR=/path/to/include/python3.13 mcpp build\n"
" or point at an interpreter whose prefix has Python.h:\n"
" MCPP_PYTHON_EXECUTABLE=/path/to/python3 mcpp build\n"
" or activate a virtual environment (VIRTUAL_ENV),\n"
" or install the development package for the python3/python on PATH.");
}

fs::path root_of_include(const fs::path& include) {
fs::path directory = include;
if (directory.filename().string().rfind("python3.", 0) == 0)
Expand Down Expand Up @@ -229,18 +251,12 @@ int main() {
rerun_inputs();

const bool is_windows = std::string_view(mcpp::target_os()) == "windows";
const std::optional<fs::path> include = find_include();
std::string failure;
const std::optional<fs::path> include = find_include(failure);
if (!include) {
std::println(std::cerr,
"nanobind-m: no Python development headers found.\n"
" Declare the SDK in mcpp.toml:\n"
" [xlings.workspace]\n"
" \"xim:python\" = \"3.13.12\"\n"
" or point at an exact directory:\n"
" MCPP_PYTHON_INCLUDE_DIR=/path/to/include/python3.13 mcpp build\n"
" or point at an interpreter whose sysconfig include directory has Python.h:\n"
" MCPP_PYTHON_EXECUTABLE=/path/to/python3 mcpp build\n"
" A system Python needs its development package (python3-dev).");
if (!failure.empty())
std::println(std::cerr, "{}", failure);
print_missing_help();
return 1;
}

Expand Down
64 changes: 49 additions & 15 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ operator helpers and the version macros. The implementation is
```toml
[dependencies]
mcpplibs.mwrap.nanobind = "3.1.0"

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

The default features include `python-sdk`, which provisions `xim:python`
3.13.12, so no `[xlings.workspace]` entry is needed for a zero-config build. A
consumer can still declare a nearer `[xlings.workspace]` entry, or disable the
feature and choose another SDK (see [Python SDK resolution](#python-sdk-resolution)).

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

Expand All @@ -35,17 +37,45 @@ NB_MODULE(example, m) {

## Python SDK resolution

`build.mcpp` resolves the directory that contains `Python.h` in this order:
The default features are `runtime` and `python-sdk`:

```toml
[features]
default = ["runtime", "python-sdk"]
python-sdk = { description = "Provision the default xim:python 3.13.12 SDK for nanobind" }

[feature-xlings.python-sdk]
"xim:python" = "3.13.12"
```

1. `MCPP_PYTHON_INCLUDE_DIR`, which must contain `Python.h`.
2. `xim:python` from `[xlings.workspace]`, either this package's declaration or
one in the resolved graph. On Windows it uses `<payload>/include`; on Unix it
`python-sdk` provisions the packaged `xim:python` 3.13.12 SDK so a zero-config
build works out of the box. `build.mcpp` resolves the directory that contains
`Python.h` in this order:

1. `MCPP_PYTHON_INCLUDE_DIR`, which must contain `Python.h`. If it is set but
does not, configuration fails; it never falls back to a lower choice.
2. `MCPP_PYTHON_EXECUTABLE`, taking the include directory from the
interpreter's prefix. If it is set but the prefix has no `Python.h`,
configuration fails; it never falls back to `xim:python` or `PATH`.
3. `VIRTUAL_ENV`, when the environment's `include` directory contains
`Python.h`.
4. `xim:python`, provisioned by `python-sdk` or declared by a consumer's
`[xlings.workspace]`. On Windows it uses `<payload>/include`; on Unix it
scans `<payload>/include/python3.*` and prefers the highest version.
3. `MCPP_PYTHON_EXECUTABLE`, then `VIRTUAL_ENV` (`bin/python3` or
`Scripts/python.exe`), then `python3` (Unix) / `python` (Windows) on `PATH`,
taking the include directory from the interpreter's prefix.
4. A non-zero configure that names the declaration, environment variables and
development-package options.
5. `python3` (Unix) / `python` (Windows) on `PATH`, taking the include
directory from the interpreter's prefix.
6. A non-zero configure that names every way to supply the SDK.

To use a custom venv, host or cross-compilation SDK instead of the packaged
default, disable `python-sdk` while keeping the runtime:

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

Then supply the SDK through `MCPP_PYTHON_INCLUDE_DIR`,
`MCPP_PYTHON_EXECUTABLE`, `VIRTUAL_ENV`, a consumer `[xlings.workspace]`
declaration or `PATH`.

The build adds only a private include directory. It never calls
`link_lib("python...")` for a normal build: a Linux CPython extension module
Expand All @@ -66,8 +96,9 @@ variables complete the picture:
| `MCPP_PYTHON_LIBRARY_DIR` | Directory holding `libpython3.x.so`/`.dylib` or `python3xx.lib`. |
| `MCPP_PYTHON_LIBRARY` | Link name, for example `python3.13` or `python313`. |

A consumer's nearer `[xlings.workspace]` entry wins over this package's default.
`MCPP_PYTHON_INCLUDE_DIR` wins over both, for `uv`, a custom build or a
A consumer's nearer `[xlings.workspace]` entry wins over the `python-sdk`
feature's default. `MCPP_PYTHON_INCLUDE_DIR` and `MCPP_PYTHON_EXECUTABLE` win
over both when set, for `uv`, a custom build or a
`Py_LIMITED_API`/free-threaded interpreter.

## The module surface
Expand Down Expand Up @@ -186,9 +217,12 @@ own visibility/aliasing flags, disable the default runtime and compile the
forwarder yourself:

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

Keep `python-sdk` when you still want the packaged SDK; omit it to use your own
SDK through the resolution above.

```toml
[build]
cxxflags = ["-DNB_SHARED", "-fvisibility=hidden", "-fno-strict-aliasing"]
Expand Down
16 changes: 9 additions & 7 deletions mcpp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,23 @@ exports = [
]

[features]
default = ["runtime"]
default = ["runtime", "python-sdk"]
# The one translation unit compat.nanobind documents: its generated forwarder
# includes the matching upstream src/nb_combined.cpp.
runtime = { sources = ["src/nb_runtime.cpp"], description = "Compile the nanobind runtime into this package" }
# The Python SDK used to compile the module interface and the runtime. Kept
# behind a default-on feature so a consumer can opt out with
# `default-features = false, features = ["runtime"]` and supply its own SDK
# through MCPP_PYTHON_INCLUDE_DIR, MCPP_PYTHON_EXECUTABLE, VIRTUAL_ENV or PATH.
python-sdk = { description = "Provision the default xim:python 3.13.12 SDK for nanobind" }
# Compile-time only: the package's own tests are embedded executables, so they
# link the Python SDK here without ever adding a link edge to a consumer. The
# support module lives outside src/ so [build] sources never scans it.
tests = { sources = ["tests/support/python_embed.cppm"], description = "Build and link this package's embedded-Python tests" }

[feature-xlings.python-sdk]
"xim:python" = "3.13.12"

[test]
features = ["tests"]

Expand All @@ -48,12 +56,6 @@ mcpplibs.mtest = "0.11.3"
[package.metadata.mwrap]
upstream-version = "3.1.0"

# The Python SDK used to compile the module interface and the runtime. A
# consumer's nearer declaration wins; MCPP_PYTHON_INCLUDE_DIR overrides this
# discovery entirely (see docs/usage.md).
[xlings.workspace]
"xim:python" = "3.13.12"

[[runtime.deploy]]
from = "LICENSE"
to = "licenses/nanobind"
Expand Down
Loading