xref: /aosp_15_r20/build/bazel/toolchains/rust/bootstrap/BUILD.bazel (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
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("@bazel_skylib//rules:common_settings.bzl", "bool_setting")
16load("@env//:env.bzl", "env")
17load("@rules_rust//rust:defs.bzl", "rust_stdlib_filegroup")
18load("@rules_rust//rust:toolchain.bzl", "rust_toolchain")
19load("@soong_injection//rust_toolchain:constants.bzl", "constants")
20load("//build/bazel/toolchains/rust:flags.bzl", "flags")
21load("//build/bazel/toolchains/rust:platforms.bzl", "platforms")
22load(":defs.bzl", "toolchain_sysroot", "with_base_transition")
23
24package(default_visibility = ["//build/bazel/toolchains/rust:__subpackages__"])
25
26rust_version = env.get(
27    "RUST_PREBUILTS_VERSION",
28    constants.RUST_DEFAULT_VERSION,
29)
30
31rust_prebuilts_path = "//prebuilts/rust/linux-x86/" + rust_version
32
33[
34    rust_toolchain(
35        name = "rust_toolchain_" + os + "_" + arch + "_base_impl",
36        binary_ext = "",
37        clippy_driver = rust_prebuilts_path + ":bin/clippy-driver",
38        default_edition = "2018",
39        dylib_ext = ".so",
40        exec_triple = "x86_64-unknown-linux-gnu",
41        extra_rustc_flags = rustc_flags + flags.device_global_rustc_flags + flags.global_rustc_flags,
42        rust_doc = rust_prebuilts_path + ":bin/rustdoc",
43        rust_std = rust_prebuilts_path + ":prebuilt_stdlibs",
44        rustc = rust_prebuilts_path + ":bin/rustc",
45        rustfmt = rust_prebuilts_path + ":bin/rustfmt",
46        staticlib_ext = ".a",
47        stdlib_linkflags = [],
48        target_triple = target_triple,
49    )
50    for (target_triple, os, arch, rustc_flags) in platforms
51]
52
53[
54    toolchain(
55        name = "rust_toolchain_" + os + "_" + arch + "_base",
56        exec_compatible_with = ["//build/bazel_common_rules/platforms/os:linux"],
57        target_compatible_with = [
58            "//build/bazel_common_rules/platforms/arch:" + arch,
59            "//build/bazel_common_rules/platforms/os:" + os,
60        ],
61        target_settings = [":base_toolchain_enabled"],
62        toolchain = ":rust_toolchain_" + os + "_" + arch + "_base_impl",
63        toolchain_type = "@rules_rust//rust:toolchain_type",
64    )
65    for (_, os, arch, __) in platforms
66]
67
68bool_setting(
69    name = "enable_base_toolchain",
70    build_setting_default = False,
71    visibility = ["//visibility:private"],
72)
73
74config_setting(
75    name = "base_toolchain_enabled",
76    flag_values = {
77        ":enable_base_toolchain": "True",
78    },
79)
80
81with_base_transition(
82    name = "stdlibs",
83    srcs = [rust_prebuilts_path + ":stdlib_sources"],
84)
85
86# TODO: b/295918553 - See whether this is actually needed given that
87# rust_toolchain already generates a sysroot with stdlibs and tools internally
88# https://github.com/bazelbuild/rules_rust/blob/ca750fa83d75a2408be93519e9c4d1a2b8b2a087/rust/toolchain.bzl#L495
89toolchain_sysroot(
90    name = "sysroot_with_stdlibs",
91    srcs = [":stdlibs"],
92    dirname = "sysroot_with_stdlibs",
93    target_triple = "aarch64-linux-android",
94)
95
96rust_stdlib_filegroup(
97    name = "rust_stdlibs",
98    srcs = [":sysroot_with_stdlibs"],
99)
100