Files
hyper/capi/gen_header.sh
Kevin Burke a5464f761a 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.
2021-04-06 14:46:14 -07:00

80 lines
1.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# This script regenerates hyper.h. As of April 2021, it only works with the
# nightly build of Rust.
set -e
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
echo "Could not create temp dir"
exit 1
fi
header_file_backup="$CAPI_DIR/include/hyper.h.backup"
function cleanup {
rm -rf "$WORK_DIR"
rm "$header_file_backup" || true
}
trap cleanup EXIT
mkdir "$WORK_DIR/src"
# Fake a library
cat > "$WORK_DIR/src/lib.rs" << EOF
#[path = "$CAPI_DIR/../src/ffi/mod.rs"]
pub mod ffi;
EOF
# And its Cargo.toml
cat > "$WORK_DIR/Cargo.toml" << EOF
[package]
name = "hyper"
version = "0.0.0"
edition = "2018"
publish = false
[dependencies]
EOF
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}" || exit 2
# Expand just the ffi module
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
mkdir src
mv expanded.rs src/lib.rs
# Bindgen!
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 0