|
| 1 | +"""A module defining toolchain utilities""" |
| 2 | + |
| 3 | +def _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.files, |
| 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.files, |
| 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.files, |
| 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.files, |
| 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.files, |
| 42 | + ) |
| 43 | + elif ctx.attr.tool == "rustc_lib": |
| 44 | + files = toolchain.rustc_lib.files |
| 45 | + elif ctx.attr.tool == "rustc_srcs": |
| 46 | + files = toolchain.rustc_srcs.files |
| 47 | + elif ctx.attr.tool == "rust_lib" or ctx.attr.tool == "rust_stdlib": |
| 48 | + files = toolchain.rust_lib.files |
| 49 | + else: |
| 50 | + fail("Unsupported tool: ", ctx.attr.tool) |
| 51 | + |
| 52 | + return [DefaultInfo( |
| 53 | + files = files, |
| 54 | + runfiles = runfiles, |
| 55 | + )] |
| 56 | + |
| 57 | +toolchain_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 | + "rustc", |
| 67 | + "rustdoc", |
| 68 | + "rustfmt", |
| 69 | + "rustc_lib", |
| 70 | + "rustc_srcs", |
| 71 | + "rust_lib", |
| 72 | + "rust_stdlib", |
| 73 | + ], |
| 74 | + mandatory = True, |
| 75 | + ), |
| 76 | + }, |
| 77 | + toolchains = [ |
| 78 | + str(Label("//rust:toolchain")), |
| 79 | + ], |
| 80 | + incompatible_use_toolchain_transition = True, |
| 81 | +) |
0 commit comments