cbindgen/README.md

141 lines
5.2 KiB
Markdown
Raw Normal View History

2017-04-27 17:41:01 -04:00
# `cbindgen`   [![Build Status]][travis] [![Latest Version]][crates.io]
[Build Status]: https://api.travis-ci.org/rlhunt/cbindgen.svg?branch=master
[travis]: https://travis-ci.org/rlhunt/cbindgen
[Latest Version]: https://img.shields.io/crates/v/cbindgen.svg
[crates.io]: https://crates.io/crates/cbindgen
2017-04-05 18:14:21 -04:00
2017-05-08 01:26:44 -04:00
This project can be used to generate C bindings for Rust code. It is currently being developed to support creating bindings for [WebRender](https://github.com/servo/webrender/), but has been designed to support any project.
2017-04-11 18:26:14 -04:00
2017-04-17 21:39:40 -04:00
## Features
* Builds bindings for a crate, its mods, its dependent crates, and their mods
* Only the necessary types for exposed functions are given bindings
2017-05-11 03:57:57 -04:00
* Can specify annotations for controlling some aspects of binding
* Support for generic structs
2017-05-08 01:26:44 -04:00
* Customizable formatting, can be used in C or C++ projects
2017-04-17 21:39:40 -04:00
2017-04-12 11:42:43 -04:00
## Use
2017-04-05 18:28:30 -04:00
### Command line
2017-05-08 01:26:44 -04:00
`cbindgen crate/ -o crate/bindings.h`
2017-04-12 11:42:43 -04:00
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 crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
cbindgen::generate(&crate_dir)
.unwrap()
.write_to_file("bindings.h");
}
2017-04-18 03:14:55 -04:00
```
## Configuration
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
# An optional string of text to output at the beginning of the generated file
2017-06-29 00:25:56 -04:00
header = "/* 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
2017-06-29 00:25:56 -04:00
trailer = "/* Text to put at the end of the generated file */"
# An optional name to use as an include guard
2017-06-29 00:25:56 -04:00
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
2017-06-29 00:29:18 -04:00
autogen_warning = "/* 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
2017-06-29 00:25:56 -04:00
include_version = true
# An optional namespace to output around the generated bindings
2017-06-29 00:25:56 -04:00
namespace = "ffi"
# An optional list of namespaces to output around the generated bindings
2017-06-29 00:25:56 -04:00
namespaces = ["mozilla", "wr"]
# The style to use for curly braces
2017-06-29 00:25:56 -04:00
braces = "[SameLine|NextLine]"
# The desired length of a line to use when formatting lines
2017-06-29 00:25:56 -04:00
line_length = 80
# The amount of spaces in a tab
2017-06-29 00:25:56 -04:00
tab_width = 2
# The language to output bindings in
2017-06-29 00:25:56 -04:00
language = "[C|C++]"
[parse]
# Whether to parse dependent crates and include their types in the generated
# bindings
2017-06-29 00:25:56 -04:00
parse_deps = true
# A white list of crate names that are allowed to be parsed
2017-06-29 00:25:56 -04:00
include = ["webrender", "webrender_traits"]
# A black list of crate names that are not allowed to be parsed
2017-06-29 00:25:56 -04:00
exclude = ["libc"]
# A list of crate names that should be run through `cargo expand` before
# parsing to expand any macros
2017-06-29 00:25:56 -04:00
expand = ["euclid"]
[fn]
# An optional prefix to put before every function declaration
2017-06-29 00:25:56 -04:00
prefix = "string"
# An optional postfix to put after any function declaration
2017-06-29 00:25:56 -04:00
postfix = "string"
# How to format function arguments
args = "[Auto|Vertical|Horizontal]"
# A rule to use to rename function argument names
rename_args = "[None|GeckoCase|LowerCase|UpperCase|PascalCase|CamelCase|SnakeCase|ScreamingSnakeCase|QualifiedScreamingSnakeCase]"
[struct]
# A rule to use to rename field names
rename_fields = "[None|GeckoCase|LowerCase|UpperCase|PascalCase|CamelCase|SnakeCase|ScreamingSnakeCase|QualifiedScreamingSnakeCase]"
# Whether to generate helper template specialization for generics
2017-07-17 22:59:05 -04:00
generic_template_specialization = true
# Whether to derive an operator== for all structs
2017-06-29 00:25:56 -04:00
derive_eq = false
# Whether to derive an operator!= for all structs
2017-06-29 00:25:56 -04:00
derive_neq = false
# Whether to derive an operator< for all structs
2017-06-29 00:25:56 -04:00
derive_lt = false
# Whether to derive an operator<= for all structs
2017-06-29 00:25:56 -04:00
derive_lte = false
# Whether to derive an operator> for all structs
2017-06-29 00:25:56 -04:00
derive_gt = false
# Whether to derive an operator>= for all structs
2017-06-29 00:25:56 -04:00
derive_gte = false
[enum]
# A rule to use to rename enum variants
rename_variants = "[None|GeckoCase|LowerCase|UpperCase|PascalCase|CamelCase|SnakeCase|ScreamingSnakeCase|QualifiedScreamingSnakeCase]"
2017-06-29 00:25:56 -04:00
```
2017-04-17 21:39:40 -04:00
## Examples
2017-05-11 04:21:06 -04:00
See `compile-tests/` for some examples of rust source that can be handled.
2017-04-17 21:39:40 -04:00
2017-04-12 11:42:43 -04:00
## How it works
1. All the structs, enums, type aliases, and functions that are representable in C are gathered
2. A dependency graph is built using the extern "C" functions as roots
2017-04-17 21:39:40 -04:00
* This removes unneeded types from the bindings and sorts the structs that depend on each other
2017-04-12 11:42:43 -04:00
3. Some code generation is done to specialize generics that are specified as type aliases
4. The items are printed in dependency order in C syntax
2017-04-12 11:42:43 -04:00
## Future work
1. Better support for types with fully specified names
2. Support for generating a FFI interface for a Struct+Impl
3. ...