xref: /aosp_15_r20/external/crosvm/net_sys/bindgen.sh (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1#!/usr/bin/env bash
2# Copyright 2022 The ChromiumOS Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# Regenerate net_sys bindgen bindings.
7
8set -euo pipefail
9cd "$(dirname "${BASH_SOURCE[0]}")/.."
10
11source tools/impl/bindgen-common.sh
12
13# Replace definition of struct sockaddr with an import of libc::sockaddr.
14replace_sockaddr_libc() {
15    # Match the following structure definition across multiple lines:
16    #
17    # #[repr(C)]
18    # #[derive(Debug, Default, Copy, Clone)]
19    # pub struct sockaddr {
20    #     pub sa_family: sa_family_t,
21    #     pub sa_data: [::std::os::raw::c_char; 14usize],
22    # }
23    sed -E \
24        -e '1h;2,$H;$!d;g' \
25        -e 's/#\[repr\(C\)\]\n#\[derive\([^)]+\)\]\npub struct sockaddr \{\n(    pub [^\n]+\n)+\}/\nuse libc::sockaddr;\n/'
26}
27
28# Remove custom sa_family_t type in favor of libc::sa_family_t.
29remove_sa_family_t() {
30    grep -v "pub type sa_family_t = "
31}
32
33bindgen_generate \
34    --allowlist-type='ifreq' \
35    --allowlist-type='net_device_flags' \
36    --bitfield-enum='net_device_flags' \
37    "${BINDGEN_LINUX_X86_HEADERS}/include/linux/if.h" \
38    | replace_linux_int_types \
39    | replace_sockaddr_libc \
40    | remove_sa_family_t \
41    | rustfmt \
42    > net_sys/src/iff.rs
43
44bindgen_generate \
45    --allowlist-type='sock_fprog' \
46    --allowlist-var='TUN_.*' \
47    --allowlist-var='IFF_NO_PI' \
48    --allowlist-var='IFF_MULTI_QUEUE' \
49    --allowlist-var='IFF_TAP' \
50    --allowlist-var='IFF_VNET_HDR' \
51    "${BINDGEN_LINUX}/include/uapi/linux/if_tun.h" \
52    | replace_linux_int_types \
53    > net_sys/src/if_tun.rs
54
55bindgen_generate \
56    --allowlist-var='SIOC.*' \
57    "${BINDGEN_LINUX}/include/uapi/linux/sockios.h" \
58    | replace_linux_int_types \
59    > net_sys/src/sockios.rs
60