• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 == "clippy":
17        files = depset([toolchain.clippy_driver])
18        runfiles = ctx.runfiles(
19            files = [
20                toolchain.clippy_driver,
21                toolchain.rustc,
22            ],
23            transitive_files = toolchain.rustc_lib,
24        )
25    elif ctx.attr.tool == "rustc":
26        files = depset([toolchain.rustc])
27        runfiles = ctx.runfiles(
28            files = [toolchain.rustc],
29            transitive_files = toolchain.rustc_lib,
30        )
31    elif ctx.attr.tool == "rustdoc":
32        files = depset([toolchain.rust_doc])
33        runfiles = ctx.runfiles(
34            files = [toolchain.rust_doc],
35            transitive_files = toolchain.rustc_lib,
36        )
37    elif ctx.attr.tool == "rustfmt":
38        files = depset([toolchain.rustfmt])
39        runfiles = ctx.runfiles(
40            files = [toolchain.rustfmt],
41            transitive_files = toolchain.rustc_lib,
42        )
43    elif ctx.attr.tool == "rustc_lib":
44        files = toolchain.rustc_lib
45    elif ctx.attr.tool == "rust_std" or ctx.attr.tool == "rust_stdlib" or ctx.attr.tool == "rust_lib":
46        files = toolchain.rust_std
47    else:
48        fail("Unsupported tool: ", ctx.attr.tool)
49
50    return [DefaultInfo(
51        files = files,
52        runfiles = runfiles,
53    )]
54
55toolchain_files = rule(
56    doc = "A rule for fetching files from a rust toolchain.",
57    implementation = _toolchain_files_impl,
58    attrs = {
59        "tool": attr.string(
60            doc = "The desired tool to get form the current rust_toolchain",
61            values = [
62                "cargo",
63                "clippy",
64                "rust_lib",
65                "rust_std",
66                "rust_stdlib",
67                "rustc_lib",
68                "rustc",
69                "rustdoc",
70                "rustfmt",
71            ],
72            mandatory = True,
73        ),
74    },
75    toolchains = [
76        str(Label("//rust:toolchain_type")),
77    ],
78)
79
80def _current_rust_toolchain_impl(ctx):
81    toolchain = ctx.toolchains[str(Label("@rules_rust//rust:toolchain_type"))]
82
83    return [
84        toolchain,
85        toolchain.make_variables,
86        DefaultInfo(
87            files = toolchain.all_files,
88        ),
89    ]
90
91current_rust_toolchain = rule(
92    doc = "A rule for exposing the current registered `rust_toolchain`.",
93    implementation = _current_rust_toolchain_impl,
94    toolchains = [
95        str(Label("@rules_rust//rust:toolchain_type")),
96    ],
97)
98