-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Add a bindgen pass #2124
Comments
Rather than baking this in as a syntax extension, I think this could be a good test for rustpkg's custom build logic. As such, it's blocked on #5677. |
Still relevant |
The major sub-bugs of #5677 are done, so this is ready to work on. |
cc #10530 (the converse operation; generating a C header from a Rust file), and some keywords (since I couldn't find this issue when opening that one): C header, C library, libclang, clang. |
I'm nominating this because I think hard-wiring the ABI of C libraries is a huge problem. Rust libraries are going to silently fail across library versions with breaking changes to the ABI. If we had this, using C from Rust would be as easy as from C++. It would be a killer feature of the language in my opinion. I don't think it would be incredibly difficult to move |
I agree. |
In theory this sounds great. How do you deal with unions? Will the bindgen do the right thing with Not to mention that you can't handle C macros, which must be shimmed. |
rust-bindgen currently converts union Foo {
int x;
char y;
double z;
}; into use std::libc::*;
pub struct Union_Foo {
data: [u64, ..1u],
}
impl Union_Foo {
pub fn x(&mut self) -> *mut c_int {
unsafe { ::std::cast::transmute(::std::ptr::to_mut_unsafe_ptr(self)) }
}
pub fn y(&mut self) -> *mut c_schar {
unsafe { ::std::cast::transmute(::std::ptr::to_mut_unsafe_ptr(self)) }
}
pub fn z(&mut self) -> *mut c_double {
unsafe { ::std::cast::transmute(::std::ptr::to_mut_unsafe_ptr(self)) }
}
} Of course, if it were built into the compiler we could have first class FFI unions.
If the C headers are const-correct, I believe it does approximately the right thing.
We can do a best effort thing, aiui libclang offers sensible handling of macros (I don't know the details). |
@metajack: A basic implementation will remove 100% of the ABI hard-wiring for libraries designed with FFI in mind like sqlite3 and libgit2. It's true that it will never be perfect for many libraries, but it will certainly vastly reduce the redundancy on the Rust side and therefore the risk of silent breakage with different versions or even configurations of the library. |
Not as high a priority as it seemed 2 years ago. P-low. |
This could probably be done as an loadable syntax extension now that we have them, e.g. #[phase(syntax)] extern crate bindgen;
#[bindgen("foo.h")] // fills the module with the appropriate `extern` block
mod foo {} |
Vala does this correctly. Of course they cheat by compiling to C, but the interface is nice. |
I don't feel as passionately abut integrating a C compiler into our Rust compiler any more. It doesn't feel very pressing, can probably done out-of-tree through Cargo eventually. Closing but feel free to reopen if you disagree. |
Change function declaration and call to ignore arguments that are ZST, and implement the closure to function pointer by reifying it to its FnOnce implementation. The casting of closures to function pointers rely on the fact that the closures do not capture anything. Those closures are represented by empty structures, which should get pruned from its argument list of the FnOnce::call_once implementation.
Make a pass that executes bindgen during the build.
This would build the jsapi module automatically based on the header files:
It would never generate any
.rs
files, just insert it directly into the AST.The text was updated successfully, but these errors were encountered: