blob: a1a85f31103dbc170971b3e3fb6529a1b6cac086 [file] [log] [blame]
#!/bin/bash
# Copyright 2024 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# This script generates Rust bindings for fdf.
# Run `./bindgen.sh check` to validate that the bindings haven't changed.
# Determine paths for this script and its directory, and set $FUCHSIA_DIR.
readonly FULL_PATH="${BASH_SOURCE[0]}"
readonly SCRIPT_DIR="$(cd "$(dirname "${FULL_PATH}")" >/dev/null 2>&1 && pwd)"
source "${SCRIPT_DIR}/../../../../../tools/devshell/lib/vars.sh"
set -eu
cd "${SCRIPT_DIR}"
readonly RELPATH="${SCRIPT_DIR#${FUCHSIA_DIR}/}/bindgen.sh"
readonly BINDGEN="${PREBUILT_RUST_BINDGEN_DIR}/bindgen"
# Generate annotations for the top of the generated source file.
readonly RAW_LINES="// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Generated by ${RELPATH} using $("${BINDGEN}" --version)
#![allow(dead_code)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
pub use fuchsia_zircon_types::*;
"
readonly RUST_FILE="src/fdf_sys.rs"
readonly ASYNC_INCLUDES_PATH="${FUCHSIA_DIR}"/zircon/system/ulib/async/include
readonly DRIVER_RUNTIME_INCLUDES_PATH="${FUCHSIA_DIR}"/sdk/lib/driver/runtime/include
# Output to a tempfile in check mode, or the actual file when generating.
if [[ -v 1 && "$1" = "check" ]] ; then
readonly OUTPUT="$(mktemp -p "${PWD}" --suffix=.rs)"
trap "rm -f ${OUTPUT}" EXIT
else
readonly OUTPUT="${RUST_FILE}"
fi
readonly tmp="$(mktemp --suffix=.h)"
function filter_headers() {
for f in "$@" ; do
# TODO(https://github.com/rust-lang/rust-bindgen/issues/316): Remove this sed
# invocation when bindgen supports macros containing type casts.
cat "${f}" >> "${tmp}" #| sed -E 's/(#define \w+) \(\(\w+_t\)(\w+)\)/\1 \(\2\)/g' >> "${tmp}"
done
}
filter_headers \
"${ASYNC_INCLUDES_PATH}"/lib/async/{dispatcher,paged_vmo,receiver,sequence_id,task,time,trap,wait}.h \
"${DRIVER_RUNTIME_INCLUDES_PATH}"/lib/fdf/{arena,channel,channel_read,dispatcher,env,handle,testing,token,types}.h \
"${FUCHSIA_DIR}"/sdk/lib/driver/symbols/symbols.h
"${BINDGEN}" \
"${tmp}" \
--disable-header-comment \
--raw-line "${RAW_LINES}" \
--with-derive-default \
--impl-debug \
--use-core \
--output "${OUTPUT}" \
--allowlist-item 'fdf_.+|async_.+|Driver.+|FDF_.+|DRIVER_.+' \
--blocklist-type 'zx_.+' \
-- \
-I "${FUCHSIA_DIR}"/zircon/system/public \
-I "${DRIVER_RUNTIME_INCLUDES_PATH}" \
-I "${ASYNC_INCLUDES_PATH}"
# Use rustfmt directly instead of `fx format-code` because that requires the
# file to be within FUCHSIA_DIR, and in check mode we'll be running it on a
# temp file in TEMPDIR.
"${PREBUILT_RUST_DIR}"/bin/rustfmt \
"${OUTPUT}" \
--config-path "${FUCHSIA_DIR}"/rustfmt.toml \
--unstable-features \
--skip-children
if [[ -v 1 && "$1" = "check" ]] && ! diff "${OUTPUT}" "${RUST_FILE}" ; then
echo "The Driver Runtime C++ headers have changed without updating the rust bindings."
echo "Please run './bindgen.sh' in ${PWD}."
exit 1
fi