blob: ac8165b2265e5840748eabebd56e2ef999d03c64 [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",
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +020056 }
57
58 // Rust lints for vendor code.
59 defaultRustcVendorLints = []string{
60 "-A deprecated",
61 "-D warnings",
62 }
63 // Clippy lints for vendor source. These are applied on top of
64 // defaultRustcVendorLints. It should be assumed that any warning lint
65 // will be promoted to a deny.
66 defaultClippyVendorLints = []string{
67 "-A clippy::complexity",
68 "-A clippy::perf",
69 "-A clippy::style",
70 }
71
72 // For prebuilts/ and external/, no linting is expected. If a warning
73 // or a deny is reported, it should be fixed upstream.
74 allowAllLints = []string{
75 "--cap-lints allow",
76 }
77)
78
79func init() {
80 // Default Rust lints. These apply to all Google-authored modules.
81 pctx.VariableFunc("RustDefaultLints", func(ctx android.PackageVarContext) string {
82 if override := ctx.Config().Getenv("RUST_DEFAULT_LINTS"); override != "" {
83 return override
84 }
85 return strings.Join(defaultRustcLints, " ")
86 })
87 pctx.VariableFunc("ClippyDefaultLints", func(ctx android.PackageVarContext) string {
88 if override := ctx.Config().Getenv("CLIPPY_DEFAULT_LINTS"); override != "" {
89 return override
90 }
91 return strings.Join(defaultClippyLints, " ")
92 })
93
94 // Rust lints that only applies to external code.
95 pctx.VariableFunc("RustVendorLints", func(ctx android.PackageVarContext) string {
96 if override := ctx.Config().Getenv("RUST_VENDOR_LINTS"); override != "" {
97 return override
98 }
99 return strings.Join(defaultRustcVendorLints, " ")
100 })
101 pctx.VariableFunc("ClippyVendorLints", func(ctx android.PackageVarContext) string {
102 if override := ctx.Config().Getenv("CLIPPY_VENDOR_LINTS"); override != "" {
103 return override
104 }
105 return strings.Join(defaultClippyVendorLints, " ")
106 })
107 pctx.StaticVariable("RustAllowAllLints", strings.Join(allowAllLints, " "))
108}
109
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +0200110const noLint = ""
111const rustcDefault = "${config.RustDefaultLints}"
112const rustcVendor = "${config.RustVendorLints}"
113const rustcAllowAll = "${config.RustAllowAllLints}"
114const clippyDefault = "${config.ClippyDefaultLints}"
115const clippyVendor = "${config.ClippyVendorLints}"
116
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200117// lintConfig defines a set of lints and clippy configuration.
118type lintConfig struct {
119 rustcConfig string // for the lints to apply to rustc.
120 clippyEnabled bool // to indicate if clippy should be executed.
121 clippyConfig string // for the lints to apply to clippy.
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +0200122}
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200123
124const (
125 androidLints = "android"
126 vendorLints = "vendor"
127 noneLints = "none"
128)
129
130// lintSets defines the categories of linting for Android and their mapping to lintConfigs.
131var lintSets = map[string]lintConfig{
132 androidLints: {rustcDefault, true, clippyDefault},
133 vendorLints: {rustcVendor, true, clippyVendor},
134 noneLints: {rustcAllowAll, false, noLint},
135}
136
137type pathLintSet struct {
138 prefix string
139 set string
140}
141
142// This is a map of local path prefixes to a lint set. The first entry
143// matching will be used. If no entry matches, androidLints ("android") will be
144// used.
145var defaultLintSetForPath = []pathLintSet{
146 {"external", noneLints},
147 {"hardware", vendorLints},
148 {"prebuilts", noneLints},
149 {"vendor/google", androidLints},
150 {"vendor", vendorLints},
151}
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +0200152
153// 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 +0200154func ClippyLintsForDir(dir string, clippyLintsProperty *string) (bool, string, error) {
155 if clippyLintsProperty != nil {
156 set, ok := lintSets[*clippyLintsProperty]
157 if ok {
158 return set.clippyEnabled, set.clippyConfig, nil
159 }
160 if *clippyLintsProperty != "default" {
161 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 +0200162 }
163 }
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200164 for _, p := range defaultLintSetForPath {
165 if strings.HasPrefix(dir, p.prefix) {
166 setConfig := lintSets[p.set]
167 return setConfig.clippyEnabled, setConfig.clippyConfig, nil
168 }
169 }
170 return true, clippyDefault, nil
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +0200171}
172
173// RustcLintsForDir returns the standard lints to be used for a repository.
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200174func RustcLintsForDir(dir string, lintProperty *string) (string, error) {
175 if lintProperty != nil {
176 set, ok := lintSets[*lintProperty]
177 if ok {
178 return set.rustcConfig, nil
179 }
180 if *lintProperty != "default" {
181 return "", fmt.Errorf("unknown value for `lints`: %v, valid options are: default, android, vendor or none", *lintProperty)
182 }
183
184 }
185 for _, p := range defaultLintSetForPath {
186 if strings.HasPrefix(dir, p.prefix) {
187 return lintSets[p.set].rustcConfig, nil
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +0200188 }
189 }
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200190 return rustcDefault, nil
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +0200191}