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
2 changes: 2 additions & 0 deletions mcpp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
148 changes: 148 additions & 0 deletions pkgs/c/compat.nanobind.lua
Original file line number Diff line number Diff line change
@@ -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 <mcpp_nanobind_combined.hpp>
--
-- and compiles that TU with its own Python SDK on the include path. The
-- forwarder lives at `<install>/mcpp_generated/include/` and reaches the
-- runtime at `<install>/src/nb_combined.cpp` through `../../src/`
-- (`<install>/mcpp_generated/include/../../src` == `<install>/src`), the same
-- relative-layout pattern compat.quickjs-ng uses for its one header. This
-- package also exposes plain `<install>/include`, so bindings include
-- `<nanobind/nanobind.h>` and the `nanobind/stl/*.h` casters normally.
--
-- WHY install(). The release unpacks to `nanobind-<version>/`. 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 `<tsl/robin_map.h>` 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: <install>/mcpp_generated/include/
-- -> <install>/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 <Python.h> 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-<version>/`. 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
75 changes: 75 additions & 0 deletions pkgs/c/compat.robin-map.lua
Original file line number Diff line number Diff line change
@@ -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 = {},
},
}
37 changes: 37 additions & 0 deletions tests/examples/nanobind/build.mcpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <string>

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;
}
12 changes: 12 additions & 0 deletions tests/examples/nanobind/mcpp.toml
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 4 additions & 0 deletions tests/examples/nanobind/src/nb_runtime.cpp
Original file line number Diff line number Diff line change
@@ -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 <mcpp_nanobind_combined.hpp>
Loading
Loading