xref: /aosp_15_r20/external/bazelbuild-rules_rust/rust/private/toolchain_utils.bzl (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1"""A module defining toolchain utilities"""
2
3def _toolchain_files_impl(ctx):
4    toolchain = ctx.toolchains[str(Label("//rust:toolchain_type"))]
5
6    runfiles = None
7    if ctx.attr.tool == "cargo":
8        files = depset([toolchain.cargo])
9        runfiles = ctx.runfiles(
10            files = [
11                toolchain.cargo,
12                toolchain.rustc,
13            ],
14            transitive_files = toolchain.rustc_lib,
15        )
16    elif ctx.attr.tool == "cargo-clippy":
17        files = depset([toolchain.cargo_clippy])
18        runfiles = ctx.runfiles(
19            files = [
20                toolchain.cargo_clippy,
21                toolchain.clippy_driver,
22                toolchain.rustc,
23            ],
24            transitive_files = toolchain.rustc_lib,
25        )
26    elif ctx.attr.tool == "clippy":
27        files = depset([toolchain.clippy_driver])
28        runfiles = ctx.runfiles(
29            files = [
30                toolchain.clippy_driver,
31                toolchain.rustc,
32            ],
33            transitive_files = toolchain.rustc_lib,
34        )
35    elif ctx.attr.tool == "rustc":
36        files = depset([toolchain.rustc])
37        runfiles = ctx.runfiles(
38            files = [toolchain.rustc],
39            transitive_files = toolchain.rustc_lib,
40        )
41    elif ctx.attr.tool == "rustdoc":
42        files = depset([toolchain.rust_doc])
43        runfiles = ctx.runfiles(
44            files = [toolchain.rust_doc],
45            transitive_files = toolchain.rustc_lib,
46        )
47    elif ctx.attr.tool == "rustfmt":
48        files = depset([toolchain.rustfmt])
49        runfiles = ctx.runfiles(
50            files = [toolchain.rustfmt],
51            transitive_files = toolchain.rustc_lib,
52        )
53    elif ctx.attr.tool == "rustc_lib":
54        files = toolchain.rustc_lib
55    elif ctx.attr.tool == "rust_std" or ctx.attr.tool == "rust_stdlib" or ctx.attr.tool == "rust_lib":
56        files = toolchain.rust_std
57    else:
58        fail("Unsupported tool: ", ctx.attr.tool)
59
60    return [DefaultInfo(
61        files = files,
62        runfiles = runfiles,
63    )]
64
65toolchain_files = rule(
66    doc = "A rule for fetching files from a rust toolchain for the exec platform.",
67    implementation = _toolchain_files_impl,
68    attrs = {
69        "tool": attr.string(
70            doc = "The desired tool to get form the current rust_toolchain",
71            values = [
72                "cargo",
73                "cargo-clippy",
74                "clippy",
75                "rust_lib",
76                "rust_std",
77                "rust_stdlib",
78                "rustc_lib",
79                "rustc",
80                "rustdoc",
81                "rustfmt",
82            ],
83            mandatory = True,
84        ),
85    },
86    toolchains = [
87        str(Label("//rust:toolchain_type")),
88    ],
89)
90
91def _current_rust_toolchain_impl(ctx):
92    toolchain = ctx.toolchains[str(Label("@rules_rust//rust:toolchain_type"))]
93
94    return [
95        toolchain,
96        toolchain.make_variables,
97        DefaultInfo(
98            files = toolchain.all_files,
99        ),
100    ]
101
102current_rust_toolchain = rule(
103    doc = "A rule for exposing the current registered `rust_toolchain`.",
104    implementation = _current_rust_toolchain_impl,
105    toolchains = [
106        str(Label("@rules_rust//rust:toolchain_type")),
107    ],
108)
109
110def _transition_to_target_impl(settings, _attr):
111    return {
112        # String conversion is needed to prevent a crash with Bazel 6.x.
113        "//command_line_option:extra_execution_platforms": [
114            str(platform)
115            for platform in settings["//command_line_option:platforms"]
116        ],
117    }
118
119_transition_to_target = transition(
120    implementation = _transition_to_target_impl,
121    inputs = ["//command_line_option:platforms"],
122    outputs = ["//command_line_option:extra_execution_platforms"],
123)
124
125def _toolchain_files_for_target_impl(ctx):
126    return [ctx.attr.toolchain_files[0][DefaultInfo]]
127
128toolchain_files_for_target = rule(
129    doc = "A rule for fetching files from a rust toolchain for the target platform.",
130    implementation = _toolchain_files_for_target_impl,
131    attrs = {
132        "toolchain_files": attr.label(cfg = _transition_to_target, mandatory = True),
133        "_allowlist_function_transition": attr.label(
134            default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
135        ),
136    },
137)
138