third_party_rust_bindgen/tests/test-one.sh
Nick Fitzgerald 61743aa190 Use quote instead of syntex for Rust code generation
The `syntex` crate is unmaintained. It is slow to build, and additionally it
requires that we pre-process `src/codegen/mod.rs` before we build the `bindgen`
crate.

The `quote` crate provides similar quasi-quoting functionality, is maintained,
and builds faster. It doesn't have a typed API or builders, however; it only
deals with tokens.

Before this commit:

```
$ cargo clean; cargo build
<snip>
    Finished dev [unoptimized + debuginfo] target(s) in 98.75 secs
```

After this commit:

```
$ cargo clean; cargo build
<snip>
    Finished dev [unoptimized + debuginfo] target(s) in 46.26 secs
```

Build time is cut in half! But what about run time?

Before this commit:

```
Generated Stylo bindings in: Duration { secs: 3, nanos: 521105668 }
```

After this commit:

```
Generated Stylo bindings in: Duration { secs: 3, nanos: 548797242 }
```

So it appears to be about 20ms slower at generating Stylo bindings, but I
suspect this is well within the noise.

Finally, this also lets us remove that nasty `mem::transmute` inside
`bindgen::ir::BindgenContext::gen` that was used for the old `syntex`
context. Now `BindgenContext` doesn't have a lifetime parameter either. This
should make it easier to revisit doing our analyses in parallel with `rayon`,
since that context was one of the things that made it hard for `BindgenContext`
to implement `Sync`.

Fixes #925
2017-09-07 10:52:31 -07:00

99 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# Usage:
#
# ./tests/test-one.sh <fuzzy-name>
#
# Generate bindings for the first match of `./tests/headers/*<fuzzy-name>*`, use
# `rustc` to compile the bindings with unit tests enabled, and run the generated
# layout tests.
set -eu
if [ $# -ne 1 ]; then
echo "Usage: $0 <fuzzy-name>"
exit 1
fi
cd "$(dirname "$0")"
cd ..
export RUST_BACKTRACE=1
unique_fuzzy_file() {
local pattern="$1"
local results="$(find ./tests/headers -type f | egrep -i "$pattern")"
local num_results=$(echo "$results" | wc -l)
if [[ -z "$results" ]]; then
>&2 echo "ERROR: no files found with pattern \"$pattern\""
exit 1
elif [[ "$num_results" -ne 1 ]]; then
>&2 echo "ERROR: Expected exactly 1 result, got $num_results:"
>&2 echo "$results"
exit 1
fi
echo "$results"
}
TEST="$(unique_fuzzy_file "$1")"
BINDINGS=$(mktemp -t bindings.rs.XXXXXX)
TEST_BINDINGS_BINARY=$(mktemp -t bindings.XXXXXX)
FLAGS="$(grep "// bindgen-flags: " "$TEST" || echo)"
FLAGS="${FLAGS/\/\/ bindgen\-flags:/}"
# Prepend the default flags added in test.rs's `create_bindgen_builder`.
FLAGS="--rustfmt-bindings --with-derive-default --raw-line '' --raw-line '#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]' --raw-line '' $FLAGS"
eval ./target/debug/bindgen \
"\"$TEST\"" \
--emit-ir \
--emit-ir-graphviz ir.dot \
--emit-clang-ast \
-o "\"$BINDINGS\"" \
$FLAGS
rustup run nightly rustfmt "$BINDINGS" || true
dot -Tpng ir.dot -o ir.png
echo
echo "=== Input header ========================================================"
echo
cat "$TEST"
echo
echo "=== Generated bindings =================================================="
echo
cat "$BINDINGS"
echo
echo "=== Diff w/ expected bindings ==========================================="
echo
EXPECTED=${TEST/headers/expectations\/tests}
EXPECTED=${EXPECTED/.hpp/.rs}
EXPECTED=${EXPECTED/.h/.rs}
rustup run nightly rustfmt "$EXPECTED" || true
# Don't exit early if there is a diff.
diff -U8 "$EXPECTED" "$BINDINGS" || true
echo
echo "=== Building bindings ==================================================="
echo
rustc --test -o "$TEST_BINDINGS_BINARY" "$BINDINGS" --crate-name bindgen_test_one
echo
echo "=== Testing bindings ===================================================="
echo
"$TEST_BINDINGS_BINARY"