-
Notifications
You must be signed in to change notification settings - Fork 746
Add --symbol-prefix flag #1375
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
Comments
We should use the C link_name if it differs from the function name. Do you have an example C code? That being said, I can see a function in |
Unfortunately that probably won't work. The way the renaming happens on the C side is with a macro variable that is set if you want a symbol prefix. So if we set that macro variable for the invocation of bindgen, then bindgen will see symbols like
Sure thing. Could I get some basic mentoring instructions? I'm not familiar with the codebase. Also, would this enable us to provide a command-line flag? Right now, I'm invoking bindgen as a CLI tool. |
I see, that makes sense, thanks. You could re-export the rust items with different names, something like: mod bindings {
// Bindgen-generated code.
}
pub use bindings::{prefix_my_function as my_function, prefix_my_other_function as my_other_function, ...}; But agree it's not ideal.
Sure, happy to! The place we store the mangled name of a function is: That gets computed in: That function should already have all the arguments you need to get a hand to the options. The options you can get from For the parse callbacks, you can see an example here for example: I think it would make sense an MVP API like: pub fn link_name_override(&self, function_name: &str) -> Option<String>; Where you can get the name passed by the caller, or using To make it work for the CLI you need to add some kind of option. I guess only an option to prefix it for now given it suits your needs should be fine. You'd look at Once you have that you could do something like the following in if let Some(callbacks) = ctx.parse_callbacks() {
if let Some(override) = callbacks.link_name_override(&cursor.spelling()) {
return Some(override);
}
}
if let Some(prefix) = ctx.options().link_name_prefix {
let mut mangled = cursor.spelling();
mangled.insert_str(0, prefix);
return Some(mangled);
}
// ... That should do I'd think. Then you'd need to add a test for this, via the command line flags (see the tests in Hope it helps, thanks! |
Awesome, those are fantastic instructions; thanks! A few questions:
I'm not sure I understand who would be calling that method, and what they'd be passing for |
Not really. Maybe using the parent ID of the function or something you can tell something is definitely C++, but... In any case given this is opt-in I don't think it's a big issue.
I think doing the simple thing even if it isn't amazing for C++ so far makes sense.
It'd be a method inside the this trait: https://github.com/rust-lang-nursery/rust-bindgen/blob/0db9588e84463c211340f0bc2cb6da2b3a99c441/src/callbacks.rs#L26 Which is a customization hook for people that use bindgen at |
Are there any chances of getting this solved in mainline? |
As you may have guessed by the fact that I haven't touched this in over a year, I didn't end up having time to work on it. If you'd like somebody to, I'd suggest posting this issue in This Week in Rust's Call for Participation. |
solved via #2228 |
* Add `generated_link_name_override` method for `ParseCallbacks`. `generated_link_name_override` lets the developer choose the link name for a symbol. If a link name is specified, the attribute `#[link_name = "\u{1}VALUE"]` will be set, overriding any other potential link name, including the mangled name. This commit also adds an option to the bindgen cli called `prefix-link-name`, to prefix the link name of exported symbols. I think this should properly solve #1375. * Fix expectation file for `prefix-link-name-c`. * Fix expectation file for `prefix-link-name-cpp` * Add a new `parse_callbacks` callback to solve the roundtrip test.
I'm trying to integrate a C library which supports symbol renaming. I want to use this feature in order to allow consumers to depend on multiple versions of my crate without encountering symbol conflicts during build.
In order to do this, I'd like the C symbol
FOO
to be exposed through Rust asFOO
- the name people expect. However, I will invoke the C library's build system in a way that causes the emitted object file to expose the nameCUSTOM_PREFIX_FOO
. Thus, I need to also emit a#[link(name = "CUSTOM_PREFIX_FOO")]
attribute on the Rust-sideFOO
declaration.It would be great if bindgen supported this natively, e.g. via a
--symbol-prefix
flag. This feature would instruct bindgen to emitlink
attributes as described above. Concretely:FOO
CUSTOM_PREFIX_FOO
FOO
#[link(name = "CUSTOM_PREFIX_FOO")]
The text was updated successfully, but these errors were encountered: