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_llvm_prebuilts//:info.bzl", "LLVM_PREBUILTS_C_INCLUDE") 17load("@rules_rust//bindgen:defs.bzl", "rust_bindgen") 18load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") 19 20package( 21 default_visibility = ["//visibility:public"], 22) 23 24rust_bindgen( 25 name = "libfdt_c_bindgen", 26 bindgen_flags = [ 27 "--ctypes-prefix", 28 "core::ffi", 29 "--use-core", 30 "--with-derive-custom-struct=fdt_header=AsBytes,FromBytes,FromZeroes,PartialEq", 31 "--allowlist-function", 32 "(fdt_.*)", 33 "--allowlist-type", 34 "(fdt_.*)", 35 "--raw-line", 36 """ 37#![allow(non_camel_case_types)] 38#![allow(non_snake_case)] 39#![allow(dead_code)] 40#![allow(unsafe_op_in_unsafe_fn)] 41#![cfg_attr(not(test), no_std)] 42 43use zerocopy::{AsBytes, FromBytes, FromZeroes}; 44""", 45 ], 46 cc_lib = "@libfdt_c", 47 # For x86_32, we need to explicitly specify 32bit architecture. 48 clang_flags = select({ 49 "@gbl//toolchain:gbl_rust_uefi_x86_32": ["-m32"], 50 "//conditions:default": ["-m64"], 51 }) + [ 52 "-I{}".format(LLVM_PREBUILTS_C_INCLUDE), 53 "-nostdinc", 54 ], 55 header = "@libfdt_c//:libfdt.h", 56) 57 58genrule( 59 name = "bindgen_source", 60 srcs = [":libfdt_c_bindgen"], 61 outs = ["src/libfdt_c_Def.rs"], 62 cmd = "cp $(SRCS) $(OUTS)", 63) 64 65rust_library( 66 name = "libfdt_c_def", 67 srcs = [":bindgen_source"], 68 crate_root = ":bindgen_source", 69 deps = ["@zerocopy"], 70) 71 72rust_library( 73 name = "libfdt", 74 srcs = ["src/lib.rs"], 75 crate_name = "fdt", 76 edition = "2021", 77 deps = [ 78 ":libfdt_c_def", 79 ":libfdt_c_static", 80 "@gbl//libc", 81 "@zerocopy", 82 ], 83) 84 85rust_test( 86 name = "libfdt_test", 87 compile_data = ["@gbl//libfdt/test:test.dtb"], 88 crate = ":libfdt", 89) 90 91link_static_cc_library( 92 name = "libfdt_c_static", 93 cc_library = "@libfdt_c", 94) 95