1# Copyright 2022 The ChromiumOS Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5# Helper functions for bindgen scripts sourced by tools/bindgen-all-the-things. 6 7export BINDGEN_LINUX="${PWD}/../../third_party/kernel/v6.6" 8 9export BINDGEN_PLATFORM2="${PWD}/../../platform2" 10 11export BINDGEN_OPTS=( 12 '--disable-header-comment' 13 '--no-layout-tests' 14 '--no-doc-comments' 15 '--with-derive-default' 16) 17 18export BINDGEN_HEADER="/* automatically generated by tools/bindgen-all-the-things */ 19 20#![allow(clippy::missing_safety_doc)] 21#![allow(clippy::undocumented_unsafe_blocks)] 22#![allow(clippy::upper_case_acronyms)] 23#![allow(non_upper_case_globals)] 24#![allow(non_camel_case_types)] 25#![allow(non_snake_case)] 26#![allow(dead_code)] 27" 28 29# Delete definitions of types like __u32 and replace their uses with the equivalent native Rust 30# type, like u32. This ensures that the types are correctly sized on all platforms, unlike the 31# definitions from the system headers, which resolve to C types like short/int/long that may vary 32# across architectures. 33replace_linux_int_types() { 34 sed -E -e '/^pub type __(u|s)(8|16|32|64) =/d' -e 's/__u(8|16|32|64)/u\1/g' -e 's/__s(8|16|32|64)/i\1/g' 35 cat 36} 37 38# Delete definitions of types like __le32 and __be32 and replace their uses with the equivalent 39# data_model types. 40replace_linux_endian_types() { 41 sed -E -e '/^pub type __(l|b)e(16|32|64) =/d' -e 's/__le(16|32|64)/Le\1/g' -e 's/__be(16|32|64)/Be\1/g' 42} 43 44# Wrapper for bindgen used by the various bindgen.sh scripts. 45# Pass extra bindgen options and the .h filename as parameters. 46# Output is produced on stdout and should be redirected to a file. 47bindgen_generate() { 48 echo "${BINDGEN_HEADER}" 49 bindgen "${BINDGEN_OPTS[@]}" "$@" 50} 51 52bindgen_cleanup() { 53 rm -rf "${BINDGEN_LINUX_X86_HEADERS}" "${BINDGEN_LINUX_ARM64_HEADERS}" "${BINDGEN_LINUX_RISCV_HEADERS}" 54} 55 56# Install Linux kernel headers for each architecture into temporary locations. These are used for KVM bindings. 57 58if [[ -z "${BINDGEN_LINUX_X86_HEADERS+x}" || ! -d "${BINDGEN_LINUX_X86_HEADERS}" || 59 -z "${BINDGEN_LINUX_ARM64_HEADERS+x}" || ! -d "${BINDGEN_LINUX_ARM64_HEADERS}" || 60 -z "${BINDGEN_LINUX_RISCV_HEADERS+x}" || ! -d "${BINDGEN_LINUX_RISCV_HEADERS}" ]]; then 61 export BINDGEN_LINUX_X86_HEADERS='/tmp/bindgen_linux_x86_headers' 62 export BINDGEN_LINUX_ARM64_HEADERS='/tmp/bindgen_linux_arm64_headers' 63 export BINDGEN_LINUX_RISCV_HEADERS='/tmp/bindgen_linux_riscv_headers' 64 65 trap bindgen_cleanup EXIT 66 67 echo -n "Installing Linux headers for x86, arm64, and riscv..." 68 ( 69 cd "${BINDGEN_LINUX}" 70 nproc=$(nproc) 71 make -s headers_install ARCH=x86 INSTALL_HDR_PATH="${BINDGEN_LINUX_X86_HEADERS}" -j "${nproc}" 72 make -s headers_install ARCH=arm64 INSTALL_HDR_PATH="${BINDGEN_LINUX_ARM64_HEADERS}" -j "${nproc}" 73 make -s headers_install ARCH=riscv INSTALL_HDR_PATH="${BINDGEN_LINUX_RISCV_HEADERS}" -j "${nproc}" 74 make -s mrproper 75 ) 76 echo " done." 77fi 78