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): |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame^] | 4 | toolchain = ctx.toolchains[str(Label("//rust:toolchain_type"))] |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 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 |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 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 | |
| 55 | toolchain_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 Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 68 | "rustc", |
| 69 | "rustdoc", |
| 70 | "rustfmt", |
| 71 | ], |
| 72 | mandatory = True, |
| 73 | ), |
| 74 | }, |
| 75 | toolchains = [ |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame^] | 76 | str(Label("//rust:toolchain_type")), |
| 77 | ], |
| 78 | incompatible_use_toolchain_transition = True, |
| 79 | ) |
| 80 | |
| 81 | def _current_rust_toolchain_impl(ctx): |
| 82 | toolchain = ctx.toolchains[str(Label("@rules_rust//rust:toolchain_type"))] |
| 83 | |
| 84 | return [ |
| 85 | toolchain, |
| 86 | toolchain.make_variables, |
| 87 | DefaultInfo( |
| 88 | files = depset([ |
| 89 | toolchain.rustc, |
| 90 | toolchain.rust_doc, |
| 91 | toolchain.rustfmt, |
| 92 | toolchain.cargo, |
| 93 | ]), |
| 94 | ), |
| 95 | ] |
| 96 | |
| 97 | current_rust_toolchain = rule( |
| 98 | doc = "A rule for exposing the current registered `rust_toolchain`.", |
| 99 | implementation = _current_rust_toolchain_impl, |
| 100 | toolchains = [ |
| 101 | str(Label("@rules_rust//rust:toolchain_type")), |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 102 | ], |
| 103 | incompatible_use_toolchain_transition = True, |
| 104 | ) |