1# Copyright (C) 2023-2024 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15load("@gbl//toolchain:gbl_workspace_util.bzl", "ANDROID_RUST_LINTS") 16load("@rules_rust//bindgen:defs.bzl", "rust_bindgen") 17load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") 18 19package( 20 default_visibility = ["//visibility:public"], 21) 22 23# Newer version of `rust_bindgen` requires a `cc_library` target that actually produces a static 24# library and instead of only headers. Thus we generate a placeholder source file to meet the 25# requirement. 26genrule( 27 name = "bindgen_noop_cc", 28 outs = ["bindgen_noop_cc.cc"], 29 cmd = "touch $(OUTS)", 30) 31 32cc_library( 33 name = "bootimg_cc_header", 34 srcs = [":bindgen_noop_cc"], 35 hdrs = ["@mkbootimg//:include/bootimg/bootimg.h"], 36) 37 38# The following genernates rust binding for C definitions in `bootimg.h`. It uses the same 39# parameters in script platform/system/tools/mkbootimg/rust/bindgen.sh used for generating the 40# checked-in version platform/system/tools/mkbootimg/rust/bootimg_priv.rs. 41 42BLOCKED_TYPES_RE = "__.+|.?int.+" 43 44BLOCKED_ITEMS_RE = "_.+|.?INT.+|PTR.+|ATOMIC.+|.+SOURCE|.+_H|SIG_.+|SIZE_.+|.?CHAR.+" 45 46CUSTOM_STRUCT_RE = "(vendor_)?(boot_img_hdr|ramdisk_table_entry)_v\\d+" 47 48CUSTOM_STRUCT_DERIVES = "AsBytes,FromBytes,FromZeroes,PartialEq,Copy,Clone,Debug" 49 50rust_bindgen( 51 name = "bootimg_defs_bindgen", 52 bindgen_flags = [ 53 "--ctypes-prefix", 54 "core::ffi", 55 "--use-core", 56 "--with-derive-default", 57 "--blocklist-type={}".format(BLOCKED_TYPES_RE), 58 "--blocklist-item={}".format(BLOCKED_ITEMS_RE), 59 "--with-derive-custom-struct={}={}".format( 60 CUSTOM_STRUCT_RE, 61 CUSTOM_STRUCT_DERIVES, 62 ), 63 "--raw-line", 64 """ 65#![allow(non_camel_case_types)] 66#![allow(non_snake_case)] 67#![cfg_attr(not(test), no_std)] 68use zerocopy::{AsBytes, FromBytes, FromZeroes};""", 69 ], 70 cc_lib = ":bootimg_cc_header", 71 clang_flags = select( 72 { 73 # For x86_32, we need to explicitly specify 32bit architecture. 74 "@gbl//toolchain:gbl_rust_uefi_x86_32": ["-m32"], 75 "//conditions:default": ["-m64"], 76 }, 77 ) + [ 78 "-x", 79 "c++", 80 "-nostdinc", 81 ], 82 header = "@mkbootimg//:include/bootimg/bootimg.h", 83) 84 85# The source files do not specify "no_std" and thus can't be used as library crate root in our EFI 86# environment. For now, the workaround is to copy over and wrap them under a top level crate that 87# enforces no_std. 88genrule( 89 name = "bootimg_sources_gen", 90 srcs = [ 91 "@mkbootimg//:rust/bootimg.rs", 92 ":bootimg_defs_bindgen", 93 ], 94 outs = [ 95 "src/bootimg.rs", 96 "src/defs.rs", 97 ], 98 # Copy each src[i] to outs[i] 99 cmd = """ 100 IFS=" " read -a srcs <<< "$(SRCS)" && \ 101 IFS=" " read -a outs <<< "$(OUTS)" && \ 102 for index in $${!srcs[@]}; do cp $${srcs[$$index]} $${outs[$$index]}; done 103""", 104) 105 106rust_library( 107 # The naming is expected by "@mkbootimg//:rust/bootimg.rs". 108 name = "bootimg_bindgen", 109 srcs = ["src/defs.rs"], 110 crate_root = "src/defs.rs", 111 data = [":bootimg_sources_gen"], 112 deps = ["@zerocopy"], 113) 114 115rust_library( 116 name = "libbootimg", 117 srcs = [ 118 "src/bootimg.rs", 119 "src/lib.rs", 120 ], 121 crate_name = "bootimg", 122 data = [":bootimg_sources_gen"], 123 rustc_flags = ANDROID_RUST_LINTS, 124 deps = [ 125 ":bootimg_bindgen", 126 "@gbl//liberror", 127 "@zerocopy", 128 ], 129) 130 131rust_test( 132 name = "libbootimg_test", 133 crate = ":libbootimg", 134 rustc_flags = ANDROID_RUST_LINTS, 135) 136