blob: 1e353bc7a5cc4b86d47e46cad5e36abdbab6f482 [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"
MÃ¥rten Kongstad802ae0f2022-07-27 13:47:32 +020020 "regexp"
Colin Cross2207f872021-03-24 12:39:08 -070021 "strings"
22
23 "github.com/google/blueprint/proptools"
24
25 "android/soong/android"
26 "android/soong/java/config"
27 "android/soong/remoteexec"
28)
29
Pedro Loureirocc203502021-10-04 17:24:00 +000030// The values allowed for Droidstubs' Api_levels_sdk_type
Cole Faust051fa912022-10-05 12:45:42 -070031var allowedApiLevelSdkTypes = []string{"public", "system", "module-lib", "system-server"}
Pedro Loureirocc203502021-10-04 17:24:00 +000032
Jihoon Kang6592e872023-12-19 01:13:16 +000033type StubsType int
34
35const (
36 Everything StubsType = iota
37 Runtime
38 Exportable
Jihoon Kang78f89142023-12-27 01:40:29 +000039 Unavailable
Jihoon Kang6592e872023-12-19 01:13:16 +000040)
41
42func (s StubsType) String() string {
43 switch s {
44 case Everything:
45 return "everything"
46 case Runtime:
47 return "runtime"
48 case Exportable:
49 return "exportable"
50 default:
51 return ""
52 }
53}
54
Jihoon Kang5d701272024-02-15 21:53:49 +000055func StringToStubsType(s string) StubsType {
56 switch strings.ToLower(s) {
57 case Everything.String():
58 return Everything
59 case Runtime.String():
60 return Runtime
61 case Exportable.String():
62 return Exportable
63 default:
64 return Unavailable
65 }
66}
67
Colin Cross2207f872021-03-24 12:39:08 -070068func init() {
69 RegisterStubsBuildComponents(android.InitRegistrationContext)
70}
71
72func RegisterStubsBuildComponents(ctx android.RegistrationContext) {
73 ctx.RegisterModuleType("stubs_defaults", StubsDefaultsFactory)
74
75 ctx.RegisterModuleType("droidstubs", DroidstubsFactory)
76 ctx.RegisterModuleType("droidstubs_host", DroidstubsHostFactory)
77
78 ctx.RegisterModuleType("prebuilt_stubs_sources", PrebuiltStubsSourcesFactory)
79}
80
Jihoon Kangee113282024-01-23 00:16:41 +000081type stubsArtifacts struct {
82 nullabilityWarningsFile android.WritablePath
83 annotationsZip android.WritablePath
84 apiVersionsXml android.WritablePath
85 metadataZip android.WritablePath
86 metadataDir android.WritablePath
87}
88
Colin Cross2207f872021-03-24 12:39:08 -070089// Droidstubs
Colin Cross2207f872021-03-24 12:39:08 -070090type Droidstubs struct {
91 Javadoc
Spandan Das2cc80ba2023-10-27 17:21:52 +000092 embeddableInModuleAndImport
Colin Cross2207f872021-03-24 12:39:08 -070093
Jihoon Kangee113282024-01-23 00:16:41 +000094 properties DroidstubsProperties
95 apiFile android.Path
96 removedApiFile android.Path
Colin Cross2207f872021-03-24 12:39:08 -070097
98 checkCurrentApiTimestamp android.WritablePath
99 updateCurrentApiTimestamp android.WritablePath
100 checkLastReleasedApiTimestamp android.WritablePath
101 apiLintTimestamp android.WritablePath
102 apiLintReport android.WritablePath
103
104 checkNullabilityWarningsTimestamp android.WritablePath
105
Jihoon Kangee113282024-01-23 00:16:41 +0000106 everythingArtifacts stubsArtifacts
107 exportableArtifacts stubsArtifacts
Jihoon Kang3c89f042023-12-19 02:40:22 +0000108
Jihoon Kangee113282024-01-23 00:16:41 +0000109 exportableApiFile android.WritablePath
110 exportableRemovedApiFile android.WritablePath
Colin Cross2207f872021-03-24 12:39:08 -0700111}
112
113type DroidstubsProperties struct {
114 // The generated public API filename by Metalava, defaults to <module>_api.txt
115 Api_filename *string
116
117 // the generated removed API filename by Metalava, defaults to <module>_removed.txt
118 Removed_api_filename *string
119
Colin Cross2207f872021-03-24 12:39:08 -0700120 Check_api struct {
121 Last_released ApiToCheck
122
123 Current ApiToCheck
124
125 Api_lint struct {
126 Enabled *bool
127
128 // If set, performs api_lint on any new APIs not found in the given signature file
129 New_since *string `android:"path"`
130
131 // If not blank, path to the baseline txt file for approved API lint violations.
132 Baseline_file *string `android:"path"`
133 }
134 }
135
136 // user can specify the version of previous released API file in order to do compatibility check.
137 Previous_api *string `android:"path"`
138
139 // is set to true, Metalava will allow framework SDK to contain annotations.
140 Annotations_enabled *bool
141
142 // a list of top-level directories containing files to merge qualifier annotations (i.e. those intended to be included in the stubs written) from.
143 Merge_annotations_dirs []string
144
145 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
146 Merge_inclusion_annotations_dirs []string
147
148 // a file containing a list of classes to do nullability validation for.
149 Validate_nullability_from_list *string
150
151 // a file containing expected warnings produced by validation of nullability annotations.
152 Check_nullability_warnings *string
153
154 // if set to true, allow Metalava to generate doc_stubs source files. Defaults to false.
155 Create_doc_stubs *bool
156
157 // if set to true, cause Metalava to output Javadoc comments in the stubs source files. Defaults to false.
158 // Has no effect if create_doc_stubs: true.
159 Output_javadoc_comments *bool
160
161 // if set to false then do not write out stubs. Defaults to true.
162 //
163 // TODO(b/146727827): Remove capability when we do not need to generate stubs and API separately.
164 Generate_stubs *bool
165
166 // if set to true, provides a hint to the build system that this rule uses a lot of memory,
Liz Kammer170dd722023-10-16 15:08:39 -0400167 // which can be used for scheduling purposes
Colin Cross2207f872021-03-24 12:39:08 -0700168 High_mem *bool
169
satayev783195c2021-06-23 21:49:57 +0100170 // if set to true, Metalava will allow framework SDK to contain API levels annotations.
Colin Cross2207f872021-03-24 12:39:08 -0700171 Api_levels_annotations_enabled *bool
172
Anton Hanssonc04a16e2022-05-09 09:30:26 +0000173 // Apply the api levels database created by this module rather than generating one in this droidstubs.
174 Api_levels_module *string
175
Colin Cross2207f872021-03-24 12:39:08 -0700176 // the dirs which Metalava extracts API levels annotations from.
177 Api_levels_annotations_dirs []string
178
Cole Faust051fa912022-10-05 12:45:42 -0700179 // the sdk kind which Metalava extracts API levels annotations from. Supports 'public', 'system', 'module-lib' and 'system-server'; defaults to public.
satayev783195c2021-06-23 21:49:57 +0100180 Api_levels_sdk_type *string
181
Colin Cross2207f872021-03-24 12:39:08 -0700182 // the filename which Metalava extracts API levels annotations from. Defaults to android.jar.
183 Api_levels_jar_filename *string
184
185 // if set to true, collect the values used by the Dev tools and
186 // write them in files packaged with the SDK. Defaults to false.
187 Write_sdk_values *bool
MÃ¥rten Kongstad802ae0f2022-07-27 13:47:32 +0200188
189 // path or filegroup to file defining extension an SDK name <-> numerical ID mapping and
190 // what APIs exist in which SDKs; passed to metalava via --sdk-extensions-info
191 Extensions_info_file *string `android:"path"`
Jihoon Kang3198f3c2023-01-26 08:08:52 +0000192
193 // API surface of this module. If set, the module contributes to an API surface.
194 // For the full list of available API surfaces, refer to soong/android/sdk_version.go
195 Api_surface *string
Jihoon Kang6592e872023-12-19 01:13:16 +0000196
197 // a list of aconfig_declarations module names that the stubs generated in this module
198 // depend on.
199 Aconfig_declarations []string
Paul Duffin27819362024-07-22 21:03:50 +0100200
201 // List of hard coded filegroups containing Metalava config files that are passed to every
202 // Metalava invocation that this module performs. See addMetalavaConfigFilesToCmd.
203 ConfigFiles []string `android:"path" blueprint:"mutated"`
Colin Cross2207f872021-03-24 12:39:08 -0700204}
205
Anton Hansson52609322021-05-05 10:36:05 +0100206// Used by xsd_config
207type ApiFilePath interface {
Jihoon Kangee113282024-01-23 00:16:41 +0000208 ApiFilePath(StubsType) (android.Path, error)
Anton Hansson52609322021-05-05 10:36:05 +0100209}
210
211type ApiStubsSrcProvider interface {
Jihoon Kangee113282024-01-23 00:16:41 +0000212 StubsSrcJar(StubsType) (android.Path, error)
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000213}
214
Anton Hansson52609322021-05-05 10:36:05 +0100215// Provider of information about API stubs, used by java_sdk_library.
216type ApiStubsProvider interface {
Jihoon Kangee113282024-01-23 00:16:41 +0000217 AnnotationsZip(StubsType) (android.Path, error)
Anton Hansson52609322021-05-05 10:36:05 +0100218 ApiFilePath
Jihoon Kangee113282024-01-23 00:16:41 +0000219 RemovedApiFilePath(StubsType) (android.Path, error)
Anton Hansson52609322021-05-05 10:36:05 +0100220
221 ApiStubsSrcProvider
222}
223
Jihoon Kang063ec002023-06-28 01:16:23 +0000224type currentApiTimestampProvider interface {
225 CurrentApiTimestamp() android.Path
226}
227
Jihoon Kang3c89f042023-12-19 02:40:22 +0000228type annotationFlagsParams struct {
229 migratingNullability bool
230 validatingNullability bool
231 nullabilityWarningsFile android.WritablePath
232 annotationsZip android.WritablePath
233}
234type stubsCommandParams struct {
235 srcJarDir android.ModuleOutPath
236 stubsDir android.OptionalPath
237 stubsSrcJar android.WritablePath
238 metadataZip android.WritablePath
239 metadataDir android.WritablePath
240 apiVersionsXml android.WritablePath
241 nullabilityWarningsFile android.WritablePath
242 annotationsZip android.WritablePath
243 stubConfig stubsCommandConfigParams
244}
245type stubsCommandConfigParams struct {
Jihoon Kanga11d6792024-03-05 16:12:20 +0000246 stubsType StubsType
247 javaVersion javaVersion
248 deps deps
249 checkApi bool
250 generateStubs bool
251 doApiLint bool
252 doCheckReleased bool
253 writeSdkValues bool
254 migratingNullability bool
255 validatingNullability bool
Jihoon Kang3c89f042023-12-19 02:40:22 +0000256}
257
Colin Cross2207f872021-03-24 12:39:08 -0700258// droidstubs passes sources files through Metalava to generate stub .java files that only contain the API to be
259// documented, filtering out hidden classes and methods. The resulting .java files are intended to be passed to
260// a droiddoc module to generate documentation.
261func DroidstubsFactory() android.Module {
262 module := &Droidstubs{}
263
264 module.AddProperties(&module.properties,
265 &module.Javadoc.properties)
Paul Duffin27819362024-07-22 21:03:50 +0100266 module.properties.ConfigFiles = getMetalavaConfigFilegroupReference()
Spandan Das2cc80ba2023-10-27 17:21:52 +0000267 module.initModuleAndImport(module)
Colin Cross2207f872021-03-24 12:39:08 -0700268
269 InitDroiddocModule(module, android.HostAndDeviceSupported)
Jihoon Kang3198f3c2023-01-26 08:08:52 +0000270
271 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
272 module.createApiContribution(ctx)
273 })
Colin Cross2207f872021-03-24 12:39:08 -0700274 return module
275}
276
277// droidstubs_host passes sources files through Metalava to generate stub .java files that only contain the API
278// to be documented, filtering out hidden classes and methods. The resulting .java files are intended to be
279// passed to a droiddoc_host module to generate documentation. Use a droidstubs_host instead of a droidstubs
280// module when symbols needed by the source files are provided by java_library_host modules.
281func DroidstubsHostFactory() android.Module {
282 module := &Droidstubs{}
283
284 module.AddProperties(&module.properties,
285 &module.Javadoc.properties)
286
Paul Duffin27819362024-07-22 21:03:50 +0100287 module.properties.ConfigFiles = getMetalavaConfigFilegroupReference()
Colin Cross2207f872021-03-24 12:39:08 -0700288 InitDroiddocModule(module, android.HostSupported)
289 return module
290}
291
Jihoon Kang246690a2024-02-01 21:55:01 +0000292func (d *Droidstubs) AnnotationsZip(stubsType StubsType) (ret android.Path, err error) {
Jihoon Kang78f89142023-12-27 01:40:29 +0000293 switch stubsType {
294 case Everything:
Jihoon Kang246690a2024-02-01 21:55:01 +0000295 ret, err = d.everythingArtifacts.annotationsZip, nil
Jihoon Kang78f89142023-12-27 01:40:29 +0000296 case Exportable:
Jihoon Kang246690a2024-02-01 21:55:01 +0000297 ret, err = d.exportableArtifacts.annotationsZip, nil
Jihoon Kang78f89142023-12-27 01:40:29 +0000298 default:
Jihoon Kang246690a2024-02-01 21:55:01 +0000299 ret, err = nil, fmt.Errorf("annotations zip not supported for the stub type %s", stubsType.String())
Jihoon Kang78f89142023-12-27 01:40:29 +0000300 }
Jihoon Kang246690a2024-02-01 21:55:01 +0000301 return ret, err
Jihoon Kang78f89142023-12-27 01:40:29 +0000302}
303
Jihoon Kang246690a2024-02-01 21:55:01 +0000304func (d *Droidstubs) ApiFilePath(stubsType StubsType) (ret android.Path, err error) {
Jihoon Kang78f89142023-12-27 01:40:29 +0000305 switch stubsType {
306 case Everything:
Jihoon Kang246690a2024-02-01 21:55:01 +0000307 ret, err = d.apiFile, nil
Jihoon Kang78f89142023-12-27 01:40:29 +0000308 case Exportable:
Jihoon Kang246690a2024-02-01 21:55:01 +0000309 ret, err = d.exportableApiFile, nil
Jihoon Kang78f89142023-12-27 01:40:29 +0000310 default:
Jihoon Kang246690a2024-02-01 21:55:01 +0000311 ret, err = nil, fmt.Errorf("api file path not supported for the stub type %s", stubsType.String())
Jihoon Kang78f89142023-12-27 01:40:29 +0000312 }
Jihoon Kang246690a2024-02-01 21:55:01 +0000313 if ret == nil && err == nil {
Jihoon Kang36c3d962024-03-14 17:28:44 +0000314 err = fmt.Errorf("api file is null for the stub type %s", stubsType.String())
Jihoon Kang246690a2024-02-01 21:55:01 +0000315 }
316 return ret, err
Jihoon Kang78f89142023-12-27 01:40:29 +0000317}
318
Jihoon Kang246690a2024-02-01 21:55:01 +0000319func (d *Droidstubs) ApiVersionsXmlFilePath(stubsType StubsType) (ret android.Path, err error) {
Jihoon Kang78f89142023-12-27 01:40:29 +0000320 switch stubsType {
321 case Everything:
Jihoon Kang246690a2024-02-01 21:55:01 +0000322 ret, err = d.everythingArtifacts.apiVersionsXml, nil
Jihoon Kang78f89142023-12-27 01:40:29 +0000323 case Exportable:
Jihoon Kang246690a2024-02-01 21:55:01 +0000324 ret, err = d.exportableArtifacts.apiVersionsXml, nil
Jihoon Kang78f89142023-12-27 01:40:29 +0000325 default:
Jihoon Kang246690a2024-02-01 21:55:01 +0000326 ret, err = nil, fmt.Errorf("api versions xml file path not supported for the stub type %s", stubsType.String())
Jihoon Kang78f89142023-12-27 01:40:29 +0000327 }
Jihoon Kang246690a2024-02-01 21:55:01 +0000328 if ret == nil && err == nil {
329 err = fmt.Errorf("api versions xml file is null for the stub type %s", stubsType.String())
330 }
331 return ret, err
Jihoon Kang78f89142023-12-27 01:40:29 +0000332}
333
Jihoon Kang246690a2024-02-01 21:55:01 +0000334func (d *Droidstubs) DocZip(stubsType StubsType) (ret android.Path, err error) {
Jihoon Kang78f89142023-12-27 01:40:29 +0000335 switch stubsType {
336 case Everything:
Jihoon Kang246690a2024-02-01 21:55:01 +0000337 ret, err = d.docZip, nil
Jihoon Kang78f89142023-12-27 01:40:29 +0000338 default:
Jihoon Kang246690a2024-02-01 21:55:01 +0000339 ret, err = nil, fmt.Errorf("docs zip not supported for the stub type %s", stubsType.String())
Jihoon Kang78f89142023-12-27 01:40:29 +0000340 }
Jihoon Kang246690a2024-02-01 21:55:01 +0000341 if ret == nil && err == nil {
342 err = fmt.Errorf("docs zip is null for the stub type %s", stubsType.String())
343 }
344 return ret, err
345}
346
347func (d *Droidstubs) RemovedApiFilePath(stubsType StubsType) (ret android.Path, err error) {
348 switch stubsType {
349 case Everything:
350 ret, err = d.removedApiFile, nil
351 case Exportable:
352 ret, err = d.exportableRemovedApiFile, nil
353 default:
354 ret, err = nil, fmt.Errorf("removed api file path not supported for the stub type %s", stubsType.String())
355 }
356 if ret == nil && err == nil {
357 err = fmt.Errorf("removed api file is null for the stub type %s", stubsType.String())
358 }
359 return ret, err
360}
361
362func (d *Droidstubs) StubsSrcJar(stubsType StubsType) (ret android.Path, err error) {
363 switch stubsType {
364 case Everything:
365 ret, err = d.stubsSrcJar, nil
366 case Exportable:
367 ret, err = d.exportableStubsSrcJar, nil
368 default:
369 ret, err = nil, fmt.Errorf("stubs srcjar not supported for the stub type %s", stubsType.String())
370 }
371 if ret == nil && err == nil {
372 err = fmt.Errorf("stubs srcjar is null for the stub type %s", stubsType.String())
373 }
374 return ret, err
Jihoon Kang78f89142023-12-27 01:40:29 +0000375}
376
Jihoon Kang063ec002023-06-28 01:16:23 +0000377func (d *Droidstubs) CurrentApiTimestamp() android.Path {
378 return d.checkCurrentApiTimestamp
379}
380
Colin Cross2207f872021-03-24 12:39:08 -0700381var metalavaMergeAnnotationsDirTag = dependencyTag{name: "metalava-merge-annotations-dir"}
382var metalavaMergeInclusionAnnotationsDirTag = dependencyTag{name: "metalava-merge-inclusion-annotations-dir"}
383var metalavaAPILevelsAnnotationsDirTag = dependencyTag{name: "metalava-api-levels-annotations-dir"}
Anton Hanssonc04a16e2022-05-09 09:30:26 +0000384var metalavaAPILevelsModuleTag = dependencyTag{name: "metalava-api-levels-module-tag"}
Jihoon Kang063ec002023-06-28 01:16:23 +0000385var metalavaCurrentApiTimestampTag = dependencyTag{name: "metalava-current-api-timestamp-tag"}
Colin Cross2207f872021-03-24 12:39:08 -0700386
387func (d *Droidstubs) DepsMutator(ctx android.BottomUpMutatorContext) {
388 d.Javadoc.addDeps(ctx)
389
390 if len(d.properties.Merge_annotations_dirs) != 0 {
391 for _, mergeAnnotationsDir := range d.properties.Merge_annotations_dirs {
392 ctx.AddDependency(ctx.Module(), metalavaMergeAnnotationsDirTag, mergeAnnotationsDir)
393 }
394 }
395
396 if len(d.properties.Merge_inclusion_annotations_dirs) != 0 {
397 for _, mergeInclusionAnnotationsDir := range d.properties.Merge_inclusion_annotations_dirs {
398 ctx.AddDependency(ctx.Module(), metalavaMergeInclusionAnnotationsDirTag, mergeInclusionAnnotationsDir)
399 }
400 }
401
402 if len(d.properties.Api_levels_annotations_dirs) != 0 {
403 for _, apiLevelsAnnotationsDir := range d.properties.Api_levels_annotations_dirs {
404 ctx.AddDependency(ctx.Module(), metalavaAPILevelsAnnotationsDirTag, apiLevelsAnnotationsDir)
405 }
406 }
Anton Hanssonc04a16e2022-05-09 09:30:26 +0000407
Jihoon Kang6592e872023-12-19 01:13:16 +0000408 if len(d.properties.Aconfig_declarations) != 0 {
409 for _, aconfigDeclarationModuleName := range d.properties.Aconfig_declarations {
410 ctx.AddDependency(ctx.Module(), aconfigDeclarationTag, aconfigDeclarationModuleName)
411 }
412 }
413
Anton Hanssonc04a16e2022-05-09 09:30:26 +0000414 if d.properties.Api_levels_module != nil {
415 ctx.AddDependency(ctx.Module(), metalavaAPILevelsModuleTag, proptools.String(d.properties.Api_levels_module))
416 }
Colin Cross2207f872021-03-24 12:39:08 -0700417}
418
Jihoon Kang3c89f042023-12-19 02:40:22 +0000419func (d *Droidstubs) sdkValuesFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, metadataDir android.WritablePath) {
420 cmd.FlagWithArg("--sdk-values ", metadataDir.String())
421}
422
423func (d *Droidstubs) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.OptionalPath, stubsType StubsType, checkApi bool) {
Jihoon Kang3c89f042023-12-19 02:40:22 +0000424
Jihoon Kang36c3d962024-03-14 17:28:44 +0000425 apiFileName := proptools.StringDefault(d.properties.Api_filename, ctx.ModuleName()+"_api.txt")
426 uncheckedApiFile := android.PathForModuleOut(ctx, stubsType.String(), apiFileName)
427 cmd.FlagWithOutput("--api ", uncheckedApiFile)
428 if checkApi || String(d.properties.Api_filename) != "" {
Jihoon Kang3c89f042023-12-19 02:40:22 +0000429 if stubsType == Everything {
430 d.apiFile = uncheckedApiFile
431 } else if stubsType == Exportable {
432 d.exportableApiFile = uncheckedApiFile
433 }
Colin Cross2207f872021-03-24 12:39:08 -0700434 } else if sourceApiFile := proptools.String(d.properties.Check_api.Current.Api_file); sourceApiFile != "" {
Jihoon Kang36c3d962024-03-14 17:28:44 +0000435 if stubsType == Everything {
436 // If check api is disabled then make the source file available for export.
437 d.apiFile = android.PathForModuleSrc(ctx, sourceApiFile)
438 } else if stubsType == Exportable {
439 d.exportableApiFile = uncheckedApiFile
440 }
Colin Cross2207f872021-03-24 12:39:08 -0700441 }
442
Jihoon Kang36c3d962024-03-14 17:28:44 +0000443 removedApiFileName := proptools.StringDefault(d.properties.Removed_api_filename, ctx.ModuleName()+"_removed.txt")
444 uncheckedRemovedFile := android.PathForModuleOut(ctx, stubsType.String(), removedApiFileName)
445 cmd.FlagWithOutput("--removed-api ", uncheckedRemovedFile)
Jihoon Kang3c89f042023-12-19 02:40:22 +0000446 if checkApi || String(d.properties.Removed_api_filename) != "" {
Jihoon Kang3c89f042023-12-19 02:40:22 +0000447 if stubsType == Everything {
448 d.removedApiFile = uncheckedRemovedFile
449 } else if stubsType == Exportable {
450 d.exportableRemovedApiFile = uncheckedRemovedFile
451 }
Colin Cross2207f872021-03-24 12:39:08 -0700452 } else if sourceRemovedApiFile := proptools.String(d.properties.Check_api.Current.Removed_api_file); sourceRemovedApiFile != "" {
Jihoon Kang36c3d962024-03-14 17:28:44 +0000453 if stubsType == Everything {
454 // If check api is disabled then make the source removed api file available for export.
455 d.removedApiFile = android.PathForModuleSrc(ctx, sourceRemovedApiFile)
456 } else if stubsType == Exportable {
457 d.exportableRemovedApiFile = uncheckedRemovedFile
458 }
Colin Cross2207f872021-03-24 12:39:08 -0700459 }
460
Colin Cross2207f872021-03-24 12:39:08 -0700461 if stubsDir.Valid() {
462 if Bool(d.properties.Create_doc_stubs) {
463 cmd.FlagWithArg("--doc-stubs ", stubsDir.String())
464 } else {
465 cmd.FlagWithArg("--stubs ", stubsDir.String())
466 if !Bool(d.properties.Output_javadoc_comments) {
467 cmd.Flag("--exclude-documentation-from-stubs")
468 }
469 }
470 }
471}
472
Jihoon Kang3c89f042023-12-19 02:40:22 +0000473func (d *Droidstubs) annotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, params annotationFlagsParams) {
Jihoon Kanga11d6792024-03-05 16:12:20 +0000474 if Bool(d.properties.Annotations_enabled) {
475 cmd.Flag(config.MetalavaAnnotationsFlags)
Andrei Onea4985e512021-04-29 16:29:34 +0100476
Jihoon Kanga11d6792024-03-05 16:12:20 +0000477 if params.migratingNullability {
Jihoon Kang5623e542024-01-31 23:27:26 +0000478 previousApiFiles := android.PathsForModuleSrc(ctx, []string{String(d.properties.Previous_api)})
479 cmd.FlagForEachInput("--migrate-nullness ", previousApiFiles)
Jihoon Kanga11d6792024-03-05 16:12:20 +0000480 }
Jihoon Kang6b93b382024-01-26 22:37:41 +0000481
Jihoon Kanga11d6792024-03-05 16:12:20 +0000482 if s := String(d.properties.Validate_nullability_from_list); s != "" {
483 cmd.FlagWithInput("--validate-nullability-from-list ", android.PathForModuleSrc(ctx, s))
484 }
Jihoon Kang6b93b382024-01-26 22:37:41 +0000485
Jihoon Kanga11d6792024-03-05 16:12:20 +0000486 if params.validatingNullability {
487 cmd.FlagWithOutput("--nullability-warnings-txt ", params.nullabilityWarningsFile)
488 }
Jihoon Kang6b93b382024-01-26 22:37:41 +0000489
Jihoon Kangca2f9e82024-01-26 01:45:12 +0000490 cmd.FlagWithOutput("--extract-annotations ", params.annotationsZip)
Jihoon Kang6b93b382024-01-26 22:37:41 +0000491
Jihoon Kanga11d6792024-03-05 16:12:20 +0000492 if len(d.properties.Merge_annotations_dirs) != 0 {
493 d.mergeAnnoDirFlags(ctx, cmd)
494 }
Jihoon Kang6b93b382024-01-26 22:37:41 +0000495
Jihoon Kanga11d6792024-03-05 16:12:20 +0000496 cmd.Flag(config.MetalavaAnnotationsWarningsFlags)
Colin Cross2207f872021-03-24 12:39:08 -0700497 }
Colin Cross2207f872021-03-24 12:39:08 -0700498}
499
500func (d *Droidstubs) mergeAnnoDirFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
501 ctx.VisitDirectDepsWithTag(metalavaMergeAnnotationsDirTag, func(m android.Module) {
502 if t, ok := m.(*ExportedDroiddocDir); ok {
503 cmd.FlagWithArg("--merge-qualifier-annotations ", t.dir.String()).Implicits(t.deps)
504 } else {
505 ctx.PropertyErrorf("merge_annotations_dirs",
506 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
507 }
508 })
509}
510
511func (d *Droidstubs) inclusionAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
512 ctx.VisitDirectDepsWithTag(metalavaMergeInclusionAnnotationsDirTag, func(m android.Module) {
513 if t, ok := m.(*ExportedDroiddocDir); ok {
514 cmd.FlagWithArg("--merge-inclusion-annotations ", t.dir.String()).Implicits(t.deps)
515 } else {
516 ctx.PropertyErrorf("merge_inclusion_annotations_dirs",
517 "module %q is not a metalava merge-annotations dir", ctx.OtherModuleName(m))
518 }
519 })
520}
521
Jihoon Kanga11d6792024-03-05 16:12:20 +0000522func (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsType StubsType, apiVersionsXml android.WritablePath) {
Anton Hanssonc04a16e2022-05-09 09:30:26 +0000523 var apiVersions android.Path
Jihoon Kanga11d6792024-03-05 16:12:20 +0000524 if proptools.Bool(d.properties.Api_levels_annotations_enabled) {
Jihoon Kang3c89f042023-12-19 02:40:22 +0000525 d.apiLevelsGenerationFlags(ctx, cmd, stubsType, apiVersionsXml)
Jihoon Kangd9a06942024-01-26 01:49:20 +0000526 apiVersions = apiVersionsXml
Anton Hanssonc04a16e2022-05-09 09:30:26 +0000527 } else {
528 ctx.VisitDirectDepsWithTag(metalavaAPILevelsModuleTag, func(m android.Module) {
529 if s, ok := m.(*Droidstubs); ok {
Jihoon Kangd9a06942024-01-26 01:49:20 +0000530 if stubsType == Everything {
531 apiVersions = s.everythingArtifacts.apiVersionsXml
532 } else if stubsType == Exportable {
533 apiVersions = s.exportableArtifacts.apiVersionsXml
534 } else {
Jihoon Kangd40c5912024-03-05 16:12:20 +0000535 ctx.ModuleErrorf("%s stubs type does not generate api-versions.xml file", stubsType.String())
Jihoon Kangd9a06942024-01-26 01:49:20 +0000536 }
Anton Hanssonc04a16e2022-05-09 09:30:26 +0000537 } else {
538 ctx.PropertyErrorf("api_levels_module",
539 "module %q is not a droidstubs module", ctx.OtherModuleName(m))
540 }
541 })
Colin Cross2207f872021-03-24 12:39:08 -0700542 }
Anton Hanssonc04a16e2022-05-09 09:30:26 +0000543 if apiVersions != nil {
544 cmd.FlagWithArg("--current-version ", ctx.Config().PlatformSdkVersion().String())
545 cmd.FlagWithArg("--current-codename ", ctx.Config().PlatformSdkCodename())
546 cmd.FlagWithInput("--apply-api-levels ", apiVersions)
547 }
548}
Colin Cross2207f872021-03-24 12:39:08 -0700549
Paul Duffin5a195f42024-05-01 12:52:35 +0100550// AndroidPlusUpdatableJar is the name of some extra jars added into `module-lib` and
551// `system-server` directories that contain all the APIs provided by the platform and updatable
552// modules because the `android.jar` files do not. See b/337836752.
553const AndroidPlusUpdatableJar = "android-plus-updatable.jar"
554
Jihoon Kang3c89f042023-12-19 02:40:22 +0000555func (d *Droidstubs) apiLevelsGenerationFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsType StubsType, apiVersionsXml android.WritablePath) {
Colin Cross2207f872021-03-24 12:39:08 -0700556 if len(d.properties.Api_levels_annotations_dirs) == 0 {
557 ctx.PropertyErrorf("api_levels_annotations_dirs",
558 "has to be non-empty if api levels annotations was enabled!")
559 }
560
Jihoon Kang3c89f042023-12-19 02:40:22 +0000561 cmd.FlagWithOutput("--generate-api-levels ", apiVersionsXml)
Colin Cross2207f872021-03-24 12:39:08 -0700562
563 filename := proptools.StringDefault(d.properties.Api_levels_jar_filename, "android.jar")
564
Paul Duffin58cfc9a2024-04-25 17:01:49 +0100565 // TODO: Avoid the duplication of API surfaces, reuse apiScope.
566 // Add all relevant --android-jar-pattern patterns for Metalava.
567 // When parsing a stub jar for a specific version, Metalava picks the first pattern that defines
568 // an actual file present on disk (in the order the patterns were passed). For system APIs for
569 // privileged apps that are only defined since API level 21 (Lollipop), fallback to public stubs
570 // for older releases. Similarly, module-lib falls back to system API.
571 var sdkDirs []string
Paul Duffin92efc612024-05-02 17:18:05 +0100572 apiLevelsSdkType := proptools.StringDefault(d.properties.Api_levels_sdk_type, "public")
573 switch apiLevelsSdkType {
Paul Duffin58cfc9a2024-04-25 17:01:49 +0100574 case "system-server":
575 sdkDirs = []string{"system-server", "module-lib", "system", "public"}
576 case "module-lib":
577 sdkDirs = []string{"module-lib", "system", "public"}
578 case "system":
579 sdkDirs = []string{"system", "public"}
580 case "public":
581 sdkDirs = []string{"public"}
582 default:
583 ctx.PropertyErrorf("api_levels_sdk_type", "needs to be one of %v", allowedApiLevelSdkTypes)
584 return
585 }
586
Paul Duffin92efc612024-05-02 17:18:05 +0100587 // Construct a pattern to match the appropriate extensions that should be included in the
588 // generated api-versions.xml file.
589 //
Paul Duffin58cfc9a2024-04-25 17:01:49 +0100590 // Use the first item in the sdkDirs array as that is the sdk type for the target API levels
591 // being generated but has the advantage over `Api_levels_sdk_type` as it has been validated.
Paul Duffin92efc612024-05-02 17:18:05 +0100592 // The exception is for system-server which needs to include module-lib and system-server. That
593 // is because while system-server extends module-lib the system-server extension directory only
594 // contains service-* modules which provide system-server APIs it does not list the modules which
595 // only provide a module-lib, so they have to be included separately.
596 extensionSurfacesPattern := sdkDirs[0]
597 if apiLevelsSdkType == "system-server" {
598 // Take the first two items in sdkDirs, which are system-server and module-lib, and construct
599 // a pattern that will match either.
600 extensionSurfacesPattern = strings.Join(sdkDirs[0:2], "|")
601 }
602 extensionsPattern := fmt.Sprintf(`/extensions/[0-9]+/(%s)/.*\.jar`, extensionSurfacesPattern)
Paul Duffin58cfc9a2024-04-25 17:01:49 +0100603
satayev783195c2021-06-23 21:49:57 +0100604 var dirs []string
MÃ¥rten Kongstad802ae0f2022-07-27 13:47:32 +0200605 var extensions_dir string
Colin Cross2207f872021-03-24 12:39:08 -0700606 ctx.VisitDirectDepsWithTag(metalavaAPILevelsAnnotationsDirTag, func(m android.Module) {
607 if t, ok := m.(*ExportedDroiddocDir); ok {
Paul Duffin58cfc9a2024-04-25 17:01:49 +0100608 extRegex := regexp.MustCompile(t.dir.String() + extensionsPattern)
MÃ¥rten Kongstad802ae0f2022-07-27 13:47:32 +0200609
610 // Grab the first extensions_dir and we find while scanning ExportedDroiddocDir.deps;
611 // ideally this should be read from prebuiltApis.properties.Extensions_*
Colin Cross2207f872021-03-24 12:39:08 -0700612 for _, dep := range t.deps {
Paul Duffin2ced2eb2024-05-01 13:13:51 +0100613 // Check to see if it matches an extension first.
614 depBase := dep.Base()
MÃ¥rten Kongstad802ae0f2022-07-27 13:47:32 +0200615 if extRegex.MatchString(dep.String()) && d.properties.Extensions_info_file != nil {
616 if extensions_dir == "" {
617 extensions_dir = t.dir.String() + "/extensions"
618 }
619 cmd.Implicit(dep)
Paul Duffin2ced2eb2024-05-01 13:13:51 +0100620 } else if depBase == filename {
621 // Check to see if it matches a dessert release for an SDK, e.g. Android, Car, Wear, etc..
Colin Cross5f6ffc72021-03-29 21:54:45 -0700622 cmd.Implicit(dep)
Paul Duffin5a195f42024-05-01 12:52:35 +0100623 } else if depBase == AndroidPlusUpdatableJar && d.properties.Extensions_info_file != nil {
624 // The output api-versions.xml has been requested to include information on SDK
625 // extensions. That means it also needs to include
626 // so
627 // The module-lib and system-server directories should use `android-plus-updatable.jar`
628 // instead of `android.jar`. See AndroidPlusUpdatableJar for more information.
629 cmd.Implicit(dep)
Paul Duffin2ced2eb2024-05-01 13:13:51 +0100630 } else if filename != "android.jar" && depBase == "android.jar" {
Colin Cross5f6ffc72021-03-29 21:54:45 -0700631 // Metalava implicitly searches these patterns:
632 // prebuilts/tools/common/api-versions/android-%/android.jar
633 // prebuilts/sdk/%/public/android.jar
634 // Add android.jar files from the api_levels_annotations_dirs directories to try
635 // to satisfy these patterns. If Metalava can't find a match for an API level
636 // between 1 and 28 in at least one pattern it will fail.
Colin Cross2207f872021-03-24 12:39:08 -0700637 cmd.Implicit(dep)
638 }
639 }
satayev783195c2021-06-23 21:49:57 +0100640
641 dirs = append(dirs, t.dir.String())
Colin Cross2207f872021-03-24 12:39:08 -0700642 } else {
643 ctx.PropertyErrorf("api_levels_annotations_dirs",
644 "module %q is not a metalava api-levels-annotations dir", ctx.OtherModuleName(m))
645 }
646 })
satayev783195c2021-06-23 21:49:57 +0100647
Paul Duffin5a195f42024-05-01 12:52:35 +0100648 // Generate the list of --android-jar-pattern options. The order matters so the first one which
649 // matches will be the one that is used for a specific api level..
Pedro Loureirocc203502021-10-04 17:24:00 +0000650 for _, sdkDir := range sdkDirs {
651 for _, dir := range dirs {
Paul Duffin5a195f42024-05-01 12:52:35 +0100652 addPattern := func(jarFilename string) {
653 cmd.FlagWithArg("--android-jar-pattern ", fmt.Sprintf("%s/%%/%s/%s", dir, sdkDir, jarFilename))
654 }
655
656 if sdkDir == "module-lib" || sdkDir == "system-server" {
657 // The module-lib and system-server android.jars do not include the updatable modules (as
658 // doing so in the source would introduce dependency cycles and the prebuilts have to
659 // match the sources). So, instead an additional `android-plus-updatable.jar` will be used
660 // that does include the updatable modules and this pattern will match that. This pattern
661 // is added in addition to the following pattern to decouple this change from the change
662 // to add the `android-plus-updatable.jar`.
663 addPattern(AndroidPlusUpdatableJar)
664 }
665
666 addPattern(filename)
Pedro Loureirocc203502021-10-04 17:24:00 +0000667 }
satayev783195c2021-06-23 21:49:57 +0100668 }
MÃ¥rten Kongstad802ae0f2022-07-27 13:47:32 +0200669
670 if d.properties.Extensions_info_file != nil {
671 if extensions_dir == "" {
672 ctx.ModuleErrorf("extensions_info_file set, but no SDK extension dirs found")
673 }
674 info_file := android.PathForModuleSrc(ctx, *d.properties.Extensions_info_file)
675 cmd.Implicit(info_file)
676 cmd.FlagWithArg("--sdk-extensions-root ", extensions_dir)
677 cmd.FlagWithArg("--sdk-extensions-info ", info_file.String())
678 }
Colin Cross2207f872021-03-24 12:39:08 -0700679}
680
Jihoon Kang472f73f2024-03-28 20:59:29 +0000681func (d *Droidstubs) apiCompatibilityFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsType StubsType) {
682 if len(d.Javadoc.properties.Out) > 0 {
683 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
684 }
685
Jihoon Kang5623e542024-01-31 23:27:26 +0000686 apiFiles := android.PathsForModuleSrc(ctx, []string{String(d.properties.Check_api.Last_released.Api_file)})
687 removedApiFiles := android.PathsForModuleSrc(ctx, []string{String(d.properties.Check_api.Last_released.Removed_api_file)})
Jihoon Kang472f73f2024-03-28 20:59:29 +0000688
Jihoon Kang5623e542024-01-31 23:27:26 +0000689 cmd.FlagForEachInput("--check-compatibility:api:released ", apiFiles)
690 cmd.FlagForEachInput("--check-compatibility:removed:released ", removedApiFiles)
Jihoon Kang472f73f2024-03-28 20:59:29 +0000691
692 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Last_released.Baseline_file)
693 if baselineFile.Valid() {
694 cmd.FlagWithInput("--baseline:compatibility:released ", baselineFile.Path())
695 }
696}
697
Colin Crosse52c2ac2022-03-28 17:03:35 -0700698func metalavaUseRbe(ctx android.ModuleContext) bool {
699 return ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_METALAVA")
700}
701
Jihoon Kang421c1cd2024-04-22 21:17:12 +0000702func metalavaCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs android.Paths,
Paul Duffin27819362024-07-22 21:03:50 +0100703 srcJarList android.Path, homeDir android.WritablePath, params stubsCommandConfigParams, configFiles android.Paths) *android.RuleBuilderCommand {
Colin Cross2207f872021-03-24 12:39:08 -0700704 rule.Command().Text("rm -rf").Flag(homeDir.String())
705 rule.Command().Text("mkdir -p").Flag(homeDir.String())
706
Anton Hansson556e8142021-06-04 16:20:25 +0100707 cmd := rule.Command()
Colin Cross2207f872021-03-24 12:39:08 -0700708 cmd.FlagWithArg("ANDROID_PREFS_ROOT=", homeDir.String())
709
Colin Crosse52c2ac2022-03-28 17:03:35 -0700710 if metalavaUseRbe(ctx) {
Colin Cross2207f872021-03-24 12:39:08 -0700711 rule.Remoteable(android.RemoteRuleSupports{RBE: true})
Colin Cross8095c292021-03-30 16:40:48 -0700712 execStrategy := ctx.Config().GetenvWithDefault("RBE_METALAVA_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
Anas Sulaiman9d7a36d2023-11-21 23:00:07 +0000713 compare := ctx.Config().IsEnvTrue("RBE_METALAVA_COMPARE")
714 remoteUpdateCache := !ctx.Config().IsEnvFalse("RBE_METALAVA_REMOTE_UPDATE_CACHE")
Colin Cross8095c292021-03-30 16:40:48 -0700715 labels := map[string]string{"type": "tool", "name": "metalava"}
716 // TODO: metalava pool rejects these jobs
717 pool := ctx.Config().GetenvWithDefault("RBE_METALAVA_POOL", "java16")
718 rule.Rewrapper(&remoteexec.REParams{
Anas Sulaiman9d7a36d2023-11-21 23:00:07 +0000719 Labels: labels,
720 ExecStrategy: execStrategy,
721 ToolchainInputs: []string{config.JavaCmd(ctx).String()},
722 Platform: map[string]string{remoteexec.PoolKey: pool},
723 Compare: compare,
724 NumLocalRuns: 1,
725 NumRemoteRuns: 1,
726 NoRemoteUpdateCache: !remoteUpdateCache,
Colin Cross8095c292021-03-30 16:40:48 -0700727 })
Colin Cross2207f872021-03-24 12:39:08 -0700728 }
729
Colin Cross6aa5c402021-03-24 12:28:50 -0700730 cmd.BuiltTool("metalava").ImplicitTool(ctx.Config().HostJavaToolPath(ctx, "metalava.jar")).
Colin Cross2207f872021-03-24 12:39:08 -0700731 Flag(config.JavacVmFlags).
Liz Kammere09e20e2023-10-16 15:07:54 -0400732 Flag(config.MetalavaAddOpens).
LeddaZcb3030f2021-10-29 16:54:04 +0200733 Flag("-J-Xmx6114m").
Jihoon Kang421c1cd2024-04-22 21:17:12 +0000734 FlagWithArg("--java-source ", params.javaVersion.String()).
735 FlagWithRspFileInputList("@", android.PathForModuleOut(ctx, fmt.Sprintf("%s.metalava.rsp", params.stubsType.String())), srcs).
Colin Cross2207f872021-03-24 12:39:08 -0700736 FlagWithInput("@", srcJarList)
737
Paul Duffinf8aaaa12023-08-10 15:16:35 +0100738 // Metalava does not differentiate between bootclasspath and classpath and has not done so for
739 // years, so it is unlikely to change any time soon.
Jihoon Kang421c1cd2024-04-22 21:17:12 +0000740 combinedPaths := append(([]android.Path)(nil), params.deps.bootClasspath.Paths()...)
741 combinedPaths = append(combinedPaths, params.deps.classpath.Paths()...)
Paul Duffinf8aaaa12023-08-10 15:16:35 +0100742 if len(combinedPaths) > 0 {
743 cmd.FlagWithInputList("--classpath ", combinedPaths, ":")
Colin Cross2207f872021-03-24 12:39:08 -0700744 }
745
Liz Kammere09e20e2023-10-16 15:07:54 -0400746 cmd.Flag(config.MetalavaFlags)
Jihoon Kangc8313892023-09-20 00:54:47 +0000747
Paul Duffin27819362024-07-22 21:03:50 +0100748 addMetalavaConfigFilesToCmd(cmd, configFiles)
749
Colin Cross2207f872021-03-24 12:39:08 -0700750 return cmd
751}
752
Paul Duffin27819362024-07-22 21:03:50 +0100753// MetalavaConfigFilegroup is the name of the filegroup in build/soong/java/metalava that lists
754// the configuration files to pass to Metalava.
755const MetalavaConfigFilegroup = "metalava-config-files"
756
757// Get a reference to the MetalavaConfigFilegroup suitable for use in a property.
758func getMetalavaConfigFilegroupReference() []string {
759 return []string{":" + MetalavaConfigFilegroup}
760}
761
762// addMetalavaConfigFilesToCmd adds --config-file options to use the config files list in the
763// MetalavaConfigFilegroup filegroup.
764func addMetalavaConfigFilesToCmd(cmd *android.RuleBuilderCommand, configFiles android.Paths) {
765 cmd.FlagForEachInput("--config-file ", configFiles)
766}
767
Jihoon Kang3c89f042023-12-19 02:40:22 +0000768// Pass flagged apis related flags to metalava. When aconfig_declarations property is not
769// defined for a module, simply revert all flagged apis annotations. If aconfig_declarations
770// property is defined, apply transformations and only revert the flagged apis that are not
771// enabled via release configurations and are not specified in aconfig_declarations
Jihoon Kang5d701272024-02-15 21:53:49 +0000772func generateRevertAnnotationArgs(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsType StubsType, aconfigFlagsPaths android.Paths) {
Jihoon Kang6592e872023-12-19 01:13:16 +0000773 var filterArgs string
774 switch stubsType {
775 // No flagged apis specific flags need to be passed to metalava when generating
776 // everything stubs
777 case Everything:
778 return
779
780 case Runtime:
781 filterArgs = "--filter='state:ENABLED+permission:READ_ONLY' --filter='permission:READ_WRITE'"
782
783 case Exportable:
Jihoon Kang59198152024-02-06 22:43:18 +0000784 // When the build flag RELEASE_EXPORT_RUNTIME_APIS is set to true, apis marked with
785 // the flagged apis that have read_write permissions are exposed on top of the enabled
786 // and read_only apis. This is to support local override of flag values at runtime.
787 if ctx.Config().ReleaseExportRuntimeApis() {
788 filterArgs = "--filter='state:ENABLED+permission:READ_ONLY' --filter='permission:READ_WRITE'"
789 } else {
790 filterArgs = "--filter='state:ENABLED+permission:READ_ONLY'"
791 }
Jihoon Kang6592e872023-12-19 01:13:16 +0000792 }
793
Jihoon Kangf1e0ff02024-11-20 21:10:40 +0000794 if len(aconfigFlagsPaths) == 0 {
795 // This argument should not be added for "everything" stubs
796 cmd.Flag("--revert-annotation android.annotation.FlaggedApi")
797 return
798 }
799
800 releasedFlaggedApisFile := android.PathForModuleOut(ctx, fmt.Sprintf("released-flagged-apis-%s.txt", stubsType.String()))
801 revertAnnotationsFile := android.PathForModuleOut(ctx, fmt.Sprintf("revert-annotations-%s.txt", stubsType.String()))
802
Jihoon Kang6592e872023-12-19 01:13:16 +0000803 ctx.Build(pctx, android.BuildParams{
804 Rule: gatherReleasedFlaggedApisRule,
805 Inputs: aconfigFlagsPaths,
806 Output: releasedFlaggedApisFile,
807 Description: fmt.Sprintf("%s gather aconfig flags", stubsType),
808 Args: map[string]string{
809 "flags_path": android.JoinPathsWithPrefix(aconfigFlagsPaths, "--cache "),
810 "filter_args": filterArgs,
811 },
812 })
813
814 ctx.Build(pctx, android.BuildParams{
815 Rule: generateMetalavaRevertAnnotationsRule,
816 Input: releasedFlaggedApisFile,
817 Output: revertAnnotationsFile,
818 Description: fmt.Sprintf("%s revert annotations", stubsType),
819 })
Jihoon Kang3c89f042023-12-19 02:40:22 +0000820
821 cmd.FlagWithInput("@", revertAnnotationsFile)
Jihoon Kang6592e872023-12-19 01:13:16 +0000822}
823
Jihoon Kang3c89f042023-12-19 02:40:22 +0000824func (d *Droidstubs) commonMetalavaStubCmd(ctx android.ModuleContext, rule *android.RuleBuilder,
825 params stubsCommandParams) *android.RuleBuilderCommand {
Colin Cross2207f872021-03-24 12:39:08 -0700826 if BoolDefault(d.properties.High_mem, false) {
827 // This metalava run uses lots of memory, restrict the number of metalava jobs that can run in parallel.
828 rule.HighMem()
829 }
830
Jihoon Kang3c89f042023-12-19 02:40:22 +0000831 if params.stubConfig.generateStubs {
832 rule.Command().Text("rm -rf").Text(params.stubsDir.String())
833 rule.Command().Text("mkdir -p").Text(params.stubsDir.String())
Colin Cross2207f872021-03-24 12:39:08 -0700834 }
835
Jihoon Kang3c89f042023-12-19 02:40:22 +0000836 srcJarList := zipSyncCmd(ctx, rule, params.srcJarDir, d.Javadoc.srcJars)
Colin Cross2207f872021-03-24 12:39:08 -0700837
Jihoon Kang3c89f042023-12-19 02:40:22 +0000838 homeDir := android.PathForModuleOut(ctx, params.stubConfig.stubsType.String(), "home")
Paul Duffin27819362024-07-22 21:03:50 +0100839
840 configFiles := android.PathsForModuleSrc(ctx, d.properties.ConfigFiles)
841
842 cmd := metalavaCmd(ctx, rule, d.Javadoc.srcFiles, srcJarList, homeDir, params.stubConfig, configFiles)
Colin Cross2207f872021-03-24 12:39:08 -0700843 cmd.Implicits(d.Javadoc.implicits)
844
Jihoon Kang3c89f042023-12-19 02:40:22 +0000845 d.stubsFlags(ctx, cmd, params.stubsDir, params.stubConfig.stubsType, params.stubConfig.checkApi)
Colin Cross2207f872021-03-24 12:39:08 -0700846
Jihoon Kang3c89f042023-12-19 02:40:22 +0000847 if params.stubConfig.writeSdkValues {
848 d.sdkValuesFlags(ctx, cmd, params.metadataDir)
849 }
850
851 annotationParams := annotationFlagsParams{
852 migratingNullability: params.stubConfig.migratingNullability,
853 validatingNullability: params.stubConfig.validatingNullability,
854 nullabilityWarningsFile: params.nullabilityWarningsFile,
855 annotationsZip: params.annotationsZip,
856 }
857
Jihoon Kanga11d6792024-03-05 16:12:20 +0000858 d.annotationsFlags(ctx, cmd, annotationParams)
Colin Cross2207f872021-03-24 12:39:08 -0700859 d.inclusionAnnotationsFlags(ctx, cmd)
Jihoon Kanga11d6792024-03-05 16:12:20 +0000860 d.apiLevelsAnnotationsFlags(ctx, cmd, params.stubConfig.stubsType, params.apiVersionsXml)
Colin Cross2207f872021-03-24 12:39:08 -0700861
Jihoon Kang472f73f2024-03-28 20:59:29 +0000862 if params.stubConfig.doCheckReleased {
863 d.apiCompatibilityFlags(ctx, cmd, params.stubConfig.stubsType)
864 }
865
Colin Crossbc139922021-03-25 18:33:16 -0700866 d.expandArgs(ctx, cmd)
Colin Cross2207f872021-03-24 12:39:08 -0700867
Colin Cross2207f872021-03-24 12:39:08 -0700868 for _, o := range d.Javadoc.properties.Out {
869 cmd.ImplicitOutput(android.PathForModuleGen(ctx, o))
870 }
871
Jihoon Kang3c89f042023-12-19 02:40:22 +0000872 return cmd
873}
Colin Cross2207f872021-03-24 12:39:08 -0700874
Jihoon Kang3c89f042023-12-19 02:40:22 +0000875// Sandbox rule for generating the everything stubs and other artifacts
876func (d *Droidstubs) everythingStubCmd(ctx android.ModuleContext, params stubsCommandConfigParams) {
877 srcJarDir := android.PathForModuleOut(ctx, Everything.String(), "srcjars")
878 rule := android.NewRuleBuilder(pctx, ctx)
879 rule.Sbox(android.PathForModuleOut(ctx, Everything.String()),
880 android.PathForModuleOut(ctx, "metalava.sbox.textproto")).
881 SandboxInputs()
882
883 var stubsDir android.OptionalPath
884 if params.generateStubs {
885 stubsDir = android.OptionalPathForPath(android.PathForModuleOut(ctx, Everything.String(), "stubsDir"))
886 d.Javadoc.stubsSrcJar = android.PathForModuleOut(ctx, Everything.String(), ctx.ModuleName()+"-"+"stubs.srcjar")
887 }
888
889 if params.writeSdkValues {
Jihoon Kangee113282024-01-23 00:16:41 +0000890 d.everythingArtifacts.metadataDir = android.PathForModuleOut(ctx, Everything.String(), "metadata")
891 d.everythingArtifacts.metadataZip = android.PathForModuleOut(ctx, Everything.String(), ctx.ModuleName()+"-metadata.zip")
Jihoon Kang3c89f042023-12-19 02:40:22 +0000892 }
893
Jihoon Kanga11d6792024-03-05 16:12:20 +0000894 if Bool(d.properties.Annotations_enabled) {
Jihoon Kang3c89f042023-12-19 02:40:22 +0000895 if params.validatingNullability {
Jihoon Kangee113282024-01-23 00:16:41 +0000896 d.everythingArtifacts.nullabilityWarningsFile = android.PathForModuleOut(ctx, Everything.String(), ctx.ModuleName()+"_nullability_warnings.txt")
Jihoon Kang3c89f042023-12-19 02:40:22 +0000897 }
Jihoon Kangee113282024-01-23 00:16:41 +0000898 d.everythingArtifacts.annotationsZip = android.PathForModuleOut(ctx, Everything.String(), ctx.ModuleName()+"_annotations.zip")
Jihoon Kang3c89f042023-12-19 02:40:22 +0000899 }
Jihoon Kanga11d6792024-03-05 16:12:20 +0000900 if Bool(d.properties.Api_levels_annotations_enabled) {
Jihoon Kangee113282024-01-23 00:16:41 +0000901 d.everythingArtifacts.apiVersionsXml = android.PathForModuleOut(ctx, Everything.String(), "api-versions.xml")
Jihoon Kang3c89f042023-12-19 02:40:22 +0000902 }
903
904 commonCmdParams := stubsCommandParams{
905 srcJarDir: srcJarDir,
906 stubsDir: stubsDir,
907 stubsSrcJar: d.Javadoc.stubsSrcJar,
Jihoon Kangee113282024-01-23 00:16:41 +0000908 metadataDir: d.everythingArtifacts.metadataDir,
909 apiVersionsXml: d.everythingArtifacts.apiVersionsXml,
910 nullabilityWarningsFile: d.everythingArtifacts.nullabilityWarningsFile,
911 annotationsZip: d.everythingArtifacts.annotationsZip,
Jihoon Kang3c89f042023-12-19 02:40:22 +0000912 stubConfig: params,
913 }
914
915 cmd := d.commonMetalavaStubCmd(ctx, rule, commonCmdParams)
916
917 d.everythingOptionalCmd(ctx, cmd, params.doApiLint, params.doCheckReleased)
918
919 if params.generateStubs {
920 rule.Command().
921 BuiltTool("soong_zip").
922 Flag("-write_if_changed").
923 Flag("-jar").
924 FlagWithOutput("-o ", d.Javadoc.stubsSrcJar).
925 FlagWithArg("-C ", stubsDir.String()).
926 FlagWithArg("-D ", stubsDir.String())
927 }
928
929 if params.writeSdkValues {
930 rule.Command().
931 BuiltTool("soong_zip").
932 Flag("-write_if_changed").
933 Flag("-d").
Jihoon Kangee113282024-01-23 00:16:41 +0000934 FlagWithOutput("-o ", d.everythingArtifacts.metadataZip).
935 FlagWithArg("-C ", d.everythingArtifacts.metadataDir.String()).
936 FlagWithArg("-D ", d.everythingArtifacts.metadataDir.String())
Jihoon Kang3c89f042023-12-19 02:40:22 +0000937 }
938
939 // TODO: We don't really need two separate API files, but this is a reminiscence of how
940 // we used to run metalava separately for API lint and the "last_released" check. Unify them.
941 if params.doApiLint {
942 rule.Command().Text("touch").Output(d.apiLintTimestamp)
943 }
944 if params.doCheckReleased {
945 rule.Command().Text("touch").Output(d.checkLastReleasedApiTimestamp)
946 }
947
948 // TODO(b/183630617): rewrapper doesn't support restat rules
949 if !metalavaUseRbe(ctx) {
950 rule.Restat()
951 }
952
953 zipSyncCleanupCmd(rule, srcJarDir)
954
955 rule.Build("metalava", "metalava merged")
956}
957
958// Sandbox rule for generating the everything artifacts that are not run by
959// default but only run based on the module configurations
960func (d *Droidstubs) everythingOptionalCmd(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, doApiLint bool, doCheckReleased bool) {
Colin Cross2207f872021-03-24 12:39:08 -0700961
962 // Add API lint options.
Paul Duffinbaf34782024-05-28 17:27:22 +0100963 treatDocumentationIssuesAsErrors := false
Jihoon Kang3c89f042023-12-19 02:40:22 +0000964 if doApiLint {
Jihoon Kang5623e542024-01-31 23:27:26 +0000965 var newSince android.Paths
966 if d.properties.Check_api.Api_lint.New_since != nil {
967 newSince = android.PathsForModuleSrc(ctx, []string{proptools.String(d.properties.Check_api.Api_lint.New_since)})
968 }
Paul Duffin0a71d732024-04-22 13:22:56 +0100969 cmd.Flag("--api-lint")
970 cmd.FlagForEachInput("--api-lint-previous-api ", newSince)
Jihoon Kang3c89f042023-12-19 02:40:22 +0000971 d.apiLintReport = android.PathForModuleOut(ctx, Everything.String(), "api_lint_report.txt")
Colin Cross2207f872021-03-24 12:39:08 -0700972 cmd.FlagWithOutput("--report-even-if-suppressed ", d.apiLintReport) // TODO: Change to ":api-lint"
973
Paul Duffinc540bee2024-08-29 15:35:58 +0100974 // If UnflaggedApi issues have not already been configured then make sure that existing
975 // UnflaggedApi issues are reported as warnings but issues in new/changed code are treated as
976 // errors by the Build Warnings Aye Aye Analyzer in Gerrit.
Paul Duffin88d3b392024-08-28 17:37:36 +0100977 // Once existing issues have been fixed this will be changed to error.
Paul Duffinc540bee2024-08-29 15:35:58 +0100978 // TODO(b/362771529): Switch to --error
979 if !strings.Contains(cmd.String(), " UnflaggedApi ") {
980 cmd.Flag("--error-when-new UnflaggedApi")
981 }
Paul Duffin88d3b392024-08-28 17:37:36 +0100982
Colin Cross0d532412021-03-25 09:38:45 -0700983 // TODO(b/154317059): Clean up this allowlist by baselining and/or checking in last-released.
Colin Cross2207f872021-03-24 12:39:08 -0700984 if d.Name() != "android.car-system-stubs-docs" &&
985 d.Name() != "android.car-stubs-docs" {
Paul Duffinbaf34782024-05-28 17:27:22 +0100986 treatDocumentationIssuesAsErrors = true
Colin Cross2207f872021-03-24 12:39:08 -0700987 cmd.Flag("--warnings-as-errors") // Most lints are actually warnings.
988 }
989
990 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.Baseline_file)
Jihoon Kang3c89f042023-12-19 02:40:22 +0000991 updatedBaselineOutput := android.PathForModuleOut(ctx, Everything.String(), "api_lint_baseline.txt")
992 d.apiLintTimestamp = android.PathForModuleOut(ctx, Everything.String(), "api_lint.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -0700993
994 // Note this string includes a special shell quote $' ... ', which decodes the "\n"s.
Colin Cross2207f872021-03-24 12:39:08 -0700995 //
996 // TODO: metalava also has a slightly different message hardcoded. Should we unify this
997 // message and metalava's one?
998 msg := `$'` + // Enclose with $' ... '
999 `************************************************************\n` +
1000 `Your API changes are triggering API Lint warnings or errors.\n` +
Colin Cross2207f872021-03-24 12:39:08 -07001001 `\n` +
Adrian Roos40be6472024-11-05 14:25:35 +00001002 `To make the failures go away:\n` +
Colin Cross2207f872021-03-24 12:39:08 -07001003 `\n` +
Adrian Roos40be6472024-11-05 14:25:35 +00001004 `1. REQUIRED: Read the messages carefully and address them by` +
1005 ` fixing the API if appropriate.\n` +
1006 `2. If the failure is a false positive, you can suppress it with:\n` +
1007 ` @SuppressLint("<id>")\n` +
Aurimas Liutikasb23b7452021-05-24 18:00:37 +00001008 ` where the <id> is given in brackets in the error message above.\n`
Colin Cross2207f872021-03-24 12:39:08 -07001009
1010 if baselineFile.Valid() {
1011 cmd.FlagWithInput("--baseline:api-lint ", baselineFile.Path())
1012 cmd.FlagWithOutput("--update-baseline:api-lint ", updatedBaselineOutput)
1013
1014 msg += fmt.Sprintf(``+
Cole Faust5146e782024-11-15 14:47:49 -08001015 `3. FOR LSC ONLY: You can update the baseline by executing\n`+
Adrian Roos40be6472024-11-05 14:25:35 +00001016 ` the following command:\n`+
Colin Cross63eeda02021-04-15 19:01:57 -07001017 ` (cd $ANDROID_BUILD_TOP && cp \\\n`+
1018 ` "%s" \\\n`+
1019 ` "%s")\n`+
Colin Cross2207f872021-03-24 12:39:08 -07001020 ` To submit the revised baseline.txt to the main Android\n`+
1021 ` repository, you will need approval.\n`, updatedBaselineOutput, baselineFile.Path())
1022 } else {
1023 msg += fmt.Sprintf(``+
Adrian Roos40be6472024-11-05 14:25:35 +00001024 `3. FOR LSC ONLY: You can add a baseline file of existing lint failures\n`+
Colin Cross2207f872021-03-24 12:39:08 -07001025 ` to the build rule of %s.\n`, d.Name())
1026 }
1027 // Note the message ends with a ' (single quote), to close the $' ... ' .
1028 msg += `************************************************************\n'`
1029
1030 cmd.FlagWithArg("--error-message:api-lint ", msg)
1031 }
1032
Paul Duffinbaf34782024-05-28 17:27:22 +01001033 if !treatDocumentationIssuesAsErrors {
Paul Duffinb679bdd2024-06-10 14:29:41 +01001034 treatDocumentationIssuesAsWarningErrorWhenNew(cmd)
Paul Duffinbaf34782024-05-28 17:27:22 +01001035 }
1036
Colin Cross2207f872021-03-24 12:39:08 -07001037 // Add "check released" options. (Detect incompatible API changes from the last public release)
Jihoon Kang3c89f042023-12-19 02:40:22 +00001038 if doCheckReleased {
Colin Cross2207f872021-03-24 12:39:08 -07001039 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Last_released.Baseline_file)
Jihoon Kang3c89f042023-12-19 02:40:22 +00001040 d.checkLastReleasedApiTimestamp = android.PathForModuleOut(ctx, Everything.String(), "check_last_released_api.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -07001041 if baselineFile.Valid() {
Jihoon Kang472f73f2024-03-28 20:59:29 +00001042 updatedBaselineOutput := android.PathForModuleOut(ctx, Everything.String(), "last_released_baseline.txt")
Colin Cross2207f872021-03-24 12:39:08 -07001043 cmd.FlagWithOutput("--update-baseline:compatibility:released ", updatedBaselineOutput)
1044 }
Colin Cross2207f872021-03-24 12:39:08 -07001045 // Note this string includes quote ($' ... '), which decodes the "\n"s.
1046 msg := `$'\n******************************\n` +
1047 `You have tried to change the API from what has been previously released in\n` +
1048 `an SDK. Please fix the errors listed above.\n` +
1049 `******************************\n'`
1050
1051 cmd.FlagWithArg("--error-message:compatibility:released ", msg)
1052 }
1053
Paul Duffin10a23c22023-08-11 22:47:31 +01001054 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") {
1055 // Pass the current API file into metalava so it can use it as the basis for determining how to
1056 // generate the output signature files (both api and removed).
1057 currentApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
1058 cmd.FlagWithInput("--use-same-format-as ", currentApiFile)
1059 }
Jihoon Kang3c89f042023-12-19 02:40:22 +00001060}
Paul Duffin10a23c22023-08-11 22:47:31 +01001061
Paul Duffinb679bdd2024-06-10 14:29:41 +01001062// HIDDEN_DOCUMENTATION_ISSUES is the set of documentation related issues that should always be
1063// hidden as they are very noisy and provide little value.
1064var HIDDEN_DOCUMENTATION_ISSUES = []string{
1065 "Deprecated",
1066 "IntDef",
1067 "Nullable",
1068}
1069
1070func treatDocumentationIssuesAsWarningErrorWhenNew(cmd *android.RuleBuilderCommand) {
1071 // Treat documentation issues as warnings, but error when new.
1072 cmd.Flag("--error-when-new-category").Flag("Documentation")
1073
1074 // Hide some documentation issues that generated a lot of noise for little benefit.
1075 cmd.FlagForEachArg("--hide ", HIDDEN_DOCUMENTATION_ISSUES)
1076}
1077
Jihoon Kang3c89f042023-12-19 02:40:22 +00001078// Sandbox rule for generating exportable stubs and other artifacts
1079func (d *Droidstubs) exportableStubCmd(ctx android.ModuleContext, params stubsCommandConfigParams) {
1080 optionalCmdParams := stubsCommandParams{
1081 stubConfig: params,
1082 }
1083
Jihoon Kang246690a2024-02-01 21:55:01 +00001084 if params.generateStubs {
1085 d.Javadoc.exportableStubsSrcJar = android.PathForModuleOut(ctx, params.stubsType.String(), ctx.ModuleName()+"-"+"stubs.srcjar")
1086 optionalCmdParams.stubsSrcJar = d.Javadoc.exportableStubsSrcJar
1087 }
1088
Jihoon Kang3c89f042023-12-19 02:40:22 +00001089 if params.writeSdkValues {
Jihoon Kangee113282024-01-23 00:16:41 +00001090 d.exportableArtifacts.metadataZip = android.PathForModuleOut(ctx, params.stubsType.String(), ctx.ModuleName()+"-metadata.zip")
1091 d.exportableArtifacts.metadataDir = android.PathForModuleOut(ctx, params.stubsType.String(), "metadata")
1092 optionalCmdParams.metadataZip = d.exportableArtifacts.metadataZip
1093 optionalCmdParams.metadataDir = d.exportableArtifacts.metadataDir
Jihoon Kang3c89f042023-12-19 02:40:22 +00001094 }
1095
Jihoon Kanga11d6792024-03-05 16:12:20 +00001096 if Bool(d.properties.Annotations_enabled) {
Jihoon Kang3c89f042023-12-19 02:40:22 +00001097 if params.validatingNullability {
Jihoon Kangee113282024-01-23 00:16:41 +00001098 d.exportableArtifacts.nullabilityWarningsFile = android.PathForModuleOut(ctx, params.stubsType.String(), ctx.ModuleName()+"_nullability_warnings.txt")
1099 optionalCmdParams.nullabilityWarningsFile = d.exportableArtifacts.nullabilityWarningsFile
Jihoon Kang3c89f042023-12-19 02:40:22 +00001100 }
Jihoon Kangee113282024-01-23 00:16:41 +00001101 d.exportableArtifacts.annotationsZip = android.PathForModuleOut(ctx, params.stubsType.String(), ctx.ModuleName()+"_annotations.zip")
1102 optionalCmdParams.annotationsZip = d.exportableArtifacts.annotationsZip
Jihoon Kang3c89f042023-12-19 02:40:22 +00001103 }
Jihoon Kanga11d6792024-03-05 16:12:20 +00001104 if Bool(d.properties.Api_levels_annotations_enabled) {
Jihoon Kangee113282024-01-23 00:16:41 +00001105 d.exportableArtifacts.apiVersionsXml = android.PathForModuleOut(ctx, params.stubsType.String(), "api-versions.xml")
1106 optionalCmdParams.apiVersionsXml = d.exportableArtifacts.apiVersionsXml
Jihoon Kang3c89f042023-12-19 02:40:22 +00001107 }
1108
1109 if params.checkApi || String(d.properties.Api_filename) != "" {
1110 filename := proptools.StringDefault(d.properties.Api_filename, ctx.ModuleName()+"_api.txt")
1111 d.exportableApiFile = android.PathForModuleOut(ctx, params.stubsType.String(), filename)
1112 }
1113
1114 if params.checkApi || String(d.properties.Removed_api_filename) != "" {
1115 filename := proptools.StringDefault(d.properties.Removed_api_filename, ctx.ModuleName()+"_api.txt")
1116 d.exportableRemovedApiFile = android.PathForModuleOut(ctx, params.stubsType.String(), filename)
1117 }
1118
1119 d.optionalStubCmd(ctx, optionalCmdParams)
1120}
1121
1122func (d *Droidstubs) optionalStubCmd(ctx android.ModuleContext, params stubsCommandParams) {
1123
1124 params.srcJarDir = android.PathForModuleOut(ctx, params.stubConfig.stubsType.String(), "srcjars")
1125 rule := android.NewRuleBuilder(pctx, ctx)
1126 rule.Sbox(android.PathForModuleOut(ctx, params.stubConfig.stubsType.String()),
1127 android.PathForModuleOut(ctx, fmt.Sprintf("metalava_%s.sbox.textproto", params.stubConfig.stubsType.String()))).
1128 SandboxInputs()
1129
1130 if params.stubConfig.generateStubs {
1131 params.stubsDir = android.OptionalPathForPath(android.PathForModuleOut(ctx, params.stubConfig.stubsType.String(), "stubsDir"))
1132 }
1133
1134 cmd := d.commonMetalavaStubCmd(ctx, rule, params)
1135
Jihoon Kang5d701272024-02-15 21:53:49 +00001136 generateRevertAnnotationArgs(ctx, cmd, params.stubConfig.stubsType, params.stubConfig.deps.aconfigProtoFiles)
Jihoon Kang3c89f042023-12-19 02:40:22 +00001137
1138 if params.stubConfig.doApiLint {
1139 // Pass the lint baseline file as an input to resolve the lint errors.
1140 // The exportable stubs generation does not update the lint baseline file.
1141 // Lint baseline file update is handled by the everything stubs
1142 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Api_lint.Baseline_file)
1143 if baselineFile.Valid() {
1144 cmd.FlagWithInput("--baseline:api-lint ", baselineFile.Path())
1145 }
1146 }
1147
Paul Duffin71527b72024-05-31 13:30:32 +01001148 // Treat documentation issues as warnings, but error when new.
Paul Duffinb679bdd2024-06-10 14:29:41 +01001149 treatDocumentationIssuesAsWarningErrorWhenNew(cmd)
Paul Duffin71527b72024-05-31 13:30:32 +01001150
Jihoon Kang3c89f042023-12-19 02:40:22 +00001151 if params.stubConfig.generateStubs {
Colin Cross2207f872021-03-24 12:39:08 -07001152 rule.Command().
1153 BuiltTool("soong_zip").
1154 Flag("-write_if_changed").
1155 Flag("-jar").
Jihoon Kang3c89f042023-12-19 02:40:22 +00001156 FlagWithOutput("-o ", params.stubsSrcJar).
1157 FlagWithArg("-C ", params.stubsDir.String()).
1158 FlagWithArg("-D ", params.stubsDir.String())
Colin Cross2207f872021-03-24 12:39:08 -07001159 }
1160
Jihoon Kang3c89f042023-12-19 02:40:22 +00001161 if params.stubConfig.writeSdkValues {
Colin Cross2207f872021-03-24 12:39:08 -07001162 rule.Command().
1163 BuiltTool("soong_zip").
1164 Flag("-write_if_changed").
1165 Flag("-d").
Jihoon Kang3c89f042023-12-19 02:40:22 +00001166 FlagWithOutput("-o ", params.metadataZip).
1167 FlagWithArg("-C ", params.metadataDir.String()).
1168 FlagWithArg("-D ", params.metadataDir.String())
Colin Cross2207f872021-03-24 12:39:08 -07001169 }
1170
Colin Cross6aa5c402021-03-24 12:28:50 -07001171 // TODO(b/183630617): rewrapper doesn't support restat rules
Colin Crosse52c2ac2022-03-28 17:03:35 -07001172 if !metalavaUseRbe(ctx) {
1173 rule.Restat()
1174 }
Colin Cross2207f872021-03-24 12:39:08 -07001175
Jihoon Kang3c89f042023-12-19 02:40:22 +00001176 zipSyncCleanupCmd(rule, params.srcJarDir)
Colin Cross2207f872021-03-24 12:39:08 -07001177
Jihoon Kang3c89f042023-12-19 02:40:22 +00001178 rule.Build(fmt.Sprintf("metalava_%s", params.stubConfig.stubsType.String()), "metalava merged")
1179}
1180
1181func (d *Droidstubs) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1182 deps := d.Javadoc.collectDeps(ctx)
1183
1184 javaVersion := getJavaVersion(ctx, String(d.Javadoc.properties.Java_version), android.SdkContext(d))
1185 generateStubs := BoolDefault(d.properties.Generate_stubs, true)
1186
1187 // Add options for the other optional tasks: API-lint and check-released.
1188 // We generate separate timestamp files for them.
1189 doApiLint := BoolDefault(d.properties.Check_api.Api_lint.Enabled, false)
1190 doCheckReleased := apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released")
1191
1192 writeSdkValues := Bool(d.properties.Write_sdk_values)
1193
1194 annotationsEnabled := Bool(d.properties.Annotations_enabled)
1195
1196 migratingNullability := annotationsEnabled && String(d.properties.Previous_api) != ""
1197 validatingNullability := annotationsEnabled && (strings.Contains(String(d.Javadoc.properties.Args), "--validate-nullability-from-merged-stubs") ||
1198 String(d.properties.Validate_nullability_from_list) != "")
1199
1200 checkApi := apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
1201 apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released")
1202
1203 stubCmdParams := stubsCommandConfigParams{
Jihoon Kanga11d6792024-03-05 16:12:20 +00001204 javaVersion: javaVersion,
1205 deps: deps,
1206 checkApi: checkApi,
1207 generateStubs: generateStubs,
1208 doApiLint: doApiLint,
1209 doCheckReleased: doCheckReleased,
1210 writeSdkValues: writeSdkValues,
1211 migratingNullability: migratingNullability,
1212 validatingNullability: validatingNullability,
Jihoon Kang3c89f042023-12-19 02:40:22 +00001213 }
1214 stubCmdParams.stubsType = Everything
1215 // Create default (i.e. "everything" stubs) rule for metalava
1216 d.everythingStubCmd(ctx, stubCmdParams)
1217
Jihoon Kangd40c5912024-03-05 16:12:20 +00001218 // The module generates "exportable" (and "runtime" eventually) stubs regardless of whether
Jihoon Kang3c89f042023-12-19 02:40:22 +00001219 // aconfig_declarations property is defined or not. If the property is not defined, the module simply
1220 // strips all flagged apis to generate the "exportable" stubs
1221 stubCmdParams.stubsType = Exportable
1222 d.exportableStubCmd(ctx, stubCmdParams)
Paul Duffinc166b682022-05-27 12:23:08 +00001223
Paul Duffine7a86642022-08-16 15:43:20 +00001224 if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") {
1225
1226 if len(d.Javadoc.properties.Out) > 0 {
1227 ctx.PropertyErrorf("out", "out property may not be combined with check_api")
1228 }
1229
1230 apiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Api_file))
1231 removedApiFile := android.PathForModuleSrc(ctx, String(d.properties.Check_api.Current.Removed_api_file))
1232 baselineFile := android.OptionalPathForModuleSrc(ctx, d.properties.Check_api.Current.Baseline_file)
1233
1234 if baselineFile.Valid() {
1235 ctx.PropertyErrorf("baseline_file", "current API check can't have a baseline file. (module %s)", ctx.ModuleName())
1236 }
1237
Jihoon Kang3c89f042023-12-19 02:40:22 +00001238 d.checkCurrentApiTimestamp = android.PathForModuleOut(ctx, Everything.String(), "check_current_api.timestamp")
Paul Duffine7a86642022-08-16 15:43:20 +00001239
1240 rule := android.NewRuleBuilder(pctx, ctx)
1241
1242 // Diff command line.
1243 // -F matches the closest "opening" line, such as "package android {"
1244 // and " public class Intent {".
1245 diff := `diff -u -F '{ *$'`
1246
1247 rule.Command().Text("( true")
1248 rule.Command().
1249 Text(diff).
1250 Input(apiFile).Input(d.apiFile)
1251
1252 rule.Command().
1253 Text(diff).
1254 Input(removedApiFile).Input(d.removedApiFile)
1255
1256 msg := fmt.Sprintf(`\n******************************\n`+
1257 `You have tried to change the API from what has been previously approved.\n\n`+
1258 `To make these errors go away, you have two choices:\n`+
1259 ` 1. You can add '@hide' javadoc comments (and remove @SystemApi/@TestApi/etc)\n`+
1260 ` to the new methods, etc. shown in the above diff.\n\n`+
1261 ` 2. You can update current.txt and/or removed.txt by executing the following command:\n`+
1262 ` m %s-update-current-api\n\n`+
1263 ` To submit the revised current.txt to the main Android repository,\n`+
1264 ` you will need approval.\n`+
Jihoon Kang3ea64672023-11-03 00:40:26 +00001265 `If your build failed due to stub validation, you can resolve the errors with\n`+
1266 `either of the two choices above and try re-building the target.\n`+
1267 `If the mismatch between the stubs and the current.txt is intended,\n`+
1268 `you can try re-building the target by executing the following command:\n`+
Jihoon Kang91bf3dd2024-01-24 00:40:23 +00001269 `m DISABLE_STUB_VALIDATION=true <your build target>.\n`+
1270 `Note that DISABLE_STUB_VALIDATION=true does not bypass checkapi.\n`+
Paul Duffine7a86642022-08-16 15:43:20 +00001271 `******************************\n`, ctx.ModuleName())
1272
1273 rule.Command().
1274 Text("touch").Output(d.checkCurrentApiTimestamp).
1275 Text(") || (").
1276 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1277 Text("; exit 38").
1278 Text(")")
1279
1280 rule.Build("metalavaCurrentApiCheck", "check current API")
1281
Jihoon Kang3c89f042023-12-19 02:40:22 +00001282 d.updateCurrentApiTimestamp = android.PathForModuleOut(ctx, Everything.String(), "update_current_api.timestamp")
Paul Duffine7a86642022-08-16 15:43:20 +00001283
1284 // update API rule
1285 rule = android.NewRuleBuilder(pctx, ctx)
1286
1287 rule.Command().Text("( true")
1288
1289 rule.Command().
1290 Text("cp").Flag("-f").
1291 Input(d.apiFile).Flag(apiFile.String())
1292
1293 rule.Command().
1294 Text("cp").Flag("-f").
1295 Input(d.removedApiFile).Flag(removedApiFile.String())
1296
1297 msg = "failed to update public API"
1298
1299 rule.Command().
1300 Text("touch").Output(d.updateCurrentApiTimestamp).
1301 Text(") || (").
1302 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1303 Text("; exit 38").
1304 Text(")")
1305
1306 rule.Build("metalavaCurrentApiUpdate", "update current API")
1307 }
1308
Colin Cross2207f872021-03-24 12:39:08 -07001309 if String(d.properties.Check_nullability_warnings) != "" {
Jihoon Kangee113282024-01-23 00:16:41 +00001310 if d.everythingArtifacts.nullabilityWarningsFile == nil {
Colin Cross2207f872021-03-24 12:39:08 -07001311 ctx.PropertyErrorf("check_nullability_warnings",
1312 "Cannot specify check_nullability_warnings unless validating nullability")
1313 }
1314
1315 checkNullabilityWarnings := android.PathForModuleSrc(ctx, String(d.properties.Check_nullability_warnings))
1316
Jihoon Kang3c89f042023-12-19 02:40:22 +00001317 d.checkNullabilityWarningsTimestamp = android.PathForModuleOut(ctx, Everything.String(), "check_nullability_warnings.timestamp")
Colin Cross2207f872021-03-24 12:39:08 -07001318
1319 msg := fmt.Sprintf(`\n******************************\n`+
1320 `The warnings encountered during nullability annotation validation did\n`+
1321 `not match the checked in file of expected warnings. The diffs are shown\n`+
1322 `above. You have two options:\n`+
1323 ` 1. Resolve the differences by editing the nullability annotations.\n`+
1324 ` 2. Update the file of expected warnings by running:\n`+
1325 ` cp %s %s\n`+
1326 ` and submitting the updated file as part of your change.`,
Jihoon Kangee113282024-01-23 00:16:41 +00001327 d.everythingArtifacts.nullabilityWarningsFile, checkNullabilityWarnings)
Colin Cross2207f872021-03-24 12:39:08 -07001328
1329 rule := android.NewRuleBuilder(pctx, ctx)
1330
1331 rule.Command().
1332 Text("(").
Jihoon Kangee113282024-01-23 00:16:41 +00001333 Text("diff").Input(checkNullabilityWarnings).Input(d.everythingArtifacts.nullabilityWarningsFile).
Colin Cross2207f872021-03-24 12:39:08 -07001334 Text("&&").
1335 Text("touch").Output(d.checkNullabilityWarningsTimestamp).
1336 Text(") || (").
1337 Text("echo").Flag("-e").Flag(`"` + msg + `"`).
1338 Text("; exit 38").
1339 Text(")")
1340
1341 rule.Build("nullabilityWarningsCheck", "nullability warnings check")
1342 }
mrziwang39e68ff2024-07-01 16:35:32 -07001343
1344 d.setOutputFiles(ctx)
1345}
1346
1347// This method sets the outputFiles property, which is used to set the
1348// OutputFilesProvider later.
1349// Droidstubs' tag supports specifying with the stubs type.
1350// While supporting the pre-existing tags, it also supports tags with
1351// the stubs type prefix. Some examples are shown below:
1352// {.annotations.zip} - pre-existing behavior. Returns the path to the
1353// annotation zip.
1354// {.exportable} - Returns the path to the exportable stubs src jar.
1355// {.exportable.annotations.zip} - Returns the path to the exportable
1356// annotations zip file.
1357// {.runtime.api_versions.xml} - Runtime stubs does not generate api versions
1358// xml file. For unsupported combinations, the default everything output file
1359// is returned.
1360func (d *Droidstubs) setOutputFiles(ctx android.ModuleContext) {
1361 tagToOutputFileFunc := map[string]func(StubsType) (android.Path, error){
1362 "": d.StubsSrcJar,
1363 ".docs.zip": d.DocZip,
1364 ".api.txt": d.ApiFilePath,
1365 android.DefaultDistTag: d.ApiFilePath,
1366 ".removed-api.txt": d.RemovedApiFilePath,
1367 ".annotations.zip": d.AnnotationsZip,
1368 ".api_versions.xml": d.ApiVersionsXmlFilePath,
1369 }
1370 stubsTypeToPrefix := map[StubsType]string{
1371 Everything: "",
1372 Exportable: ".exportable",
1373 }
1374 for _, tag := range android.SortedKeys(tagToOutputFileFunc) {
1375 for _, stubType := range android.SortedKeys(stubsTypeToPrefix) {
1376 tagWithPrefix := stubsTypeToPrefix[stubType] + tag
1377 outputFile, err := tagToOutputFileFunc[tag](stubType)
Cole Faust5146e782024-11-15 14:47:49 -08001378 if err == nil && outputFile != nil {
mrziwang39e68ff2024-07-01 16:35:32 -07001379 ctx.SetOutputFiles(android.Paths{outputFile}, tagWithPrefix)
1380 }
1381 }
1382 }
Colin Cross2207f872021-03-24 12:39:08 -07001383}
1384
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001385func (d *Droidstubs) createApiContribution(ctx android.DefaultableHookContext) {
1386 api_file := d.properties.Check_api.Current.Api_file
1387 api_surface := d.properties.Api_surface
1388
1389 props := struct {
1390 Name *string
1391 Api_surface *string
1392 Api_file *string
Jihoon Kang42b589c2023-02-03 22:56:13 +00001393 Visibility []string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001394 }{}
1395
1396 props.Name = proptools.StringPtr(d.Name() + ".api.contribution")
1397 props.Api_surface = api_surface
1398 props.Api_file = api_file
Jihoon Kang42b589c2023-02-03 22:56:13 +00001399 props.Visibility = []string{"//visibility:override", "//visibility:public"}
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001400
1401 ctx.CreateModule(ApiContributionFactory, &props)
1402}
1403
Spandan Das0b555e32022-11-28 18:48:51 +00001404// TODO (b/262014796): Export the API contributions of CorePlatformApi
1405// A map to populate the api surface of a droidstub from a substring appearing in its name
1406// This map assumes that droidstubs (either checked-in or created by java_sdk_library)
1407// use a strict naming convention
1408var (
1409 droidstubsModuleNamingToSdkKind = map[string]android.SdkKind{
Paul Duffin2ced2eb2024-05-01 13:13:51 +01001410 // public is commented out since the core libraries use public in their java_sdk_library names
Spandan Das0b555e32022-11-28 18:48:51 +00001411 "intracore": android.SdkIntraCore,
1412 "intra.core": android.SdkIntraCore,
1413 "system_server": android.SdkSystemServer,
1414 "system-server": android.SdkSystemServer,
1415 "system": android.SdkSystem,
1416 "module_lib": android.SdkModule,
1417 "module-lib": android.SdkModule,
Spandan Dasda977552023-01-26 20:45:16 +00001418 "platform.api": android.SdkCorePlatform,
Spandan Das0b555e32022-11-28 18:48:51 +00001419 "test": android.SdkTest,
Spandan Das4ac2aed2022-12-28 01:54:29 +00001420 "toolchain": android.SdkToolchain,
Spandan Das0b555e32022-11-28 18:48:51 +00001421 }
1422)
1423
Colin Cross2207f872021-03-24 12:39:08 -07001424func StubsDefaultsFactory() android.Module {
1425 module := &DocDefaults{}
1426
1427 module.AddProperties(
1428 &JavadocProperties{},
1429 &DroidstubsProperties{},
1430 )
1431
1432 android.InitDefaultsModule(module)
1433
1434 return module
1435}
1436
1437var _ android.PrebuiltInterface = (*PrebuiltStubsSources)(nil)
1438
1439type PrebuiltStubsSourcesProperties struct {
1440 Srcs []string `android:"path"`
Spandan Das23956d12024-01-19 00:22:22 +00001441
1442 // Name of the source soong module that gets shadowed by this prebuilt
1443 // If unspecified, follows the naming convention that the source module of
1444 // the prebuilt is Name() without "prebuilt_" prefix
1445 Source_module_name *string
1446
1447 // Non-nil if this prebuilt stub srcs module was dynamically created by a java_sdk_library_import
1448 // The name is the undecorated name of the java_sdk_library as it appears in the blueprint file
1449 // (without any prebuilt_ prefix)
1450 Created_by_java_sdk_library_name *string `blueprint:"mutated"`
1451}
1452
1453func (j *PrebuiltStubsSources) BaseModuleName() string {
1454 return proptools.StringDefault(j.properties.Source_module_name, j.ModuleBase.Name())
1455}
1456
1457func (j *PrebuiltStubsSources) CreatedByJavaSdkLibraryName() *string {
1458 return j.properties.Created_by_java_sdk_library_name
Colin Cross2207f872021-03-24 12:39:08 -07001459}
1460
1461type PrebuiltStubsSources struct {
1462 android.ModuleBase
1463 android.DefaultableModuleBase
Spandan Das2cc80ba2023-10-27 17:21:52 +00001464 embeddableInModuleAndImport
1465
Colin Cross2207f872021-03-24 12:39:08 -07001466 prebuilt android.Prebuilt
Colin Cross2207f872021-03-24 12:39:08 -07001467
1468 properties PrebuiltStubsSourcesProperties
1469
kgui67007242022-01-25 13:50:25 +08001470 stubsSrcJar android.Path
Colin Cross2207f872021-03-24 12:39:08 -07001471}
1472
Jihoon Kangee113282024-01-23 00:16:41 +00001473func (d *PrebuiltStubsSources) StubsSrcJar(_ StubsType) (android.Path, error) {
1474 return d.stubsSrcJar, nil
Colin Cross2207f872021-03-24 12:39:08 -07001475}
1476
1477func (p *PrebuiltStubsSources) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross2207f872021-03-24 12:39:08 -07001478 if len(p.properties.Srcs) != 1 {
Anton Hansson86758ac2021-11-03 14:44:12 +00001479 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 -07001480 return
1481 }
1482
Anton Hansson86758ac2021-11-03 14:44:12 +00001483 src := p.properties.Srcs[0]
1484 if filepath.Ext(src) == ".srcjar" {
1485 // This is a srcjar. We can use it directly.
1486 p.stubsSrcJar = android.PathForModuleSrc(ctx, src)
1487 } else {
1488 outPath := android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
Colin Cross2207f872021-03-24 12:39:08 -07001489
Anton Hansson86758ac2021-11-03 14:44:12 +00001490 // This is a directory. Glob the contents just in case the directory does not exist.
1491 srcGlob := src + "/**/*"
1492 srcPaths := android.PathsForModuleSrc(ctx, []string{srcGlob})
Colin Cross2207f872021-03-24 12:39:08 -07001493
Anton Hansson86758ac2021-11-03 14:44:12 +00001494 // Although PathForModuleSrc can return nil if either the path doesn't exist or
1495 // the path components are invalid it won't in this case because no components
1496 // are specified and the module directory must exist in order to get this far.
1497 srcDir := android.PathForModuleSrc(ctx).(android.SourcePath).Join(ctx, src)
Colin Cross2207f872021-03-24 12:39:08 -07001498
Anton Hansson86758ac2021-11-03 14:44:12 +00001499 rule := android.NewRuleBuilder(pctx, ctx)
1500 rule.Command().
1501 BuiltTool("soong_zip").
1502 Flag("-write_if_changed").
1503 Flag("-jar").
1504 FlagWithOutput("-o ", outPath).
1505 FlagWithArg("-C ", srcDir.String()).
1506 FlagWithRspFileInputList("-r ", outPath.ReplaceExtension(ctx, "rsp"), srcPaths)
1507 rule.Restat()
1508 rule.Build("zip src", "Create srcjar from prebuilt source")
1509 p.stubsSrcJar = outPath
1510 }
mrziwangaa2a2b62024-07-01 12:09:20 -07001511
1512 ctx.SetOutputFiles(android.Paths{p.stubsSrcJar}, "")
1513 // prebuilt droidstubs does not output "exportable" stubs.
1514 // Output the "everything" stubs srcjar file if the tag is ".exportable".
1515 ctx.SetOutputFiles(android.Paths{p.stubsSrcJar}, ".exportable")
Colin Cross2207f872021-03-24 12:39:08 -07001516}
1517
1518func (p *PrebuiltStubsSources) Prebuilt() *android.Prebuilt {
1519 return &p.prebuilt
1520}
1521
1522func (p *PrebuiltStubsSources) Name() string {
1523 return p.prebuilt.Name(p.ModuleBase.Name())
1524}
1525
1526// prebuilt_stubs_sources imports a set of java source files as if they were
1527// generated by droidstubs.
1528//
1529// By default, a prebuilt_stubs_sources has a single variant that expects a
1530// set of `.java` files generated by droidstubs.
1531//
1532// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1533// for host modules.
1534//
1535// Intended only for use by sdk snapshots.
1536func PrebuiltStubsSourcesFactory() android.Module {
1537 module := &PrebuiltStubsSources{}
1538
1539 module.AddProperties(&module.properties)
Spandan Das2cc80ba2023-10-27 17:21:52 +00001540 module.initModuleAndImport(module)
Colin Cross2207f872021-03-24 12:39:08 -07001541
1542 android.InitPrebuiltModule(module, &module.properties.Srcs)
Colin Cross2207f872021-03-24 12:39:08 -07001543 InitDroiddocModule(module, android.HostAndDeviceSupported)
1544 return module
1545}