cbindgen

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.
Features
- Builds bindings for a crate, its mods, its dependent crates, and their mods
- Only the necessary types for exposed functions are given bindings
- Can specify annotations for controlling some aspects of binding
- Support for generic structs and unions
- Support for exporting constants and statics
- Customizable formatting, can be used in C or C++ projects
- Support for generating
#ifdef
's for#[cfg]
attributes
Use
Command line
cbindgen 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:
extern crate cbindgen;
use env;
Configuration
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.
# An optional string of text to output at the beginning of the generated file
= "/* Text to put at the beginning of the generated file. Probably a license. */"
# An optional string of text to output at the end of the generated file
= "/* Text to put at the end of the generated file */"
# An optional name to use as an include guard
= "mozilla_wr_bindings_h"
# An optional string of text to output between major sections of the generated
# file as a warning against manual editing
= "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */"
# Whether to include a comment with the version of cbindgen used to generate the
# file
= true
# An optional namespace to output around the generated bindings
= "ffi"
# An optional list of namespaces to output around the generated bindings
= ["mozilla", "wr"]
# The style to use for curly braces
= "[SameLine|NextLine]"
# The desired length of a line to use when formatting lines
= 80
# The amount of spaces in a tab
= 2
# The language to output bindings in
= "[C|C++]"
[]
# Whether to parse dependent crates and include their types in the generated
# bindings
= true
# A white list of crate names that are allowed to be parsed
= ["webrender", "webrender_traits"]
# A black list of crate names that are not allowed to be parsed
= ["libc"]
# A list of crate names that should be run through `cargo expand` before
# parsing to expand any macros
= ["euclid"]
[]
# An optional prefix to put before every function declaration
= "string"
# An optional postfix to put after any function declaration
= "string"
# How to format function arguments
= "[Auto|Vertical|Horizontal]"
# A rule to use to rename function argument names
= "[None|GeckoCase|LowerCase|UpperCase|PascalCase|CamelCase|SnakeCase|ScreamingSnakeCase|QualifiedScreamingSnakeCase]"
[]
# A rule to use to rename field names
= "[None|GeckoCase|LowerCase|UpperCase|PascalCase|CamelCase|SnakeCase|ScreamingSnakeCase|QualifiedScreamingSnakeCase]"
# Whether to derive an operator== for all structs
= false
# Whether to derive an operator!= for all structs
= false
# Whether to derive an operator< for all structs
= false
# Whether to derive an operator<= for all structs
= false
# Whether to derive an operator> for all structs
= false
# Whether to derive an operator>= for all structs
= false
[]
# A rule to use to rename enum variants
= "[None|GeckoCase|LowerCase|UpperCase|PascalCase|CamelCase|SnakeCase|ScreamingSnakeCase|QualifiedScreamingSnakeCase]"
Examples
See tests/rust/
for some examples of rust source that can be handled.
Major differences between cbindgen
and rusty-cheddar
cbindgen
supports genericscbindgen
supports C++ output usingenum class
andtemplate specialization
cbindgen
supports generating bindings including multiple modules and crates
There may be other differences, but those are the ones that I know of. Please correct me if I misrepresented anything.
Prominent users
If you're using cbindgen
and would like to be added to this list, please open a pull request!