rust-bindgen
rust-bindgen copied to clipboard
[Proposal] Bindgen for multiple versions of a given library enabled through feature gating.
Hi
For https://github.com/coreylowman/cudarc there was a need to support multiple versions of cuda C library.
Here cuda 11.4 .. cuda 12.8 for instance. This was done previously by generating a binding for each library, and manually import various files with feature flags.
In this PR: https://github.com/coreylowman/cudarc/pull/366 I implemented something that would take various bindgen created binding files, and fuse them into a single file where every symbol is marked with the feature gating which vastly reduces the amount of code generated in the end.
While writing it, I thought maybe it could become something useful more generally for the bindgen community since I took quite a bit of code from bindgen to write it.
Do you think it's something interesting ? I could try to look up how to implement if there's enough interest.
For a for visual show:
From
#[cfg(feature = "v1.0.0")]
mod v100{
pub mysymbol;
}
#[cfg(feature = "v1.1.0")]
mod v110{
pub mysymbol;
pub myothersymbol;
}
To
// Feature flag elided since every version has this symbol
pub mysymbol;
#[cfg(any(feature = "v1.0.0"))]
pub myothersymbol;
I'm doing something quite similar with a patch in my fork of bindgen, which adds a bindgen callback to add attributes based on documentation comments (callback code, bindgen patch). The generated bindings than have feature guards (example).
Upstreaming support for adding custom cfg guards would definitly be interesting to me.
@jschwe has done some great prior work. I was doing attribute related work in https://github.com/rust-lang/rust-bindgen/pull/3088 but it seems dead on arrival. You want to perhaps band together and work on an improved version (fork)? rust-lang seems to be overwhelmed with other work right now and not pay enough attention to this project.—Rich