fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
52c87aad
0.5.0
1"""A module defining rustfmt rules""" 2 3load ( ":common.bzl" , "rust_common" ) 4load ( ":utils.bzl" , "find_toolchain" ) 5 6def _find_rustfmtable_srcs ( target , aspect_ctx = None ): 7"""Parse a target for rustfmt formattable sources. 8 9Args: 10target (Target): The target the aspect is running on. 11aspect_ctx (ctx, optional): The aspect's context object. 12 13Returns: 14list: A list of formattable sources (`File`). 15""" 16if rust_common . crate_info not in target : 17return [] 18 19# Ignore external targets 20if target . label . workspace_root . startswith ( "external" ): 21return [] 22 23# Targets annotated with `norustfmt` will not be formatted 24if aspect_ctx and "norustfmt" in aspect_ctx . rule . attr . tags : 25return [] 26 27crate_info = target [ rust_common . crate_info ] 28 29# Filter out any generated files 30srcs = [ src for src in crate_info . srcs . to_list () if src . is_source ] 31 32return srcs 33 34def _generate_manifest ( edition , srcs , ctx ): 35# Gather the source paths to non-generated files 36src_paths = [ src . path for src in srcs ] 37 38# Write the rustfmt manifest 39manifest = ctx . actions . declare_file ( ctx . label . name + ".rustfmt" ) 40ctx . actions . write ( 41output = manifest , 42content = "\n ". join ( src_paths + [ 43edition , 44]), 45) 46 47return manifest 48 49def _perform_check ( edition , srcs , ctx ): 50toolchain = find_toolchain ( ctx ) 51config = ctx . file . _config 52marker = ctx . actions . declare_file ( ctx . label . name + ".rustfmt.ok" ) 53 54args = ctx . actions . args () 55args . add ( "--touch-file" ) 56args . add ( marker ) 57args . add ( "--" ) 58args . add ( toolchain . rustfmt ) 59args . add ( "--config-path" ) 60args . add ( config ) 61args . add ( "--edition" ) 62args . add ( edition ) 63args . add ( "--check" ) 64args . add_all ( srcs ) 65 66ctx . actions . run ( 67executable = ctx . executable . _process_wrapper , 68inputs = srcs + [ config ], 69outputs = [ marker ], 70tools = [ toolchain . rustfmt ], 71arguments = [ args ], 72mnemonic = "Rustfmt" , 73) 74 75return marker 76 77def _rustfmt_aspect_impl ( target , ctx ): 78srcs = _find_rustfmtable_srcs ( target , ctx ) 79 80# If there are no formattable sources, do nothing. 81if not srcs : 82return [] 83 84# Parse the edition to use for formatting from the target 85edition = target [ rust_common . crate_info ]. edition 86 87manifest = _generate_manifest ( edition , srcs , ctx ) 88marker = _perform_check ( edition , srcs , ctx ) 89 90return [ 91OutputGroupInfo ( 92rustfmt_manifest = depset ([ manifest ]), 93rustfmt_checks = depset ([ marker ]), 94), 95] 96 97rustfmt_aspect = aspect ( 98implementation = _rustfmt_aspect_impl , 99doc = """\ 100This aspect is used to gather information about a crate for use in rustfmt and perform rustfmt checks 101 102Output Groups: 103 104- `rustfmt_manifest`: A manifest used by rustfmt binaries to provide crate specific settings. 105- `rustfmt_checks`: Executes `rustfmt --check` on the specified target. 106 107The build setting `@rules_rust//:rustfmt.toml` is used to control the Rustfmt [configuration settings][cs] 108used at runtime. 109 110[cs]: https://rust-lang.github.io/rustfmt/ 111 112This aspect is executed on any target which provides the `CrateInfo` provider. However 113users may tag a target with `norustfmt` to have it skipped. Additionally, generated 114source files are also ignored by this aspect. 115""" , 116attrs = { 117"_config" : attr . label ( 118doc = "The `rustfmt.toml` file used for formatting" , 119allow_single_file = True , 120default = Label ( "//:rustfmt.toml" ), 121), 122"_process_wrapper" : attr . label ( 123doc = "A process wrapper for running rustfmt on all platforms" , 124cfg = "exec" , 125executable = True , 126default = Label ( "//util/process_wrapper" ), 127), 128}, 129incompatible_use_toolchain_transition = True , 130fragments = [ "cpp" ], 131host_fragments = [ "cpp" ], 132toolchains = [ 133str ( Label ( "//rust:toolchain" )), 134], 135) 136 137def _rustfmt_test_impl ( ctx ): 138# The executable of a test target must be the output of an action in 139# the rule implementation. This file is simply a symlink to the real 140# rustfmt test runner. 141runner = ctx . actions . declare_file ( "{}{}" . format ( 142ctx . label . name , 143ctx . executable . _runner . extension , 144)) 145 146ctx . actions . symlink ( 147output = runner , 148target_file = ctx . executable . _runner , 149is_executable = True , 150) 151 152manifests = [ target [ OutputGroupInfo ]. rustfmt_manifest for target in ctx . attr . targets ] 153srcs = [ depset ( _find_rustfmtable_srcs ( target )) for target in ctx . attr . targets ] 154 155runfiles = ctx . runfiles ( 156transitive_files = depset ( transitive = manifests + srcs ), 157) 158 159runfiles = runfiles . merge ( 160ctx . attr . _runner [ DefaultInfo ]. default_runfiles , 161) 162 163return [ DefaultInfo ( 164files = depset ([ runner ]), 165runfiles = runfiles , 166executable = runner , 167)] 168 169rustfmt_test = rule ( 170implementation = _rustfmt_test_impl , 171doc = "A test rule for performing `rustfmt --check` on a set of targets" , 172attrs = { 173"targets" : attr . label_list ( 174doc = "Rust targets to run `rustfmt --check` on." , 175providers = [ rust_common . crate_info ], 176aspects = [ rustfmt_aspect ], 177), 178"_runner" : attr . label ( 179doc = "The rustfmt test runner" , 180cfg = "exec" , 181executable = True , 182default = Label ( "//tools/rustfmt:rustfmt_test" ), 183), 184}, 185test = True , 186)