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
1 change: 1 addition & 0 deletions mcpp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ members = [
"tests/mwrap/lua-embed",
"tests/mwrap/quickjs-embed",
"tests/mwrap/pocketpy-embed",
"tests/mwrap/nanobind-embed",
"tests/mwrap/libpng-image",
"tests/mwrap/libwebp-image",
"tests/mwrap/slint-ui",
Expand Down
38 changes: 38 additions & 0 deletions pkgs/mwrap/nanobind.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-- Immutable upstream-aligned mwrap source release (nanobind-m mwrap-3.1.0).
-- The CN mirror is added by a maintainer with mcpp-res access; until then CN
-- users fall back to the GitHub release (docs/cn-mirror.md).
package = {
spec = "1",
namespace = "mcpplibs.mwrap",
name = "nanobind",
description = "nanobind-m: nanobind 3.1.0 (C++/Python bindings, STL casters, ndarray/DLPack, operators) as C++23 modules",
licenses = { "Apache-2.0" },
repo = "https://github.com/mcpplibs/nanobind-m",
type = "package",
extensions = {
autoupdate = {
tag = "mwrap-${version}",
url = "https://github.com/mcpplibs/nanobind-m/releases/download/${tag}/nanobind-m-${version}.tar.gz",
},
},
xpm = {
linux = {
["3.1.0"] = {
url = "https://github.com/mcpplibs/nanobind-m/releases/download/mwrap-3.1.0/nanobind-m-3.1.0.tar.gz",
sha256 = "38d68f89744ce241197133a4cc07330e5e344e752d1b8a98826f140e8a05ef39",
},
},
macosx = {
["3.1.0"] = {
url = "https://github.com/mcpplibs/nanobind-m/releases/download/mwrap-3.1.0/nanobind-m-3.1.0.tar.gz",
sha256 = "38d68f89744ce241197133a4cc07330e5e344e752d1b8a98826f140e8a05ef39",
},
},
windows = {
["3.1.0"] = {
url = "https://github.com/mcpplibs/nanobind-m/releases/download/mwrap-3.1.0/nanobind-m-3.1.0.tar.gz",
sha256 = "38d68f89744ce241197133a4cc07330e5e344e752d1b8a98826f140e8a05ef39",
},
},
},
}
1 change: 1 addition & 0 deletions tests/mwrap/check_installed_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"mcpplibs.mwrap.slint": ("slint.cppm", "upstream/slint/LICENSE.md", b"Slint License", b"SixtyFPS"),
"mcpplibs.mwrap.wuffs": ("wuffs.cppm", "upstream/wuffs/LICENSE", b"Apache License", b"The Wuffs Authors"),
"mcpplibs.mwrap.protobuf": ("protobuf.cppm", "upstream/protobuf/LICENSE", b"Redistribution and use in source and binary forms", b"Google"),
"mcpplibs.mwrap.nanobind": ("nanobind.cppm", "upstream/nanobind/LICENSE", b"Wenzel Jakob", b"Wenzel Jakob"),
}


Expand Down
53 changes: 53 additions & 0 deletions tests/mwrap/nanobind-embed/build.mcpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Point the consumer at the declared xim:python SDK: its headers, its link
// library and, for the embedded interpreter, the home passed to PyConfig.
import std;
import mcpp;

namespace {

namespace fs = std::filesystem;

bool has_python_h(const fs::path& dir) {
std::error_code ec;
return fs::is_regular_file(dir / "Python.h", ec);
}

} // namespace

int main() {
const fs::path python = mcpp::xpkg_dir("xim", "python");
if (python.empty() || !fs::is_directory(python)) {
std::println(std::cerr, "nanobind-embed: the declared xim:python SDK is unavailable");
return 1;
}
const bool windows = std::string_view(mcpp::target_os()) == "windows";
const fs::path headers = windows ? python / "include" : python / "include" / "python3.13";
const fs::path libraries = windows ? python / "libs" : python / "lib";
if (!has_python_h(headers)) {
std::println(std::cerr, "nanobind-embed: no Python.h under {}", headers.string());
return 1;
}
if (!fs::is_directory(libraries)) {
std::println(std::cerr, "nanobind-embed: no Python library directory at {}", libraries.string());
return 1;
}

mcpp::include_dir(headers.string().c_str());
mcpp::link_search(libraries.string().c_str());
mcpp::link_lib(windows ? "python313" : "python3.13");
mcpp::runtime_search_dir((windows ? python : libraries).string().c_str());

const fs::path config_dir = fs::path(mcpp::out_dir()) / "python-sdk";
std::error_code ec;
fs::create_directories(config_dir, ec);
std::ofstream config(config_dir / "python_sdk.h", std::ios::binary | std::ios::trunc);
config << "#define MCPP_PYTHON_HOME R\"SDK(" << python.generic_string() << ")SDK\"\n";
config.close();
if (!config) {
std::println(std::cerr, "nanobind-embed: cannot write python_sdk.h");
return 1;
}
mcpp::include_dir(config_dir.string().c_str());
mcpp::rerun_if_changed("build.mcpp");
return 0;
}
13 changes: 13 additions & 0 deletions tests/mwrap/nanobind-embed/mcpp.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "nanobind-embed"
version = "3.1.0"
description = "Consume the immutable nanobind wrapper: embedded CPython module with NB_MODULE, STL casters, classes, exceptions, ndarray and version constants"

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

