fork/rules_rust

git clone https://git.julia.blue/fork/rules_rust

UebelAndreAdded `rustfmt_toolchain` and refactored toolchain repository rules (#1719)d5289ad1

rust-analyzer
3.1 KiB108 linesraw
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    incompatible_use_toolchain_transition = True,
79)
80
81def _current_rust_toolchain_impl(ctx):
82    toolchain = ctx.toolchains[str(Label("@rules_rust//rust:toolchain_type"))]
83
84    files = [
85        toolchain.rustc,
86        toolchain.rust_doc,
87        toolchain.cargo,
88    ]
89
90    if toolchain.rustfmt:
91        files.append(toolchain.rustfmt)
92
93    return [
94        toolchain,
95        toolchain.make_variables,
96        DefaultInfo(
97            files = depset(files),
98        ),
99    ]
100
101current_rust_toolchain = rule(
102    doc = "A rule for exposing the current registered `rust_toolchain`.",
103    implementation = _current_rust_toolchain_impl,
104    toolchains = [
105        str(Label("@rules_rust//rust:toolchain_type")),
106    ],
107    incompatible_use_toolchain_transition = True,
108)