Thiébaud Weksteen | 8e46efa | 2020-06-30 21:43:35 +0200 | [diff] [blame] | 1 | // Copyright 2020 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package config |
| 16 | |
| 17 | import ( |
Thiébaud Weksteen | 9e8451e | 2020-08-13 12:55:59 +0200 | [diff] [blame] | 18 | "fmt" |
Thiébaud Weksteen | 8e46efa | 2020-06-30 21:43:35 +0200 | [diff] [blame] | 19 | "strings" |
| 20 | |
| 21 | "android/soong/android" |
| 22 | ) |
| 23 | |
| 24 | // Overarching principles for Rust lints on Android: |
| 25 | // The Android build system tries to avoid reporting warnings during the build. |
| 26 | // Therefore, by default, we upgrade warnings to denials. For some of these |
| 27 | // lints, an allow exception is setup, using the variables below. |
Thiébaud Weksteen | 9e8451e | 2020-08-13 12:55:59 +0200 | [diff] [blame] | 28 | // |
Thiébaud Weksteen | 8e46efa | 2020-06-30 21:43:35 +0200 | [diff] [blame] | 29 | // The lints are split into two categories. The first one contains the built-in |
| 30 | // lints (https://doc.rust-lang.org/rustc/lints/index.html). The second is |
| 31 | // specific to Clippy lints (https://rust-lang.github.io/rust-clippy/master/). |
| 32 | // |
Thiébaud Weksteen | 9e8451e | 2020-08-13 12:55:59 +0200 | [diff] [blame] | 33 | // For both categories, there are 3 levels of linting possible: |
| 34 | // - "android", for the strictest lints that applies to all Android platform code. |
| 35 | // - "vendor", for relaxed rules. |
| 36 | // - "none", to disable the linting. |
| 37 | // There is a fourth option ("default") which automatically selects the linting level |
| 38 | // based on the module's location. See defaultLintSetForPath. |
| 39 | // |
| 40 | // When developing a module, you may set `lints = "none"` and `clippy_lints = |
| 41 | // "none"` to disable all the linting. Expect some questioning during code review |
| 42 | // if you enable one of these options. |
Thiébaud Weksteen | 8e46efa | 2020-06-30 21:43:35 +0200 | [diff] [blame] | 43 | var ( |
| 44 | // Default Rust lints that applies to Google-authored modules. |
| 45 | defaultRustcLints = []string{ |
| 46 | "-A deprecated", |
| 47 | "-D missing-docs", |
| 48 | "-D warnings", |
| 49 | } |
| 50 | // Default Clippy lints. These are applied on top of defaultRustcLints. |
| 51 | // It should be assumed that any warning lint will be promoted to a |
| 52 | // deny. |
| 53 | defaultClippyLints = []string{ |
| 54 | "-A clippy::type-complexity", |
Jeff Vander Stoep | 41f8157 | 2021-02-19 16:51:10 +0100 | [diff] [blame] | 55 | "-A clippy::unnecessary-wraps", |
Ivan Lozano | b6d0d9c | 2021-02-25 11:24:35 -0500 | [diff] [blame] | 56 | "-A clippy::unusual-byte-groupings", |
Jeff Vander Stoep | 0f36d16 | 2021-04-01 19:40:37 +0200 | [diff] [blame] | 57 | "-A clippy::upper-case-acronyms", |
Thiébaud Weksteen | 8e46efa | 2020-06-30 21:43:35 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // Rust lints for vendor code. |
| 61 | defaultRustcVendorLints = []string{ |
| 62 | "-A deprecated", |
| 63 | "-D warnings", |
| 64 | } |
| 65 | // Clippy lints for vendor source. These are applied on top of |
| 66 | // defaultRustcVendorLints. It should be assumed that any warning lint |
| 67 | // will be promoted to a deny. |
| 68 | defaultClippyVendorLints = []string{ |
| 69 | "-A clippy::complexity", |
| 70 | "-A clippy::perf", |
| 71 | "-A clippy::style", |
| 72 | } |
| 73 | |
| 74 | // For prebuilts/ and external/, no linting is expected. If a warning |
| 75 | // or a deny is reported, it should be fixed upstream. |
| 76 | allowAllLints = []string{ |
| 77 | "--cap-lints allow", |
| 78 | } |
| 79 | ) |
| 80 | |
| 81 | func init() { |
| 82 | // Default Rust lints. These apply to all Google-authored modules. |
| 83 | pctx.VariableFunc("RustDefaultLints", func(ctx android.PackageVarContext) string { |
| 84 | if override := ctx.Config().Getenv("RUST_DEFAULT_LINTS"); override != "" { |
| 85 | return override |
| 86 | } |
| 87 | return strings.Join(defaultRustcLints, " ") |
| 88 | }) |
| 89 | pctx.VariableFunc("ClippyDefaultLints", func(ctx android.PackageVarContext) string { |
| 90 | if override := ctx.Config().Getenv("CLIPPY_DEFAULT_LINTS"); override != "" { |
| 91 | return override |
| 92 | } |
| 93 | return strings.Join(defaultClippyLints, " ") |
| 94 | }) |
| 95 | |
| 96 | // Rust lints that only applies to external code. |
| 97 | pctx.VariableFunc("RustVendorLints", func(ctx android.PackageVarContext) string { |
| 98 | if override := ctx.Config().Getenv("RUST_VENDOR_LINTS"); override != "" { |
| 99 | return override |
| 100 | } |
| 101 | return strings.Join(defaultRustcVendorLints, " ") |
| 102 | }) |
| 103 | pctx.VariableFunc("ClippyVendorLints", func(ctx android.PackageVarContext) string { |
| 104 | if override := ctx.Config().Getenv("CLIPPY_VENDOR_LINTS"); override != "" { |
| 105 | return override |
| 106 | } |
| 107 | return strings.Join(defaultClippyVendorLints, " ") |
| 108 | }) |
| 109 | pctx.StaticVariable("RustAllowAllLints", strings.Join(allowAllLints, " ")) |
| 110 | } |
| 111 | |
Thiébaud Weksteen | 8e46efa | 2020-06-30 21:43:35 +0200 | [diff] [blame] | 112 | const noLint = "" |
| 113 | const rustcDefault = "${config.RustDefaultLints}" |
| 114 | const rustcVendor = "${config.RustVendorLints}" |
| 115 | const rustcAllowAll = "${config.RustAllowAllLints}" |
| 116 | const clippyDefault = "${config.ClippyDefaultLints}" |
| 117 | const clippyVendor = "${config.ClippyVendorLints}" |
| 118 | |
Thiébaud Weksteen | 9e8451e | 2020-08-13 12:55:59 +0200 | [diff] [blame] | 119 | // lintConfig defines a set of lints and clippy configuration. |
| 120 | type lintConfig struct { |
| 121 | rustcConfig string // for the lints to apply to rustc. |
| 122 | clippyEnabled bool // to indicate if clippy should be executed. |
| 123 | clippyConfig string // for the lints to apply to clippy. |
Thiébaud Weksteen | 8e46efa | 2020-06-30 21:43:35 +0200 | [diff] [blame] | 124 | } |
Thiébaud Weksteen | 9e8451e | 2020-08-13 12:55:59 +0200 | [diff] [blame] | 125 | |
| 126 | const ( |
| 127 | androidLints = "android" |
| 128 | vendorLints = "vendor" |
| 129 | noneLints = "none" |
| 130 | ) |
| 131 | |
| 132 | // lintSets defines the categories of linting for Android and their mapping to lintConfigs. |
| 133 | var lintSets = map[string]lintConfig{ |
| 134 | androidLints: {rustcDefault, true, clippyDefault}, |
| 135 | vendorLints: {rustcVendor, true, clippyVendor}, |
| 136 | noneLints: {rustcAllowAll, false, noLint}, |
| 137 | } |
| 138 | |
| 139 | type pathLintSet struct { |
| 140 | prefix string |
| 141 | set string |
| 142 | } |
| 143 | |
| 144 | // This is a map of local path prefixes to a lint set. The first entry |
| 145 | // matching will be used. If no entry matches, androidLints ("android") will be |
| 146 | // used. |
| 147 | var defaultLintSetForPath = []pathLintSet{ |
| 148 | {"external", noneLints}, |
| 149 | {"hardware", vendorLints}, |
| 150 | {"prebuilts", noneLints}, |
| 151 | {"vendor/google", androidLints}, |
| 152 | {"vendor", vendorLints}, |
| 153 | } |
Thiébaud Weksteen | 8e46efa | 2020-06-30 21:43:35 +0200 | [diff] [blame] | 154 | |
| 155 | // ClippyLintsForDir returns a boolean if Clippy should be executed and if so, the lints to be used. |
Thiébaud Weksteen | 9e8451e | 2020-08-13 12:55:59 +0200 | [diff] [blame] | 156 | func ClippyLintsForDir(dir string, clippyLintsProperty *string) (bool, string, error) { |
| 157 | if clippyLintsProperty != nil { |
| 158 | set, ok := lintSets[*clippyLintsProperty] |
| 159 | if ok { |
| 160 | return set.clippyEnabled, set.clippyConfig, nil |
| 161 | } |
| 162 | if *clippyLintsProperty != "default" { |
| 163 | return false, "", fmt.Errorf("unknown value for `clippy_lints`: %v, valid options are: default, android, vendor or none", *clippyLintsProperty) |
Thiébaud Weksteen | 8e46efa | 2020-06-30 21:43:35 +0200 | [diff] [blame] | 164 | } |
| 165 | } |
Thiébaud Weksteen | 9e8451e | 2020-08-13 12:55:59 +0200 | [diff] [blame] | 166 | for _, p := range defaultLintSetForPath { |
| 167 | if strings.HasPrefix(dir, p.prefix) { |
| 168 | setConfig := lintSets[p.set] |
| 169 | return setConfig.clippyEnabled, setConfig.clippyConfig, nil |
| 170 | } |
| 171 | } |
| 172 | return true, clippyDefault, nil |
Thiébaud Weksteen | 8e46efa | 2020-06-30 21:43:35 +0200 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // RustcLintsForDir returns the standard lints to be used for a repository. |
Thiébaud Weksteen | 9e8451e | 2020-08-13 12:55:59 +0200 | [diff] [blame] | 176 | func RustcLintsForDir(dir string, lintProperty *string) (string, error) { |
| 177 | if lintProperty != nil { |
| 178 | set, ok := lintSets[*lintProperty] |
| 179 | if ok { |
| 180 | return set.rustcConfig, nil |
| 181 | } |
| 182 | if *lintProperty != "default" { |
| 183 | return "", fmt.Errorf("unknown value for `lints`: %v, valid options are: default, android, vendor or none", *lintProperty) |
| 184 | } |
| 185 | |
| 186 | } |
| 187 | for _, p := range defaultLintSetForPath { |
| 188 | if strings.HasPrefix(dir, p.prefix) { |
| 189 | return lintSets[p.set].rustcConfig, nil |
Thiébaud Weksteen | 8e46efa | 2020-06-30 21:43:35 +0200 | [diff] [blame] | 190 | } |
| 191 | } |
Thiébaud Weksteen | 9e8451e | 2020-08-13 12:55:59 +0200 | [diff] [blame] | 192 | return rustcDefault, nil |
Thiébaud Weksteen | 8e46efa | 2020-06-30 21:43:35 +0200 | [diff] [blame] | 193 | } |