Skip to content

Commit 0a770cc

Browse files
author
bors-servo
authored
Auto merge of rust-lang#408 - fitzgen:clean-up-docs, r=emilio
Clean up the README.md and CONTRIBUTING.md docs Use the `doctoc` program to generate tables of contents, stop referring to forking and make this the canonical `bindgen`, move the section on building to last. r? @emilio
2 parents 4ac39de + 78235aa commit 0a770cc

File tree

2 files changed

+137
-80
lines changed

2 files changed

+137
-80
lines changed

CONTRIBUTING.md

Lines changed: 71 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
1-
# Contributing to `servo/rust-bindgen`
1+
# Contributing to `bindgen`
22

33
Hi! We'd love to have your contributions! If you want help or mentorship, reach
4-
out to us in a GitHub issue, or stop by #servo on irc.mozilla.org and introduce
5-
yourself.
6-
7-
* [Code of Conduct](#coc)
8-
* [Filing an Issue](#issue)
9-
* [Building](#building)
10-
* [Testing](#tests)
11-
* [Overview](#tests-overview)
12-
* [Running All Tests](#tests-all)
13-
* [Running a Single, Specific Test](#tests-one)
14-
* [Authoring New Tests](#tests-new)
15-
* [Automatic Code Formatting](#formatting)
16-
* [Debug Logging](#logs)
17-
* [Using `creduce` to Minimize Test Cases](#creduce)
18-
19-
## Code of Conduct <span id="coc"/>
4+
out to us in a GitHub issue, or stop by
5+
[#servo on irc.mozilla.org](irc://irc.mozilla.org#servo) and introduce yourself.
6+
7+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
8+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
9+
10+
11+
- [Code of Conduct](#code-of-conduct)
12+
- [Filing an Issue](#filing-an-issue)
13+
- [Building](#building)
14+
- [Testing](#testing)
15+
- [Overview](#overview)
16+
- [Running All Tests](#running-all-tests)
17+
- [Authoring New Tests](#authoring-new-tests)
18+
- [Automatic code formatting](#automatic-code-formatting)
19+
- [Debug Logging](#debug-logging)
20+
- [Using `creduce` to Minimize Test Cases](#using-creduce-to-minimize-test-cases)
21+
- [Isolating Your Test Case](#isolating-your-test-case)
22+
- [Writing a Predicate Script](#writing-a-predicate-script)
23+
24+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
25+
26+
## Code of Conduct
2027

2128
We abide by the [Rust Code of Conduct][coc] and ask that you do as well.
2229

2330
[coc]: https://www.rust-lang.org/en-US/conduct.html
2431

25-
## Filing an Issue <span id="issue"/>
32+
## Filing an Issue
2633

2734
Think you've found a bug? File an issue! To help us understand and reproduce the
2835
issue, provide us with:
@@ -33,39 +40,68 @@ issue, provide us with:
3340
* The actual `bindgen` output
3441
* The [debugging logs](#logs) generated when running `bindgen` on this testcase
3542

36-
## Building <span id="building"/>
43+
## Building
44+
45+
To build `libbindgen`:
46+
47+
```
48+
$ cd bindgen/libbindgen
49+
$ cargo build
50+
```
51+
52+
To build the `bindgen` executable:
3753

38-
Build instructions are in the [README](./README.md).
54+
```
55+
$ cd bindgen/bindgen
56+
$ cargo build
57+
```
58+
59+
If you installed multiple versions of llvm, it may not be able to locate the
60+
latest version of libclang. In that case, you may want to either uninstall other
61+
versions of llvm, or specify the path of the desired libclang explicitly:
62+
63+
```
64+
$ export LIBCLANG_PATH=path/to/clang-3.9/lib
65+
```
66+
67+
On Linux and macOS, you may also need to add a path to `libclang.so` (usually
68+
the same path as above) to library search path. This can be done as below:
69+
70+
```
71+
$ export LD_LIBRARY_PATH=path/to/clang-3.9/lib # for Linux
72+
$ export DYLD_LIBRARY_PATH=path/to/clang-3.9/lib # for macOS
73+
```
3974

40-
Additionally, you may want to build and test with the `_docs` feature to ensure
75+
Additionally, you may want to build and test with the `docs_` feature to ensure
4176
that you aren't forgetting to document types and functions. CI will catch it if
4277
you forget, but the turn around will be a lot slower ;)
4378

4479
```
4580
$ cd libbindgen && cargo build --features "llvm_stable _docs"
4681
```
4782

48-
## Testing <span id="tests"/>
83+
## Testing
4984

5085
Code for binding generation and testing thereof is in the `libbindgen` crate.
5186
The following sections assume you are working in that subdirectory.
5287

53-
### Overview <span id="tests-overview"/>
88+
### Overview
5489

55-
Input C/C++ test headers reside in the `tests/headers` directory. Expected
56-
output Rust bindings live in `tests/expectations/tests`. For example,
57-
`tests/headers/my_header.h`'s expected generated Rust bindings would be
58-
`tests/expectations/tests/my_header.rs`.
90+
Input C/C++ test headers reside in the `libbindgen/tests/headers`
91+
directory. Expected output Rust bindings live in
92+
`libbindgen/tests/expectations/tests`. For example,
93+
`libbindgen/tests/headers/my_header.h`'s expected generated Rust bindings would
94+
be `libbindgen/tests/expectations/tests/my_header.rs`.
5995

6096
Run `cargo test` to compare generated Rust bindings to the expectations.
6197

62-
### Running All Tests <span id="tests-all"/>
98+
### Running All Tests
6399

64100
```
65101
$ cargo test [--features llvm_stable]
66102
```
67103

68-
### Authoring New Tests <span id="tests-new"/>
104+
### Authoring New Tests
69105

70106
To add a new test header to the suite, simply put it in the `tests/headers`
71107
directory. Next, run `bindgen` to generate the initial expected output Rust
@@ -84,7 +120,7 @@ Then verify the new Rust bindings compile and pass some basic tests:
84120
$ cargo test -p tests_expectations
85121
```
86122

87-
## Automatic code formatting <span id="formatting"/>
123+
## Automatic code formatting
88124

89125
There's a `rustfmt.toml` file in the repo. Ideally changes should be consistent
90126
with the style, though that's not enforced right now.
@@ -105,10 +141,10 @@ $ cargo install rustfmt
105141

106142
And ensure `~/.cargo/bin` is on your path.
107143

108-
## Debug Logging <span id="logs"/>
144+
## Debug Logging
109145

110146
To help debug what `bindgen` is doing, you can define the environment variable
111-
`RUST_LOG=bindgen` to get a bunch of debugging log spew.
147+
`RUST_LOG=libbindgen` to get a bunch of debugging log spew.
112148

113149
```
114150
$ RUST_LOG=libbindgen ./target/debug/bindgen [flags...] ~/path/to/some/header.h
@@ -117,10 +153,11 @@ $ RUST_LOG=libbindgen ./target/debug/bindgen [flags...] ~/path/to/some/header.h
117153
This logging can also be used when debugging failing tests:
118154

119155
```
120-
$ RUST_LOG=libbindgen cd libbindgen && cargo test
156+
$ cd libbindgen
157+
$ RUST_LOG=libbindgen cargo test
121158
```
122159

123-
## Using `creduce` to Minimize Test Cases <span id="creduce"/>
160+
## Using `creduce` to Minimize Test Cases
124161

125162
If you are hacking on `bindgen` and find a test case that causes an unexpected
126163
panic, results in bad Rust bindings, or some other incorrectness in `bindgen`,

README.md

Lines changed: 66 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,63 @@
1-
# Servo's rust-bindgen
1+
# `bindgen`
22

3-
A binding generator for the Rust language.
3+
Automatically generates Rust FFI bindings to C and C++ libraries.
44

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 -->
77

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.
108

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)
1229

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 -->
1531

16-
### Installing Clang 3.9
32+
## Usage
1733

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
1942

2043
Download and install the official pre-built binary from
2144
[LLVM download page](http://releases.llvm.org/download.html).
2245

23-
#### OSX
46+
##### OSX
2447

2548
If you use Homebrew:
49+
2650
```
2751
$ brew install llvm
2852
```
2953

3054
If you use MacPorts:
55+
3156
```
3257
$ port install clang-3.9
3358
```
3459

35-
#### Debian-based Linuxes
60+
##### Debian-based Linuxes
3661

3762
```
3863
# 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
4267
version of Ubuntu or other Debian-based distros, you may need to add the LLVM
4368
repos to get version 3.9. See http://apt.llvm.org/.
4469

45-
#### Arch
70+
##### Arch
4671

4772
```
4873
# pacman -S clang
4974
```
5075

51-
#### From source
76+
##### From source
5277

5378
If your package manager doesn't yet offer Clang 3.9, you'll need to build from
5479
source. For that, follow the instructions
@@ -61,31 +86,26 @@ Those instructions list optional steps. For bindgen:
6186
* Checkout and build the compiler-rt
6287
* You do not need to checkout or build libcxx
6388

64-
## Building
89+
### Library usage with `build.rs`
6590

66-
```
67-
$ cd bindgen
68-
$ cargo build
69-
```
91+
#### `build.rs` Tutorial
7092

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]
7794

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`
8498

85-
# Library usage with `build.rs`
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.
86102

87-
See [the Stylo build script][stylo-script] to see how it is used inside the
88-
Servo organisation.
103+
[integration]: ./bindgen-integration
104+
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.
89109

90110
[stylo-script]: https://github.com/servo/servo/blob/master/components/style/build_gecko.rs
91111

@@ -124,28 +144,28 @@ In `src/main.rs`:
124144
include!(concat!(env!("OUT_DIR"), "/example.rs"));
125145
```
126146

127-
# Command Line Usage
147+
### Command Line Usage
128148

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+
```
132152

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`.
134154

135-
## C++ Usage
155+
### C++
136156

137157
This fork of rust-bindgen can handle a number of C++ features.
138158

139159
When passing in header files, the file will automatically be treated as C++ if
140160
it ends in ``.hpp``. If it doesn't, ``-x c++`` can be used to force C++ mode.
141161

142-
## Annotations
162+
### Annotations
143163

144164
The translation of classes, structs, enums, and typedefs can be adjusted using
145165
annotations. Annotations are specifically formatted html tags inside doxygen
146166
style comments.
147167

148-
### `opaque`
168+
#### `opaque`
149169

150170
The `opaque` annotation instructs bindgen to ignore all fields defined in
151171
a struct/class.
@@ -154,7 +174,7 @@ a struct/class.
154174
/// <div rustbindgen opaque></div>
155175
```
156176

157-
### `hide`
177+
#### `hide`
158178

159179
The `hide` annotation instructs bindgen to ignore the struct/class/field/enum
160180
completely.
@@ -163,7 +183,7 @@ completely.
163183
/// <div rustbindgen hide></div>
164184
```
165185

166-
### `replaces`
186+
#### `replaces`
167187

168188
The `replaces` annotation can be used to use a type as a replacement for other
169189
(presumably more complex) type. This is used in Stylo to generate bindings for
@@ -188,7 +208,7 @@ public:
188208
That way, after code generation, the bindings for the `nsTArray` type are
189209
the ones that would be generated for `nsTArray_Simple`.
190210
191-
### `nocopy`
211+
#### `nocopy`
192212
193213
The `nocopy` annotation is used to prevent bindgen to autoderive the `Copy`
194214
and `Clone` traits for a type.

0 commit comments

Comments
 (0)