Skip to content

Commit 293ada7

Browse files
committed
Revamp gen_header using cargo-expand
1 parent 982e6a5 commit 293ada7

File tree

4 files changed

+27
-89
lines changed

4 files changed

+27
-89
lines changed

.github/workflows/CI.yml

+7-7
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,6 @@ jobs:
185185
toolchain: stable
186186
override: true
187187

188-
- name: Install cbindgen
189-
uses: actions-rs/cargo@v1
190-
with:
191-
command: install
192-
args: cbindgen
193-
194188
- name: Build FFI
195189
uses: actions-rs/cargo@v1
196190
env:
@@ -208,7 +202,7 @@ jobs:
208202
RUSTFLAGS: --cfg hyper_unstable_ffi
209203
with:
210204
command: test
211-
args: --features full,ffi --lib
205+
args: --features client,http1,http2,ffi --lib
212206

213207
ffi-header:
214208
name: Verify hyper.h is up to date
@@ -232,6 +226,12 @@ jobs:
232226
command: install
233227
args: cbindgen
234228

229+
- name: Install cargo-expand
230+
uses: actions-rs/cargo@v1
231+
with:
232+
command: install
233+
args: cargo-expand
234+
235235
- name: Build FFI
236236
uses: actions-rs/cargo@v1
237237
env:

capi/cbindgen.toml

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@ header = """/*
55
* Copyright 2021 Sean McArthur. MIT License.
66
* Generated by gen_header.sh. Do not edit directly.
77
*/"""
8-
include_guard = "_HYPER_H"
8+
pragma_once = true
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

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
* Generated by gen_header.sh. Do not edit directly.
44
*/
55

6-
#ifndef _HYPER_H
7-
#define _HYPER_H
6+
#pragma once
87

98
#include <stdint.h>
109
#include <stddef.h>
10+
#include <stdbool.h>
1111

1212
/*
1313
Return in iter functions to continue iterating.
@@ -759,5 +759,3 @@ void hyper_waker_wake(struct hyper_waker *waker);
759759
#ifdef __cplusplus
760760
} // extern "C"
761761
#endif // __cplusplus
762-
763-
#endif /* _HYPER_H */

0 commit comments

Comments
 (0)