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