blob: 90d9896043f14887a9e5ba85a11369edc75d3fd6 [file] [log] [blame]
Colin Cross2207f872021-03-24 12:39:08 -07001// Copyright 2021 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
15package java
16
17import (
18 "fmt"
19 "strings"
20
21 "github.com/google/blueprint/proptools"
22
23 "android/soong/android"
24 "android/soong/java/config"
25 "android/soong/remoteexec"
26)
27
28func init() {
29 RegisterStubsBuildComponents(android.InitRegistrationContext)
30}
31
32func RegisterStubsBuildComponents(ctx android.RegistrationContext) {
33 ctx.RegisterModuleType("stubs_defaults", StubsDefaultsFactory)
34
35 ctx.RegisterModuleType("droidstubs", DroidstubsFactory)
36 ctx.RegisterModuleType("droidstubs_host", DroidstubsHostFactory)
37
38 ctx.RegisterModuleType("prebuilt_stubs_sources", PrebuiltStubsSourcesFactory)
39}
40
41//
42// Droidstubs
43//
44type Droidstubs struct {
45 Javadoc
46 android.SdkBase
47
48 properties DroidstubsProperties
49 apiFile android.WritablePath
50 apiXmlFile android.WritablePath
51 lastReleasedApiXmlFile android.WritablePath
52 privateApiFile android.WritablePath
53 removedApiFile android.WritablePath
54 removedDexApiFile android.WritablePath
55 nullabilityWarningsFile android.WritablePath
56
57 checkCurrentApiTimestamp android.WritablePath
58 updateCurrentApiTimestamp android.WritablePath
59 checkLastReleasedApiTimestamp android.WritablePath
60 apiLintTimestamp android.WritablePath
61 apiLintReport android.WritablePath
62
63 checkNullabilityWarningsTimestamp android.WritablePath
64
65 annotationsZip android.WritablePath
66 apiVersionsXml android.WritablePath
67
68 apiFilePath android.Path
69 removedApiFilePath android.Path
70
71 metadataZip android.WritablePath
72 metadataDir android.WritablePath
73}
74
75type DroidstubsProperties struct {
76 // The generated public API filename by Metalava, defaults to <module>_api.txt
77 Api_filename *string
78
79 // the generated removed API filename by Metalava, defaults to <module>_removed.txt
80 Removed_api_filename *string
81
82 // the generated removed Dex API filename by Metalava.
83 Removed_dex_api_filename *string
84
85 Check_api struct {
86 Last_released ApiToCheck
87
88 Current ApiToCheck
89
90 Api_lint struct {
91 Enabled *bool
92
93 // If set, performs api_lint on any new APIs not found in the given signature file
94 New_since *string `android:"path"`
95
96 // If not blank, path to the baseline txt file for approved API lint violations.
97 Baseline_file *string `android:"path"`
98 }
99 }
100
101 // user can specify the version of previous released API file in order to do compatibility check.
102 Previous_api *string `android:"path"`
103
104 // is set to true, Metalava will allow framework SDK to contain annotations.
105 Annotations_enabled *bool
106
107 // a list of top-level directories containing files to merge qualifier annotations (i.e. those intended to be included in the stubs written) from.
108 Merge_annotations_dirs []string
109
110 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
111 Merge_inclusion_annotations_dirs []string
112
113 // a file containing a list of classes to do nullability validation for.
114 Validate_nullability_from_list *string
115
116 // a file containing expected warnings produced by validation of nullability annotations.
117 Check_nullability_warnings *string
118
119 // if set to true, allow Metalava to generate doc_stubs source files. Defaults to false.
120 Create_doc_stubs *bool
121
122 // if set to true, cause Metalava to output Javadoc comments in the stubs source files. Defaults to false.
123 // Has no effect if create_doc_stubs: true.
124 Output_javadoc_comments *bool
125
126 // if set to false then do not write out stubs. Defaults to true.
127 //
128 // TODO(b/146727827): Remove capability when we do not need to generate stubs and API separately.
129 Generate_stubs *bool
130
131 // if set to true, provides a hint to the build system that this rule uses a lot of memory,
132 // whicih can be used for scheduling purposes
133 High_mem *bool
134
135 // is set to true, Metalava will allow framework SDK to contain API levels annotations.
136 Api_levels_annotations_enabled *bool
137
138 // the dirs which Metalava extracts API levels annotations from.
139 Api_levels_annotations_dirs []string
140
141 // the filename which Metalava extracts API levels annotations from. Defaults to android.jar.
142 Api_levels_jar_filename *string
143
144 // if set to true, collect the values used by the Dev tools and
145 // write them in files packaged with the SDK. Defaults to false.
146 Write_sdk_values *bool
147}
148
149// droidstubs passes sources files through Metalava to generate stub .java files that only contain the API to be
150// documented, filtering out hidden classes and methods. The resulting .java files are intended to be passed to
151// a droiddoc module to generate documentation.
152func DroidstubsFactory() android.Module {
153 module := &Droidstubs{}
154
155 module.AddProperties(&module.properties,
156 &module.Javadoc.properties)
157
158 InitDroiddocModule(module, android.HostAndDeviceSupported)
159 android.InitSdkAwareModule(module)
160 return module
161}
162
163// droidstubs_host passes sources files through Metalava to generate stub .java files that only contain the API
164// to be documented, filtering out hidden classes and methods. The resulting .java files are intended to be
165// passed to a droiddoc_host module to generate documentation. Use a droidstubs_host instead of a droidstubs
166// module when symbols needed by the source files are provided by java_library_host modules.
167func DroidstubsHostFactory() android.Module {
168 module := &Droidstubs{}
169
170 module.AddProperties(&module.properties,
171 &module.Javadoc.properties)
172
173 InitDroiddocModule(module, android.HostSupported)
174 return module
175}
176
177func (d *Droidstubs) OutputFiles(tag string) (android.Paths, error) {
178 switch tag {
179 case "":
180 return android.Paths{d.stubsSrcJar}, nil
181 case ".docs.zip":
182 return android.Paths{d.docZip}, nil
183 case ".api.txt", android.DefaultDistTag:
184 // This is the default dist path for dist properties that have no tag property.
185 return android.Paths{d.apiFilePath}, nil
186 case ".removed-api.txt":
187 return android.Paths{d.removedApiFilePath}, nil
188 case ".annotations.zip":
189 return android.Paths{d.annotationsZip}, nil
190 case ".api_versions.xml":
191 return android.Paths{d.apiVersionsXml}, nil
192 default:
193 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
194 }
195}
196
197func (d *Droidstubs) ApiFilePath() android.Path {
198 return d.apiFilePath
199}
200
201func (d *Droidstubs) RemovedApiFilePath() android.Path {
202 return d.removedApiFilePath
203}
204
205func (d *Droidstubs) StubsSrcJar() android.Path {
206 return d.stubsSrcJar
207}
208
209var metalavaMergeAnnotationsDirTag = dependencyTag{name: "metalava-merge-annotations-dir"}
210var metalavaMergeInclusionAnnotationsDirTag = dependencyTag{name: "metalava-merge-inclusion-annotations-dir"}
211var metalavaAPILevelsAnnotationsDirTag = dependencyTag{name: "metalava-api-levels-annotations-dir"}
212
213func (d *Droidstubs) DepsMutator(ctx android.BottomUpMutatorContext) {
214 d.Javadoc.addDeps(ctx)
215
216 if len(d.properties.Merge_annotations_dirs) != 0 {
217 for _, mergeAnnotationsDir := range d.properties.Merge_annotations_dirs {
218 ctx.AddDependency(ctx.Module(), metalavaMergeAnnotationsDirTag, mergeAnnotationsDir)
219 }
220 }
221
222 if len(d.properties.Merge_inclusion_annotations_dirs) != 0 {
223 for _, mergeInclusionAnnotationsDir := range d.properties.Merge_inclusion_annotations_dirs {
224 ctx.AddDependency(ctx.Module(), metalavaMergeInclusionAnnotationsDirTag, mergeInclusionAnnotationsDir)
225 }
226 }
227
228 if len(d.properties.Api_levels_annotations_dirs) != 0 {
229 for _, apiLevelsAnnotationsDir := range d.properties.Api_levels_annotations_dirs {
230 ctx.AddDependency(ctx.Module(), metalavaAPILevelsAnnotationsDirTag, apiLevelsAnnotationsDir)
231 }
232 }
233}
234
235func (d *Droidstubs) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.OptionalPath) {
236 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
237 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
238 String(d.properties.Api_filename) != "" {
239 filename := proptools.StringDefault(d.properties.Api_filename, ctx.ModuleName()+"_api.txt")
Colin Crosscb77f752021-03-24 12:04:44 -0700240 d.apiFile = android.PathForModuleOut(ctx, "metalava", filename)
Colin Cross2207f872021-03-24 12:39:08 -0700241 cmd.FlagWithOutput("--api ", d.apiFile)
242 d.apiFilePath = d.apiFile
243 } else if sourceApiFile := proptools.String(d.properties.Check_api.Current.Api_file); sourceApiFile != "" {
244 // If check api is disabled then make the source file available for export.
245 d.apiFilePath = android.PathForModuleSrc(ctx, sourceApiFile)
246 }
247
248 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
249 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
250 String(d.properties.Removed_api_filename) != "" {
251 filename := proptools.StringDefault(d.properties.Removed_api_filename, ctx.ModuleName()+"_removed.txt")
Colin Crosscb77f752021-03-24 12:04:44 -0700252 d.removedApiFile = android.PathForModuleOut(ctx, "metalava", filename)
Colin Cross2207f872021-03-24 12:39:08 -0700253 cmd.FlagWithOutput("--removed-api ", d.removedApiFile)
254 d.removedApiFilePath = d.removedApiFile
255 } else if sourceRemovedApiFile := proptools.String(d.properties.Check_api.Current.Removed_api_file); sourceRemovedApiFile != "" {
256 // If check api is disabled then make the source removed api file available for export.
257 d.removedApiFilePath = android.PathForModuleSrc(ctx, sourceRemovedApiFile)
258 }
259
260 if String(d.properties.Removed_dex_api_filename) != "" {
Colin Crosscb77f752021-03-24 12:04:44 -0700261 d.removedDexApiFile = android.PathForModuleOut(ctx, "metalava", String(d.properties.Removed_dex_api_filename))
Colin Cross2207f872021-03-24 12:39:08 -0700262 cmd.FlagWithOutput("--removed-dex-api ", d.removedDexApiFile)
263 }
264
265 if Bool(d.properties.Write_sdk_values) {
Colin Crosscb77f752021-03-24 12:04:44 -0700266 d.metadataDir = android.PathForModuleOut(ctx, "metalava", "metadata")
Colin Cross2207f872021-03-24 12:39:08 -0700267 cmd.FlagWithArg("--sdk-values ", d.metadataDir.String())
268 }
269
270 if stubsDir.Valid() {
271 if Bool(d.properties.Create_doc_stubs) {
272 cmd.FlagWithArg("--doc-stubs ", stubsDir.String())
273 } else {
274 cmd.FlagWithArg("--stubs ", stubsDir.String())
275 if !Bool(d.properties.Output_javadoc_comments) {
276 cmd.Flag("--exclude-documentation-from-stubs")
277 }
278 }
279 }
280}
281
282func (d *Droidstubs) annotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
283 if Bool(d.properties.Annotations_enabled) {
284 cmd.Flag("--include-annotations")
285
Andrei Onea4985e512021-04-29 16:29:34 +0100286 cmd.FlagWithArg("--exclude-annotation ", "androidx.annotation.RequiresApi")
287
Colin Cross2207f872021-03-24 12:39:08 -0700288 validatingNullability :=
Colin Crossbc139922021-03-25 18:33:16 -0700289 strings.Contains(String(d.Javadoc.properties.Args), "--validate-nullability-from-merged-stubs") ||
Colin Cross2207f872021-03-24 12:39:08 -0700290 String(d.properties.Validate_nullability_from_list) != ""
291
292 migratingNullability := String(d.properties.Previous_api) != ""
293 if migratingNullability {
294 previousApi := android.PathForModuleSrc(ctx, String(d.properties.Previous_api))
295 cmd.FlagWithInput("--migrate-nullness ", previousApi)
296 }
297
298 if s := String(d.properties.Validate_nullability_from_list); s != "" {
299 cmd.FlagWithInput("--validate-nullability-from-list ", android.PathForModuleSrc(ctx, s))
300 }
301
302 if validatingNullability {
Colin Crosscb77f752021-03-24 12:04:44 -0700303 d.nullabilityWarningsFile = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"_nullability_warnings.txt")
Colin Cross2207f872021-03-24 12:39:08 -0700304 cmd.FlagWithOutput("--nullability-warnings-txt ", d.nullabilityWarningsFile)
305 }
306
Colin Crosscb77f752021-03-24 12:04:44 -0700307 d.annotationsZip = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"_annotations.zip")
Colin Cross2207f872021-03-24 12:39:08 -0700308 cmd.FlagWithOutput("--extract-annotations ", d.annotationsZip)
309
310 if len(d.properties.Merge_annotations_dirs) != 0 {
311 d.mergeAnnoDirFlags(ctx, cmd)
312 }
313
314 // TODO(tnorbye): find owners to fix these warnings when annotation was enabled.
315 cmd.FlagWithArg("--hide ", "HiddenTypedefConstant").
316 FlagWithArg("--hide ", "SuperfluousPrefix").
317 FlagWithArg("--hide ", "AnnotationExtraction")
318 }
319}
320
321func (d *Droidstubs) mergeAnnoDirFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
322 ctx.VisitDirectDepsWithTag(metalavaMergeAnnotationsDirTag, func(m android.Module) {
323 if t, ok := m.(*ExportedDroiddocDir); ok {
324 cmd.FlagWithArg("--merge-qualifier-annotations ", t.dir.String()).Implicits(t.deps)
325 } else {
326 ctx.PropertyErrorf("merge_annotations_dirs",
327 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
328 }
329 })
330}
331
332func (d *Droidstubs) inclusionAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
333 ctx.VisitDirectDepsWithTag(metalavaMergeInclusionAnnotationsDirTag, func(m android.Module) {
334 if t, ok := m.(*ExportedDroiddocDir); ok {
335 cmd.FlagWithArg("--merge-inclusion-annotations ", t.dir.String()).Implicits(t.deps)
336 } else {
337 ctx.PropertyErrorf("merge_inclusion_annotations_dirs",
338 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
339 }
340 })
341}
342
343func (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
344 if !Bool(d.properties.Api_levels_annotations_enabled) {
345 return
346 }
347
Colin Crosscb77f752021-03-24 12:04:44 -0700348 d.apiVersionsXml = android.PathForModuleOut(ctx, "metalava", "api-versions.xml")
Colin Cross2207f872021-03-24 12:39:08 -0700349
350 if len(d.properties.Api_levels_annotations_dirs) == 0 {
351 ctx.PropertyErrorf("api_levels_annotations_dirs",
352 "has to be non-empty if api levels annotations was enabled!")
353 }
354
355 cmd.FlagWithOutput("--generate-api-levels ", d.apiVersionsXml)
356 cmd.FlagWithInput("--apply-api-levels ", d.apiVersionsXml)
357 cmd.FlagWithArg("--current-version ", ctx.Config().PlatformSdkVersion().String())
358 cmd.FlagWithArg("--current-codename ", ctx.Config().PlatformSdkCodename())
359
360 filename := proptools.StringDefault(d.properties.Api_levels_jar_filename, "android.jar")
361
362 ctx.VisitDirectDepsWithTag(metalavaAPILevelsAnnotationsDirTag, func(m android.Module) {
363 if t, ok := m.(*ExportedDroiddocDir); ok {
364 for _, dep := range t.deps {
Colin Cross5f6ffc72021-03-29 21:54:45 -0700365 if dep.Base() == filename {
366 cmd.Implicit(dep)
367 }
368 if filename != "android.jar" && dep.Base() == "android.jar" {
369 // Metalava implicitly searches these patterns:
370 // prebuilts/tools/common/api-versions/android-%/android.jar
371 // prebuilts/sdk/%/public/android.jar
372 // Add android.jar files from the api_levels_annotations_dirs directories to try
373 // to satisfy these patterns. If Metalava can't find a match for an API level
374 // between 1 and 28 in at least one pattern it will fail.
Colin Cross2207f872021-03-24 12:39:08 -0700375 cmd.Implicit(dep)
376 }
377 }
378 cmd.FlagWithArg("--android-jar-pattern ", t.dir.String()+"/%/public/"+filename)
379 } else {
380 ctx.PropertyErrorf("api_levels_annotations_dirs",
381 "module %q is not a metalava api-levels-annotations dir", ctx.OtherModuleName(m))
382 }
383 })
384}
385
386func metalavaCmd(ctx android.ModuleContext, rule *android.RuleBuilder, javaVersion javaVersion, srcs android.Paths,
387 srcJarList android.Path, bootclasspath, classpath classpath, sourcepaths android.Paths,
Colin Cross8095c292021-03-30 16:40:48 -0700388 homeDir android.WritablePath) *android.RuleBuilderCommand {
Colin Cross2207f872021-03-24 12:39:08 -0700389 rule.Command().Text("rm -rf").Flag(homeDir.String())
390 rule.Command().Text("mkdir -p").Flag(homeDir.String())
391
392 cmd := rule.Command()
393 cmd.FlagWithArg("ANDROID_PREFS_ROOT=", homeDir.String())
394
395 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_METALAVA") {
396 rule.Remoteable(android.RemoteRuleSupports{RBE: true})
Colin Cross8095c292021-03-30 16:40:48 -0700397 execStrategy := ctx.Config().GetenvWithDefault("RBE_METALAVA_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
398 labels := map[string]string{"type": "tool", "name": "metalava"}
399 // TODO: metalava pool rejects these jobs
400 pool := ctx.Config().GetenvWithDefault("RBE_METALAVA_POOL", "java16")
401 rule.Rewrapper(&remoteexec.REParams{
402 Labels: labels,
403 ExecStrategy: execStrategy,
404 ToolchainInputs: []string{config.JavaCmd(ctx).String()},
405 Platform: map[string]string{remoteexec.PoolKey: pool},
406 })
Colin Cross2207f872021-03-24 12:39:08 -0700407 }
408
Colin Cross6aa5c402021-03-24 12:28:50 -0700409 cmd.BuiltTool("metalava").ImplicitTool(ctx.Config().HostJavaToolPath(ctx, "metalava.jar")).
Colin Cross2207f872021-03-24 12:39:08 -0700410 Flag(config.JavacVmFlags).
411 Flag("-J--add-opens=java.base/java.util=ALL-UNNAMED").
412 FlagWithArg("-encoding ", "UTF-8").
413 FlagWithArg("-source ", javaVersion.String()).
414 FlagWithRspFileInputList("@", android.PathForModuleOut(ctx, "metalava.rsp"), srcs).
415 FlagWithInput("@", srcJarList)
416
Colin Cross2207f872021-03-24 12:39:08 -0700417 if len(bootclasspath) > 0 {
418 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
419 }
420
421 if len(classpath) > 0 {
422 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
423 }
424
425 if len(sourcepaths) > 0 {
426 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
427 } else {
428 cmd.FlagWithArg("-sourcepath ", `""`)
429 }
430
431 cmd.Flag("--no-banner").
432 Flag("--color").
433 Flag("--quiet").
434 Flag("--format=v2").
435 FlagWithArg("--repeat-errors-max ", "10").
436 FlagWithArg("--hide ", "UnresolvedImport")
437
438 return cmd
439}
440
441func (d *Droidstubs) GenerateAndroidBuildActions(ctx android.ModuleContext) {
442 deps := d.Javadoc.collectDeps(ctx)
443
Jiyong Parkf1691d22021-03-29 20:11:58 +0900444 javaVersion := getJavaVersion(ctx, String(d.Javadoc.properties.Java_version), android.SdkContext(d))
Colin Cross2207f872021-03-24 12:39:08 -0700445
446 // Create rule for metalava
447
Colin Crosscb77f752021-03-24 12:04:44 -0700448 srcJarDir := android.PathForModuleOut(ctx, "metalava", "srcjars")
Colin Cross2207f872021-03-24 12:39:08 -0700449
450 rule := android.NewRuleBuilder(pctx, ctx)
451
Colin Cross8095c292021-03-30 16:40:48 -0700452 rule.Sbox(android.PathForModuleOut(ctx, "metalava"),
453 android.PathForModuleOut(ctx, "metalava.sbox.textproto")).
454 SandboxInputs()
Colin Cross6aa5c402021-03-24 12:28:50 -0700455
Colin Cross2207f872021-03-24 12:39:08 -0700456 if BoolDefault(d.properties.High_mem, false) {
457 // This metalava run uses lots of memory, restrict the number of metalava jobs that can run in parallel.
458 rule.HighMem()
459 }
460
461 generateStubs := BoolDefault(d.properties.Generate_stubs, true)
462 var stubsDir android.OptionalPath
463 if generateStubs {
Colin Crosscb77f752021-03-24 12:04:44 -0700464 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"-"+"stubs.srcjar")
465 stubsDir = android.OptionalPathForPath(android.PathForModuleOut(ctx, "metalava", "stubsDir"))
Colin Cross2207f872021-03-24 12:39:08 -0700466 rule.Command().Text("rm -rf").Text(stubsDir.String())
467 rule.Command().Text("mkdir -p").Text(stubsDir.String())
468 }
469
470 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
471
Colin Crosscb77f752021-03-24 12:04:44 -0700472 homeDir := android.PathForModuleOut(ctx, "metalava", "home")
Colin Cross2207f872021-03-24 12:39:08 -0700473 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
Colin Cross8095c292021-03-30 16:40:48 -0700474 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths, homeDir)
Colin Cross2207f872021-03-24 12:39:08 -0700475 cmd.Implicits(d.Javadoc.implicits)
476
477 d.stubsFlags(ctx, cmd, stubsDir)
478
479 d.annotationsFlags(ctx, cmd)
480 d.inclusionAnnotationsFlags(ctx, cmd)
481 d.apiLevelsAnnotationsFlags(ctx, cmd)
482
Colin Crossbc139922021-03-25 18:33:16 -0700483 d.expandArgs(ctx, cmd)
Colin Cross2207f872021-03-24 12:39:08 -0700484
Colin Cross2207f872021-03-24 12:39:08 -0700485 for _, o := range d.Javadoc.properties.Out {
486 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
487 }
488
489 // Add options for the other optional tasks: API-lint and check-released.
490 // We generate separate timestamp files for them.
491
492 doApiLint := false
493 doCheckReleased := false
494
495 // Add API lint options.
496
497 if BoolDefault(d.properties.Check_api.Api_lint.Enabled, false) {
498 doApiLint = true
499
500 newSince := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.New_since)
501 if newSince.Valid() {
502 cmd.FlagWithInput("--api-lint ", newSince.Path())
503 } else {
504 cmd.Flag("--api-lint")
505 }
Colin Crosscb77f752021-03-24 12:04:44 -0700506 d.apiLintReport = android.PathForModuleOut(ctx, "metalava", "api_lint_report.txt")
Colin Cross2207f872021-03-24 12:39:08 -0700507 cmd.FlagWithOutput("--report-even-if-suppressed ", d.apiLintReport) // TODO: Change to ":api-lint"
508
Colin Cross0d532412021-03-25 09:38:45 -0700509 // TODO(b/154317059): Clean up this allowlist by baselining and/or checking in last-released.
Colin Cross2207f872021-03-24 12:39:08 -0700510 if d.Name() != "android.car-system-stubs-docs" &&
511 d.Name() != "android.car-stubs-docs" {
512 cmd.Flag("--lints-as-errors")
513 cmd.Flag("--warnings-as-errors") // Most lints are actually warnings.
514 }
515
516 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.Baseline_file)
Colin Crosscb77f752021-03-24 12:04:44 -0700517 updatedBaselineOutput := android.PathForModuleOut(ctx, "metalava", "api_lint_baseline.txt")
518 d.apiLintTimestamp = android.PathForModuleOut(ctx, "metalava", "api_lint.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -0700519
520 // Note this string includes a special shell quote $' ... ', which decodes the "\n"s.
Colin Cross2207f872021-03-24 12:39:08 -0700521 //
522 // TODO: metalava also has a slightly different message hardcoded. Should we unify this
523 // message and metalava's one?
524 msg := `$'` + // Enclose with $' ... '
525 `************************************************************\n` +
526 `Your API changes are triggering API Lint warnings or errors.\n` +
527 `To make these errors go away, fix the code according to the\n` +
528 `error and/or warning messages above.\n` +
529 `\n` +
530 `If it is not possible to do so, there are workarounds:\n` +
531 `\n` +
532 `1. You can suppress the errors with @SuppressLint("<id>")\n`
533
534 if baselineFile.Valid() {
535 cmd.FlagWithInput("--baseline:api-lint ", baselineFile.Path())
536 cmd.FlagWithOutput("--update-baseline:api-lint ", updatedBaselineOutput)
537
538 msg += fmt.Sprintf(``+
539 `2. You can update the baseline by executing the following\n`+
540 ` command:\n`+
Colin Cross63eeda02021-04-15 19:01:57 -0700541 ` (cd $ANDROID_BUILD_TOP && cp \\\n`+
542 ` "%s" \\\n`+
543 ` "%s")\n`+
Colin Cross2207f872021-03-24 12:39:08 -0700544 ` To submit the revised baseline.txt to the main Android\n`+
545 ` repository, you will need approval.\n`, updatedBaselineOutput, baselineFile.Path())
546 } else {
547 msg += fmt.Sprintf(``+
548 `2. You can add a baseline file of existing lint failures\n`+
549 ` to the build rule of %s.\n`, d.Name())
550 }
551 // Note the message ends with a ' (single quote), to close the $' ... ' .
552 msg += `************************************************************\n'`
553
554 cmd.FlagWithArg("--error-message:api-lint ", msg)
555 }
556
557 // Add "check released" options. (Detect incompatible API changes from the last public release)
558
559 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") {
560 doCheckReleased = true
561
562 if len(d.Javadoc.properties.Out) > 0 {
563 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
564 }
565
566 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
567 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
568 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Last_released.Baseline_file)
Colin Crosscb77f752021-03-24 12:04:44 -0700569 updatedBaselineOutput := android.PathForModuleOut(ctx, "metalava", "last_released_baseline.txt")
Colin Cross2207f872021-03-24 12:39:08 -0700570
Colin Crosscb77f752021-03-24 12:04:44 -0700571 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "metalava", "check_last_released_api.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -0700572
573 cmd.FlagWithInput("--check-compatibility:api:released ", apiFile)
574 cmd.FlagWithInput("--check-compatibility:removed:released ", removedApiFile)
575
576 if baselineFile.Valid() {
577 cmd.FlagWithInput("--baseline:compatibility:released ", baselineFile.Path())
578 cmd.FlagWithOutput("--update-baseline:compatibility:released ", updatedBaselineOutput)
579 }
580
581 // Note this string includes quote ($' ... '), which decodes the "\n"s.
582 msg := `$'\n******************************\n` +
583 `You have tried to change the API from what has been previously released in\n` +
584 `an SDK. Please fix the errors listed above.\n` +
585 `******************************\n'`
586
587 cmd.FlagWithArg("--error-message:compatibility:released ", msg)
588 }
589
Colin Cross2207f872021-03-24 12:39:08 -0700590 if generateStubs {
591 rule.Command().
592 BuiltTool("soong_zip").
593 Flag("-write_if_changed").
594 Flag("-jar").
595 FlagWithOutput("-o ", d.Javadoc.stubsSrcJar).
596 FlagWithArg("-C ", stubsDir.String()).
597 FlagWithArg("-D ", stubsDir.String())
598 }
599
600 if Bool(d.properties.Write_sdk_values) {
Colin Crosscb77f752021-03-24 12:04:44 -0700601 d.metadataZip = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"-metadata.zip")
Colin Cross2207f872021-03-24 12:39:08 -0700602 rule.Command().
603 BuiltTool("soong_zip").
604 Flag("-write_if_changed").
605 Flag("-d").
606 FlagWithOutput("-o ", d.metadataZip).
607 FlagWithArg("-C ", d.metadataDir.String()).
608 FlagWithArg("-D ", d.metadataDir.String())
609 }
610
611 // TODO: We don't really need two separate API files, but this is a reminiscence of how
612 // we used to run metalava separately for API lint and the "last_released" check. Unify them.
613 if doApiLint {
614 rule.Command().Text("touch").Output(d.apiLintTimestamp)
615 }
616 if doCheckReleased {
617 rule.Command().Text("touch").Output(d.checkLastReleasedApiTimestamp)
618 }
619
Colin Cross6aa5c402021-03-24 12:28:50 -0700620 // TODO(b/183630617): rewrapper doesn't support restat rules
Colin Cross8095c292021-03-30 16:40:48 -0700621 // rule.Restat()
Colin Cross2207f872021-03-24 12:39:08 -0700622
623 zipSyncCleanupCmd(rule, srcJarDir)
624
625 rule.Build("metalava", "metalava merged")
626
627 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") {
628
629 if len(d.Javadoc.properties.Out) > 0 {
630 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
631 }
632
633 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
634 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
635 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Current.Baseline_file)
636
637 if baselineFile.Valid() {
638 ctx.PropertyErrorf("baseline_file", "current API check can't have a baseline file. (module %s)", ctx.ModuleName())
639 }
640
Colin Crosscb77f752021-03-24 12:04:44 -0700641 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "metalava", "check_current_api.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -0700642
643 rule := android.NewRuleBuilder(pctx, ctx)
644
645 // Diff command line.
646 // -F matches the closest "opening" line, such as "package android {"
647 // and " public class Intent {".
648 diff := `diff -u -F '{ *$'`
649
650 rule.Command().Text("( true")
651 rule.Command().
652 Text(diff).
653 Input(apiFile).Input(d.apiFile)
654
655 rule.Command().
656 Text(diff).
657 Input(removedApiFile).Input(d.removedApiFile)
658
659 msg := fmt.Sprintf(`\n******************************\n`+
660 `You have tried to change the API from what has been previously approved.\n\n`+
661 `To make these errors go away, you have two choices:\n`+
662 ` 1. You can add '@hide' javadoc comments (and remove @SystemApi/@TestApi/etc)\n`+
663 ` to the new methods, etc. shown in the above diff.\n\n`+
664 ` 2. You can update current.txt and/or removed.txt by executing the following command:\n`+
665 ` m %s-update-current-api\n\n`+
666 ` To submit the revised current.txt to the main Android repository,\n`+
667 ` you will need approval.\n`+
668 `******************************\n`, ctx.ModuleName())
669
670 rule.Command().
671 Text("touch").Output(d.checkCurrentApiTimestamp).
672 Text(") || (").
673 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
674 Text("; exit 38").
675 Text(")")
676
677 rule.Build("metalavaCurrentApiCheck", "check current API")
678
Colin Crosscb77f752021-03-24 12:04:44 -0700679 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "metalava", "update_current_api.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -0700680
681 // update API rule
682 rule = android.NewRuleBuilder(pctx, ctx)
683
684 rule.Command().Text("( true")
685
686 rule.Command().
687 Text("cp").Flag("-f").
688 Input(d.apiFile).Flag(apiFile.String())
689
690 rule.Command().
691 Text("cp").Flag("-f").
692 Input(d.removedApiFile).Flag(removedApiFile.String())
693
694 msg = "failed to update public API"
695
696 rule.Command().
697 Text("touch").Output(d.updateCurrentApiTimestamp).
698 Text(") || (").
699 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
700 Text("; exit 38").
701 Text(")")
702
703 rule.Build("metalavaCurrentApiUpdate", "update current API")
704 }
705
706 if String(d.properties.Check_nullability_warnings) != "" {
707 if d.nullabilityWarningsFile == nil {
708 ctx.PropertyErrorf("check_nullability_warnings",
709 "Cannot specify check_nullability_warnings unless validating nullability")
710 }
711
712 checkNullabilityWarnings := android.PathForModuleSrc(ctx, String(d.properties.Check_nullability_warnings))
713
Colin Crosscb77f752021-03-24 12:04:44 -0700714 d.checkNullabilityWarningsTimestamp = android.PathForModuleOut(ctx, "metalava", "check_nullability_warnings.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -0700715
716 msg := fmt.Sprintf(`\n******************************\n`+
717 `The warnings encountered during nullability annotation validation did\n`+
718 `not match the checked in file of expected warnings. The diffs are shown\n`+
719 `above. You have two options:\n`+
720 ` 1. Resolve the differences by editing the nullability annotations.\n`+
721 ` 2. Update the file of expected warnings by running:\n`+
722 ` cp %s %s\n`+
723 ` and submitting the updated file as part of your change.`,
724 d.nullabilityWarningsFile, checkNullabilityWarnings)
725
726 rule := android.NewRuleBuilder(pctx, ctx)
727
728 rule.Command().
729 Text("(").
730 Text("diff").Input(checkNullabilityWarnings).Input(d.nullabilityWarningsFile).
731 Text("&&").
732 Text("touch").Output(d.checkNullabilityWarningsTimestamp).
733 Text(") || (").
734 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
735 Text("; exit 38").
736 Text(")")
737
738 rule.Build("nullabilityWarningsCheck", "nullability warnings check")
739 }
740}
741
742func StubsDefaultsFactory() android.Module {
743 module := &DocDefaults{}
744
745 module.AddProperties(
746 &JavadocProperties{},
747 &DroidstubsProperties{},
748 )
749
750 android.InitDefaultsModule(module)
751
752 return module
753}
754
755var _ android.PrebuiltInterface = (*PrebuiltStubsSources)(nil)
756
757type PrebuiltStubsSourcesProperties struct {
758 Srcs []string `android:"path"`
759}
760
761type PrebuiltStubsSources struct {
762 android.ModuleBase
763 android.DefaultableModuleBase
764 prebuilt android.Prebuilt
765 android.SdkBase
766
767 properties PrebuiltStubsSourcesProperties
768
769 stubsSrcJar android.ModuleOutPath
770}
771
772func (p *PrebuiltStubsSources) OutputFiles(tag string) (android.Paths, error) {
773 switch tag {
774 case "":
775 return android.Paths{p.stubsSrcJar}, nil
776 default:
777 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
778 }
779}
780
781func (d *PrebuiltStubsSources) StubsSrcJar() android.Path {
782 return d.stubsSrcJar
783}
784
785func (p *PrebuiltStubsSources) GenerateAndroidBuildActions(ctx android.ModuleContext) {
786 p.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
787
788 if len(p.properties.Srcs) != 1 {
789 ctx.PropertyErrorf("srcs", "must only specify one directory path, contains %d paths", len(p.properties.Srcs))
790 return
791 }
792
793 localSrcDir := p.properties.Srcs[0]
794 // Although PathForModuleSrc can return nil if either the path doesn't exist or
795 // the path components are invalid it won't in this case because no components
796 // are specified and the module directory must exist in order to get this far.
797 srcDir := android.PathForModuleSrc(ctx).(android.SourcePath).Join(ctx, localSrcDir)
798
799 // Glob the contents of the directory just in case the directory does not exist.
800 srcGlob := localSrcDir + "/**/*"
801 srcPaths := android.PathsForModuleSrc(ctx, []string{srcGlob})
802
803 rule := android.NewRuleBuilder(pctx, ctx)
804 rule.Command().
805 BuiltTool("soong_zip").
806 Flag("-write_if_changed").
807 Flag("-jar").
808 FlagWithOutput("-o ", p.stubsSrcJar).
809 FlagWithArg("-C ", srcDir.String()).
810 FlagWithRspFileInputList("-r ", p.stubsSrcJar.ReplaceExtension(ctx, "rsp"), srcPaths)
811
812 rule.Restat()
813
814 rule.Build("zip src", "Create srcjar from prebuilt source")
815}
816
817func (p *PrebuiltStubsSources) Prebuilt() *android.Prebuilt {
818 return &p.prebuilt
819}
820
821func (p *PrebuiltStubsSources) Name() string {
822 return p.prebuilt.Name(p.ModuleBase.Name())
823}
824
825// prebuilt_stubs_sources imports a set of java source files as if they were
826// generated by droidstubs.
827//
828// By default, a prebuilt_stubs_sources has a single variant that expects a
829// set of `.java` files generated by droidstubs.
830//
831// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
832// for host modules.
833//
834// Intended only for use by sdk snapshots.
835func PrebuiltStubsSourcesFactory() android.Module {
836 module := &PrebuiltStubsSources{}
837
838 module.AddProperties(&module.properties)
839
840 android.InitPrebuiltModule(module, &module.properties.Srcs)
841 android.InitSdkAwareModule(module)
842 InitDroiddocModule(module, android.HostAndDeviceSupported)
843 return module
844}