blob: f0d1f42f61d400010cc5e2e3d0934d31a58cf952 [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 (
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +000018 "fmt"
Anton Hansson07a12952022-01-12 17:28:39 +000019 "sort"
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +000020 "strings"
Anton Hansson07a12952022-01-12 17:28:39 +000021
Anton Hansson0860aaf2021-10-08 16:48:03 +010022 "github.com/google/blueprint/proptools"
23
24 "android/soong/android"
25 "android/soong/genrule"
Anton Hanssoncb00f942022-01-13 09:45:12 +000026 "android/soong/java"
Anton Hansson0860aaf2021-10-08 16:48:03 +010027)
28
Anton Hansson05e944d2022-01-13 12:26:30 +000029const art = "art.module.public.api"
30const conscrypt = "conscrypt.module.public.api"
31const i18n = "i18n.module.public.api"
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +000032const virtualization = "framework-virtualization"
Mark White3cc5e002023-08-07 11:18:09 +000033const location = "framework-location"
Anton Hansson09a9c4e2022-04-08 10:59:46 +010034
Anton Hansson95e89a82022-01-28 11:31:50 +000035var core_libraries_modules = []string{art, conscrypt, i18n}
Zi Wang0d6a5302023-02-16 14:54:01 -080036
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +000037// List of modules that are not yet updatable, and hence they can still compile
38// against hidden APIs. These modules are filtered out when building the
39// updatable-framework-module-impl (because updatable-framework-module-impl is
40// built against module_current SDK). Instead they are directly statically
41// linked into the all-framework-module-lib, which is building against hidden
42// APIs.
Nikita Ioffe5593fbb2022-12-01 14:52:34 +000043// In addition, the modules in this list are allowed to contribute to test APIs
44// stubs.
Roshan Pius96dac952023-12-07 10:54:05 -080045var non_updatable_modules = []string{virtualization, location}
Anton Hansson05e944d2022-01-13 12:26:30 +000046
Anton Hansson0860aaf2021-10-08 16:48:03 +010047// The intention behind this soong plugin is to generate a number of "merged"
48// API-related modules that would otherwise require a large amount of very
49// similar Android.bp boilerplate to define. For example, the merged current.txt
50// API definitions (created by merging the non-updatable current.txt with all
51// the module current.txts). This simplifies the addition of new android
52// modules, by reducing the number of genrules etc a new module must be added to.
53
54// The properties of the combined_apis module type.
55type CombinedApisProperties struct {
Anton Hansson16ff3572022-01-11 18:36:35 +000056 // Module libraries in the bootclasspath
Jihoon Kangcc4c8f92024-07-18 18:52:03 +000057 Bootclasspath proptools.Configurable[[]string]
Anton Hansson07a12952022-01-12 17:28:39 +000058 // Module libraries on the bootclasspath if include_nonpublic_framework_api is true.
59 Conditional_bootclasspath []string
Anton Hansson16ff3572022-01-11 18:36:35 +000060 // Module libraries in system server
Jihoon Kangcc4c8f92024-07-18 18:52:03 +000061 System_server_classpath proptools.Configurable[[]string]
Anton Hansson0860aaf2021-10-08 16:48:03 +010062}
63
64type CombinedApis struct {
65 android.ModuleBase
Harshit Mahajanb52adbc2023-12-15 21:56:42 +000066 android.DefaultableModuleBase
Anton Hansson0860aaf2021-10-08 16:48:03 +010067
68 properties CombinedApisProperties
69}
70
71func init() {
72 registerBuildComponents(android.InitRegistrationContext)
73}
74
75func registerBuildComponents(ctx android.RegistrationContext) {
76 ctx.RegisterModuleType("combined_apis", combinedApisModuleFactory)
Harshit Mahajanb52adbc2023-12-15 21:56:42 +000077 ctx.RegisterModuleType("combined_apis_defaults", CombinedApisModuleDefaultsFactory)
Anton Hansson0860aaf2021-10-08 16:48:03 +010078}
79
80var PrepareForCombinedApisTest = android.FixtureRegisterWithContext(registerBuildComponents)
81
Jihoon Kangcc4c8f92024-07-18 18:52:03 +000082func (a *CombinedApis) bootclasspath(ctx android.ConfigAndErrorContext) []string {
83 return a.properties.Bootclasspath.GetOrDefault(a.ConfigurableEvaluator(ctx), nil)
84}
85
86func (a *CombinedApis) systemServerClasspath(ctx android.ConfigAndErrorContext) []string {
87 return a.properties.System_server_classpath.GetOrDefault(a.ConfigurableEvaluator(ctx), nil)
88}
89
90func (a *CombinedApis) apiFingerprintStubDeps(ctx android.BottomUpMutatorContext) []string {
Spandan Das67b79062024-02-12 11:40:51 +000091 ret := []string{}
92 ret = append(
93 ret,
Jihoon Kangcc4c8f92024-07-18 18:52:03 +000094 transformArray(a.bootclasspath(ctx), "", ".stubs")...,
Spandan Das67b79062024-02-12 11:40:51 +000095 )
96 ret = append(
97 ret,
Jihoon Kangcc4c8f92024-07-18 18:52:03 +000098 transformArray(a.bootclasspath(ctx), "", ".stubs.system")...,
Spandan Das67b79062024-02-12 11:40:51 +000099 )
100 ret = append(
101 ret,
Jihoon Kangcc4c8f92024-07-18 18:52:03 +0000102 transformArray(a.bootclasspath(ctx), "", ".stubs.module_lib")...,
Spandan Das67b79062024-02-12 11:40:51 +0000103 )
104 ret = append(
105 ret,
Jihoon Kangcc4c8f92024-07-18 18:52:03 +0000106 transformArray(a.systemServerClasspath(ctx), "", ".stubs.system_server")...,
Spandan Das67b79062024-02-12 11:40:51 +0000107 )
108 return ret
109}
110
111func (a *CombinedApis) DepsMutator(ctx android.BottomUpMutatorContext) {
Jihoon Kangcc4c8f92024-07-18 18:52:03 +0000112 ctx.AddDependency(ctx.Module(), nil, a.apiFingerprintStubDeps(ctx)...)
Spandan Das67b79062024-02-12 11:40:51 +0000113}
114
Anton Hansson0860aaf2021-10-08 16:48:03 +0100115func (a *CombinedApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Das67b79062024-02-12 11:40:51 +0000116 ctx.WalkDeps(func(child, parent android.Module) bool {
117 if _, ok := child.(java.AndroidLibraryDependency); ok && child.Name() != "framework-res" {
118 // Stubs of BCP and SSCP libraries should not have any dependencies on apps
119 // This check ensures that we do not run into circular dependencies when UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT=true
120 ctx.ModuleErrorf(
121 "Module %s is not a valid dependency of the stub library %s\n."+
122 "If this dependency has been added via `libs` of java_sdk_library, please move it to `impl_only_libs`\n",
123 child.Name(), parent.Name())
124 return false // error detected
125 }
126 return true
127 })
128
Anton Hansson0860aaf2021-10-08 16:48:03 +0100129}
130
131type genruleProps struct {
132 Name *string
133 Cmd *string
134 Dists []android.Dist
135 Out []string
136 Srcs []string
137 Tools []string
138 Visibility []string
139}
140
Anton Hanssoncb00f942022-01-13 09:45:12 +0000141type libraryProps struct {
Jihoon Kanga7073b52024-02-12 23:18:52 +0000142 Name *string
143 Sdk_version *string
144 Static_libs []string
145 Visibility []string
146 Defaults []string
147 Is_stubs_module *bool
Anton Hanssoncb00f942022-01-13 09:45:12 +0000148}
149
Anton Hansson4468d7c2022-01-14 12:10:01 +0000150type fgProps struct {
151 Name *string
152 Srcs []string
153 Visibility []string
154}
155
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000156type defaultsProps struct {
157 Name *string
158 Api_surface *string
159 Api_contributions []string
160 Defaults_visibility []string
Jihoon Kang471a05b2023-08-01 06:37:17 +0000161 Previous_api *string
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000162}
163
Anton Hansson0860aaf2021-10-08 16:48:03 +0100164// Struct to pass parameters for the various merged [current|removed].txt file modules we create.
165type MergedTxtDefinition struct {
166 // "current.txt" or "removed.txt"
167 TxtFilename string
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100168 // Filename in the new dist dir. "android.txt" or "android-removed.txt"
169 DistFilename string
Anton Hansson0860aaf2021-10-08 16:48:03 +0100170 // The module for the non-updatable / non-module part of the api.
171 BaseTxt string
172 // The list of modules that are relevant for this merged txt.
173 Modules []string
174 // The output tag for each module to use.e.g. {.public.api.txt} for current.txt
175 ModuleTag string
176 // public, system, module-lib or system-server
177 Scope string
178}
179
Jihoon Kang31cf2742024-02-07 19:52:19 +0000180func createMergedTxt(ctx android.LoadHookContext, txt MergedTxtDefinition, stubsTypeSuffix string, doDist bool) {
Anton Hansson0860aaf2021-10-08 16:48:03 +0100181 metalavaCmd := "$(location metalava)"
182 // Silence reflection warnings. See b/168689341
183 metalavaCmd += " -J--add-opens=java.base/java.util=ALL-UNNAMED "
Paul Duffinf3b1fc42023-08-14 22:03:06 +0100184 metalavaCmd += " --quiet merge-signatures --format=v2 "
Anton Hansson0860aaf2021-10-08 16:48:03 +0100185
186 filename := txt.TxtFilename
187 if txt.Scope != "public" {
188 filename = txt.Scope + "-" + filename
189 }
Jihoon Kang31cf2742024-02-07 19:52:19 +0000190 moduleName := ctx.ModuleName() + stubsTypeSuffix + filename
Chris Parsons3b7e34b2023-09-27 22:34:57 +0000191
Anton Hansson0860aaf2021-10-08 16:48:03 +0100192 props := genruleProps{}
Chris Parsons3b7e34b2023-09-27 22:34:57 +0000193 props.Name = proptools.StringPtr(moduleName)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100194 props.Tools = []string{"metalava"}
Anton Hansson16ff3572022-01-11 18:36:35 +0000195 props.Out = []string{filename}
Paul Duffinf3b1fc42023-08-14 22:03:06 +0100196 props.Cmd = proptools.StringPtr(metalavaCmd + "$(in) --out $(out)")
Anton Hanssonfd316452022-01-14 11:15:52 +0000197 props.Srcs = append([]string{txt.BaseTxt}, createSrcs(txt.Modules, txt.ModuleTag)...)
Jihoon Kang31cf2742024-02-07 19:52:19 +0000198 if doDist {
199 props.Dists = []android.Dist{
200 {
201 Targets: []string{"droidcore"},
202 Dir: proptools.StringPtr("api"),
203 Dest: proptools.StringPtr(filename),
204 },
205 {
206 Targets: []string{"api_txt", "sdk"},
207 Dir: proptools.StringPtr("apistubs/android/" + txt.Scope + "/api"),
208 Dest: proptools.StringPtr(txt.DistFilename),
209 },
210 }
Anton Hansson0860aaf2021-10-08 16:48:03 +0100211 }
212 props.Visibility = []string{"//visibility:public"}
Colin Crossc6420762023-12-07 12:38:40 -0800213 ctx.CreateModule(genrule.GenRuleFactory, &props)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100214}
215
Cole Faustdcda3702022-10-04 14:46:35 -0700216func createMergedAnnotationsFilegroups(ctx android.LoadHookContext, modules, system_server_modules []string) {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000217 for _, i := range []struct {
Cole Faustdcda3702022-10-04 14:46:35 -0700218 name string
219 tag string
220 modules []string
221 }{
222 {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000223 name: "all-modules-public-annotations",
224 tag: "{.public.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700225 modules: modules,
226 }, {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000227 name: "all-modules-system-annotations",
228 tag: "{.system.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700229 modules: modules,
230 }, {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000231 name: "all-modules-module-lib-annotations",
232 tag: "{.module-lib.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700233 modules: modules,
234 }, {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000235 name: "all-modules-system-server-annotations",
236 tag: "{.system-server.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700237 modules: system_server_modules,
238 },
239 } {
240 props := fgProps{}
241 props.Name = proptools.StringPtr(i.name)
242 props.Srcs = createSrcs(i.modules, i.tag)
Colin Crossc6420762023-12-07 12:38:40 -0800243 ctx.CreateModule(android.FileGroupFactory, &props)
Cole Faustdcda3702022-10-04 14:46:35 -0700244 }
Anton Hansson74b15642022-06-23 08:27:23 +0000245}
246
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000247func createMergedPublicStubs(ctx android.LoadHookContext, modules []string) {
248 props := libraryProps{}
249 props.Name = proptools.StringPtr("all-modules-public-stubs")
250 props.Static_libs = transformArray(modules, "", ".stubs")
251 props.Sdk_version = proptools.StringPtr("module_current")
252 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000253 props.Is_stubs_module = proptools.BoolPtr(true)
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000254 ctx.CreateModule(java.LibraryFactory, &props)
255}
256
Jihoon Kang059b9492023-12-29 00:40:34 +0000257func createMergedPublicExportableStubs(ctx android.LoadHookContext, modules []string) {
258 props := libraryProps{}
259 props.Name = proptools.StringPtr("all-modules-public-stubs-exportable")
260 props.Static_libs = transformArray(modules, "", ".stubs.exportable")
261 props.Sdk_version = proptools.StringPtr("module_current")
262 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000263 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang059b9492023-12-29 00:40:34 +0000264 ctx.CreateModule(java.LibraryFactory, &props)
265}
266
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000267func createMergedSystemStubs(ctx android.LoadHookContext, modules []string) {
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000268 // First create the all-updatable-modules-system-stubs
269 {
270 updatable_modules := removeAll(modules, non_updatable_modules)
271 props := libraryProps{}
272 props.Name = proptools.StringPtr("all-updatable-modules-system-stubs")
273 props.Static_libs = transformArray(updatable_modules, "", ".stubs.system")
274 props.Sdk_version = proptools.StringPtr("module_current")
275 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000276 props.Is_stubs_module = proptools.BoolPtr(true)
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000277 ctx.CreateModule(java.LibraryFactory, &props)
278 }
279 // Now merge all-updatable-modules-system-stubs and stubs from non-updatable modules
280 // into all-modules-system-stubs.
281 {
282 props := libraryProps{}
283 props.Name = proptools.StringPtr("all-modules-system-stubs")
284 props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.system")
285 props.Static_libs = append(props.Static_libs, "all-updatable-modules-system-stubs")
286 props.Sdk_version = proptools.StringPtr("module_current")
287 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000288 props.Is_stubs_module = proptools.BoolPtr(true)
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000289 ctx.CreateModule(java.LibraryFactory, &props)
290 }
291}
292
Jihoon Kang059b9492023-12-29 00:40:34 +0000293func createMergedSystemExportableStubs(ctx android.LoadHookContext, modules []string) {
294 // First create the all-updatable-modules-system-stubs
295 {
296 updatable_modules := removeAll(modules, non_updatable_modules)
297 props := libraryProps{}
298 props.Name = proptools.StringPtr("all-updatable-modules-system-stubs-exportable")
299 props.Static_libs = transformArray(updatable_modules, "", ".stubs.exportable.system")
300 props.Sdk_version = proptools.StringPtr("module_current")
301 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000302 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang059b9492023-12-29 00:40:34 +0000303 ctx.CreateModule(java.LibraryFactory, &props)
304 }
305 // Now merge all-updatable-modules-system-stubs and stubs from non-updatable modules
306 // into all-modules-system-stubs.
307 {
308 props := libraryProps{}
309 props.Name = proptools.StringPtr("all-modules-system-stubs-exportable")
310 props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.exportable.system")
311 props.Static_libs = append(props.Static_libs, "all-updatable-modules-system-stubs-exportable")
312 props.Sdk_version = proptools.StringPtr("module_current")
313 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000314 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang059b9492023-12-29 00:40:34 +0000315 ctx.CreateModule(java.LibraryFactory, &props)
316 }
317}
318
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000319func createMergedTestStubsForNonUpdatableModules(ctx android.LoadHookContext) {
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000320 props := libraryProps{}
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000321 props.Name = proptools.StringPtr("all-non-updatable-modules-test-stubs")
322 props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.test")
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000323 props.Sdk_version = proptools.StringPtr("module_current")
324 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000325 props.Is_stubs_module = proptools.BoolPtr(true)
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000326 ctx.CreateModule(java.LibraryFactory, &props)
327}
328
Jihoon Kang059b9492023-12-29 00:40:34 +0000329func createMergedTestExportableStubsForNonUpdatableModules(ctx android.LoadHookContext) {
330 props := libraryProps{}
331 props.Name = proptools.StringPtr("all-non-updatable-modules-test-stubs-exportable")
332 props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.exportable.test")
333 props.Sdk_version = proptools.StringPtr("module_current")
334 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000335 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang059b9492023-12-29 00:40:34 +0000336 ctx.CreateModule(java.LibraryFactory, &props)
337}
338
Anton Hansson95e89a82022-01-28 11:31:50 +0000339func createMergedFrameworkImpl(ctx android.LoadHookContext, modules []string) {
340 // This module is for the "framework-all" module, which should not include the core libraries.
341 modules = removeAll(modules, core_libraries_modules)
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +0000342 // Remove the modules that belong to non-updatable APEXes since those are allowed to compile
343 // against unstable APIs.
344 modules = removeAll(modules, non_updatable_modules)
345 // First create updatable-framework-module-impl, which contains all updatable modules.
346 // This module compiles against module_lib SDK.
347 {
348 props := libraryProps{}
349 props.Name = proptools.StringPtr("updatable-framework-module-impl")
350 props.Static_libs = transformArray(modules, "", ".impl")
351 props.Sdk_version = proptools.StringPtr("module_current")
352 props.Visibility = []string{"//frameworks/base"}
353 ctx.CreateModule(java.LibraryFactory, &props)
354 }
355
356 // Now create all-framework-module-impl, which contains updatable-framework-module-impl
357 // and all non-updatable modules. This module compiles against hidden APIs.
358 {
359 props := libraryProps{}
360 props.Name = proptools.StringPtr("all-framework-module-impl")
361 props.Static_libs = transformArray(non_updatable_modules, "", ".impl")
362 props.Static_libs = append(props.Static_libs, "updatable-framework-module-impl")
363 props.Sdk_version = proptools.StringPtr("core_platform")
364 props.Visibility = []string{"//frameworks/base"}
365 ctx.CreateModule(java.LibraryFactory, &props)
366 }
Anton Hansson95e89a82022-01-28 11:31:50 +0000367}
368
Jihoon Kang059b9492023-12-29 00:40:34 +0000369func createMergedFrameworkModuleLibExportableStubs(ctx android.LoadHookContext, modules []string) {
370 // The user of this module compiles against the "core" SDK and against non-updatable modules,
371 // so remove to avoid dupes.
372 modules = removeAll(modules, core_libraries_modules)
373 modules = removeAll(modules, non_updatable_modules)
374 props := libraryProps{}
375 props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api-exportable")
376 props.Static_libs = transformArray(modules, "", ".stubs.exportable.module_lib")
377 props.Sdk_version = proptools.StringPtr("module_current")
378 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000379 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang059b9492023-12-29 00:40:34 +0000380 ctx.CreateModule(java.LibraryFactory, &props)
381}
382
Anton Hansson95e89a82022-01-28 11:31:50 +0000383func createMergedFrameworkModuleLibStubs(ctx android.LoadHookContext, modules []string) {
Mark White3cc5e002023-08-07 11:18:09 +0000384 // The user of this module compiles against the "core" SDK and against non-updatable modules,
385 // so remove to avoid dupes.
Anton Hansson95e89a82022-01-28 11:31:50 +0000386 modules = removeAll(modules, core_libraries_modules)
Mark White3cc5e002023-08-07 11:18:09 +0000387 modules = removeAll(modules, non_updatable_modules)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000388 props := libraryProps{}
389 props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api")
Anton Hanssonfd316452022-01-14 11:15:52 +0000390 props.Static_libs = transformArray(modules, "", ".stubs.module_lib")
Anton Hanssoncb00f942022-01-13 09:45:12 +0000391 props.Sdk_version = proptools.StringPtr("module_current")
392 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000393 props.Is_stubs_module = proptools.BoolPtr(true)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000394 ctx.CreateModule(java.LibraryFactory, &props)
395}
396
Paul Duffinfb5e07d2024-05-02 14:51:41 +0100397func createMergedFrameworkSystemServerExportableStubs(ctx android.LoadHookContext, bootclasspath, system_server_classpath []string) {
398 // The user of this module compiles against the "core" SDK and against non-updatable bootclasspathModules,
399 // so remove to avoid dupes.
400 bootclasspathModules := removeAll(bootclasspath, core_libraries_modules)
401 bootclasspathModules = removeAll(bootclasspath, non_updatable_modules)
402 modules := append(
403 // Include all the module-lib APIs from the bootclasspath libraries.
404 transformArray(bootclasspathModules, "", ".stubs.exportable.module_lib"),
405 // Then add all the system-server APIs from the service-* libraries.
406 transformArray(system_server_classpath, "", ".stubs.exportable.system_server")...,
407 )
408 props := libraryProps{}
409 props.Name = proptools.StringPtr("framework-updatable-stubs-system_server_api-exportable")
410 props.Static_libs = modules
411 props.Sdk_version = proptools.StringPtr("system_server_current")
412 props.Visibility = []string{"//frameworks/base"}
413 props.Is_stubs_module = proptools.BoolPtr(true)
414 ctx.CreateModule(java.LibraryFactory, &props)
415}
416
Anton Hansson4468d7c2022-01-14 12:10:01 +0000417func createPublicStubsSourceFilegroup(ctx android.LoadHookContext, modules []string) {
418 props := fgProps{}
419 props.Name = proptools.StringPtr("all-modules-public-stubs-source")
420 props.Srcs = createSrcs(modules, "{.public.stubs.source}")
421 props.Visibility = []string{"//frameworks/base"}
Colin Crossc6420762023-12-07 12:38:40 -0800422 ctx.CreateModule(android.FileGroupFactory, &props)
Anton Hansson4468d7c2022-01-14 12:10:01 +0000423}
424
Jihoon Kang31cf2742024-02-07 19:52:19 +0000425func createMergedTxts(ctx android.LoadHookContext, bootclasspath, system_server_classpath []string, baseTxtModulePrefix, stubsTypeSuffix string, doDist bool) {
Anton Hansson0860aaf2021-10-08 16:48:03 +0100426 var textFiles []MergedTxtDefinition
Anton Hansson16ff3572022-01-11 18:36:35 +0000427
Anton Hansson0860aaf2021-10-08 16:48:03 +0100428 tagSuffix := []string{".api.txt}", ".removed-api.txt}"}
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100429 distFilename := []string{"android.txt", "android-removed.txt"}
Anton Hansson0860aaf2021-10-08 16:48:03 +0100430 for i, f := range []string{"current.txt", "removed.txt"} {
431 textFiles = append(textFiles, MergedTxtDefinition{
Colin Crossc6420762023-12-07 12:38:40 -0800432 TxtFilename: f,
433 DistFilename: distFilename[i],
Jihoon Kang31cf2742024-02-07 19:52:19 +0000434 BaseTxt: ":" + baseTxtModulePrefix + f,
Colin Crossc6420762023-12-07 12:38:40 -0800435 Modules: bootclasspath,
436 ModuleTag: "{.public" + tagSuffix[i],
437 Scope: "public",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100438 })
439 textFiles = append(textFiles, MergedTxtDefinition{
Colin Crossc6420762023-12-07 12:38:40 -0800440 TxtFilename: f,
441 DistFilename: distFilename[i],
Jihoon Kang31cf2742024-02-07 19:52:19 +0000442 BaseTxt: ":" + baseTxtModulePrefix + "system-" + f,
Colin Crossc6420762023-12-07 12:38:40 -0800443 Modules: bootclasspath,
444 ModuleTag: "{.system" + tagSuffix[i],
445 Scope: "system",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100446 })
447 textFiles = append(textFiles, MergedTxtDefinition{
Colin Crossc6420762023-12-07 12:38:40 -0800448 TxtFilename: f,
449 DistFilename: distFilename[i],
Jihoon Kang31cf2742024-02-07 19:52:19 +0000450 BaseTxt: ":" + baseTxtModulePrefix + "module-lib-" + f,
Colin Crossc6420762023-12-07 12:38:40 -0800451 Modules: bootclasspath,
452 ModuleTag: "{.module-lib" + tagSuffix[i],
453 Scope: "module-lib",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100454 })
455 textFiles = append(textFiles, MergedTxtDefinition{
Colin Crossc6420762023-12-07 12:38:40 -0800456 TxtFilename: f,
457 DistFilename: distFilename[i],
Jihoon Kang31cf2742024-02-07 19:52:19 +0000458 BaseTxt: ":" + baseTxtModulePrefix + "system-server-" + f,
Colin Crossc6420762023-12-07 12:38:40 -0800459 Modules: system_server_classpath,
460 ModuleTag: "{.system-server" + tagSuffix[i],
461 Scope: "system-server",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100462 })
463 }
464 for _, txt := range textFiles {
Jihoon Kang31cf2742024-02-07 19:52:19 +0000465 createMergedTxt(ctx, txt, stubsTypeSuffix, doDist)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100466 }
467}
468
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000469func createApiContributionDefaults(ctx android.LoadHookContext, modules []string) {
470 defaultsSdkKinds := []android.SdkKind{
471 android.SdkPublic, android.SdkSystem, android.SdkModule,
472 }
473 for _, sdkKind := range defaultsSdkKinds {
474 props := defaultsProps{}
475 props.Name = proptools.StringPtr(
476 sdkKind.DefaultJavaLibraryName() + "_contributions")
477 if sdkKind == android.SdkModule {
478 props.Name = proptools.StringPtr(
479 sdkKind.DefaultJavaLibraryName() + "_contributions_full")
480 }
481 props.Api_surface = proptools.StringPtr(sdkKind.String())
482 apiSuffix := ""
483 if sdkKind != android.SdkPublic {
484 apiSuffix = "." + strings.ReplaceAll(sdkKind.String(), "-", "_")
485 }
486 props.Api_contributions = transformArray(
487 modules, "", fmt.Sprintf(".stubs.source%s.api.contribution", apiSuffix))
488 props.Defaults_visibility = []string{"//visibility:public"}
Paul Duffina2c4cd72024-06-18 18:49:48 +0100489 props.Previous_api = proptools.StringPtr(":android.api.combined." + sdkKind.String() + ".latest")
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000490 ctx.CreateModule(java.DefaultsFactory, &props)
491 }
492}
493
Jihoon Kang1453baa2023-05-27 05:32:30 +0000494func createFullApiLibraries(ctx android.LoadHookContext) {
495 javaLibraryNames := []string{
496 "android_stubs_current",
497 "android_system_stubs_current",
498 "android_test_stubs_current",
Mark Whitee35b1382023-08-12 01:31:26 +0000499 "android_test_frameworks_core_stubs_current",
Jihoon Kang1453baa2023-05-27 05:32:30 +0000500 "android_module_lib_stubs_current",
501 "android_system_server_stubs_current",
502 }
503
504 for _, libraryName := range javaLibraryNames {
505 props := libraryProps{}
506 props.Name = proptools.StringPtr(libraryName)
507 staticLib := libraryName + ".from-source"
508 if ctx.Config().BuildFromTextStub() {
509 staticLib = libraryName + ".from-text"
510 }
511 props.Static_libs = []string{staticLib}
512 props.Defaults = []string{"android.jar_defaults"}
513 props.Visibility = []string{"//visibility:public"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000514 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang1453baa2023-05-27 05:32:30 +0000515
516 ctx.CreateModule(java.LibraryFactory, &props)
517 }
518}
519
Jihoon Kang059b9492023-12-29 00:40:34 +0000520func createFullExportableApiLibraries(ctx android.LoadHookContext) {
521 javaLibraryNames := []string{
522 "android_stubs_current_exportable",
523 "android_system_stubs_current_exportable",
524 "android_test_stubs_current_exportable",
525 "android_module_lib_stubs_current_exportable",
526 "android_system_server_stubs_current_exportable",
527 }
528
529 for _, libraryName := range javaLibraryNames {
530 props := libraryProps{}
531 props.Name = proptools.StringPtr(libraryName)
532 staticLib := libraryName + ".from-source"
533 props.Static_libs = []string{staticLib}
534 props.Defaults = []string{"android.jar_defaults"}
535 props.Visibility = []string{"//visibility:public"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000536 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang059b9492023-12-29 00:40:34 +0000537
538 ctx.CreateModule(java.LibraryFactory, &props)
539 }
540}
541
Anton Hansson0860aaf2021-10-08 16:48:03 +0100542func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) {
Jihoon Kangcc4c8f92024-07-18 18:52:03 +0000543 bootclasspath := a.bootclasspath(ctx)
544 system_server_classpath := a.systemServerClasspath(ctx)
Anton Hansson07a12952022-01-12 17:28:39 +0000545 if ctx.Config().VendorConfig("ANDROID").Bool("include_nonpublic_framework_api") {
546 bootclasspath = append(bootclasspath, a.properties.Conditional_bootclasspath...)
547 sort.Strings(bootclasspath)
548 }
Jihoon Kang31cf2742024-02-07 19:52:19 +0000549 createMergedTxts(ctx, bootclasspath, system_server_classpath, "non-updatable-", "-", false)
550 createMergedTxts(ctx, bootclasspath, system_server_classpath, "non-updatable-exportable-", "-exportable-", true)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100551
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000552 createMergedPublicStubs(ctx, bootclasspath)
553 createMergedSystemStubs(ctx, bootclasspath)
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000554 createMergedTestStubsForNonUpdatableModules(ctx)
Anton Hansson95e89a82022-01-28 11:31:50 +0000555 createMergedFrameworkModuleLibStubs(ctx, bootclasspath)
556 createMergedFrameworkImpl(ctx, bootclasspath)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000557
Jihoon Kang059b9492023-12-29 00:40:34 +0000558 createMergedPublicExportableStubs(ctx, bootclasspath)
559 createMergedSystemExportableStubs(ctx, bootclasspath)
560 createMergedTestExportableStubsForNonUpdatableModules(ctx)
561 createMergedFrameworkModuleLibExportableStubs(ctx, bootclasspath)
Paul Duffinfb5e07d2024-05-02 14:51:41 +0100562 createMergedFrameworkSystemServerExportableStubs(ctx, bootclasspath, system_server_classpath)
Jihoon Kang059b9492023-12-29 00:40:34 +0000563
Cole Faustdcda3702022-10-04 14:46:35 -0700564 createMergedAnnotationsFilegroups(ctx, bootclasspath, system_server_classpath)
Anton Hanssoncc18e032022-01-12 14:45:22 +0000565
Anton Hansson4468d7c2022-01-14 12:10:01 +0000566 createPublicStubsSourceFilegroup(ctx, bootclasspath)
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000567
568 createApiContributionDefaults(ctx, bootclasspath)
Jihoon Kang1453baa2023-05-27 05:32:30 +0000569
570 createFullApiLibraries(ctx)
Jihoon Kang059b9492023-12-29 00:40:34 +0000571
572 createFullExportableApiLibraries(ctx)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100573}
574
575func combinedApisModuleFactory() android.Module {
576 module := &CombinedApis{}
577 module.AddProperties(&module.properties)
578 android.InitAndroidModule(module)
Harshit Mahajanb52adbc2023-12-15 21:56:42 +0000579 android.InitDefaultableModule(module)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100580 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) })
581 return module
582}
Anton Hanssonfd316452022-01-14 11:15:52 +0000583
584// Various utility methods below.
585
586// Creates an array of ":<m><tag>" for each m in <modules>.
587func createSrcs(modules []string, tag string) []string {
588 return transformArray(modules, ":", tag)
589}
590
591// Creates an array of "<prefix><m><suffix>", for each m in <modules>.
592func transformArray(modules []string, prefix, suffix string) []string {
593 a := make([]string, 0, len(modules))
594 for _, module := range modules {
595 a = append(a, prefix+module+suffix)
596 }
597 return a
598}
599
600func removeAll(s []string, vs []string) []string {
601 for _, v := range vs {
602 s = remove(s, v)
603 }
604 return s
605}
606
607func remove(s []string, v string) []string {
608 s2 := make([]string, 0, len(s))
609 for _, sv := range s {
610 if sv != v {
611 s2 = append(s2, sv)
612 }
613 }
614 return s2
615}
Harshit Mahajanb52adbc2023-12-15 21:56:42 +0000616
617// Defaults
618type CombinedApisModuleDefaults struct {
619 android.ModuleBase
620 android.DefaultsModuleBase
621}
622
623func CombinedApisModuleDefaultsFactory() android.Module {
624 module := &CombinedApisModuleDefaults{}
625 module.AddProperties(&CombinedApisProperties{})
626 android.InitDefaultsModule(module)
627 return module
628}