[dev-dependencies]
mcpplibs.mtest = "0.11.3"

[xlings.workspace]
"xim:python" = "3.13.12"
108 changes: 108 additions & 0 deletions tests/mwrap/nanobind-embed/tests/behavior.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <mwrap/nanobind/macros.hpp>
#include <python_sdk.h>

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

using namespace mcpplibs::mtest;
namespace nb = nanobind;

namespace {

struct Point {
double x = 0, y = 0;
double norm() const { return std::sqrt(x * x + y * y); }
};

std::string join_words(const std::vector<std::string>& words) {
std::string result;
for (const std::string& word : words) {
if (!result.empty()) result += ' ';
result += word;
}
return result;
}

void fail_negative(int value) {
if (value < 0) throw nb::value_error("negative value");
}

} // namespace

NB_MODULE(nanobind_embed, m) {
m.doc() = "nanobind-embed: the immutable nanobind wrapper";
m.def("add", [](int a, int b) { return a + b; }, nb::arg("a"), nb::arg("b"));
m.def("join_words", &join_words, nb::arg("words"));
m.def("fail_negative", &fail_negative, nb::arg("value"));
m.def("array_sum", [](nb::ndarray<const float, nb::ndim<1>> values) {
double total = 0;
for (std::size_t i = 0; i < values.shape(0); ++i) total += values(i);
return total;
}, nb::arg("values"));
nb::class_<Point>(m, "Point")
.def(nb::init<double, double>(), nb::arg("x") = 0, nb::arg("y") = 0)
.def_rw("x", &Point::x)
.def_rw("y", &Point::y)
.def("norm", &Point::norm);
}

UTest behavior{"behavior", [] {
expect(fatal(PyImport_AppendInittab("nanobind_embed", PyInit_nanobind_embed) == 0))
<< "PyImport_AppendInittab failed";

PyConfig config;
PyConfig_InitPythonConfig(&config);
PyStatus status = PyConfig_SetBytesString(&config, &config.home, MCPP_PYTHON_HOME);
if (!PyStatus_Exception(status)) {
config.parse_argv = 0;
config.install_signal_handlers = 0;
status = Py_InitializeFromConfig(&config);
}
PyConfig_Clear(&config);
expect(fatal(!PyStatus_Exception(status))) << (status.err_msg ? status.err_msg : "unknown PyStatus");

struct Guard {
~Guard() {
if (Py_IsInitialized()) Py_Finalize();
}
} guard;

"module, functions and stl casters"_test = [] {
expect(eq(PyRun_SimpleString(
"import nanobind_embed as b\n"
"assert b.add(2, 3) == 5\n"
"assert b.add(a=10, b=32) == 42\n"
"assert b.join_words(['hello', 'nanobind', 'world']) == 'hello nanobind world'\n"),
0));
};

"classes and exceptions"_test = [] {
expect(eq(PyRun_SimpleString(
"import nanobind_embed as b\n"
"p = b.Point(3, 4)\n"
"assert p.x == 3 and p.y == 4\n"
"assert abs(p.norm() - 5.0) < 1e-12\n"
"p.x = 6\n"
"assert p.x == 6\n"
"try:\n"
" b.fail_negative(-1)\n"
"except ValueError as error:\n"
" assert str(error) == 'negative value'\n"
"else:\n"
" raise AssertionError('expected ValueError')\n"
"b.fail_negative(1)\n"),
0));
};

"ndarray through the buffer protocol"_test = [] {
expect(eq(PyRun_SimpleString(
"import array\n"
"import nanobind_embed as b\n"
"values = array.array('f', [1.0, 2.0, 3.0])\n"
"assert abs(b.array_sum(values) - 6.0) < 1e-12\n"),
0));
};
}};
17 changes: 17 additions & 0 deletions tests/mwrap/nanobind-embed/tests/macros.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import mcpplibs.mtest;
import mcpplibs.mwrap.nanobind.macros;

using namespace mcpplibs::mtest;
namespace macros = mcpplibs::mwrap::nanobind::macros;

static_assert(macros::NB_VERSION_MAJOR == 3);
static_assert(macros::NB_VERSION_MINOR == 1);
static_assert(macros::NB_VERSION_PATCH == 0);
static_assert(macros::NB_VERSION_DEV == 0);

UTest versions{"versions", [] {
expect(eq(macros::NB_VERSION_MAJOR, 3));
expect(eq(macros::NB_VERSION_MINOR, 1));
expect(eq(macros::NB_VERSION_PATCH, 0));
expect(eq(macros::NB_VERSION_DEV, 0));
}};
Loading