Skip to content

[Proposal] Bindgen for multiple versions of a given library enabled through feature gating. #3185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Narsil opened this issue Apr 4, 2025 · 3 comments

Comments

@Narsil
Copy link

Narsil commented Apr 4, 2025

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: coreylowman/cudarc#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;
@jschwe
Copy link
Contributor

jschwe commented Apr 8, 2025

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.

@rich-ayr
Copy link

rich-ayr commented Apr 10, 2025

@jschwe has done some great prior work. I was doing attribute related work in #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

@rich-ayr
Copy link

rich-ayr commented Apr 12, 2025

P.S.: Contact via [email protected]—"Re: rust-bindgen contribution" if anyone is interested to more actively work on a bindgen fork/"v2". The project has grown quite convoluted and hard to maintain imo. Many low hanging fruits we can get done quickly and send in a single version for review in the end; this way we can get the project back into the rust-lang organization with great improvements; just an idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants