From 1b2a83b6f7987407daf19a617f0dda7f02ba377a Mon Sep 17 00:00:00 2001 From: Andy Caldwell Date: Wed, 21 Dec 2022 23:08:59 +0000 Subject: [PATCH 1/2] Revamp gen_header using cargo-expand --- .github/workflows/CI.yml | 14 +++---- capi/cbindgen.toml | 7 +--- capi/gen_header.sh | 89 ++++++++-------------------------------- capi/include/hyper.h | 6 +-- 4 files changed, 27 insertions(+), 89 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 7683bf8438..71e9b97ef5 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -185,12 +185,6 @@ jobs: toolchain: stable override: true - - name: Install cbindgen - uses: actions-rs/cargo@v1 - with: - command: install - args: cbindgen - - name: Build FFI uses: actions-rs/cargo@v1 env: @@ -208,7 +202,7 @@ jobs: RUSTFLAGS: --cfg hyper_unstable_ffi with: command: test - args: --features full,ffi --lib + args: --features server,client,http1,http2,ffi --lib ffi-header: name: Verify hyper.h is up to date @@ -232,6 +226,12 @@ jobs: command: install args: cbindgen + - name: Install cargo-expand + uses: actions-rs/cargo@v1 + with: + command: install + args: cargo-expand + - name: Build FFI uses: actions-rs/cargo@v1 env: diff --git a/capi/cbindgen.toml b/capi/cbindgen.toml index d1a58234b5..112747a412 100644 --- a/capi/cbindgen.toml +++ b/capi/cbindgen.toml @@ -5,11 +5,8 @@ header = """/* * Copyright 2021 Sean McArthur. MIT License. * Generated by gen_header.sh. Do not edit directly. */""" -include_guard = "_HYPER_H" +pragma_once = true no_includes = true -sys_includes = ["stdint.h", "stddef.h"] +sys_includes = ["stdint.h", "stddef.h", "stdbool.h"] cpp_compat = true documentation_style = "c" - -[parse.expand] -crates = ["hyper-capi"] diff --git a/capi/gen_header.sh b/capi/gen_header.sh index d0b9c13a32..7a08d3e6ff 100755 --- a/capi/gen_header.sh +++ b/capi/gen_header.sh @@ -6,101 +6,44 @@ 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 -rf "$WORK_DIR" || true 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] -# Determined which dependencies we need by running the "cargo rustc" command -# below and watching the compile error output for references to unknown imports, -# until we didn't get any errors. -bytes = "1" -futures-channel = "0.3" -futures-util = { version = "0.3", default-features = false, features = ["alloc"] } -libc = { version = "0.2", optional = true } -http = "0.2" -http-body = "0.4" -tokio = { version = "1", features = ["rt"] } - -[features] -default = [ - "client", - "ffi", - "http1", -] +WORK_DIR=$(mktemp -d) -http1 = [] -client = [] -ffi = ["libc", "tokio/rt"] -EOF +# check if tmp dir was created +if [[ ! "$WORK_DIR" || ! -d "$WORK_DIR" ]]; then + echo "Could not create temp dir" + exit 1 +fi 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=$(RUSTFLAGS='--cfg hyper_unstable_ffi' cargo rustc -- -Z unpretty=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. - # - # However, on Github Actions, this will result in automatic "annotations" - # being added to files not related to a PR, so if this is `--verify` mode, - # then don't show it. - # - # But yes show it when using it locally. - if [[ "--verify" != "$1" ]]; then - echo "$output" - fi +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 + cat $WORK_DIR/expand_stderr.err 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 + "${@}"\ + $WORK_DIR/expanded.rs 2> $WORK_DIR/cbindgen_stderr.err; then bindgen_exit_code=$? if [[ "--verify" == "$1" ]]; then - echo "diff generated (<) vs backup (>)" - diff "$CAPI_DIR/include/hyper.h" "$header_file_backup" + echo "Changes from previous header (old < > new)" + diff -u "$header_file_backup" "$CAPI_DIR/include/hyper.h" + else + echo "cbindgen failed:" + cat $WORK_DIR/cbindgen_stderr.err fi exit $bindgen_exit_code fi diff --git a/capi/include/hyper.h b/capi/include/hyper.h index d41ccaaccd..a1f6d3d77e 100644 --- a/capi/include/hyper.h +++ b/capi/include/hyper.h @@ -3,11 +3,11 @@ * Generated by gen_header.sh. Do not edit directly. */ -#ifndef _HYPER_H -#define _HYPER_H +#pragma once #include #include +#include /* Return in iter functions to continue iterating. @@ -759,5 +759,3 @@ void hyper_waker_wake(struct hyper_waker *waker); #ifdef __cplusplus } // extern "C" #endif // __cplusplus - -#endif /* _HYPER_H */ From b9c98610c53571b6b476d617e2efb5ce0cf1d400 Mon Sep 17 00:00:00 2001 From: Andy Caldwell Date: Wed, 28 Dec 2022 17:15:14 +0000 Subject: [PATCH 2/2] Revert to using an include guard --- capi/cbindgen.toml | 2 +- capi/include/hyper.h | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/capi/cbindgen.toml b/capi/cbindgen.toml index 112747a412..615df080f6 100644 --- a/capi/cbindgen.toml +++ b/capi/cbindgen.toml @@ -5,7 +5,7 @@ header = """/* * Copyright 2021 Sean McArthur. MIT License. * Generated by gen_header.sh. Do not edit directly. */""" -pragma_once = true +include_guard = "_HYPER_H" no_includes = true sys_includes = ["stdint.h", "stddef.h", "stdbool.h"] cpp_compat = true diff --git a/capi/include/hyper.h b/capi/include/hyper.h index a1f6d3d77e..591cb9771c 100644 --- a/capi/include/hyper.h +++ b/capi/include/hyper.h @@ -3,7 +3,8 @@ * Generated by gen_header.sh. Do not edit directly. */ -#pragma once +#ifndef _HYPER_H +#define _HYPER_H #include #include @@ -759,3 +760,5 @@ void hyper_waker_wake(struct hyper_waker *waker); #ifdef __cplusplus } // extern "C" #endif // __cplusplus + +#endif /* _HYPER_H */