From b311d14818ccbaacb00bdc576809e2a0f3445840 Mon Sep 17 00:00:00 2001 From: Shinonome-Ryoko14 Date: Thu, 3 Dec 2026 00:49:09 +0800 Subject: [PATCH] feat(mwrap): add mcpplibs.mwrap.nanobind 3.1.0 --- mcpp.toml | 1 + pkgs/mwrap/nanobind.lua | 38 ++++++ tests/mwrap/check_installed_template.py | 1 + tests/mwrap/nanobind-embed/build.mcpp | 53 +++++++++ tests/mwrap/nanobind-embed/mcpp.toml | 13 +++ tests/mwrap/nanobind-embed/tests/behavior.cpp | 108 ++++++++++++++++++ tests/mwrap/nanobind-embed/tests/macros.cpp | 17 +++ 7 files changed, 231 insertions(+) create mode 100644 pkgs/mwrap/nanobind.lua create mode 100644 tests/mwrap/nanobind-embed/build.mcpp create mode 100644 tests/mwrap/nanobind-embed/mcpp.toml create mode 100644 tests/mwrap/nanobind-embed/tests/behavior.cpp create mode 100644 tests/mwrap/nanobind-embed/tests/macros.cpp diff --git a/mcpp.toml b/mcpp.toml index 5040d1b7..522765d5 100644 --- a/mcpp.toml +++ b/mcpp.toml @@ -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", diff --git a/pkgs/mwrap/nanobind.lua b/pkgs/mwrap/nanobind.lua new file mode 100644 index 00000000..28a1419b --- /dev/null +++ b/pkgs/mwrap/nanobind.lua @@ -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", + }, + }, + }, +} diff --git a/tests/mwrap/check_installed_template.py b/tests/mwrap/check_installed_template.py index 4897e472..93a19c06 100644 --- a/tests/mwrap/check_installed_template.py +++ b/tests/mwrap/check_installed_template.py @@ -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"), } diff --git a/tests/mwrap/nanobind-embed/build.mcpp b/tests/mwrap/nanobind-embed/build.mcpp new file mode 100644 index 00000000..aa3e28b0 --- /dev/null +++ b/tests/mwrap/nanobind-embed/build.mcpp @@ -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; +} diff --git a/tests/mwrap/nanobind-embed/mcpp.toml b/tests/mwrap/nanobind-embed/mcpp.toml new file mode 100644 index 00000000..2aa29806 --- /dev/null +++ b/tests/mwrap/nanobind-embed/mcpp.toml @@ -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" diff --git a/tests/mwrap/nanobind-embed/tests/behavior.cpp b/tests/mwrap/nanobind-embed/tests/behavior.cpp new file mode 100644 index 00000000..85172f40 --- /dev/null +++ b/tests/mwrap/nanobind-embed/tests/behavior.cpp @@ -0,0 +1,108 @@ +#include +#include + +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& 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> 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_(m, "Point") + .def(nb::init(), 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)); + }; +}}; diff --git a/tests/mwrap/nanobind-embed/tests/macros.cpp b/tests/mwrap/nanobind-embed/tests/macros.cpp new file mode 100644 index 00000000..822b4e35 --- /dev/null +++ b/tests/mwrap/nanobind-embed/tests/macros.cpp @@ -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)); +}};