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