xref: /aosp_15_r20/build/bazel/toolchains/rust/bootstrap/defs.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1*7594170eSAndroid Build Coastguard Worker# Copyright (C) 2023 The Android Open Source Project
2*7594170eSAndroid Build Coastguard Worker#
3*7594170eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*7594170eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*7594170eSAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*7594170eSAndroid Build Coastguard Worker#
7*7594170eSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
8*7594170eSAndroid Build Coastguard Worker#
9*7594170eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*7594170eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*7594170eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*7594170eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*7594170eSAndroid Build Coastguard Worker# limitations under the License.
14*7594170eSAndroid Build Coastguard Worker
15*7594170eSAndroid Build Coastguard Worker"""
16*7594170eSAndroid Build Coastguard WorkerThis file contains rule and transition for building rust toolchain for device
17*7594170eSAndroid Build Coastguard Worker"""
18*7594170eSAndroid Build Coastguard Worker
19*7594170eSAndroid Build Coastguard Worker# Flags common to builds of the standard library.
20*7594170eSAndroid Build Coastguard Worker_EXTRA_FLAGS_FOR_STDLIB_BUILDS = [
21*7594170eSAndroid Build Coastguard Worker    # Even though --cap-lints=allow is set at the toolchain level (b/301466790),
22*7594170eSAndroid Build Coastguard Worker    # setting it again here is needed to disble linting in all upstream deps
23*7594170eSAndroid Build Coastguard Worker    "--cap-lints=allow",
24*7594170eSAndroid Build Coastguard Worker]
25*7594170eSAndroid Build Coastguard Worker
26*7594170eSAndroid Build Coastguard Worker_TRANSITION_OUTPUTS = [
27*7594170eSAndroid Build Coastguard Worker    "//command_line_option:compilation_mode",
28*7594170eSAndroid Build Coastguard Worker    "//build/bazel/toolchains/rust/bootstrap:enable_base_toolchain",
29*7594170eSAndroid Build Coastguard Worker    "@rules_rust//:extra_rustc_flags",
30*7594170eSAndroid Build Coastguard Worker    "@rules_rust//:extra_exec_rustc_flags",
31*7594170eSAndroid Build Coastguard Worker    "@rules_rust//rust/settings:use_real_import_macro",
32*7594170eSAndroid Build Coastguard Worker    "@rules_rust//rust/settings:pipelined_compilation",
33*7594170eSAndroid Build Coastguard Worker    "//command_line_option:cpu",
34*7594170eSAndroid Build Coastguard Worker]
35*7594170eSAndroid Build Coastguard Worker
36*7594170eSAndroid Build Coastguard Workerdef _base_transition_impl(_, __):
37*7594170eSAndroid Build Coastguard Worker    return {
38*7594170eSAndroid Build Coastguard Worker        "//build/bazel/toolchains/rust/bootstrap:enable_base_toolchain": True,
39*7594170eSAndroid Build Coastguard Worker        "//command_line_option:compilation_mode": "opt",
40*7594170eSAndroid Build Coastguard Worker        "//command_line_option:cpu": "k8",
41*7594170eSAndroid Build Coastguard Worker        "@rules_rust//:extra_rustc_flags": _EXTRA_FLAGS_FOR_STDLIB_BUILDS,
42*7594170eSAndroid Build Coastguard Worker        "@rules_rust//:extra_exec_rustc_flags": _EXTRA_FLAGS_FOR_STDLIB_BUILDS,
43*7594170eSAndroid Build Coastguard Worker        "@rules_rust//rust/settings:use_real_import_macro": False,
44*7594170eSAndroid Build Coastguard Worker        "@rules_rust//rust/settings:pipelined_compilation": True,
45*7594170eSAndroid Build Coastguard Worker    }
46*7594170eSAndroid Build Coastguard Worker
47*7594170eSAndroid Build Coastguard Worker_base_transition = transition(
48*7594170eSAndroid Build Coastguard Worker    inputs = [],
49*7594170eSAndroid Build Coastguard Worker    outputs = _TRANSITION_OUTPUTS,
50*7594170eSAndroid Build Coastguard Worker    implementation = _base_transition_impl,
51*7594170eSAndroid Build Coastguard Worker)
52*7594170eSAndroid Build Coastguard Worker
53*7594170eSAndroid Build Coastguard Worker# Re-exports libs passed in `srcs` attribute. Used together with
54*7594170eSAndroid Build Coastguard Worker# transitions to build stdlibs at various stages.
55*7594170eSAndroid Build Coastguard Workerdef _with_base_transition_impl(ctx):
56*7594170eSAndroid Build Coastguard Worker    return [DefaultInfo(files = depset(ctx.files.srcs))]
57*7594170eSAndroid Build Coastguard Worker
58*7594170eSAndroid Build Coastguard Worker# For each os + arch configuration, there is a pair of
59*7594170eSAndroid Build Coastguard Worker# 1. `<os_arch>_base_rust_toolchain`
60*7594170eSAndroid Build Coastguard Worker# 2. `<os_arch>_rust_toolchain`
61*7594170eSAndroid Build Coastguard Worker# registered in WORKSPACE. The base toolchains are only enabled when
62*7594170eSAndroid Build Coastguard Worker# `base_toolchain_enabled` `config_setting` is enabled.
63*7594170eSAndroid Build Coastguard Worker# Because `base_toolchain_enabled` is False by default, only <os_arch>_rust_toolchain
64*7594170eSAndroid Build Coastguard Worker# is enabled for a particular os + arch configuration and hence is resolved to build
65*7594170eSAndroid Build Coastguard Worker# a normal downstream rust target.
66*7594170eSAndroid Build Coastguard Worker#
67*7594170eSAndroid Build Coastguard Worker# with_base_transition attachs an incoming transition to `enable_base_toolchain`
68*7594170eSAndroid Build Coastguard Worker# which enable the base toolchains. When building for a particular configuration,
69*7594170eSAndroid Build Coastguard Worker# 1. `<os_arch>_base_rust_toolchain`
70*7594170eSAndroid Build Coastguard Worker# 2. `<os_arch>_rust_toolchain`
71*7594170eSAndroid Build Coastguard Worker# are enabled.
72*7594170eSAndroid Build Coastguard Worker# Because the base toolchain is registered before the non-base one, Bazel resolves
73*7594170eSAndroid Build Coastguard Worker# to the base toolchain. This mechanism allows building rust stdlibs using the base toolchain
74*7594170eSAndroid Build Coastguard Workerwith_base_transition = rule(
75*7594170eSAndroid Build Coastguard Worker    implementation = _with_base_transition_impl,
76*7594170eSAndroid Build Coastguard Worker    attrs = {
77*7594170eSAndroid Build Coastguard Worker        "srcs": attr.label_list(
78*7594170eSAndroid Build Coastguard Worker            allow_files = True,
79*7594170eSAndroid Build Coastguard Worker        ),
80*7594170eSAndroid Build Coastguard Worker        "_allowlist_function_transition": attr.label(
81*7594170eSAndroid Build Coastguard Worker            default = Label("@bazel_tools//tools/allowlists/function_transition_allowlist"),
82*7594170eSAndroid Build Coastguard Worker        ),
83*7594170eSAndroid Build Coastguard Worker    },
84*7594170eSAndroid Build Coastguard Worker    cfg = _base_transition,
85*7594170eSAndroid Build Coastguard Worker)
86*7594170eSAndroid Build Coastguard Worker
87*7594170eSAndroid Build Coastguard Workerdef _toolchain_sysroot_impl(ctx):
88*7594170eSAndroid Build Coastguard Worker    sysroot = ctx.attr.dirname
89*7594170eSAndroid Build Coastguard Worker    outputs = []
90*7594170eSAndroid Build Coastguard Worker
91*7594170eSAndroid Build Coastguard Worker    rustlibdir = "{}/lib/rustlib/{}/lib".format(sysroot, ctx.attr.target_triple)
92*7594170eSAndroid Build Coastguard Worker    rustbindir = "{}/bin".format(sysroot)
93*7594170eSAndroid Build Coastguard Worker
94*7594170eSAndroid Build Coastguard Worker    for inp in ctx.files.srcs:
95*7594170eSAndroid Build Coastguard Worker        if inp.short_path in ctx.attr.tools:
96*7594170eSAndroid Build Coastguard Worker            out = ctx.actions.declare_file(rustbindir + "/" + ctx.attr.tools[inp.short_path])
97*7594170eSAndroid Build Coastguard Worker        else:
98*7594170eSAndroid Build Coastguard Worker            out = ctx.actions.declare_file(rustlibdir + "/" + inp.basename)
99*7594170eSAndroid Build Coastguard Worker
100*7594170eSAndroid Build Coastguard Worker        outputs.append(out)
101*7594170eSAndroid Build Coastguard Worker        ctx.actions.symlink(output = out, target_file = inp)
102*7594170eSAndroid Build Coastguard Worker
103*7594170eSAndroid Build Coastguard Worker    return [DefaultInfo(
104*7594170eSAndroid Build Coastguard Worker        files = depset(outputs),
105*7594170eSAndroid Build Coastguard Worker        runfiles = ctx.runfiles(files = outputs),
106*7594170eSAndroid Build Coastguard Worker    )]
107*7594170eSAndroid Build Coastguard Worker
108*7594170eSAndroid Build Coastguard Workertoolchain_sysroot = rule(
109*7594170eSAndroid Build Coastguard Worker    implementation = _toolchain_sysroot_impl,
110*7594170eSAndroid Build Coastguard Worker    doc = """Creates a directory tree with copies of the passed Rust libraries
111*7594170eSAndroid Build Coastguard Worker    and tools, suitable to use in a rust_stdlib_filegroup.
112*7594170eSAndroid Build Coastguard Worker
113*7594170eSAndroid Build Coastguard Worker    The `srcs` attribute should enumerate the libraries and tools. Tools are
114*7594170eSAndroid Build Coastguard Worker    distinguished from libraries via the `tools` attribute, which should
115*7594170eSAndroid Build Coastguard Worker    contain an entry from the tool short_path to its final name under
116*7594170eSAndroid Build Coastguard Worker    dirname/bin/
117*7594170eSAndroid Build Coastguard Worker
118*7594170eSAndroid Build Coastguard Worker    The libraries are processed by creating symlinks to them in a local
119*7594170eSAndroid Build Coastguard Worker    directory rooted at `dirname`, e.g.,
120*7594170eSAndroid Build Coastguard Worker    dirname/lib/rustlib/x86_64-unknown-linux-gnu/lib/
121*7594170eSAndroid Build Coastguard Worker
122*7594170eSAndroid Build Coastguard Worker    The output under `dirname` is intended to constitute a valid sysroot, per
123*7594170eSAndroid Build Coastguard Worker    https://rustc-dev-guide.rust-lang.org/building/bootstrapping.html#what-is-a-sysroot
124*7594170eSAndroid Build Coastguard Worker    """,
125*7594170eSAndroid Build Coastguard Worker    attrs = {
126*7594170eSAndroid Build Coastguard Worker        "dirname": attr.string(
127*7594170eSAndroid Build Coastguard Worker            mandatory = True,
128*7594170eSAndroid Build Coastguard Worker        ),
129*7594170eSAndroid Build Coastguard Worker        "target_triple": attr.string(
130*7594170eSAndroid Build Coastguard Worker            doc = "The target triple for the rlibs.",
131*7594170eSAndroid Build Coastguard Worker            default = "x86_64-unknown-linux-gnu",
132*7594170eSAndroid Build Coastguard Worker        ),
133*7594170eSAndroid Build Coastguard Worker        "srcs": attr.label_list(
134*7594170eSAndroid Build Coastguard Worker            allow_files = True,
135*7594170eSAndroid Build Coastguard Worker        ),
136*7594170eSAndroid Build Coastguard Worker        "tools": attr.string_dict(
137*7594170eSAndroid Build Coastguard Worker            doc = "A map from tool's short_path to its final name under bin/",
138*7594170eSAndroid Build Coastguard Worker        ),
139*7594170eSAndroid Build Coastguard Worker    },
140*7594170eSAndroid Build Coastguard Worker)
141