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