From d97272d0192c665e453ba60fca687b992a35bb25 Mon Sep 17 00:00:00 2001 From: Shinonome-Ryoko14 Date: Mon, 30 Nov 2026 01:09:04 +0800 Subject: [PATCH] feat(compat): add compat.robin-map 1.4.1 and compat.nanobind 3.1.0 --- mcpp.toml | 2 + pkgs/c/compat.nanobind.lua | 148 ++++++++++++++++++ pkgs/c/compat.robin-map.lua | 75 +++++++++ tests/examples/nanobind/build.mcpp | 37 +++++ tests/examples/nanobind/mcpp.toml | 12 ++ tests/examples/nanobind/src/nb_runtime.cpp | 4 + .../examples/nanobind/tests/embed_module.cpp | 116 ++++++++++++++ tests/examples/robin-map/mcpp.toml | 9 ++ tests/examples/robin-map/tests/smoke.cpp | 60 +++++++ 9 files changed, 463 insertions(+) create mode 100644 pkgs/c/compat.nanobind.lua create mode 100644 pkgs/c/compat.robin-map.lua create mode 100644 tests/examples/nanobind/build.mcpp create mode 100644 tests/examples/nanobind/mcpp.toml create mode 100644 tests/examples/nanobind/src/nb_runtime.cpp create mode 100644 tests/examples/nanobind/tests/embed_module.cpp create mode 100644 tests/examples/robin-map/mcpp.toml create mode 100644 tests/examples/robin-map/tests/smoke.cpp diff --git a/mcpp.toml b/mcpp.toml index b3c1e832..6cf3014e 100644 --- a/mcpp.toml +++ b/mcpp.toml @@ -186,6 +186,7 @@ members = [ "tests/examples/redis-plus-plus", "tests/examples/redis-plus-plus-v133", "tests/examples/redis-plus-plus-async", + "tests/examples/robin-map", "tests/examples/safeint", "tests/examples/safeint-c-api", "tests/examples/sdl2", @@ -291,6 +292,7 @@ members = [ "tests/examples/etl", "tests/examples/galay", "tests/examples/miniaudio", + "tests/examples/nanobind", "tests/examples/nanosvg", "tests/examples/nanodbc", "tests/examples/plf-hive", diff --git a/pkgs/c/compat.nanobind.lua b/pkgs/c/compat.nanobind.lua new file mode 100644 index 00000000..e1410ff8 --- /dev/null +++ b/pkgs/c/compat.nanobind.lua @@ -0,0 +1,148 @@ +-- compat.nanobind — wjakob/nanobind, a small binding library for Python. +-- +-- Shape B: the package installs upstream's `include/` and `src/`, but compiles +-- no C++ itself. The Python SDK must come from the consumer (xim:python, uv or +-- a host interpreter), because the runtime source embedded by +-- `src/nb_combined.cpp` has to be built with exactly that interpreter's ABI: +-- version, free-threaded configuration, Py_LIMITED_API and visibility flags. +-- This is the same rule compat.pybind11 follows: no Python.h here, ever. +-- +-- WHAT THE CONSUMER DOES. A project that wants the nanobind runtime includes +-- the generated forwarder in exactly ONE translation unit: +-- +-- #include +-- +-- and compiles that TU with its own Python SDK on the include path. The +-- forwarder lives at `/mcpp_generated/include/` and reaches the +-- runtime at `/src/nb_combined.cpp` through `../../src/` +-- (`/mcpp_generated/include/../../src` == `/src`), the same +-- relative-layout pattern compat.quickjs-ng uses for its one header. This +-- package also exposes plain `/include`, so bindings include +-- `` and the `nanobind/stl/*.h` casters normally. +-- +-- WHY install(). The release unpacks to `nanobind-/`. Moving that +-- directory to be the install dir itself (the compat.quickjs-ng hook) leaves +-- no version in any path of the descriptor, so a new release needs no edit +-- here. The hook checks `include/nanobind/nanobind.h` and +-- `src/nb_combined.cpp` before declaring success. +-- +-- C++ STANDARD. Upstream's `src/nb_combined.cpp` is documented for C++17 and +-- newer and builds unchanged as C++23: it includes the sibling `src/*.cpp` +-- files by quote and needs no extra source-version macro. Its own comment +-- recommends `-fno-strict-aliasing` for the libnanobind part and +-- `-fvisibility=hidden`; those belong to the consumer's compile line (the +-- consumer controls the Python ABI flags too), not to this header-only +-- package, whose only compiled TU is the C anchor below. +-- +-- ROBIN-MAP. wjakob/nanobind keeps `ext/robin_map` as a git submodule, and the +-- GitHub release archive leaves that directory empty. compat.robin-map is a +-- first-class dependency instead, so `` reaches the consumer +-- through the normal dependency include path and install() never downloads +-- anything from the network. +-- +-- NO CN MIRROR. The url is upstream's tag archive as a plain string, the +-- fallback docs/cn-mirror.md describes for a contributor without mcpp-res write +-- access. A maintainer can turn it into a GLOBAL/CN table later; the sha256 +-- does not change. +-- +-- LICENSES. BSD-3-Clause (LICENSE); the bundled ext/robin_map submodule would be +-- MIT, but release archives ship it empty and this package depends on +-- compat.robin-map (MIT) for those headers instead. +package = { + spec = "1", + namespace = "compat", + name = "nanobind", + description = "Seamless operability between C++17 and Python (runtime source embedded by the consumer)", + licenses = {"BSD-3-Clause"}, + repo = "https://github.com/wjakob/nanobind", + type = "package", + + extensions = { + autoupdate = { + tag = "v${version}", + url = "https://github.com/wjakob/nanobind/archive/refs/tags/${tag}.tar.gz", + }, + }, + + xpm = { + linux = { + ["3.1.0"] = { + url = "https://github.com/wjakob/nanobind/archive/refs/tags/v3.1.0.tar.gz", + sha256 = "39659890a112e84e7263e4a263c71dd2621f866f7a4386ce95ef410e6c0e5c2f", + }, + }, + macosx = { + ["3.1.0"] = { + url = "https://github.com/wjakob/nanobind/archive/refs/tags/v3.1.0.tar.gz", + sha256 = "39659890a112e84e7263e4a263c71dd2621f866f7a4386ce95ef410e6c0e5c2f", + }, + }, + windows = { + ["3.1.0"] = { + url = "https://github.com/wjakob/nanobind/archive/refs/tags/v3.1.0.tar.gz", + sha256 = "39659890a112e84e7263e4a263c71dd2621f866f7a4386ce95ef410e6c0e5c2f", + }, + }, + }, + + mcpp = { + language = "c++23", + import_std = false, + c_standard = "c11", + include_dirs = { "include", "mcpp_generated/include" }, + generated_files = { + ["mcpp_generated/nanobind_anchor.c"] = [==[ +int mcpp_nanobind_headers_anchor(void) { return 0; } +]==], + -- Compiled by exactly one consumer TU per Python extension or + -- embedding binary, with that consumer's Python SDK. The relative + -- path is the install layout: /mcpp_generated/include/ + -- -> /src/. + ["mcpp_generated/include/mcpp_nanobind_combined.hpp"] = [==[ +#pragma once +// Compile this header in exactly ONE translation unit per Python extension +// or embedding binary, with on the include path. +#include "../../src/nb_combined.cpp" +]==], + }, + sources = { "mcpp_generated/nanobind_anchor.c" }, + targets = { ["nanobind"] = { kind = "lib" } }, + deps = { ["compat.robin-map"] = "1.4.1" }, + }, +} + +import("xim.libxpkg.pkginfo") +import("xim.libxpkg.log") + +-- Once a descriptor defines install(), the archive is extracted to a sibling of +-- the install dir rather than into it; moving it is the descriptor's job. +-- The release unpacks to `nanobind-/`. Moving that directory to be the +-- install dir itself leaves no version in any path of the descriptor, so a new +-- release needs no edit here. +function install() + local v = pkginfo.version() + local srcroot + for _, name in ipairs({ "nanobind-" .. v, "nanobind-v" .. v }) do + if os.isfile(path.join(name, "include", "nanobind", "nanobind.h")) + and os.isfile(path.join(name, "src", "nb_combined.cpp")) then + srcroot = name + break + end + end + if not srcroot then + log.error("[nanobind] extracted tree not found (looked for nanobind-%s / nanobind-v%s)", v, v) + return false + end + os.tryrm(pkginfo.install_dir()) + os.mv(srcroot, pkginfo.install_dir()) + local install_dir = pkginfo.install_dir() + if not os.isfile(path.join(install_dir, "include", "nanobind", "nanobind.h")) then + log.error("[nanobind] include/nanobind/nanobind.h missing after install") + return false + end + if not os.isfile(path.join(install_dir, "src", "nb_combined.cpp")) then + log.error("[nanobind] src/nb_combined.cpp missing after install") + return false + end + return true +end diff --git a/pkgs/c/compat.robin-map.lua b/pkgs/c/compat.robin-map.lua new file mode 100644 index 00000000..faf30727 --- /dev/null +++ b/pkgs/c/compat.robin-map.lua @@ -0,0 +1,75 @@ +-- compat.robin-map — Tessil/robin-map, a header-only hash map and hash set +-- (`tsl::robin_map`, `tsl::robin_set`). +-- +-- Shape B: the public payload is `include/tsl/*.h`, so the package installs +-- the release tree and exposes `*/include` (the version directory is still in +-- the path before install, exactly like compat.pybind11). Its own build is a +-- one-line C anchor: mcpp needs at least one source to make the `robin-map` +-- library target, and a header-only library would otherwise have nothing to +-- compile. The anchor is never linked by a consumer that only wants headers. +-- +-- WHY THIS IS ITS OWN PACKAGE. wjakob/nanobind carries `ext/robin_map` as a +-- git submodule, and GitHub's release tarballs leave that directory empty. +-- compat.nanobind therefore declares this package as a dependency instead of +-- downloading a submodule from inside install(), which specs forbid. A +-- consumer that uses nanobind's headers on their own gets the same headers +-- through the normal dependency graph. +-- +-- NO CN MIRROR. The url is upstream's tag archive as a plain string, the +-- fallback docs/cn-mirror.md describes for a contributor without mcpp-res write +-- access. A maintainer can turn it into a GLOBAL/CN table later; the sha256 +-- does not change. +-- +-- LICENSES. MIT (LICENSE). +package = { + spec = "1", + namespace = "compat", + name = "robin-map", + description = "A C++ implementation of a fast hash map and hash set using robin hood hashing", + licenses = {"MIT"}, + repo = "https://github.com/Tessil/robin-map", + type = "package", + + extensions = { + autoupdate = { + tag = "v${version}", + url = "https://github.com/Tessil/robin-map/archive/refs/tags/${tag}.tar.gz", + }, + }, + + xpm = { + linux = { + ["1.4.1"] = { + url = "https://github.com/Tessil/robin-map/archive/refs/tags/v1.4.1.tar.gz", + sha256 = "0e3f53a377fdcdc5f9fed7a4c0d4f99e82bbb64175233bd13427fef9a771f4a1", + }, + }, + macosx = { + ["1.4.1"] = { + url = "https://github.com/Tessil/robin-map/archive/refs/tags/v1.4.1.tar.gz", + sha256 = "0e3f53a377fdcdc5f9fed7a4c0d4f99e82bbb64175233bd13427fef9a771f4a1", + }, + }, + windows = { + ["1.4.1"] = { + url = "https://github.com/Tessil/robin-map/archive/refs/tags/v1.4.1.tar.gz", + sha256 = "0e3f53a377fdcdc5f9fed7a4c0d4f99e82bbb64175233bd13427fef9a771f4a1", + }, + }, + }, + + mcpp = { + language = "c++23", + import_std = false, + c_standard = "c11", + include_dirs = { "*/include" }, + generated_files = { + ["mcpp_generated/robin_map_anchor.c"] = [==[ +int mcpp_robin_map_headers_anchor(void) { return 0; } +]==], + }, + sources = { "mcpp_generated/robin_map_anchor.c" }, + targets = { ["robin-map"] = { kind = "lib" } }, + deps = {}, + }, +} diff --git a/tests/examples/nanobind/build.mcpp b/tests/examples/nanobind/build.mcpp new file mode 100644 index 00000000..8b35e277 --- /dev/null +++ b/tests/examples/nanobind/build.mcpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include + +import mcpp; + +int main() { + namespace fs = std::filesystem; + const fs::path python = mcpp::xpkg_dir("xim", "python"); + if (python.empty() || !fs::is_directory(python)) { + std::fputs("the declared xim:python SDK is unavailable\n", stderr); + return 1; + } + const bool windows = std::string(mcpp::target_os()) == "windows"; + const fs::path headers = python / "include" / (windows ? "" : "python3.13"); + const fs::path libraries = python / (windows ? "libs" : "lib"); + if (!fs::exists(headers / "Python.h")) 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((python / (windows ? "" : "lib")).string().c_str()); + // src/nb_runtime.cpp compiles nanobind's combined runtime into this project. + // Upstream's own build recommends this for the libnanobind part; the flag + // belongs on the consumer's compile line, not in the header-only package. + if (!windows) mcpp::cxxflag("-fno-strict-aliasing"); + // Resolve the standard library in the same SDK when embedding Python. + const fs::path config_dir = fs::absolute(".mcpp/python-sdk"); + fs::create_directories(config_dir); + std::ofstream config(config_dir / "python_sdk.h"); + config << "#define MCPP_PYTHON_HOME R\"SDK(" << python.generic_string() << ")SDK\"\n"; + config.close(); + if (!config) return 1; + mcpp::include_dir(config_dir.string().c_str()); + mcpp::rerun_if_changed("build.mcpp"); + return 0; +} diff --git a/tests/examples/nanobind/mcpp.toml b/tests/examples/nanobind/mcpp.toml new file mode 100644 index 00000000..7c7f1f32 --- /dev/null +++ b/tests/examples/nanobind/mcpp.toml @@ -0,0 +1,12 @@ +[package] +name = "nanobind-tests" +version = "0.1.0" + +[dependencies.compat] +nanobind = "3.1.0" + +[indices] +compat = { path = "../../.." } + +[xlings.workspace] +"xim:python" = "3.13.12" diff --git a/tests/examples/nanobind/src/nb_runtime.cpp b/tests/examples/nanobind/src/nb_runtime.cpp new file mode 100644 index 00000000..4983f489 --- /dev/null +++ b/tests/examples/nanobind/src/nb_runtime.cpp @@ -0,0 +1,4 @@ +// compat.nanobind — the runtime half of the test, in its own translation unit. +// The forwarder must be compiled exactly once per extension or embedding +// binary; tests/embed_module.cpp includes only the public nanobind headers. +#include diff --git a/tests/examples/nanobind/tests/embed_module.cpp b/tests/examples/nanobind/tests/embed_module.cpp new file mode 100644 index 00000000..2d0d9996 --- /dev/null +++ b/tests/examples/nanobind/tests/embed_module.cpp @@ -0,0 +1,116 @@ +// compat.nanobind — expose a module to an embedded interpreter, then let Python +// exercise every advertised surface: a free function with named arguments, STL +// conversion, a class with a read-only property, and exception translation. +// +// The nanobind runtime (src/nb_combined.cpp) is compiled in src/nb_runtime.cpp, +// not here: NB_MODULE needs the headers only, exactly as a real extension does. +#include +#include +#include + +#include + +#include +#include +#include +#include + +namespace nb = nanobind; + +namespace { + +int add(int a, int b) { return a + b; } + +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; +} + +class Counter { +public: + explicit Counter(int start = 0) : value_(start) {} + int inc(int amount) { + value_ += amount; + return value_; + } + int value() const { return value_; } + +private: + int value_; +}; + +void fail_on_negative(int x) { + if (x < 0) throw std::runtime_error("negative value"); +} + +} // namespace + +NB_MODULE(nb_smoke, m) { + m.doc() = "compat.nanobind smoke test"; + m.def("add", &add, nb::arg("a"), nb::arg("b")); + m.def("join_words", &join_words, nb::arg("words")); + nb::class_(m, "Counter") + .def(nb::init(), nb::arg("start") = 0) + .def("inc", &Counter::inc, nb::arg("amount")) + .def_prop_ro("value", &Counter::value); + m.def("fail_on_negative", &fail_on_negative, nb::arg("x")); +} + +namespace { + +constexpr const char* kScript = R"PYTHON( +import nb_smoke + +assert nb_smoke.add(2, 3) == 5 +assert nb_smoke.add(a=10, b=32) == 42 +assert nb_smoke.join_words(["hello", "nanobind", "world"]) == "hello nanobind world" + +counter = nb_smoke.Counter(10) +assert counter.value == 10 +assert counter.inc(5) == 15 +assert counter.value == 15 + +try: + nb_smoke.fail_on_negative(-1) +except RuntimeError as error: + assert "negative" in str(error), str(error) +else: + raise AssertionError("expected RuntimeError") + +print("nanobind OK") +)PYTHON"; + +} // namespace + +int main() { + if (PyImport_AppendInittab("nb_smoke", &PyInit_nb_smoke) < 0) { + std::fputs("PyImport_AppendInittab failed\n", stderr); + return 1; + } + + PyConfig config; + PyConfig_InitPythonConfig(&config); + PyStatus status = PyConfig_SetBytesString(&config, &config.home, MCPP_PYTHON_HOME); + if (!PyStatus_Exception(status)) status = Py_InitializeFromConfig(&config); + PyConfig_Clear(&config); + if (PyStatus_Exception(status)) { + std::fputs("Python initialization failed\n", stderr); + return 1; + } + + const int script = PyRun_SimpleString(kScript); + const int finalized = Py_FinalizeEx(); + if (script != 0) { + std::fputs("the embedded Python assertions failed\n", stderr); + return 1; + } + if (finalized != 0) { + std::fputs("Py_FinalizeEx reported live references\n", stderr); + return 1; + } + return 0; +} diff --git a/tests/examples/robin-map/mcpp.toml b/tests/examples/robin-map/mcpp.toml new file mode 100644 index 00000000..f70c2d51 --- /dev/null +++ b/tests/examples/robin-map/mcpp.toml @@ -0,0 +1,9 @@ +[package] +name = "robin-map-tests" +version = "0.1.0" + +[dependencies.compat] +robin-map = "1.4.1" + +[indices] +compat = { path = "../../.." } diff --git a/tests/examples/robin-map/tests/smoke.cpp b/tests/examples/robin-map/tests/smoke.cpp new file mode 100644 index 00000000..1b7879d5 --- /dev/null +++ b/tests/examples/robin-map/tests/smoke.cpp @@ -0,0 +1,60 @@ +// compat.robin-map — exercise the two containers the package exists for: +// tsl::robin_map and tsl::robin_set. The checks assert observable behaviour +// (lookup, iteration, erasure, rehashing), not merely that the headers compile. +#include +#include + +#include +#include +#include + +int main() { + bool ok = true; + auto check = [&](bool cond, const char* what) { + if (!cond) { + std::fprintf(stderr, "FAIL: %s\n", what); + ok = false; + } + }; + + // ---- robin_map ------------------------------------------------------- + tsl::robin_map map; + for (int i = 0; i < 1000; ++i) map.emplace("k" + std::to_string(i), i); + check(map.size() == 1000, "robin_map size after 1000 emplaces"); + check(map.at("k0") == 0 && map.at("k999") == 999, "robin_map lookup"); + check(map.find("absent") == map.end(), "robin_map miss returns end()"); + + // Rehashing must not lose or corrupt entries. + long sum = 0; + for (const auto& [key, value] : map) { + (void)key; + sum += value; + } + check(sum == 999L * 1000 / 2, "robin_map values survive rehashing"); + + check(map.erase("k500") == 1, "robin_map erase reports one removal"); + check(map.erase("k500") == 0, "robin_map second erase reports none"); + check(map.size() == 999, "robin_map size after erase"); + check(!map.contains("k500"), "robin_map erased key is gone"); + + map["k1000"] = 1000; + check(map["k1000"] == 1000, "robin_map operator[] inserts"); + check(map.count("k1000") == 1, "robin_map count finds inserted key"); + + // ---- robin_set ------------------------------------------------------- + tsl::robin_set set; + for (int value : {42, 7, 99, 1, 63, 7}) set.insert(value); + check(set.size() == 5, "robin_set drops the duplicate"); + check(set.contains(63), "robin_set contains"); + check(set.find(100) == set.end(), "robin_set miss returns end()"); + + std::vector seen; + for (int value : set) seen.push_back(value); + check(seen.size() == set.size(), "robin_set iteration visits every element"); + check(set.erase(99) == 1, "robin_set erase reports one removal"); + check(set.erase(99) == 0, "robin_set second erase reports none"); + check(!set.contains(99), "robin_set erased key is gone"); + + if (ok) std::puts("robin-map OK"); + return ok ? 0 : 1; +}