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]
attributescbindgen 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::generate(&crate_dir)
.unwrap()
.write_to_file("bindings.h");
}
```
There are some options that can be used to configure the binding generation. They can be specified by creating a cbindgen.toml
with the options in the binding crate root or at a path manually specified through the command line. Alternatively, build scripts can specify them using cbindgen::generate_with_config
.
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"]
[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]"
generictemplatespecialization = true
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 compile-tests/
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.