1*d4726bddSHONG Yifan"""A module defining toolchain utilities""" 2*d4726bddSHONG Yifan 3*d4726bddSHONG Yifandef _toolchain_files_impl(ctx): 4*d4726bddSHONG Yifan toolchain = ctx.toolchains[str(Label("//rust:toolchain_type"))] 5*d4726bddSHONG Yifan 6*d4726bddSHONG Yifan runfiles = None 7*d4726bddSHONG Yifan if ctx.attr.tool == "cargo": 8*d4726bddSHONG Yifan files = depset([toolchain.cargo]) 9*d4726bddSHONG Yifan runfiles = ctx.runfiles( 10*d4726bddSHONG Yifan files = [ 11*d4726bddSHONG Yifan toolchain.cargo, 12*d4726bddSHONG Yifan toolchain.rustc, 13*d4726bddSHONG Yifan ], 14*d4726bddSHONG Yifan transitive_files = toolchain.rustc_lib, 15*d4726bddSHONG Yifan ) 16*d4726bddSHONG Yifan elif ctx.attr.tool == "cargo-clippy": 17*d4726bddSHONG Yifan files = depset([toolchain.cargo_clippy]) 18*d4726bddSHONG Yifan runfiles = ctx.runfiles( 19*d4726bddSHONG Yifan files = [ 20*d4726bddSHONG Yifan toolchain.cargo_clippy, 21*d4726bddSHONG Yifan toolchain.clippy_driver, 22*d4726bddSHONG Yifan toolchain.rustc, 23*d4726bddSHONG Yifan ], 24*d4726bddSHONG Yifan transitive_files = toolchain.rustc_lib, 25*d4726bddSHONG Yifan ) 26*d4726bddSHONG Yifan elif ctx.attr.tool == "clippy": 27*d4726bddSHONG Yifan files = depset([toolchain.clippy_driver]) 28*d4726bddSHONG Yifan runfiles = ctx.runfiles( 29*d4726bddSHONG Yifan files = [ 30*d4726bddSHONG Yifan toolchain.clippy_driver, 31*d4726bddSHONG Yifan toolchain.rustc, 32*d4726bddSHONG Yifan ], 33*d4726bddSHONG Yifan transitive_files = toolchain.rustc_lib, 34*d4726bddSHONG Yifan ) 35*d4726bddSHONG Yifan elif ctx.attr.tool == "rustc": 36*d4726bddSHONG Yifan files = depset([toolchain.rustc]) 37*d4726bddSHONG Yifan runfiles = ctx.runfiles( 38*d4726bddSHONG Yifan files = [toolchain.rustc], 39*d4726bddSHONG Yifan transitive_files = toolchain.rustc_lib, 40*d4726bddSHONG Yifan ) 41*d4726bddSHONG Yifan elif ctx.attr.tool == "rustdoc": 42*d4726bddSHONG Yifan files = depset([toolchain.rust_doc]) 43*d4726bddSHONG Yifan runfiles = ctx.runfiles( 44*d4726bddSHONG Yifan files = [toolchain.rust_doc], 45*d4726bddSHONG Yifan transitive_files = toolchain.rustc_lib, 46*d4726bddSHONG Yifan ) 47*d4726bddSHONG Yifan elif ctx.attr.tool == "rustfmt": 48*d4726bddSHONG Yifan files = depset([toolchain.rustfmt]) 49*d4726bddSHONG Yifan runfiles = ctx.runfiles( 50*d4726bddSHONG Yifan files = [toolchain.rustfmt], 51*d4726bddSHONG Yifan transitive_files = toolchain.rustc_lib, 52*d4726bddSHONG Yifan ) 53*d4726bddSHONG Yifan elif ctx.attr.tool == "rustc_lib": 54*d4726bddSHONG Yifan files = toolchain.rustc_lib 55*d4726bddSHONG Yifan elif ctx.attr.tool == "rust_std" or ctx.attr.tool == "rust_stdlib" or ctx.attr.tool == "rust_lib": 56*d4726bddSHONG Yifan files = toolchain.rust_std 57*d4726bddSHONG Yifan else: 58*d4726bddSHONG Yifan fail("Unsupported tool: ", ctx.attr.tool) 59*d4726bddSHONG Yifan 60*d4726bddSHONG Yifan return [DefaultInfo( 61*d4726bddSHONG Yifan files = files, 62*d4726bddSHONG Yifan runfiles = runfiles, 63*d4726bddSHONG Yifan )] 64*d4726bddSHONG Yifan 65*d4726bddSHONG Yifantoolchain_files = rule( 66*d4726bddSHONG Yifan doc = "A rule for fetching files from a rust toolchain for the exec platform.", 67*d4726bddSHONG Yifan implementation = _toolchain_files_impl, 68*d4726bddSHONG Yifan attrs = { 69*d4726bddSHONG Yifan "tool": attr.string( 70*d4726bddSHONG Yifan doc = "The desired tool to get form the current rust_toolchain", 71*d4726bddSHONG Yifan values = [ 72*d4726bddSHONG Yifan "cargo", 73*d4726bddSHONG Yifan "cargo-clippy", 74*d4726bddSHONG Yifan "clippy", 75*d4726bddSHONG Yifan "rust_lib", 76*d4726bddSHONG Yifan "rust_std", 77*d4726bddSHONG Yifan "rust_stdlib", 78*d4726bddSHONG Yifan "rustc_lib", 79*d4726bddSHONG Yifan "rustc", 80*d4726bddSHONG Yifan "rustdoc", 81*d4726bddSHONG Yifan "rustfmt", 82*d4726bddSHONG Yifan ], 83*d4726bddSHONG Yifan mandatory = True, 84*d4726bddSHONG Yifan ), 85*d4726bddSHONG Yifan }, 86*d4726bddSHONG Yifan toolchains = [ 87*d4726bddSHONG Yifan str(Label("//rust:toolchain_type")), 88*d4726bddSHONG Yifan ], 89*d4726bddSHONG Yifan) 90*d4726bddSHONG Yifan 91*d4726bddSHONG Yifandef _current_rust_toolchain_impl(ctx): 92*d4726bddSHONG Yifan toolchain = ctx.toolchains[str(Label("@rules_rust//rust:toolchain_type"))] 93*d4726bddSHONG Yifan 94*d4726bddSHONG Yifan return [ 95*d4726bddSHONG Yifan toolchain, 96*d4726bddSHONG Yifan toolchain.make_variables, 97*d4726bddSHONG Yifan DefaultInfo( 98*d4726bddSHONG Yifan files = toolchain.all_files, 99*d4726bddSHONG Yifan ), 100*d4726bddSHONG Yifan ] 101*d4726bddSHONG Yifan 102*d4726bddSHONG Yifancurrent_rust_toolchain = rule( 103*d4726bddSHONG Yifan doc = "A rule for exposing the current registered `rust_toolchain`.", 104*d4726bddSHONG Yifan implementation = _current_rust_toolchain_impl, 105*d4726bddSHONG Yifan toolchains = [ 106*d4726bddSHONG Yifan str(Label("@rules_rust//rust:toolchain_type")), 107*d4726bddSHONG Yifan ], 108*d4726bddSHONG Yifan) 109*d4726bddSHONG Yifan 110*d4726bddSHONG Yifandef _transition_to_target_impl(settings, _attr): 111*d4726bddSHONG Yifan return { 112*d4726bddSHONG Yifan # String conversion is needed to prevent a crash with Bazel 6.x. 113*d4726bddSHONG Yifan "//command_line_option:extra_execution_platforms": [ 114*d4726bddSHONG Yifan str(platform) 115*d4726bddSHONG Yifan for platform in settings["//command_line_option:platforms"] 116*d4726bddSHONG Yifan ], 117*d4726bddSHONG Yifan } 118*d4726bddSHONG Yifan 119*d4726bddSHONG Yifan_transition_to_target = transition( 120*d4726bddSHONG Yifan implementation = _transition_to_target_impl, 121*d4726bddSHONG Yifan inputs = ["//command_line_option:platforms"], 122*d4726bddSHONG Yifan outputs = ["//command_line_option:extra_execution_platforms"], 123*d4726bddSHONG Yifan) 124*d4726bddSHONG Yifan 125*d4726bddSHONG Yifandef _toolchain_files_for_target_impl(ctx): 126*d4726bddSHONG Yifan return [ctx.attr.toolchain_files[0][DefaultInfo]] 127*d4726bddSHONG Yifan 128*d4726bddSHONG Yifantoolchain_files_for_target = rule( 129*d4726bddSHONG Yifan doc = "A rule for fetching files from a rust toolchain for the target platform.", 130*d4726bddSHONG Yifan implementation = _toolchain_files_for_target_impl, 131*d4726bddSHONG Yifan attrs = { 132*d4726bddSHONG Yifan "toolchain_files": attr.label(cfg = _transition_to_target, mandatory = True), 133*d4726bddSHONG Yifan "_allowlist_function_transition": attr.label( 134*d4726bddSHONG Yifan default = "@bazel_tools//tools/allowlists/function_transition_allowlist", 135*d4726bddSHONG Yifan ), 136*d4726bddSHONG Yifan }, 137*d4726bddSHONG Yifan) 138