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