xref: /aosp_15_r20/build/bazel/toolchains/rust/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("@env//:env.bzl", "env")
16load("@rules_rust//proto/protobuf:toolchain.bzl", "rust_proto_toolchain")
17load("@rules_rust//rust:toolchain.bzl", "rust_toolchain")
18load("@soong_injection//rust_toolchain:constants.bzl", "constants")
19load("//build/bazel/toolchains/rust:flags.bzl", "flags")
20load("//build/bazel/toolchains/rust:platforms.bzl", "platforms")
21
22package(default_visibility = ["//visibility:public"])
23
24rust_version = env.get(
25    "RUST_PREBUILTS_VERSION",
26    constants.RUST_DEFAULT_VERSION,
27)
28
29rust_prebuilts_path = "//prebuilts/rust/linux-x86/" + rust_version
30
31genrule(
32    name = "no_op_file",
33    outs = ["no_op_file.txt"],
34    cmd = "touch $(OUTS)",
35)
36
37rust_proto_toolchain(
38    name = "proto-toolchain-impl",
39    edition = "2021",
40    grpc_compile_deps = [],
41    # TODO: GRPC-providing rust_protobuf_host modules still use protobuf2
42    # so that grpc_rust_plugin still depend son deprecated protobuf modules
43    # Fix libbt_facade_proto and libbt_topshim_facade_protobuf to use protobuf3
44    # and upgrade grpc_rust_plugin to use new protobuf modules like protoc-gen-rust
45    grpc_plugin = ":no_op_file",
46    proto_compile_deps = ["//external/rust/crates/protobuf:libprotobuf"],
47    proto_plugin = "//external/rust/crates/protobuf-codegen:protoc-gen-rust",
48    protoc = "//external/protobuf:aprotoc",
49)
50
51toolchain(
52    name = "proto-toolchain",
53    toolchain = ":proto-toolchain-impl",
54    toolchain_type = "@rules_rust//proto/protobuf:toolchain_type",
55)
56
57rust_toolchain(
58    name = "rust_toolchain_x86_64_unknown-linux-gnu_impl",
59    binary_ext = "",
60    default_edition = "2018",
61    dylib_ext = ".so",
62    exec_triple = "x86_64-unknown-linux-gnu",
63    extra_rustc_flags = flags.global_rustc_flags + flags.linux_host_rustc_flags,
64    rust_doc = rust_prebuilts_path + ":bin/rustdoc",
65    rust_std = rust_prebuilts_path + ":prebuilt_stdlibs",
66    rustc = rust_prebuilts_path + ":bin/rustc",
67    staticlib_ext = ".a",
68    stdlib_linkflags = [],
69    target_triple = "x86_64-unknown-linux-gnu",
70)
71
72toolchain(
73    name = "rust_toolchain_x86_64_unknown-linux-gnu",
74    exec_compatible_with = ["//build/bazel_common_rules/platforms/os:linux"],
75    target_compatible_with = ["//build/bazel_common_rules/platforms/os:linux"],
76    toolchain = ":rust_toolchain_x86_64_unknown-linux-gnu_impl",
77    toolchain_type = "@rules_rust//rust:toolchain_type",
78)
79
80[
81    rust_toolchain(
82        name = "rust_toolchain_" + os + "_" + arch + "_impl",
83        binary_ext = "",
84        clippy_driver = rust_prebuilts_path + ":bin/clippy-driver",
85        default_edition = "2018",
86        dylib_ext = ".so",
87        exec_triple = "x86_64-unknown-linux-gnu",
88        # The system sysroot for the prebuilt compiler is prebuilts/rust/linux-x86/<rust_version>
89        # At runtime, rust rules set `-L <path-to-sysroot_with_stdlibs>/lib/rustlib/<target-triple>/lib`
90        # which is the directory of standard library built from source.
91        # We set --sysroot to /dev/null so that rustc does not error due
92        # to there being multiple candidates for stdlibs.
93        #
94        # error[E0464]: multiple candidates for `rlib` dependency `std` found
95        #   |
96        #   = note: candidate #1: bazel-built <OUR_DIR>/bin/prebuilts/rust/linux-x86/1.69.0/lib/rustlib/src/rust/libstd-1674459207.rlib
97        #   = note: candidate #2: prebuilt /prebuilts/rust/linux-x86/1.69.0/lib/rustlib/aarch64-linux-android/lib/libstd-33356ea78c96f310.so
98        #
99        # See https://doc.rust-lang.org/rustc/command-line-arguments.html#--sysroot-override-the-system-root
100        extra_rustc_flags = [
101            "--sysroot=/dev/null",
102        ] + rustc_flags + flags.device_global_rustc_flags + flags.global_rustc_flags,
103        rust_doc = rust_prebuilts_path + ":bin/rustdoc",
104        rust_std = "//build/bazel/toolchains/rust/bootstrap:rust_stdlibs",
105        rustc = rust_prebuilts_path + ":bin/rustc",
106        rustc_lib = "//build/bazel/toolchains/rust/bootstrap:sysroot_with_stdlibs",
107        rustfmt = rust_prebuilts_path + ":bin/rustfmt",
108        staticlib_ext = ".a",
109        stdlib_linkflags = [],
110        target_triple = target_triple,
111    )
112    for (target_triple, os, arch, rustc_flags) in platforms
113]
114
115[
116    toolchain(
117        name = "rust_toolchain_" + os + "_" + arch,
118        exec_compatible_with = ["//build/bazel_common_rules/platforms/os:linux"],
119        target_compatible_with = [
120            "//build/bazel_common_rules/platforms/arch:" + arch,
121            "//build/bazel_common_rules/platforms/os:" + os,
122        ],
123        toolchain = ":rust_toolchain_" + os + "_" + arch + "_impl",
124        toolchain_type = "@rules_rust//rust:toolchain_type",
125    )
126    for (target_triple, os, arch, _) in platforms
127]
128