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