Skip to content

[RFC] 新增 Python 扩展混编插件方案(基于 uv + nanobind,支持 C++23 模块一键导出给 Python) #68

In review

Description

@Shinonome-Ryoko14

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 等外部工具,无需手工配环境):

[package]
name = "fast_ops"
version = "0.1.0"

[build-dependencies.mcpp]
plugins = { version = "0.27.0", features = ["rules-python"], host-module = true }

build.mcpp:

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;
}

构建完成后,用户可直接运行验证:

mcpp build
uv run python -c "import fast_ops; print(fast_ops.add(1, 2))"

Activity

  1. Shinonome-Ryoko14 commented on Nov 28, 2026

    @Shinonome-Ryoko14
    MemberAuthor

    I'd like to take this on and work on the implementation!

    Here is my initial plan for the first milestone:

    • Verify the mcpp-plugins integration mechanism and build.mcpp schema
    • Build a minimal prototype demonstrating C++23 / Python (nanobind + uv) interop

    @Sunrisepeak Could you please assign this issue to me? Any feedback on the RFC design before I start coding is also welcome.

  2. self-assigned this
    on Nov 28, 2026
  3. added
    enhancement
    on Nov 28, 2026
  4. Shinonome-Ryoko14 commented on Nov 28, 2026

    @Shinonome-Ryoko14
    MemberAuthor

    综合对比了目前主流的跨语言方案(Rust 的 PyO3 + maturin / Mozilla UniFFI,以及 C++ 的 nanobind + scikit-build-core),我打算按分层演进、V1 零外部构建依赖的策略来落地,彻底省略 uv:

    方案定位与选型结论

    • 核心理念(双接口共存):库源码保持纯 C++23 模块(export module ...),零 Python 侵入,C++ 侧照常 import;Python FFI 桥接层由构建插件在 build.mcpp 中自动生成。
    • V1 路线(类 UniFFI / 复用 mcpp.tools.island 思路):
      • 构建期不依赖 uv、Python.h 或 libpython,产物为标准共享库(.so / .dll / .dylib),天然兼容 mcpp 的跨平台与交叉编译,一份产物通吃所有 Python 3 版本。
      • 插件根据声明自动生成两份文件:
        1. C++ 侧的 extern "C" 桥接源文件(自动编译进共享库,处理 Windows __declspec(dllexport) / Linux 符号导出);
        2. Python 侧带完整 Type Hints 的 .py 包装文件(使用标准库 ctypes 加载同目录共享库)。
    • V2 预留扩展:在 options 中预留 backend 枚举,后续可平滑扩展到原生 CPython Stable ABI (abi3) / nanobind 后端(面向 NumPy / DLPack 零拷贝与高频计算场景),以及接入 AST/接口扫描实现零声明全自动提取。

    V1 最小交付边界

    • 包含:支持基础标量与字符串类型(i32 / i64 / f64 / bool / string)的函数导出;在 tests/ 新增 consumer 工程,同时验证 C++ 可执行文件 import 调用与 python3 导入调用通过。
    • 暂不包含:完整 C++23 源码解析器、复杂类/STL 容器自动映射、.whl 打包(留待后续迭代)。

    如果没有异议,我先按 tools/island.cppm 和 docs/plugin-development.md 的规范推进,跑通 Linux 端 MVP 后先提交一个关联 Refs #68 的 Draft PR 供 Review,再补齐 Windows 端适配。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions