fork/rules_rust

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

UebelAndreAdded TemplateVariableInfo to rust_toolchain (#1377)e765719e

0.7.0
3.2 KiB107 linesraw
1"""A module defining toolchain utilities"""
2
3def _toolchain_files_impl(ctx):
4    toolchain = ctx.toolchains[str(Label("//rust:toolchain"))]
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 == "rustc_srcs":
46        files = toolchain.rustc_srcs.files
47    elif ctx.attr.tool == "rust_std" or ctx.attr.tool == "rust_stdlib" or ctx.attr.tool == "rust_lib":
48        files = toolchain.rust_std
49    else:
50        fail("Unsupported tool: ", ctx.attr.tool)
51
52    return [DefaultInfo(
53        files = files,
54        runfiles = runfiles,
55    )]
56
57toolchain_files = rule(
58    doc = "A rule for fetching files from a rust toolchain.",
59    implementation = _toolchain_files_impl,
60    attrs = {
61        "tool": attr.string(
62            doc = "The desired tool to get form the current rust_toolchain",
63            values = [
64                "cargo",
65                "clippy",
66                "rust_lib",
67                "rust_std",
68                "rust_stdlib",
69                "rustc_lib",
70                "rustc_srcs",
71                "rustc",
72                "rustdoc",
73                "rustfmt",
74            ],
75            mandatory = True,
76        ),
77    },
78    toolchains = [
79        str(Label("//rust:toolchain")),
80    ],
81    incompatible_use_toolchain_transition = True,
82)
83
84def _current_rust_toolchain_impl(ctx):
85    toolchain = ctx.toolchains[str(Label("@rules_rust//rust:toolchain"))]
86
87    return [
88        toolchain,
89        toolchain.make_variables,
90        DefaultInfo(
91            files = depset([
92                toolchain.rustc,
93                toolchain.rust_doc,
94                toolchain.rustfmt,
95                toolchain.cargo,
96            ]),
97        ),
98    ]
99
100current_rust_toolchain = rule(
101    doc = "A rule for exposing the current registered `rust_toolchain`.",
102    implementation = _current_rust_toolchain_impl,
103    toolchains = [
104        str(Label("@rules_rust//rust:toolchain")),
105    ],
106    incompatible_use_toolchain_transition = True,
107)