crosvm/net_sys/bindgen.sh
Daniel Verkamp ac0b33ad9f net_sys: regenerate bindgen bindings
This replaces the bindgen-based bindings with Rust libc crate types and
constants where possible, so our custom bindings are significantly
reduced.

BUG=b:218388029
TEST=tools/presubmit --quick

Change-Id: I7f20ecf5ccd651456f9a23ce7964fdbd0e8064ef
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3339851
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2022-04-06 19:20:34 +00:00

55 lines
1.6 KiB
Bash
Executable file

#!/usr/bin/env bash
# Copyright 2022 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# Regenerate net_sys bindgen bindings.
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")/.."
source tools/impl/bindgen-common.sh
# Replace definition of struct sockaddr with an import of libc::sockaddr.
replace_sockaddr_libc() {
# Match the following structure definition across multiple lines:
#
# #[repr(C)]
# #[derive(Debug, Default, Copy, Clone)]
# pub struct sockaddr {
# pub sa_family: sa_family_t,
# pub sa_data: [::std::os::raw::c_char; 14usize],
# }
sed -E \
-e '1h;2,$H;$!d;g' \
-e 's/#\[repr\(C\)\]\n#\[derive\([^)]+\)\]\npub struct sockaddr \{\n( pub [^\n]+\n)+\}/\nuse libc::sockaddr;\n/'
}
# Remove custom sa_family_t type in favor of libc::sa_family_t.
remove_sa_family_t() {
grep -v "pub type sa_family_t = "
}
bindgen_generate \
--allowlist-type='ifreq' \
--allowlist-type='net_device_flags' \
--bitfield-enum='net_device_flags' \
"${BINDGEN_LINUX_X86_HEADERS}/include/linux/if.h" \
| replace_linux_int_types \
| replace_sockaddr_libc \
| remove_sa_family_t \
| rustfmt \
> net_sys/src/iff.rs
bindgen_generate \
--allowlist-type='sock_fprog' \
--allowlist-var='TUN_.*' \
"${BINDGEN_LINUX}/include/uapi/linux/if_tun.h" \
| replace_linux_int_types \
> net_sys/src/if_tun.rs
bindgen_generate \
--allowlist-var='SIOC.*' \
"${BINDGEN_LINUX}/include/uapi/linux/sockios.h" \
| replace_linux_int_types \
> net_sys/src/sockios.rs