• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (C) 2023 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("@rules_rust//bindgen:defs.bzl", "rust_bindgen")
16load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
17
18package(
19    default_visibility = ["//visibility:public"],
20)
21
22# Newer version of `rust_bindgen` requires a `cc_library` target that actually produces a static
23# library and instead of only headers. Thus we generate a placeholder source file to meet the
24# requirement.
25genrule(
26    name = "bindgen_noop_cc",
27    outs = ["bindgen_noop_cc.cc"],
28    cmd = "touch $(OUTS)",
29)
30
31cc_library(
32    name = "bootimg_cc_header",
33    srcs = [":bindgen_noop_cc"],
34    hdrs = ["@mkbootimg//:include/bootimg/bootimg.h"],
35)
36
37# The following genernates rust binding for C definitions in `bootimg.h`. It uses the same
38# parameters in script platform/system/tools/mkbootimg/rust/bindgen.sh used for generating the
39# checked-in version platform/system/tools/mkbootimg/rust/bootimg_priv.rs.
40
41BLOCKED_TYPES_RE = "__.+|.?int.+"
42
43BLOCKED_ITEMS_RE = "_.+|.?INT.+|PTR.+|ATOMIC.+|.+SOURCE|.+_H|SIG_.+|SIZE_.+|.?CHAR.+"
44
45CUSTOM_STRUCT_RE = "(vendor_)?(boot_img_hdr|ramdisk_table_entry)_v\\d+"
46
47CUSTOM_STRUCT_DERIVES = "AsBytes,FromBytes,FromZeroes,PartialEq,Copy,Clone,Debug"
48
49rust_bindgen(
50    name = "bootimg_defs_bindgen",
51    bindgen_flags = [
52        "--ctypes-prefix",
53        "core::ffi",
54        "--use-core",
55        "--with-derive-default",
56        "--blocklist-type={}".format(BLOCKED_TYPES_RE),
57        "--blocklist-item={}".format(BLOCKED_ITEMS_RE),
58        "--with-derive-custom-struct={}={}".format(CUSTOM_STRUCT_RE, CUSTOM_STRUCT_DERIVES),
59        "--raw-line",
60        """
61#![allow(non_camel_case_types)]
62#![allow(non_snake_case)]
63#![cfg_attr(not(test), no_std)]
64use zerocopy::{AsBytes, FromBytes, FromZeroes};""",
65    ],
66    cc_lib = ":bootimg_cc_header",
67    clang_flags = select({
68        # For x86_32, we need to explicitly specify 32bit architecture.
69        "@gbl//toolchain:gbl_rust_uefi_x86_32": ["-m32"],
70        "//conditions:default": ["-m64"],
71    }) + [
72        "-x",
73        "c++",
74        "-nostdinc",
75    ],
76    header = "@mkbootimg//:include/bootimg/bootimg.h",
77)
78
79# The source files do not specify "no_std" and thus can't be used as library crate root in our EFI
80# environment. For now, the workaround is to copy over and wrap them under a top level crate that
81# enforces no_std.
82genrule(
83    name = "bootimg_sources_gen",
84    srcs = [
85        "@mkbootimg//:rust/bootimg.rs",
86        ":bootimg_defs_bindgen",
87    ],
88    outs = [
89        "src/bootimg.rs",
90        "src/defs.rs",
91    ],
92    # Copy each src[i] to outs[i]
93    cmd = """
94        IFS=" " read -a srcs <<< "$(SRCS)" && \
95        IFS=" " read -a outs <<< "$(OUTS)" && \
96        for index in $${!srcs[@]}; do cp $${srcs[$$index]} $${outs[$$index]}; done
97""",
98)
99
100rust_library(
101    # The naming is expected by "@mkbootimg//:rust/bootimg.rs".
102    name = "bootimg_private",
103    srcs = ["src/defs.rs"],
104    crate_root = "src/defs.rs",
105    data = [":bootimg_sources_gen"],
106    deps = ["@zerocopy"],
107)
108
109rust_library(
110    name = "libbootimg",
111    srcs = [
112        "src/bootimg.rs",
113        "src/lib.rs",
114    ],
115    crate_name = "bootimg",
116    data = [":bootimg_sources_gen"],
117    deps = [
118        ":bootimg_private",
119        "@zerocopy",
120    ],
121)
122
123rust_test(
124    name = "libbootimg_test",
125    crate = ":libbootimg",
126)
127