Dov Shlachter | 6676eb6 | 2023-08-22 15:11:08 -0700 | [diff] [blame] | 1 | #! /usr/bin/env bash |
| 2 | # Copyright 2023, The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | set -e |
| 17 | |
| 18 | # Run this script to regenerate bootimg_priv.rs if |
| 19 | # include/bootimg/bootimg.h ever changes. |
| 20 | # The rust_bindgen rule is not cooperative, causing custom derive types |
| 21 | # to not percolate to the generated structures. |
| 22 | # It's just easier to do all the munging in a script. |
| 23 | |
| 24 | SCRATCH_DIR=$(mktemp -d) |
| 25 | |
| 26 | cleanup (){ |
| 27 | rm -rf ${SCRATCH_DIR} |
| 28 | } |
| 29 | |
| 30 | trap cleanup EXIT |
| 31 | pushd ~/aosp > /dev/null |
| 32 | |
| 33 | BOOTIMG_DIR=$(realpath system/tools/mkbootimg/) |
| 34 | # The stdint include generates a lot of unnecessary types that the |
| 35 | # generated rust bindings really don't need. |
| 36 | BLOCKED_TYPES_RE="__.+|.?int.+" |
| 37 | # The stdint include generates a lot of unnecessary constants that the |
| 38 | # generated rust bindings really don't need. |
| 39 | BLOCKED_ITEMS_RE="_.+|.?INT.+|PTR.+|ATOMIC.+|.+SOURCE|.+_H|SIG_.+|SIZE_.+|.?CHAR.+" |
| 40 | CUSTOM_STRUCT_RE="(vendor_)?(boot_img_hdr|ramdisk_table_entry)_v\d+" |
Frederick Mayle | c528e67 | 2023-10-15 18:28:13 +0000 | [diff] [blame] | 41 | CUSTOM_STRUCT_DERIVES="AsBytes,FromBytes,FromZeroes,PartialEq,Copy,Clone,Debug" |
Dov Shlachter | 6676eb6 | 2023-08-22 15:11:08 -0700 | [diff] [blame] | 42 | BINDGEN_FLAGS="--use-core --with-derive-default" |
| 43 | BOOTIMG_PRIV=${BOOTIMG_DIR}/rust/bootimg_priv.rs |
| 44 | |
| 45 | # We need C++ isms, and the only obvious way to convince bindgen |
| 46 | # that the source is C++ is with a C++ extension. |
| 47 | cp ${BOOTIMG_DIR}/include/bootimg/bootimg.h ${SCRATCH_DIR}/bootimg.hpp |
| 48 | |
| 49 | ./out/host/linux-x86/bin/bindgen \ |
| 50 | --blocklist-type="${BLOCKED_TYPES_RE}" \ |
| 51 | --blocklist-item="${BLOCKED_ITEMS_RE}" \ |
| 52 | --with-derive-custom-struct="${CUSTOM_STRUCT_RE}=${CUSTOM_STRUCT_DERIVES}" \ |
| 53 | ${BINDGEN_FLAGS} \ |
| 54 | ${SCRATCH_DIR}/bootimg.hpp \ |
| 55 | -o ${SCRATCH_DIR}/bootimg_gen.rs |
| 56 | |
| 57 | cat << EOF | cat - ${SCRATCH_DIR}/bootimg_gen.rs > ${BOOTIMG_PRIV} |
| 58 | // Copyright $(date +%Y), The Android Open Source Project |
| 59 | // |
| 60 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 61 | // you may not use this file except in compliance with the License. |
| 62 | // You may obtain a copy of the License at |
| 63 | // |
| 64 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 65 | // |
| 66 | // Unless required by applicable law or agreed to in writing, software |
| 67 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 68 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 69 | // See the License for the specific language governing permissions and |
| 70 | // limitations under the License. |
| 71 | |
Frederick Mayle | c528e67 | 2023-10-15 18:28:13 +0000 | [diff] [blame] | 72 | use zerocopy::{AsBytes, FromBytes, FromZeroes}; |
Dov Shlachter | 6676eb6 | 2023-08-22 15:11:08 -0700 | [diff] [blame] | 73 | |
| 74 | EOF |
| 75 | |
| 76 | rustfmt ${BOOTIMG_PRIV} --config-path system/tools/aidl/rustfmt.toml |