1# Copyright 2022 The Chromium OS Authors. All rights reserved. 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 7# virtio-video bindings need some types that are only in the 5.10-arcvm branch. 8# This should be switched to a vanilla kernel when these are upstream. 9export BINDGEN_LINUX="${PWD}/../../third_party/kernel/v5.10-arcvm" 10 11export BINDGEN_PLATFORM2="${PWD}/../../platform2" 12 13export BINDGEN_OPTS=( 14 '--disable-header-comment' 15 '--no-layout-tests' 16 '--no-doc-comments' 17 '--with-derive-default' 18 '--size_t-is-usize' 19) 20 21export BINDGEN_HEADER="/* automatically generated by tools/bindgen-all-the-things */ 22 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}" 54} 55 56# Install Linux kernel headers for x86 and arm 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}" ]]; then 60 export BINDGEN_LINUX_X86_HEADERS='/tmp/bindgen_linux_x86_headers' 61 export BINDGEN_LINUX_ARM64_HEADERS='/tmp/bindgen_linux_arm64_headers' 62 63 trap bindgen_cleanup EXIT 64 65 echo -n "Installing Linux headers for x86 and arm64..." 66 ( 67 cd "${BINDGEN_LINUX}" 68 nproc=$(nproc) 69 make -s headers_install ARCH=x86 INSTALL_HDR_PATH="${BINDGEN_LINUX_X86_HEADERS}" -j "${nproc}" 70 make -s headers_install ARCH=arm64 INSTALL_HDR_PATH="${BINDGEN_LINUX_ARM64_HEADERS}" -j "${nproc}" 71 make -s mrproper 72 ) 73 echo " done." 74fi 75