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