1
- # Servo's rust- bindgen
1
+ # ` bindgen `
2
2
3
- A binding generator for the Rust language .
3
+ Automatically generates Rust FFI bindings to C and C++ libraries .
4
4
5
- This is a fork of [ crabtw/rust-bindgen ] ( https://github.com/crabtw/rust-bindgen )
6
- designed to work on C++ code as well.
5
+ <!-- START doctoc generated TOC please keep comment here to allow auto update -->
6
+ <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
7
7
8
- Currently this is being used for Servo's SpiderMonkey bindings, and also for
9
- the [ Stylo] ( https://public.etherpad-mozilla.org/p/stylo ) project.
10
8
11
- ## Requirements
9
+ - [ Usage] ( #usage )
10
+ - [ Requirements] ( #requirements )
11
+ - [ Installing Clang 3.9] ( #installing-clang-39 )
12
+ - [ Windows] ( #windows )
13
+ - [ OSX] ( #osx )
14
+ - [ Debian-based Linuxes] ( #debian-based-linuxes )
15
+ - [ Arch] ( #arch )
16
+ - [ From source] ( #from-source )
17
+ - [ Library usage with ` build.rs ` ] ( #library-usage-with-buildrs )
18
+ - [ ` build.rs ` Tutorial] ( #buildrs-tutorial )
19
+ - [ Simple Example: ` ./bindgen-integration ` ] ( #simple-example-bindgen-integration )
20
+ - [ Real World Example: Stylo] ( #real-world-example-stylo )
21
+ - [ Command Line Usage] ( #command-line-usage )
22
+ - [ C++] ( #c )
23
+ - [ Annotations] ( #annotations )
24
+ - [ ` opaque ` ] ( #opaque )
25
+ - [ ` hide ` ] ( #hide )
26
+ - [ ` replaces ` ] ( #replaces )
27
+ - [ ` nocopy ` ] ( #nocopy )
28
+ - [ Building From Source] ( #building-from-source )
12
29
13
- It is recommended to use clang 3.9 with the current generator. It can run with
14
- clang 3.8 with some features disabled.
30
+ <!-- END doctoc generated TOC please keep comment here to allow auto update -->
15
31
16
- ### Installing Clang 3.9
32
+ ## Usage
17
33
18
- #### Windows
34
+ ### Requirements
35
+
36
+ It is recommended to use Clang 3.9 or greater, however ` bindgen ` can run with
37
+ older Clangs with some features disabled.
38
+
39
+ #### Installing Clang 3.9
40
+
41
+ ##### Windows
19
42
20
43
Download and install the official pre-built binary from
21
44
[ LLVM download page] ( http://releases.llvm.org/download.html ) .
22
45
23
- #### OSX
46
+ ##### OSX
24
47
25
48
If you use Homebrew:
49
+
26
50
```
27
51
$ brew install llvm
28
52
```
29
53
30
54
If you use MacPorts:
55
+
31
56
```
32
57
$ port install clang-3.9
33
58
```
34
59
35
- #### Debian-based Linuxes
60
+ ##### Debian-based Linuxes
36
61
37
62
```
38
63
# apt-get install llvm-3.9-dev libclang-3.9-dev
@@ -42,13 +67,13 @@ Ubuntu 16.10 provides the necessary packages directly. If you are using older
42
67
version of Ubuntu or other Debian-based distros, you may need to add the LLVM
43
68
repos to get version 3.9. See http://apt.llvm.org/ .
44
69
45
- #### Arch
70
+ ##### Arch
46
71
47
72
```
48
73
# pacman -S clang
49
74
```
50
75
51
- #### From source
76
+ ##### From source
52
77
53
78
If your package manager doesn't yet offer Clang 3.9, you'll need to build from
54
79
source. For that, follow the instructions
@@ -61,31 +86,26 @@ Those instructions list optional steps. For bindgen:
61
86
* Checkout and build the compiler-rt
62
87
* You do not need to checkout or build libcxx
63
88
64
- ## Building
89
+ ### Library usage with ` build.rs `
65
90
66
- ```
67
- $ cd bindgen
68
- $ cargo build
69
- ```
91
+ #### ` build.rs ` Tutorial
70
92
71
- If you installed multiple versions of llvm, it may not be able to locate the
72
- latest version of libclang. In that case, you may want to either uninstall
73
- other versions of llvm, or specify the path of the desired libclang explicitly:
74
- ```
75
- $ export LIBCLANG_PATH=path/to/clang-3.9/lib
76
- ```
93
+ [ Here is a step-by-step tutorial for generating FFI bindings to the ` bzip2 ` C library.] [ tutorial ]
77
94
78
- On Linux and macOS, you may also need to add a path to ` libclang.so ` (usually
79
- the same path as above) to library search path. This can be done as below:
80
- ```
81
- $ export LD_LIBRARY_PATH=path/to/clang-3.9/lib # for Linux
82
- $ export DYLD_LIBRARY_PATH=path/to/clang-3.9/lib # for macOS
83
- ```
95
+ [ tutorial ] : http://fitzgeraldnick.com/2016/12/14/using-libbindgen-in-build-rs.html
96
+
97
+ #### Simple Example: ` ./bindgen-integration `
98
+
99
+ The [ ` ./bindgen-integration ` ] [ integration ] directory has an example crate that
100
+ generates FFI bindings in ` build.rs ` and can be used a template for new
101
+ projects.
84
102
85
- # Library usage with ` build.rs `
103
+ [ integration ] : ./bindgen-integration
86
104
87
- See [ the Stylo build script] [ stylo-script ] to see how it is used inside the
88
- Servo organisation.
105
+ #### Real World Example: Stylo
106
+
107
+ A real world example is [ the Stylo build script] [ stylo-script ] used for
108
+ integrating Servo's layout system into Gecko.
89
109
90
110
[ stylo-script ] : https://github.com/servo/servo/blob/master/components/style/build_gecko.rs
91
111
@@ -124,28 +144,28 @@ In `src/main.rs`:
124
144
include! (concat! (env! (" OUT_DIR" ), " /example.rs" ));
125
145
```
126
146
127
- # Command Line Usage
147
+ ### Command Line Usage
128
148
129
- There are a few options documented when running ` ./bindgen --help ` . Other
130
- options might exist (see [ the SpiderMonkey script ] [ sm-script ] to see how it
131
- is used inside the Servo organisation.
149
+ ```
150
+ $ cargo install bindgen
151
+ ```
132
152
133
- [ sm-script ] : https://github.com/servo/rust-mozjs/blob/master/etc/bindings.sh
153
+ There are a few options documented when running ` ./bindgen --help ` .
134
154
135
- ## C++ Usage
155
+ ### C++
136
156
137
157
This fork of rust-bindgen can handle a number of C++ features.
138
158
139
159
When passing in header files, the file will automatically be treated as C++ if
140
160
it ends in `` .hpp `` . If it doesn't, `` -x c++ `` can be used to force C++ mode.
141
161
142
- ## Annotations
162
+ ### Annotations
143
163
144
164
The translation of classes, structs, enums, and typedefs can be adjusted using
145
165
annotations. Annotations are specifically formatted html tags inside doxygen
146
166
style comments.
147
167
148
- ### ` opaque `
168
+ #### ` opaque `
149
169
150
170
The ` opaque ` annotation instructs bindgen to ignore all fields defined in
151
171
a struct/class.
@@ -154,7 +174,7 @@ a struct/class.
154
174
// / <div rustbindgen opaque></div>
155
175
```
156
176
157
- ### ` hide `
177
+ #### ` hide `
158
178
159
179
The ` hide ` annotation instructs bindgen to ignore the struct/class/field/enum
160
180
completely.
@@ -163,7 +183,7 @@ completely.
163
183
/// <div rustbindgen hide></div>
164
184
```
165
185
166
- ### ` replaces `
186
+ #### ` replaces `
167
187
168
188
The ` replaces ` annotation can be used to use a type as a replacement for other
169
189
(presumably more complex) type. This is used in Stylo to generate bindings for
@@ -188,7 +208,28 @@ public:
188
208
That way, after code generation, the bindings for the `nsTArray` type are
189
209
the ones that would be generated for `nsTArray_Simple`.
190
210
191
- ### `nocopy`
211
+ #### `nocopy`
192
212
193
213
The `nocopy` annotation is used to prevent bindgen to autoderive the `Copy`
194
214
and `Clone` traits for a type.
215
+
216
+ ## Building From Source
217
+
218
+ ```
219
+ $ cd bindgen
220
+ $ cargo build
221
+ ```
222
+
223
+ If you installed multiple versions of llvm, it may not be able to locate the
224
+ latest version of libclang. In that case, you may want to either uninstall
225
+ other versions of llvm, or specify the path of the desired libclang explicitly:
226
+ ```
227
+ $ export LIBCLANG_PATH=path/to/clang-3.9/lib
228
+ ```
229
+
230
+ On Linux and macOS, you may also need to add a path to `libclang.so` (usually
231
+ the same path as above) to library search path. This can be done as below:
232
+ ```
233
+ $ export LD_LIBRARY_PATH=path/to/clang-3.9/lib # for Linux
234
+ $ export DYLD_LIBRARY_PATH=path/to/clang-3.9/lib # for macOS
235
+ ```
0 commit comments