xref: /aosp_15_r20/bootable/libbootloader/gbl/libboot/BUILD (revision 5225e6b173e52d2efc6bcf950c27374fd72adabc)
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("@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")
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 = "bindgen_cc_lib",
34    srcs = [":bindgen_noop_cc"],
35    deps = ["@linux_x86_64_sysroot//:linux_x86_64_sysroot_include"],
36)
37
38CUSTOM_DERIVES = "AsBytes,FromBytes,FromZeroes"
39
40rust_bindgen(
41    name = "x86_bootparam_bindgen",
42    bindgen_flags = [
43        "--ctypes-prefix",
44        "core::ffi",
45        "--use-core",
46        "--allowlist-type",
47        "boot_params",
48        "--with-derive-default",
49        "--with-derive-custom-struct=.*={}".format(CUSTOM_DERIVES),
50        "--with-derive-custom-union=.*={}".format(CUSTOM_DERIVES),
51        "--raw-line",
52        """
53#![allow(non_camel_case_types)]
54#![allow(non_snake_case)]
55#![cfg_attr(not(test), no_std)]
56use zerocopy::{AsBytes, FromBytes, FromZeroes};""",
57    ],
58    cc_lib = ":bindgen_cc_lib",
59    header = "@linux_x86_64_sysroot//:sysroot/usr/include/x86_64-linux-gnu/asm/bootparam.h",
60)
61
62rust_library(
63    name = "x86_bootparam_defs",
64    srcs = [":x86_bootparam_bindgen"],
65    crate_root = ":x86_bootparam_bindgen",
66    data = [":x86_bootparam_bindgen"],
67    deps = ["@zerocopy"],
68)
69
70rust_library(
71    name = "libboot",
72    srcs = glob(["**/*.rs"]),
73    crate_name = "boot",
74    edition = "2021",
75    rustc_flags = ANDROID_RUST_LINTS,
76    deps = [
77        ":x86_bootparam_defs",
78        "@gbl//liberror",
79        "@gbl//libsafemath",
80        "@zbi",
81        "@zerocopy",
82    ] + select({
83        "@gbl//toolchain:gbl_rust_uefi_aarch64": [
84            "@gbl//libboot/aarch64_cache_helper:aarch64_cache_helper_staticlib",
85        ],
86        "//conditions:default": [],
87    }),
88)
89