README.md 5.11 KB
Newer Older
Ryan Hunt's avatar
Ryan Hunt committed
1
2
3
4
5
6
# `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
Kartikaya Gupta's avatar
Kartikaya Gupta committed
7

Ryan Hunt's avatar
Ryan Hunt committed
8
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.
Kartikaya Gupta's avatar
Kartikaya Gupta committed
9

10
11
12
13
## Features

  * Builds bindings for a crate, its mods, its dependent crates, and their mods
  * Only the necessary types for exposed functions are given bindings
Ryan Hunt's avatar
Ryan Hunt committed
14
  * Can specify annotations for controlling some aspects of binding
15
  * Generic structs can be exposed using `type IntFoo = Foo<i32>;`
Ryan Hunt's avatar
Ryan Hunt committed
16
  * Customizable formatting, can be used in C or C++ projects
17

Ryan Hunt's avatar
Ryan Hunt committed
18
## Use
Kartikaya Gupta's avatar
Kartikaya Gupta committed
19

20
21
### Command line

Ryan Hunt's avatar
Ryan Hunt committed
22
`cbindgen crate/ -o crate/bindings.h`
Ryan Hunt's avatar
Ryan Hunt committed
23

24
25
26
27
28
29
30
31
32
33
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;

34
use std::env;
35
36

fn main() {
Ryan Hunt's avatar
Ryan Hunt committed
37
38
39
40
41
    let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();

    cbindgen::generate(crate_dir)
      .unwrap()
      .write_to_file("bindings.h");
42
}
Ryan Hunt's avatar
Ryan Hunt committed
43

44
45
```

46
47
## Configuration

48
49
50
51
52
53
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
Ryan Hunt's avatar
Ryan Hunt committed
54
header = "/* Text to put at the beginning of the generated file. Probably a license. */"
55
# An optional string of text to output at the end of the generated file
Ryan Hunt's avatar
Ryan Hunt committed
56
trailer = "/* Text to put at the end of the generated file */"
57
# An optional name to use as an include guard
Ryan Hunt's avatar
Ryan Hunt committed
58
include_guard = "mozilla_wr_bindings_h"
59
60
# An optional string of text to output between major sections of the generated
# file as a warning against manual editing
Ryan Hunt's avatar
Ryan Hunt committed
61
autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */"
62
63
# Whether to include a comment with the version of cbindgen used to generate the
# file
Ryan Hunt's avatar
Ryan Hunt committed
64
include_version = true
65
# An optional namespace to output around the generated bindings
Ryan Hunt's avatar
Ryan Hunt committed
66
namespace = "ffi"
67
# An optional list of namespaces to output around the generated bindings
Ryan Hunt's avatar
Ryan Hunt committed
68
namespaces = ["mozilla", "wr"]
69
# The style to use for curly braces
Ryan Hunt's avatar
Ryan Hunt committed
70
braces = "[SameLine|NextLine]"
71
# The desired length of a line to use when formatting lines
Ryan Hunt's avatar
Ryan Hunt committed
72
line_length = 80
73
# The amount of spaces in a tab
Ryan Hunt's avatar
Ryan Hunt committed
74
tab_width = 2
75
# The language to output bindings in
Ryan Hunt's avatar
Ryan Hunt committed
76
language = "[C|C++]"
77
78
79
80

[parse]
# Whether to parse dependent crates and include their types in the generated
# bindings
Ryan Hunt's avatar
Ryan Hunt committed
81
parse_deps = true
82
# A white list of crate names that are allowed to be parsed
Ryan Hunt's avatar
Ryan Hunt committed
83
include = ["webrender", "webrender_traits"]
84
# A black list of crate names that are not allowed to be parsed
Ryan Hunt's avatar
Ryan Hunt committed
85
exclude = ["libc"]
86
87
# A list of crate names that should be run through `cargo expand` before
# parsing to expand any macros
Ryan Hunt's avatar
Ryan Hunt committed
88
expand = ["euclid"]
89
90
91

[fn]
# An optional prefix to put before every function declaration
Ryan Hunt's avatar
Ryan Hunt committed
92
prefix = "string"
93
# An optional postfix to put after any function declaration
Ryan Hunt's avatar
Ryan Hunt committed
94
postfix = "string"
95
# How to format function arguments
Ryan Hunt's avatar
Ryan Hunt committed
96
args = "Auto|Vertical|Horizontal"
97
# A rule to use to rename function argument names
98
rename_args = "[None|GeckoCase|LowerCase|UpperCase|PascalCase|CamelCase|SnakeCase|ScreamingSnakeCase|QualifiedScreamingSnakeCase]"
99
100
101

[struct]
# A rule to use to rename field names
102
rename_fields = "[None|GeckoCase|LowerCase|UpperCase|PascalCase|CamelCase|SnakeCase|ScreamingSnakeCase|QualifiedScreamingSnakeCase]"
103
# Whether to derive an operator== for all structs
Ryan Hunt's avatar
Ryan Hunt committed
104
derive_eq = false
105
# Whether to derive an operator!= for all structs
Ryan Hunt's avatar
Ryan Hunt committed
106
derive_neq = false
107
# Whether to derive an operator< for all structs
Ryan Hunt's avatar
Ryan Hunt committed
108
derive_lt = false
109
# Whether to derive an operator<= for all structs
Ryan Hunt's avatar
Ryan Hunt committed
110
derive_lte = false
111
# Whether to derive an operator> for all structs
Ryan Hunt's avatar
Ryan Hunt committed
112
derive_gt = false
113
# Whether to derive an operator>= for all structs
Ryan Hunt's avatar
Ryan Hunt committed
114
derive_gte = false
115
116
117

[enum]
# A rule to use to rename enum variants
118
rename_variants = "[None|GeckoCase|LowerCase|UpperCase|PascalCase|CamelCase|SnakeCase|ScreamingSnakeCase|QualifiedScreamingSnakeCase]"
Ryan Hunt's avatar
Ryan Hunt committed
119

120
```
121

122
123
## Examples

Ryan Hunt's avatar
Ryan Hunt committed
124
See `compile-tests/` for some examples of rust source that can be handled.
125

Ryan Hunt's avatar
Ryan Hunt committed
126
127
128
129
## 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
130
    * This removes unneeded types from the bindings and sorts the structs that depend on each other
Ryan Hunt's avatar
Ryan Hunt committed
131
3. Some code generation is done to specialize generics that are specified as type aliases
132
4. The items are printed in dependency order in C syntax
Ryan Hunt's avatar
Ryan Hunt committed
133
134
135

## Future work

136
137
138
1. Better support for types with fully specified names
2. Support for generating a FFI interface for a Struct+Impl
3. ...