Skip to content

Commit

Permalink
chore(ffi): run gen_header.sh in CI environment (#2488)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kevinburke authored Apr 6, 2021
1 parent c7ab1aa commit a5464f7
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 98 deletions.
40 changes: 34 additions & 6 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
- test
- features
- ffi
- ffi-header
- doc
steps:
- run: exit 0
Expand Down Expand Up @@ -119,9 +120,7 @@ jobs:
ffi:
name: Test C API (FFI)
needs: [style]

runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v1
Expand All @@ -147,10 +146,6 @@ jobs:
command: build
args: --features client,http1,http2,ffi

# TODO: re-enable check once figuring out how to get it working in CI
# - name: Verify cbindgen
# run: ./capi/gen_header.sh --verify

- name: Make Examples
run: cd capi/examples && make client

Expand All @@ -162,6 +157,39 @@ jobs:
command: test
args: --features full,ffi --lib

ffi-header:
name: Verify hyper.h is up to date
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
default: true
override: true
components: cargo

- name: Install cbindgen
uses: actions-rs/cargo@v1
with:
command: install
args: cbindgen

- name: Build FFI
uses: actions-rs/cargo@v1
env:
RUSTFLAGS: --cfg hyper_unstable_ffi
with:
command: build
args: --features client,http1,http2,ffi

- name: Ensure that hyper.h is up to date
run: ./capi/gen_header.sh --verify

doc:
name: Build docs
needs: [style, test]
Expand Down
6 changes: 6 additions & 0 deletions capi/cbindgen.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# See https://github.com/eqrion/cbindgen/blob/master/docs.md#cbindgentoml for
# a list of possible configuration values.
language = "C"
header = """/*
* Copyright 2021 Sean McArthur. MIT License.
* Generated by gen_header.sh. Do not edit directly.
*/"""
include_guard = "_HYPER_H"
no_includes = true
sys_includes = ["stdint.h", "stddef.h"]
Expand Down
43 changes: 25 additions & 18 deletions capi/gen_header.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#!/usr/bin/env bash

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

set -e

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

WORK_DIR=$(mktemp -d)

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

function cleanup {
#echo "$WORK_DIR"
rm -rf "$WORK_DIR"
rm "$header_file_backup"
rm "$header_file_backup" || true
}

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

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

cd $WORK_DIR
cd "${WORK_DIR}" || exit 2

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

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


# Bindgen!
cbindgen\
-c "$CAPI_DIR/cbindgen.toml"\
--lockfile "$CAPI_DIR/../Cargo.lock"\
-o "$CAPI_DIR/include/hyper.h"\
$1

bindgen_exit_code=$?

if [[ "--verify" == "$1" && "$bindgen_exit_code" != 0 ]]; then
echo "diff generated (<) vs backup (>)"
diff "$CAPI_DIR/include/hyper.h" "$header_file_backup"
if ! cbindgen \
--config "$CAPI_DIR/cbindgen.toml" \
--lockfile "$CAPI_DIR/../Cargo.lock" \
--output "$CAPI_DIR/include/hyper.h" \
"${@}"; then
bindgen_exit_code=$?
if [[ "--verify" == "$1" ]]; then
echo "diff generated (<) vs backup (>)"
diff "$CAPI_DIR/include/hyper.h" "$header_file_backup"
fi
exit $bindgen_exit_code
fi

exit $bindgen_exit_code
exit 0
Loading

0 comments on commit a5464f7

Please sign in to comment.