fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
5079b64d
rust-analyzer
1"""A module defining rustfmt rules""" 2 3load ( ":common.bzl" , "rust_common" ) 4 5def _get_rustfmt_ready_crate_info ( target ): 6"""Check that a target is suitable for rustfmt and extract the `CrateInfo` provider from it. 7 8Args: 9target (Target): The target the aspect is running on. 10 11Returns: 12CrateInfo, optional: A `CrateInfo` provider if clippy should be run or `None`. 13""" 14 15# Ignore external targets 16if target . label . workspace_root . startswith ( "external" ): 17return None 18 19# Obviously ignore any targets that don't contain `CrateInfo` 20if rust_common . crate_info in target : 21return target [ rust_common . crate_info ] 22elif rust_common . test_crate_info in target : 23return target [ rust_common . test_crate_info ]. crate 24else : 25return None 26 27def _find_rustfmtable_srcs ( crate_info , aspect_ctx = None ): 28"""Parse a `CrateInfo` provider for rustfmt formattable sources. 29 30Args: 31crate_info (CrateInfo): A `CrateInfo` provider. 32aspect_ctx (ctx, optional): The aspect's context object. 33 34Returns: 35list: A list of formattable sources (`File`). 36""" 37 38# Targets with specific tags will not be formatted 39if aspect_ctx : 40ignore_tags = [ 41"no-format" , 42"no-rustfmt" , 43"norustfmt" , 44] 45 46for tag in ignore_tags : 47if tag in aspect_ctx . rule . attr . tags : 48return [] 49 50# Filter out any generated files 51srcs = [ src for src in crate_info . srcs . to_list () if src . is_source ] 52 53return srcs 54 55def _generate_manifest ( edition , srcs , ctx ): 56# Gather the source paths to non-generated files 57src_paths = [ src . path for src in srcs ] 58 59# Write the rustfmt manifest 60manifest = ctx . actions . declare_file ( ctx . label . name + ".rustfmt" ) 61ctx . actions . write ( 62output = manifest , 63content = "\n ". join ( src_paths + [ 64edition , 65]), 66) 67 68return manifest 69 70def _perform_check ( edition , srcs , ctx ): 71rustfmt_toolchain = ctx . toolchains [ Label ( "//rust/rustfmt:toolchain_type" )] 72 73config = ctx . file . _config 74marker = ctx . actions . declare_file ( ctx . label . name + ".rustfmt.ok" ) 75 76args = ctx . actions . args () 77args . add ( "--touch-file" ) 78args . add ( marker ) 79args . add ( "--" ) 80args . add ( rustfmt_toolchain . rustfmt ) 81args . add ( "--config-path" ) 82args . add ( config ) 83args . add ( "--edition" ) 84args . add ( edition ) 85args . add ( "--check" ) 86args . add_all ( srcs ) 87 88ctx . actions . run ( 89executable = ctx . executable . _process_wrapper , 90inputs = srcs + [ config ], 91outputs = [ marker ], 92tools = [ rustfmt_toolchain . all_files ], 93arguments = [ args ], 94mnemonic = "Rustfmt" , 95) 96 97return marker 98 99def _rustfmt_aspect_impl ( target , ctx ): 100crate_info = _get_rustfmt_ready_crate_info ( target ) 101 102if not crate_info : 103return [] 104 105srcs = _find_rustfmtable_srcs ( crate_info , ctx ) 106 107# If there are no formattable sources, do nothing. 108if not srcs : 109return [] 110 111edition = crate_info . edition 112 113marker = _perform_check ( edition , srcs , ctx ) 114 115return [ 116OutputGroupInfo ( 117rustfmt_checks = depset ([ marker ]), 118), 119] 120 121rustfmt_aspect = aspect ( 122implementation = _rustfmt_aspect_impl , 123doc = """\ 124This aspect is used to gather information about a crate for use in rustfmt and perform rustfmt checks 125 126Output Groups: 127 128- `rustfmt_checks`: Executes `rustfmt --check` on the specified target. 129 130The build setting `@rules_rust//:rustfmt.toml` is used to control the Rustfmt [configuration settings][cs] 131used at runtime. 132 133[cs]: https://rust-lang.github.io/rustfmt/ 134 135This aspect is executed on any target which provides the `CrateInfo` provider. However 136users may tag a target with `no-rustfmt` or `no-format` to have it skipped. Additionally, 137generated source files are also ignored by this aspect. 138""" , 139attrs = { 140"_config" : attr . label ( 141doc = "The `rustfmt.toml` file used for formatting" , 142allow_single_file = True , 143default = Label ( "//:rustfmt.toml" ), 144), 145"_process_wrapper" : attr . label ( 146doc = "A process wrapper for running rustfmt on all platforms" , 147cfg = "exec" , 148executable = True , 149default = Label ( "//util/process_wrapper" ), 150), 151}, 152incompatible_use_toolchain_transition = True , 153required_providers = [ 154[ rust_common . crate_info ], 155[ rust_common . test_crate_info ], 156], 157fragments = [ "cpp" ], 158host_fragments = [ "cpp" ], 159toolchains = [ 160str ( Label ( "//rust/rustfmt:toolchain_type" )), 161], 162) 163 164def _rustfmt_test_manifest_aspect_impl ( target , ctx ): 165crate_info = _get_rustfmt_ready_crate_info ( target ) 166 167if not crate_info : 168return [] 169 170# Parse the edition to use for formatting from the target 171edition = crate_info . edition 172 173srcs = _find_rustfmtable_srcs ( crate_info , ctx ) 174manifest = _generate_manifest ( edition , srcs , ctx ) 175 176return [ 177OutputGroupInfo ( 178rustfmt_manifest = depset ([ manifest ]), 179), 180] 181 182# This aspect contains functionality split out of `rustfmt_aspect` which broke when 183# `required_providers` was added to it. Aspects which have `required_providers` seems 184# to not function with attributes that also require providers. 185_rustfmt_test_manifest_aspect = aspect ( 186implementation = _rustfmt_test_manifest_aspect_impl , 187doc = """\ 188This aspect is used to gather information about a crate for use in `rustfmt_test` 189 190Output Groups: 191 192- `rustfmt_manifest`: A manifest used by rustfmt binaries to provide crate specific settings. 193""" , 194incompatible_use_toolchain_transition = True , 195fragments = [ "cpp" ], 196host_fragments = [ "cpp" ], 197toolchains = [ 198str ( Label ( "//rust/rustfmt:toolchain_type" )), 199], 200) 201 202def _rustfmt_test_impl ( ctx ): 203# The executable of a test target must be the output of an action in 204# the rule implementation. This file is simply a symlink to the real 205# rustfmt test runner. 206is_windows = ctx . executable . _runner . extension == ".exe" 207runner = ctx . actions . declare_file ( "{}{}" . format ( 208ctx . label . name , 209".exe" if is_windows else "" , 210)) 211 212ctx . actions . symlink ( 213output = runner , 214target_file = ctx . executable . _runner , 215is_executable = True , 216) 217 218crate_infos = [ _get_rustfmt_ready_crate_info ( target ) for target in ctx . attr . targets ] 219srcs = [ depset ( _find_rustfmtable_srcs ( crate_info )) for crate_info in crate_infos if crate_info ] 220 221# Some targets may be included in tests but tagged as "no-format". In this 222# case, there will be no manifest. 223manifests = [ getattr ( target [ OutputGroupInfo ], "rustfmt_manifest" , None ) for target in ctx . attr . targets ] 224manifests = depset ( transitive = [ manifest for manifest in manifests if manifest ]) 225 226runfiles = ctx . runfiles ( 227transitive_files = depset ( transitive = srcs + [ manifests ]), 228) 229 230runfiles = runfiles . merge ( 231ctx . attr . _runner [ DefaultInfo ]. default_runfiles , 232) 233 234path_env_sep = ";" if is_windows else ":" 235 236return [ 237DefaultInfo ( 238files = depset ([ runner ]), 239runfiles = runfiles , 240executable = runner , 241), 242testing . TestEnvironment ({ 243"RUSTFMT_MANIFESTS" : path_env_sep . join ([ 244manifest . short_path 245for manifest in sorted ( manifests . to_list ()) 246]), 247"RUST_BACKTRACE" : "1" , 248}), 249] 250 251rustfmt_test = rule ( 252implementation = _rustfmt_test_impl , 253doc = "A test rule for performing `rustfmt --check` on a set of targets" , 254attrs = { 255"targets" : attr . label_list ( 256doc = "Rust targets to run `rustfmt --check` on." , 257providers = [ 258[ rust_common . crate_info ], 259[ rust_common . test_crate_info ], 260], 261aspects = [ _rustfmt_test_manifest_aspect ], 262), 263"_runner" : attr . label ( 264doc = "The rustfmt test runner" , 265cfg = "exec" , 266executable = True , 267default = Label ( "//tools/rustfmt:rustfmt_test" ), 268), 269}, 270test = True , 271) 272 273def _rustfmt_toolchain_impl ( ctx ): 274make_variables = { 275"RUSTFMT" : ctx . file . rustfmt . path , 276} 277 278if ctx . attr . rustc : 279make_variables . update ({ 280"RUSTC" : ctx . file . rustc . path , 281}) 282 283make_variable_info = platform_common . TemplateVariableInfo ( make_variables ) 284 285all_files = [ ctx . file . rustfmt ] + ctx . files . rustc_lib 286if ctx . file . rustc : 287all_files . append ( ctx . file . rustc ) 288 289toolchain = platform_common . ToolchainInfo ( 290rustfmt = ctx . file . rustfmt , 291rustc = ctx . file . rustc , 292rustc_lib = depset ( ctx . files . rustc_lib ), 293all_files = depset ( all_files ), 294make_variables = make_variable_info , 295) 296 297return [ 298toolchain , 299make_variable_info , 300] 301 302rustfmt_toolchain = rule ( 303doc = "A toolchain for [rustfmt](https://rust-lang.github.io/rustfmt/)" , 304implementation = _rustfmt_toolchain_impl , 305incompatible_use_toolchain_transition = True , 306attrs = { 307"rustc" : attr . label ( 308doc = "The location of the `rustc` binary. Can be a direct source or a filegroup containing one item." , 309allow_single_file = True , 310cfg = "exec" , 311), 312"rustc_lib" : attr . label ( 313doc = "The libraries used by rustc during compilation." , 314cfg = "exec" , 315), 316"rustfmt" : attr . label ( 317doc = "The location of the `rustfmt` binary. Can be a direct source or a filegroup containing one item." , 318allow_single_file = True , 319cfg = "exec" , 320mandatory = True , 321), 322}, 323toolchains = [ 324str ( Label ( "@rules_rust//rust:toolchain_type" )), 325], 326) 327 328def _current_rustfmt_toolchain_impl ( ctx ): 329toolchain = ctx . toolchains [ str ( Label ( "@rules_rust//rust/rustfmt:toolchain_type" ))] 330 331return [ 332toolchain , 333toolchain . make_variables , 334DefaultInfo ( 335files = depset ([ 336toolchain . rustfmt , 337]), 338runfiles = ctx . runfiles ( transitive_files = toolchain . all_files ), 339), 340] 341 342current_rustfmt_toolchain = rule ( 343doc = "A rule for exposing the current registered `rustfmt_toolchain`." , 344implementation = _current_rustfmt_toolchain_impl , 345toolchains = [ 346str ( Label ( "@rules_rust//rust/rustfmt:toolchain_type" )), 347], 348incompatible_use_toolchain_transition = True , 349)