blob: 7c05e4f1d977ba2cc28a230854caa5bf3d212266 [file] [log] [blame]
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +02001// 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
15package config
16
17import (
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020018 "fmt"
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +020019 "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 Weksteen9e8451e2020-08-13 12:55:59 +020028//
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +020029// 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 Weksteen9e8451e2020-08-13 12:55:59 +020033// 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 Weksteen8e46efa2020-06-30 21:43:35 +020043var (
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 Stoep41f81572021-02-19 16:51:10 +010055 "-A clippy::unnecessary-wraps",
Ivan Lozanob6d0d9c2021-02-25 11:24:35 -050056 "-A clippy::unusual-byte-groupings",
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +020057 }
58
59 // Rust lints for vendor code.
60 defaultRustcVendorLints = []string{
61 "-A deprecated",
62 "-D warnings",
63 }
64 // Clippy lints for vendor source. These are applied on top of
65 // defaultRustcVendorLints. It should be assumed that any warning lint
66 // will be promoted to a deny.
67 defaultClippyVendorLints = []string{
68 "-A clippy::complexity",
69 "-A clippy::perf",
70 "-A clippy::style",
71 }
72
73 // For prebuilts/ and external/, no linting is expected. If a warning
74 // or a deny is reported, it should be fixed upstream.
75 allowAllLints = []string{
76 "--cap-lints allow",
77 }
78)
79
80func init() {
81 // Default Rust lints. These apply to all Google-authored modules.
82 pctx.VariableFunc("RustDefaultLints", func(ctx android.PackageVarContext) string {
83 if override := ctx.Config().Getenv("RUST_DEFAULT_LINTS"); override != "" {
84 return override
85 }
86 return strings.Join(defaultRustcLints, " ")
87 })
88 pctx.VariableFunc("ClippyDefaultLints", func(ctx android.PackageVarContext) string {
89 if override := ctx.Config().Getenv("CLIPPY_DEFAULT_LINTS"); override != "" {
90 return override
91 }
92 return strings.Join(defaultClippyLints, " ")
93 })
94
95 // Rust lints that only applies to external code.
96 pctx.VariableFunc("RustVendorLints", func(ctx android.PackageVarContext) string {
97 if override := ctx.Config().Getenv("RUST_VENDOR_LINTS"); override != "" {
98 return override
99 }
100 return strings.Join(defaultRustcVendorLints, " ")
101 })
102 pctx.VariableFunc("ClippyVendorLints", func(ctx android.PackageVarContext) string {
103 if override := ctx.Config().Getenv("CLIPPY_VENDOR_LINTS"); override != "" {
104 return override
105 }
106 return strings.Join(defaultClippyVendorLints, " ")
107 })
108 pctx.StaticVariable("RustAllowAllLints", strings.Join(allowAllLints, " "))
109}
110
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +0200111const noLint = ""
112const rustcDefault = "${config.RustDefaultLints}"
113const rustcVendor = "${config.RustVendorLints}"
114const rustcAllowAll = "${config.RustAllowAllLints}"
115const clippyDefault = "${config.ClippyDefaultLints}"
116const clippyVendor = "${config.ClippyVendorLints}"
117
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200118// lintConfig defines a set of lints and clippy configuration.
119type lintConfig struct {
120 rustcConfig string // for the lints to apply to rustc.
121 clippyEnabled bool // to indicate if clippy should be executed.
122 clippyConfig string // for the lints to apply to clippy.
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +0200123}
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200124
125const (
126 androidLints = "android"
127 vendorLints = "vendor"
128 noneLints = "none"
129)
130
131// lintSets defines the categories of linting for Android and their mapping to lintConfigs.
132var lintSets = map[string]lintConfig{
133 androidLints: {rustcDefault, true, clippyDefault},
134 vendorLints: {rustcVendor, true, clippyVendor},
135 noneLints: {rustcAllowAll, false, noLint},
136}
137
138type pathLintSet struct {
139 prefix string
140 set string
141}
142
143// This is a map of local path prefixes to a lint set. The first entry
144// matching will be used. If no entry matches, androidLints ("android") will be
145// used.
146var defaultLintSetForPath = []pathLintSet{
147 {"external", noneLints},
148 {"hardware", vendorLints},
149 {"prebuilts", noneLints},
150 {"vendor/google", androidLints},
151 {"vendor", vendorLints},
152}
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +0200153
154// ClippyLintsForDir returns a boolean if Clippy should be executed and if so, the lints to be used.
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200155func ClippyLintsForDir(dir string, clippyLintsProperty *string) (bool, string, error) {
156 if clippyLintsProperty != nil {
157 set, ok := lintSets[*clippyLintsProperty]
158 if ok {
159 return set.clippyEnabled, set.clippyConfig, nil
160 }
161 if *clippyLintsProperty != "default" {
162 return false, "", fmt.Errorf("unknown value for `clippy_lints`: %v, valid options are: default, android, vendor or none", *clippyLintsProperty)
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +0200163 }
164 }
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200165 for _, p := range defaultLintSetForPath {
166 if strings.HasPrefix(dir, p.prefix) {
167 setConfig := lintSets[p.set]
168 return setConfig.clippyEnabled, setConfig.clippyConfig, nil
169 }
170 }
171 return true, clippyDefault, nil
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +0200172}
173
174// RustcLintsForDir returns the standard lints to be used for a repository.
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200175func RustcLintsForDir(dir string, lintProperty *string) (string, error) {
176 if lintProperty != nil {
177 set, ok := lintSets[*lintProperty]
178 if ok {
179 return set.rustcConfig, nil
180 }
181 if *lintProperty != "default" {
182 return "", fmt.Errorf("unknown value for `lints`: %v, valid options are: default, android, vendor or none", *lintProperty)
183 }
184
185 }
186 for _, p := range defaultLintSetForPath {
187 if strings.HasPrefix(dir, p.prefix) {
188 return lintSets[p.set].rustcConfig, nil
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +0200189 }
190 }
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200191 return rustcDefault, nil
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +0200192}