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