fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
8d074a0b
patching
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_name : 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 ): 56workspace = ctx . label . workspace_name or ctx . workspace_name 57 58# Gather the source paths to non-generated files 59content = ctx . actions . args () 60content . set_param_file_format ( "multiline" ) 61content . add_all ( srcs , format_each = workspace + "/%s" ) 62content . add ( edition ) 63 64# Write the rustfmt manifest 65manifest = ctx . actions . declare_file ( ctx . label . name + ".rustfmt" ) 66ctx . actions . write ( 67output = manifest , 68content = content , 69) 70 71return manifest 72 73def _perform_check ( edition , srcs , ctx ): 74rustfmt_toolchain = ctx . toolchains [ Label ( "//rust/rustfmt:toolchain_type" )] 75 76config = ctx . file . _config 77marker = ctx . actions . declare_file ( ctx . label . name + ".rustfmt.ok" ) 78 79args = ctx . actions . args () 80args . add ( "--touch-file" , marker ) 81args . add ( "--" ) 82args . add ( rustfmt_toolchain . rustfmt ) 83args . add ( "--config-path" , config ) 84args . add ( "--edition" , 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}, 152required_providers = [ 153[ rust_common . crate_info ], 154[ rust_common . test_crate_info ], 155], 156fragments = [ "cpp" ], 157toolchains = [ 158str ( Label ( "//rust/rustfmt:toolchain_type" )), 159], 160) 161 162def _rustfmt_test_manifest_aspect_impl ( target , ctx ): 163crate_info = _get_rustfmt_ready_crate_info ( target ) 164 165if not crate_info : 166return [] 167 168# Parse the edition to use for formatting from the target 169edition = crate_info . edition 170 171srcs = _find_rustfmtable_srcs ( crate_info , ctx ) 172manifest = _generate_manifest ( edition , srcs , ctx ) 173 174return [ 175OutputGroupInfo ( 176rustfmt_manifest = depset ([ manifest ]), 177), 178] 179 180# This aspect contains functionality split out of `rustfmt_aspect` which broke when 181# `required_providers` was added to it. Aspects which have `required_providers` seems 182# to not function with attributes that also require providers. 183_rustfmt_test_manifest_aspect = aspect ( 184implementation = _rustfmt_test_manifest_aspect_impl , 185doc = """\ 186This aspect is used to gather information about a crate for use in `rustfmt_test` 187 188Output Groups: 189 190- `rustfmt_manifest`: A manifest used by rustfmt binaries to provide crate specific settings. 191""" , 192fragments = [ "cpp" ], 193toolchains = [ 194str ( Label ( "//rust/rustfmt:toolchain_type" )), 195], 196) 197 198def _rustfmt_test_impl ( ctx ): 199# The executable of a test target must be the output of an action in 200# the rule implementation. This file is simply a symlink to the real 201# rustfmt test runner. 202is_windows = ctx . executable . _runner . extension == ".exe" 203runner = ctx . actions . declare_file ( "{}{}" . format ( 204ctx . label . name , 205".exe" if is_windows else "" , 206)) 207 208ctx . actions . symlink ( 209output = runner , 210target_file = ctx . executable . _runner , 211is_executable = True , 212) 213 214crate_infos = [ _get_rustfmt_ready_crate_info ( target ) for target in ctx . attr . targets ] 215srcs = [ depset ( _find_rustfmtable_srcs ( crate_info )) for crate_info in crate_infos if crate_info ] 216 217# Some targets may be included in tests but tagged as "no-format". In this 218# case, there will be no manifest. 219manifests = [ getattr ( target [ OutputGroupInfo ], "rustfmt_manifest" , None ) for target in ctx . attr . targets ] 220manifests = depset ( transitive = [ manifest for manifest in manifests if manifest ]) 221 222runfiles = ctx . runfiles ( 223transitive_files = depset ( transitive = srcs + [ manifests ]), 224) 225 226runfiles = runfiles . merge ( 227ctx . attr . _runner [ DefaultInfo ]. default_runfiles , 228) 229 230workspace = ctx . label . workspace_name or ctx . workspace_name 231 232return [ 233DefaultInfo ( 234files = depset ([ runner ]), 235runfiles = runfiles , 236executable = runner , 237), 238testing . TestEnvironment ({ 239"RUSTFMT_MANIFESTS" : ctx . configuration . host_path_separator . join ([ 240workspace + "/" + manifest . short_path 241for manifest in sorted ( manifests . to_list ()) 242]), 243"RUST_BACKTRACE" : "1" , 244}), 245] 246 247rustfmt_test = rule ( 248implementation = _rustfmt_test_impl , 249doc = "A test rule for performing `rustfmt --check` on a set of targets" , 250attrs = { 251"targets" : attr . label_list ( 252doc = "Rust targets to run `rustfmt --check` on." , 253providers = [ 254[ rust_common . crate_info ], 255[ rust_common . test_crate_info ], 256], 257aspects = [ _rustfmt_test_manifest_aspect ], 258), 259"_runner" : attr . label ( 260doc = "The rustfmt test runner" , 261cfg = "exec" , 262executable = True , 263default = Label ( "//tools/rustfmt:rustfmt_test" ), 264), 265}, 266test = True , 267) 268 269def _rustfmt_toolchain_impl ( ctx ): 270make_variables = { 271"RUSTFMT" : ctx . file . rustfmt . path , 272} 273 274if ctx . attr . rustc : 275make_variables . update ({ 276"RUSTC" : ctx . file . rustc . path , 277}) 278 279make_variable_info = platform_common . TemplateVariableInfo ( make_variables ) 280 281all_files = [ ctx . file . rustfmt ] + ctx . files . rustc_lib 282if ctx . file . rustc : 283all_files . append ( ctx . file . rustc ) 284 285toolchain = platform_common . ToolchainInfo ( 286rustfmt = ctx . file . rustfmt , 287rustc = ctx . file . rustc , 288rustc_lib = depset ( ctx . files . rustc_lib ), 289all_files = depset ( all_files ), 290make_variables = make_variable_info , 291) 292 293return [ 294toolchain , 295make_variable_info , 296] 297 298rustfmt_toolchain = rule ( 299doc = "A toolchain for [rustfmt](https://rust-lang.github.io/rustfmt/)" , 300implementation = _rustfmt_toolchain_impl , 301attrs = { 302"rustc" : attr . label ( 303doc = "The location of the `rustc` binary. Can be a direct source or a filegroup containing one item." , 304allow_single_file = True , 305cfg = "exec" , 306), 307"rustc_lib" : attr . label ( 308doc = "The libraries used by rustc during compilation." , 309cfg = "exec" , 310), 311"rustfmt" : attr . label ( 312doc = "The location of the `rustfmt` binary. Can be a direct source or a filegroup containing one item." , 313allow_single_file = True , 314cfg = "exec" , 315mandatory = True , 316), 317}, 318toolchains = [ 319str ( Label ( "@rules_rust//rust:toolchain_type" )), 320], 321) 322 323def _current_rustfmt_toolchain_impl ( ctx ): 324toolchain = ctx . toolchains [ str ( Label ( "@rules_rust//rust/rustfmt:toolchain_type" ))] 325 326return [ 327toolchain , 328toolchain . make_variables , 329DefaultInfo ( 330files = depset ([ 331toolchain . rustfmt , 332]), 333runfiles = ctx . runfiles ( transitive_files = toolchain . all_files ), 334), 335] 336 337current_rustfmt_toolchain = rule ( 338doc = "A rule for exposing the current registered `rustfmt_toolchain`." , 339implementation = _current_rustfmt_toolchain_impl , 340toolchains = [ 341str ( Label ( "@rules_rust//rust/rustfmt:toolchain_type" )), 342], 343)