Skip to content

Commit 468b9af

Browse files
authored
chore(ffi): Revamp gen_header using cargo-expand (#3092)
1 parent 3e41a4e commit 468b9af

File tree

4 files changed

+25
-84
lines changed

4 files changed

+25
-84
lines changed

.github/workflows/CI.yml

+7-7
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,6 @@ jobs:
167167
- name: Install Rust
168168
uses: dtolnay/rust-toolchain@stable
169169

170-
- name: Install cbindgen
171-
uses: actions-rs/cargo@v1
172-
with:
173-
command: install
174-
args: cbindgen
175-
176170
- name: Build FFI
177171
uses: actions-rs/cargo@v1
178172
env:
@@ -190,7 +184,7 @@ jobs:
190184
RUSTFLAGS: --cfg hyper_unstable_ffi
191185
with:
192186
command: test
193-
args: --features full,ffi --lib
187+
args: --features server,client,http1,http2,ffi --lib
194188

195189
ffi-header:
196190
name: Verify hyper.h is up to date
@@ -208,6 +202,12 @@ jobs:
208202
command: install
209203
args: cbindgen
210204

205+
- name: Install cargo-expand
206+
uses: actions-rs/cargo@v1
207+
with:
208+
command: install
209+
args: cargo-expand
210+
211211
- name: Build FFI
212212
uses: actions-rs/cargo@v1
213213
env:

capi/cbindgen.toml

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ header = """/*
77
*/"""
88
include_guard = "_HYPER_H"
99
no_includes = true
10-
sys_includes = ["stdint.h", "stddef.h"]
10+
sys_includes = ["stdint.h", "stddef.h", "stdbool.h"]
1111
cpp_compat = true
1212
documentation_style = "c"
13-
14-
[parse.expand]
15-
crates = ["hyper-capi"]

capi/gen_header.sh

+16-73
Original file line numberDiff line numberDiff line change
@@ -6,101 +6,44 @@
66
set -e
77

88
CAPI_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
9-
10-
WORK_DIR=$(mktemp -d)
11-
12-
# check if tmp dir was created
13-
if [[ ! "$WORK_DIR" || ! -d "$WORK_DIR" ]]; then
14-
echo "Could not create temp dir"
15-
exit 1
16-
fi
17-
189
header_file_backup="$CAPI_DIR/include/hyper.h.backup"
1910

2011
function cleanup {
21-
rm -rf "$WORK_DIR"
12+
rm -rf "$WORK_DIR" || true
2213
rm "$header_file_backup" || true
2314
}
2415

2516
trap cleanup EXIT
2617

27-
mkdir "$WORK_DIR/src"
28-
29-
# Fake a library
30-
cat > "$WORK_DIR/src/lib.rs" << EOF
31-
#[path = "$CAPI_DIR/../src/ffi/mod.rs"]
32-
pub mod ffi;
33-
EOF
34-
35-
# And its Cargo.toml
36-
cat > "$WORK_DIR/Cargo.toml" << EOF
37-
[package]
38-
name = "hyper"
39-
version = "0.0.0"
40-
edition = "2018"
41-
publish = false
42-
43-
[dependencies]
44-
# Determined which dependencies we need by running the "cargo rustc" command
45-
# below and watching the compile error output for references to unknown imports,
46-
# until we didn't get any errors.
47-
bytes = "1"
48-
futures-channel = "0.3"
49-
futures-util = { version = "0.3", default-features = false, features = ["alloc"] }
50-
libc = { version = "0.2", optional = true }
51-
http = "0.2"
52-
http-body = "0.4"
53-
tokio = { version = "1", features = ["rt"] }
54-
55-
[features]
56-
default = [
57-
"client",
58-
"ffi",
59-
"http1",
60-
]
18+
WORK_DIR=$(mktemp -d)
6119

62-
http1 = []
63-
client = []
64-
ffi = ["libc", "tokio/rt"]
65-
EOF
20+
# check if tmp dir was created
21+
if [[ ! "$WORK_DIR" || ! -d "$WORK_DIR" ]]; then
22+
echo "Could not create temp dir"
23+
exit 1
24+
fi
6625

6726
cp "$CAPI_DIR/include/hyper.h" "$header_file_backup"
6827

69-
#cargo metadata --no-default-features --features ffi --format-version 1 > "$WORK_DIR/metadata.json"
70-
71-
cd "${WORK_DIR}" || exit 2
72-
7328
# Expand just the ffi module
74-
if ! output=$(RUSTFLAGS='--cfg hyper_unstable_ffi' cargo rustc -- -Z unpretty=expanded 2>&1 > expanded.rs); then
75-
# As of April 2021 the script above prints a lot of warnings/errors, and
76-
# exits with a nonzero return code, but hyper.h still gets generated.
77-
#
78-
# However, on Github Actions, this will result in automatic "annotations"
79-
# being added to files not related to a PR, so if this is `--verify` mode,
80-
# then don't show it.
81-
#
82-
# But yes show it when using it locally.
83-
if [[ "--verify" != "$1" ]]; then
84-
echo "$output"
85-
fi
29+
if ! RUSTFLAGS='--cfg hyper_unstable_ffi' cargo expand --features client,http1,http2,ffi ::ffi 2> $WORK_DIR/expand_stderr.err > $WORK_DIR/expanded.rs; then
30+
cat $WORK_DIR/expand_stderr.err
8631
fi
8732

88-
# Replace the previous copy with the single expanded file
89-
rm -rf ./src
90-
mkdir src
91-
mv expanded.rs src/lib.rs
92-
93-
9433
# Bindgen!
9534
if ! cbindgen \
9635
--config "$CAPI_DIR/cbindgen.toml" \
9736
--lockfile "$CAPI_DIR/../Cargo.lock" \
9837
--output "$CAPI_DIR/include/hyper.h" \
99-
"${@}"; then
38+
"${@}"\
39+
$WORK_DIR/expanded.rs 2> $WORK_DIR/cbindgen_stderr.err; then
10040
bindgen_exit_code=$?
10141
if [[ "--verify" == "$1" ]]; then
102-
echo "diff generated (<) vs backup (>)"
103-
diff "$CAPI_DIR/include/hyper.h" "$header_file_backup"
42+
echo "Changes from previous header (old < > new)"
43+
diff -u "$header_file_backup" "$CAPI_DIR/include/hyper.h"
44+
else
45+
echo "cbindgen failed:"
46+
cat $WORK_DIR/cbindgen_stderr.err
10447
fi
10548
exit $bindgen_exit_code
10649
fi

capi/include/hyper.h

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <stdint.h>
1010
#include <stddef.h>
11+
#include <stdbool.h>
1112

1213
/*
1314
Return in iter functions to continue iterating.

0 commit comments

Comments
 (0)