blob: 449fac63f90c12099ccbc43c6bb7504725731484 [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
57 Bootclasspath []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
61 System_server_classpath []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
Spandan Das67b79062024-02-12 11:40:51 +000082func (a *CombinedApis) apiFingerprintStubDeps() []string {
83 ret := []string{}
84 ret = append(
85 ret,
86 transformArray(a.properties.Bootclasspath, "", ".stubs")...,
87 )
88 ret = append(
89 ret,
90 transformArray(a.properties.Bootclasspath, "", ".stubs.system")...,
91 )
92 ret = append(
93 ret,
94 transformArray(a.properties.Bootclasspath, "", ".stubs.module_lib")...,
95 )
96 ret = append(
97 ret,
98 transformArray(a.properties.System_server_classpath, "", ".stubs.system_server")...,
99 )
100 return ret
101}
102
103func (a *CombinedApis) DepsMutator(ctx android.BottomUpMutatorContext) {
104 ctx.AddDependency(ctx.Module(), nil, a.apiFingerprintStubDeps()...)
105}
106
Anton Hansson0860aaf2021-10-08 16:48:03 +0100107func (a *CombinedApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Das67b79062024-02-12 11:40:51 +0000108 ctx.WalkDeps(func(child, parent android.Module) bool {
109 if _, ok := child.(java.AndroidLibraryDependency); ok && child.Name() != "framework-res" {
110 // Stubs of BCP and SSCP libraries should not have any dependencies on apps
111 // This check ensures that we do not run into circular dependencies when UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT=true
112 ctx.ModuleErrorf(
113 "Module %s is not a valid dependency of the stub library %s\n."+
114 "If this dependency has been added via `libs` of java_sdk_library, please move it to `impl_only_libs`\n",
115 child.Name(), parent.Name())
116 return false // error detected
117 }
118 return true
119 })
120
Anton Hansson0860aaf2021-10-08 16:48:03 +0100121}
122
123type genruleProps struct {
124 Name *string
125 Cmd *string
126 Dists []android.Dist
127 Out []string
128 Srcs []string
129 Tools []string
130 Visibility []string
131}
132
Anton Hanssoncb00f942022-01-13 09:45:12 +0000133type libraryProps struct {
Jihoon Kanga7073b52024-02-12 23:18:52 +0000134 Name *string
135 Sdk_version *string
136 Static_libs []string
137 Visibility []string
138 Defaults []string
139 Is_stubs_module *bool
Anton Hanssoncb00f942022-01-13 09:45:12 +0000140}
141
Anton Hansson4468d7c2022-01-14 12:10:01 +0000142type fgProps struct {
143 Name *string
144 Srcs []string
145 Visibility []string
146}
147
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000148type defaultsProps struct {
149 Name *string
150 Api_surface *string
151 Api_contributions []string
152 Defaults_visibility []string
Jihoon Kang471a05b2023-08-01 06:37:17 +0000153 Previous_api *string
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000154}
155
Anton Hansson0860aaf2021-10-08 16:48:03 +0100156// Struct to pass parameters for the various merged [current|removed].txt file modules we create.
157type MergedTxtDefinition struct {
158 // "current.txt" or "removed.txt"
159 TxtFilename string
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100160 // Filename in the new dist dir. "android.txt" or "android-removed.txt"
161 DistFilename string
Anton Hansson0860aaf2021-10-08 16:48:03 +0100162 // The module for the non-updatable / non-module part of the api.
163 BaseTxt string
164 // The list of modules that are relevant for this merged txt.
165 Modules []string
166 // The output tag for each module to use.e.g. {.public.api.txt} for current.txt
167 ModuleTag string
168 // public, system, module-lib or system-server
169 Scope string
170}
171
Jihoon Kang31cf2742024-02-07 19:52:19 +0000172func createMergedTxt(ctx android.LoadHookContext, txt MergedTxtDefinition, stubsTypeSuffix string, doDist bool) {
Anton Hansson0860aaf2021-10-08 16:48:03 +0100173 metalavaCmd := "$(location metalava)"
174 // Silence reflection warnings. See b/168689341
175 metalavaCmd += " -J--add-opens=java.base/java.util=ALL-UNNAMED "
Paul Duffinf3b1fc42023-08-14 22:03:06 +0100176 metalavaCmd += " --quiet merge-signatures --format=v2 "
Anton Hansson0860aaf2021-10-08 16:48:03 +0100177
178 filename := txt.TxtFilename
179 if txt.Scope != "public" {
180 filename = txt.Scope + "-" + filename
181 }
Jihoon Kang31cf2742024-02-07 19:52:19 +0000182 moduleName := ctx.ModuleName() + stubsTypeSuffix + filename
Chris Parsons3b7e34b2023-09-27 22:34:57 +0000183
Anton Hansson0860aaf2021-10-08 16:48:03 +0100184 props := genruleProps{}
Chris Parsons3b7e34b2023-09-27 22:34:57 +0000185 props.Name = proptools.StringPtr(moduleName)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100186 props.Tools = []string{"metalava"}
Anton Hansson16ff3572022-01-11 18:36:35 +0000187 props.Out = []string{filename}
Paul Duffinf3b1fc42023-08-14 22:03:06 +0100188 props.Cmd = proptools.StringPtr(metalavaCmd + "$(in) --out $(out)")
Anton Hanssonfd316452022-01-14 11:15:52 +0000189 props.Srcs = append([]string{txt.BaseTxt}, createSrcs(txt.Modules, txt.ModuleTag)...)
Jihoon Kang31cf2742024-02-07 19:52:19 +0000190 if doDist {
191 props.Dists = []android.Dist{
192 {
193 Targets: []string{"droidcore"},
194 Dir: proptools.StringPtr("api"),
195 Dest: proptools.StringPtr(filename),
196 },
197 {
198 Targets: []string{"api_txt", "sdk"},
199 Dir: proptools.StringPtr("apistubs/android/" + txt.Scope + "/api"),
200 Dest: proptools.StringPtr(txt.DistFilename),
201 },
202 }
Anton Hansson0860aaf2021-10-08 16:48:03 +0100203 }
204 props.Visibility = []string{"//visibility:public"}
Colin Crossc6420762023-12-07 12:38:40 -0800205 ctx.CreateModule(genrule.GenRuleFactory, &props)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100206}
207
Cole Faustdcda3702022-10-04 14:46:35 -0700208func createMergedAnnotationsFilegroups(ctx android.LoadHookContext, modules, system_server_modules []string) {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000209 for _, i := range []struct {
Cole Faustdcda3702022-10-04 14:46:35 -0700210 name string
211 tag string
212 modules []string
213 }{
214 {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000215 name: "all-modules-public-annotations",
216 tag: "{.public.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700217 modules: modules,
218 }, {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000219 name: "all-modules-system-annotations",
220 tag: "{.system.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700221 modules: modules,
222 }, {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000223 name: "all-modules-module-lib-annotations",
224 tag: "{.module-lib.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-server-annotations",
228 tag: "{.system-server.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700229 modules: system_server_modules,
230 },
231 } {
232 props := fgProps{}
233 props.Name = proptools.StringPtr(i.name)
234 props.Srcs = createSrcs(i.modules, i.tag)
Colin Crossc6420762023-12-07 12:38:40 -0800235 ctx.CreateModule(android.FileGroupFactory, &props)
Cole Faustdcda3702022-10-04 14:46:35 -0700236 }
Anton Hansson74b15642022-06-23 08:27:23 +0000237}
238
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000239func createMergedPublicStubs(ctx android.LoadHookContext, modules []string) {
240 props := libraryProps{}
241 props.Name = proptools.StringPtr("all-modules-public-stubs")
242 props.Static_libs = transformArray(modules, "", ".stubs")
243 props.Sdk_version = proptools.StringPtr("module_current")
244 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000245 props.Is_stubs_module = proptools.BoolPtr(true)
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000246 ctx.CreateModule(java.LibraryFactory, &props)
247}
248
Jihoon Kang059b9492023-12-29 00:40:34 +0000249func createMergedPublicExportableStubs(ctx android.LoadHookContext, modules []string) {
250 props := libraryProps{}
251 props.Name = proptools.StringPtr("all-modules-public-stubs-exportable")
252 props.Static_libs = transformArray(modules, "", ".stubs.exportable")
253 props.Sdk_version = proptools.StringPtr("module_current")
254 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000255 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang059b9492023-12-29 00:40:34 +0000256 ctx.CreateModule(java.LibraryFactory, &props)
257}
258
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000259func createMergedSystemStubs(ctx android.LoadHookContext, modules []string) {
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000260 // First create the all-updatable-modules-system-stubs
261 {
262 updatable_modules := removeAll(modules, non_updatable_modules)
263 props := libraryProps{}
264 props.Name = proptools.StringPtr("all-updatable-modules-system-stubs")
265 props.Static_libs = transformArray(updatable_modules, "", ".stubs.system")
266 props.Sdk_version = proptools.StringPtr("module_current")
267 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000268 props.Is_stubs_module = proptools.BoolPtr(true)
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000269 ctx.CreateModule(java.LibraryFactory, &props)
270 }
271 // Now merge all-updatable-modules-system-stubs and stubs from non-updatable modules
272 // into all-modules-system-stubs.
273 {
274 props := libraryProps{}
275 props.Name = proptools.StringPtr("all-modules-system-stubs")
276 props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.system")
277 props.Static_libs = append(props.Static_libs, "all-updatable-modules-system-stubs")
278 props.Sdk_version = proptools.StringPtr("module_current")
279 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000280 props.Is_stubs_module = proptools.BoolPtr(true)
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000281 ctx.CreateModule(java.LibraryFactory, &props)
282 }
283}
284
Jihoon Kang059b9492023-12-29 00:40:34 +0000285func createMergedSystemExportableStubs(ctx android.LoadHookContext, modules []string) {
286 // First create the all-updatable-modules-system-stubs
287 {
288 updatable_modules := removeAll(modules, non_updatable_modules)
289 props := libraryProps{}
290 props.Name = proptools.StringPtr("all-updatable-modules-system-stubs-exportable")
291 props.Static_libs = transformArray(updatable_modules, "", ".stubs.exportable.system")
292 props.Sdk_version = proptools.StringPtr("module_current")
293 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000294 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang059b9492023-12-29 00:40:34 +0000295 ctx.CreateModule(java.LibraryFactory, &props)
296 }
297 // Now merge all-updatable-modules-system-stubs and stubs from non-updatable modules
298 // into all-modules-system-stubs.
299 {
300 props := libraryProps{}
301 props.Name = proptools.StringPtr("all-modules-system-stubs-exportable")
302 props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.exportable.system")
303 props.Static_libs = append(props.Static_libs, "all-updatable-modules-system-stubs-exportable")
304 props.Sdk_version = proptools.StringPtr("module_current")
305 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000306 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang059b9492023-12-29 00:40:34 +0000307 ctx.CreateModule(java.LibraryFactory, &props)
308 }
309}
310
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000311func createMergedTestStubsForNonUpdatableModules(ctx android.LoadHookContext) {
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000312 props := libraryProps{}
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000313 props.Name = proptools.StringPtr("all-non-updatable-modules-test-stubs")
314 props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.test")
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000315 props.Sdk_version = proptools.StringPtr("module_current")
316 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000317 props.Is_stubs_module = proptools.BoolPtr(true)
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000318 ctx.CreateModule(java.LibraryFactory, &props)
319}
320
Jihoon Kang059b9492023-12-29 00:40:34 +0000321func createMergedTestExportableStubsForNonUpdatableModules(ctx android.LoadHookContext) {
322 props := libraryProps{}
323 props.Name = proptools.StringPtr("all-non-updatable-modules-test-stubs-exportable")
324 props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.exportable.test")
325 props.Sdk_version = proptools.StringPtr("module_current")
326 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000327 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang059b9492023-12-29 00:40:34 +0000328 ctx.CreateModule(java.LibraryFactory, &props)
329}
330
Anton Hansson95e89a82022-01-28 11:31:50 +0000331func createMergedFrameworkImpl(ctx android.LoadHookContext, modules []string) {
332 // This module is for the "framework-all" module, which should not include the core libraries.
333 modules = removeAll(modules, core_libraries_modules)
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +0000334 // Remove the modules that belong to non-updatable APEXes since those are allowed to compile
335 // against unstable APIs.
336 modules = removeAll(modules, non_updatable_modules)
337 // First create updatable-framework-module-impl, which contains all updatable modules.
338 // This module compiles against module_lib SDK.
339 {
340 props := libraryProps{}
341 props.Name = proptools.StringPtr("updatable-framework-module-impl")
342 props.Static_libs = transformArray(modules, "", ".impl")
343 props.Sdk_version = proptools.StringPtr("module_current")
344 props.Visibility = []string{"//frameworks/base"}
345 ctx.CreateModule(java.LibraryFactory, &props)
346 }
347
348 // Now create all-framework-module-impl, which contains updatable-framework-module-impl
349 // and all non-updatable modules. This module compiles against hidden APIs.
350 {
351 props := libraryProps{}
352 props.Name = proptools.StringPtr("all-framework-module-impl")
353 props.Static_libs = transformArray(non_updatable_modules, "", ".impl")
354 props.Static_libs = append(props.Static_libs, "updatable-framework-module-impl")
355 props.Sdk_version = proptools.StringPtr("core_platform")
356 props.Visibility = []string{"//frameworks/base"}
357 ctx.CreateModule(java.LibraryFactory, &props)
358 }
Anton Hansson95e89a82022-01-28 11:31:50 +0000359}
360
Jihoon Kang059b9492023-12-29 00:40:34 +0000361func createMergedFrameworkModuleLibExportableStubs(ctx android.LoadHookContext, modules []string) {
362 // The user of this module compiles against the "core" SDK and against non-updatable modules,
363 // so remove to avoid dupes.
364 modules = removeAll(modules, core_libraries_modules)
365 modules = removeAll(modules, non_updatable_modules)
366 props := libraryProps{}
367 props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api-exportable")
368 props.Static_libs = transformArray(modules, "", ".stubs.exportable.module_lib")
369 props.Sdk_version = proptools.StringPtr("module_current")
370 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000371 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang059b9492023-12-29 00:40:34 +0000372 ctx.CreateModule(java.LibraryFactory, &props)
373}
374
Anton Hansson95e89a82022-01-28 11:31:50 +0000375func createMergedFrameworkModuleLibStubs(ctx android.LoadHookContext, modules []string) {
Mark White3cc5e002023-08-07 11:18:09 +0000376 // The user of this module compiles against the "core" SDK and against non-updatable modules,
377 // so remove to avoid dupes.
Anton Hansson95e89a82022-01-28 11:31:50 +0000378 modules = removeAll(modules, core_libraries_modules)
Mark White3cc5e002023-08-07 11:18:09 +0000379 modules = removeAll(modules, non_updatable_modules)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000380 props := libraryProps{}
381 props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api")
Anton Hanssonfd316452022-01-14 11:15:52 +0000382 props.Static_libs = transformArray(modules, "", ".stubs.module_lib")
Anton Hanssoncb00f942022-01-13 09:45:12 +0000383 props.Sdk_version = proptools.StringPtr("module_current")
384 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000385 props.Is_stubs_module = proptools.BoolPtr(true)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000386 ctx.CreateModule(java.LibraryFactory, &props)
387}
388
Paul Duffinfb5e07d2024-05-02 14:51:41 +0100389func createMergedFrameworkSystemServerExportableStubs(ctx android.LoadHookContext, bootclasspath, system_server_classpath []string) {
390 // The user of this module compiles against the "core" SDK and against non-updatable bootclasspathModules,
391 // so remove to avoid dupes.
392 bootclasspathModules := removeAll(bootclasspath, core_libraries_modules)
393 bootclasspathModules = removeAll(bootclasspath, non_updatable_modules)
394 modules := append(
395 // Include all the module-lib APIs from the bootclasspath libraries.
396 transformArray(bootclasspathModules, "", ".stubs.exportable.module_lib"),
397 // Then add all the system-server APIs from the service-* libraries.
398 transformArray(system_server_classpath, "", ".stubs.exportable.system_server")...,
399 )
400 props := libraryProps{}
401 props.Name = proptools.StringPtr("framework-updatable-stubs-system_server_api-exportable")
402 props.Static_libs = modules
403 props.Sdk_version = proptools.StringPtr("system_server_current")
404 props.Visibility = []string{"//frameworks/base"}
405 props.Is_stubs_module = proptools.BoolPtr(true)
406 ctx.CreateModule(java.LibraryFactory, &props)
407}
408
Anton Hansson4468d7c2022-01-14 12:10:01 +0000409func createPublicStubsSourceFilegroup(ctx android.LoadHookContext, modules []string) {
410 props := fgProps{}
411 props.Name = proptools.StringPtr("all-modules-public-stubs-source")
412 props.Srcs = createSrcs(modules, "{.public.stubs.source}")
413 props.Visibility = []string{"//frameworks/base"}
Colin Crossc6420762023-12-07 12:38:40 -0800414 ctx.CreateModule(android.FileGroupFactory, &props)
Anton Hansson4468d7c2022-01-14 12:10:01 +0000415}
416
Jihoon Kang31cf2742024-02-07 19:52:19 +0000417func createMergedTxts(ctx android.LoadHookContext, bootclasspath, system_server_classpath []string, baseTxtModulePrefix, stubsTypeSuffix string, doDist bool) {
Anton Hansson0860aaf2021-10-08 16:48:03 +0100418 var textFiles []MergedTxtDefinition
Anton Hansson16ff3572022-01-11 18:36:35 +0000419
Anton Hansson0860aaf2021-10-08 16:48:03 +0100420 tagSuffix := []string{".api.txt}", ".removed-api.txt}"}
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100421 distFilename := []string{"android.txt", "android-removed.txt"}
Anton Hansson0860aaf2021-10-08 16:48:03 +0100422 for i, f := range []string{"current.txt", "removed.txt"} {
423 textFiles = append(textFiles, MergedTxtDefinition{
Colin Crossc6420762023-12-07 12:38:40 -0800424 TxtFilename: f,
425 DistFilename: distFilename[i],
Jihoon Kang31cf2742024-02-07 19:52:19 +0000426 BaseTxt: ":" + baseTxtModulePrefix + f,
Colin Crossc6420762023-12-07 12:38:40 -0800427 Modules: bootclasspath,
428 ModuleTag: "{.public" + tagSuffix[i],
429 Scope: "public",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100430 })
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 + "system-" + f,
Colin Crossc6420762023-12-07 12:38:40 -0800435 Modules: bootclasspath,
436 ModuleTag: "{.system" + tagSuffix[i],
437 Scope: "system",
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 + "module-lib-" + f,
Colin Crossc6420762023-12-07 12:38:40 -0800443 Modules: bootclasspath,
444 ModuleTag: "{.module-lib" + tagSuffix[i],
445 Scope: "module-lib",
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 + "system-server-" + f,
Colin Crossc6420762023-12-07 12:38:40 -0800451 Modules: system_server_classpath,
452 ModuleTag: "{.system-server" + tagSuffix[i],
453 Scope: "system-server",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100454 })
455 }
456 for _, txt := range textFiles {
Jihoon Kang31cf2742024-02-07 19:52:19 +0000457 createMergedTxt(ctx, txt, stubsTypeSuffix, doDist)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100458 }
459}
460
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000461func createApiContributionDefaults(ctx android.LoadHookContext, modules []string) {
462 defaultsSdkKinds := []android.SdkKind{
463 android.SdkPublic, android.SdkSystem, android.SdkModule,
464 }
465 for _, sdkKind := range defaultsSdkKinds {
466 props := defaultsProps{}
467 props.Name = proptools.StringPtr(
468 sdkKind.DefaultJavaLibraryName() + "_contributions")
469 if sdkKind == android.SdkModule {
470 props.Name = proptools.StringPtr(
471 sdkKind.DefaultJavaLibraryName() + "_contributions_full")
472 }
473 props.Api_surface = proptools.StringPtr(sdkKind.String())
474 apiSuffix := ""
475 if sdkKind != android.SdkPublic {
476 apiSuffix = "." + strings.ReplaceAll(sdkKind.String(), "-", "_")
477 }
478 props.Api_contributions = transformArray(
479 modules, "", fmt.Sprintf(".stubs.source%s.api.contribution", apiSuffix))
480 props.Defaults_visibility = []string{"//visibility:public"}
Jihoon Kang471a05b2023-08-01 06:37:17 +0000481 props.Previous_api = proptools.StringPtr(":android.api.public.latest")
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000482 ctx.CreateModule(java.DefaultsFactory, &props)
483 }
484}
485
Jihoon Kang1453baa2023-05-27 05:32:30 +0000486func createFullApiLibraries(ctx android.LoadHookContext) {
487 javaLibraryNames := []string{
488 "android_stubs_current",
489 "android_system_stubs_current",
490 "android_test_stubs_current",
Mark Whitee35b1382023-08-12 01:31:26 +0000491 "android_test_frameworks_core_stubs_current",
Jihoon Kang1453baa2023-05-27 05:32:30 +0000492 "android_module_lib_stubs_current",
493 "android_system_server_stubs_current",
494 }
495
496 for _, libraryName := range javaLibraryNames {
497 props := libraryProps{}
498 props.Name = proptools.StringPtr(libraryName)
499 staticLib := libraryName + ".from-source"
500 if ctx.Config().BuildFromTextStub() {
501 staticLib = libraryName + ".from-text"
502 }
503 props.Static_libs = []string{staticLib}
504 props.Defaults = []string{"android.jar_defaults"}
505 props.Visibility = []string{"//visibility:public"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000506 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang1453baa2023-05-27 05:32:30 +0000507
508 ctx.CreateModule(java.LibraryFactory, &props)
509 }
510}
511
Jihoon Kang059b9492023-12-29 00:40:34 +0000512func createFullExportableApiLibraries(ctx android.LoadHookContext) {
513 javaLibraryNames := []string{
514 "android_stubs_current_exportable",
515 "android_system_stubs_current_exportable",
516 "android_test_stubs_current_exportable",
517 "android_module_lib_stubs_current_exportable",
518 "android_system_server_stubs_current_exportable",
519 }
520
521 for _, libraryName := range javaLibraryNames {
522 props := libraryProps{}
523 props.Name = proptools.StringPtr(libraryName)
524 staticLib := libraryName + ".from-source"
525 props.Static_libs = []string{staticLib}
526 props.Defaults = []string{"android.jar_defaults"}
527 props.Visibility = []string{"//visibility:public"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000528 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang059b9492023-12-29 00:40:34 +0000529
530 ctx.CreateModule(java.LibraryFactory, &props)
531 }
532}
533
Anton Hansson0860aaf2021-10-08 16:48:03 +0100534func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) {
Anton Hansson07a12952022-01-12 17:28:39 +0000535 bootclasspath := a.properties.Bootclasspath
Cole Faustdcda3702022-10-04 14:46:35 -0700536 system_server_classpath := a.properties.System_server_classpath
Anton Hansson07a12952022-01-12 17:28:39 +0000537 if ctx.Config().VendorConfig("ANDROID").Bool("include_nonpublic_framework_api") {
538 bootclasspath = append(bootclasspath, a.properties.Conditional_bootclasspath...)
539 sort.Strings(bootclasspath)
540 }
Jihoon Kang31cf2742024-02-07 19:52:19 +0000541 createMergedTxts(ctx, bootclasspath, system_server_classpath, "non-updatable-", "-", false)
542 createMergedTxts(ctx, bootclasspath, system_server_classpath, "non-updatable-exportable-", "-exportable-", true)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100543
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000544 createMergedPublicStubs(ctx, bootclasspath)
545 createMergedSystemStubs(ctx, bootclasspath)
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000546 createMergedTestStubsForNonUpdatableModules(ctx)
Anton Hansson95e89a82022-01-28 11:31:50 +0000547 createMergedFrameworkModuleLibStubs(ctx, bootclasspath)
548 createMergedFrameworkImpl(ctx, bootclasspath)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000549
Jihoon Kang059b9492023-12-29 00:40:34 +0000550 createMergedPublicExportableStubs(ctx, bootclasspath)
551 createMergedSystemExportableStubs(ctx, bootclasspath)
552 createMergedTestExportableStubsForNonUpdatableModules(ctx)
553 createMergedFrameworkModuleLibExportableStubs(ctx, bootclasspath)
Paul Duffinfb5e07d2024-05-02 14:51:41 +0100554 createMergedFrameworkSystemServerExportableStubs(ctx, bootclasspath, system_server_classpath)
Jihoon Kang059b9492023-12-29 00:40:34 +0000555
Cole Faustdcda3702022-10-04 14:46:35 -0700556 createMergedAnnotationsFilegroups(ctx, bootclasspath, system_server_classpath)
Anton Hanssoncc18e032022-01-12 14:45:22 +0000557
Anton Hansson4468d7c2022-01-14 12:10:01 +0000558 createPublicStubsSourceFilegroup(ctx, bootclasspath)
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000559
560 createApiContributionDefaults(ctx, bootclasspath)
Jihoon Kang1453baa2023-05-27 05:32:30 +0000561
562 createFullApiLibraries(ctx)
Jihoon Kang059b9492023-12-29 00:40:34 +0000563
564 createFullExportableApiLibraries(ctx)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100565}
566
567func combinedApisModuleFactory() android.Module {
568 module := &CombinedApis{}
569 module.AddProperties(&module.properties)
570 android.InitAndroidModule(module)
Harshit Mahajanb52adbc2023-12-15 21:56:42 +0000571 android.InitDefaultableModule(module)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100572 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) })
573 return module
574}
Anton Hanssonfd316452022-01-14 11:15:52 +0000575
576// Various utility methods below.
577
578// Creates an array of ":<m><tag>" for each m in <modules>.
579func createSrcs(modules []string, tag string) []string {
580 return transformArray(modules, ":", tag)
581}
582
583// Creates an array of "<prefix><m><suffix>", for each m in <modules>.
584func transformArray(modules []string, prefix, suffix string) []string {
585 a := make([]string, 0, len(modules))
586 for _, module := range modules {
587 a = append(a, prefix+module+suffix)
588 }
589 return a
590}
591
592func removeAll(s []string, vs []string) []string {
593 for _, v := range vs {
594 s = remove(s, v)
595 }
596 return s
597}
598
599func remove(s []string, v string) []string {
600 s2 := make([]string, 0, len(s))
601 for _, sv := range s {
602 if sv != v {
603 s2 = append(s2, sv)
604 }
605 }
606 return s2
607}
Harshit Mahajanb52adbc2023-12-15 21:56:42 +0000608
609// Defaults
610type CombinedApisModuleDefaults struct {
611 android.ModuleBase
612 android.DefaultsModuleBase
613}
614
615func CombinedApisModuleDefaultsFactory() android.Module {
616 module := &CombinedApisModuleDefaults{}
617 module.AddProperties(&CombinedApisProperties{})
618 android.InitDefaultsModule(module)
619 return module
620}