fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
0e1297d3
main
1"""A module defining Rust 'unpretty' rules""" 2 3load ( "//rust/private:common.bzl" , "rust_common" ) 4load ( 5"//rust/private:rust.bzl" , 6"RUSTC_ATTRS" , 7"get_rust_test_flags" , 8) 9load ( 10"//rust/private:rustc.bzl" , 11"collect_deps" , 12"collect_inputs" , 13"construct_arguments" , 14) 15load ( 16"//rust/private:utils.bzl" , 17"determine_output_hash" , 18"find_cc_toolchain" , 19"find_toolchain" , 20) 21 22# This list is determined by running the following command: 23# 24# rustc +nightly -Zunpretty= 25# 26_UNPRETTY_MODES = [ 27"ast-tree,expanded" , 28"ast-tree" , 29"expanded,hygiene" , 30"expanded,identified" , 31"expanded" , 32"hir-tree" , 33"hir,identified" , 34"hir,typed" , 35"hir" , 36"identified" , 37"mir-cfg" , 38"mir" , 39"normal" , 40] 41 42RustUnprettyInfo = provider ( 43doc = "A provider describing the Rust unpretty mode." , 44fields = { 45"modes" : "Depset[string]: Can be any of {}" . format ([ "'{}'" . format ( m ) for m in _UNPRETTY_MODES ]), 46}, 47) 48 49def _rust_unpretty_flag_impl ( ctx ): 50value = ctx . build_setting_value 51invalid = [] 52for mode in value : 53if mode not in _UNPRETTY_MODES : 54invalid . append ( mode ) 55if invalid : 56fail ( "{} build setting allowed to take values [{}] but was set to unallowed values: {}" . format ( 57ctx . label , 58", " . join ([ "'{}'" . format ( m ) for m in _UNPRETTY_MODES ]), 59invalid , 60)) 61 62return RustUnprettyInfo ( modes = depset ( value )) 63 64rust_unpretty_flag = rule ( 65doc = "A build setting which represents the Rust unpretty mode. The allowed values are {}" . format ( _UNPRETTY_MODES ), 66implementation = _rust_unpretty_flag_impl , 67build_setting = config . string_list ( 68flag = True , 69repeatable = True , 70), 71) 72 73def _nightly_unpretty_transition_impl ( settings , attr ): 74mode = settings [ str ( Label ( "//rust/settings:unpretty" ))] 75 76# Use the presence of _unpretty_modes as a proxy for whether this is a rust_unpretty target. 77if hasattr ( attr , "_unpretty_modes" ) and hasattr ( attr , "mode" ): 78mode = mode + [ attr . mode ] 79 80return { 81str ( Label ( "//rust/settings:unpretty" )): depset ( mode ). to_list (), 82str ( Label ( "//rust/toolchain/channel" )): "nightly" , 83} 84 85nightly_unpretty_transition = transition ( 86implementation = _nightly_unpretty_transition_impl , 87inputs = [ str ( Label ( "//rust/settings:unpretty" ))], 88outputs = [ 89str ( Label ( "//rust/settings:unpretty" )), 90str ( Label ( "//rust/toolchain/channel" )), 91], 92) 93 94def _get_unpretty_ready_crate_info ( target , aspect_ctx ): 95"""Check that a target is suitable for expansion and extract the `CrateInfo` provider from it. 96 97Args: 98target (Target): The target the aspect is running on. 99aspect_ctx (ctx, optional): The aspect's context object. 100 101Returns: 102CrateInfo, optional: A `CrateInfo` provider if rust unpretty should be run or `None`. 103""" 104 105# Ignore external targets 106if target . label . workspace_root . startswith ( "external" ): 107return None 108 109# Targets with specific tags will not be formatted 110if aspect_ctx : 111ignore_tags = [ 112"nounpretty" , 113"no-unpretty" , 114"no_unpretty" , 115] 116 117for tag in ignore_tags : 118if tag in aspect_ctx . rule . attr . tags : 119return None 120 121# Obviously ignore any targets that don't contain `CrateInfo` 122if rust_common . crate_info not in target : 123return None 124 125return target [ rust_common . crate_info ] 126 127def _rust_unpretty_aspect_impl ( target , ctx ): 128crate_info = _get_unpretty_ready_crate_info ( target , ctx ) 129if not crate_info : 130return [] 131 132toolchain = find_toolchain ( ctx ) 133cc_toolchain , feature_configuration = find_cc_toolchain ( ctx ) 134 135dep_info , build_info , _ = collect_deps ( 136deps = crate_info . deps , 137proc_macro_deps = crate_info . proc_macro_deps , 138aliases = crate_info . aliases , 139) 140 141compile_inputs , out_dir , build_env_files , build_flags_files , linkstamp_outs , ambiguous_libs = collect_inputs ( 142ctx , 143ctx . rule . file , 144ctx . rule . files , 145# Rust expand doesn't need to invoke transitive linking, therefore doesn't need linkstamps. 146depset ([]), 147toolchain , 148cc_toolchain , 149feature_configuration , 150crate_info , 151dep_info , 152build_info , 153) 154 155output_groups = {} 156outputs = [] 157 158for mode in ctx . attr . _unpretty_modes [ RustUnprettyInfo ]. modes . to_list (): 159pretty_mode = mode . replace ( "-" , "_" ) 160mnemonic = "RustUnpretty{}" . format ( "" . join ([ 161o . title () 162for m in pretty_mode . split ( "," ) 163for o in m . split ( "_" ) 164])) 165 166unpretty_out = ctx . actions . declare_file ( 167"{}.unpretty.{}.rs" . format ( ctx . label . name , pretty_mode . replace ( "," , "." )), 168sibling = crate_info . output , 169) 170 171output_groups . update ({ "rust_unpretty_{}" . format ( pretty_mode . replace ( "," , "_" )): depset ([ unpretty_out ])}) 172outputs . append ( unpretty_out ) 173 174rust_flags = [] 175if crate_info . is_test : 176rust_flags = get_rust_test_flags ( ctx . rule . attr ) 177 178args , env = construct_arguments ( 179ctx = ctx , 180attr = ctx . rule . attr , 181file = ctx . file , 182toolchain = toolchain , 183tool_path = toolchain . rustc . path , 184cc_toolchain = cc_toolchain , 185feature_configuration = feature_configuration , 186crate_info = crate_info , 187dep_info = dep_info , 188linkstamp_outs = linkstamp_outs , 189ambiguous_libs = ambiguous_libs , 190output_hash = determine_output_hash ( crate_info . root , ctx . label ), 191rust_flags = rust_flags , 192out_dir = out_dir , 193build_env_files = build_env_files , 194build_flags_files = build_flags_files , 195emit = [ "dep-info" , "metadata" ], 196skip_expanding_rustc_env = True , 197) 198 199args . process_wrapper_flags . add ( "--stdout-file" , unpretty_out ) 200 201# Expand all macros and dump the source to stdout. 202# Tracking issue: https://github.com/rust-lang/rust/issues/43364 203args . rustc_flags . add ( "-Zunpretty={}" . format ( mode )) 204 205ctx . actions . run ( 206executable = ctx . executable . _process_wrapper , 207inputs = compile_inputs , 208outputs = [ unpretty_out ], 209env = env , 210arguments = args . all , 211mnemonic = mnemonic , 212toolchain = "@rules_rust//rust:toolchain_type" , 213) 214 215output_groups . update ({ "rust_unpretty" : depset ( outputs )}) 216 217return [ 218OutputGroupInfo ( ** output_groups ), 219] 220 221# Example: Expand all rust targets in the codebase. 222# bazel build --aspects=@rules_rust//rust:defs.bzl%rust_unpretty_aspect \ 223# --output_groups=expanded \ 224# //... 225rust_unpretty_aspect = aspect ( 226implementation = _rust_unpretty_aspect_impl , 227fragments = [ "cpp" ], 228attrs = { 229"_unpretty_modes" : attr . label ( 230doc = "The values to pass to `--unpretty`" , 231providers = [ RustUnprettyInfo ], 232default = Label ( "//rust/settings:unpretty" ), 233), 234} | RUSTC_ATTRS , 235toolchains = [ 236str ( Label ( "//rust:toolchain_type" )), 237"@bazel_tools//tools/cpp:toolchain_type" , 238], 239required_providers = [ rust_common . crate_info ], 240doc = """\ 241Executes Rust expand on specified targets. 242 243This aspect applies to existing rust_library, rust_test, and rust_binary rules. 244 245As an example, if the following is defined in `examples/hello_lib/BUILD.bazel`: 246 247```python 248load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") 249 250rust_library( 251name = "hello_lib", 252srcs = ["src/lib.rs"], 253) 254 255rust_test( 256name = "greeting_test", 257srcs = ["tests/greeting.rs"], 258deps = [":hello_lib"], 259) 260``` 261 262Then the targets can be expanded with the following command: 263 264```output 265$ bazel build --aspects=@rules_rust//rust:defs.bzl%rust_unpretty_aspect \ 266--output_groups=rust_unpretty_expanded //hello_lib:all 267``` 268""" , 269) 270 271def _rust_unpretty_rule_impl ( ctx ): 272mode = ctx . attr . mode 273output_group = "rust_unpretty_{}" . format ( mode . replace ( "," , "_" ). replace ( "-" , "_" )) 274files = [] 275for target in ctx . attr . deps : 276files . append ( getattr ( target [ OutputGroupInfo ], output_group )) 277 278return [ DefaultInfo ( files = depset ( transitive = files ))] 279 280rust_unpretty = rule ( 281implementation = _rust_unpretty_rule_impl , 282cfg = nightly_unpretty_transition , 283attrs = { 284"deps" : attr . label_list ( 285doc = "Rust targets to run unpretty on." , 286providers = [ rust_common . crate_info ], 287aspects = [ rust_unpretty_aspect ], 288), 289"mode" : attr . string ( 290doc = "The value to pass to `--unpretty`" , 291values = _UNPRETTY_MODES , 292default = "expanded" , 293), 294"_allowlist_function_transition" : attr . label ( 295default = Label ( "//tools/allowlists/function_transition_allowlist" ), 296), 297"_unpretty_modes" : attr . label ( 298doc = "The values to pass to `--unpretty`" , 299providers = [ RustUnprettyInfo ], 300default = Label ( "//rust/settings:unpretty" ), 301), 302}, 303doc = """\ 304Executes rust unpretty on a specific target. 305 306Similar to `rust_unpretty_aspect`, but allows specifying a list of dependencies \ 307within the build system. 308 309For example, given the following example targets: 310 311```python 312load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") 313 314rust_library( 315name = "hello_lib", 316srcs = ["src/lib.rs"], 317) 318 319rust_test( 320name = "greeting_test", 321srcs = ["tests/greeting.rs"], 322deps = [":hello_lib"], 323) 324``` 325 326Rust expand can be set as a build target with the following: 327 328```python 329load("@rules_rust//rust:defs.bzl", "rust_unpretty") 330 331rust_unpretty( 332name = "hello_library_expand", 333testonly = True, 334deps = [ 335":hello_lib", 336":greeting_test", 337], 338mode = "expand", 339) 340``` 341""" , 342)