bindgen-common.sh 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. # Helper functions for bindgen scripts sourced by tools/bindgen-all-the-things.
  5. export BINDGEN_LINUX="${PWD}/../../third_party/kernel/v6.6"
  6. export BINDGEN_PLATFORM2="${PWD}/../../platform2"
  7. export BINDGEN_OPTS=(
  8. '--disable-header-comment'
  9. '--no-layout-tests'
  10. '--no-doc-comments'
  11. '--with-derive-default'
  12. )
  13. export BINDGEN_HEADER="/* automatically generated by tools/bindgen-all-the-things */
  14. #![allow(clippy::missing_safety_doc)]
  15. #![allow(clippy::undocumented_unsafe_blocks)]
  16. #![allow(clippy::upper_case_acronyms)]
  17. #![allow(non_upper_case_globals)]
  18. #![allow(non_camel_case_types)]
  19. #![allow(non_snake_case)]
  20. #![allow(dead_code)]
  21. "
  22. # Delete definitions of types like __u32 and replace their uses with the equivalent native Rust
  23. # type, like u32. This ensures that the types are correctly sized on all platforms, unlike the
  24. # definitions from the system headers, which resolve to C types like short/int/long that may vary
  25. # across architectures.
  26. replace_linux_int_types() {
  27. 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'
  28. cat
  29. }
  30. # Delete definitions of types like __le32 and __be32 and replace their uses with the equivalent
  31. # data_model types.
  32. replace_linux_endian_types() {
  33. 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'
  34. }
  35. # Wrapper for bindgen used by the various bindgen.sh scripts.
  36. # Pass extra bindgen options and the .h filename as parameters.
  37. # Output is produced on stdout and should be redirected to a file.
  38. bindgen_generate() {
  39. echo "${BINDGEN_HEADER}"
  40. bindgen "${BINDGEN_OPTS[@]}" "$@"
  41. }
  42. bindgen_cleanup() {
  43. rm -rf "${BINDGEN_LINUX_X86_HEADERS}" "${BINDGEN_LINUX_ARM64_HEADERS}" "${BINDGEN_LINUX_RISCV_HEADERS}"
  44. }
  45. # Install Linux kernel headers for each architecture into temporary locations. These are used for KVM bindings.
  46. if [[ -z "${BINDGEN_LINUX_X86_HEADERS+x}" || ! -d "${BINDGEN_LINUX_X86_HEADERS}" ||
  47. -z "${BINDGEN_LINUX_ARM64_HEADERS+x}" || ! -d "${BINDGEN_LINUX_ARM64_HEADERS}" ||
  48. -z "${BINDGEN_LINUX_RISCV_HEADERS+x}" || ! -d "${BINDGEN_LINUX_RISCV_HEADERS}" ]]; then
  49. export BINDGEN_LINUX_X86_HEADERS='/tmp/bindgen_linux_x86_headers'
  50. export BINDGEN_LINUX_ARM64_HEADERS='/tmp/bindgen_linux_arm64_headers'
  51. export BINDGEN_LINUX_RISCV_HEADERS='/tmp/bindgen_linux_riscv_headers'
  52. trap bindgen_cleanup EXIT
  53. echo -n "Installing Linux headers for x86, arm64, and riscv..."
  54. (
  55. cd "${BINDGEN_LINUX}"
  56. nproc=$(nproc)
  57. make -s headers_install ARCH=x86 INSTALL_HDR_PATH="${BINDGEN_LINUX_X86_HEADERS}" -j "${nproc}"
  58. make -s headers_install ARCH=arm64 INSTALL_HDR_PATH="${BINDGEN_LINUX_ARM64_HEADERS}" -j "${nproc}"
  59. make -s headers_install ARCH=riscv INSTALL_HDR_PATH="${BINDGEN_LINUX_RISCV_HEADERS}" -j "${nproc}"
  60. make -s mrproper
  61. )
  62. echo " done."
  63. fi