fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
c3d71edd
main
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 aspect_ctx . rule . attr . tags : 47if tag . replace ( "-" , "_" ). lower () in ignore_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" , 95progress_message = "Rustfmt %{label}" , 96) 97 98return marker 99 100def _rustfmt_aspect_impl ( target , ctx ): 101crate_info = _get_rustfmt_ready_crate_info ( target ) 102 103if not crate_info : 104return [] 105 106srcs = _find_rustfmtable_srcs ( crate_info , ctx ) 107 108# If there are no formattable sources, do nothing. 109if not srcs : 110return [] 111 112edition = crate_info . edition 113 114marker = _perform_check ( edition , srcs , ctx ) 115 116return [ 117OutputGroupInfo ( 118rustfmt_checks = depset ([ marker ]), 119), 120] 121 122rustfmt_aspect = aspect ( 123implementation = _rustfmt_aspect_impl , 124doc = """\ 125This aspect is used to gather information about a crate for use in rustfmt and perform rustfmt checks 126 127Output Groups: 128 129- `rustfmt_checks`: Executes `rustfmt --check` on the specified target. 130 131The build setting `@rules_rust//rust/settings:rustfmt.toml` is used to control the Rustfmt [configuration settings][cs] 132used at runtime. 133 134[cs]: https://rust-lang.github.io/rustfmt/ 135 136This aspect is executed on any target which provides the `CrateInfo` provider. However 137users may tag a target with `no-rustfmt` or `no-format` to have it skipped. Additionally, 138generated source files are also ignored by this aspect. 139""" , 140attrs = { 141"_config" : attr . label ( 142doc = "The `rustfmt.toml` file used for formatting" , 143allow_single_file = True , 144default = Label ( "//rust/settings:rustfmt.toml" ), 145), 146"_process_wrapper" : attr . label ( 147doc = "A process wrapper for running rustfmt on all platforms" , 148cfg = "exec" , 149executable = True , 150default = Label ( "//util/process_wrapper" ), 151), 152}, 153required_providers = [ 154[ rust_common . crate_info ], 155[ rust_common . test_crate_info ], 156], 157fragments = [ "cpp" ], 158toolchains = [ 159str ( Label ( "//rust/rustfmt:toolchain_type" )), 160], 161) 162 163def _rustfmt_test_manifest_aspect_impl ( target , ctx ): 164crate_info = _get_rustfmt_ready_crate_info ( target ) 165 166if not crate_info : 167return [] 168 169# Parse the edition to use for formatting from the target 170edition = crate_info . edition 171 172srcs = _find_rustfmtable_srcs ( crate_info , ctx ) 173manifest = _generate_manifest ( edition , srcs , ctx ) 174 175return [ 176OutputGroupInfo ( 177rustfmt_manifest = depset ([ manifest ]), 178), 179] 180 181# This aspect contains functionality split out of `rustfmt_aspect` which broke when 182# `required_providers` was added to it. Aspects which have `required_providers` seems 183# to not function with attributes that also require providers. 184_rustfmt_test_manifest_aspect = aspect ( 185implementation = _rustfmt_test_manifest_aspect_impl , 186doc = """\ 187This aspect is used to gather information about a crate for use in `rustfmt_test` 188 189Output Groups: 190 191- `rustfmt_manifest`: A manifest used by rustfmt binaries to provide crate specific settings. 192""" , 193fragments = [ "cpp" ], 194toolchains = [ 195str ( Label ( "//rust/rustfmt:toolchain_type" )), 196], 197) 198 199def _rustfmt_test_impl ( ctx ): 200# The executable of a test target must be the output of an action in 201# the rule implementation. This file is simply a symlink to the real 202# rustfmt test runner. 203is_windows = ctx . executable . _runner . extension == ".exe" 204runner = ctx . actions . declare_file ( "{}{}" . format ( 205ctx . label . name , 206".exe" if is_windows else "" , 207)) 208 209ctx . actions . symlink ( 210output = runner , 211target_file = ctx . executable . _runner , 212is_executable = True , 213) 214 215crate_infos = [ _get_rustfmt_ready_crate_info ( target ) for target in ctx . attr . targets ] 216srcs = [ depset ( _find_rustfmtable_srcs ( crate_info )) for crate_info in crate_infos if crate_info ] 217 218# Some targets may be included in tests but tagged as "no-format". In this 219# case, there will be no manifest. 220manifests = [ getattr ( target [ OutputGroupInfo ], "rustfmt_manifest" , None ) for target in ctx . attr . targets ] 221manifests = depset ( transitive = [ manifest for manifest in manifests if manifest ]) 222 223runfiles = ctx . runfiles ( 224transitive_files = depset ( transitive = srcs + [ manifests ]), 225) 226 227runfiles = runfiles . merge ( 228ctx . attr . _runner [ DefaultInfo ]. default_runfiles , 229) 230 231workspace = ctx . label . workspace_name or ctx . workspace_name 232 233return [ 234DefaultInfo ( 235files = depset ([ runner ]), 236runfiles = runfiles , 237executable = runner , 238), 239RunEnvironmentInfo ( 240environment = { 241"RUSTFMT_MANIFESTS" : ctx . configuration . host_path_separator . join ([ 242workspace + "/" + manifest . short_path 243for manifest in sorted ( manifests . to_list ()) 244]), 245"RUST_BACKTRACE" : "1" , 246}, 247), 248] 249 250rustfmt_test = rule ( 251implementation = _rustfmt_test_impl , 252doc = "A test rule for performing `rustfmt --check` on a set of targets" , 253attrs = { 254"targets" : attr . label_list ( 255doc = "Rust targets to run `rustfmt --check` on." , 256providers = [ 257[ rust_common . crate_info ], 258[ rust_common . test_crate_info ], 259], 260aspects = [ _rustfmt_test_manifest_aspect ], 261), 262"_runner" : attr . label ( 263doc = "The rustfmt test runner" , 264cfg = "exec" , 265executable = True , 266default = Label ( "//tools/rustfmt:rustfmt_test" ), 267), 268}, 269test = True , 270) 271 272def _rustfmt_toolchain_impl ( ctx ): 273make_variables = { 274"RUSTFMT" : ctx . file . rustfmt . path , 275} 276 277if ctx . attr . rustc : 278make_variables . update ({ 279"RUSTC" : ctx . file . rustc . path , 280}) 281 282make_variable_info = platform_common . TemplateVariableInfo ( make_variables ) 283 284all_files = [ ctx . file . rustfmt ] + ctx . files . rustc_lib 285if ctx . file . rustc : 286all_files . append ( ctx . file . rustc ) 287 288toolchain = platform_common . ToolchainInfo ( 289rustfmt = ctx . file . rustfmt , 290rustc = ctx . file . rustc , 291rustc_lib = depset ( ctx . files . rustc_lib ), 292all_files = depset ( all_files ), 293make_variables = make_variable_info , 294) 295 296return [ 297toolchain , 298make_variable_info , 299] 300 301rustfmt_toolchain = rule ( 302doc = "A toolchain for [rustfmt](https://rust-lang.github.io/rustfmt/)" , 303implementation = _rustfmt_toolchain_impl , 304attrs = { 305"rustc" : attr . label ( 306doc = "The location of the `rustc` binary. Can be a direct source or a filegroup containing one item." , 307allow_single_file = True , 308cfg = "exec" , 309), 310"rustc_lib" : attr . label ( 311doc = "The libraries used by rustc during compilation." , 312cfg = "exec" , 313), 314"rustfmt" : attr . label ( 315doc = "The location of the `rustfmt` binary. Can be a direct source or a filegroup containing one item." , 316allow_single_file = True , 317cfg = "exec" , 318mandatory = True , 319), 320}, 321toolchains = [ 322str ( Label ( "@rules_rust//rust:toolchain_type" )), 323], 324) 325 326def _current_rustfmt_toolchain_impl ( ctx ): 327toolchain = ctx . toolchains [ str ( Label ( "@rules_rust//rust/rustfmt:toolchain_type" ))] 328 329return [ 330toolchain , 331toolchain . make_variables , 332DefaultInfo ( 333files = depset ([ 334toolchain . rustfmt , 335]), 336runfiles = ctx . runfiles ( transitive_files = toolchain . all_files ), 337), 338] 339 340current_rustfmt_toolchain = rule ( 341doc = "A rule for exposing the current registered `rustfmt_toolchain`." , 342implementation = _current_rustfmt_toolchain_impl , 343toolchains = [ 344str ( Label ( "@rules_rust//rust/rustfmt:toolchain_type" )), 345], 346)