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