blob: 8eaef7a4765b9ea9b64c29748a387f086d84facc [file] [log] [blame] [edit]
#!/bin/bash
# Copyright 2023 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 the zbi C library.
# 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="${FULL_PATH#${FUCHSIA_DIR}/}"
readonly BINDGEN="${PREBUILT_RUST_BINDGEN_DIR}/bindgen"
# Generate annotations for the top of the generated source file.
readonly RAW_LINES="// Copyright 2023 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 non-conventional naming for imports from C/C++.
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(clippy::undocumented_unsafe_blocks)]
use zerocopy::{AsBytes, FromBytes, FromZeroes};
// Configure linkage for MacOS.
#[cfg(target_os = \"macos\")]
#[link(name = \"IOKit\", kind = \"framework\")]
#[link(name = \"CoreFoundation\", kind = \"framework\")]
extern \"C\" {}"
readonly OUTPUT="src/zbi_format.rs"
readonly INPUT=( \
"${FUCHSIA_DIR}/sdk/lib/zbi-format/include/lib/zbi-format/kernel.h" \
"${FUCHSIA_DIR}/sdk/lib/zbi-format/include/lib/zbi-format/zbi.h" \
)
tmp="$(mktemp --suffix=.h)"
trap "rm ${tmp}" EXIT
for f in ${INPUT[@]}; do
# TODO(https://github.com/rust-lang/rust-bindgen/issues/316): Remove this sed
# invocation when bindgen supports macros containing type casts.
# sed#1: join lines split with '\'
# sed#2: replace matching defines with static const of specified type.
cat "${f}" \
| sed -e ':x /\\$/ { N; s/\\\n//g ; bx }' \
| sed -E 's/#define (\w+) +\(\((\w+)\)\((.+)\)\).*/static const \2 \1 = (\3);/g' \
>> "${tmp}"
done
echo ${tmp}
"${BINDGEN}" \
${tmp} \
--disable-header-comment \
--raw-line "${RAW_LINES}" \
--with-derive-default \
--with-derive-partialeq \
--impl-debug \
--with-derive-custom-struct zbi_header_t=FromBytes,AsBytes,FromZeroes \
--output ${OUTPUT} \
--allowlist-type '(zbi_type_t|zbi_flags_t|zbi_header_t|zbi_kernel_t)' \
--allowlist-var 'ZBI_.+' \
-- -x c \
-I ${FUCHSIA_DIR}/sdk/lib/zbi-format/include \