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 15""" 16This file contains rules and logic for setting up GBL workspace dependencies in the AOSP 17u-boot-mainline branch. 18""" 19 20load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") 21load("@gbl//toolchain:gbl_workspace_util.bzl", "android_rust_prebuilts", "gbl_llvm_prebuilts") 22 23_CLANG_VERSION = "r547379" 24 25def rust_crate_build_file( 26 name, 27 rule = "rust_library", 28 crate_name = "", 29 deps = [], 30 proc_macro_deps = [], 31 features = [], 32 edition = "2021", 33 rustc_flags = []): 34 """Generate BUILD file content for a rust crate 35 36 This helper is suitable for crates that have straightforward build rules. Specifically, the 37 crate contains a single Rust target that includes all source files under the repo. 38 There is not any need of preprocessing, patching or source generation. 39 40 Args: 41 name (String): name of the rust_library target. 42 rule (String): Bazel Rust rule to build, defaults to `rust_library`. 43 crate_name (String): name of the rust_library crate, same as name by default. 44 deps (List of strings): The `deps` field. 45 proc_macro_deps (List of strings): The `proc_macro_deps` field. 46 features (List of strings): The `features` field. 47 edition (String): Rust edition. 48 rustc_flags (List of strings): The `rustc_flags` field. 49 50 Returns: 51 A string for the BUILD file content. 52 """ 53 crate_name = name if len(crate_name) == 0 else crate_name 54 deps = "[{}]".format(",".join(["\"{}\"".format(ele) for ele in deps])) 55 proc_macro_deps = "[{}]".format(",".join(["\"{}\"".format(ele) for ele in proc_macro_deps])) 56 features = "[{}]".format(",".join(["\"{}\"".format(ele) for ele in features])) 57 rustc_flags = "[{}]".format(",".join(["\"{}\"".format(ele) for ele in rustc_flags])) 58 return """ 59load("@rules_rust//rust:defs.bzl", \"{rule}\") 60 61{rule}( 62 name = \"{}\", 63 crate_name = \"{}\", 64 srcs = glob(["**/*.rs"]), 65 crate_features = {}, 66 edition = \"{edition}\", 67 rustc_flags = {}, 68 visibility = ["//visibility:public"], 69 deps = {}, 70 proc_macro_deps = {} 71) 72""".format(name, crate_name, features, rustc_flags, deps, proc_macro_deps, edition = edition, rule = rule) 73 74def define_gbl_workspace(name = None): 75 """Set up worksapce dependencies for GBL 76 77 Dependencies are checked out during "repo init". The rule simply maps them to the correct repo 78 names. 79 80 Args: 81 name (String): Placeholder for buildifier check. 82 """ 83 maybe( 84 repo_rule = native.local_repository, 85 name = "rules_rust", 86 path = "external/bazelbuild-rules_rust", 87 ) 88 89 maybe( 90 repo_rule = native.local_repository, 91 name = "rules_shell", 92 path = "external/bazelbuild-rules_shell", 93 ) 94 95 maybe( 96 repo_rule = native.local_repository, 97 name = "rules_license", 98 path = "external/bazelbuild-rules_license", 99 ) 100 101 native.local_repository( 102 name = "googletest", 103 path = "external/googletest", 104 ) 105 106 native.new_local_repository( 107 name = "rules_rust_tinyjson", 108 path = "external/rust/android-crates-io/crates/tinyjson", 109 build_file = "@rules_rust//util/process_wrapper:BUILD.tinyjson.bazel", 110 ) 111 112 native.new_local_repository( 113 name = "llvm_linux_x86_64_prebuilts", 114 path = "prebuilts/clang/host/linux-x86/clang-{}".format(_CLANG_VERSION), 115 build_file_content = "", 116 ) 117 118 native.new_local_repository( 119 name = "linux_x86_64_sysroot", 120 path = "prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8", 121 build_file_content = """exports_files(glob(["**/*"])) 122cc_library( 123 name = "linux_x86_64_sysroot_include", 124 hdrs = glob(["sysroot/usr/include/**/*.h"]), 125 includes = [ "sysroot/usr/include" ], 126 visibility = ["//visibility:public"], 127) 128""", 129 ) 130 131 android_rust_prebuilts( 132 name = "rust_prebuilts", 133 path = "prebuilts/rust/", 134 build_file = "@gbl//toolchain:BUILD.android_rust_prebuilts.bazel", 135 ) 136 137 native.new_local_repository( 138 name = "bindgen", 139 path = "prebuilts/clang-tools/linux-x86/bin", 140 build_file_content = """exports_files(["bindgen"])""", 141 ) 142 143 native.new_local_repository( 144 name = "elfutils", 145 path = "external/elfutils", 146 build_file_content = """ 147cc_library( 148 name = "elf_type_header", 149 hdrs = ["libelf/elf.h"], 150 visibility = ["//visibility:public"], 151) 152""", 153 ) 154 155 native.new_local_repository( 156 name = "mkbootimg", 157 path = "tools/mkbootimg", 158 build_file_content = """exports_files(glob(["**/*"]))""", 159 ) 160 161 native.new_local_repository( 162 name = "libfdt_c", 163 path = "external/dtc/libfdt", 164 build_file = "@gbl//libfdt:BUILD.libfdt_c.bazel", 165 ) 166 167 native.new_local_repository( 168 name = "libufdt_c", 169 path = "external/libufdt", 170 build_file = "@gbl//libfdt:BUILD.libufdt_c.bazel", 171 ) 172 173 native.new_local_repository( 174 name = "libdttable_c", 175 path = "external/libufdt/utils/src", 176 build_file = "@gbl//libdttable:BUILD.libdttable_c.bazel", 177 ) 178 179 native.new_local_repository( 180 name = "arm_trusted_firmware", 181 path = "external/arm-trusted-firmware", 182 build_file = "@gbl//libboot/aarch64_cache_helper:BUILD.arm_trusted_firmware.bazel", 183 ) 184 185 native.new_local_repository( 186 name = "avb", 187 path = "external/avb", 188 build_file = "@gbl//libavb:BUILD.avb.bazel", 189 ) 190 191 native.new_local_repository( 192 name = "uuid", 193 path = "external/rust/android-crates-io/crates/uuid", 194 build_file_content = rust_crate_build_file("uuid"), 195 ) 196 197 native.new_local_repository( 198 name = "spin", 199 path = "external/rust/android-crates-io/crates/spin", 200 build_file_content = rust_crate_build_file( 201 "spin", 202 features = [ 203 "mutex", 204 "spin_mutex", 205 ], 206 rustc_flags = [ 207 "-A", 208 "unused_imports", 209 ], 210 ), 211 ) 212 213 native.new_local_repository( 214 name = "static_assertions", 215 path = "external/rust/android-crates-io/crates/static_assertions", 216 build_file_content = rust_crate_build_file("static_assertions"), 217 ) 218 219 native.new_local_repository( 220 name = "managed", 221 path = "external/rust/android-crates-io/crates/managed", 222 build_file_content = rust_crate_build_file( 223 "managed", 224 features = ["map"], 225 rustc_flags = [ 226 "-A", 227 "unused_macros", 228 "-A", 229 "redundant_semicolons", 230 ], 231 ), 232 ) 233 234 native.new_local_repository( 235 name = "itertools", 236 path = "external/rust/android-crates-io/crates/itertools", 237 build_file_content = rust_crate_build_file( 238 "itertools", 239 deps = ["@either"], 240 features = ["default", "use_std", "use_alloc"], 241 rustc_flags = ["-A", "dead_code"], 242 ), 243 ) 244 245 native.new_local_repository( 246 name = "itertools_noalloc", 247 path = "external/rust/android-crates-io/crates/itertools", 248 build_file_content = rust_crate_build_file( 249 "itertools_noalloc", 250 crate_name = "itertools", 251 features = [], 252 deps = ["@either_noalloc"], 253 rustc_flags = ["-A", "dead_code"], 254 ), 255 ) 256 257 native.new_local_repository( 258 name = "either", 259 path = "external/rust/android-crates-io/crates/either", 260 build_file_content = rust_crate_build_file( 261 "either", 262 features = ["default", "use_std"], 263 ), 264 ) 265 266 native.new_local_repository( 267 name = "either_noalloc", 268 path = "external/rust/android-crates-io/crates/either", 269 build_file_content = rust_crate_build_file( 270 "either_noalloc", 271 crate_name = "either", 272 features = [], 273 ), 274 ) 275 276 # TODO(b/383783832): migrate to android-crates-io 277 native.new_local_repository( 278 name = "smoltcp", 279 path = "external/rust/crates/smoltcp", 280 build_file = "@gbl//smoltcp:BUILD.smoltcp.bazel", 281 ) 282 283 native.new_local_repository( 284 name = "arrayvec", 285 path = "external/rust/android-crates-io/crates/arrayvec", 286 build_file_content = rust_crate_build_file( 287 "arrayvec", 288 rustc_flags = ["-A", "dead_code"], 289 ), 290 ) 291 292 native.new_local_repository( 293 name = "downcast", 294 path = "external/rust/android-crates-io/crates/downcast", 295 build_file_content = rust_crate_build_file( 296 "downcast", 297 features = ["default", "std"], 298 ), 299 ) 300 301 native.new_local_repository( 302 name = "fragile", 303 path = "external/rust/android-crates-io/crates/fragile", 304 build_file_content = rust_crate_build_file("fragile"), 305 ) 306 307 native.new_local_repository( 308 name = "lazy_static", 309 path = "external/rust/android-crates-io/crates/lazy_static", 310 build_file_content = rust_crate_build_file("lazy_static"), 311 ) 312 313 native.new_local_repository( 314 name = "mockall", 315 path = "external/rust/android-crates-io/crates/mockall", 316 build_file_content = rust_crate_build_file( 317 "mockall", 318 deps = [ 319 "@cfg_if", 320 "@downcast", 321 "@fragile", 322 "@lazy_static", 323 "@predicates", 324 "@predicates_tree", 325 ], 326 proc_macro_deps = ["@mockall_derive"], 327 ), 328 ) 329 330 native.new_local_repository( 331 name = "mockall_derive", 332 path = "external/rust/android-crates-io/crates/mockall_derive", 333 build_file_content = rust_crate_build_file( 334 "mockall_derive", 335 rule = "rust_proc_macro", 336 deps = ["@cfg_if", "@proc_macro2", "@quote", "@syn"], 337 ), 338 ) 339 340 native.new_local_repository( 341 name = "predicates", 342 path = "external/rust/android-crates-io/crates/predicates", 343 build_file_content = rust_crate_build_file( 344 "predicates", 345 deps = ["@itertools", "@predicates_core", "@termcolor"], 346 ), 347 ) 348 349 native.new_local_repository( 350 name = "predicates_core", 351 path = "external/rust/android-crates-io/crates/predicates-core", 352 build_file_content = rust_crate_build_file("predicates_core"), 353 ) 354 355 native.new_local_repository( 356 name = "predicates_tree", 357 path = "external/rust/android-crates-io/crates/predicates-tree", 358 build_file_content = rust_crate_build_file( 359 "predicates_tree", 360 deps = ["@predicates_core", "@termtree"], 361 ), 362 ) 363 364 native.new_local_repository( 365 name = "termcolor", 366 path = "external/rust/android-crates-io/crates/termcolor", 367 build_file_content = rust_crate_build_file("termcolor"), 368 ) 369 370 native.new_local_repository( 371 name = "termtree", 372 path = "external/rust/android-crates-io/crates/termtree", 373 build_file_content = rust_crate_build_file("termtree"), 374 ) 375 376 # TODO(b/383783832): migrate to android-crates-io 377 native.new_local_repository( 378 name = "zune_inflate", 379 path = "external/rust/crates/zune-inflate", 380 build_file_content = rust_crate_build_file( 381 "zune_inflate", 382 features = ["gzip"], 383 ), 384 ) 385 386 native.new_local_repository( 387 name = "lz4_flex", 388 path = "external/rust/android-crates-io/crates/lz4_flex", 389 build_file_content = rust_crate_build_file( 390 "lz4_flex", 391 features = ["safe-decode"], 392 rustc_flags = ["-A", "dead_code"], 393 ), 394 ) 395 396 native.new_local_repository( 397 name = "zbi", 398 path = "prebuilts/fuchsia_sdk/", 399 build_file = "//prebuilts/fuchsia_sdk:BUILD.zbi.bazel", 400 ) 401 402 native.new_local_repository( 403 name = "zerocopy", 404 path = "external/rust/android-crates-io/crates/zerocopy", 405 build_file_content = rust_crate_build_file( 406 "zerocopy", 407 features = ["derive", "simd", "zerocopy-derive"], 408 proc_macro_deps = ["@zerocopy_derive"], 409 ), 410 ) 411 412 native.new_local_repository( 413 name = "zerocopy_derive", 414 path = "external/rust/android-crates-io/crates/zerocopy-derive", 415 build_file_content = rust_crate_build_file( 416 "zerocopy_derive", 417 rule = "rust_proc_macro", 418 deps = ["@proc_macro2", "@quote", "@syn"], 419 ), 420 ) 421 422 native.new_local_repository( 423 name = "bitflags", 424 path = "external/rust/android-crates-io/crates/bitflags", 425 build_file_content = rust_crate_build_file("bitflags"), 426 ) 427 428 native.new_local_repository( 429 name = "byteorder", 430 path = "external/rust/android-crates-io/crates/byteorder", 431 build_file_content = rust_crate_build_file("byteorder"), 432 ) 433 434 native.new_local_repository( 435 name = "cfg_if", 436 path = "external/rust/android-crates-io/crates/cfg-if", 437 build_file_content = rust_crate_build_file("cfg_if"), 438 ) 439 440 native.new_local_repository( 441 name = "crc32fast", 442 path = "external/rust/android-crates-io/crates/crc32fast", 443 build_file_content = rust_crate_build_file( 444 "crc32fast", 445 deps = ["@cfg_if"], 446 # Current version of the crate doesn't compile with newer editions. 447 edition = "2015", 448 ), 449 ) 450 451 native.new_local_repository( 452 name = "hex", 453 path = "external/rust/android-crates-io/crates/hex", 454 build_file_content = rust_crate_build_file( 455 "hex", 456 features = ["alloc", "default", "std"], 457 ), 458 ) 459 460 native.new_local_repository( 461 name = "quote", 462 path = "external/rust/android-crates-io/crates/quote", 463 build_file_content = rust_crate_build_file( 464 "quote", 465 features = ["default", "proc-macro"], 466 deps = ["@proc_macro2"], 467 ), 468 ) 469 470 native.new_local_repository( 471 name = "unicode_ident", 472 path = "external/rust/android-crates-io/crates/unicode-ident", 473 build_file_content = rust_crate_build_file("unicode_ident"), 474 ) 475 476 native.new_local_repository( 477 name = "syn", 478 path = "external/rust/android-crates-io/crates/syn", 479 build_file_content = rust_crate_build_file( 480 "syn", 481 features = [ 482 "clone-impls", 483 "default", 484 "derive", 485 "extra-traits", 486 "full", 487 "parsing", 488 "printing", 489 "proc-macro", 490 "quote", 491 "visit", 492 "visit-mut", 493 ], 494 deps = ["@proc_macro2", "@quote", "@unicode_ident"], 495 ), 496 ) 497 498 native.new_local_repository( 499 name = "proc_macro2", 500 path = "external/rust/android-crates-io/crates/proc-macro2", 501 build_file_content = rust_crate_build_file( 502 "proc_macro2", 503 deps = ["@unicode_ident"], 504 features = ["default", "proc-macro", "span-locations"], 505 ), 506 ) 507 508 # Set up a repo to export LLVM tool/library/header/sysroot paths 509 gbl_llvm_prebuilts(name = "gbl_llvm_prebuilts") 510 511 # We don't register GBL toolchains here because they will be masked by toolchains from 512 # `build/kleaf//:` as they are registered earlier. Instead, we will pass GBL toolchains via 513 # bazel commandline argument "--extra_toolchains=@gbl//toolchain:all" when building GBL 514 # targets, which allows them to be evaluated first during toolchain resolution. 515