blob: b42cb66fe606a3986dc25c635937352de60797e9 [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
286 validatingNullability :=
Colin Crossbc139922021-03-25 18:33:16 -0700287 strings.Contains(String(d.Javadoc.properties.Args), "--validate-nullability-from-merged-stubs") ||
Colin Cross2207f872021-03-24 12:39:08 -0700288 String(d.properties.Validate_nullability_from_list) != ""
289
290 migratingNullability := String(d.properties.Previous_api) != ""
291 if migratingNullability {
292 previousApi := android.PathForModuleSrc(ctx, String(d.properties.Previous_api))
293 cmd.FlagWithInput("--migrate-nullness ", previousApi)
294 }
295
296 if s := String(d.properties.Validate_nullability_from_list); s != "" {
297 cmd.FlagWithInput("--validate-nullability-from-list ", android.PathForModuleSrc(ctx, s))
298 }
299
300 if validatingNullability {
Colin Crosscb77f752021-03-24 12:04:44 -0700301 d.nullabilityWarningsFile = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"_nullability_warnings.txt")
Colin Cross2207f872021-03-24 12:39:08 -0700302 cmd.FlagWithOutput("--nullability-warnings-txt ", d.nullabilityWarningsFile)
303 }
304
Colin Crosscb77f752021-03-24 12:04:44 -0700305 d.annotationsZip = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"_annotations.zip")
Colin Cross2207f872021-03-24 12:39:08 -0700306 cmd.FlagWithOutput("--extract-annotations ", d.annotationsZip)
307
308 if len(d.properties.Merge_annotations_dirs) != 0 {
309 d.mergeAnnoDirFlags(ctx, cmd)
310 }
311
312 // TODO(tnorbye): find owners to fix these warnings when annotation was enabled.
313 cmd.FlagWithArg("--hide ", "HiddenTypedefConstant").
314 FlagWithArg("--hide ", "SuperfluousPrefix").
315 FlagWithArg("--hide ", "AnnotationExtraction")
316 }
317}
318
319func (d *Droidstubs) mergeAnnoDirFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
320 ctx.VisitDirectDepsWithTag(metalavaMergeAnnotationsDirTag, func(m android.Module) {
321 if t, ok := m.(*ExportedDroiddocDir); ok {
322 cmd.FlagWithArg("--merge-qualifier-annotations ", t.dir.String()).Implicits(t.deps)
323 } else {
324 ctx.PropertyErrorf("merge_annotations_dirs",
325 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
326 }
327 })
328}
329
330func (d *Droidstubs) inclusionAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
331 ctx.VisitDirectDepsWithTag(metalavaMergeInclusionAnnotationsDirTag, func(m android.Module) {
332 if t, ok := m.(*ExportedDroiddocDir); ok {
333 cmd.FlagWithArg("--merge-inclusion-annotations ", t.dir.String()).Implicits(t.deps)
334 } else {
335 ctx.PropertyErrorf("merge_inclusion_annotations_dirs",
336 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
337 }
338 })
339}
340
341func (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
342 if !Bool(d.properties.Api_levels_annotations_enabled) {
343 return
344 }
345
Colin Crosscb77f752021-03-24 12:04:44 -0700346 d.apiVersionsXml = android.PathForModuleOut(ctx, "metalava", "api-versions.xml")
Colin Cross2207f872021-03-24 12:39:08 -0700347
348 if len(d.properties.Api_levels_annotations_dirs) == 0 {
349 ctx.PropertyErrorf("api_levels_annotations_dirs",
350 "has to be non-empty if api levels annotations was enabled!")
351 }
352
353 cmd.FlagWithOutput("--generate-api-levels ", d.apiVersionsXml)
354 cmd.FlagWithInput("--apply-api-levels ", d.apiVersionsXml)
355 cmd.FlagWithArg("--current-version ", ctx.Config().PlatformSdkVersion().String())
356 cmd.FlagWithArg("--current-codename ", ctx.Config().PlatformSdkCodename())
357
358 filename := proptools.StringDefault(d.properties.Api_levels_jar_filename, "android.jar")
359
360 ctx.VisitDirectDepsWithTag(metalavaAPILevelsAnnotationsDirTag, func(m android.Module) {
361 if t, ok := m.(*ExportedDroiddocDir); ok {
362 for _, dep := range t.deps {
363 if strings.HasSuffix(dep.String(), filename) {
364 cmd.Implicit(dep)
365 }
366 }
367 cmd.FlagWithArg("--android-jar-pattern ", t.dir.String()+"/%/public/"+filename)
368 } else {
369 ctx.PropertyErrorf("api_levels_annotations_dirs",
370 "module %q is not a metalava api-levels-annotations dir", ctx.OtherModuleName(m))
371 }
372 })
373}
374
375func metalavaCmd(ctx android.ModuleContext, rule *android.RuleBuilder, javaVersion javaVersion, srcs android.Paths,
376 srcJarList android.Path, bootclasspath, classpath classpath, sourcepaths android.Paths,
377 implicitsRsp, homeDir android.WritablePath, sandbox bool) *android.RuleBuilderCommand {
378 rule.Command().Text("rm -rf").Flag(homeDir.String())
379 rule.Command().Text("mkdir -p").Flag(homeDir.String())
380
381 cmd := rule.Command()
382 cmd.FlagWithArg("ANDROID_PREFS_ROOT=", homeDir.String())
383
384 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_METALAVA") {
385 rule.Remoteable(android.RemoteRuleSupports{RBE: true})
Colin Cross6aa5c402021-03-24 12:28:50 -0700386 if sandbox {
387 execStrategy := ctx.Config().GetenvWithDefault("RBE_METALAVA_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
388 labels := map[string]string{"type": "tool", "name": "metalava"}
389 // TODO: metalava pool rejects these jobs
390 pool := ctx.Config().GetenvWithDefault("RBE_METALAVA_POOL", "java16")
391 rule.Rewrapper(&remoteexec.REParams{
392 Labels: labels,
393 ExecStrategy: execStrategy,
394 ToolchainInputs: []string{config.JavaCmd(ctx).String()},
395 Platform: map[string]string{remoteexec.PoolKey: pool},
396 })
397 } else {
398 execStrategy := remoteexec.LocalExecStrategy
399 labels := map[string]string{"type": "compile", "lang": "java", "compiler": "metalava", "shallow": "true"}
400 pool := ctx.Config().GetenvWithDefault("RBE_METALAVA_POOL", "metalava")
401
402 inputs := []string{
403 ctx.Config().HostJavaToolPath(ctx, "metalava").String(),
404 homeDir.String(),
405 }
406 if v := ctx.Config().Getenv("RBE_METALAVA_INPUTS"); v != "" {
407 inputs = append(inputs, strings.Split(v, ",")...)
408 }
409 cmd.Text((&remoteexec.REParams{
410 Labels: labels,
411 ExecStrategy: execStrategy,
412 Inputs: inputs,
413 RSPFiles: []string{implicitsRsp.String()},
414 ToolchainInputs: []string{config.JavaCmd(ctx).String()},
415 Platform: map[string]string{remoteexec.PoolKey: pool},
416 EnvironmentVariables: []string{"ANDROID_PREFS_ROOT"},
417 }).NoVarTemplate(ctx.Config().RBEWrapper()))
Colin Cross2207f872021-03-24 12:39:08 -0700418 }
Colin Cross2207f872021-03-24 12:39:08 -0700419 }
420
Colin Cross6aa5c402021-03-24 12:28:50 -0700421 cmd.BuiltTool("metalava").ImplicitTool(ctx.Config().HostJavaToolPath(ctx, "metalava.jar")).
Colin Cross2207f872021-03-24 12:39:08 -0700422 Flag(config.JavacVmFlags).
423 Flag("-J--add-opens=java.base/java.util=ALL-UNNAMED").
424 FlagWithArg("-encoding ", "UTF-8").
425 FlagWithArg("-source ", javaVersion.String()).
426 FlagWithRspFileInputList("@", android.PathForModuleOut(ctx, "metalava.rsp"), srcs).
427 FlagWithInput("@", srcJarList)
428
Colin Cross6aa5c402021-03-24 12:28:50 -0700429 if !sandbox {
430 if javaHome := ctx.Config().Getenv("ANDROID_JAVA_HOME"); javaHome != "" {
431 cmd.Implicit(android.PathForSource(ctx, javaHome))
432 }
Colin Cross2207f872021-03-24 12:39:08 -0700433
Colin Cross2207f872021-03-24 12:39:08 -0700434 cmd.FlagWithOutput("--strict-input-files:warn ", android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"violations.txt"))
Colin Cross2207f872021-03-24 12:39:08 -0700435
Colin Cross6aa5c402021-03-24 12:28:50 -0700436 if implicitsRsp != nil {
437 cmd.FlagWithArg("--strict-input-files-exempt ", "@"+implicitsRsp.String())
438 }
Colin Cross2207f872021-03-24 12:39:08 -0700439 }
440
441 if len(bootclasspath) > 0 {
442 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
443 }
444
445 if len(classpath) > 0 {
446 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
447 }
448
449 if len(sourcepaths) > 0 {
450 cmd.FlagWithList("-sourcepath ", sourcepaths.Strings(), ":")
451 } else {
452 cmd.FlagWithArg("-sourcepath ", `""`)
453 }
454
455 cmd.Flag("--no-banner").
456 Flag("--color").
457 Flag("--quiet").
458 Flag("--format=v2").
459 FlagWithArg("--repeat-errors-max ", "10").
460 FlagWithArg("--hide ", "UnresolvedImport")
461
462 return cmd
463}
464
465func (d *Droidstubs) GenerateAndroidBuildActions(ctx android.ModuleContext) {
466 deps := d.Javadoc.collectDeps(ctx)
467
468 javaVersion := getJavaVersion(ctx, String(d.Javadoc.properties.Java_version), sdkContext(d))
469
470 // Create rule for metalava
471
Colin Crosscb77f752021-03-24 12:04:44 -0700472 srcJarDir := android.PathForModuleOut(ctx, "metalava", "srcjars")
Colin Cross2207f872021-03-24 12:39:08 -0700473
474 rule := android.NewRuleBuilder(pctx, ctx)
475
Colin Cross6aa5c402021-03-24 12:28:50 -0700476 sandbox := proptools.Bool(d.Javadoc.properties.Sandbox)
477 if sandbox {
478 rule.Sbox(android.PathForModuleOut(ctx, "metalava"),
479 android.PathForModuleOut(ctx, "metalava.sbox.textproto")).
480 SandboxInputs()
481 }
482
Colin Cross2207f872021-03-24 12:39:08 -0700483 if BoolDefault(d.properties.High_mem, false) {
484 // This metalava run uses lots of memory, restrict the number of metalava jobs that can run in parallel.
485 rule.HighMem()
486 }
487
488 generateStubs := BoolDefault(d.properties.Generate_stubs, true)
489 var stubsDir android.OptionalPath
490 if generateStubs {
Colin Crosscb77f752021-03-24 12:04:44 -0700491 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"-"+"stubs.srcjar")
492 stubsDir = android.OptionalPathForPath(android.PathForModuleOut(ctx, "metalava", "stubsDir"))
Colin Cross2207f872021-03-24 12:39:08 -0700493 rule.Command().Text("rm -rf").Text(stubsDir.String())
494 rule.Command().Text("mkdir -p").Text(stubsDir.String())
495 }
496
497 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
498
499 implicitsRsp := android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"implicits.rsp")
Colin Crosscb77f752021-03-24 12:04:44 -0700500 homeDir := android.PathForModuleOut(ctx, "metalava", "home")
Colin Cross2207f872021-03-24 12:39:08 -0700501 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
502 deps.bootClasspath, deps.classpath, d.Javadoc.sourcepaths, implicitsRsp, homeDir,
Colin Cross6aa5c402021-03-24 12:28:50 -0700503 sandbox)
Colin Cross2207f872021-03-24 12:39:08 -0700504 cmd.Implicits(d.Javadoc.implicits)
505
506 d.stubsFlags(ctx, cmd, stubsDir)
507
508 d.annotationsFlags(ctx, cmd)
509 d.inclusionAnnotationsFlags(ctx, cmd)
510 d.apiLevelsAnnotationsFlags(ctx, cmd)
511
Colin Crossbc139922021-03-25 18:33:16 -0700512 d.expandArgs(ctx, cmd)
Colin Cross2207f872021-03-24 12:39:08 -0700513
Colin Cross2207f872021-03-24 12:39:08 -0700514 for _, o := range d.Javadoc.properties.Out {
515 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
516 }
517
518 // Add options for the other optional tasks: API-lint and check-released.
519 // We generate separate timestamp files for them.
520
521 doApiLint := false
522 doCheckReleased := false
523
524 // Add API lint options.
525
526 if BoolDefault(d.properties.Check_api.Api_lint.Enabled, false) {
527 doApiLint = true
528
529 newSince := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.New_since)
530 if newSince.Valid() {
531 cmd.FlagWithInput("--api-lint ", newSince.Path())
532 } else {
533 cmd.Flag("--api-lint")
534 }
Colin Crosscb77f752021-03-24 12:04:44 -0700535 d.apiLintReport = android.PathForModuleOut(ctx, "metalava", "api_lint_report.txt")
Colin Cross2207f872021-03-24 12:39:08 -0700536 cmd.FlagWithOutput("--report-even-if-suppressed ", d.apiLintReport) // TODO: Change to ":api-lint"
537
Colin Cross0d532412021-03-25 09:38:45 -0700538 // TODO(b/154317059): Clean up this allowlist by baselining and/or checking in last-released.
Colin Cross2207f872021-03-24 12:39:08 -0700539 if d.Name() != "android.car-system-stubs-docs" &&
540 d.Name() != "android.car-stubs-docs" {
541 cmd.Flag("--lints-as-errors")
542 cmd.Flag("--warnings-as-errors") // Most lints are actually warnings.
543 }
544
545 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.Baseline_file)
Colin Crosscb77f752021-03-24 12:04:44 -0700546 updatedBaselineOutput := android.PathForModuleOut(ctx, "metalava", "api_lint_baseline.txt")
547 d.apiLintTimestamp = android.PathForModuleOut(ctx, "metalava", "api_lint.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -0700548
549 // Note this string includes a special shell quote $' ... ', which decodes the "\n"s.
550 // However, because $' ... ' doesn't expand environmental variables, we can't just embed
551 // $PWD, so we have to terminate $'...', use "$PWD", then start $' ... ' again,
552 // which is why we have '"$PWD"$' in it.
553 //
554 // TODO: metalava also has a slightly different message hardcoded. Should we unify this
555 // message and metalava's one?
556 msg := `$'` + // Enclose with $' ... '
557 `************************************************************\n` +
558 `Your API changes are triggering API Lint warnings or errors.\n` +
559 `To make these errors go away, fix the code according to the\n` +
560 `error and/or warning messages above.\n` +
561 `\n` +
562 `If it is not possible to do so, there are workarounds:\n` +
563 `\n` +
564 `1. You can suppress the errors with @SuppressLint("<id>")\n`
565
566 if baselineFile.Valid() {
567 cmd.FlagWithInput("--baseline:api-lint ", baselineFile.Path())
568 cmd.FlagWithOutput("--update-baseline:api-lint ", updatedBaselineOutput)
569
570 msg += fmt.Sprintf(``+
571 `2. You can update the baseline by executing the following\n`+
572 ` command:\n`+
573 ` cp \\\n`+
574 ` "'"$PWD"$'/%s" \\\n`+
575 ` "'"$PWD"$'/%s"\n`+
576 ` To submit the revised baseline.txt to the main Android\n`+
577 ` repository, you will need approval.\n`, updatedBaselineOutput, baselineFile.Path())
578 } else {
579 msg += fmt.Sprintf(``+
580 `2. You can add a baseline file of existing lint failures\n`+
581 ` to the build rule of %s.\n`, d.Name())
582 }
583 // Note the message ends with a ' (single quote), to close the $' ... ' .
584 msg += `************************************************************\n'`
585
586 cmd.FlagWithArg("--error-message:api-lint ", msg)
587 }
588
589 // Add "check released" options. (Detect incompatible API changes from the last public release)
590
591 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") {
592 doCheckReleased = true
593
594 if len(d.Javadoc.properties.Out) > 0 {
595 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
596 }
597
598 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
599 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
600 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Last_released.Baseline_file)
Colin Crosscb77f752021-03-24 12:04:44 -0700601 updatedBaselineOutput := android.PathForModuleOut(ctx, "metalava", "last_released_baseline.txt")
Colin Cross2207f872021-03-24 12:39:08 -0700602
Colin Crosscb77f752021-03-24 12:04:44 -0700603 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "metalava", "check_last_released_api.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -0700604
605 cmd.FlagWithInput("--check-compatibility:api:released ", apiFile)
606 cmd.FlagWithInput("--check-compatibility:removed:released ", removedApiFile)
607
608 if baselineFile.Valid() {
609 cmd.FlagWithInput("--baseline:compatibility:released ", baselineFile.Path())
610 cmd.FlagWithOutput("--update-baseline:compatibility:released ", updatedBaselineOutput)
611 }
612
613 // Note this string includes quote ($' ... '), which decodes the "\n"s.
614 msg := `$'\n******************************\n` +
615 `You have tried to change the API from what has been previously released in\n` +
616 `an SDK. Please fix the errors listed above.\n` +
617 `******************************\n'`
618
619 cmd.FlagWithArg("--error-message:compatibility:released ", msg)
620 }
621
Colin Cross6aa5c402021-03-24 12:28:50 -0700622 if !sandbox {
623 // When sandboxing is enabled RuleBuilder tracks all the inputs needed for remote execution.
624 // Without it we have to do it manually.
625 impRule := android.NewRuleBuilder(pctx, ctx)
626 impCmd := impRule.Command()
627 // An action that copies the ninja generated rsp file to a new location. This allows us to
628 // add a large number of inputs to a file without exceeding bash command length limits (which
629 // would happen if we use the WriteFile rule). The cp is needed because RuleBuilder sets the
630 // rsp file to be ${output}.rsp.
631 impCmd.Text("cp").
632 FlagWithRspFileInputList("", android.PathForModuleOut(ctx, "metalava-implicits.rsp"), cmd.GetImplicits()).
633 Output(implicitsRsp)
634 impRule.Build("implicitsGen", "implicits generation")
635 cmd.Implicit(implicitsRsp)
636 }
Colin Cross2207f872021-03-24 12:39:08 -0700637
638 if generateStubs {
639 rule.Command().
640 BuiltTool("soong_zip").
641 Flag("-write_if_changed").
642 Flag("-jar").
643 FlagWithOutput("-o ", d.Javadoc.stubsSrcJar).
644 FlagWithArg("-C ", stubsDir.String()).
645 FlagWithArg("-D ", stubsDir.String())
646 }
647
648 if Bool(d.properties.Write_sdk_values) {
Colin Crosscb77f752021-03-24 12:04:44 -0700649 d.metadataZip = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"-metadata.zip")
Colin Cross2207f872021-03-24 12:39:08 -0700650 rule.Command().
651 BuiltTool("soong_zip").
652 Flag("-write_if_changed").
653 Flag("-d").
654 FlagWithOutput("-o ", d.metadataZip).
655 FlagWithArg("-C ", d.metadataDir.String()).
656 FlagWithArg("-D ", d.metadataDir.String())
657 }
658
659 // TODO: We don't really need two separate API files, but this is a reminiscence of how
660 // we used to run metalava separately for API lint and the "last_released" check. Unify them.
661 if doApiLint {
662 rule.Command().Text("touch").Output(d.apiLintTimestamp)
663 }
664 if doCheckReleased {
665 rule.Command().Text("touch").Output(d.checkLastReleasedApiTimestamp)
666 }
667
Colin Cross6aa5c402021-03-24 12:28:50 -0700668 // TODO(b/183630617): rewrapper doesn't support restat rules
669 if !sandbox {
670 rule.Restat()
671 }
Colin Cross2207f872021-03-24 12:39:08 -0700672
673 zipSyncCleanupCmd(rule, srcJarDir)
674
675 rule.Build("metalava", "metalava merged")
676
677 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") {
678
679 if len(d.Javadoc.properties.Out) > 0 {
680 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
681 }
682
683 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
684 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
685 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Current.Baseline_file)
686
687 if baselineFile.Valid() {
688 ctx.PropertyErrorf("baseline_file", "current API check can't have a baseline file. (module %s)", ctx.ModuleName())
689 }
690
Colin Crosscb77f752021-03-24 12:04:44 -0700691 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "metalava", "check_current_api.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -0700692
693 rule := android.NewRuleBuilder(pctx, ctx)
694
695 // Diff command line.
696 // -F matches the closest "opening" line, such as "package android {"
697 // and " public class Intent {".
698 diff := `diff -u -F '{ *$'`
699
700 rule.Command().Text("( true")
701 rule.Command().
702 Text(diff).
703 Input(apiFile).Input(d.apiFile)
704
705 rule.Command().
706 Text(diff).
707 Input(removedApiFile).Input(d.removedApiFile)
708
709 msg := fmt.Sprintf(`\n******************************\n`+
710 `You have tried to change the API from what has been previously approved.\n\n`+
711 `To make these errors go away, you have two choices:\n`+
712 ` 1. You can add '@hide' javadoc comments (and remove @SystemApi/@TestApi/etc)\n`+
713 ` to the new methods, etc. shown in the above diff.\n\n`+
714 ` 2. You can update current.txt and/or removed.txt by executing the following command:\n`+
715 ` m %s-update-current-api\n\n`+
716 ` To submit the revised current.txt to the main Android repository,\n`+
717 ` you will need approval.\n`+
718 `******************************\n`, ctx.ModuleName())
719
720 rule.Command().
721 Text("touch").Output(d.checkCurrentApiTimestamp).
722 Text(") || (").
723 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
724 Text("; exit 38").
725 Text(")")
726
727 rule.Build("metalavaCurrentApiCheck", "check current API")
728
Colin Crosscb77f752021-03-24 12:04:44 -0700729 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "metalava", "update_current_api.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -0700730
731 // update API rule
732 rule = android.NewRuleBuilder(pctx, ctx)
733
734 rule.Command().Text("( true")
735
736 rule.Command().
737 Text("cp").Flag("-f").
738 Input(d.apiFile).Flag(apiFile.String())
739
740 rule.Command().
741 Text("cp").Flag("-f").
742 Input(d.removedApiFile).Flag(removedApiFile.String())
743
744 msg = "failed to update public API"
745
746 rule.Command().
747 Text("touch").Output(d.updateCurrentApiTimestamp).
748 Text(") || (").
749 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
750 Text("; exit 38").
751 Text(")")
752
753 rule.Build("metalavaCurrentApiUpdate", "update current API")
754 }
755
756 if String(d.properties.Check_nullability_warnings) != "" {
757 if d.nullabilityWarningsFile == nil {
758 ctx.PropertyErrorf("check_nullability_warnings",
759 "Cannot specify check_nullability_warnings unless validating nullability")
760 }
761
762 checkNullabilityWarnings := android.PathForModuleSrc(ctx, String(d.properties.Check_nullability_warnings))
763
Colin Crosscb77f752021-03-24 12:04:44 -0700764 d.checkNullabilityWarningsTimestamp = android.PathForModuleOut(ctx, "metalava", "check_nullability_warnings.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -0700765
766 msg := fmt.Sprintf(`\n******************************\n`+
767 `The warnings encountered during nullability annotation validation did\n`+
768 `not match the checked in file of expected warnings. The diffs are shown\n`+
769 `above. You have two options:\n`+
770 ` 1. Resolve the differences by editing the nullability annotations.\n`+
771 ` 2. Update the file of expected warnings by running:\n`+
772 ` cp %s %s\n`+
773 ` and submitting the updated file as part of your change.`,
774 d.nullabilityWarningsFile, checkNullabilityWarnings)
775
776 rule := android.NewRuleBuilder(pctx, ctx)
777
778 rule.Command().
779 Text("(").
780 Text("diff").Input(checkNullabilityWarnings).Input(d.nullabilityWarningsFile).
781 Text("&&").
782 Text("touch").Output(d.checkNullabilityWarningsTimestamp).
783 Text(") || (").
784 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
785 Text("; exit 38").
786 Text(")")
787
788 rule.Build("nullabilityWarningsCheck", "nullability warnings check")
789 }
790}
791
792func StubsDefaultsFactory() android.Module {
793 module := &DocDefaults{}
794
795 module.AddProperties(
796 &JavadocProperties{},
797 &DroidstubsProperties{},
798 )
799
800 android.InitDefaultsModule(module)
801
802 return module
803}
804
805var _ android.PrebuiltInterface = (*PrebuiltStubsSources)(nil)
806
807type PrebuiltStubsSourcesProperties struct {
808 Srcs []string `android:"path"`
809}
810
811type PrebuiltStubsSources struct {
812 android.ModuleBase
813 android.DefaultableModuleBase
814 prebuilt android.Prebuilt
815 android.SdkBase
816
817 properties PrebuiltStubsSourcesProperties
818
819 stubsSrcJar android.ModuleOutPath
820}
821
822func (p *PrebuiltStubsSources) OutputFiles(tag string) (android.Paths, error) {
823 switch tag {
824 case "":
825 return android.Paths{p.stubsSrcJar}, nil
826 default:
827 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
828 }
829}
830
831func (d *PrebuiltStubsSources) StubsSrcJar() android.Path {
832 return d.stubsSrcJar
833}
834
835func (p *PrebuiltStubsSources) GenerateAndroidBuildActions(ctx android.ModuleContext) {
836 p.stubsSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
837
838 if len(p.properties.Srcs) != 1 {
839 ctx.PropertyErrorf("srcs", "must only specify one directory path, contains %d paths", len(p.properties.Srcs))
840 return
841 }
842
843 localSrcDir := p.properties.Srcs[0]
844 // Although PathForModuleSrc can return nil if either the path doesn't exist or
845 // the path components are invalid it won't in this case because no components
846 // are specified and the module directory must exist in order to get this far.
847 srcDir := android.PathForModuleSrc(ctx).(android.SourcePath).Join(ctx, localSrcDir)
848
849 // Glob the contents of the directory just in case the directory does not exist.
850 srcGlob := localSrcDir + "/**/*"
851 srcPaths := android.PathsForModuleSrc(ctx, []string{srcGlob})
852
853 rule := android.NewRuleBuilder(pctx, ctx)
854 rule.Command().
855 BuiltTool("soong_zip").
856 Flag("-write_if_changed").
857 Flag("-jar").
858 FlagWithOutput("-o ", p.stubsSrcJar).
859 FlagWithArg("-C ", srcDir.String()).
860 FlagWithRspFileInputList("-r ", p.stubsSrcJar.ReplaceExtension(ctx, "rsp"), srcPaths)
861
862 rule.Restat()
863
864 rule.Build("zip src", "Create srcjar from prebuilt source")
865}
866
867func (p *PrebuiltStubsSources) Prebuilt() *android.Prebuilt {
868 return &p.prebuilt
869}
870
871func (p *PrebuiltStubsSources) Name() string {
872 return p.prebuilt.Name(p.ModuleBase.Name())
873}
874
875// prebuilt_stubs_sources imports a set of java source files as if they were
876// generated by droidstubs.
877//
878// By default, a prebuilt_stubs_sources has a single variant that expects a
879// set of `.java` files generated by droidstubs.
880//
881// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
882// for host modules.
883//
884// Intended only for use by sdk snapshots.
885func PrebuiltStubsSourcesFactory() android.Module {
886 module := &PrebuiltStubsSources{}
887
888 module.AddProperties(&module.properties)
889
890 android.InitPrebuiltModule(module, &module.properties.Srcs)
891 android.InitSdkAwareModule(module)
892 InitDroiddocModule(module, android.HostAndDeviceSupported)
893 return module
894}