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