ThiƩbaud Weksteen | 92f703b | 2020-06-22 13:28:02 +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 ( |
| 18 | "strings" |
| 19 | |
| 20 | "android/soong/android" |
| 21 | ) |
| 22 | |
| 23 | var ( |
| 24 | defaultLints = []string{ |
| 25 | "-D missing-docs", |
| 26 | "-D clippy::missing-safety-doc", |
| 27 | } |
| 28 | defaultVendorLints = []string{ |
| 29 | "", |
| 30 | } |
| 31 | ) |
| 32 | |
| 33 | func init() { |
| 34 | // Default Rust lints. These apply to all Google-authored modules. |
| 35 | pctx.VariableFunc("ClippyDefaultLints", func(ctx android.PackageVarContext) string { |
| 36 | if override := ctx.Config().Getenv("CLIPPY_DEFAULT_LINTS"); override != "" { |
| 37 | return override |
| 38 | } |
| 39 | return strings.Join(defaultLints, " ") |
| 40 | }) |
| 41 | |
| 42 | // Rust lints that only applies to external code. |
| 43 | pctx.VariableFunc("ClippyVendorLints", func(ctx android.PackageVarContext) string { |
| 44 | if override := ctx.Config().Getenv("CLIPPY_VENDOR_LINTS"); override != "" { |
| 45 | return override |
| 46 | } |
| 47 | return strings.Join(defaultVendorLints, " ") |
| 48 | }) |
| 49 | } |
| 50 | |
| 51 | type PathBasedClippyConfig struct { |
| 52 | PathPrefix string |
| 53 | Enabled bool |
| 54 | ClippyConfig string |
| 55 | } |
| 56 | |
| 57 | const clippyNone = "" |
| 58 | const clippyDefault = "${config.ClippyDefaultLints}" |
| 59 | const clippyVendor = "${config.ClippyVendorLints}" |
| 60 | |
| 61 | // This is a map of local path prefixes to a boolean indicating if the lint |
| 62 | // rule should be generated and if so, the set of lints to use. The first entry |
| 63 | // matching will be used. If no entry is matching, clippyDefault will be used. |
| 64 | var DefaultLocalTidyChecks = []PathBasedClippyConfig{ |
| 65 | {"external/", false, clippyNone}, |
| 66 | {"hardware/", true, clippyVendor}, |
| 67 | {"prebuilts/", false, clippyNone}, |
| 68 | {"vendor/google", true, clippyDefault}, |
| 69 | {"vendor/", true, clippyVendor}, |
| 70 | } |
| 71 | |
| 72 | // ClippyLintsForDir returns the Clippy lints to be used for a repository. |
| 73 | func ClippyLintsForDir(dir string) (bool, string) { |
| 74 | for _, pathCheck := range DefaultLocalTidyChecks { |
| 75 | if strings.HasPrefix(dir, pathCheck.PathPrefix) { |
| 76 | return pathCheck.Enabled, pathCheck.ClippyConfig |
| 77 | } |
| 78 | } |
| 79 | return true, clippyDefault |
| 80 | } |