Repository navigation
Add a C ABI library option to the rust template - #218
Merged
Merged
Conversation
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
force-pushed
the
rust-c-abi
branch
from
November 10, 2026 19:30
c927d3f to
5808c45
Compare
5 of 6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
add_c_abi(default false,rustonly) generates the split-library layout from the templates roadmap, the shape fsspec-rs now uses:rust/capibuildslib<module>_capi, anextern "C"surface over the Rust crate with opaque handles, paired create/free functions, an ABI version constant, and panic catching.build.rswritesrust/capi/include/<module>.hthrough the cbindgen library; the project checks it in andmake lint-rsfails on drift.<module>_declaration against the built library, ships it under<module>/libwith the header under<module>/include/<module>, and links the PyO3 extension against it (links, rpath, soname/install name). The extension crate'scapianddirectfeatures followHATCH_RS_LIBRARY_SOURCEthroughlibrary-source-features: wheels bundle the library, conda builds can resolve an external one, and Pyodide compiles the crate in.get_prefix(),get_include(),get_library_dirs(), andget_libraries(), and adds itslibdirectory to the Windows DLL search path before the extension loads. cibuildwheel repair commands exclude the bundled library on Linux and macOS.rust/rustfmt.tomlsetsmax_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.yamlplusgen-rust-capi/test-rust-capitargets and arust-capimatrix entry exercise the option. The test target also commitsrust/Cargo.lockand the generated header.Without the option, the rendered
rustproject is unchanged.Verified locally on macOS: the generated project builds,
make lint,make checks, Rust and Python tests pass (the Python test round-tripsExamplethrough the C library and loads it with ctypes), the wheel contains the extension, library, and header only, andcheck_sdist_artifacts.pypasses.Type of Change
Checklist
make lint)make test)