## 1. 背景与动机 在 AI / 科学计算 / 异构算子开发场景下,Python 负责上层编排、C++ 负责底层计算是高频刚需。`mcpp-plugins` 目前已经原生支持了 `rules-cuda`、`rules-hip`、`rules-sycl`、`rules-ascendc` 等异构编程规则;如果能打通 **C++23 模块 → Python 原生扩展模块** 的构建链路,就可以用一套 `mcpp.toml` + `build.mcpp` 把纯 C++23 模块(乃至 GPU/NPU 算子)直接编译为 Python 可 `import` 的扩展库。 传统 C++ 写 Python 扩展(CMake + scikit-build + 手动配 Python ABI/头文件)配置繁琐;借助 `mcpp` 的工具链声明(如 `uv`)与 `build.mcpp` 构建插件机制,有望把跨语言构建体验做到接近 Rust `maturin` 的零配置水准。 --- ## 2. 预期用户侧体验(示意) > 注:以下模块名与配置项为概念示意,具体命名按本仓家族规范调整。 **`mcpp.toml`**(声明插件 feature 与 `uv` 等外部工具,无需手工配环境): ```toml [package] name = "fast_ops" version = "0.1.0" [build-dependencies.mcpp] plugins = { version = "0.27.0", features = ["rules-python"], host-module = true } ``` build.mcpp: ```cpp import std; import mcpp; import mcpp.rules.python; // 或 mcpp.tools.python,待定 int main() { mcpp::rules::python::options opt; opt.module_name = "fast_ops"; // Python 侧 import 的模块名 opt.python_version = "3.12"; // 交由 uv 解析/拉取对应 Python 环境 opt.binding = "nanobind"; // 默认 nanobind,同时支持 "raw"(纯 Python C-API) return mcpp::rules::python::configure(opt) ? 0 : 1; } ``` 构建完成后,用户可直接运行验证: ```bash mcpp build uv run python -c "import fast_ops; print(fast_ops.add(1, 2))" ```
Activity
Shinonome-Ryoko14 commentedon Nov 28, 2026
I'd like to take this on and work on the implementation!
Here is my initial plan for the first milestone:
mcpp-pluginsintegration mechanism andbuild.mcppschemananobind+uv) interop@Sunrisepeak Could you please assign this issue to me? Any feedback on the RFC design before I start coding is also welcome.
Shinonome-Ryoko14 commentedon Nov 28, 2026
综合对比了目前主流的跨语言方案(Rust 的
PyO3 + maturin/Mozilla UniFFI,以及 C++ 的nanobind + scikit-build-core),我打算按分层演进、V1 零外部构建依赖的策略来落地,彻底省略uv:方案定位与选型结论
export module ...),零 Python 侵入,C++ 侧照常import;Python FFI 桥接层由构建插件在build.mcpp中自动生成。mcpp.tools.island思路):uv、Python.h或libpython,产物为标准共享库(.so/.dll/.dylib),天然兼容mcpp的跨平台与交叉编译,一份产物通吃所有 Python 3 版本。extern "C"桥接源文件(自动编译进共享库,处理 Windows__declspec(dllexport)/ Linux 符号导出);.py包装文件(使用标准库ctypes加载同目录共享库)。options中预留backend枚举,后续可平滑扩展到原生 CPython Stable ABI (abi3) /nanobind后端(面向 NumPy / DLPack 零拷贝与高频计算场景),以及接入 AST/接口扫描实现零声明全自动提取。V1 最小交付边界
i32/i64/f64/bool/string)的函数导出;在tests/新增 consumer 工程,同时验证 C++ 可执行文件import调用与python3导入调用通过。.whl打包(留待后续迭代)。如果没有异议,我先按
tools/island.cppm和docs/plugin-development.md的规范推进,跑通 Linux 端 MVP 后先提交一个关联Refs #68的 Draft PR 供 Review,再补齐 Windows 端适配。