fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
30e68b49
0.8.1
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 23if aspect_ctx : 24# Targets with specifc tags will not be formatted 25ignore_tags = [ 26"no-format" , 27"no-rustfmt" , 28"norustfmt" , 29] 30 31for tag in ignore_tags : 32if tag in aspect_ctx . rule . attr . tags : 33return [] 34 35crate_info = target [ rust_common . crate_info ] 36 37# Filter out any generated files 38srcs = [ src for src in crate_info . srcs . to_list () if src . is_source ] 39 40return srcs 41 42def _generate_manifest ( edition , srcs , ctx ): 43# Gather the source paths to non-generated files 44src_paths = [ src . path for src in srcs ] 45 46# Write the rustfmt manifest 47manifest = ctx . actions . declare_file ( ctx . label . name + ".rustfmt" ) 48ctx . actions . write ( 49output = manifest , 50content = "\n ". join ( src_paths + [ 51edition , 52]), 53) 54 55return manifest 56 57def _perform_check ( edition , srcs , ctx ): 58toolchain = find_toolchain ( ctx ) 59config = ctx . file . _config 60marker = ctx . actions . declare_file ( ctx . label . name + ".rustfmt.ok" ) 61 62args = ctx . actions . args () 63args . add ( "--touch-file" ) 64args . add ( marker ) 65args . add ( "--" ) 66args . add ( toolchain . rustfmt ) 67args . add ( "--config-path" ) 68args . add ( config ) 69args . add ( "--edition" ) 70args . add ( edition ) 71args . add ( "--check" ) 72args . add_all ( srcs ) 73 74ctx . actions . run ( 75executable = ctx . executable . _process_wrapper , 76inputs = srcs + [ config ], 77outputs = [ marker ], 78tools = [ toolchain . rustfmt ], 79arguments = [ args ], 80mnemonic = "Rustfmt" , 81) 82 83return marker 84 85def _rustfmt_aspect_impl ( target , ctx ): 86srcs = _find_rustfmtable_srcs ( target , ctx ) 87 88# If there are no formattable sources, do nothing. 89if not srcs : 90return [] 91 92# Parse the edition to use for formatting from the target 93edition = target [ rust_common . crate_info ]. edition 94 95manifest = _generate_manifest ( edition , srcs , ctx ) 96marker = _perform_check ( edition , srcs , ctx ) 97 98return [ 99OutputGroupInfo ( 100rustfmt_manifest = depset ([ manifest ]), 101rustfmt_checks = depset ([ marker ]), 102), 103] 104 105rustfmt_aspect = aspect ( 106implementation = _rustfmt_aspect_impl , 107doc = """\ 108This aspect is used to gather information about a crate for use in rustfmt and perform rustfmt checks 109 110Output Groups: 111 112- `rustfmt_manifest`: A manifest used by rustfmt binaries to provide crate specific settings. 113- `rustfmt_checks`: Executes `rustfmt --check` on the specified target. 114 115The build setting `@rules_rust//:rustfmt.toml` is used to control the Rustfmt [configuration settings][cs] 116used at runtime. 117 118[cs]: https://rust-lang.github.io/rustfmt/ 119 120This aspect is executed on any target which provides the `CrateInfo` provider. However 121users may tag a target with `no-rustfmt` or `no-format` to have it skipped. Additionally, 122generated source files are also ignored by this aspect. 123""" , 124attrs = { 125"_config" : attr . label ( 126doc = "The `rustfmt.toml` file used for formatting" , 127allow_single_file = True , 128default = Label ( "//:rustfmt.toml" ), 129), 130"_process_wrapper" : attr . label ( 131doc = "A process wrapper for running rustfmt on all platforms" , 132cfg = "exec" , 133executable = True , 134default = Label ( "//util/process_wrapper" ), 135), 136}, 137incompatible_use_toolchain_transition = True , 138fragments = [ "cpp" ], 139host_fragments = [ "cpp" ], 140toolchains = [ 141str ( Label ( "//rust:toolchain" )), 142], 143) 144 145def _rustfmt_test_impl ( ctx ): 146# The executable of a test target must be the output of an action in 147# the rule implementation. This file is simply a symlink to the real 148# rustfmt test runner. 149is_windows = ctx . executable . _runner . extension == ".exe" 150runner = ctx . actions . declare_file ( "{}{}" . format ( 151ctx . label . name , 152".exe" if is_windows else "" , 153)) 154 155ctx . actions . symlink ( 156output = runner , 157target_file = ctx . executable . _runner , 158is_executable = True , 159) 160 161manifests = depset ( transitive = [ target [ OutputGroupInfo ]. rustfmt_manifest for target in ctx . attr . targets ]) 162srcs = [ depset ( _find_rustfmtable_srcs ( target )) for target in ctx . attr . targets ] 163 164runfiles = ctx . runfiles ( 165transitive_files = depset ( transitive = srcs + [ manifests ]), 166) 167 168runfiles = runfiles . merge ( 169ctx . attr . _runner [ DefaultInfo ]. default_runfiles , 170) 171 172path_env_sep = ";" if is_windows else ":" 173 174return [ 175DefaultInfo ( 176files = depset ([ runner ]), 177runfiles = runfiles , 178executable = runner , 179), 180testing . TestEnvironment ({ 181"RUSTFMT_MANIFESTS" : path_env_sep . join ([ 182manifest . short_path 183for manifest in sorted ( manifests . to_list ()) 184]), 185"RUST_BACKTRACE" : "1" , 186}), 187] 188 189rustfmt_test = rule ( 190implementation = _rustfmt_test_impl , 191doc = "A test rule for performing `rustfmt --check` on a set of targets" , 192attrs = { 193"targets" : attr . label_list ( 194doc = "Rust targets to run `rustfmt --check` on." , 195providers = [ rust_common . crate_info ], 196aspects = [ rustfmt_aspect ], 197), 198"_runner" : attr . label ( 199doc = "The rustfmt test runner" , 200cfg = "exec" , 201executable = True , 202default = Label ( "//tools/rustfmt:rustfmt_test" ), 203), 204}, 205test = True , 206) 207 208def _rustfmt_workspace_name_impl ( ctx ): 209output = ctx . actions . declare_file ( ctx . label . name ) 210 211ctx . actions . write ( 212output = output , 213content = "RUSTFMT_WORKSPACE={}" . format ( 214ctx . workspace_name , 215), 216) 217 218return [ DefaultInfo ( 219files = depset ([ output ]), 220)] 221 222rustfmt_workspace_name = rule ( 223implementation = _rustfmt_workspace_name_impl , 224doc = "A rule for detecting the workspace name for Rustfmt runfiles." , 225)