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