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