blob: 115388be52611c2a47eee9f6f0915b0c4776c8fe [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"
Anton Hansson86758ac2021-11-03 14:44:12 +000019 "path/filepath"
Colin Cross2207f872021-03-24 12:39:08 -070020 "strings"
21
22 "github.com/google/blueprint/proptools"
23
24 "android/soong/android"
25 "android/soong/java/config"
26 "android/soong/remoteexec"
27)
28
Pedro Loureirocc203502021-10-04 17:24:00 +000029// The values allowed for Droidstubs' Api_levels_sdk_type
30var allowedApiLevelSdkTypes = []string{"public", "system", "module-lib"}
31
Colin Cross2207f872021-03-24 12:39:08 -070032func init() {
33 RegisterStubsBuildComponents(android.InitRegistrationContext)
34}
35
36func RegisterStubsBuildComponents(ctx android.RegistrationContext) {
37 ctx.RegisterModuleType("stubs_defaults", StubsDefaultsFactory)
38
39 ctx.RegisterModuleType("droidstubs", DroidstubsFactory)
40 ctx.RegisterModuleType("droidstubs_host", DroidstubsHostFactory)
41
42 ctx.RegisterModuleType("prebuilt_stubs_sources", PrebuiltStubsSourcesFactory)
43}
44
45//
46// Droidstubs
47//
48type Droidstubs struct {
49 Javadoc
50 android.SdkBase
51
52 properties DroidstubsProperties
53 apiFile android.WritablePath
54 apiXmlFile android.WritablePath
55 lastReleasedApiXmlFile android.WritablePath
56 privateApiFile android.WritablePath
57 removedApiFile android.WritablePath
Colin Cross2207f872021-03-24 12:39:08 -070058 nullabilityWarningsFile android.WritablePath
59
60 checkCurrentApiTimestamp android.WritablePath
61 updateCurrentApiTimestamp android.WritablePath
62 checkLastReleasedApiTimestamp android.WritablePath
63 apiLintTimestamp android.WritablePath
64 apiLintReport android.WritablePath
65
66 checkNullabilityWarningsTimestamp android.WritablePath
67
68 annotationsZip android.WritablePath
69 apiVersionsXml android.WritablePath
70
71 apiFilePath android.Path
72 removedApiFilePath android.Path
73
74 metadataZip android.WritablePath
75 metadataDir android.WritablePath
76}
77
78type DroidstubsProperties struct {
79 // The generated public API filename by Metalava, defaults to <module>_api.txt
80 Api_filename *string
81
82 // the generated removed API filename by Metalava, defaults to <module>_removed.txt
83 Removed_api_filename *string
84
Colin Cross2207f872021-03-24 12:39:08 -070085 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
satayev783195c2021-06-23 21:49:57 +0100135 // if set to true, Metalava will allow framework SDK to contain API levels annotations.
Colin Cross2207f872021-03-24 12:39:08 -0700136 Api_levels_annotations_enabled *bool
137
Anton Hanssonc04a16e2022-05-09 09:30:26 +0000138 // Apply the api levels database created by this module rather than generating one in this droidstubs.
139 Api_levels_module *string
140
Colin Cross2207f872021-03-24 12:39:08 -0700141 // the dirs which Metalava extracts API levels annotations from.
142 Api_levels_annotations_dirs []string
143
Pedro Loureirocc203502021-10-04 17:24:00 +0000144 // the sdk kind which Metalava extracts API levels annotations from. Supports 'public', 'system' and 'module-lib' for now; defaults to public.
satayev783195c2021-06-23 21:49:57 +0100145 Api_levels_sdk_type *string
146
Colin Cross2207f872021-03-24 12:39:08 -0700147 // the filename which Metalava extracts API levels annotations from. Defaults to android.jar.
148 Api_levels_jar_filename *string
149
150 // if set to true, collect the values used by the Dev tools and
151 // write them in files packaged with the SDK. Defaults to false.
152 Write_sdk_values *bool
153}
154
Anton Hansson52609322021-05-05 10:36:05 +0100155// Used by xsd_config
156type ApiFilePath interface {
157 ApiFilePath() android.Path
158}
159
160type ApiStubsSrcProvider interface {
161 StubsSrcJar() android.Path
162}
163
164// Provider of information about API stubs, used by java_sdk_library.
165type ApiStubsProvider interface {
Anton Hanssond78eb762021-09-21 15:25:12 +0100166 AnnotationsZip() android.Path
Anton Hansson52609322021-05-05 10:36:05 +0100167 ApiFilePath
168 RemovedApiFilePath() android.Path
169
170 ApiStubsSrcProvider
171}
172
Colin Cross2207f872021-03-24 12:39:08 -0700173// droidstubs passes sources files through Metalava to generate stub .java files that only contain the API to be
174// documented, filtering out hidden classes and methods. The resulting .java files are intended to be passed to
175// a droiddoc module to generate documentation.
176func DroidstubsFactory() android.Module {
177 module := &Droidstubs{}
178
179 module.AddProperties(&module.properties,
180 &module.Javadoc.properties)
181
182 InitDroiddocModule(module, android.HostAndDeviceSupported)
183 android.InitSdkAwareModule(module)
184 return module
185}
186
187// droidstubs_host passes sources files through Metalava to generate stub .java files that only contain the API
188// to be documented, filtering out hidden classes and methods. The resulting .java files are intended to be
189// passed to a droiddoc_host module to generate documentation. Use a droidstubs_host instead of a droidstubs
190// module when symbols needed by the source files are provided by java_library_host modules.
191func DroidstubsHostFactory() android.Module {
192 module := &Droidstubs{}
193
194 module.AddProperties(&module.properties,
195 &module.Javadoc.properties)
196
197 InitDroiddocModule(module, android.HostSupported)
198 return module
199}
200
201func (d *Droidstubs) OutputFiles(tag string) (android.Paths, error) {
202 switch tag {
203 case "":
204 return android.Paths{d.stubsSrcJar}, nil
205 case ".docs.zip":
206 return android.Paths{d.docZip}, nil
207 case ".api.txt", android.DefaultDistTag:
208 // This is the default dist path for dist properties that have no tag property.
209 return android.Paths{d.apiFilePath}, nil
210 case ".removed-api.txt":
211 return android.Paths{d.removedApiFilePath}, nil
212 case ".annotations.zip":
213 return android.Paths{d.annotationsZip}, nil
214 case ".api_versions.xml":
215 return android.Paths{d.apiVersionsXml}, nil
216 default:
217 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
218 }
219}
220
Anton Hanssond78eb762021-09-21 15:25:12 +0100221func (d *Droidstubs) AnnotationsZip() android.Path {
222 return d.annotationsZip
223}
224
Colin Cross2207f872021-03-24 12:39:08 -0700225func (d *Droidstubs) ApiFilePath() android.Path {
226 return d.apiFilePath
227}
228
229func (d *Droidstubs) RemovedApiFilePath() android.Path {
230 return d.removedApiFilePath
231}
232
233func (d *Droidstubs) StubsSrcJar() android.Path {
234 return d.stubsSrcJar
235}
236
237var metalavaMergeAnnotationsDirTag = dependencyTag{name: "metalava-merge-annotations-dir"}
238var metalavaMergeInclusionAnnotationsDirTag = dependencyTag{name: "metalava-merge-inclusion-annotations-dir"}
239var metalavaAPILevelsAnnotationsDirTag = dependencyTag{name: "metalava-api-levels-annotations-dir"}
Anton Hanssonc04a16e2022-05-09 09:30:26 +0000240var metalavaAPILevelsModuleTag = dependencyTag{name: "metalava-api-levels-module-tag"}
Colin Cross2207f872021-03-24 12:39:08 -0700241
242func (d *Droidstubs) DepsMutator(ctx android.BottomUpMutatorContext) {
243 d.Javadoc.addDeps(ctx)
244
245 if len(d.properties.Merge_annotations_dirs) != 0 {
246 for _, mergeAnnotationsDir := range d.properties.Merge_annotations_dirs {
247 ctx.AddDependency(ctx.Module(), metalavaMergeAnnotationsDirTag, mergeAnnotationsDir)
248 }
249 }
250
251 if len(d.properties.Merge_inclusion_annotations_dirs) != 0 {
252 for _, mergeInclusionAnnotationsDir := range d.properties.Merge_inclusion_annotations_dirs {
253 ctx.AddDependency(ctx.Module(), metalavaMergeInclusionAnnotationsDirTag, mergeInclusionAnnotationsDir)
254 }
255 }
256
257 if len(d.properties.Api_levels_annotations_dirs) != 0 {
258 for _, apiLevelsAnnotationsDir := range d.properties.Api_levels_annotations_dirs {
259 ctx.AddDependency(ctx.Module(), metalavaAPILevelsAnnotationsDirTag, apiLevelsAnnotationsDir)
260 }
261 }
Anton Hanssonc04a16e2022-05-09 09:30:26 +0000262
263 if d.properties.Api_levels_module != nil {
264 ctx.AddDependency(ctx.Module(), metalavaAPILevelsModuleTag, proptools.String(d.properties.Api_levels_module))
265 }
Colin Cross2207f872021-03-24 12:39:08 -0700266}
267
268func (d *Droidstubs) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.OptionalPath) {
269 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
270 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
271 String(d.properties.Api_filename) != "" {
272 filename := proptools.StringDefault(d.properties.Api_filename, ctx.ModuleName()+"_api.txt")
Colin Crosscb77f752021-03-24 12:04:44 -0700273 d.apiFile = android.PathForModuleOut(ctx, "metalava", filename)
Colin Cross2207f872021-03-24 12:39:08 -0700274 cmd.FlagWithOutput("--api ", d.apiFile)
275 d.apiFilePath = d.apiFile
276 } else if sourceApiFile := proptools.String(d.properties.Check_api.Current.Api_file); sourceApiFile != "" {
277 // If check api is disabled then make the source file available for export.
278 d.apiFilePath = android.PathForModuleSrc(ctx, sourceApiFile)
279 }
280
281 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
282 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") ||
283 String(d.properties.Removed_api_filename) != "" {
284 filename := proptools.StringDefault(d.properties.Removed_api_filename, ctx.ModuleName()+"_removed.txt")
Colin Crosscb77f752021-03-24 12:04:44 -0700285 d.removedApiFile = android.PathForModuleOut(ctx, "metalava", filename)
Colin Cross2207f872021-03-24 12:39:08 -0700286 cmd.FlagWithOutput("--removed-api ", d.removedApiFile)
287 d.removedApiFilePath = d.removedApiFile
288 } else if sourceRemovedApiFile := proptools.String(d.properties.Check_api.Current.Removed_api_file); sourceRemovedApiFile != "" {
289 // If check api is disabled then make the source removed api file available for export.
290 d.removedApiFilePath = android.PathForModuleSrc(ctx, sourceRemovedApiFile)
291 }
292
Colin Cross2207f872021-03-24 12:39:08 -0700293 if Bool(d.properties.Write_sdk_values) {
Colin Crosscb77f752021-03-24 12:04:44 -0700294 d.metadataDir = android.PathForModuleOut(ctx, "metalava", "metadata")
Colin Cross2207f872021-03-24 12:39:08 -0700295 cmd.FlagWithArg("--sdk-values ", d.metadataDir.String())
296 }
297
298 if stubsDir.Valid() {
299 if Bool(d.properties.Create_doc_stubs) {
300 cmd.FlagWithArg("--doc-stubs ", stubsDir.String())
301 } else {
302 cmd.FlagWithArg("--stubs ", stubsDir.String())
303 if !Bool(d.properties.Output_javadoc_comments) {
304 cmd.Flag("--exclude-documentation-from-stubs")
305 }
306 }
307 }
308}
309
310func (d *Droidstubs) annotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
311 if Bool(d.properties.Annotations_enabled) {
312 cmd.Flag("--include-annotations")
313
Andrei Onea4985e512021-04-29 16:29:34 +0100314 cmd.FlagWithArg("--exclude-annotation ", "androidx.annotation.RequiresApi")
315
Colin Cross2207f872021-03-24 12:39:08 -0700316 validatingNullability :=
Colin Crossbc139922021-03-25 18:33:16 -0700317 strings.Contains(String(d.Javadoc.properties.Args), "--validate-nullability-from-merged-stubs") ||
Colin Cross2207f872021-03-24 12:39:08 -0700318 String(d.properties.Validate_nullability_from_list) != ""
319
320 migratingNullability := String(d.properties.Previous_api) != ""
321 if migratingNullability {
322 previousApi := android.PathForModuleSrc(ctx, String(d.properties.Previous_api))
323 cmd.FlagWithInput("--migrate-nullness ", previousApi)
324 }
325
326 if s := String(d.properties.Validate_nullability_from_list); s != "" {
327 cmd.FlagWithInput("--validate-nullability-from-list ", android.PathForModuleSrc(ctx, s))
328 }
329
330 if validatingNullability {
Colin Crosscb77f752021-03-24 12:04:44 -0700331 d.nullabilityWarningsFile = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"_nullability_warnings.txt")
Colin Cross2207f872021-03-24 12:39:08 -0700332 cmd.FlagWithOutput("--nullability-warnings-txt ", d.nullabilityWarningsFile)
333 }
334
Colin Crosscb77f752021-03-24 12:04:44 -0700335 d.annotationsZip = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"_annotations.zip")
Colin Cross2207f872021-03-24 12:39:08 -0700336 cmd.FlagWithOutput("--extract-annotations ", d.annotationsZip)
337
338 if len(d.properties.Merge_annotations_dirs) != 0 {
339 d.mergeAnnoDirFlags(ctx, cmd)
340 }
341
342 // TODO(tnorbye): find owners to fix these warnings when annotation was enabled.
343 cmd.FlagWithArg("--hide ", "HiddenTypedefConstant").
344 FlagWithArg("--hide ", "SuperfluousPrefix").
Sam Gilbert049af112022-03-04 16:03:53 -0500345 FlagWithArg("--hide ", "AnnotationExtraction").
346 // b/222738070
Sam Gilbert675f0b42022-03-08 11:24:44 -0500347 FlagWithArg("--hide ", "BannedThrow").
348 // b/223382732
349 FlagWithArg("--hide ", "ChangedDefault")
Colin Cross2207f872021-03-24 12:39:08 -0700350 }
351}
352
353func (d *Droidstubs) mergeAnnoDirFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
354 ctx.VisitDirectDepsWithTag(metalavaMergeAnnotationsDirTag, func(m android.Module) {
355 if t, ok := m.(*ExportedDroiddocDir); ok {
356 cmd.FlagWithArg("--merge-qualifier-annotations ", t.dir.String()).Implicits(t.deps)
357 } else {
358 ctx.PropertyErrorf("merge_annotations_dirs",
359 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
360 }
361 })
362}
363
364func (d *Droidstubs) inclusionAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
365 ctx.VisitDirectDepsWithTag(metalavaMergeInclusionAnnotationsDirTag, func(m android.Module) {
366 if t, ok := m.(*ExportedDroiddocDir); ok {
367 cmd.FlagWithArg("--merge-inclusion-annotations ", t.dir.String()).Implicits(t.deps)
368 } else {
369 ctx.PropertyErrorf("merge_inclusion_annotations_dirs",
370 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
371 }
372 })
373}
374
375func (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Anton Hanssonc04a16e2022-05-09 09:30:26 +0000376 var apiVersions android.Path
377 if proptools.Bool(d.properties.Api_levels_annotations_enabled) {
378 d.apiLevelsGenerationFlags(ctx, cmd)
379 apiVersions = d.apiVersionsXml
380 } else {
381 ctx.VisitDirectDepsWithTag(metalavaAPILevelsModuleTag, func(m android.Module) {
382 if s, ok := m.(*Droidstubs); ok {
383 apiVersions = s.apiVersionsXml
384 } else {
385 ctx.PropertyErrorf("api_levels_module",
386 "module %q is not a droidstubs module", ctx.OtherModuleName(m))
387 }
388 })
Colin Cross2207f872021-03-24 12:39:08 -0700389 }
Anton Hanssonc04a16e2022-05-09 09:30:26 +0000390 if apiVersions != nil {
391 cmd.FlagWithArg("--current-version ", ctx.Config().PlatformSdkVersion().String())
392 cmd.FlagWithArg("--current-codename ", ctx.Config().PlatformSdkCodename())
393 cmd.FlagWithInput("--apply-api-levels ", apiVersions)
394 }
395}
Colin Cross2207f872021-03-24 12:39:08 -0700396
Anton Hanssonc04a16e2022-05-09 09:30:26 +0000397func (d *Droidstubs) apiLevelsGenerationFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
Colin Cross2207f872021-03-24 12:39:08 -0700398 if len(d.properties.Api_levels_annotations_dirs) == 0 {
399 ctx.PropertyErrorf("api_levels_annotations_dirs",
400 "has to be non-empty if api levels annotations was enabled!")
401 }
402
Anton Hanssonc04a16e2022-05-09 09:30:26 +0000403 d.apiVersionsXml = android.PathForModuleOut(ctx, "metalava", "api-versions.xml")
Colin Cross2207f872021-03-24 12:39:08 -0700404 cmd.FlagWithOutput("--generate-api-levels ", d.apiVersionsXml)
Colin Cross2207f872021-03-24 12:39:08 -0700405
406 filename := proptools.StringDefault(d.properties.Api_levels_jar_filename, "android.jar")
407
satayev783195c2021-06-23 21:49:57 +0100408 var dirs []string
Colin Cross2207f872021-03-24 12:39:08 -0700409 ctx.VisitDirectDepsWithTag(metalavaAPILevelsAnnotationsDirTag, func(m android.Module) {
410 if t, ok := m.(*ExportedDroiddocDir); ok {
411 for _, dep := range t.deps {
Colin Cross5f6ffc72021-03-29 21:54:45 -0700412 if dep.Base() == filename {
413 cmd.Implicit(dep)
414 }
415 if filename != "android.jar" && dep.Base() == "android.jar" {
416 // Metalava implicitly searches these patterns:
417 // prebuilts/tools/common/api-versions/android-%/android.jar
418 // prebuilts/sdk/%/public/android.jar
419 // Add android.jar files from the api_levels_annotations_dirs directories to try
420 // to satisfy these patterns. If Metalava can't find a match for an API level
421 // between 1 and 28 in at least one pattern it will fail.
Colin Cross2207f872021-03-24 12:39:08 -0700422 cmd.Implicit(dep)
423 }
424 }
satayev783195c2021-06-23 21:49:57 +0100425
426 dirs = append(dirs, t.dir.String())
Colin Cross2207f872021-03-24 12:39:08 -0700427 } else {
428 ctx.PropertyErrorf("api_levels_annotations_dirs",
429 "module %q is not a metalava api-levels-annotations dir", ctx.OtherModuleName(m))
430 }
431 })
satayev783195c2021-06-23 21:49:57 +0100432
433 // Add all relevant --android-jar-pattern patterns for Metalava.
434 // When parsing a stub jar for a specific version, Metalava picks the first pattern that defines
435 // an actual file present on disk (in the order the patterns were passed). For system APIs for
436 // privileged apps that are only defined since API level 21 (Lollipop), fallback to public stubs
Pedro Loureirocc203502021-10-04 17:24:00 +0000437 // for older releases. Similarly, module-lib falls back to system API.
438 var sdkDirs []string
439 switch proptools.StringDefault(d.properties.Api_levels_sdk_type, "public") {
440 case "module-lib":
441 sdkDirs = []string{"module-lib", "system", "public"}
442 case "system":
443 sdkDirs = []string{"system", "public"}
444 case "public":
445 sdkDirs = []string{"public"}
446 default:
447 ctx.PropertyErrorf("api_levels_sdk_type", "needs to be one of %v", allowedApiLevelSdkTypes)
448 return
satayev783195c2021-06-23 21:49:57 +0100449 }
Pedro Loureirocc203502021-10-04 17:24:00 +0000450
451 for _, sdkDir := range sdkDirs {
452 for _, dir := range dirs {
453 cmd.FlagWithArg("--android-jar-pattern ", fmt.Sprintf("%s/%%/%s/%s", dir, sdkDir, filename))
454 }
satayev783195c2021-06-23 21:49:57 +0100455 }
Colin Cross2207f872021-03-24 12:39:08 -0700456}
457
Colin Crosse52c2ac2022-03-28 17:03:35 -0700458func metalavaUseRbe(ctx android.ModuleContext) bool {
459 return ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_METALAVA")
460}
461
Colin Cross2207f872021-03-24 12:39:08 -0700462func metalavaCmd(ctx android.ModuleContext, rule *android.RuleBuilder, javaVersion javaVersion, srcs android.Paths,
Anton Hansson556e8142021-06-04 16:20:25 +0100463 srcJarList android.Path, bootclasspath, classpath classpath, homeDir android.WritablePath) *android.RuleBuilderCommand {
Colin Cross2207f872021-03-24 12:39:08 -0700464 rule.Command().Text("rm -rf").Flag(homeDir.String())
465 rule.Command().Text("mkdir -p").Flag(homeDir.String())
466
Anton Hansson556e8142021-06-04 16:20:25 +0100467 cmd := rule.Command()
Colin Cross2207f872021-03-24 12:39:08 -0700468 cmd.FlagWithArg("ANDROID_PREFS_ROOT=", homeDir.String())
469
Colin Crosse52c2ac2022-03-28 17:03:35 -0700470 if metalavaUseRbe(ctx) {
Colin Cross2207f872021-03-24 12:39:08 -0700471 rule.Remoteable(android.RemoteRuleSupports{RBE: true})
Colin Cross8095c292021-03-30 16:40:48 -0700472 execStrategy := ctx.Config().GetenvWithDefault("RBE_METALAVA_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
473 labels := map[string]string{"type": "tool", "name": "metalava"}
474 // TODO: metalava pool rejects these jobs
475 pool := ctx.Config().GetenvWithDefault("RBE_METALAVA_POOL", "java16")
476 rule.Rewrapper(&remoteexec.REParams{
477 Labels: labels,
478 ExecStrategy: execStrategy,
479 ToolchainInputs: []string{config.JavaCmd(ctx).String()},
480 Platform: map[string]string{remoteexec.PoolKey: pool},
481 })
Colin Cross2207f872021-03-24 12:39:08 -0700482 }
483
Colin Cross6aa5c402021-03-24 12:28:50 -0700484 cmd.BuiltTool("metalava").ImplicitTool(ctx.Config().HostJavaToolPath(ctx, "metalava.jar")).
Colin Cross2207f872021-03-24 12:39:08 -0700485 Flag(config.JavacVmFlags).
486 Flag("-J--add-opens=java.base/java.util=ALL-UNNAMED").
487 FlagWithArg("-encoding ", "UTF-8").
488 FlagWithArg("-source ", javaVersion.String()).
489 FlagWithRspFileInputList("@", android.PathForModuleOut(ctx, "metalava.rsp"), srcs).
490 FlagWithInput("@", srcJarList)
491
Colin Cross2207f872021-03-24 12:39:08 -0700492 if len(bootclasspath) > 0 {
493 cmd.FlagWithInputList("-bootclasspath ", bootclasspath.Paths(), ":")
494 }
495
496 if len(classpath) > 0 {
497 cmd.FlagWithInputList("-classpath ", classpath.Paths(), ":")
498 }
499
Colin Cross2207f872021-03-24 12:39:08 -0700500 cmd.Flag("--no-banner").
501 Flag("--color").
502 Flag("--quiet").
503 Flag("--format=v2").
504 FlagWithArg("--repeat-errors-max ", "10").
Sam Gilbert09cb5db2021-10-06 11:28:28 -0400505 FlagWithArg("--hide ", "UnresolvedImport").
Ember Rose7c57af32022-04-01 10:34:44 -0400506 FlagWithArg("--hide ", "InvalidNullabilityOverride").
Sam Gilbert28e41282022-03-09 15:24:48 +0000507 // b/223382732
508 FlagWithArg("--hide ", "ChangedDefault")
Colin Cross2207f872021-03-24 12:39:08 -0700509
510 return cmd
511}
512
513func (d *Droidstubs) GenerateAndroidBuildActions(ctx android.ModuleContext) {
514 deps := d.Javadoc.collectDeps(ctx)
515
Jiyong Parkf1691d22021-03-29 20:11:58 +0900516 javaVersion := getJavaVersion(ctx, String(d.Javadoc.properties.Java_version), android.SdkContext(d))
Colin Cross2207f872021-03-24 12:39:08 -0700517
518 // Create rule for metalava
519
Colin Crosscb77f752021-03-24 12:04:44 -0700520 srcJarDir := android.PathForModuleOut(ctx, "metalava", "srcjars")
Colin Cross2207f872021-03-24 12:39:08 -0700521
522 rule := android.NewRuleBuilder(pctx, ctx)
523
Colin Cross8095c292021-03-30 16:40:48 -0700524 rule.Sbox(android.PathForModuleOut(ctx, "metalava"),
525 android.PathForModuleOut(ctx, "metalava.sbox.textproto")).
526 SandboxInputs()
Colin Cross6aa5c402021-03-24 12:28:50 -0700527
Colin Cross2207f872021-03-24 12:39:08 -0700528 if BoolDefault(d.properties.High_mem, false) {
529 // This metalava run uses lots of memory, restrict the number of metalava jobs that can run in parallel.
530 rule.HighMem()
531 }
532
533 generateStubs := BoolDefault(d.properties.Generate_stubs, true)
534 var stubsDir android.OptionalPath
535 if generateStubs {
Colin Crosscb77f752021-03-24 12:04:44 -0700536 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"-"+"stubs.srcjar")
537 stubsDir = android.OptionalPathForPath(android.PathForModuleOut(ctx, "metalava", "stubsDir"))
Colin Cross2207f872021-03-24 12:39:08 -0700538 rule.Command().Text("rm -rf").Text(stubsDir.String())
539 rule.Command().Text("mkdir -p").Text(stubsDir.String())
540 }
541
542 srcJarList := zipSyncCmd(ctx, rule, srcJarDir, d.Javadoc.srcJars)
543
Colin Crosscb77f752021-03-24 12:04:44 -0700544 homeDir := android.PathForModuleOut(ctx, "metalava", "home")
Colin Cross2207f872021-03-24 12:39:08 -0700545 cmd := metalavaCmd(ctx, rule, javaVersion, d.Javadoc.srcFiles, srcJarList,
Anton Hansson556e8142021-06-04 16:20:25 +0100546 deps.bootClasspath, deps.classpath, homeDir)
Colin Cross2207f872021-03-24 12:39:08 -0700547 cmd.Implicits(d.Javadoc.implicits)
548
549 d.stubsFlags(ctx, cmd, stubsDir)
550
551 d.annotationsFlags(ctx, cmd)
552 d.inclusionAnnotationsFlags(ctx, cmd)
553 d.apiLevelsAnnotationsFlags(ctx, cmd)
554
Colin Crossbc139922021-03-25 18:33:16 -0700555 d.expandArgs(ctx, cmd)
Colin Cross2207f872021-03-24 12:39:08 -0700556
Colin Cross2207f872021-03-24 12:39:08 -0700557 for _, o := range d.Javadoc.properties.Out {
558 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
559 }
560
561 // Add options for the other optional tasks: API-lint and check-released.
562 // We generate separate timestamp files for them.
563
564 doApiLint := false
565 doCheckReleased := false
566
567 // Add API lint options.
568
569 if BoolDefault(d.properties.Check_api.Api_lint.Enabled, false) {
570 doApiLint = true
571
572 newSince := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.New_since)
573 if newSince.Valid() {
574 cmd.FlagWithInput("--api-lint ", newSince.Path())
575 } else {
576 cmd.Flag("--api-lint")
577 }
Colin Crosscb77f752021-03-24 12:04:44 -0700578 d.apiLintReport = android.PathForModuleOut(ctx, "metalava", "api_lint_report.txt")
Colin Cross2207f872021-03-24 12:39:08 -0700579 cmd.FlagWithOutput("--report-even-if-suppressed ", d.apiLintReport) // TODO: Change to ":api-lint"
580
Colin Cross0d532412021-03-25 09:38:45 -0700581 // TODO(b/154317059): Clean up this allowlist by baselining and/or checking in last-released.
Colin Cross2207f872021-03-24 12:39:08 -0700582 if d.Name() != "android.car-system-stubs-docs" &&
583 d.Name() != "android.car-stubs-docs" {
584 cmd.Flag("--lints-as-errors")
585 cmd.Flag("--warnings-as-errors") // Most lints are actually warnings.
586 }
587
588 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.Baseline_file)
Colin Crosscb77f752021-03-24 12:04:44 -0700589 updatedBaselineOutput := android.PathForModuleOut(ctx, "metalava", "api_lint_baseline.txt")
590 d.apiLintTimestamp = android.PathForModuleOut(ctx, "metalava", "api_lint.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -0700591
592 // Note this string includes a special shell quote $' ... ', which decodes the "\n"s.
Colin Cross2207f872021-03-24 12:39:08 -0700593 //
594 // TODO: metalava also has a slightly different message hardcoded. Should we unify this
595 // message and metalava's one?
596 msg := `$'` + // Enclose with $' ... '
597 `************************************************************\n` +
598 `Your API changes are triggering API Lint warnings or errors.\n` +
599 `To make these errors go away, fix the code according to the\n` +
600 `error and/or warning messages above.\n` +
601 `\n` +
602 `If it is not possible to do so, there are workarounds:\n` +
603 `\n` +
Aurimas Liutikasb23b7452021-05-24 18:00:37 +0000604 `1. You can suppress the errors with @SuppressLint("<id>")\n` +
605 ` where the <id> is given in brackets in the error message above.\n`
Colin Cross2207f872021-03-24 12:39:08 -0700606
607 if baselineFile.Valid() {
608 cmd.FlagWithInput("--baseline:api-lint ", baselineFile.Path())
609 cmd.FlagWithOutput("--update-baseline:api-lint ", updatedBaselineOutput)
610
611 msg += fmt.Sprintf(``+
612 `2. You can update the baseline by executing the following\n`+
613 ` command:\n`+
Colin Cross63eeda02021-04-15 19:01:57 -0700614 ` (cd $ANDROID_BUILD_TOP && cp \\\n`+
615 ` "%s" \\\n`+
616 ` "%s")\n`+
Colin Cross2207f872021-03-24 12:39:08 -0700617 ` To submit the revised baseline.txt to the main Android\n`+
618 ` repository, you will need approval.\n`, updatedBaselineOutput, baselineFile.Path())
619 } else {
620 msg += fmt.Sprintf(``+
621 `2. You can add a baseline file of existing lint failures\n`+
622 ` to the build rule of %s.\n`, d.Name())
623 }
624 // Note the message ends with a ' (single quote), to close the $' ... ' .
625 msg += `************************************************************\n'`
626
627 cmd.FlagWithArg("--error-message:api-lint ", msg)
628 }
629
630 // Add "check released" options. (Detect incompatible API changes from the last public release)
631
632 if apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released") {
633 doCheckReleased = true
634
635 if len(d.Javadoc.properties.Out) > 0 {
636 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
637 }
638
639 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Api_file))
640 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Last_released.Removed_api_file))
641 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Last_released.Baseline_file)
Colin Crosscb77f752021-03-24 12:04:44 -0700642 updatedBaselineOutput := android.PathForModuleOut(ctx, "metalava", "last_released_baseline.txt")
Colin Cross2207f872021-03-24 12:39:08 -0700643
Colin Crosscb77f752021-03-24 12:04:44 -0700644 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, "metalava", "check_last_released_api.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -0700645
646 cmd.FlagWithInput("--check-compatibility:api:released ", apiFile)
647 cmd.FlagWithInput("--check-compatibility:removed:released ", removedApiFile)
648
649 if baselineFile.Valid() {
650 cmd.FlagWithInput("--baseline:compatibility:released ", baselineFile.Path())
651 cmd.FlagWithOutput("--update-baseline:compatibility:released ", updatedBaselineOutput)
652 }
653
654 // Note this string includes quote ($' ... '), which decodes the "\n"s.
655 msg := `$'\n******************************\n` +
656 `You have tried to change the API from what has been previously released in\n` +
657 `an SDK. Please fix the errors listed above.\n` +
658 `******************************\n'`
659
660 cmd.FlagWithArg("--error-message:compatibility:released ", msg)
661 }
662
Colin Cross2207f872021-03-24 12:39:08 -0700663 if generateStubs {
664 rule.Command().
665 BuiltTool("soong_zip").
666 Flag("-write_if_changed").
667 Flag("-jar").
668 FlagWithOutput("-o ", d.Javadoc.stubsSrcJar).
669 FlagWithArg("-C ", stubsDir.String()).
670 FlagWithArg("-D ", stubsDir.String())
671 }
672
673 if Bool(d.properties.Write_sdk_values) {
Colin Crosscb77f752021-03-24 12:04:44 -0700674 d.metadataZip = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"-metadata.zip")
Colin Cross2207f872021-03-24 12:39:08 -0700675 rule.Command().
676 BuiltTool("soong_zip").
677 Flag("-write_if_changed").
678 Flag("-d").
679 FlagWithOutput("-o ", d.metadataZip).
680 FlagWithArg("-C ", d.metadataDir.String()).
681 FlagWithArg("-D ", d.metadataDir.String())
682 }
683
684 // TODO: We don't really need two separate API files, but this is a reminiscence of how
685 // we used to run metalava separately for API lint and the "last_released" check. Unify them.
686 if doApiLint {
687 rule.Command().Text("touch").Output(d.apiLintTimestamp)
688 }
689 if doCheckReleased {
690 rule.Command().Text("touch").Output(d.checkLastReleasedApiTimestamp)
691 }
692
Colin Cross6aa5c402021-03-24 12:28:50 -0700693 // TODO(b/183630617): rewrapper doesn't support restat rules
Colin Crosse52c2ac2022-03-28 17:03:35 -0700694 if !metalavaUseRbe(ctx) {
695 rule.Restat()
696 }
Colin Cross2207f872021-03-24 12:39:08 -0700697
698 zipSyncCleanupCmd(rule, srcJarDir)
699
700 rule.Build("metalava", "metalava merged")
701
702 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") {
703
704 if len(d.Javadoc.properties.Out) > 0 {
705 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
706 }
707
708 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
709 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
710 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Current.Baseline_file)
711
712 if baselineFile.Valid() {
713 ctx.PropertyErrorf("baseline_file", "current API check can't have a baseline file. (module %s)", ctx.ModuleName())
714 }
715
Colin Crosscb77f752021-03-24 12:04:44 -0700716 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, "metalava", "check_current_api.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -0700717
718 rule := android.NewRuleBuilder(pctx, ctx)
719
720 // Diff command line.
721 // -F matches the closest "opening" line, such as "package android {"
722 // and " public class Intent {".
723 diff := `diff -u -F '{ *$'`
724
725 rule.Command().Text("( true")
726 rule.Command().
727 Text(diff).
728 Input(apiFile).Input(d.apiFile)
729
730 rule.Command().
731 Text(diff).
732 Input(removedApiFile).Input(d.removedApiFile)
733
734 msg := fmt.Sprintf(`\n******************************\n`+
735 `You have tried to change the API from what has been previously approved.\n\n`+
736 `To make these errors go away, you have two choices:\n`+
737 ` 1. You can add '@hide' javadoc comments (and remove @SystemApi/@TestApi/etc)\n`+
738 ` to the new methods, etc. shown in the above diff.\n\n`+
739 ` 2. You can update current.txt and/or removed.txt by executing the following command:\n`+
740 ` m %s-update-current-api\n\n`+
741 ` To submit the revised current.txt to the main Android repository,\n`+
742 ` you will need approval.\n`+
743 `******************************\n`, ctx.ModuleName())
744
745 rule.Command().
746 Text("touch").Output(d.checkCurrentApiTimestamp).
747 Text(") || (").
748 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
749 Text("; exit 38").
750 Text(")")
751
752 rule.Build("metalavaCurrentApiCheck", "check current API")
753
Colin Crosscb77f752021-03-24 12:04:44 -0700754 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, "metalava", "update_current_api.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -0700755
756 // update API rule
757 rule = android.NewRuleBuilder(pctx, ctx)
758
759 rule.Command().Text("( true")
760
761 rule.Command().
762 Text("cp").Flag("-f").
763 Input(d.apiFile).Flag(apiFile.String())
764
765 rule.Command().
766 Text("cp").Flag("-f").
767 Input(d.removedApiFile).Flag(removedApiFile.String())
768
769 msg = "failed to update public API"
770
771 rule.Command().
772 Text("touch").Output(d.updateCurrentApiTimestamp).
773 Text(") || (").
774 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
775 Text("; exit 38").
776 Text(")")
777
778 rule.Build("metalavaCurrentApiUpdate", "update current API")
779 }
780
781 if String(d.properties.Check_nullability_warnings) != "" {
782 if d.nullabilityWarningsFile == nil {
783 ctx.PropertyErrorf("check_nullability_warnings",
784 "Cannot specify check_nullability_warnings unless validating nullability")
785 }
786
787 checkNullabilityWarnings := android.PathForModuleSrc(ctx, String(d.properties.Check_nullability_warnings))
788
Colin Crosscb77f752021-03-24 12:04:44 -0700789 d.checkNullabilityWarningsTimestamp = android.PathForModuleOut(ctx, "metalava", "check_nullability_warnings.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -0700790
791 msg := fmt.Sprintf(`\n******************************\n`+
792 `The warnings encountered during nullability annotation validation did\n`+
793 `not match the checked in file of expected warnings. The diffs are shown\n`+
794 `above. You have two options:\n`+
795 ` 1. Resolve the differences by editing the nullability annotations.\n`+
796 ` 2. Update the file of expected warnings by running:\n`+
797 ` cp %s %s\n`+
798 ` and submitting the updated file as part of your change.`,
799 d.nullabilityWarningsFile, checkNullabilityWarnings)
800
801 rule := android.NewRuleBuilder(pctx, ctx)
802
803 rule.Command().
804 Text("(").
805 Text("diff").Input(checkNullabilityWarnings).Input(d.nullabilityWarningsFile).
806 Text("&&").
807 Text("touch").Output(d.checkNullabilityWarningsTimestamp).
808 Text(") || (").
809 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
810 Text("; exit 38").
811 Text(")")
812
813 rule.Build("nullabilityWarningsCheck", "nullability warnings check")
814 }
815}
816
817func StubsDefaultsFactory() android.Module {
818 module := &DocDefaults{}
819
820 module.AddProperties(
821 &JavadocProperties{},
822 &DroidstubsProperties{},
823 )
824
825 android.InitDefaultsModule(module)
826
827 return module
828}
829
830var _ android.PrebuiltInterface = (*PrebuiltStubsSources)(nil)
831
832type PrebuiltStubsSourcesProperties struct {
833 Srcs []string `android:"path"`
834}
835
836type PrebuiltStubsSources struct {
837 android.ModuleBase
838 android.DefaultableModuleBase
839 prebuilt android.Prebuilt
840 android.SdkBase
841
842 properties PrebuiltStubsSourcesProperties
843
kgui67007242022-01-25 13:50:25 +0800844 stubsSrcJar android.Path
Colin Cross2207f872021-03-24 12:39:08 -0700845}
846
847func (p *PrebuiltStubsSources) OutputFiles(tag string) (android.Paths, error) {
848 switch tag {
849 case "":
850 return android.Paths{p.stubsSrcJar}, nil
851 default:
852 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
853 }
854}
855
856func (d *PrebuiltStubsSources) StubsSrcJar() android.Path {
857 return d.stubsSrcJar
858}
859
860func (p *PrebuiltStubsSources) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross2207f872021-03-24 12:39:08 -0700861 if len(p.properties.Srcs) != 1 {
Anton Hansson86758ac2021-11-03 14:44:12 +0000862 ctx.PropertyErrorf("srcs", "must only specify one directory path or srcjar, contains %d paths", len(p.properties.Srcs))
Colin Cross2207f872021-03-24 12:39:08 -0700863 return
864 }
865
Anton Hansson86758ac2021-11-03 14:44:12 +0000866 src := p.properties.Srcs[0]
867 if filepath.Ext(src) == ".srcjar" {
868 // This is a srcjar. We can use it directly.
869 p.stubsSrcJar = android.PathForModuleSrc(ctx, src)
870 } else {
871 outPath := android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
Colin Cross2207f872021-03-24 12:39:08 -0700872
Anton Hansson86758ac2021-11-03 14:44:12 +0000873 // This is a directory. Glob the contents just in case the directory does not exist.
874 srcGlob := src + "/**/*"
875 srcPaths := android.PathsForModuleSrc(ctx, []string{srcGlob})
Colin Cross2207f872021-03-24 12:39:08 -0700876
Anton Hansson86758ac2021-11-03 14:44:12 +0000877 // Although PathForModuleSrc can return nil if either the path doesn't exist or
878 // the path components are invalid it won't in this case because no components
879 // are specified and the module directory must exist in order to get this far.
880 srcDir := android.PathForModuleSrc(ctx).(android.SourcePath).Join(ctx, src)
Colin Cross2207f872021-03-24 12:39:08 -0700881
Anton Hansson86758ac2021-11-03 14:44:12 +0000882 rule := android.NewRuleBuilder(pctx, ctx)
883 rule.Command().
884 BuiltTool("soong_zip").
885 Flag("-write_if_changed").
886 Flag("-jar").
887 FlagWithOutput("-o ", outPath).
888 FlagWithArg("-C ", srcDir.String()).
889 FlagWithRspFileInputList("-r ", outPath.ReplaceExtension(ctx, "rsp"), srcPaths)
890 rule.Restat()
891 rule.Build("zip src", "Create srcjar from prebuilt source")
892 p.stubsSrcJar = outPath
893 }
Colin Cross2207f872021-03-24 12:39:08 -0700894}
895
896func (p *PrebuiltStubsSources) Prebuilt() *android.Prebuilt {
897 return &p.prebuilt
898}
899
900func (p *PrebuiltStubsSources) Name() string {
901 return p.prebuilt.Name(p.ModuleBase.Name())
902}
903
904// prebuilt_stubs_sources imports a set of java source files as if they were
905// generated by droidstubs.
906//
907// By default, a prebuilt_stubs_sources has a single variant that expects a
908// set of `.java` files generated by droidstubs.
909//
910// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
911// for host modules.
912//
913// Intended only for use by sdk snapshots.
914func PrebuiltStubsSourcesFactory() android.Module {
915 module := &PrebuiltStubsSources{}
916
917 module.AddProperties(&module.properties)
918
919 android.InitPrebuiltModule(module, &module.properties.Srcs)
920 android.InitSdkAwareModule(module)
921 InitDroiddocModule(module, android.HostAndDeviceSupported)
922 return module
923}