Skip to content

Commit a5464f7

Browse files
authored
chore(ffi): run gen_header.sh in CI environment (#2488)
Clean up the script so that any unexpected error terminates the script, and stop suppressing errors that may contain useful information (for example, that you are using the stable version but need to use the nightly). This is useful because if hyper.h is not up to date going forward the CI should flag it. As is, there are a bunch of changes to hyper.h that have not been checked in (or were generated by a newer version of the cbindgen script.) Fixes #2483.
1 parent c7ab1aa commit a5464f7

File tree

4 files changed

+241
-98
lines changed

4 files changed

+241
-98
lines changed

.github/workflows/CI.yml

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
- test
1818
- features
1919
- ffi
20+
- ffi-header
2021
- doc
2122
steps:
2223
- run: exit 0
@@ -119,9 +120,7 @@ jobs:
119120
ffi:
120121
name: Test C API (FFI)
121122
needs: [style]
122-
123123
runs-on: ubuntu-latest
124-
125124
steps:
126125
- name: Checkout
127126
uses: actions/checkout@v1
@@ -147,10 +146,6 @@ jobs:
147146
command: build
148147
args: --features client,http1,http2,ffi
149148

150-
# TODO: re-enable check once figuring out how to get it working in CI
151-
# - name: Verify cbindgen
152-
# run: ./capi/gen_header.sh --verify
153-
154149
- name: Make Examples
155150
run: cd capi/examples && make client
156151

@@ -162,6 +157,39 @@ jobs:
162157
command: test
163158
args: --features full,ffi --lib
164159

160+
ffi-header:
161+
name: Verify hyper.h is up to date
162+
runs-on: ubuntu-latest
163+
steps:
164+
- name: Checkout
165+
uses: actions/checkout@v1
166+
167+
- name: Install Rust
168+
uses: actions-rs/toolchain@v1
169+
with:
170+
profile: minimal
171+
toolchain: nightly
172+
default: true
173+
override: true
174+
components: cargo
175+
176+
- name: Install cbindgen
177+
uses: actions-rs/cargo@v1
178+
with:
179+
command: install
180+
args: cbindgen
181+
182+
- name: Build FFI
183+
uses: actions-rs/cargo@v1
184+
env:
185+
RUSTFLAGS: --cfg hyper_unstable_ffi
186+
with:
187+
command: build
188+
args: --features client,http1,http2,ffi
189+
190+
- name: Ensure that hyper.h is up to date
191+
run: ./capi/gen_header.sh --verify
192+
165193
doc:
166194
name: Build docs
167195
needs: [style, test]

capi/cbindgen.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
# See https://github.com/eqrion/cbindgen/blob/master/docs.md#cbindgentoml for
2+
# a list of possible configuration values.
13
language = "C"
4+
header = """/*
5+
* Copyright 2021 Sean McArthur. MIT License.
6+
* Generated by gen_header.sh. Do not edit directly.
7+
*/"""
28
include_guard = "_HYPER_H"
39
no_includes = true
410
sys_includes = ["stdint.h", "stddef.h"]

capi/gen_header.sh

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#!/usr/bin/env bash
22

3-
CAPI_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
3+
# This script regenerates hyper.h. As of April 2021, it only works with the
4+
# nightly build of Rust.
5+
6+
set -e
47

5-
WORK_DIR=`mktemp -d`
8+
CAPI_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
69

10+
WORK_DIR=$(mktemp -d)
711

812
# check if tmp dir was created
913
if [[ ! "$WORK_DIR" || ! -d "$WORK_DIR" ]]; then
@@ -14,9 +18,8 @@ fi
1418
header_file_backup="$CAPI_DIR/include/hyper.h.backup"
1519

1620
function cleanup {
17-
#echo "$WORK_DIR"
1821
rm -rf "$WORK_DIR"
19-
rm "$header_file_backup"
22+
rm "$header_file_backup" || true
2023
}
2124

2225
trap cleanup EXIT
@@ -44,10 +47,14 @@ cp "$CAPI_DIR/include/hyper.h" "$header_file_backup"
4447

4548
#cargo metadata --no-default-features --features ffi --format-version 1 > "$WORK_DIR/metadata.json"
4649

47-
cd $WORK_DIR
50+
cd "${WORK_DIR}" || exit 2
4851

4952
# Expand just the ffi module
50-
cargo rustc -- -Z unstable-options --pretty=expanded > expanded.rs 2>/dev/null
53+
if ! output=$(cargo rustc -- -Z unstable-options --pretty=expanded 2>&1 > expanded.rs); then
54+
# As of April 2021 the script above prints a lot of warnings/errors, and
55+
# exits with a nonzero return code, but hyper.h still gets generated.
56+
echo "$output"
57+
fi
5158

5259
# Replace the previous copy with the single expanded file
5360
rm -rf ./src
@@ -56,17 +63,17 @@ mv expanded.rs src/lib.rs
5663

5764

5865
# Bindgen!
59-
cbindgen\
60-
-c "$CAPI_DIR/cbindgen.toml"\
61-
--lockfile "$CAPI_DIR/../Cargo.lock"\
62-
-o "$CAPI_DIR/include/hyper.h"\
63-
$1
64-
65-
bindgen_exit_code=$?
66-
67-
if [[ "--verify" == "$1" && "$bindgen_exit_code" != 0 ]]; then
68-
echo "diff generated (<) vs backup (>)"
69-
diff "$CAPI_DIR/include/hyper.h" "$header_file_backup"
66+
if ! cbindgen \
67+
--config "$CAPI_DIR/cbindgen.toml" \
68+
--lockfile "$CAPI_DIR/../Cargo.lock" \
69+
--output "$CAPI_DIR/include/hyper.h" \
70+
"${@}"; then
71+
bindgen_exit_code=$?
72+
if [[ "--verify" == "$1" ]]; then
73+
echo "diff generated (<) vs backup (>)"
74+
diff "$CAPI_DIR/include/hyper.h" "$header_file_backup"
75+
fi
76+
exit $bindgen_exit_code
7077
fi
7178

72-
exit $bindgen_exit_code
79+
exit 0

0 commit comments

Comments
 (0)