blob: c91ff815395f9a36450387716edc38980528bc21 [file] [log] [blame]
Anton Hansson0860aaf2021-10-08 16:48:03 +01001// Copyright (C) 2021 The Android Open Source Project
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 api
16
17import (
Anton Hansson07a12952022-01-12 17:28:39 +000018 "sort"
19
Anton Hansson0860aaf2021-10-08 16:48:03 +010020 "github.com/google/blueprint/proptools"
21
22 "android/soong/android"
23 "android/soong/genrule"
Anton Hanssoncb00f942022-01-13 09:45:12 +000024 "android/soong/java"
Anton Hansson0860aaf2021-10-08 16:48:03 +010025)
26
Anton Hansson05e944d2022-01-13 12:26:30 +000027const art = "art.module.public.api"
28const conscrypt = "conscrypt.module.public.api"
29const i18n = "i18n.module.public.api"
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +000030const virtualization = "framework-virtualization"
Anton Hansson09a9c4e2022-04-08 10:59:46 +010031
Anton Hansson95e89a82022-01-28 11:31:50 +000032var core_libraries_modules = []string{art, conscrypt, i18n}
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +000033// List of modules that are not yet updatable, and hence they can still compile
34// against hidden APIs. These modules are filtered out when building the
35// updatable-framework-module-impl (because updatable-framework-module-impl is
36// built against module_current SDK). Instead they are directly statically
37// linked into the all-framework-module-lib, which is building against hidden
38// APIs.
Nikita Ioffe5593fbb2022-12-01 14:52:34 +000039// In addition, the modules in this list are allowed to contribute to test APIs
40// stubs.
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +000041var non_updatable_modules = []string{virtualization}
Anton Hansson05e944d2022-01-13 12:26:30 +000042
Anton Hansson0860aaf2021-10-08 16:48:03 +010043// The intention behind this soong plugin is to generate a number of "merged"
44// API-related modules that would otherwise require a large amount of very
45// similar Android.bp boilerplate to define. For example, the merged current.txt
46// API definitions (created by merging the non-updatable current.txt with all
47// the module current.txts). This simplifies the addition of new android
48// modules, by reducing the number of genrules etc a new module must be added to.
49
50// The properties of the combined_apis module type.
51type CombinedApisProperties struct {
Anton Hansson16ff3572022-01-11 18:36:35 +000052 // Module libraries in the bootclasspath
53 Bootclasspath []string
Anton Hansson07a12952022-01-12 17:28:39 +000054 // Module libraries on the bootclasspath if include_nonpublic_framework_api is true.
55 Conditional_bootclasspath []string
Anton Hansson16ff3572022-01-11 18:36:35 +000056 // Module libraries in system server
57 System_server_classpath []string
Anton Hansson0860aaf2021-10-08 16:48:03 +010058}
59
60type CombinedApis struct {
61 android.ModuleBase
62
63 properties CombinedApisProperties
64}
65
66func init() {
67 registerBuildComponents(android.InitRegistrationContext)
68}
69
70func registerBuildComponents(ctx android.RegistrationContext) {
71 ctx.RegisterModuleType("combined_apis", combinedApisModuleFactory)
72}
73
74var PrepareForCombinedApisTest = android.FixtureRegisterWithContext(registerBuildComponents)
75
76func (a *CombinedApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
77}
78
79type genruleProps struct {
80 Name *string
81 Cmd *string
82 Dists []android.Dist
83 Out []string
84 Srcs []string
85 Tools []string
86 Visibility []string
87}
88
Anton Hanssoncb00f942022-01-13 09:45:12 +000089type libraryProps struct {
90 Name *string
91 Sdk_version *string
92 Static_libs []string
93 Visibility []string
94}
95
Anton Hansson4468d7c2022-01-14 12:10:01 +000096type fgProps struct {
97 Name *string
98 Srcs []string
99 Visibility []string
100}
101
Anton Hansson0860aaf2021-10-08 16:48:03 +0100102// Struct to pass parameters for the various merged [current|removed].txt file modules we create.
103type MergedTxtDefinition struct {
104 // "current.txt" or "removed.txt"
105 TxtFilename string
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100106 // Filename in the new dist dir. "android.txt" or "android-removed.txt"
107 DistFilename string
Anton Hansson0860aaf2021-10-08 16:48:03 +0100108 // The module for the non-updatable / non-module part of the api.
109 BaseTxt string
110 // The list of modules that are relevant for this merged txt.
111 Modules []string
112 // The output tag for each module to use.e.g. {.public.api.txt} for current.txt
113 ModuleTag string
114 // public, system, module-lib or system-server
115 Scope string
116}
117
118func createMergedTxt(ctx android.LoadHookContext, txt MergedTxtDefinition) {
119 metalavaCmd := "$(location metalava)"
120 // Silence reflection warnings. See b/168689341
121 metalavaCmd += " -J--add-opens=java.base/java.util=ALL-UNNAMED "
122 metalavaCmd += " --quiet --no-banner --format=v2 "
123
124 filename := txt.TxtFilename
125 if txt.Scope != "public" {
126 filename = txt.Scope + "-" + filename
127 }
Anton Hansson0860aaf2021-10-08 16:48:03 +0100128 props := genruleProps{}
129 props.Name = proptools.StringPtr(ctx.ModuleName() + "-" + filename)
130 props.Tools = []string{"metalava"}
Anton Hansson16ff3572022-01-11 18:36:35 +0000131 props.Out = []string{filename}
Anton Hansson0860aaf2021-10-08 16:48:03 +0100132 props.Cmd = proptools.StringPtr(metalavaCmd + "$(in) --api $(out)")
Anton Hanssonfd316452022-01-14 11:15:52 +0000133 props.Srcs = append([]string{txt.BaseTxt}, createSrcs(txt.Modules, txt.ModuleTag)...)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100134 props.Dists = []android.Dist{
135 {
136 Targets: []string{"droidcore"},
137 Dir: proptools.StringPtr("api"),
138 Dest: proptools.StringPtr(filename),
139 },
140 {
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100141 Targets: []string{"api_txt", "sdk"},
Anton Hansson0860aaf2021-10-08 16:48:03 +0100142 Dir: proptools.StringPtr("apistubs/android/" + txt.Scope + "/api"),
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100143 Dest: proptools.StringPtr(txt.DistFilename),
Anton Hansson0860aaf2021-10-08 16:48:03 +0100144 },
145 }
146 props.Visibility = []string{"//visibility:public"}
147 ctx.CreateModule(genrule.GenRuleFactory, &props)
148}
149
150func createMergedStubsSrcjar(ctx android.LoadHookContext, modules []string) {
151 props := genruleProps{}
152 props.Name = proptools.StringPtr(ctx.ModuleName() + "-current.srcjar")
153 props.Tools = []string{"merge_zips"}
154 props.Out = []string{"current.srcjar"}
155 props.Cmd = proptools.StringPtr("$(location merge_zips) $(out) $(in)")
Anton Hanssonfd316452022-01-14 11:15:52 +0000156 props.Srcs = append([]string{":api-stubs-docs-non-updatable"}, createSrcs(modules, "{.public.stubs.source}")...)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100157 props.Visibility = []string{"//visibility:private"} // Used by make module in //development, mind
158 ctx.CreateModule(genrule.GenRuleFactory, &props)
159}
160
Cole Faustdcda3702022-10-04 14:46:35 -0700161func createMergedAnnotationsFilegroups(ctx android.LoadHookContext, modules, system_server_modules []string) {
162 for _, i := range []struct{
163 name string
164 tag string
165 modules []string
166 }{
167 {
168 name: "all-modules-public-annotations",
169 tag: "{.public.annotations.zip}",
170 modules: modules,
171 }, {
172 name: "all-modules-system-annotations",
173 tag: "{.system.annotations.zip}",
174 modules: modules,
175 }, {
176 name: "all-modules-module-lib-annotations",
177 tag: "{.module-lib.annotations.zip}",
178 modules: modules,
179 }, {
180 name: "all-modules-system-server-annotations",
181 tag: "{.system-server.annotations.zip}",
182 modules: system_server_modules,
183 },
184 } {
185 props := fgProps{}
186 props.Name = proptools.StringPtr(i.name)
187 props.Srcs = createSrcs(i.modules, i.tag)
188 ctx.CreateModule(android.FileGroupFactory, &props)
189 }
Anton Hansson74b15642022-06-23 08:27:23 +0000190}
191
Anton Hansson0860aaf2021-10-08 16:48:03 +0100192func createFilteredApiVersions(ctx android.LoadHookContext, modules []string) {
Anton Hansson05e944d2022-01-13 12:26:30 +0000193 // For the filtered api versions, we prune all APIs except art module's APIs. because
194 // 1) ART apis are available by default to all modules, while other module-to-module deps are
195 // explicit and probably receive more scrutiny anyway
196 // 2) The number of ART/libcore APIs is large, so not linting them would create a large gap
197 // 3) It's a compromise. Ideally we wouldn't be filtering out any module APIs, and have
198 // per-module lint databases that excludes just that module's APIs. Alas, that's more
199 // difficult to achieve.
200 modules = remove(modules, art)
201
Cole Faustdcda3702022-10-04 14:46:35 -0700202 for _, i := range []struct{
203 name string
204 out string
205 in string
206 }{
207 {
208 // We shouldn't need public-filtered or system-filtered.
209 // public-filtered is currently used to lint things that
210 // use the module sdk or the system server sdk, but those
211 // should be switched over to module-filtered and
212 // system-server-filtered, and then public-filtered can
213 // be removed.
214 name: "api-versions-xml-public-filtered",
215 out: "api-versions-public-filtered.xml",
216 in: ":api_versions_public{.api_versions.xml}",
217 }, {
218 name: "api-versions-xml-module-lib-filtered",
219 out: "api-versions-module-lib-filtered.xml",
220 in: ":api_versions_module_lib{.api_versions.xml}",
221 }, {
222 name: "api-versions-xml-system-server-filtered",
223 out: "api-versions-system-server-filtered.xml",
224 in: ":api_versions_system_server{.api_versions.xml}",
225 },
226 } {
227 props := genruleProps{}
228 props.Name = proptools.StringPtr(i.name)
229 props.Out = []string{i.out}
230 // Note: order matters: first parameter is the full api-versions.xml
231 // after that the stubs files in any order
232 // stubs files are all modules that export API surfaces EXCEPT ART
233 props.Srcs = append([]string{i.in}, createSrcs(modules, ".stubs{.jar}")...)
234 props.Tools = []string{"api_versions_trimmer"}
235 props.Cmd = proptools.StringPtr("$(location api_versions_trimmer) $(out) $(in)")
236 props.Dists = []android.Dist{{Targets: []string{"sdk"}}}
237 ctx.CreateModule(genrule.GenRuleFactory, &props)
238 }
Anton Hansson0860aaf2021-10-08 16:48:03 +0100239}
240
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000241func createMergedPublicStubs(ctx android.LoadHookContext, modules []string) {
242 props := libraryProps{}
243 props.Name = proptools.StringPtr("all-modules-public-stubs")
244 props.Static_libs = transformArray(modules, "", ".stubs")
245 props.Sdk_version = proptools.StringPtr("module_current")
246 props.Visibility = []string{"//frameworks/base"}
247 ctx.CreateModule(java.LibraryFactory, &props)
248}
249
250func createMergedSystemStubs(ctx android.LoadHookContext, modules []string) {
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000251 // First create the all-updatable-modules-system-stubs
252 {
253 updatable_modules := removeAll(modules, non_updatable_modules)
254 props := libraryProps{}
255 props.Name = proptools.StringPtr("all-updatable-modules-system-stubs")
256 props.Static_libs = transformArray(updatable_modules, "", ".stubs.system")
257 props.Sdk_version = proptools.StringPtr("module_current")
258 props.Visibility = []string{"//frameworks/base"}
259 ctx.CreateModule(java.LibraryFactory, &props)
260 }
261 // Now merge all-updatable-modules-system-stubs and stubs from non-updatable modules
262 // into all-modules-system-stubs.
263 {
264 props := libraryProps{}
265 props.Name = proptools.StringPtr("all-modules-system-stubs")
266 props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.system")
267 props.Static_libs = append(props.Static_libs, "all-updatable-modules-system-stubs")
268 props.Sdk_version = proptools.StringPtr("module_current")
269 props.Visibility = []string{"//frameworks/base"}
270 ctx.CreateModule(java.LibraryFactory, &props)
271 }
272}
273
274func createMergedTestStubsForNonUpdatableModules(ctx android.LoadHookContext) {
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000275 props := libraryProps{}
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000276 props.Name = proptools.StringPtr("all-non-updatable-modules-test-stubs")
277 props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.test")
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000278 props.Sdk_version = proptools.StringPtr("module_current")
279 props.Visibility = []string{"//frameworks/base"}
280 ctx.CreateModule(java.LibraryFactory, &props)
281}
282
Anton Hansson95e89a82022-01-28 11:31:50 +0000283func createMergedFrameworkImpl(ctx android.LoadHookContext, modules []string) {
284 // This module is for the "framework-all" module, which should not include the core libraries.
285 modules = removeAll(modules, core_libraries_modules)
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +0000286 // Remove the modules that belong to non-updatable APEXes since those are allowed to compile
287 // against unstable APIs.
288 modules = removeAll(modules, non_updatable_modules)
289 // First create updatable-framework-module-impl, which contains all updatable modules.
290 // This module compiles against module_lib SDK.
291 {
292 props := libraryProps{}
293 props.Name = proptools.StringPtr("updatable-framework-module-impl")
294 props.Static_libs = transformArray(modules, "", ".impl")
295 props.Sdk_version = proptools.StringPtr("module_current")
296 props.Visibility = []string{"//frameworks/base"}
297 ctx.CreateModule(java.LibraryFactory, &props)
298 }
299
300 // Now create all-framework-module-impl, which contains updatable-framework-module-impl
301 // and all non-updatable modules. This module compiles against hidden APIs.
302 {
303 props := libraryProps{}
304 props.Name = proptools.StringPtr("all-framework-module-impl")
305 props.Static_libs = transformArray(non_updatable_modules, "", ".impl")
306 props.Static_libs = append(props.Static_libs, "updatable-framework-module-impl")
307 props.Sdk_version = proptools.StringPtr("core_platform")
308 props.Visibility = []string{"//frameworks/base"}
309 ctx.CreateModule(java.LibraryFactory, &props)
310 }
Anton Hansson95e89a82022-01-28 11:31:50 +0000311}
312
313func createMergedFrameworkModuleLibStubs(ctx android.LoadHookContext, modules []string) {
Anton Hanssoncb00f942022-01-13 09:45:12 +0000314 // The user of this module compiles against the "core" SDK, so remove core libraries to avoid dupes.
Anton Hansson95e89a82022-01-28 11:31:50 +0000315 modules = removeAll(modules, core_libraries_modules)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000316 props := libraryProps{}
317 props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api")
Anton Hanssonfd316452022-01-14 11:15:52 +0000318 props.Static_libs = transformArray(modules, "", ".stubs.module_lib")
Anton Hanssoncb00f942022-01-13 09:45:12 +0000319 props.Sdk_version = proptools.StringPtr("module_current")
320 props.Visibility = []string{"//frameworks/base"}
321 ctx.CreateModule(java.LibraryFactory, &props)
322}
323
Anton Hansson4468d7c2022-01-14 12:10:01 +0000324func createPublicStubsSourceFilegroup(ctx android.LoadHookContext, modules []string) {
325 props := fgProps{}
326 props.Name = proptools.StringPtr("all-modules-public-stubs-source")
327 props.Srcs = createSrcs(modules, "{.public.stubs.source}")
328 props.Visibility = []string{"//frameworks/base"}
329 ctx.CreateModule(android.FileGroupFactory, &props)
330}
331
Anton Hansson07a12952022-01-12 17:28:39 +0000332func createMergedTxts(ctx android.LoadHookContext, bootclasspath, system_server_classpath []string) {
Anton Hansson0860aaf2021-10-08 16:48:03 +0100333 var textFiles []MergedTxtDefinition
Anton Hansson16ff3572022-01-11 18:36:35 +0000334
Anton Hansson0860aaf2021-10-08 16:48:03 +0100335 tagSuffix := []string{".api.txt}", ".removed-api.txt}"}
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100336 distFilename := []string{"android.txt", "android-removed.txt"}
Anton Hansson0860aaf2021-10-08 16:48:03 +0100337 for i, f := range []string{"current.txt", "removed.txt"} {
338 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100339 TxtFilename: f,
340 DistFilename: distFilename[i],
341 BaseTxt: ":non-updatable-" + f,
342 Modules: bootclasspath,
343 ModuleTag: "{.public" + tagSuffix[i],
344 Scope: "public",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100345 })
346 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100347 TxtFilename: f,
348 DistFilename: distFilename[i],
349 BaseTxt: ":non-updatable-system-" + f,
350 Modules: bootclasspath,
351 ModuleTag: "{.system" + tagSuffix[i],
352 Scope: "system",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100353 })
354 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100355 TxtFilename: f,
356 DistFilename: distFilename[i],
357 BaseTxt: ":non-updatable-module-lib-" + f,
358 Modules: bootclasspath,
359 ModuleTag: "{.module-lib" + tagSuffix[i],
360 Scope: "module-lib",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100361 })
362 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100363 TxtFilename: f,
364 DistFilename: distFilename[i],
365 BaseTxt: ":non-updatable-system-server-" + f,
366 Modules: system_server_classpath,
367 ModuleTag: "{.system-server" + tagSuffix[i],
368 Scope: "system-server",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100369 })
370 }
371 for _, txt := range textFiles {
372 createMergedTxt(ctx, txt)
373 }
374}
375
376func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) {
Anton Hansson07a12952022-01-12 17:28:39 +0000377 bootclasspath := a.properties.Bootclasspath
Cole Faustdcda3702022-10-04 14:46:35 -0700378 system_server_classpath := a.properties.System_server_classpath
Anton Hansson07a12952022-01-12 17:28:39 +0000379 if ctx.Config().VendorConfig("ANDROID").Bool("include_nonpublic_framework_api") {
380 bootclasspath = append(bootclasspath, a.properties.Conditional_bootclasspath...)
381 sort.Strings(bootclasspath)
382 }
Cole Faustdcda3702022-10-04 14:46:35 -0700383 createMergedTxts(ctx, bootclasspath, system_server_classpath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100384
Anton Hansson07a12952022-01-12 17:28:39 +0000385 createMergedStubsSrcjar(ctx, bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100386
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000387 createMergedPublicStubs(ctx, bootclasspath)
388 createMergedSystemStubs(ctx, bootclasspath)
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000389 createMergedTestStubsForNonUpdatableModules(ctx)
Anton Hansson95e89a82022-01-28 11:31:50 +0000390 createMergedFrameworkModuleLibStubs(ctx, bootclasspath)
391 createMergedFrameworkImpl(ctx, bootclasspath)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000392
Cole Faustdcda3702022-10-04 14:46:35 -0700393 createMergedAnnotationsFilegroups(ctx, bootclasspath, system_server_classpath)
Anton Hanssoncc18e032022-01-12 14:45:22 +0000394
Anton Hansson05e944d2022-01-13 12:26:30 +0000395 createFilteredApiVersions(ctx, bootclasspath)
Anton Hansson4468d7c2022-01-14 12:10:01 +0000396
397 createPublicStubsSourceFilegroup(ctx, bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100398}
399
400func combinedApisModuleFactory() android.Module {
401 module := &CombinedApis{}
402 module.AddProperties(&module.properties)
403 android.InitAndroidModule(module)
404 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) })
405 return module
406}
Anton Hanssonfd316452022-01-14 11:15:52 +0000407
408// Various utility methods below.
409
410// Creates an array of ":<m><tag>" for each m in <modules>.
411func createSrcs(modules []string, tag string) []string {
412 return transformArray(modules, ":", tag)
413}
414
415// Creates an array of "<prefix><m><suffix>", for each m in <modules>.
416func transformArray(modules []string, prefix, suffix string) []string {
417 a := make([]string, 0, len(modules))
418 for _, module := range modules {
419 a = append(a, prefix+module+suffix)
420 }
421 return a
422}
423
424func removeAll(s []string, vs []string) []string {
425 for _, v := range vs {
426 s = remove(s, v)
427 }
428 return s
429}
430
431func remove(s []string, v string) []string {
432 s2 := make([]string, 0, len(s))
433 for _, sv := range s {
434 if sv != v {
435 s2 = append(s2, sv)
436 }
437 }
438 return s2
439}