blob: c89a447e11dcd0f7b35e4d1c028ad244ebaf27ce [file] [log] [blame]
Brian Silvermancc09f182022-03-09 15:40:20 -08001"""A module defining toolchain utilities"""
2
3def _toolchain_files_impl(ctx):
Brian Silverman5f6f2762022-08-13 19:30:05 -07004 toolchain = ctx.toolchains[str(Label("//rust:toolchain_type"))]
Brian Silvermancc09f182022-03-09 15:40:20 -08005
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
Brian Silvermancc09f182022-03-09 15:40:20 -080045 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",
Brian Silvermancc09f182022-03-09 15:40:20 -080068 "rustc",
69 "rustdoc",
70 "rustfmt",
71 ],
72 mandatory = True,
73 ),
74 },
75 toolchains = [
Brian Silverman5f6f2762022-08-13 19:30:05 -070076 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
Adam Snaider1c095c92023-07-08 02:09:58 -040084 files = [
85 toolchain.rustc,
86 toolchain.rust_doc,
87 toolchain.cargo,
88 ]
89
90 if toolchain.rustfmt:
91 files.append(toolchain.rustfmt)
92
Brian Silverman5f6f2762022-08-13 19:30:05 -070093 return [
94 toolchain,
95 toolchain.make_variables,
96 DefaultInfo(
Adam Snaider1c095c92023-07-08 02:09:58 -040097 files = depset(files),
Brian Silverman5f6f2762022-08-13 19:30:05 -070098 ),
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")),
Brian Silvermancc09f182022-03-09 15:40:20 -0800106 ],
107 incompatible_use_toolchain_transition = True,
108)