| #!/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)] |
| // Allow unused definitions |
| #![allow(dead_code, unused_results)] |
| |
| use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout}; |
| |
| // 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/*.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_.*_t'=FromBytes,IntoBytes,Immutable,KnownLayout \ |
| --output ${OUTPUT} \ |
| --allowlist-type 'zbi_.+_t' \ |
| --allowlist-var 'ZBI_.+' \ |
| --blocklist-type 'zbi_topology_.+_t_.*' \ |
| --blocklist-type 'zbi_topology_(entity|architecture_info|processor|node)_t' \ |
| --use-core \ |
| -- -x c \ |
| -I ${FUCHSIA_DIR}/sdk/lib/zbi-format/include \ |
| |