diff options
Diffstat (limited to 'java/lint.go')
| -rw-r--r-- | java/lint.go | 106 |
1 files changed, 51 insertions, 55 deletions
diff --git a/java/lint.go b/java/lint.go index 40ef48416..2eea07d31 100644 --- a/java/lint.go +++ b/java/lint.go @@ -17,7 +17,6 @@ package java import ( "fmt" "sort" - "strconv" "strings" "github.com/google/blueprint/proptools" @@ -56,7 +55,8 @@ type LintProperties struct { // Modules that provide extra lint checks Extra_check_modules []string - // Name of the file that lint uses as the baseline. Defaults to "lint-baseline.xml". + // The lint baseline file to use. If specified, lint warnings listed in this file will be + // suppressed during lint checks. Baseline_filename *string // If true, baselining updatability lint checks (e.g. NewApi) is prohibited. Defaults to false. @@ -66,6 +66,10 @@ type LintProperties struct { // This will be true by default for test module types, false otherwise. // If soong gets support for testonly, this flag should be replaced with that. Test *bool + + // Whether to ignore the exit code of Android lint. This is the --exit_code + // option. Defaults to false. + Suppress_exit_code *bool } } @@ -80,15 +84,16 @@ type linter struct { classes android.Path extraLintCheckJars android.Paths library bool - minSdkVersion int - targetSdkVersion int - compileSdkVersion int + minSdkVersion android.ApiLevel + targetSdkVersion android.ApiLevel + compileSdkVersion android.ApiLevel compileSdkKind android.SdkKind javaLanguageLevel string kotlinLanguageLevel string outputs lintOutputs properties LintProperties extraMainlineLintErrors []string + compile_data android.Paths reports android.Paths @@ -117,18 +122,18 @@ type LintDepSetsIntf interface { } type LintDepSets struct { - HTML, Text, XML *android.DepSet + HTML, Text, XML *android.DepSet[android.Path] } type LintDepSetsBuilder struct { - HTML, Text, XML *android.DepSetBuilder + HTML, Text, XML *android.DepSetBuilder[android.Path] } func NewLintDepSetBuilder() LintDepSetsBuilder { return LintDepSetsBuilder{ - HTML: android.NewDepSetBuilder(android.POSTORDER), - Text: android.NewDepSetBuilder(android.POSTORDER), - XML: android.NewDepSetBuilder(android.POSTORDER), + HTML: android.NewDepSetBuilder[android.Path](android.POSTORDER), + Text: android.NewDepSetBuilder[android.Path](android.POSTORDER), + XML: android.NewDepSetBuilder[android.Path](android.POSTORDER), } } @@ -314,25 +319,19 @@ func (l *linter) writeLintProjectXML(ctx android.ModuleContext, rule *android.Ru cmd.FlagWithInput("@", android.PathForSource(ctx, "build/soong/java/lint_defaults.txt")) - if l.compileSdkKind == android.SdkPublic { - cmd.FlagForEachArg("--error_check ", l.extraMainlineLintErrors) - } else { - // TODO(b/268261262): Remove this branch. We're demoting NewApi to a warning due to pre-existing issues that need to be fixed. - cmd.FlagForEachArg("--warning_check ", l.extraMainlineLintErrors) - } + cmd.FlagForEachArg("--error_check ", l.extraMainlineLintErrors) cmd.FlagForEachArg("--disable_check ", l.properties.Lint.Disabled_checks) cmd.FlagForEachArg("--warning_check ", l.properties.Lint.Warning_checks) cmd.FlagForEachArg("--error_check ", l.properties.Lint.Error_checks) cmd.FlagForEachArg("--fatal_check ", l.properties.Lint.Fatal_checks) - // TODO(b/193460475): Re-enable strict updatability linting - //if l.GetStrictUpdatabilityLinting() { - // // Verify the module does not baseline issues that endanger safe updatability. - // if baselinePath := l.getBaselineFilepath(ctx); baselinePath.Valid() { - // cmd.FlagWithInput("--baseline ", baselinePath.Path()) - // cmd.FlagForEachArg("--disallowed_issues ", updatabilityChecks) - // } - //} + if l.GetStrictUpdatabilityLinting() { + // Verify the module does not baseline issues that endanger safe updatability. + if l.properties.Lint.Baseline_filename != nil { + cmd.FlagWithInput("--baseline ", android.PathForModuleSrc(ctx, *l.properties.Lint.Baseline_filename)) + cmd.FlagForEachArg("--disallowed_issues ", updatabilityChecks) + } + } return lintPaths{ projectXML: projectXMLPath, @@ -352,33 +351,26 @@ func (l *linter) generateManifest(ctx android.ModuleContext, rule *android.RuleB Text(`echo "<?xml version='1.0' encoding='utf-8'?>" &&`). Text(`echo "<manifest xmlns:android='http://schemas.android.com/apk/res/android'" &&`). Text(`echo " android:versionCode='1' android:versionName='1' >" &&`). - Textf(`echo " <uses-sdk android:minSdkVersion='%d' android:targetSdkVersion='%d'/>" &&`, - l.minSdkVersion, l.targetSdkVersion). + Textf(`echo " <uses-sdk android:minSdkVersion='%s' android:targetSdkVersion='%s'/>" &&`, + l.minSdkVersion.String(), l.targetSdkVersion.String()). Text(`echo "</manifest>"`). Text(") >").Output(manifestPath) return manifestPath } -func (l *linter) getBaselineFilepath(ctx android.ModuleContext) android.OptionalPath { - var lintBaseline android.OptionalPath - if lintFilename := proptools.StringDefault(l.properties.Lint.Baseline_filename, "lint-baseline.xml"); lintFilename != "" { - if String(l.properties.Lint.Baseline_filename) != "" { - // if manually specified, we require the file to exist - lintBaseline = android.OptionalPathForPath(android.PathForModuleSrc(ctx, lintFilename)) - } else { - lintBaseline = android.ExistentPathForSource(ctx, ctx.ModuleDir(), lintFilename) - } - } - return lintBaseline -} - func (l *linter) lint(ctx android.ModuleContext) { if !l.enabled() { return } - if l.minSdkVersion != l.compileSdkVersion { + for _, flag := range l.properties.Lint.Flags { + if strings.Contains(flag, "--disable") || strings.Contains(flag, "--enable") || strings.Contains(flag, "--check") { + ctx.PropertyErrorf("lint.flags", "Don't use --disable, --enable, or --check in the flags field, instead use the dedicated disabled_checks, warning_checks, error_checks, or fatal_checks fields") + } + } + + if l.minSdkVersion.CompareTo(l.compileSdkVersion) == -1 { l.extraMainlineLintErrors = append(l.extraMainlineLintErrors, updatabilityChecks...) // Skip lint warning checks for NewApi warnings for libcore where they come from source // files that reference the API they are adding (b/208656169). @@ -408,8 +400,7 @@ func (l *linter) lint(ctx android.ModuleContext) { extraLintCheckModules := ctx.GetDirectDepsWithTag(extraLintCheckTag) for _, extraLintCheckModule := range extraLintCheckModules { - if ctx.OtherModuleHasProvider(extraLintCheckModule, JavaInfoProvider) { - dep := ctx.OtherModuleProvider(extraLintCheckModule, JavaInfoProvider).(JavaInfo) + if dep, ok := android.OtherModuleProvider(ctx, extraLintCheckModule, JavaInfoProvider); ok { l.extraLintCheckJars = append(l.extraLintCheckJars, dep.ImplementationAndResourcesJars...) } else { ctx.PropertyErrorf("lint.extra_check_modules", @@ -444,7 +435,7 @@ func (l *linter) lint(ctx android.ModuleContext) { srcsList := android.PathForModuleOut(ctx, "lint", "lint-srcs.list") srcsListRsp := android.PathForModuleOut(ctx, "lint-srcs.list.rsp") - rule.Command().Text("cp").FlagWithRspFileInputList("", srcsListRsp, l.srcs).Output(srcsList) + rule.Command().Text("cp").FlagWithRspFileInputList("", srcsListRsp, l.srcs).Output(srcsList).Implicits(l.compile_data) lintPaths := l.writeLintProjectXML(ctx, rule, srcsList) @@ -487,12 +478,13 @@ func (l *linter) lint(ctx android.ModuleContext) { cmd.BuiltTool("lint").ImplicitTool(ctx.Config().HostJavaToolPath(ctx, "lint.jar")). Flag("--quiet"). + Flag("--include-aosp-issues"). FlagWithInput("--project ", lintPaths.projectXML). FlagWithInput("--config ", lintPaths.configXML). FlagWithOutput("--html ", html). FlagWithOutput("--text ", text). FlagWithOutput("--xml ", xml). - FlagWithArg("--compile-sdk-version ", strconv.Itoa(l.compileSdkVersion)). + FlagWithArg("--compile-sdk-version ", l.compileSdkVersion.String()). FlagWithArg("--java-language-level ", l.javaLanguageLevel). FlagWithArg("--kotlin-language-level ", l.kotlinLanguageLevel). FlagWithArg("--url ", fmt.Sprintf(".=.,%s=out", android.PathForOutput(ctx).String())). @@ -504,7 +496,8 @@ func (l *linter) lint(ctx android.ModuleContext) { rule.Temporary(lintPaths.projectXML) rule.Temporary(lintPaths.configXML) - if exitCode := ctx.Config().Getenv("ANDROID_LINT_SUPPRESS_EXIT_CODE"); exitCode == "" { + suppressExitCode := BoolDefault(l.properties.Lint.Suppress_exit_code, false) + if exitCode := ctx.Config().Getenv("ANDROID_LINT_SUPPRESS_EXIT_CODE"); exitCode == "" && !suppressExitCode { cmd.Flag("--exitcode") } @@ -512,9 +505,8 @@ func (l *linter) lint(ctx android.ModuleContext) { cmd.FlagWithArg("--check ", checkOnly) } - lintBaseline := l.getBaselineFilepath(ctx) - if lintBaseline.Valid() { - cmd.FlagWithInput("--baseline ", lintBaseline.Path()) + if l.properties.Lint.Baseline_filename != nil { + cmd.FlagWithInput("--baseline ", android.PathForModuleSrc(ctx, *l.properties.Lint.Baseline_filename)) } cmd.FlagWithOutput("--write-reference-baseline ", referenceBaseline) @@ -550,12 +542,16 @@ func (l *linter) lint(ctx android.ModuleContext) { if l.buildModuleReportZip { l.reports = BuildModuleLintReportZips(ctx, l.LintDepSets()) } + + // Create a per-module phony target to run the lint check. + phonyName := ctx.ModuleName() + "-lint" + ctx.Phony(phonyName, xml) } func BuildModuleLintReportZips(ctx android.ModuleContext, depSets LintDepSets) android.Paths { - htmlList := depSets.HTML.ToSortedList() - textList := depSets.Text.ToSortedList() - xmlList := depSets.XML.ToSortedList() + htmlList := android.SortedUniquePaths(depSets.HTML.ToList()) + textList := android.SortedUniquePaths(depSets.Text.ToList()) + xmlList := android.SortedUniquePaths(depSets.XML.ToList()) if len(htmlList) == 0 && len(textList) == 0 && len(xmlList) == 0 { return nil @@ -610,7 +606,7 @@ func (l *lintSingleton) copyLintDependencies(ctx android.SingletonContext) { apiVersionsDb := findModuleOrErr(ctx, files.apiVersionsModule) if apiVersionsDb == nil { if !ctx.Config().AllowMissingDependencies() { - ctx.Errorf("lint: missing module api_versions_public") + ctx.Errorf("lint: missing module %s", files.apiVersionsModule) } return } @@ -618,7 +614,7 @@ func (l *lintSingleton) copyLintDependencies(ctx android.SingletonContext) { sdkAnnotations := findModuleOrErr(ctx, files.annotationsModule) if sdkAnnotations == nil { if !ctx.Config().AllowMissingDependencies() { - ctx.Errorf("lint: missing module sdk-annotations.zip") + ctx.Errorf("lint: missing module %s", files.annotationsModule) } return } @@ -654,7 +650,7 @@ func (l *lintSingleton) generateLintReportZips(ctx android.SingletonContext) { } if apex, ok := m.(android.ApexModule); ok && apex.NotAvailableForPlatform() { - apexInfo := ctx.ModuleProvider(m, android.ApexInfoProvider).(android.ApexInfo) + apexInfo, _ := android.SingletonModuleProvider(ctx, m, android.ApexInfoProvider) if apexInfo.IsForPlatform() { // There are stray platform variants of modules in apexes that are not available for // the platform, and they sometimes can't be built. Don't depend on them. @@ -705,7 +701,7 @@ func (l *lintSingleton) MakeVars(ctx android.MakeVarsContext) { var _ android.SingletonMakeVarsProvider = (*lintSingleton)(nil) func init() { - android.RegisterSingletonType("lint", + android.RegisterParallelSingletonType("lint", func() android.Singleton { return &lintSingleton{} }) registerLintBuildComponents(android.InitRegistrationContext) |