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_toolchain.bzl", "link_static_cc_library") 16load("@gbl//toolchain:gbl_workspace_util.bzl", "ANDROID_RUST_LINTS") 17load("@gbl_llvm_prebuilts//:info.bzl", "LLVM_PREBUILTS_C_INCLUDE") 18load("@rules_rust//bindgen:defs.bzl", "rust_bindgen") 19load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") 20 21package( 22 default_visibility = ["//visibility:public"], 23) 24 25exports_files(glob(["**/*"])) 26 27rust_bindgen( 28 name = "libfdt_c_bindgen", 29 bindgen_flags = [ 30 "--ctypes-prefix", 31 "core::ffi", 32 "--use-core", 33 "--with-derive-custom-struct=fdt_header=AsBytes,FromBytes,FromZeroes,PartialEq", 34 "--allowlist-function", 35 "(fdt_.*)", 36 "--allowlist-type", 37 "(fdt_.*)", 38 "--raw-line", 39 """ 40#![allow(non_camel_case_types)] 41#![allow(non_snake_case)] 42#![allow(dead_code)] 43#![allow(unsafe_op_in_unsafe_fn)] 44#![cfg_attr(not(test), no_std)] 45 46use zerocopy::{AsBytes, FromBytes, FromZeroes}; 47""", 48 ], 49 cc_lib = "@libfdt_c", 50 # For x86_32, we need to explicitly specify 32bit architecture. 51 clang_flags = select({ 52 "@gbl//toolchain:gbl_rust_uefi_x86_32": ["-m32"], 53 "//conditions:default": ["-m64"], 54 }) + [ 55 "-I{}".format(LLVM_PREBUILTS_C_INCLUDE), 56 "-nostdinc", 57 ], 58 header = "@libfdt_c//:libfdt.h", 59) 60 61rust_bindgen( 62 name = "libufdt_c_bindgen", 63 bindgen_flags = [ 64 "--ctypes-prefix", 65 "core::ffi", 66 "--use-core", 67 "--opaque-type", 68 "fdt_header", 69 "--allowlist-function", 70 "ufdt_apply_multioverlay", 71 "--raw-line", 72 """ 73# ![cfg_attr(not(test), no_std)] 74""", 75 ], 76 cc_lib = "@libufdt_c", 77 # For x86_32, we need to explicitly specify 32bit architecture. 78 clang_flags = select({ 79 "@gbl//toolchain:gbl_rust_uefi_x86_32": ["-m32"], 80 "//conditions:default": ["-m64"], 81 }) + [ 82 "-I{}".format(LLVM_PREBUILTS_C_INCLUDE), 83 "-nostdinc", 84 ], 85 header = "@libufdt_c//:include/ufdt_overlay.h", 86) 87 88rust_library( 89 name = "libfdt_bindgen", 90 srcs = [":libfdt_c_bindgen"], 91 deps = ["@zerocopy"], 92) 93 94rust_library( 95 name = "libufdt_bindgen", 96 srcs = [":libufdt_c_bindgen"], 97) 98 99rust_library( 100 name = "libfdt_sysdeps", 101 srcs = ["deps/lib.rs"], 102 rustc_flags = ANDROID_RUST_LINTS, 103 deps = [ 104 "@gbl//libc", 105 ], 106) 107 108link_static_cc_library( 109 name = "libfdt_sysdeps_static", 110 cc_library = ":libfdt_sysdeps", 111) 112 113rust_library( 114 name = "libfdt", 115 srcs = ["src/lib.rs"], 116 crate_name = "fdt", 117 edition = "2021", 118 rustc_flags = ANDROID_RUST_LINTS, 119 deps = [ 120 ":libfdt_bindgen", 121 ":libfdt_c_static", 122 ":libfdt_sysdeps_static", 123 ":libufdt_bindgen", 124 ":libufdt_c_static", 125 "@arrayvec", 126 "@gbl//libc", 127 "@gbl//liberror", 128 "@gbl//libsafemath", 129 "@zerocopy", 130 ], 131) 132 133rust_test( 134 name = "libfdt_test", 135 compile_data = [ 136 "@gbl//libfdt/test/data:all", 137 ], 138 crate = ":libfdt", 139 rustc_flags = ANDROID_RUST_LINTS, 140) 141 142link_static_cc_library( 143 name = "libfdt_c_static", 144 cc_library = "@libfdt_c", 145) 146 147link_static_cc_library( 148 name = "libufdt_c_static", 149 cc_library = "@libufdt_c", 150) 151