Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame^] | 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, |
| 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 | |
| 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 | "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 | ) |