Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 1 | // Copyright 2020 Google Inc. All rights reserved. |
| 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 java |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "sort" |
Cole Faust | e5bf3fb | 2022-07-01 19:39:14 +0000 | [diff] [blame] | 20 | "strconv" |
Colin Cross | 988dfcc | 2020-07-16 17:32:17 -0700 | [diff] [blame] | 21 | "strings" |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 22 | |
Pedro Loureiro | 5d190cc | 2021-02-15 15:41:33 +0000 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" |
| 24 | |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 25 | "android/soong/android" |
Colin Cross | 31972dc | 2021-03-04 10:44:12 -0800 | [diff] [blame] | 26 | "android/soong/java/config" |
| 27 | "android/soong/remoteexec" |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
Jaewoong Jung | 79e6f6b | 2021-04-21 14:01:55 -0700 | [diff] [blame] | 30 | // lint checks automatically enforced for modules that have different min_sdk_version than |
| 31 | // sdk_version |
| 32 | var updatabilityChecks = []string{"NewApi"} |
| 33 | |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 34 | type LintProperties struct { |
| 35 | // Controls for running Android Lint on the module. |
| 36 | Lint struct { |
| 37 | |
| 38 | // If true, run Android Lint on the module. Defaults to true. |
| 39 | Enabled *bool |
| 40 | |
| 41 | // Flags to pass to the Android Lint tool. |
| 42 | Flags []string |
| 43 | |
| 44 | // Checks that should be treated as fatal. |
| 45 | Fatal_checks []string |
| 46 | |
| 47 | // Checks that should be treated as errors. |
| 48 | Error_checks []string |
| 49 | |
| 50 | // Checks that should be treated as warnings. |
| 51 | Warning_checks []string |
| 52 | |
| 53 | // Checks that should be skipped. |
| 54 | Disabled_checks []string |
Colin Cross | 92e4b46 | 2020-06-18 15:56:48 -0700 | [diff] [blame] | 55 | |
| 56 | // Modules that provide extra lint checks |
| 57 | Extra_check_modules []string |
Pedro Loureiro | 5d190cc | 2021-02-15 15:41:33 +0000 | [diff] [blame] | 58 | |
| 59 | // Name of the file that lint uses as the baseline. Defaults to "lint-baseline.xml". |
| 60 | Baseline_filename *string |
Jaewoong Jung | 48de883 | 2021-04-21 16:17:25 -0700 | [diff] [blame] | 61 | |
| 62 | // If true, baselining updatability lint checks (e.g. NewApi) is prohibited. Defaults to false. |
| 63 | Strict_updatability_linting *bool |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | type linter struct { |
Pedro Loureiro | f4a88b1 | 2021-02-25 16:23:22 +0000 | [diff] [blame] | 68 | name string |
| 69 | manifest android.Path |
| 70 | mergedManifest android.Path |
| 71 | srcs android.Paths |
| 72 | srcJars android.Paths |
| 73 | resources android.Paths |
| 74 | classpath android.Paths |
| 75 | classes android.Path |
| 76 | extraLintCheckJars android.Paths |
| 77 | test bool |
| 78 | library bool |
Cole Faust | e5bf3fb | 2022-07-01 19:39:14 +0000 | [diff] [blame] | 79 | minSdkVersion int |
| 80 | targetSdkVersion int |
| 81 | compileSdkVersion int |
Pedro Loureiro | 18233a2 | 2021-06-08 18:11:21 +0000 | [diff] [blame] | 82 | compileSdkKind android.SdkKind |
Pedro Loureiro | f4a88b1 | 2021-02-25 16:23:22 +0000 | [diff] [blame] | 83 | javaLanguageLevel string |
| 84 | kotlinLanguageLevel string |
| 85 | outputs lintOutputs |
| 86 | properties LintProperties |
| 87 | extraMainlineLintErrors []string |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 88 | |
Colin Cross | 08dca38 | 2020-07-21 20:31:17 -0700 | [diff] [blame] | 89 | reports android.Paths |
| 90 | |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 91 | buildModuleReportZip bool |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | type lintOutputs struct { |
Colin Cross | 08dca38 | 2020-07-21 20:31:17 -0700 | [diff] [blame] | 95 | html android.Path |
| 96 | text android.Path |
| 97 | xml android.Path |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 98 | |
Colin Cross | 08dca38 | 2020-07-21 20:31:17 -0700 | [diff] [blame] | 99 | depSets LintDepSets |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Colin Cross | 08dca38 | 2020-07-21 20:31:17 -0700 | [diff] [blame] | 102 | type lintOutputsIntf interface { |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 103 | lintOutputs() *lintOutputs |
| 104 | } |
| 105 | |
Spandan Das | 17854f5 | 2022-01-14 21:19:14 +0000 | [diff] [blame] | 106 | type LintDepSetsIntf interface { |
Colin Cross | 08dca38 | 2020-07-21 20:31:17 -0700 | [diff] [blame] | 107 | LintDepSets() LintDepSets |
Jaewoong Jung | 476b9d6 | 2021-05-10 15:30:00 -0700 | [diff] [blame] | 108 | |
| 109 | // Methods used to propagate strict_updatability_linting values. |
Spandan Das | 17854f5 | 2022-01-14 21:19:14 +0000 | [diff] [blame] | 110 | GetStrictUpdatabilityLinting() bool |
| 111 | SetStrictUpdatabilityLinting(bool) |
Colin Cross | 08dca38 | 2020-07-21 20:31:17 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | type LintDepSets struct { |
| 115 | HTML, Text, XML *android.DepSet |
| 116 | } |
| 117 | |
| 118 | type LintDepSetsBuilder struct { |
| 119 | HTML, Text, XML *android.DepSetBuilder |
| 120 | } |
| 121 | |
| 122 | func NewLintDepSetBuilder() LintDepSetsBuilder { |
| 123 | return LintDepSetsBuilder{ |
| 124 | HTML: android.NewDepSetBuilder(android.POSTORDER), |
| 125 | Text: android.NewDepSetBuilder(android.POSTORDER), |
| 126 | XML: android.NewDepSetBuilder(android.POSTORDER), |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func (l LintDepSetsBuilder) Direct(html, text, xml android.Path) LintDepSetsBuilder { |
| 131 | l.HTML.Direct(html) |
| 132 | l.Text.Direct(text) |
| 133 | l.XML.Direct(xml) |
| 134 | return l |
| 135 | } |
| 136 | |
| 137 | func (l LintDepSetsBuilder) Transitive(depSets LintDepSets) LintDepSetsBuilder { |
| 138 | if depSets.HTML != nil { |
| 139 | l.HTML.Transitive(depSets.HTML) |
| 140 | } |
| 141 | if depSets.Text != nil { |
| 142 | l.Text.Transitive(depSets.Text) |
| 143 | } |
| 144 | if depSets.XML != nil { |
| 145 | l.XML.Transitive(depSets.XML) |
| 146 | } |
| 147 | return l |
| 148 | } |
| 149 | |
| 150 | func (l LintDepSetsBuilder) Build() LintDepSets { |
| 151 | return LintDepSets{ |
| 152 | HTML: l.HTML.Build(), |
| 153 | Text: l.Text.Build(), |
| 154 | XML: l.XML.Build(), |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func (l *linter) LintDepSets() LintDepSets { |
| 159 | return l.outputs.depSets |
| 160 | } |
| 161 | |
Spandan Das | 17854f5 | 2022-01-14 21:19:14 +0000 | [diff] [blame] | 162 | func (l *linter) GetStrictUpdatabilityLinting() bool { |
Jaewoong Jung | 476b9d6 | 2021-05-10 15:30:00 -0700 | [diff] [blame] | 163 | return BoolDefault(l.properties.Lint.Strict_updatability_linting, false) |
| 164 | } |
| 165 | |
Spandan Das | 17854f5 | 2022-01-14 21:19:14 +0000 | [diff] [blame] | 166 | func (l *linter) SetStrictUpdatabilityLinting(strictLinting bool) { |
Jaewoong Jung | 476b9d6 | 2021-05-10 15:30:00 -0700 | [diff] [blame] | 167 | l.properties.Lint.Strict_updatability_linting = &strictLinting |
| 168 | } |
| 169 | |
Spandan Das | 17854f5 | 2022-01-14 21:19:14 +0000 | [diff] [blame] | 170 | var _ LintDepSetsIntf = (*linter)(nil) |
Colin Cross | 08dca38 | 2020-07-21 20:31:17 -0700 | [diff] [blame] | 171 | |
| 172 | var _ lintOutputsIntf = (*linter)(nil) |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 173 | |
| 174 | func (l *linter) lintOutputs() *lintOutputs { |
| 175 | return &l.outputs |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | func (l *linter) enabled() bool { |
| 179 | return BoolDefault(l.properties.Lint.Enabled, true) |
| 180 | } |
| 181 | |
Colin Cross | 92e4b46 | 2020-06-18 15:56:48 -0700 | [diff] [blame] | 182 | func (l *linter) deps(ctx android.BottomUpMutatorContext) { |
| 183 | if !l.enabled() { |
| 184 | return |
| 185 | } |
| 186 | |
Colin Cross | 988dfcc | 2020-07-16 17:32:17 -0700 | [diff] [blame] | 187 | extraCheckModules := l.properties.Lint.Extra_check_modules |
| 188 | |
| 189 | if checkOnly := ctx.Config().Getenv("ANDROID_LINT_CHECK"); checkOnly != "" { |
| 190 | if checkOnlyModules := ctx.Config().Getenv("ANDROID_LINT_CHECK_EXTRA_MODULES"); checkOnlyModules != "" { |
| 191 | extraCheckModules = strings.Split(checkOnlyModules, ",") |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), |
| 196 | extraLintCheckTag, extraCheckModules...) |
Colin Cross | 92e4b46 | 2020-06-18 15:56:48 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Colin Cross | ad22bc2 | 2021-03-10 09:45:40 -0800 | [diff] [blame] | 199 | // lintPaths contains the paths to lint's inputs and outputs to make it easier to pass them |
| 200 | // around. |
Colin Cross | 31972dc | 2021-03-04 10:44:12 -0800 | [diff] [blame] | 201 | type lintPaths struct { |
| 202 | projectXML android.WritablePath |
| 203 | configXML android.WritablePath |
| 204 | cacheDir android.WritablePath |
| 205 | homeDir android.WritablePath |
| 206 | srcjarDir android.WritablePath |
Colin Cross | 31972dc | 2021-03-04 10:44:12 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Colin Cross | 9b93af4 | 2021-03-10 10:40:58 -0800 | [diff] [blame] | 209 | func lintRBEExecStrategy(ctx android.ModuleContext) string { |
| 210 | return ctx.Config().GetenvWithDefault("RBE_LINT_EXEC_STRATEGY", remoteexec.LocalExecStrategy) |
| 211 | } |
| 212 | |
Colin Cross | 31972dc | 2021-03-04 10:44:12 -0800 | [diff] [blame] | 213 | func (l *linter) writeLintProjectXML(ctx android.ModuleContext, rule *android.RuleBuilder) lintPaths { |
Colin Cross | 31972dc | 2021-03-04 10:44:12 -0800 | [diff] [blame] | 214 | projectXMLPath := android.PathForModuleOut(ctx, "lint", "project.xml") |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 215 | // Lint looks for a lint.xml file next to the project.xml file, give it one. |
Colin Cross | 31972dc | 2021-03-04 10:44:12 -0800 | [diff] [blame] | 216 | configXMLPath := android.PathForModuleOut(ctx, "lint", "lint.xml") |
| 217 | cacheDir := android.PathForModuleOut(ctx, "lint", "cache") |
| 218 | homeDir := android.PathForModuleOut(ctx, "lint", "home") |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 219 | |
Colin Cross | 1661aff | 2021-03-12 17:56:51 -0800 | [diff] [blame] | 220 | srcJarDir := android.PathForModuleOut(ctx, "lint", "srcjars") |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 221 | srcJarList := zipSyncCmd(ctx, rule, srcJarDir, l.srcJars) |
| 222 | |
| 223 | cmd := rule.Command(). |
Jaewoong Jung | 5a42025 | 2021-04-19 17:58:22 -0700 | [diff] [blame] | 224 | BuiltTool("lint_project_xml"). |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 225 | FlagWithOutput("--project_out ", projectXMLPath). |
| 226 | FlagWithOutput("--config_out ", configXMLPath). |
| 227 | FlagWithArg("--name ", ctx.ModuleName()) |
| 228 | |
| 229 | if l.library { |
| 230 | cmd.Flag("--library") |
| 231 | } |
| 232 | if l.test { |
| 233 | cmd.Flag("--test") |
| 234 | } |
| 235 | if l.manifest != nil { |
Colin Cross | 5bedfa2 | 2021-03-23 17:07:14 -0700 | [diff] [blame] | 236 | cmd.FlagWithInput("--manifest ", l.manifest) |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 237 | } |
| 238 | if l.mergedManifest != nil { |
Colin Cross | 5bedfa2 | 2021-03-23 17:07:14 -0700 | [diff] [blame] | 239 | cmd.FlagWithInput("--merged_manifest ", l.mergedManifest) |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Colin Cross | 5bedfa2 | 2021-03-23 17:07:14 -0700 | [diff] [blame] | 242 | // TODO(ccross): some of the files in l.srcs are generated sources and should be passed to |
| 243 | // lint separately. |
| 244 | srcsList := android.PathForModuleOut(ctx, "lint-srcs.list") |
| 245 | cmd.FlagWithRspFileInputList("--srcs ", srcsList, l.srcs) |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 246 | |
| 247 | cmd.FlagWithInput("--generated_srcs ", srcJarList) |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 248 | |
Colin Cross | 5bedfa2 | 2021-03-23 17:07:14 -0700 | [diff] [blame] | 249 | if len(l.resources) > 0 { |
| 250 | resourcesList := android.PathForModuleOut(ctx, "lint-resources.list") |
| 251 | cmd.FlagWithRspFileInputList("--resources ", resourcesList, l.resources) |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | if l.classes != nil { |
Colin Cross | 5bedfa2 | 2021-03-23 17:07:14 -0700 | [diff] [blame] | 255 | cmd.FlagWithInput("--classes ", l.classes) |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Colin Cross | 5bedfa2 | 2021-03-23 17:07:14 -0700 | [diff] [blame] | 258 | cmd.FlagForEachInput("--classpath ", l.classpath) |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 259 | |
Colin Cross | 5bedfa2 | 2021-03-23 17:07:14 -0700 | [diff] [blame] | 260 | cmd.FlagForEachInput("--extra_checks_jar ", l.extraLintCheckJars) |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 261 | |
Colin Cross | 1661aff | 2021-03-12 17:56:51 -0800 | [diff] [blame] | 262 | cmd.FlagWithArg("--root_dir ", "$PWD") |
Colin Cross | c31efeb | 2020-06-23 10:25:26 -0700 | [diff] [blame] | 263 | |
| 264 | // The cache tag in project.xml is relative to the root dir, or the project.xml file if |
| 265 | // the root dir is not set. |
| 266 | cmd.FlagWithArg("--cache_dir ", cacheDir.String()) |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 267 | |
| 268 | cmd.FlagWithInput("@", |
| 269 | android.PathForSource(ctx, "build/soong/java/lint_defaults.txt")) |
| 270 | |
Pedro Loureiro | f4a88b1 | 2021-02-25 16:23:22 +0000 | [diff] [blame] | 271 | cmd.FlagForEachArg("--error_check ", l.extraMainlineLintErrors) |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 272 | cmd.FlagForEachArg("--disable_check ", l.properties.Lint.Disabled_checks) |
| 273 | cmd.FlagForEachArg("--warning_check ", l.properties.Lint.Warning_checks) |
| 274 | cmd.FlagForEachArg("--error_check ", l.properties.Lint.Error_checks) |
| 275 | cmd.FlagForEachArg("--fatal_check ", l.properties.Lint.Fatal_checks) |
| 276 | |
Spandan Das | 17854f5 | 2022-01-14 21:19:14 +0000 | [diff] [blame] | 277 | if l.GetStrictUpdatabilityLinting() { |
Jaewoong Jung | 3c87b1d | 2021-04-22 11:01:36 -0700 | [diff] [blame] | 278 | // Verify the module does not baseline issues that endanger safe updatability. |
Jaewoong Jung | 48de883 | 2021-04-21 16:17:25 -0700 | [diff] [blame] | 279 | if baselinePath := l.getBaselineFilepath(ctx); baselinePath.Valid() { |
| 280 | cmd.FlagWithInput("--baseline ", baselinePath.Path()) |
| 281 | cmd.FlagForEachArg("--disallowed_issues ", updatabilityChecks) |
| 282 | } |
| 283 | } |
| 284 | |
Colin Cross | 31972dc | 2021-03-04 10:44:12 -0800 | [diff] [blame] | 285 | return lintPaths{ |
| 286 | projectXML: projectXMLPath, |
| 287 | configXML: configXMLPath, |
| 288 | cacheDir: cacheDir, |
| 289 | homeDir: homeDir, |
Colin Cross | 31972dc | 2021-03-04 10:44:12 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Liz Kammer | 20ebfb4 | 2020-07-28 11:32:07 -0700 | [diff] [blame] | 294 | // generateManifest adds a command to the rule to write a simple manifest that contains the |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 295 | // minSdkVersion and targetSdkVersion for modules (like java_library) that don't have a manifest. |
Colin Cross | 1661aff | 2021-03-12 17:56:51 -0800 | [diff] [blame] | 296 | func (l *linter) generateManifest(ctx android.ModuleContext, rule *android.RuleBuilder) android.WritablePath { |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 297 | manifestPath := android.PathForModuleOut(ctx, "lint", "AndroidManifest.xml") |
| 298 | |
| 299 | rule.Command().Text("("). |
| 300 | Text(`echo "<?xml version='1.0' encoding='utf-8'?>" &&`). |
| 301 | Text(`echo "<manifest xmlns:android='http://schemas.android.com/apk/res/android'" &&`). |
| 302 | Text(`echo " android:versionCode='1' android:versionName='1' >" &&`). |
Cole Faust | e5bf3fb | 2022-07-01 19:39:14 +0000 | [diff] [blame] | 303 | Textf(`echo " <uses-sdk android:minSdkVersion='%d' android:targetSdkVersion='%d'/>" &&`, |
| 304 | l.minSdkVersion, l.targetSdkVersion). |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 305 | Text(`echo "</manifest>"`). |
| 306 | Text(") >").Output(manifestPath) |
| 307 | |
| 308 | return manifestPath |
| 309 | } |
| 310 | |
Jaewoong Jung | 302c5b8 | 2021-04-19 08:54:36 -0700 | [diff] [blame] | 311 | func (l *linter) getBaselineFilepath(ctx android.ModuleContext) android.OptionalPath { |
| 312 | var lintBaseline android.OptionalPath |
| 313 | if lintFilename := proptools.StringDefault(l.properties.Lint.Baseline_filename, "lint-baseline.xml"); lintFilename != "" { |
| 314 | if String(l.properties.Lint.Baseline_filename) != "" { |
| 315 | // if manually specified, we require the file to exist |
| 316 | lintBaseline = android.OptionalPathForPath(android.PathForModuleSrc(ctx, lintFilename)) |
| 317 | } else { |
| 318 | lintBaseline = android.ExistentPathForSource(ctx, ctx.ModuleDir(), lintFilename) |
| 319 | } |
| 320 | } |
| 321 | return lintBaseline |
| 322 | } |
| 323 | |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 324 | func (l *linter) lint(ctx android.ModuleContext) { |
| 325 | if !l.enabled() { |
| 326 | return |
| 327 | } |
| 328 | |
Cole Faust | e5bf3fb | 2022-07-01 19:39:14 +0000 | [diff] [blame] | 329 | if l.minSdkVersion != l.compileSdkVersion { |
Jaewoong Jung | 79e6f6b | 2021-04-21 14:01:55 -0700 | [diff] [blame] | 330 | l.extraMainlineLintErrors = append(l.extraMainlineLintErrors, updatabilityChecks...) |
Orion Hodson | b816652 | 2022-08-15 20:23:38 +0100 | [diff] [blame^] | 331 | // Skip lint warning checks for NewApi warnings for libcore where they come from source |
| 332 | // files that reference the API they are adding (b/208656169). |
| 333 | if ctx.ModuleDir() != "libcore" { |
| 334 | _, filtered := android.FilterList(l.properties.Lint.Warning_checks, updatabilityChecks) |
| 335 | |
| 336 | if len(filtered) != 0 { |
| 337 | ctx.PropertyErrorf("lint.warning_checks", |
| 338 | "Can't treat %v checks as warnings if min_sdk_version is different from sdk_version.", filtered) |
| 339 | } |
Jaewoong Jung | 79e6f6b | 2021-04-21 14:01:55 -0700 | [diff] [blame] | 340 | } |
Orion Hodson | b816652 | 2022-08-15 20:23:38 +0100 | [diff] [blame^] | 341 | |
| 342 | _, filtered := android.FilterList(l.properties.Lint.Disabled_checks, updatabilityChecks) |
Jaewoong Jung | 79e6f6b | 2021-04-21 14:01:55 -0700 | [diff] [blame] | 343 | if len(filtered) != 0 { |
| 344 | ctx.PropertyErrorf("lint.disabled_checks", |
| 345 | "Can't disable %v checks if min_sdk_version is different from sdk_version.", filtered) |
| 346 | } |
Cole Faust | 3f64626 | 2022-06-29 14:58:03 -0700 | [diff] [blame] | 347 | |
| 348 | // TODO(b/238784089): Remove this workaround when the NewApi issues have been addressed in PermissionController |
| 349 | if ctx.ModuleName() == "PermissionController" { |
| 350 | l.extraMainlineLintErrors = android.FilterListPred(l.extraMainlineLintErrors, func(s string) bool { |
| 351 | return s != "NewApi" |
| 352 | }) |
| 353 | l.properties.Lint.Warning_checks = append(l.properties.Lint.Warning_checks, "NewApi") |
| 354 | } |
Pedro Loureiro | f4a88b1 | 2021-02-25 16:23:22 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Colin Cross | 92e4b46 | 2020-06-18 15:56:48 -0700 | [diff] [blame] | 357 | extraLintCheckModules := ctx.GetDirectDepsWithTag(extraLintCheckTag) |
| 358 | for _, extraLintCheckModule := range extraLintCheckModules { |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 359 | if ctx.OtherModuleHasProvider(extraLintCheckModule, JavaInfoProvider) { |
| 360 | dep := ctx.OtherModuleProvider(extraLintCheckModule, JavaInfoProvider).(JavaInfo) |
| 361 | l.extraLintCheckJars = append(l.extraLintCheckJars, dep.ImplementationAndResourcesJars...) |
Colin Cross | 92e4b46 | 2020-06-18 15:56:48 -0700 | [diff] [blame] | 362 | } else { |
| 363 | ctx.PropertyErrorf("lint.extra_check_modules", |
| 364 | "%s is not a java module", ctx.OtherModuleName(extraLintCheckModule)) |
| 365 | } |
| 366 | } |
| 367 | |
Colin Cross | 1661aff | 2021-03-12 17:56:51 -0800 | [diff] [blame] | 368 | rule := android.NewRuleBuilder(pctx, ctx). |
| 369 | Sbox(android.PathForModuleOut(ctx, "lint"), |
| 370 | android.PathForModuleOut(ctx, "lint.sbox.textproto")). |
| 371 | SandboxInputs() |
| 372 | |
| 373 | if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_LINT") { |
| 374 | pool := ctx.Config().GetenvWithDefault("RBE_LINT_POOL", "java16") |
| 375 | rule.Remoteable(android.RemoteRuleSupports{RBE: true}) |
| 376 | rule.Rewrapper(&remoteexec.REParams{ |
| 377 | Labels: map[string]string{"type": "tool", "name": "lint"}, |
| 378 | ExecStrategy: lintRBEExecStrategy(ctx), |
| 379 | ToolchainInputs: []string{config.JavaCmd(ctx).String()}, |
Colin Cross | 95fad7a | 2021-06-09 12:48:53 -0700 | [diff] [blame] | 380 | Platform: map[string]string{remoteexec.PoolKey: pool}, |
Colin Cross | 1661aff | 2021-03-12 17:56:51 -0800 | [diff] [blame] | 381 | }) |
| 382 | } |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 383 | |
| 384 | if l.manifest == nil { |
| 385 | manifest := l.generateManifest(ctx, rule) |
| 386 | l.manifest = manifest |
Colin Cross | 1661aff | 2021-03-12 17:56:51 -0800 | [diff] [blame] | 387 | rule.Temporary(manifest) |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Colin Cross | 31972dc | 2021-03-04 10:44:12 -0800 | [diff] [blame] | 390 | lintPaths := l.writeLintProjectXML(ctx, rule) |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 391 | |
Colin Cross | 1661aff | 2021-03-12 17:56:51 -0800 | [diff] [blame] | 392 | html := android.PathForModuleOut(ctx, "lint", "lint-report.html") |
| 393 | text := android.PathForModuleOut(ctx, "lint", "lint-report.txt") |
| 394 | xml := android.PathForModuleOut(ctx, "lint", "lint-report.xml") |
Colin Cross | 6b76c15 | 2021-09-09 09:36:25 -0700 | [diff] [blame] | 395 | baseline := android.PathForModuleOut(ctx, "lint", "lint-baseline.xml") |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 396 | |
Colin Cross | 08dca38 | 2020-07-21 20:31:17 -0700 | [diff] [blame] | 397 | depSetsBuilder := NewLintDepSetBuilder().Direct(html, text, xml) |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 398 | |
| 399 | ctx.VisitDirectDepsWithTag(staticLibTag, func(dep android.Module) { |
Spandan Das | 17854f5 | 2022-01-14 21:19:14 +0000 | [diff] [blame] | 400 | if depLint, ok := dep.(LintDepSetsIntf); ok { |
Colin Cross | 08dca38 | 2020-07-21 20:31:17 -0700 | [diff] [blame] | 401 | depSetsBuilder.Transitive(depLint.LintDepSets()) |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 402 | } |
| 403 | }) |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 404 | |
Colin Cross | 31972dc | 2021-03-04 10:44:12 -0800 | [diff] [blame] | 405 | rule.Command().Text("rm -rf").Flag(lintPaths.cacheDir.String()).Flag(lintPaths.homeDir.String()) |
| 406 | rule.Command().Text("mkdir -p").Flag(lintPaths.cacheDir.String()).Flag(lintPaths.homeDir.String()) |
Colin Cross | 5c113d1 | 2021-03-04 10:01:34 -0800 | [diff] [blame] | 407 | rule.Command().Text("rm -f").Output(html).Output(text).Output(xml) |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 408 | |
Pedro Loureiro | 18233a2 | 2021-06-08 18:11:21 +0000 | [diff] [blame] | 409 | var apiVersionsName, apiVersionsPrebuilt string |
Pedro Loureiro | ffb643f | 2021-07-05 13:53:36 +0000 | [diff] [blame] | 410 | if l.compileSdkKind == android.SdkModule || l.compileSdkKind == android.SdkSystemServer { |
| 411 | // When compiling an SDK module (or system server) we use the filtered |
| 412 | // database because otherwise lint's |
Pedro Loureiro | 18233a2 | 2021-06-08 18:11:21 +0000 | [diff] [blame] | 413 | // NewApi check produces too many false positives; This database excludes information |
| 414 | // about classes created in mainline modules hence removing those false positives. |
| 415 | apiVersionsName = "api_versions_public_filtered.xml" |
| 416 | apiVersionsPrebuilt = "prebuilts/sdk/current/public/data/api-versions-filtered.xml" |
| 417 | } else { |
| 418 | apiVersionsName = "api_versions.xml" |
| 419 | apiVersionsPrebuilt = "prebuilts/sdk/current/public/data/api-versions.xml" |
| 420 | } |
| 421 | |
Colin Cross | 8a6ed37 | 2020-07-06 11:45:51 -0700 | [diff] [blame] | 422 | var annotationsZipPath, apiVersionsXMLPath android.Path |
Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 423 | if ctx.Config().AlwaysUsePrebuiltSdks() { |
Colin Cross | 8a6ed37 | 2020-07-06 11:45:51 -0700 | [diff] [blame] | 424 | annotationsZipPath = android.PathForSource(ctx, "prebuilts/sdk/current/public/data/annotations.zip") |
Pedro Loureiro | 18233a2 | 2021-06-08 18:11:21 +0000 | [diff] [blame] | 425 | apiVersionsXMLPath = android.PathForSource(ctx, apiVersionsPrebuilt) |
Colin Cross | 8a6ed37 | 2020-07-06 11:45:51 -0700 | [diff] [blame] | 426 | } else { |
| 427 | annotationsZipPath = copiedAnnotationsZipPath(ctx) |
Pedro Loureiro | 18233a2 | 2021-06-08 18:11:21 +0000 | [diff] [blame] | 428 | apiVersionsXMLPath = copiedAPIVersionsXmlPath(ctx, apiVersionsName) |
Colin Cross | 8a6ed37 | 2020-07-06 11:45:51 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Colin Cross | 31972dc | 2021-03-04 10:44:12 -0800 | [diff] [blame] | 431 | cmd := rule.Command() |
| 432 | |
Pedro Loureiro | 70acc3d | 2021-04-06 17:49:19 +0000 | [diff] [blame] | 433 | cmd.Flag(`JAVA_OPTS="-Xmx3072m --add-opens java.base/java.util=ALL-UNNAMED"`). |
Colin Cross | 31972dc | 2021-03-04 10:44:12 -0800 | [diff] [blame] | 434 | FlagWithArg("ANDROID_SDK_HOME=", lintPaths.homeDir.String()). |
Colin Cross | 8a6ed37 | 2020-07-06 11:45:51 -0700 | [diff] [blame] | 435 | FlagWithInput("SDK_ANNOTATIONS=", annotationsZipPath). |
Colin Cross | 31972dc | 2021-03-04 10:44:12 -0800 | [diff] [blame] | 436 | FlagWithInput("LINT_OPTS=-DLINT_API_DATABASE=", apiVersionsXMLPath) |
| 437 | |
Colin Cross | 1661aff | 2021-03-12 17:56:51 -0800 | [diff] [blame] | 438 | cmd.BuiltTool("lint").ImplicitTool(ctx.Config().HostJavaToolPath(ctx, "lint.jar")). |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 439 | Flag("--quiet"). |
Colin Cross | 31972dc | 2021-03-04 10:44:12 -0800 | [diff] [blame] | 440 | FlagWithInput("--project ", lintPaths.projectXML). |
| 441 | FlagWithInput("--config ", lintPaths.configXML). |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 442 | FlagWithOutput("--html ", html). |
| 443 | FlagWithOutput("--text ", text). |
| 444 | FlagWithOutput("--xml ", xml). |
Cole Faust | e5bf3fb | 2022-07-01 19:39:14 +0000 | [diff] [blame] | 445 | FlagWithArg("--compile-sdk-version ", strconv.Itoa(l.compileSdkVersion)). |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 446 | FlagWithArg("--java-language-level ", l.javaLanguageLevel). |
| 447 | FlagWithArg("--kotlin-language-level ", l.kotlinLanguageLevel). |
| 448 | FlagWithArg("--url ", fmt.Sprintf(".=.,%s=out", android.PathForOutput(ctx).String())). |
| 449 | Flag("--exitcode"). |
| 450 | Flags(l.properties.Lint.Flags). |
Colin Cross | 31972dc | 2021-03-04 10:44:12 -0800 | [diff] [blame] | 451 | Implicit(annotationsZipPath). |
Colin Cross | 5bedfa2 | 2021-03-23 17:07:14 -0700 | [diff] [blame] | 452 | Implicit(apiVersionsXMLPath) |
Colin Cross | 988dfcc | 2020-07-16 17:32:17 -0700 | [diff] [blame] | 453 | |
Colin Cross | 1661aff | 2021-03-12 17:56:51 -0800 | [diff] [blame] | 454 | rule.Temporary(lintPaths.projectXML) |
| 455 | rule.Temporary(lintPaths.configXML) |
| 456 | |
Colin Cross | 988dfcc | 2020-07-16 17:32:17 -0700 | [diff] [blame] | 457 | if checkOnly := ctx.Config().Getenv("ANDROID_LINT_CHECK"); checkOnly != "" { |
| 458 | cmd.FlagWithArg("--check ", checkOnly) |
| 459 | } |
| 460 | |
Jaewoong Jung | 302c5b8 | 2021-04-19 08:54:36 -0700 | [diff] [blame] | 461 | lintBaseline := l.getBaselineFilepath(ctx) |
| 462 | if lintBaseline.Valid() { |
| 463 | cmd.FlagWithInput("--baseline ", lintBaseline.Path()) |
Pedro Loureiro | 5d190cc | 2021-02-15 15:41:33 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Colin Cross | 6b76c15 | 2021-09-09 09:36:25 -0700 | [diff] [blame] | 466 | cmd.FlagWithOutput("--write-reference-baseline ", baseline) |
| 467 | |
Colin Cross | 31972dc | 2021-03-04 10:44:12 -0800 | [diff] [blame] | 468 | cmd.Text("|| (").Text("if [ -e").Input(text).Text("]; then cat").Input(text).Text("; fi; exit 7)") |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 469 | |
Colin Cross | 31972dc | 2021-03-04 10:44:12 -0800 | [diff] [blame] | 470 | rule.Command().Text("rm -rf").Flag(lintPaths.cacheDir.String()).Flag(lintPaths.homeDir.String()) |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 471 | |
Colin Cross | ee4a8b7 | 2021-04-05 18:38:05 -0700 | [diff] [blame] | 472 | // The HTML output contains a date, remove it to make the output deterministic. |
| 473 | rule.Command().Text(`sed -i.tmp -e 's|Check performed at .*\(</nav>\)|\1|'`).Output(html) |
| 474 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 475 | rule.Build("lint", "lint") |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 476 | |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 477 | l.outputs = lintOutputs{ |
| 478 | html: html, |
| 479 | text: text, |
| 480 | xml: xml, |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 481 | |
Colin Cross | 08dca38 | 2020-07-21 20:31:17 -0700 | [diff] [blame] | 482 | depSets: depSetsBuilder.Build(), |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 483 | } |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 484 | |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 485 | if l.buildModuleReportZip { |
Colin Cross | 08dca38 | 2020-07-21 20:31:17 -0700 | [diff] [blame] | 486 | l.reports = BuildModuleLintReportZips(ctx, l.LintDepSets()) |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 487 | } |
| 488 | } |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 489 | |
Colin Cross | 08dca38 | 2020-07-21 20:31:17 -0700 | [diff] [blame] | 490 | func BuildModuleLintReportZips(ctx android.ModuleContext, depSets LintDepSets) android.Paths { |
| 491 | htmlList := depSets.HTML.ToSortedList() |
| 492 | textList := depSets.Text.ToSortedList() |
| 493 | xmlList := depSets.XML.ToSortedList() |
| 494 | |
| 495 | if len(htmlList) == 0 && len(textList) == 0 && len(xmlList) == 0 { |
| 496 | return nil |
| 497 | } |
| 498 | |
| 499 | htmlZip := android.PathForModuleOut(ctx, "lint-report-html.zip") |
| 500 | lintZip(ctx, htmlList, htmlZip) |
| 501 | |
| 502 | textZip := android.PathForModuleOut(ctx, "lint-report-text.zip") |
| 503 | lintZip(ctx, textList, textZip) |
| 504 | |
| 505 | xmlZip := android.PathForModuleOut(ctx, "lint-report-xml.zip") |
| 506 | lintZip(ctx, xmlList, xmlZip) |
| 507 | |
| 508 | return android.Paths{htmlZip, textZip, xmlZip} |
| 509 | } |
| 510 | |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 511 | type lintSingleton struct { |
| 512 | htmlZip android.WritablePath |
| 513 | textZip android.WritablePath |
| 514 | xmlZip android.WritablePath |
| 515 | } |
| 516 | |
| 517 | func (l *lintSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
| 518 | l.generateLintReportZips(ctx) |
| 519 | l.copyLintDependencies(ctx) |
| 520 | } |
| 521 | |
Pedro Loureiro | 18233a2 | 2021-06-08 18:11:21 +0000 | [diff] [blame] | 522 | func findModuleOrErr(ctx android.SingletonContext, moduleName string) android.Module { |
| 523 | var res android.Module |
| 524 | ctx.VisitAllModules(func(m android.Module) { |
| 525 | if ctx.ModuleName(m) == moduleName { |
| 526 | if res == nil { |
| 527 | res = m |
| 528 | } else { |
| 529 | ctx.Errorf("lint: multiple %s modules found: %s and %s", moduleName, |
| 530 | ctx.ModuleSubDir(m), ctx.ModuleSubDir(res)) |
| 531 | } |
| 532 | } |
| 533 | }) |
| 534 | return res |
| 535 | } |
| 536 | |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 537 | func (l *lintSingleton) copyLintDependencies(ctx android.SingletonContext) { |
Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 538 | if ctx.Config().AlwaysUsePrebuiltSdks() { |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 539 | return |
| 540 | } |
| 541 | |
Anton Hansson | 67cf60e | 2022-05-09 09:36:22 +0000 | [diff] [blame] | 542 | apiVersionsDb := findModuleOrErr(ctx, "api_versions_public") |
| 543 | if apiVersionsDb == nil { |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 544 | if !ctx.Config().AllowMissingDependencies() { |
Anton Hansson | 67cf60e | 2022-05-09 09:36:22 +0000 | [diff] [blame] | 545 | ctx.Errorf("lint: missing module api_versions_public") |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 546 | } |
| 547 | return |
| 548 | } |
| 549 | |
Anton Hansson | ea17a45 | 2022-05-09 09:42:17 +0000 | [diff] [blame] | 550 | sdkAnnotations := findModuleOrErr(ctx, "sdk-annotations.zip") |
| 551 | if sdkAnnotations == nil { |
| 552 | if !ctx.Config().AllowMissingDependencies() { |
| 553 | ctx.Errorf("lint: missing module sdk-annotations.zip") |
| 554 | } |
| 555 | return |
| 556 | } |
| 557 | |
Pedro Loureiro | 18233a2 | 2021-06-08 18:11:21 +0000 | [diff] [blame] | 558 | filteredDb := findModuleOrErr(ctx, "api-versions-xml-public-filtered") |
| 559 | if filteredDb == nil { |
| 560 | if !ctx.Config().AllowMissingDependencies() { |
| 561 | ctx.Errorf("lint: missing api-versions-xml-public-filtered") |
| 562 | } |
| 563 | return |
| 564 | } |
| 565 | |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 566 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 00d93b1 | 2021-03-04 10:00:09 -0800 | [diff] [blame] | 567 | Rule: android.CpIfChanged, |
Anton Hansson | ea17a45 | 2022-05-09 09:42:17 +0000 | [diff] [blame] | 568 | Input: android.OutputFileForModule(ctx, sdkAnnotations, ""), |
Colin Cross | 8a6ed37 | 2020-07-06 11:45:51 -0700 | [diff] [blame] | 569 | Output: copiedAnnotationsZipPath(ctx), |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 570 | }) |
| 571 | |
| 572 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 00d93b1 | 2021-03-04 10:00:09 -0800 | [diff] [blame] | 573 | Rule: android.CpIfChanged, |
Anton Hansson | 67cf60e | 2022-05-09 09:36:22 +0000 | [diff] [blame] | 574 | Input: android.OutputFileForModule(ctx, apiVersionsDb, ".api_versions.xml"), |
Pedro Loureiro | 18233a2 | 2021-06-08 18:11:21 +0000 | [diff] [blame] | 575 | Output: copiedAPIVersionsXmlPath(ctx, "api_versions.xml"), |
| 576 | }) |
| 577 | |
| 578 | ctx.Build(pctx, android.BuildParams{ |
| 579 | Rule: android.CpIfChanged, |
| 580 | Input: android.OutputFileForModule(ctx, filteredDb, ""), |
| 581 | Output: copiedAPIVersionsXmlPath(ctx, "api_versions_public_filtered.xml"), |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 582 | }) |
| 583 | } |
| 584 | |
Colin Cross | 8a6ed37 | 2020-07-06 11:45:51 -0700 | [diff] [blame] | 585 | func copiedAnnotationsZipPath(ctx android.PathContext) android.WritablePath { |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 586 | return android.PathForOutput(ctx, "lint", "annotations.zip") |
| 587 | } |
| 588 | |
Pedro Loureiro | 18233a2 | 2021-06-08 18:11:21 +0000 | [diff] [blame] | 589 | func copiedAPIVersionsXmlPath(ctx android.PathContext, name string) android.WritablePath { |
| 590 | return android.PathForOutput(ctx, "lint", name) |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | func (l *lintSingleton) generateLintReportZips(ctx android.SingletonContext) { |
Colin Cross | 8a6ed37 | 2020-07-06 11:45:51 -0700 | [diff] [blame] | 594 | if ctx.Config().UnbundledBuild() { |
| 595 | return |
| 596 | } |
| 597 | |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 598 | var outputs []*lintOutputs |
| 599 | var dirs []string |
| 600 | ctx.VisitAllModules(func(m android.Module) { |
Jingwen Chen | cda22c9 | 2020-11-23 00:22:30 -0500 | [diff] [blame] | 601 | if ctx.Config().KatiEnabled() && !m.ExportedToMake() { |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 602 | return |
| 603 | } |
| 604 | |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 605 | if apex, ok := m.(android.ApexModule); ok && apex.NotAvailableForPlatform() { |
| 606 | apexInfo := ctx.ModuleProvider(m, android.ApexInfoProvider).(android.ApexInfo) |
| 607 | if apexInfo.IsForPlatform() { |
| 608 | // There are stray platform variants of modules in apexes that are not available for |
| 609 | // the platform, and they sometimes can't be built. Don't depend on them. |
| 610 | return |
| 611 | } |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Colin Cross | 08dca38 | 2020-07-21 20:31:17 -0700 | [diff] [blame] | 614 | if l, ok := m.(lintOutputsIntf); ok { |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 615 | outputs = append(outputs, l.lintOutputs()) |
| 616 | } |
| 617 | }) |
| 618 | |
| 619 | dirs = android.SortedUniqueStrings(dirs) |
| 620 | |
| 621 | zip := func(outputPath android.WritablePath, get func(*lintOutputs) android.Path) { |
| 622 | var paths android.Paths |
| 623 | |
| 624 | for _, output := range outputs { |
Colin Cross | 08dca38 | 2020-07-21 20:31:17 -0700 | [diff] [blame] | 625 | if p := get(output); p != nil { |
| 626 | paths = append(paths, p) |
| 627 | } |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 630 | lintZip(ctx, paths, outputPath) |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | l.htmlZip = android.PathForOutput(ctx, "lint-report-html.zip") |
| 634 | zip(l.htmlZip, func(l *lintOutputs) android.Path { return l.html }) |
| 635 | |
| 636 | l.textZip = android.PathForOutput(ctx, "lint-report-text.zip") |
| 637 | zip(l.textZip, func(l *lintOutputs) android.Path { return l.text }) |
| 638 | |
| 639 | l.xmlZip = android.PathForOutput(ctx, "lint-report-xml.zip") |
| 640 | zip(l.xmlZip, func(l *lintOutputs) android.Path { return l.xml }) |
| 641 | |
| 642 | ctx.Phony("lint-check", l.htmlZip, l.textZip, l.xmlZip) |
| 643 | } |
| 644 | |
| 645 | func (l *lintSingleton) MakeVars(ctx android.MakeVarsContext) { |
Colin Cross | 8a6ed37 | 2020-07-06 11:45:51 -0700 | [diff] [blame] | 646 | if !ctx.Config().UnbundledBuild() { |
| 647 | ctx.DistForGoal("lint-check", l.htmlZip, l.textZip, l.xmlZip) |
| 648 | } |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | var _ android.SingletonMakeVarsProvider = (*lintSingleton)(nil) |
| 652 | |
| 653 | func init() { |
| 654 | android.RegisterSingletonType("lint", |
| 655 | func() android.Singleton { return &lintSingleton{} }) |
Jaewoong Jung | 476b9d6 | 2021-05-10 15:30:00 -0700 | [diff] [blame] | 656 | |
| 657 | registerLintBuildComponents(android.InitRegistrationContext) |
| 658 | } |
| 659 | |
| 660 | func registerLintBuildComponents(ctx android.RegistrationContext) { |
| 661 | ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) { |
| 662 | ctx.TopDown("enforce_strict_updatability_linting", enforceStrictUpdatabilityLintingMutator).Parallel() |
| 663 | }) |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 664 | } |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 665 | |
| 666 | func lintZip(ctx android.BuilderContext, paths android.Paths, outputPath android.WritablePath) { |
| 667 | paths = android.SortedUniquePaths(android.CopyOfPaths(paths)) |
| 668 | |
| 669 | sort.Slice(paths, func(i, j int) bool { |
| 670 | return paths[i].String() < paths[j].String() |
| 671 | }) |
| 672 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 673 | rule := android.NewRuleBuilder(pctx, ctx) |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 674 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 675 | rule.Command().BuiltTool("soong_zip"). |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 676 | FlagWithOutput("-o ", outputPath). |
| 677 | FlagWithArg("-C ", android.PathForIntermediates(ctx).String()). |
Colin Cross | 70c4741 | 2021-03-12 17:48:14 -0800 | [diff] [blame] | 678 | FlagWithRspFileInputList("-r ", outputPath.ReplaceExtension(ctx, "rsp"), paths) |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 679 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 680 | rule.Build(outputPath.Base(), outputPath.Base()) |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 681 | } |
Jaewoong Jung | 476b9d6 | 2021-05-10 15:30:00 -0700 | [diff] [blame] | 682 | |
| 683 | // Enforce the strict updatability linting to all applicable transitive dependencies. |
| 684 | func enforceStrictUpdatabilityLintingMutator(ctx android.TopDownMutatorContext) { |
| 685 | m := ctx.Module() |
Spandan Das | 17854f5 | 2022-01-14 21:19:14 +0000 | [diff] [blame] | 686 | if d, ok := m.(LintDepSetsIntf); ok && d.GetStrictUpdatabilityLinting() { |
Jaewoong Jung | 476b9d6 | 2021-05-10 15:30:00 -0700 | [diff] [blame] | 687 | ctx.VisitDirectDepsWithTag(staticLibTag, func(d android.Module) { |
Spandan Das | 17854f5 | 2022-01-14 21:19:14 +0000 | [diff] [blame] | 688 | if a, ok := d.(LintDepSetsIntf); ok { |
| 689 | a.SetStrictUpdatabilityLinting(true) |
Jaewoong Jung | 476b9d6 | 2021-05-10 15:30:00 -0700 | [diff] [blame] | 690 | } |
| 691 | }) |
| 692 | } |
| 693 | } |