blob: 9531056035b69e9f896f37b206b744a75ab79a73 [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
Anton Hansson52609322021-05-05 10:36:05 +0100149// Used by xsd_config
150type ApiFilePath interface {
151 ApiFilePath() android.Path
152}
153
154type ApiStubsSrcProvider interface {
155 StubsSrcJar() android.Path
156}
157
158// Provider of information about API stubs, used by java_sdk_library.
159type ApiStubsProvider interface {
160 ApiFilePath
161 RemovedApiFilePath() android.Path
162
163 ApiStubsSrcProvider
164}
165
Colin Cross2207f872021-03-24 12:39:08 -0700166// droidstubs passes sources files through Metalava to generate stub .java files that only contain the API to be
167// documented, filtering out hidden classes and methods. The resulting .java files are intended to be passed to
168// a droiddoc module to generate documentation.
169func DroidstubsFactory() android.Module {
170 module := &Droidstubs{}
171
172 module.AddProperties(&module.properties,
173 &module.Javadoc.properties)
174
175 InitDroiddocModule(module, android.HostAndDeviceSupported)
176 android.InitSdkAwareModule(module)
177 return module
178}
179
180// droidstubs_host passes sources files through Metalava to generate stub .java files that only contain the API
181// to be documented, filtering out hidden classes and methods. The resulting .java files are intended to be
182// passed to a droiddoc_host module to generate documentation. Use a droidstubs_host instead of a droidstubs
183// module when symbols needed by the source files are provided by java_library_host modules.
184func DroidstubsHostFactory() android.Module {
185 module := &Droidstubs{}
186
187 module.AddProperties(&module.properties,
188 &module.Javadoc.properties)
189
190 InitDroiddocModule(module, android.HostSupported)
191 return module
192}
193
194func (d *Droidstubs) OutputFiles(tag string) (android.Paths, error) {
195 switch tag {
196 case "":
197 return android.Paths{d.stubsSrcJar}, nil
198 case ".docs.zip":
199 return android.Paths{d.docZip}, nil
200 case ".api.txt", android.DefaultDistTag:
201 // This is the default dist path for dist properties that have no tag property.
202 return android.Paths{d.apiFilePath}, nil
203 case ".removed-api.txt":
204 return android.Paths{d.removedApiFilePath}, nil
205 case ".annotations.zip":
206 return android.Paths{d.annotationsZip}, nil
207 case ".api_versions.xml":
208 return android.Paths{d.apiVersionsXml}, nil
209 default:
210 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
211 }
212}
213
214func (d *Droidstubs) ApiFilePath() android.Path {
215 return d.apiFilePath
216}
217
218func (d *Droidstubs) RemovedApiFilePath() android.Path {
219 return d.removedApiFilePath
220}
221
222func (d *Droidstubs) StubsSrcJar() android.Path {
223 return d.stubsSrcJar
224}
225
226var metalavaMergeAnnotationsDirTag = dependencyTag{name: "metalava-merge-annotations-dir"}
227var metalavaMergeInclusionAnnotationsDirTag = dependencyTag{name: "metalava-merge-inclusion-annotations-dir"}
228var metalavaAPILevelsAnnotationsDirTag = dependencyTag{name: "metalava-api-levels-annotations-dir"}
229
230func (d *Droidstubs) DepsMutator(ctx android.BottomUpMutatorContext) {
231 d.Javadoc.addDeps(ctx)
232
233 if len(d.properties.Merge_annotations_dirs) != 0 {
234 for _, mergeAnnotationsDir := range d.properties.Merge_annotations_dirs {
235 ctx.AddDependency(ctx.Module(), metalavaMergeAnnotationsDirTag, mergeAnnotationsDir)
236 }
237 }
238
239 if len(d.properties.Merge_inclusion_annotations_dirs) != 0 {
240 for _, mergeInclusionAnnotationsDir := range d.properties.Merge_inclusion_annotations_dirs {
241 ctx.AddDependency(ctx.Module(), metalavaMergeInclusionAnnotationsDirTag, mergeInclusionAnnotationsDir)
242 }
243 }
244
245 if len(d.properties.Api_levels_annotations_dirs) != 0 {
246 for _, apiLevelsAnnotationsDir := range d.properties.Api_levels_annotations_dirs {
247 ctx.AddDependency(ctx.Module(), metalavaAPILevelsAnnotationsDirTag, apiLevelsAnnotationsDir)
248 }
249 }
250}
251
252func (d *Droidstubs) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.OptionalPath) {
253 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
254 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
255 String(d.properties.Api_filename) != "" {
256 filename := proptools.StringDefault(d.properties.Api_filename, ctx.ModuleName()+"_api.txt")
Colin Crosscb77f752021-03-24 12:04:44 -0700257 d.apiFile = android.PathForModuleOut(ctx, "metalava", filename)
Colin Cross2207f872021-03-24 12:39:08 -0700258 cmd.FlagWithOutput("--api ", d.apiFile)
259 d.apiFilePath = d.apiFile
260 } else if sourceApiFile := proptools.String(d.properties.Check_api.Current.Api_file); sourceApiFile != "" {
261 // If check api is disabled then make the source file available for export.
262 d.apiFilePath = android.PathForModuleSrc(ctx, sourceApiFile)
263 }
264
265 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
266 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
267 String(d.properties.Removed_api_filename) != "" {
268 filename := proptools.StringDefault(d.properties.Removed_api_filename, ctx.ModuleName()+"_removed.txt")
Colin Crosscb77f752021-03-24 12:04:44 -0700269 d.removedApiFile = android.PathForModuleOut(ctx, "metalava", filename)
Colin Cross2207f872021-03-24 12:39:08 -0700270 cmd.FlagWithOutput("--removed-api ", d.removedApiFile)
271 d.removedApiFilePath = d.removedApiFile
272 } else if sourceRemovedApiFile := proptools.String(d.properties.Check_api.Current.Removed_api_file); sourceRemovedApiFile != "" {
273 // If check api is disabled then make the source removed api file available for export.
274 d.removedApiFilePath = android.PathForModuleSrc(ctx, sourceRemovedApiFile)
275 }
276
277 if String(d.properties.Removed_dex_api_filename) != "" {
Colin Crosscb77f752021-03-24 12:04:44 -0700278 d.removedDexApiFile = android.PathForModuleOut(ctx, "metalava", String(d.properties.Removed_dex_api_filename))
Colin Cross2207f872021-03-24 12:39:08 -0700279 cmd.FlagWithOutput("--removed-dex-api ", d.removedDexApiFile)
280 }
281
282 if Bool(d.properties.Write_sdk_values) {
Colin Crosscb77f752021-03-24 12:04:44 -0700283 d.metadataDir = android.PathForModuleOut(ctx, "metalava", "metadata")
Colin Cross2207f872021-03-24 12:39:08 -0700284 cmd.FlagWithArg("--sdk-values ", d.metadataDir.String())
285 }
286
287 if stubsDir.Valid() {
288 if Bool(d.properties.Create_doc_stubs) {
289 cmd.FlagWithArg("--doc-stubs ", stubsDir.String())
290 } else {
291 cmd.FlagWithArg("--stubs ", stubsDir.String())
292 if !Bool(d.properties.Output_javadoc_comments) {
293 cmd.Flag("--exclude-documentation-from-stubs")
294 }
295 }
296 }
297}
298
299func (d *Droidstubs) annotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
300 if Bool(d.properties.Annotations_enabled) {
301 cmd.Flag("--include-annotations")
302
Andrei Onea4985e512021-04-29 16:29:34 +0100303 cmd.FlagWithArg("--exclude-annotation ", "androidx.annotation.RequiresApi")
304
Colin Cross2207f872021-03-24 12:39:08 -0700305 validatingNullability :=
Colin Crossbc139922021-03-25 18:33:16 -0700306 strings.Contains(String(d.Javadoc.properties.Args), "--validate-nullability-from-merged-stubs") ||
Colin Cross2207f872021-03-24 12:39:08 -0700307 String(d.properties.Validate_nullability_from_list) != ""
308
309 migratingNullability := String(d.properties.Previous_api) != ""
310 if migratingNullability {
311 previousApi := android.PathForModuleSrc(ctx, String(d.properties.Previous_api))
312 cmd.FlagWithInput("--migrate-nullness ", previousApi)
313 }
314
315 if s := String(d.properties.Validate_nullability_from_list); s != "" {
316 cmd.FlagWithInput("--validate-nullability-from-list ", android.PathForModuleSrc(ctx, s))
317 }
318
319 if validatingNullability {
Colin Crosscb77f752021-03-24 12:04:44 -0700320 d.nullabilityWarningsFile = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"_nullability_warnings.txt")
Colin Cross2207f872021-03-24 12:39:08 -0700321 cmd.FlagWithOutput("--nullability-warnings-txt ", d.nullabilityWarningsFile)
322 }
323
Colin Crosscb77f752021-03-24 12:04:44 -0700324 d.annotationsZip = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"_annotations.zip")
Colin Cross2207f872021-03-24 12:39:08 -0700325 cmd.FlagWithOutput("--extract-annotations ", d.annotationsZip)
326
327 if len(d.properties.Merge_annotations_dirs) != 0 {
328 d.mergeAnnoDirFlags(ctx, cmd)
329 }
330
331 // TODO(tnorbye): find owners to fix these warnings when annotation was enabled.
332 cmd.FlagWithArg("--hide ", "HiddenTypedefConstant").
333 FlagWithArg("--hide ", "SuperfluousPrefix").
334 FlagWithArg("--hide ", "AnnotationExtraction")
335 }
336}
337
338func (d *Droidstubs) mergeAnnoDirFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
339 ctx.VisitDirectDepsWithTag(metalavaMergeAnnotationsDirTag, func(m android.Module) {
340 if t, ok := m.(*ExportedDroiddocDir); ok {
341 cmd.FlagWithArg("--merge-qualifier-annotations ", t.dir.String()).Implicits(t.deps)
342 } else {
343 ctx.PropertyErrorf("merge_annotations_dirs",
344 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
345 }
346 })
347}
348
349func (d *Droidstubs) inclusionAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
350 ctx.VisitDirectDepsWithTag(metalavaMergeInclusionAnnotationsDirTag, func(m android.Module) {
351 if t, ok := m.(*ExportedDroiddocDir); ok {
352 cmd.FlagWithArg("--merge-inclusion-annotations ", t.dir.String()).Implicits(t.deps)
353 } else {
354 ctx.PropertyErrorf("merge_inclusion_annotations_dirs",
355 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
356 }
357 })
358}
359
360func (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
361 if !Bool(d.properties.Api_levels_annotations_enabled) {
362 return
363 }
364
Colin Crosscb77f752021-03-24 12:04:44 -0700365 d.apiVersionsXml = android.PathForModuleOut(ctx, "metalava", "api-versions.xml")
Colin Cross2207f872021-03-24 12:39:08 -0700366
367 if len(d.properties.Api_levels_annotations_dirs) == 0 {
368 ctx.PropertyErrorf("api_levels_annotations_dirs",
369 "has to be non-empty if api levels annotations was enabled!")
370 }
371
372 cmd.FlagWithOutput("--generate-api-levels ", d.apiVersionsXml)
373 cmd.FlagWithInput("--apply-api-levels ", d.apiVersionsXml)
374 cmd.FlagWithArg("--current-version ", ctx.Config().PlatformSdkVersion().String())
375 cmd.FlagWithArg("--current-codename ", ctx.Config().PlatformSdkCodename())
376
377 filename := proptools.StringDefault(d.properties.Api_levels_jar_filename, "android.jar")
378
379 ctx.VisitDirectDepsWithTag(metalavaAPILevelsAnnotationsDirTag, func(m android.Module) {
380 if t, ok := m.(*ExportedDroiddocDir); ok {
381 for _, dep := range t.deps {
Colin Cross5f6ffc72021-03-29 21:54:45 -0700382 if dep.Base() == filename {
383 cmd.Implicit(dep)
384 }
385 if filename != "android.jar" && dep.Base() == "android.jar" {
386 // Metalava implicitly searches these patterns:
387 // prebuilts/tools/common/api-versions/android-%/android.jar
388 // prebuilts/sdk/%/public/android.jar
389 // Add android.jar files from the api_levels_annotations_dirs directories to try
390 // to satisfy these patterns. If Metalava can't find a match for an API level
391 // between 1 and 28 in at least one pattern it will fail.
Colin Cross2207f872021-03-24 12:39:08 -0700392 cmd.Implicit(dep)
393 }
394 }
395 cmd.FlagWithArg("--android-jar-pattern ", t.dir.String()+"/%/public/"+filename)
396 } else {
397 ctx.PropertyErrorf("api_levels_annotations_dirs",
398 "module %q is not a metalava api-levels-annotations dir", ctx.OtherModuleName(m))
399 }
400 })
401}
402
403func metalavaCmd(ctx android.ModuleContext, rule *android.RuleBuilder, javaVersion javaVersion, srcs android.Paths,
404 srcJarList android.Path, bootclasspath, classpath classpath, sourcepaths android.Paths,
Colin Cross8095c292021-03-30 16:40:48 -0700405 homeDir android.WritablePath) *android.RuleBuilderCommand {
Colin Cross2207f872021-03-24 12:39:08 -0700406 rule.Command().Text("rm -rf").Flag(homeDir.String())
407 rule.Command().Text("mkdir -p").Flag(homeDir.String())
408
409 cmd := rule.Command()
410 cmd.FlagWithArg("ANDROID_PREFS_ROOT=", homeDir.String())
411
412 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_METALAVA") {
413 rule.Remoteable(android.RemoteRuleSupports{RBE: true})
Colin Cross8095c292021-03-30 16:40:48 -0700414 execStrategy := ctx.Config().GetenvWithDefault("RBE_METALAVA_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
415 labels := map[string]string{"type": "tool", "name": "metalava"}
416 // TODO: metalava pool rejects these jobs
417 pool := ctx.Config().GetenvWithDefault("RBE_METALAVA_POOL", "java16")
418 rule.Rewrapper(&remoteexec.REParams{
419 Labels: labels,
420 ExecStrategy: execStrategy,
421 ToolchainInputs: []string{config.JavaCmd(ctx).String()},
422 Platform: map[string]string{remoteexec.PoolKey: pool},
423 })
Colin Cross2207f872021-03-24 12:39:08 -0700424 }
425
Colin Cross6aa5c402021-03-24 12:28:50 -0700426 cmd.BuiltTool("metalava").ImplicitTool(ctx.Config().HostJavaToolPath(ctx, "metalava.jar")).
Colin Cross2207f872021-03-24 12:39:08 -0700427 Flag(config.JavacVmFlags).
428 Flag("-J--add-opens=java.base/java.util=ALL-UNNAMED").
429 FlagWithArg("-encoding ", "UTF-8").
430 FlagWithArg("-source ", javaVersion.String()).
431 FlagWithRspFileInputList("@", android.PathForModuleOut(ctx, "metalava.rsp"), srcs).
432 FlagWithInput("@", srcJarList)
433
Colin Cross2207f872021-03-24 12:39:08 -0700434 if len(bootclasspath) > 0 {
435 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
436 }
437
438 if len(classpath) > 0 {
439 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
440 }
441
442 if len(sourcepaths) > 0 {
443 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
444 } else {
445 cmd.FlagWithArg("-sourcepath ", `""`)
446 }
447
448 cmd.Flag("--no-banner").
449 Flag("--color").
450 Flag("--quiet").
451 Flag("--format=v2").
452 FlagWithArg("--repeat-errors-max ", "10").
453 FlagWithArg("--hide ", "UnresolvedImport")
454
455 return cmd
456}
457
458func (d *Droidstubs) GenerateAndroidBuildActions(ctx android.ModuleContext) {
459 deps := d.Javadoc.collectDeps(ctx)
460
Jiyong Parkf1691d22021-03-29 20:11:58 +0900461 javaVersion := getJavaVersion(ctx, String(d.Javadoc.properties.Java_version), android.SdkContext(d))
Colin Cross2207f872021-03-24 12:39:08 -0700462
463 // Create rule for metalava
464
Colin Crosscb77f752021-03-24 12:04:44 -0700465 srcJarDir := android.PathForModuleOut(ctx, "metalava", "srcjars")
Colin Cross2207f872021-03-24 12:39:08 -0700466
467 rule := android.NewRuleBuilder(pctx, ctx)
468
Colin Cross8095c292021-03-30 16:40:48 -0700469 rule.Sbox(android.PathForModuleOut(ctx, "metalava"),
470 android.PathForModuleOut(ctx, "metalava.sbox.textproto")).
471 SandboxInputs()
Colin Cross6aa5c402021-03-24 12:28:50 -0700472
Colin Cross2207f872021-03-24 12:39:08 -0700473 if BoolDefault(d.properties.High_mem, false) {
474 // This metalava run uses lots of memory, restrict the number of metalava jobs that can run in parallel.
475 rule.HighMem()
476 }
477
478 generateStubs := BoolDefault(d.properties.Generate_stubs, true)
479 var stubsDir android.OptionalPath
480 if generateStubs {
Colin Crosscb77f752021-03-24 12:04:44 -0700481 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"-"+"stubs.srcjar")
482 stubsDir = android.OptionalPathForPath(android.PathForModuleOut(ctx, "metalava", "stubsDir"))
Colin Cross2207f872021-03-24 12:39:08 -0700483 rule.Command().Text("rm -rf").Text(stubsDir.String())
484 rule.Command().Text("mkdir -p").Text(stubsDir.String())
485 }
486
487 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
488
Colin Crosscb77f752021-03-24 12:04:44 -0700489 homeDir := android.PathForModuleOut(ctx, "metalava", "home")
Colin Cross2207f872021-03-24 12:39:08 -0700490 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
Colin Cross8095c292021-03-30 16:40:48 -0700491 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths, homeDir)
Colin Cross2207f872021-03-24 12:39:08 -0700492 cmd.Implicits(d.Javadoc.implicits)
493
494 d.stubsFlags(ctx, cmd, stubsDir)
495
496 d.annotationsFlags(ctx, cmd)
497 d.inclusionAnnotationsFlags(ctx, cmd)
498 d.apiLevelsAnnotationsFlags(ctx, cmd)
499
Colin Crossbc139922021-03-25 18:33:16 -0700500 d.expandArgs(ctx, cmd)
Colin Cross2207f872021-03-24 12:39:08 -0700501
Colin Cross2207f872021-03-24 12:39:08 -0700502 for _, o := range d.Javadoc.properties.Out {
503 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
504 }
505
506 // Add options for the other optional tasks: API-lint and check-released.
507 // We generate separate timestamp files for them.
508
509 doApiLint := false
510 doCheckReleased := false
511
512 // Add API lint options.
513
514 if BoolDefault(d.properties.Check_api.Api_lint.Enabled, false) {
515 doApiLint = true
516
517 newSince := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.New_since)
518 if newSince.Valid() {
519 cmd.FlagWithInput("--api-lint ", newSince.Path())
520 } else {
521 cmd.Flag("--api-lint")
522 }
Colin Crosscb77f752021-03-24 12:04:44 -0700523 d.apiLintReport = android.PathForModuleOut(ctx, "metalava", "api_lint_report.txt")
Colin Cross2207f872021-03-24 12:39:08 -0700524 cmd.FlagWithOutput("--report-even-if-suppressed ", d.apiLintReport) // TODO: Change to ":api-lint"
525
Colin Cross0d532412021-03-25 09:38:45 -0700526 // TODO(b/154317059): Clean up this allowlist by baselining and/or checking in last-released.
Colin Cross2207f872021-03-24 12:39:08 -0700527 if d.Name() != "android.car-system-stubs-docs" &&
528 d.Name() != "android.car-stubs-docs" {
529 cmd.Flag("--lints-as-errors")
530 cmd.Flag("--warnings-as-errors") // Most lints are actually warnings.
531 }
532
533 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.Baseline_file)
Colin Crosscb77f752021-03-24 12:04:44 -0700534 updatedBaselineOutput := android.PathForModuleOut(ctx, "metalava", "api_lint_baseline.txt")
535 d.apiLintTimestamp = android.PathForModuleOut(ctx, "metalava", "api_lint.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -0700536
537 // Note this string includes a special shell quote $' ... ', which decodes the "\n"s.
Colin Cross2207f872021-03-24 12:39:08 -0700538 //
539 // TODO: metalava also has a slightly different message hardcoded. Should we unify this
540 // message and metalava's one?
541 msg := `$'` + // Enclose with $' ... '
542 `************************************************************\n` +
543 `Your API changes are triggering API Lint warnings or errors.\n` +
544 `To make these errors go away, fix the code according to the\n` +
545 `error and/or warning messages above.\n` +
546 `\n` +
547 `If it is not possible to do so, there are workarounds:\n` +
548 `\n` +
Aurimas Liutikasb23b7452021-05-24 18:00:37 +0000549 `1. You can suppress the errors with @SuppressLint("<id>")\n` +
550 ` where the <id> is given in brackets in the error message above.\n`
Colin Cross2207f872021-03-24 12:39:08 -0700551
552 if baselineFile.Valid() {
553 cmd.FlagWithInput("--baseline:api-lint ", baselineFile.Path())
554 cmd.FlagWithOutput("--update-baseline:api-lint ", updatedBaselineOutput)
555
556 msg += fmt.Sprintf(``+
557 `2. You can update the baseline by executing the following\n`+
558 ` command:\n`+
Colin Cross63eeda02021-04-15 19:01:57 -0700559 ` (cd $ANDROID_BUILD_TOP && cp \\\n`+
560 ` "%s" \\\n`+
561 ` "%s")\n`+
Colin Cross2207f872021-03-24 12:39:08 -0700562 ` To submit the revised baseline.txt to the main Android\n`+
563 ` repository, you will need approval.\n`, updatedBaselineOutput, baselineFile.Path())
564 } else {
565 msg += fmt.Sprintf(``+
566 `2. You can add a baseline file of existing lint failures\n`+
567 ` to the build rule of %s.\n`, d.Name())
568 }
569 // Note the message ends with a ' (single quote), to close the $' ... ' .
570 msg += `************************************************************\n'`
571
572 cmd.FlagWithArg("--error-message:api-lint ", msg)
573 }
574
575 // Add "check released" options. (Detect incompatible API changes from the last public release)
576
577 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") {
578 doCheckReleased = true
579
580 if len(d.Javadoc.properties.Out) > 0 {
581 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
582 }
583
584 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
585 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
586 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Last_released.Baseline_file)
Colin Crosscb77f752021-03-24 12:04:44 -0700587 updatedBaselineOutput := android.PathForModuleOut(ctx, "metalava", "last_released_baseline.txt")
Colin Cross2207f872021-03-24 12:39:08 -0700588
Colin Crosscb77f752021-03-24 12:04:44 -0700589 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "metalava", "check_last_released_api.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -0700590
591 cmd.FlagWithInput("--check-compatibility:api:released ", apiFile)
592 cmd.FlagWithInput("--check-compatibility:removed:released ", removedApiFile)
593
594 if baselineFile.Valid() {
595 cmd.FlagWithInput("--baseline:compatibility:released ", baselineFile.Path())
596 cmd.FlagWithOutput("--update-baseline:compatibility:released ", updatedBaselineOutput)
597 }
598
599 // Note this string includes quote ($' ... '), which decodes the "\n"s.
600 msg := `$'\n******************************\n` +
601 `You have tried to change the API from what has been previously released in\n` +
602 `an SDK. Please fix the errors listed above.\n` +
603 `******************************\n'`
604
605 cmd.FlagWithArg("--error-message:compatibility:released ", msg)
606 }
607
Colin Cross2207f872021-03-24 12:39:08 -0700608 if generateStubs {
609 rule.Command().
610 BuiltTool("soong_zip").
611 Flag("-write_if_changed").
612 Flag("-jar").
613 FlagWithOutput("-o ", d.Javadoc.stubsSrcJar).
614 FlagWithArg("-C ", stubsDir.String()).
615 FlagWithArg("-D ", stubsDir.String())
616 }
617
618 if Bool(d.properties.Write_sdk_values) {
Colin Crosscb77f752021-03-24 12:04:44 -0700619 d.metadataZip = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"-metadata.zip")
Colin Cross2207f872021-03-24 12:39:08 -0700620 rule.Command().
621 BuiltTool("soong_zip").
622 Flag("-write_if_changed").
623 Flag("-d").
624 FlagWithOutput("-o ", d.metadataZip).
625 FlagWithArg("-C ", d.metadataDir.String()).
626 FlagWithArg("-D ", d.metadataDir.String())
627 }
628
629 // TODO: We don't really need two separate API files, but this is a reminiscence of how
630 // we used to run metalava separately for API lint and the "last_released" check. Unify them.
631 if doApiLint {
632 rule.Command().Text("touch").Output(d.apiLintTimestamp)
633 }
634 if doCheckReleased {
635 rule.Command().Text("touch").Output(d.checkLastReleasedApiTimestamp)
636 }
637
Colin Cross6aa5c402021-03-24 12:28:50 -0700638 // TODO(b/183630617): rewrapper doesn't support restat rules
Colin Cross8095c292021-03-30 16:40:48 -0700639 // rule.Restat()
Colin Cross2207f872021-03-24 12:39:08 -0700640
641 zipSyncCleanupCmd(rule, srcJarDir)
642
643 rule.Build("metalava", "metalava merged")
644
645 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") {
646
647 if len(d.Javadoc.properties.Out) > 0 {
648 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
649 }
650
651 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
652 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
653 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Current.Baseline_file)
654
655 if baselineFile.Valid() {
656 ctx.PropertyErrorf("baseline_file", "current API check can't have a baseline file. (module %s)", ctx.ModuleName())
657 }
658
Colin Crosscb77f752021-03-24 12:04:44 -0700659 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "metalava", "check_current_api.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -0700660
661 rule := android.NewRuleBuilder(pctx, ctx)
662
663 // Diff command line.
664 // -F matches the closest "opening" line, such as "package android {"
665 // and " public class Intent {".
666 diff := `diff -u -F '{ *$'`
667
668 rule.Command().Text("( true")
669 rule.Command().
670 Text(diff).
671 Input(apiFile).Input(d.apiFile)
672
673 rule.Command().
674 Text(diff).
675 Input(removedApiFile).Input(d.removedApiFile)
676
677 msg := fmt.Sprintf(`\n******************************\n`+
678 `You have tried to change the API from what has been previously approved.\n\n`+
679 `To make these errors go away, you have two choices:\n`+
680 ` 1. You can add '@hide' javadoc comments (and remove @SystemApi/@TestApi/etc)\n`+
681 ` to the new methods, etc. shown in the above diff.\n\n`+
682 ` 2. You can update current.txt and/or removed.txt by executing the following command:\n`+
683 ` m %s-update-current-api\n\n`+
684 ` To submit the revised current.txt to the main Android repository,\n`+
685 ` you will need approval.\n`+
686 `******************************\n`, ctx.ModuleName())
687
688 rule.Command().
689 Text("touch").Output(d.checkCurrentApiTimestamp).
690 Text(") || (").
691 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
692 Text("; exit 38").
693 Text(")")
694
695 rule.Build("metalavaCurrentApiCheck", "check current API")
696
Colin Crosscb77f752021-03-24 12:04:44 -0700697 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "metalava", "update_current_api.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -0700698
699 // update API rule
700 rule = android.NewRuleBuilder(pctx, ctx)
701
702 rule.Command().Text("( true")
703
704 rule.Command().
705 Text("cp").Flag("-f").
706 Input(d.apiFile).Flag(apiFile.String())
707
708 rule.Command().
709 Text("cp").Flag("-f").
710 Input(d.removedApiFile).Flag(removedApiFile.String())
711
712 msg = "failed to update public API"
713
714 rule.Command().
715 Text("touch").Output(d.updateCurrentApiTimestamp).
716 Text(") || (").
717 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
718 Text("; exit 38").
719 Text(")")
720
721 rule.Build("metalavaCurrentApiUpdate", "update current API")
722 }
723
724 if String(d.properties.Check_nullability_warnings) != "" {
725 if d.nullabilityWarningsFile == nil {
726 ctx.PropertyErrorf("check_nullability_warnings",
727 "Cannot specify check_nullability_warnings unless validating nullability")
728 }
729
730 checkNullabilityWarnings := android.PathForModuleSrc(ctx, String(d.properties.Check_nullability_warnings))
731
Colin Crosscb77f752021-03-24 12:04:44 -0700732 d.checkNullabilityWarningsTimestamp = android.PathForModuleOut(ctx, "metalava", "check_nullability_warnings.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -0700733
734 msg := fmt.Sprintf(`\n******************************\n`+
735 `The warnings encountered during nullability annotation validation did\n`+
736 `not match the checked in file of expected warnings. The diffs are shown\n`+
737 `above. You have two options:\n`+
738 ` 1. Resolve the differences by editing the nullability annotations.\n`+
739 ` 2. Update the file of expected warnings by running:\n`+
740 ` cp %s %s\n`+
741 ` and submitting the updated file as part of your change.`,
742 d.nullabilityWarningsFile, checkNullabilityWarnings)
743
744 rule := android.NewRuleBuilder(pctx, ctx)
745
746 rule.Command().
747 Text("(").
748 Text("diff").Input(checkNullabilityWarnings).Input(d.nullabilityWarningsFile).
749 Text("&&").
750 Text("touch").Output(d.checkNullabilityWarningsTimestamp).
751 Text(") || (").
752 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
753 Text("; exit 38").
754 Text(")")
755
756 rule.Build("nullabilityWarningsCheck", "nullability warnings check")
757 }
758}
759
760func StubsDefaultsFactory() android.Module {
761 module := &DocDefaults{}
762
763 module.AddProperties(
764 &JavadocProperties{},
765 &DroidstubsProperties{},
766 )
767
768 android.InitDefaultsModule(module)
769
770 return module
771}
772
773var _ android.PrebuiltInterface = (*PrebuiltStubsSources)(nil)
774
775type PrebuiltStubsSourcesProperties struct {
776 Srcs []string `android:"path"`
777}
778
779type PrebuiltStubsSources struct {
780 android.ModuleBase
781 android.DefaultableModuleBase
782 prebuilt android.Prebuilt
783 android.SdkBase
784
785 properties PrebuiltStubsSourcesProperties
786
787 stubsSrcJar android.ModuleOutPath
788}
789
790func (p *PrebuiltStubsSources) OutputFiles(tag string) (android.Paths, error) {
791 switch tag {
792 case "":
793 return android.Paths{p.stubsSrcJar}, nil
794 default:
795 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
796 }
797}
798
799func (d *PrebuiltStubsSources) StubsSrcJar() android.Path {
800 return d.stubsSrcJar
801}
802
803func (p *PrebuiltStubsSources) GenerateAndroidBuildActions(ctx android.ModuleContext) {
804 p.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
805
806 if len(p.properties.Srcs) != 1 {
807 ctx.PropertyErrorf("srcs", "must only specify one directory path, contains %d paths", len(p.properties.Srcs))
808 return
809 }
810
811 localSrcDir := p.properties.Srcs[0]
812 // Although PathForModuleSrc can return nil if either the path doesn't exist or
813 // the path components are invalid it won't in this case because no components
814 // are specified and the module directory must exist in order to get this far.
815 srcDir := android.PathForModuleSrc(ctx).(android.SourcePath).Join(ctx, localSrcDir)
816
817 // Glob the contents of the directory just in case the directory does not exist.
818 srcGlob := localSrcDir + "/**/*"
819 srcPaths := android.PathsForModuleSrc(ctx, []string{srcGlob})
820
821 rule := android.NewRuleBuilder(pctx, ctx)
822 rule.Command().
823 BuiltTool("soong_zip").
824 Flag("-write_if_changed").
825 Flag("-jar").
826 FlagWithOutput("-o ", p.stubsSrcJar).
827 FlagWithArg("-C ", srcDir.String()).
828 FlagWithRspFileInputList("-r ", p.stubsSrcJar.ReplaceExtension(ctx, "rsp"), srcPaths)
829
830 rule.Restat()
831
832 rule.Build("zip src", "Create srcjar from prebuilt source")
833}
834
835func (p *PrebuiltStubsSources) Prebuilt() *android.Prebuilt {
836 return &p.prebuilt
837}
838
839func (p *PrebuiltStubsSources) Name() string {
840 return p.prebuilt.Name(p.ModuleBase.Name())
841}
842
843// prebuilt_stubs_sources imports a set of java source files as if they were
844// generated by droidstubs.
845//
846// By default, a prebuilt_stubs_sources has a single variant that expects a
847// set of `.java` files generated by droidstubs.
848//
849// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
850// for host modules.
851//
852// Intended only for use by sdk snapshots.
853func PrebuiltStubsSourcesFactory() android.Module {
854 module := &PrebuiltStubsSources{}
855
856 module.AddProperties(&module.properties)
857
858 android.InitPrebuiltModule(module, &module.properties.Srcs)
859 android.InitSdkAwareModule(module)
860 InitDroiddocModule(module, android.HostAndDeviceSupported)
861 return module
862}