cbindgen
![Build Status] ![Latest Version] ![Api Rustdoc]This project can be used to generate C bindings for Rust code. It is currently being developed to support creating bindings for WebRender, but has been designed to support any project.
#ifdef
's for #[cfg]
attributes#[repr(sized)]
tagged enum'scbindgen crate/ -o crate/bindings.h
See cbindgen --help
for more options.
build.rs
cbindgen
can also be used in build scripts. How this fits into compiling the native code depends on your project.
Here's an example build.rs script: ```rust extern crate cbindgen;
use std::env;
fn main() { let cratedir = env::var("CARGOMANIFEST_DIR").unwrap();
cbindgen::Builder::new()
.with_crate(crate_dir)
.generate()
.expect("Unable to generate bindings")
.write_to_file("bindings.h");
}
```
There are some options that can be used to configure the binding generation.
For the command line, they can be specified by creating a cbindgen.toml
with the options. This can be placed in the binding crate root or at a path manually specified.
For build scripts, options can be specified on the builder or by writing a cbindgen.toml
and using the helper function cbindgen::generate
.
Here is a description of the options available in a config.
```toml
header = "/* Text to put at the beginning of the generated file. Probably a license. */"
trailer = "/* Text to put at the end of the generated file */"
includeguard = "mozillawrbindingsh"
autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */"
include_version = true
namespace = "ffi"
namespaces = ["mozilla", "wr"]
braces = "[SameLine|NextLine]"
line_length = 80
tab_width = 2
language = "[C|C++]"
[parse]
parse_deps = true
include = ["webrender", "webrender_traits"]
exclude = ["libc"]
cargo expand
beforeexpand = ["euclid"]
[export]
include = ["Foo", "Bar"]
exclude = ["Bad"]
prefix = "CAPI_"
[export.rename] "Struct" = "CAPI_Struct"
[fn]
prefix = "string"
postfix = "string"
args = "[Auto|Vertical|Horizontal]"
rename_args = "[None|GeckoCase|LowerCase|UpperCase|PascalCase|CamelCase|SnakeCase|ScreamingSnakeCase|QualifiedScreamingSnakeCase]"
[struct]
rename_fields = "[None|GeckoCase|LowerCase|UpperCase|PascalCase|CamelCase|SnakeCase|ScreamingSnakeCase|QualifiedScreamingSnakeCase]"
derive_eq = false
derive_neq = false
derive_lt = false
derive_lte = false
derive_gt = false
derive_gte = false
[enum]
rename_variants = "[None|GeckoCase|LowerCase|UpperCase|PascalCase|CamelCase|SnakeCase|ScreamingSnakeCase|QualifiedScreamingSnakeCase]"
```
See tests/rust/
for some examples of rust source that can be handled.
cbindgen
and rusty-cheddar
cbindgen
supports genericscbindgen
supports C++ output using enum class
and template specialization
cbindgen
supports generating bindings including multiple modules and cratesThere may be other differences, but those are the ones that I know of. Please correct me if I misrepresented anything.
If you're using cbindgen
and would like to be added to this list, please open a pull request!