Skip to content

Add a C ABI library option to the rust template - #218

Merged
python-templates-automerge[bot] merged 4 commits into
mainfrom
rust-c-abi
Nov 11, 2026
Merged

python-templates-automerge[bot] merged 4 commits into
mainfrom
rust-c-abi

Conversation

@timkpaine

Copy link
Copy Markdown
Member

Description

add_c_abi (default false, rust only) generates the split-library layout from the templates roadmap, the shape fsspec-rs now uses:

  • rust/capi builds lib<module>_capi, an extern "C" surface over the Rust crate with opaque handles, paired create/free functions, an ABI version constant, and panic catching. build.rs writes rust/capi/include/<module>.h through the cbindgen library; the project checks it in and make lint-rs fails on drift.
  • hatch-rs 0.5.0 validates every <module>_ declaration against the built library, ships it under <module>/lib with the header under <module>/include/<module>, and links the PyO3 extension against it (links, rpath, soname/install name). The extension crate's capi and direct features follow HATCH_RS_LIBRARY_SOURCE through library-source-features: wheels bundle the library, conda builds can resolve an external one, and Pyodide compiles the crate in.
  • The package exposes get_prefix(), get_include(), get_library_dirs(), and get_libraries(), and adds its lib directory to the Windows DLL search path before the extension loads. cibuildwheel repair commands exclude the bundled library on Linux and macOS.
  • rust/rustfmt.toml sets max_width = 150 (matching the Python line length) so the generated Rust stays rustfmt-clean for module names up to about 35 characters; the signatures carry the module name twice.
  • examples/rust-capi.yaml plus gen-rust-capi/test-rust-capi targets and a rust-capi matrix entry exercise the option. The test target also commits rust/Cargo.lock and the generated header.

Without the option, the rendered rust project is unchanged.

Verified locally on macOS: the generated project builds, make lint, make checks, Rust and Python tests pass (the Python test round-trips Example through the C library and loads it with ctypes), the wheel contains the extension, library, and header only, and check_sdist_artifacts.py passes.

Type of Change

  • New feature
  • Documentation update
  • CI / build configuration

Checklist

  • Linting passes (make lint)
  • Tests pass (make test)
  • New tests added for new functionality
  • Documentation updated (if applicable)
  • Changelog / version bump (if applicable)

add_c_abi adds rust/capi, a crate that builds lib<module>_capi with an
extern "C" surface over the Rust crate. build.rs writes the header
through cbindgen and the project checks it in; make lint-rs fails on
drift. hatch-rs ships the library under <module>/lib with the header
under <module>/include/<module>, and the PyO3 extension links against it
instead of compiling the crate in. The extension crate's capi and direct
features follow hatch-rs's library-source, so wheels bundle the library,
conda builds can use an external one, and Pyodide compiles the crate in.

The package exposes get_prefix, get_include, get_library_dirs, and
get_libraries, and adds its lib directory to the Windows DLL search
path. Wheel repair keeps the bundled library. rust/rustfmt.toml sets
max_width = 150 so generated code stays formatted for long module names.

A rust-capi example and gen/test targets exercise the option in CI.

Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
copier needs every question answered to run without a prompt, and the rendered README had two blank lines after the C ABI section.

Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
@timkpaine timkpaine added the tag: automerge Automatically merge this PR when status checks pass label Nov 11, 2026
@python-templates-automerge
python-templates-automerge Bot merged commit 4eb4b3a into main Nov 11, 2026
46 checks passed
@python-templates-automerge
python-templates-automerge Bot deleted the rust-c-abi branch November 11, 2026 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tag: automerge Automatically merge this PR when status checks pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant