2017-01-19 19:24:39 +00:00
|
|
|
# Contributing to `bindgen`
|
2016-10-21 20:57:08 +00:00
|
|
|
|
|
|
|
Hi! We'd love to have your contributions! If you want help or mentorship, reach
|
2017-01-19 19:24:39 +00:00
|
|
|
out to us in a GitHub issue, or stop by
|
2021-03-29 11:59:09 +00:00
|
|
|
[#rust on chat.mozilla.org](https://chat.mozilla.org/#/room/#rust:mozilla.org)
|
|
|
|
and introduce yourself.
|
2017-01-19 19:24:39 +00:00
|
|
|
|
|
|
|
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
|
|
|
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
|
|
|
|
|
|
|
- [Code of Conduct](#code-of-conduct)
|
|
|
|
- [Filing an Issue](#filing-an-issue)
|
2017-06-14 21:41:19 +00:00
|
|
|
- [Looking to Start Contributing to `bindgen`?](#looking-to-start-contributing-to-bindgen)
|
2017-01-19 19:24:39 +00:00
|
|
|
- [Building](#building)
|
|
|
|
- [Testing](#testing)
|
|
|
|
- [Overview](#overview)
|
2017-08-10 23:52:08 +00:00
|
|
|
- [Testing Bindings Generation](#testing-bindings-generation)
|
|
|
|
- [Testing Generated Bindings](#testing-generated-bindings)
|
|
|
|
- [Testing a Single Header's Bindings Generation and Compiling its Bindings](#testing-a-single-headers-bindings-generation-and-compiling-its-bindings)
|
2017-01-19 19:24:39 +00:00
|
|
|
- [Authoring New Tests](#authoring-new-tests)
|
2017-05-25 00:31:32 +00:00
|
|
|
- [Test Expectations and `libclang` Versions](#test-expectations-and-libclang-versions)
|
2017-11-16 15:47:16 +00:00
|
|
|
- [Integration Tests](#integration-tests)
|
2017-10-05 17:26:00 +00:00
|
|
|
- [Fuzzing `bindgen` with `csmith`](#fuzzing-bindgen-with-csmith)
|
2017-12-06 05:18:59 +00:00
|
|
|
- [Property tests for `bindgen` with `quickchecking`](#property-tests-for-bindgen-with-quickchecking)
|
2017-09-13 20:05:28 +00:00
|
|
|
- [Code Overview](#code-overview)
|
2023-02-07 15:36:50 +00:00
|
|
|
- [Implementing new options using `syn`](#implementing-new-options-using-syn)
|
2017-06-14 22:06:44 +00:00
|
|
|
- [Pull Requests and Code Reviews](#pull-requests-and-code-reviews)
|
2017-02-17 16:34:37 +00:00
|
|
|
- [Generating Graphviz Dot Files](#generating-graphviz-dot-files)
|
2017-01-19 19:24:39 +00:00
|
|
|
- [Debug Logging](#debug-logging)
|
|
|
|
- [Using `creduce` to Minimize Test Cases](#using-creduce-to-minimize-test-cases)
|
2017-10-10 00:01:31 +00:00
|
|
|
- [Getting `creduce`](#getting-creduce)
|
2017-01-19 19:24:39 +00:00
|
|
|
- [Isolating Your Test Case](#isolating-your-test-case)
|
|
|
|
- [Writing a Predicate Script](#writing-a-predicate-script)
|
2023-02-07 15:36:50 +00:00
|
|
|
- [Cutting a new bindgen release](#cutting-a-new-bindgen-release)
|
|
|
|
- [Updating the changelog](#updating-the-changelog)
|
|
|
|
- [Bumping the version numbers.](#bumping-the-version-numbers)
|
|
|
|
- [Merge to `main`](#merge-to-main)
|
|
|
|
- [Publish and add a git tag for the right commit](#publish-and-add-a-git-tag-for-the-right-commit)
|
2017-01-19 19:24:39 +00:00
|
|
|
|
|
|
|
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
|
|
|
|
|
|
## Code of Conduct
|
2016-10-21 20:57:08 +00:00
|
|
|
|
|
|
|
We abide by the [Rust Code of Conduct][coc] and ask that you do as well.
|
|
|
|
|
|
|
|
[coc]: https://www.rust-lang.org/en-US/conduct.html
|
|
|
|
|
2017-01-19 19:24:39 +00:00
|
|
|
## Filing an Issue
|
2016-10-21 20:57:08 +00:00
|
|
|
|
|
|
|
Think you've found a bug? File an issue! To help us understand and reproduce the
|
|
|
|
issue, provide us with:
|
|
|
|
|
2017-06-14 21:41:03 +00:00
|
|
|
* A (preferably reduced) C/C++ header file that reproduces the issue
|
2016-10-21 20:57:08 +00:00
|
|
|
* The `bindgen` flags used to reproduce the issue with the header file
|
|
|
|
* The expected `bindgen` output
|
|
|
|
* The actual `bindgen` output
|
|
|
|
* The [debugging logs](#logs) generated when running `bindgen` on this testcase
|
|
|
|
|
2017-06-14 21:41:19 +00:00
|
|
|
## Looking to Start Contributing to `bindgen`?
|
|
|
|
|
2019-05-21 00:14:30 +00:00
|
|
|
* [Issues labeled "easy"](https://github.com/rust-lang/rust-bindgen/issues?q=is%3Aopen+is%3Aissue+label%3AE-easy)
|
|
|
|
* [Issues labeled "less easy"](https://github.com/rust-lang/rust-bindgen/issues?q=is%3Aopen+is%3Aissue+label%3AE-less-easy)
|
|
|
|
* [Issues labeled "help wanted"](https://github.com/rust-lang/rust-bindgen/labels/help%20wanted)
|
|
|
|
* Still can't find something to work on? [Drop a comment here](https://github.com/rust-lang/rust-bindgen/issues/747)
|
2017-06-14 21:41:19 +00:00
|
|
|
|
2017-01-19 19:24:39 +00:00
|
|
|
## Building
|
2016-10-21 20:57:08 +00:00
|
|
|
|
2017-01-23 18:43:04 +00:00
|
|
|
To build the `bindgen` library and the `bindgen` executable:
|
2016-10-21 20:57:08 +00:00
|
|
|
|
2017-01-19 19:47:48 +00:00
|
|
|
```
|
|
|
|
$ cargo build
|
|
|
|
```
|
|
|
|
|
|
|
|
If you installed multiple versions of llvm, it may not be able to locate the
|
|
|
|
latest version of libclang. In that case, you may want to either uninstall other
|
|
|
|
versions of llvm, or specify the path of the desired libclang explicitly:
|
|
|
|
|
|
|
|
```
|
2022-02-02 07:19:43 +00:00
|
|
|
$ export LIBCLANG_PATH=path/to/clang-9.0/lib
|
2017-01-19 19:47:48 +00:00
|
|
|
```
|
|
|
|
|
2017-04-06 21:38:05 +00:00
|
|
|
Additionally, you may want to build and test with the `testing_only_docs`
|
|
|
|
feature to ensure that you aren't forgetting to document types and functions. CI
|
|
|
|
will catch it if you forget, but the turn around will be a lot slower ;)
|
2016-10-24 21:58:10 +00:00
|
|
|
|
2016-10-30 14:21:48 +00:00
|
|
|
```
|
2017-04-06 21:38:05 +00:00
|
|
|
$ cargo build --features testing_only_docs
|
2016-10-30 14:21:48 +00:00
|
|
|
```
|
|
|
|
|
2017-01-19 19:24:39 +00:00
|
|
|
## Testing
|
2016-10-21 20:57:08 +00:00
|
|
|
|
2017-01-19 19:24:39 +00:00
|
|
|
### Overview
|
2016-10-21 20:57:08 +00:00
|
|
|
|
2023-01-07 18:41:15 +00:00
|
|
|
Input C/C++ test headers reside in the `bindgen-tests/tests/headers` directory. Expected
|
|
|
|
output Rust bindings live in `bindgen-tests/tests/expectations/tests`. For example,
|
|
|
|
`bindgen-tests/tests/headers/my_header.h`'s expected generated Rust bindings would be
|
|
|
|
`bindgen-tests/tests/expectations/tests/my_header.rs`.
|
2017-01-22 11:58:12 +00:00
|
|
|
|
2017-11-16 15:47:16 +00:00
|
|
|
There are also some integration tests in the `./bindgen-integration` crate, which uses `bindgen` to
|
2017-08-10 23:52:08 +00:00
|
|
|
generate bindings to some C++ code, and then uses the bindings, asserting that
|
2017-11-16 15:47:16 +00:00
|
|
|
values are what we expect them to be, both on the Rust and C++ side.
|
2016-10-21 20:57:08 +00:00
|
|
|
|
2017-09-05 16:59:01 +00:00
|
|
|
The generated and expected bindings are run through `rustfmt` before they are
|
|
|
|
compared. Make sure you have `rustfmt` up to date:
|
|
|
|
|
|
|
|
```
|
|
|
|
$ rustup update nightly
|
2021-07-25 22:27:07 +00:00
|
|
|
$ rustup component add rustfmt --toolchain nightly
|
2017-09-05 16:59:01 +00:00
|
|
|
```
|
|
|
|
|
2017-11-16 15:47:16 +00:00
|
|
|
Note: running `cargo test` from the root directory of `bindgen`'s repository does not
|
|
|
|
automatically test the generated bindings or run the integration tests.
|
|
|
|
These steps must be performed manually when needed.
|
|
|
|
|
2017-08-10 23:52:08 +00:00
|
|
|
### Testing Bindings Generation
|
2016-10-21 20:57:08 +00:00
|
|
|
|
2023-01-07 18:41:15 +00:00
|
|
|
To regenerate bindings from the corpus of test headers in `bindgen-tests/tests/headers` and
|
|
|
|
compare them against the expected bindings in `bindgen-tests/tests/expectations/tests`, run:
|
2016-10-21 20:57:08 +00:00
|
|
|
|
|
|
|
```
|
2017-08-10 23:52:08 +00:00
|
|
|
$ cargo test
|
2016-10-21 20:57:08 +00:00
|
|
|
```
|
|
|
|
|
2017-08-10 23:52:08 +00:00
|
|
|
As long as you aren't making any changes to `bindgen`'s output, running this
|
|
|
|
should be sufficient to test your local modifications.
|
2017-05-25 00:31:32 +00:00
|
|
|
|
2019-01-08 18:26:57 +00:00
|
|
|
You may set the `BINDGEN_OVERWRITE_EXPECTED` environment variable to overwrite
|
|
|
|
the expected bindings with `bindgen`'s current output:
|
|
|
|
|
|
|
|
```
|
2020-06-29 01:57:07 +00:00
|
|
|
$ BINDGEN_OVERWRITE_EXPECTED=1 cargo test
|
2019-01-08 18:26:57 +00:00
|
|
|
```
|
|
|
|
|
2020-11-17 19:47:44 +00:00
|
|
|
If you set the BINDGEN_TESTS_DIFFTOOL environment variable, `cargo test` will
|
|
|
|
execute $BINDGEN_TESTS_DIFFTOOL /path/of/expected/output /path/of/actual/output
|
|
|
|
when the expected output differs from the actual output. You can use this to
|
|
|
|
hand check differences by setting it to e.g. "meld" (assuming you have meld
|
|
|
|
installed).
|
|
|
|
|
2020-06-29 01:57:07 +00:00
|
|
|
If you're not changing command line arguments, you may want to set
|
|
|
|
`BINDGEN_DISABLE_ROUNDTRIP_TEST` to avoid a lot of tests for round-tripping of
|
|
|
|
those.
|
|
|
|
|
2017-08-10 23:52:08 +00:00
|
|
|
### Testing Generated Bindings
|
2017-05-25 00:31:32 +00:00
|
|
|
|
2017-08-10 23:52:08 +00:00
|
|
|
If your local changes are introducing expected modifications in the
|
2023-01-07 18:41:15 +00:00
|
|
|
`bindgen-tests/tests/expectations/tests/*` bindings files, then you should test that the
|
2017-08-10 23:52:08 +00:00
|
|
|
generated bindings files still compile, and that their struct layout tests still
|
2017-11-16 15:47:16 +00:00
|
|
|
pass. Also, run the integration tests (see below).
|
2017-05-25 00:31:32 +00:00
|
|
|
|
2017-08-10 23:52:08 +00:00
|
|
|
You can do this with these commands:
|
2017-03-15 22:11:48 +00:00
|
|
|
|
2017-08-10 23:52:08 +00:00
|
|
|
```
|
2023-01-07 18:41:15 +00:00
|
|
|
$ cd bindgen-tests/tests/expectations
|
2017-08-10 23:52:08 +00:00
|
|
|
$ cargo test
|
|
|
|
```
|
|
|
|
|
|
|
|
### Testing a Single Header's Bindings Generation and Compiling its Bindings
|
|
|
|
|
2022-08-11 19:14:23 +00:00
|
|
|
Note: You will to need to install [Graphviz](https://graphviz.org/) since that
|
|
|
|
is a dependency for running `test-one.sh`.
|
|
|
|
|
2017-08-10 23:52:08 +00:00
|
|
|
Sometimes its useful to work with one test header from start (generating
|
|
|
|
bindings for it) to finish (compiling the bindings and running their layout
|
2023-01-07 18:41:15 +00:00
|
|
|
tests). This can be done with the `bindgen-tests/tests/test-one.sh` script. It supports fuzzy
|
2017-08-10 23:52:08 +00:00
|
|
|
searching for test headers. For example, to test
|
2017-03-15 22:11:48 +00:00
|
|
|
`tests/headers/what_is_going_on.hpp`, execute this command:
|
|
|
|
|
|
|
|
```
|
2023-01-07 18:41:15 +00:00
|
|
|
$ ./bindgen-tests/tests/test-one.sh going
|
2017-03-15 22:11:48 +00:00
|
|
|
```
|
|
|
|
|
2017-11-16 15:47:16 +00:00
|
|
|
Note that `test-one.sh` does not recompile `bindgen`, so if you change the code,
|
|
|
|
you'll need to rebuild it before running the script again.
|
|
|
|
|
2017-01-19 19:24:39 +00:00
|
|
|
### Authoring New Tests
|
2016-10-21 20:57:08 +00:00
|
|
|
|
2023-01-07 18:41:15 +00:00
|
|
|
To add a new test header to the suite, simply put it in the `bindgen-tests/tests/headers`
|
2016-11-15 03:37:20 +00:00
|
|
|
directory. Next, run `bindgen` to generate the initial expected output Rust
|
2023-01-07 18:41:15 +00:00
|
|
|
bindings. Put those in `bindgen-tests/tests/expectations/tests`.
|
2016-10-21 20:57:08 +00:00
|
|
|
|
|
|
|
If your new test requires certain flags to be passed to `bindgen`, you can
|
|
|
|
specify them at the top of the test header, with a comment like this:
|
|
|
|
|
2017-08-10 23:52:08 +00:00
|
|
|
`new_test_header.hpp`:
|
|
|
|
|
2016-10-21 20:57:08 +00:00
|
|
|
```c
|
|
|
|
// bindgen-flags: --enable-cxx-namespaces -- -std=c++14
|
|
|
|
```
|
|
|
|
|
2017-08-10 23:52:08 +00:00
|
|
|
Then verify the new Rust bindings compile and pass their layout tests:
|
2016-11-05 09:44:24 +00:00
|
|
|
|
|
|
|
```
|
2023-01-07 18:41:15 +00:00
|
|
|
$ cd bindgen-tests/tests/expectations
|
2017-08-10 23:52:08 +00:00
|
|
|
$ cargo test new_test_header
|
2016-11-05 09:44:24 +00:00
|
|
|
```
|
|
|
|
|
2017-05-25 00:31:32 +00:00
|
|
|
### Test Expectations and `libclang` Versions
|
|
|
|
|
|
|
|
If a test generates different bindings across different `libclang` versions (for
|
|
|
|
example, because we take advantage of better/newer APIs when possible), then you
|
|
|
|
can add multiple test expectations, one for each supported `libclang`
|
2023-01-07 18:41:15 +00:00
|
|
|
version. Instead of having a single `bindgen-tests/tests/expectations/tests/my_test.rs` file,
|
2017-05-25 00:31:32 +00:00
|
|
|
add each of:
|
|
|
|
|
2023-01-07 18:41:15 +00:00
|
|
|
* `bindgen-tests/tests/expectations/tests/libclang-9/my_test.rs`
|
|
|
|
* `bindgen-tests/tests/expectations/tests/libclang-5/my_test.rs`
|
2017-05-25 00:31:32 +00:00
|
|
|
|
|
|
|
If you need to update the test expectations for a test file that generates
|
|
|
|
different bindings for different `libclang` versions, you *don't* need to have
|
|
|
|
many version of `libclang` installed locally. Just make a work-in-progress pull
|
|
|
|
request, and then when Travis CI fails, it will log a diff of the
|
|
|
|
expectations. Use the diff to patch the appropriate expectation file locally and
|
|
|
|
then update your pull request.
|
|
|
|
|
2017-08-10 23:52:08 +00:00
|
|
|
Usually, `bindgen`'s test runner can infer which version of `libclang` you
|
|
|
|
have. If for some reason it can't, you can force a specific `libclang` version
|
|
|
|
to check the bindings against with a cargo feature:
|
|
|
|
|
|
|
|
```
|
|
|
|
$ cargo test --features testing_only_libclang_$VERSION
|
|
|
|
```
|
|
|
|
|
|
|
|
Where `$VERSION` is one of:
|
|
|
|
|
|
|
|
* `4`
|
|
|
|
* `3_9`
|
|
|
|
* `3_8`
|
|
|
|
|
|
|
|
depending on which version of `libclang` you have installed.
|
|
|
|
|
2017-11-16 15:47:16 +00:00
|
|
|
### Integration Tests
|
|
|
|
|
|
|
|
The `./bindgen-integration` crate uses `bindgen` to
|
|
|
|
generate bindings to some C++ code, and then uses the bindings, asserting that
|
|
|
|
values are what we expect them to be, both on the Rust and C++ side.
|
|
|
|
|
|
|
|
To run the integration tests, issue the following:
|
|
|
|
|
|
|
|
```
|
|
|
|
$ cd bindgen-integration
|
|
|
|
$ cargo test
|
|
|
|
```
|
|
|
|
|
2017-10-05 17:26:00 +00:00
|
|
|
### Fuzzing `bindgen` with `csmith`
|
|
|
|
|
|
|
|
We <3 finding hidden bugs and the people who help us find them! One way to help
|
|
|
|
uncover hidden bugs is by running `csmith` to generate random headers to test
|
|
|
|
`bindgen` against.
|
|
|
|
|
|
|
|
See [./csmith-fuzzing/README.md](./csmith-fuzzing/README.md) for details.
|
|
|
|
|
2017-12-06 05:18:59 +00:00
|
|
|
### Property tests for `bindgen` with `quickchecking`
|
|
|
|
|
2017-12-09 06:34:52 +00:00
|
|
|
The `tests/quickchecking` crate generates property tests for `bindgen`.
|
2017-12-06 05:18:59 +00:00
|
|
|
From the crate's directory you can run the tests with `cargo run`. For details
|
2021-11-26 01:42:47 +00:00
|
|
|
on additional configuration including how to preserve / inspect the generated
|
|
|
|
property tests, see
|
2017-12-06 05:18:59 +00:00
|
|
|
[./tests/quickchecking/README.md](./tests/quickchecking/README.md).
|
|
|
|
|
2017-09-13 20:05:28 +00:00
|
|
|
## Code Overview
|
|
|
|
|
|
|
|
`bindgen` takes C and C++ header files as input and generates corresponding Rust
|
|
|
|
`#[repr(C)]` type definitions and `extern` foreign function declarations.
|
|
|
|
|
|
|
|
First, we use `libclang` to parse the input headers. See `src/clang.rs` for our
|
|
|
|
Rust-y wrappers over the raw C `libclang` API that the `clang-sys` crate
|
|
|
|
exposes. We walk over `libclang`'s AST and construct our own internal
|
|
|
|
representation (IR). The `ir` module and submodules (`src/ir/*`) contain the IR
|
|
|
|
type definitions and `libclang` AST into IR parsing code.
|
|
|
|
|
|
|
|
The umbrella IR type is the `Item`. It contains various nested `enum`s that let
|
|
|
|
us drill down and get more specific about the kind of construct that we're
|
|
|
|
looking at. Here is a summary of the IR types and their relationships:
|
|
|
|
|
|
|
|
* `Item` contains:
|
|
|
|
* An `ItemId` to uniquely identify it.
|
|
|
|
* An `ItemKind`, which is one of:
|
|
|
|
* A `Module`, which is originally a C++ namespace and becomes a Rust
|
|
|
|
module. It contains the set of `ItemId`s of `Item`s that are defined
|
|
|
|
within it.
|
|
|
|
* A `Type`, which contains:
|
|
|
|
* A `Layout`, describing the type's size and alignment.
|
|
|
|
* A `TypeKind`, which is one of:
|
|
|
|
* Some integer type.
|
|
|
|
* Some float type.
|
|
|
|
* A `Pointer` to another type.
|
|
|
|
* A function pointer type, with `ItemId`s of its parameter types
|
|
|
|
and return type.
|
|
|
|
* An `Alias` to another type (`typedef` or `using X = ...`).
|
|
|
|
* A fixed size `Array` of `n` elements of another type.
|
|
|
|
* A `Comp` compound type, which is either a `struct`, `class`,
|
|
|
|
or `union`. This is potentially a template definition.
|
|
|
|
* A `TemplateInstantiation` referencing some template definition
|
|
|
|
and a set of template argument types.
|
|
|
|
* Etc...
|
|
|
|
* A `Function`, which contains:
|
|
|
|
* An ABI
|
|
|
|
* A mangled name
|
|
|
|
* a `FunctionKind`, which describes whether this function is a plain
|
|
|
|
function, method, static method, constructor, destructor, etc.
|
|
|
|
* The `ItemId` of its function pointer type.
|
|
|
|
* A `Var` representing a static variable or `#define` constant, which
|
|
|
|
contains:
|
|
|
|
* Its type's `ItemId`
|
|
|
|
* Optionally, a mangled name
|
|
|
|
* Optionally, a value
|
2021-11-26 01:42:47 +00:00
|
|
|
* An optional `clang::SourceLocation` that holds the first source code
|
|
|
|
location where the `Item` was encountered.
|
2017-09-13 20:05:28 +00:00
|
|
|
|
|
|
|
The IR forms a graph of interconnected and inter-referencing types and
|
|
|
|
functions. The `ir::traversal` module provides IR graph traversal
|
|
|
|
infrastructure: edge kind definitions (base member vs field type vs function
|
|
|
|
parameter, etc...), the `Trace` trait to enumerate an IR thing's outgoing edges,
|
|
|
|
various traversal types.
|
|
|
|
|
|
|
|
After constructing the IR, we run a series of analyses on it. These analyses do
|
|
|
|
everything from allocate logical bitfields into physical units, compute for
|
|
|
|
which types we can `#[derive(Debug)]`, to determining which implicit template
|
|
|
|
parameters a given type uses. The analyses are defined in
|
|
|
|
`src/ir/analysis/*`. They are implemented as fixed-point algorithms, using the
|
|
|
|
`ir::analysis::MonotoneFramework` trait.
|
|
|
|
|
|
|
|
The final phase is generating Rust source text from the analyzed IR, and it is
|
|
|
|
defined in `src/codegen/*`. We use the `quote` crate, which provides the `quote!
|
2022-09-06 21:34:58 +00:00
|
|
|
{ ... }` macro for quasi-quoting Rust forms. Some options that affect the
|
|
|
|
generated Rust code are implemented using the [`syn`](https://docs.rs/syn) crate.
|
|
|
|
|
|
|
|
### Implementing new options using `syn`
|
|
|
|
|
2022-09-23 20:24:19 +00:00
|
|
|
If a new option can be implemented using the `syn` crate it should be added to
|
|
|
|
the `codegen::postprocessing` module by following these steps:
|
2022-09-06 21:34:58 +00:00
|
|
|
|
2022-09-26 15:55:05 +00:00
|
|
|
- Introduce a new field to `BindgenOptions` for the option.
|
|
|
|
- Write a free function inside `codegen::postprocessing` implementing the
|
|
|
|
option. This function with the same name of the `BindgenOptions` field.
|
|
|
|
- Add a new value to the `codegen::postprocessing::PASSES` for the option using
|
|
|
|
the `pass!` macro.
|
2017-09-13 20:05:28 +00:00
|
|
|
|
2017-06-14 22:06:44 +00:00
|
|
|
## Pull Requests and Code Reviews
|
|
|
|
|
|
|
|
Ensure that each commit stands alone, and passes tests. This enables better `git
|
|
|
|
bisect`ing when needed. If your commits do not stand on their own, then rebase
|
2023-01-18 17:23:13 +00:00
|
|
|
them on top of the latest main and squash them into a single commit.
|
2017-06-14 22:06:44 +00:00
|
|
|
|
|
|
|
All pull requests undergo code review before merging. To request review, comment
|
|
|
|
`r? @github_username_of_reviewer`. They we will respond with `r+` to approve the
|
|
|
|
pull request, or may leave feedback and request changes to the pull request. Any
|
|
|
|
changes should be squashed into the original commit.
|
|
|
|
|
|
|
|
Unsure who to ask for review? Ask any of:
|
|
|
|
|
|
|
|
* `@emilio`
|
|
|
|
* `@fitzgen`
|
|
|
|
|
|
|
|
More resources:
|
|
|
|
|
|
|
|
* [Servo's GitHub Workflow](https://github.com/servo/servo/wiki/Github-workflow)
|
|
|
|
* [Beginner's Guide to Rebasing and Squashing](https://github.com/servo/servo/wiki/Beginner's-guide-to-rebasing-and-squashing)
|
|
|
|
|
2017-02-17 16:34:37 +00:00
|
|
|
## Generating Graphviz Dot Files
|
|
|
|
|
|
|
|
We can generate [Graphviz](http://graphviz.org/pdf/dotguide.pdf) dot files from
|
|
|
|
our internal representation of a C/C++ input header, and then you can create a
|
|
|
|
PNG or PDF from it with Graphviz's `dot` program. This is very useful when
|
|
|
|
debugging bindgen!
|
|
|
|
|
|
|
|
First, make sure you have Graphviz and `dot` installed:
|
|
|
|
|
|
|
|
```
|
|
|
|
$ brew install graphviz # OS X
|
|
|
|
$ sudo dnf install graphviz # Fedora
|
|
|
|
$ # Etc...
|
|
|
|
```
|
|
|
|
|
|
|
|
Then, use the `--emit-ir-graphviz` flag to generate a `dot` file from our IR:
|
|
|
|
|
|
|
|
```
|
|
|
|
$ cargo run -- example.hpp --emit-ir-graphviz output.dot
|
|
|
|
```
|
|
|
|
|
|
|
|
Finally, convert the `dot` file to an image:
|
|
|
|
|
|
|
|
```
|
|
|
|
$ dot -Tpng output.dot -o output.png
|
|
|
|
```
|
|
|
|
|
|
|
|
The final result will look something like this:
|
|
|
|
|
|
|
|
[![An example graphviz rendering of our IR](./example-graphviz-ir.png)](./example-graphviz-ir.png)
|
|
|
|
|
2017-01-19 19:24:39 +00:00
|
|
|
## Debug Logging
|
2016-10-21 20:57:08 +00:00
|
|
|
|
|
|
|
To help debug what `bindgen` is doing, you can define the environment variable
|
2017-01-22 11:58:12 +00:00
|
|
|
`RUST_LOG=bindgen` to get a bunch of debugging log spew.
|
2016-10-21 20:57:08 +00:00
|
|
|
|
|
|
|
```
|
2017-01-22 11:58:12 +00:00
|
|
|
$ RUST_LOG=bindgen ./target/debug/bindgen [flags...] ~/path/to/some/header.h
|
2016-10-21 20:57:08 +00:00
|
|
|
```
|
|
|
|
|
2016-11-15 03:37:20 +00:00
|
|
|
This logging can also be used when debugging failing tests:
|
2016-10-21 20:57:08 +00:00
|
|
|
|
|
|
|
```
|
2017-01-22 11:58:12 +00:00
|
|
|
$ RUST_LOG=bindgen cargo test
|
2016-10-21 20:57:08 +00:00
|
|
|
```
|
2016-11-11 00:04:08 +00:00
|
|
|
|
2017-01-19 19:24:39 +00:00
|
|
|
## Using `creduce` to Minimize Test Cases
|
2016-11-11 00:04:08 +00:00
|
|
|
|
2017-10-10 00:01:31 +00:00
|
|
|
If you find a test case that triggers an unexpected panic in `bindgen`, causes
|
|
|
|
`bindgen` to emit bindings that won't compile, define structs with the wrong
|
|
|
|
size/alignment, or results in any other kind of incorrectness, then using
|
|
|
|
`creduce` can help reduce the test case to a minimal one that still exhibits
|
|
|
|
that same bad behavior.
|
2016-11-11 00:04:08 +00:00
|
|
|
|
2017-10-10 00:01:31 +00:00
|
|
|
***Reduced test cases are SUPER helpful when filing bug reports!***
|
|
|
|
|
|
|
|
### Getting `creduce`
|
|
|
|
|
|
|
|
Often, you can install `creduce` from your OS's package manager:
|
|
|
|
|
|
|
|
```
|
|
|
|
$ sudo apt install creduce
|
|
|
|
$ brew install creduce
|
|
|
|
$ # Etc...
|
|
|
|
```
|
|
|
|
|
2023-01-18 17:23:13 +00:00
|
|
|
Otherwise, follow [these instructions](https://github.com/csmith-project/creduce/blob/main/INSTALL.md) for building and/or installing `creduce`.
|
2016-11-11 00:04:08 +00:00
|
|
|
|
|
|
|
Running `creduce` requires two things:
|
|
|
|
|
|
|
|
1. Your isolated test case, and
|
|
|
|
|
|
|
|
2. A script to act as a predicate script describing whether the behavior you're
|
|
|
|
trying to isolate occurred.
|
|
|
|
|
|
|
|
With those two things in hand, running `creduce` looks like this:
|
|
|
|
|
2017-10-10 00:01:31 +00:00
|
|
|
$ creduce ./predicate.sh ./isolated-test-case.h
|
2016-11-11 00:04:08 +00:00
|
|
|
|
|
|
|
### Isolating Your Test Case
|
|
|
|
|
2017-07-13 21:28:44 +00:00
|
|
|
If you're using `bindgen` as a command line tool, pass
|
|
|
|
`--dump-preprocessed-input` flag.
|
2016-11-11 00:04:08 +00:00
|
|
|
|
2017-07-13 21:28:44 +00:00
|
|
|
If you're using `bindgen` as a Rust library, invoke the
|
|
|
|
`bindgen::Builder::dump_preprocessed_input` method where you call
|
|
|
|
`bindgen::Builder::generate`.
|
2016-11-11 00:04:08 +00:00
|
|
|
|
2017-07-13 21:28:44 +00:00
|
|
|
Afterwards, there should be a `__bindgen.i` or `__bindgen.ii` file containing
|
|
|
|
the combined and preprocessed input headers, which is usable as an isolated,
|
|
|
|
standalone test case.
|
|
|
|
|
2016-11-11 00:04:08 +00:00
|
|
|
### Writing a Predicate Script
|
|
|
|
|
2017-10-10 00:01:31 +00:00
|
|
|
Writing a `predicate.sh` script for a `bindgen` test case is straightforward. We
|
|
|
|
already have a general purpose predicate script that you can use, you just have
|
|
|
|
to wrap and configure it.
|
2016-11-11 00:04:08 +00:00
|
|
|
|
|
|
|
```bash
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# Exit the script with a nonzero exit code if:
|
|
|
|
# * any individual command finishes with a nonzero exit code, or
|
|
|
|
# * we access any undefined variable.
|
|
|
|
set -eu
|
|
|
|
|
2017-10-10 00:01:31 +00:00
|
|
|
# Invoke the general purpose predicate script that comes in the
|
|
|
|
# `bindgen` repository.
|
|
|
|
#
|
|
|
|
# You'll need to replace `--whatever-flags` with things that are specific to the
|
|
|
|
# incorrectness you're trying to pin down. See below for details.
|
|
|
|
path/to/rust-bindgen/csmith-fuzzing/predicate.py \
|
|
|
|
--whatever-flags \
|
|
|
|
./isolated-test-case.h
|
|
|
|
```
|
|
|
|
|
|
|
|
When hunting down a particular panic emanating from inside `bindgen`, you can
|
|
|
|
invoke `predicate.py` like this:
|
2016-11-11 00:04:08 +00:00
|
|
|
|
2017-10-10 00:01:31 +00:00
|
|
|
```bash
|
|
|
|
path/to/rust-bindgen/csmith-fuzzing/predicate.py \
|
|
|
|
--expect-bindgen-fail \
|
|
|
|
--bindgen-grep "thread main panicked at '<insert panic message here>'" \
|
|
|
|
./isolated-test-case.h
|
|
|
|
```
|
2016-11-11 00:04:08 +00:00
|
|
|
|
2017-10-10 00:01:31 +00:00
|
|
|
Alternatively, when hunting down a bad `#[derive(Eq)]` that is causing `rustc`
|
|
|
|
to fail to compile `bindgen`'s emitted bindings, you can invoke `predicate.py`
|
|
|
|
like this:
|
2016-11-11 00:04:08 +00:00
|
|
|
|
2017-10-10 00:01:31 +00:00
|
|
|
```bash
|
|
|
|
path/to/rust-bindgen/csmith-fuzzing/predicate.py \
|
|
|
|
--bindings-grep NameOfTheStructThatIsErroneouslyDerivingEq \
|
|
|
|
--expect-compile-fail \
|
|
|
|
--rustc-grep 'error[E0277]: the trait bound `f64: std::cmp::Eq` is not satisfied' \
|
|
|
|
./isolated-test-case.h
|
2016-11-11 00:04:08 +00:00
|
|
|
```
|
|
|
|
|
2017-10-10 00:01:31 +00:00
|
|
|
Or, when minimizing a failing layout test in the compiled bindings, you can
|
|
|
|
invoke `predicate.py` like this:
|
2016-11-11 00:04:08 +00:00
|
|
|
|
2017-10-10 00:01:31 +00:00
|
|
|
```bash
|
|
|
|
path/to/rust-bindgen/csmith-fuzzing/predicate.py \
|
|
|
|
--bindings-grep MyStruct \
|
|
|
|
--expect-layout-tests-fail \
|
|
|
|
--layout-tests-grep "thread 'bindgen_test_layout_MyStruct' panicked" \
|
|
|
|
./isolated-test-case.h
|
|
|
|
```
|
|
|
|
|
|
|
|
For details on all the flags that you can pass to `predicate.py`, run:
|
2016-11-11 00:04:08 +00:00
|
|
|
|
2017-10-10 00:01:31 +00:00
|
|
|
```
|
|
|
|
$ path/to/rust-bindgen/csmith-fuzzing/predicate.py --help
|
|
|
|
```
|
2016-11-11 00:04:08 +00:00
|
|
|
|
2017-10-10 00:01:31 +00:00
|
|
|
And you can always write your own, arbitrary predicate script if you prefer.
|
|
|
|
(Although, maybe we should add extra functionality to `predicate.py` -- file an
|
|
|
|
issue if you think so!)
|
2016-11-11 00:04:08 +00:00
|
|
|
|
2017-10-10 00:01:31 +00:00
|
|
|
`creduce` is *really* helpful and can cut hundreds of thousands of lines of test
|
|
|
|
case down to 5 lines.
|
2016-11-11 00:04:08 +00:00
|
|
|
|
|
|
|
Happy bug hunting and test case reducing!
|
|
|
|
|
|
|
|
[More information on using `creduce`.](https://embed.cs.utah.edu/creduce/using/)
|
2022-11-22 18:27:27 +00:00
|
|
|
|
|
|
|
## Cutting a new bindgen release
|
|
|
|
|
|
|
|
To cut a release, the following needs to happen:
|
|
|
|
|
|
|
|
### Updating the changelog
|
|
|
|
|
|
|
|
Update the CHANGELOG.md file with the changes from the last release. Something
|
|
|
|
like the following is a useful way to check what has landed:
|
|
|
|
|
|
|
|
```
|
|
|
|
$ git log --oneline v0.62.0..HEAD
|
|
|
|
```
|
|
|
|
|
|
|
|
Also worth checking the [next-release tag](https://github.com/rust-lang/rust-bindgen/pulls?q=is%3Apr+label%3Anext-release).
|
|
|
|
|
|
|
|
Once that's done and the changelog is up-to-date, run `doctoc` on it.
|
|
|
|
|
|
|
|
If needed, install it locally by running:
|
|
|
|
|
|
|
|
```
|
|
|
|
$ npm install doctoc
|
|
|
|
$ ./node_modules/doctoc/doctoc.js CHANGELOG.md
|
|
|
|
```
|
|
|
|
|
|
|
|
### Bumping the version numbers.
|
|
|
|
|
|
|
|
Bump version numbers as needed. Run tests just to ensure everything is working
|
|
|
|
as expected.
|
|
|
|
|
2023-01-18 17:23:13 +00:00
|
|
|
### Merge to `main`
|
2022-11-22 18:27:27 +00:00
|
|
|
|
2023-01-18 17:23:13 +00:00
|
|
|
For regular releases, the changes above should end up in `main` before
|
2022-11-22 18:27:27 +00:00
|
|
|
publishing. For dot-releases of an old version (e.g., cherry-picking an
|
|
|
|
important fix) you can skip this.
|
|
|
|
|
|
|
|
### Publish and add a git tag for the right commit
|
|
|
|
|
|
|
|
Once you're in the right commit, do:
|
|
|
|
|
|
|
|
```
|
|
|
|
$ git tag -a v0.62.1 # With the right version of course
|
|
|
|
$ pushd bindgen && cargo publish && popd
|
|
|
|
$ pushd bindgen-cli && cargo publish && popd
|
|
|
|
$ git push --tags upstream # To publish the tag
|
|
|
|
```
|