blob: b034b2b79d7d38ef3e046f2acabd3cd834b50843 [file] [log] [blame] [edit]
#!/usr/bin/env bash
# Copyright 2022 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.
# 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 2022 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)
use fuchsia_zircon::sys::{zx_handle_t, zx_status_t};
use usize as size_t;
"
readonly OUTPUT="src/syslog.rs"
tmp="$(mktemp --suffix=.h)"
for f in "${FUCHSIA_DIR}"/zircon/system/ulib/syslog/include/lib/syslog/{logger,global}.h; do
# TODO(https://github.com/rust-lang/rust-bindgen/issues/316): Remove this sed
# invocation when bindgen supports macros containing type casts.
#
# Note also that the outer parentheses are removed because bindgen doesn't
# seem to support hex literals wrapped in parentheses.
cat "${f}" | sed -E 's/(#define \w+) \(\(\w+_t\)(\w+)\)/\1 \2/g' >> "${tmp}"
done
"${BINDGEN}" \
"${tmp}" \
--disable-header-comment \
--no-layout-tests \
--raw-line "${RAW_LINES}" \
--output "${OUTPUT}" \
--allowlist-function 'fx_log_(reconfigure|get_logger)' \
--allowlist-function 'fx_logger_((get|set)_min_severity|log|create|destroy|get_connection_status)' \
--allowlist-type 'fx_log(ger)?_.+' \
--allowlist-var 'FX_LOG_.+' \
--blocklist-type 'zx_.+' \
--blocklist-type '(__u?int\d+|size)_t' \
--blocklist-item 'FX_LOG_(NONE|LEGACY_API_VERSION)' \
--blocklist-item 'FX_LOG_MAX_TAG.+' \
--blocklist-item 'FX_LOG_SEVERITY_(STEP_SIZE|MAX)' \
-- \
-I "${FUCHSIA_DIR}"/zircon/system/ulib/syslog/include \
-I "${FUCHSIA_DIR}"/zircon/system/public
sed -i -E 's/(FX_LOG_(SEVERITY_(DEFAULT|MAX)|[A-Z]+)): u32 = /\1: fx_log_severity_t = /g' "${OUTPUT}"
sed -i -E 's/(FX_LOG_VERBOSITY_STEP_SIZE): u32 = /\1: u8 = /g' "${OUTPUT}"
fx format-code --files=${OUTPUT}