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
16set -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
24SCRATCH_DIR=$(mktemp -d)
25
26cleanup (){
27    rm -rf ${SCRATCH_DIR}
28}
29
30trap cleanup EXIT
31pushd ~/aosp > /dev/null
32
33BOOTIMG_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.
36BLOCKED_TYPES_RE="__.+|.?int.+"
37# The stdint include generates a lot of unnecessary constants that the
38# generated rust bindings really don't need.
39BLOCKED_ITEMS_RE="_.+|.?INT.+|PTR.+|ATOMIC.+|.+SOURCE|.+_H|SIG_.+|SIZE_.+|.?CHAR.+"
40CUSTOM_STRUCT_RE="(vendor_)?(boot_img_hdr|ramdisk_table_entry)_v\d+"
41CUSTOM_STRUCT_DERIVES="AsBytes,FromBytes,FromZeroes,PartialEq,Copy,Clone,Debug"
42BINDGEN_FLAGS="--use-core --with-derive-default"
43BOOTIMG_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.
47cp ${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
57cat << 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
72use zerocopy::{AsBytes, FromBytes, FromZeroes};
73
74EOF
75
76rustfmt ${BOOTIMG_PRIV} --config-path system/tools/aidl/rustfmt.toml
77