blob: 640773be0f9b881434d519781ec157a84fe5bde9 [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 (
Cole Faustb1fc07e2024-09-05 11:37:57 -070018 "slices"
Anton Hansson07a12952022-01-12 17:28:39 +000019
Anton Hansson0860aaf2021-10-08 16:48:03 +010020 "github.com/google/blueprint/proptools"
21
22 "android/soong/android"
Anton Hanssoncb00f942022-01-13 09:45:12 +000023 "android/soong/java"
Anton Hansson0860aaf2021-10-08 16:48:03 +010024)
25
Anton Hansson05e944d2022-01-13 12:26:30 +000026const art = "art.module.public.api"
27const conscrypt = "conscrypt.module.public.api"
28const i18n = "i18n.module.public.api"
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +000029const virtualization = "framework-virtualization"
Mark White3cc5e002023-08-07 11:18:09 +000030const location = "framework-location"
Harshit Mahajan989825d2024-10-09 12:42:55 +000031const platformCrashrecovery = "framework-platformcrashrecovery"
Sandeep Bandaru46f44ce2024-11-26 18:44:20 +000032const ondeviceintelligence = "framework-ondeviceintelligence-platform"
Anton Hansson09a9c4e2022-04-08 10:59:46 +010033
Anton Hansson95e89a82022-01-28 11:31:50 +000034var core_libraries_modules = []string{art, conscrypt, i18n}
Zi Wang0d6a5302023-02-16 14:54:01 -080035
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +000036// List of modules that are not yet updatable, and hence they can still compile
37// against hidden APIs. These modules are filtered out when building the
38// updatable-framework-module-impl (because updatable-framework-module-impl is
39// built against module_current SDK). Instead they are directly statically
40// linked into the all-framework-module-lib, which is building against hidden
41// APIs.
Nikita Ioffe5593fbb2022-12-01 14:52:34 +000042// In addition, the modules in this list are allowed to contribute to test APIs
43// stubs.
Sandeep Bandaru46f44ce2024-11-26 18:44:20 +000044var non_updatable_modules = []string{virtualization, location, platformCrashrecovery, ondeviceintelligence}
Anton Hansson05e944d2022-01-13 12:26:30 +000045
Anton Hansson0860aaf2021-10-08 16:48:03 +010046// The intention behind this soong plugin is to generate a number of "merged"
47// API-related modules that would otherwise require a large amount of very
48// similar Android.bp boilerplate to define. For example, the merged current.txt
49// API definitions (created by merging the non-updatable current.txt with all
50// the module current.txts). This simplifies the addition of new android
51// modules, by reducing the number of genrules etc a new module must be added to.
52
53// The properties of the combined_apis module type.
54type CombinedApisProperties struct {
Anton Hansson16ff3572022-01-11 18:36:35 +000055 // Module libraries in the bootclasspath
Jihoon Kangcc4c8f92024-07-18 18:52:03 +000056 Bootclasspath proptools.Configurable[[]string]
Anton Hansson07a12952022-01-12 17:28:39 +000057 // Module libraries on the bootclasspath if include_nonpublic_framework_api is true.
58 Conditional_bootclasspath []string
Anton Hansson16ff3572022-01-11 18:36:35 +000059 // Module libraries in system server
Jihoon Kangcc4c8f92024-07-18 18:52:03 +000060 System_server_classpath proptools.Configurable[[]string]
Anton Hansson0860aaf2021-10-08 16:48:03 +010061}
62
63type CombinedApis struct {
64 android.ModuleBase
65
66 properties CombinedApisProperties
67}
68
69func init() {
70 registerBuildComponents(android.InitRegistrationContext)
71}
72
73func registerBuildComponents(ctx android.RegistrationContext) {
74 ctx.RegisterModuleType("combined_apis", combinedApisModuleFactory)
75}
76
77var PrepareForCombinedApisTest = android.FixtureRegisterWithContext(registerBuildComponents)
78
Jihoon Kangcc4c8f92024-07-18 18:52:03 +000079func (a *CombinedApis) apiFingerprintStubDeps(ctx android.BottomUpMutatorContext) []string {
Cole Faustb1fc07e2024-09-05 11:37:57 -070080 bootClasspath := a.properties.Bootclasspath.GetOrDefault(ctx, nil)
81 systemServerClasspath := a.properties.System_server_classpath.GetOrDefault(ctx, nil)
82 var ret []string
Spandan Das67b79062024-02-12 11:40:51 +000083 ret = append(
84 ret,
Cole Faustb1fc07e2024-09-05 11:37:57 -070085 transformArray(bootClasspath, "", ".stubs")...,
Spandan Das67b79062024-02-12 11:40:51 +000086 )
87 ret = append(
88 ret,
Cole Faustb1fc07e2024-09-05 11:37:57 -070089 transformArray(bootClasspath, "", ".stubs.system")...,
Spandan Das67b79062024-02-12 11:40:51 +000090 )
91 ret = append(
92 ret,
Cole Faustb1fc07e2024-09-05 11:37:57 -070093 transformArray(bootClasspath, "", ".stubs.module_lib")...,
Spandan Das67b79062024-02-12 11:40:51 +000094 )
95 ret = append(
96 ret,
Cole Faustb1fc07e2024-09-05 11:37:57 -070097 transformArray(systemServerClasspath, "", ".stubs.system_server")...,
Spandan Das67b79062024-02-12 11:40:51 +000098 )
99 return ret
100}
101
102func (a *CombinedApis) DepsMutator(ctx android.BottomUpMutatorContext) {
Jihoon Kangcc4c8f92024-07-18 18:52:03 +0000103 ctx.AddDependency(ctx.Module(), nil, a.apiFingerprintStubDeps(ctx)...)
Spandan Das67b79062024-02-12 11:40:51 +0000104}
105
Anton Hansson0860aaf2021-10-08 16:48:03 +0100106func (a *CombinedApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Yu Liud3614e02025-01-10 19:08:54 +0000107 ctx.WalkDepsProxy(func(child, parent android.ModuleProxy) bool {
108 javaInfo, ok := android.OtherModuleProvider(ctx, child, java.JavaInfoProvider)
109 if ok && javaInfo.AndroidLibraryDependencyInfo != nil && child.Name() != "framework-res" {
Spandan Das67b79062024-02-12 11:40:51 +0000110 // 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
Cole Faustb1fc07e2024-09-05 11:37:57 -0700128 Srcs proptools.Configurable[[]string]
Anton Hansson0860aaf2021-10-08 16:48:03 +0100129 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
Cole Faustb1fc07e2024-09-05 11:37:57 -0700136 Static_libs proptools.Configurable[[]string]
Jihoon Kanga7073b52024-02-12 23:18:52 +0000137 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 {
Cole Faust5aeb9fd2024-10-22 16:30:57 -0700143 Name *string
144 Srcs proptools.Configurable[[]string]
145 Device_common_srcs proptools.Configurable[[]string]
146 Visibility []string
Anton Hansson4468d7c2022-01-14 12:10:01 +0000147}
148
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000149type defaultsProps struct {
150 Name *string
151 Api_surface *string
152 Api_contributions []string
153 Defaults_visibility []string
Jihoon Kang471a05b2023-08-01 06:37:17 +0000154 Previous_api *string
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000155}
156
Anton Hansson0860aaf2021-10-08 16:48:03 +0100157// Struct to pass parameters for the various merged [current|removed].txt file modules we create.
158type MergedTxtDefinition struct {
159 // "current.txt" or "removed.txt"
160 TxtFilename string
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100161 // Filename in the new dist dir. "android.txt" or "android-removed.txt"
162 DistFilename string
Anton Hansson0860aaf2021-10-08 16:48:03 +0100163 // The module for the non-updatable / non-module part of the api.
164 BaseTxt string
165 // The list of modules that are relevant for this merged txt.
Cole Faustb1fc07e2024-09-05 11:37:57 -0700166 Modules proptools.Configurable[[]string]
Anton Hansson0860aaf2021-10-08 16:48:03 +0100167 // The output tag for each module to use.e.g. {.public.api.txt} for current.txt
168 ModuleTag string
169 // public, system, module-lib or system-server
170 Scope string
171}
172
Jihoon Kang31cf2742024-02-07 19:52:19 +0000173func createMergedTxt(ctx android.LoadHookContext, txt MergedTxtDefinition, stubsTypeSuffix string, doDist bool) {
Anton Hansson0860aaf2021-10-08 16:48:03 +0100174 metalavaCmd := "$(location metalava)"
175 // Silence reflection warnings. See b/168689341
176 metalavaCmd += " -J--add-opens=java.base/java.util=ALL-UNNAMED "
Paul Duffinf3b1fc42023-08-14 22:03:06 +0100177 metalavaCmd += " --quiet merge-signatures --format=v2 "
Anton Hansson0860aaf2021-10-08 16:48:03 +0100178
179 filename := txt.TxtFilename
180 if txt.Scope != "public" {
181 filename = txt.Scope + "-" + filename
182 }
Jihoon Kang31cf2742024-02-07 19:52:19 +0000183 moduleName := ctx.ModuleName() + stubsTypeSuffix + filename
Chris Parsons3b7e34b2023-09-27 22:34:57 +0000184
Anton Hansson0860aaf2021-10-08 16:48:03 +0100185 props := genruleProps{}
Chris Parsons3b7e34b2023-09-27 22:34:57 +0000186 props.Name = proptools.StringPtr(moduleName)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100187 props.Tools = []string{"metalava"}
Anton Hansson16ff3572022-01-11 18:36:35 +0000188 props.Out = []string{filename}
Paul Duffinf3b1fc42023-08-14 22:03:06 +0100189 props.Cmd = proptools.StringPtr(metalavaCmd + "$(in) --out $(out)")
Cole Faustb1fc07e2024-09-05 11:37:57 -0700190 props.Srcs = proptools.NewSimpleConfigurable([]string{txt.BaseTxt})
191 props.Srcs.Append(createSrcs(txt.Modules, txt.ModuleTag))
Jihoon Kang31cf2742024-02-07 19:52:19 +0000192 if doDist {
193 props.Dists = []android.Dist{
194 {
195 Targets: []string{"droidcore"},
196 Dir: proptools.StringPtr("api"),
197 Dest: proptools.StringPtr(filename),
198 },
199 {
200 Targets: []string{"api_txt", "sdk"},
201 Dir: proptools.StringPtr("apistubs/android/" + txt.Scope + "/api"),
202 Dest: proptools.StringPtr(txt.DistFilename),
203 },
204 }
Anton Hansson0860aaf2021-10-08 16:48:03 +0100205 }
206 props.Visibility = []string{"//visibility:public"}
Cole Faust5aeb9fd2024-10-22 16:30:57 -0700207 ctx.CreateModule(java.GenRuleFactory, &props)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100208}
209
Cole Faustb1fc07e2024-09-05 11:37:57 -0700210func createMergedAnnotationsFilegroups(ctx android.LoadHookContext, modules, system_server_modules proptools.Configurable[[]string]) {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000211 for _, i := range []struct {
Cole Faustdcda3702022-10-04 14:46:35 -0700212 name string
213 tag string
Cole Faustb1fc07e2024-09-05 11:37:57 -0700214 modules proptools.Configurable[[]string]
Cole Faustdcda3702022-10-04 14:46:35 -0700215 }{
216 {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000217 name: "all-modules-public-annotations",
218 tag: "{.public.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700219 modules: modules,
220 }, {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000221 name: "all-modules-system-annotations",
222 tag: "{.system.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700223 modules: modules,
224 }, {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000225 name: "all-modules-module-lib-annotations",
226 tag: "{.module-lib.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700227 modules: modules,
228 }, {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000229 name: "all-modules-system-server-annotations",
230 tag: "{.system-server.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700231 modules: system_server_modules,
232 },
233 } {
234 props := fgProps{}
235 props.Name = proptools.StringPtr(i.name)
Cole Faust5aeb9fd2024-10-22 16:30:57 -0700236 props.Device_common_srcs = createSrcs(i.modules, i.tag)
Colin Crossc6420762023-12-07 12:38:40 -0800237 ctx.CreateModule(android.FileGroupFactory, &props)
Cole Faustdcda3702022-10-04 14:46:35 -0700238 }
Anton Hansson74b15642022-06-23 08:27:23 +0000239}
240
Cole Faustb1fc07e2024-09-05 11:37:57 -0700241func createMergedPublicStubs(ctx android.LoadHookContext, modules proptools.Configurable[[]string]) {
242 modules = modules.Clone()
243 transformConfigurableArray(modules, "", ".stubs")
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000244 props := libraryProps{}
245 props.Name = proptools.StringPtr("all-modules-public-stubs")
Cole Faustb1fc07e2024-09-05 11:37:57 -0700246 props.Static_libs = modules
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000247 props.Sdk_version = proptools.StringPtr("module_current")
248 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000249 props.Is_stubs_module = proptools.BoolPtr(true)
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000250 ctx.CreateModule(java.LibraryFactory, &props)
251}
252
Cole Faustb1fc07e2024-09-05 11:37:57 -0700253func createMergedPublicExportableStubs(ctx android.LoadHookContext, modules proptools.Configurable[[]string]) {
254 modules = modules.Clone()
255 transformConfigurableArray(modules, "", ".stubs.exportable")
Jihoon Kang059b9492023-12-29 00:40:34 +0000256 props := libraryProps{}
257 props.Name = proptools.StringPtr("all-modules-public-stubs-exportable")
Cole Faustb1fc07e2024-09-05 11:37:57 -0700258 props.Static_libs = modules
Jihoon Kang059b9492023-12-29 00:40:34 +0000259 props.Sdk_version = proptools.StringPtr("module_current")
260 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000261 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang059b9492023-12-29 00:40:34 +0000262 ctx.CreateModule(java.LibraryFactory, &props)
263}
264
Cole Faustb1fc07e2024-09-05 11:37:57 -0700265func createMergedSystemStubs(ctx android.LoadHookContext, modules proptools.Configurable[[]string]) {
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000266 // First create the all-updatable-modules-system-stubs
267 {
Cole Faustb1fc07e2024-09-05 11:37:57 -0700268 updatable_modules := modules.Clone()
269 removeAll(updatable_modules, non_updatable_modules)
270 transformConfigurableArray(updatable_modules, "", ".stubs.system")
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000271 props := libraryProps{}
272 props.Name = proptools.StringPtr("all-updatable-modules-system-stubs")
Cole Faustb1fc07e2024-09-05 11:37:57 -0700273 props.Static_libs = updatable_modules
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000274 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 {
Cole Faustb1fc07e2024-09-05 11:37:57 -0700282 static_libs := transformArray(non_updatable_modules, "", ".stubs.system")
283 static_libs = append(static_libs, "all-updatable-modules-system-stubs")
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000284 props := libraryProps{}
285 props.Name = proptools.StringPtr("all-modules-system-stubs")
Cole Faustb1fc07e2024-09-05 11:37:57 -0700286 props.Static_libs = proptools.NewSimpleConfigurable(static_libs)
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000287 props.Sdk_version = proptools.StringPtr("module_current")
288 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000289 props.Is_stubs_module = proptools.BoolPtr(true)
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000290 ctx.CreateModule(java.LibraryFactory, &props)
291 }
292}
293
Cole Faustb1fc07e2024-09-05 11:37:57 -0700294func createMergedSystemExportableStubs(ctx android.LoadHookContext, modules proptools.Configurable[[]string]) {
Jihoon Kang059b9492023-12-29 00:40:34 +0000295 // First create the all-updatable-modules-system-stubs
296 {
Cole Faustb1fc07e2024-09-05 11:37:57 -0700297 updatable_modules := modules.Clone()
298 removeAll(updatable_modules, non_updatable_modules)
299 transformConfigurableArray(updatable_modules, "", ".stubs.exportable.system")
Jihoon Kang059b9492023-12-29 00:40:34 +0000300 props := libraryProps{}
301 props.Name = proptools.StringPtr("all-updatable-modules-system-stubs-exportable")
Cole Faustb1fc07e2024-09-05 11:37:57 -0700302 props.Static_libs = updatable_modules
Jihoon Kang059b9492023-12-29 00:40:34 +0000303 props.Sdk_version = proptools.StringPtr("module_current")
304 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000305 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang059b9492023-12-29 00:40:34 +0000306 ctx.CreateModule(java.LibraryFactory, &props)
307 }
308 // Now merge all-updatable-modules-system-stubs and stubs from non-updatable modules
309 // into all-modules-system-stubs.
310 {
Cole Faustb1fc07e2024-09-05 11:37:57 -0700311 static_libs := transformArray(non_updatable_modules, "", ".stubs.exportable.system")
312 static_libs = append(static_libs, "all-updatable-modules-system-stubs-exportable")
Jihoon Kang059b9492023-12-29 00:40:34 +0000313 props := libraryProps{}
314 props.Name = proptools.StringPtr("all-modules-system-stubs-exportable")
Cole Faustb1fc07e2024-09-05 11:37:57 -0700315 props.Static_libs = proptools.NewSimpleConfigurable(static_libs)
Jihoon Kang059b9492023-12-29 00:40:34 +0000316 props.Sdk_version = proptools.StringPtr("module_current")
317 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000318 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang059b9492023-12-29 00:40:34 +0000319 ctx.CreateModule(java.LibraryFactory, &props)
320 }
321}
322
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000323func createMergedTestStubsForNonUpdatableModules(ctx android.LoadHookContext) {
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000324 props := libraryProps{}
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000325 props.Name = proptools.StringPtr("all-non-updatable-modules-test-stubs")
Cole Faustb1fc07e2024-09-05 11:37:57 -0700326 props.Static_libs = proptools.NewSimpleConfigurable(transformArray(non_updatable_modules, "", ".stubs.test"))
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000327 props.Sdk_version = proptools.StringPtr("module_current")
328 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000329 props.Is_stubs_module = proptools.BoolPtr(true)
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000330 ctx.CreateModule(java.LibraryFactory, &props)
331}
332
Jihoon Kang059b9492023-12-29 00:40:34 +0000333func createMergedTestExportableStubsForNonUpdatableModules(ctx android.LoadHookContext) {
334 props := libraryProps{}
335 props.Name = proptools.StringPtr("all-non-updatable-modules-test-stubs-exportable")
Cole Faustb1fc07e2024-09-05 11:37:57 -0700336 props.Static_libs = proptools.NewSimpleConfigurable(transformArray(non_updatable_modules, "", ".stubs.exportable.test"))
Jihoon Kang059b9492023-12-29 00:40:34 +0000337 props.Sdk_version = proptools.StringPtr("module_current")
338 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000339 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang059b9492023-12-29 00:40:34 +0000340 ctx.CreateModule(java.LibraryFactory, &props)
341}
342
Cole Faustb1fc07e2024-09-05 11:37:57 -0700343func createMergedFrameworkImpl(ctx android.LoadHookContext, modules proptools.Configurable[[]string]) {
344 modules = modules.Clone()
Anton Hansson95e89a82022-01-28 11:31:50 +0000345 // This module is for the "framework-all" module, which should not include the core libraries.
Cole Faustb1fc07e2024-09-05 11:37:57 -0700346 removeAll(modules, core_libraries_modules)
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +0000347 // Remove the modules that belong to non-updatable APEXes since those are allowed to compile
348 // against unstable APIs.
Cole Faustb1fc07e2024-09-05 11:37:57 -0700349 removeAll(modules, non_updatable_modules)
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +0000350 // First create updatable-framework-module-impl, which contains all updatable modules.
351 // This module compiles against module_lib SDK.
352 {
Cole Faustb1fc07e2024-09-05 11:37:57 -0700353 transformConfigurableArray(modules, "", ".impl")
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +0000354 props := libraryProps{}
355 props.Name = proptools.StringPtr("updatable-framework-module-impl")
Cole Faustb1fc07e2024-09-05 11:37:57 -0700356 props.Static_libs = modules
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +0000357 props.Sdk_version = proptools.StringPtr("module_current")
358 props.Visibility = []string{"//frameworks/base"}
359 ctx.CreateModule(java.LibraryFactory, &props)
360 }
361
362 // Now create all-framework-module-impl, which contains updatable-framework-module-impl
363 // and all non-updatable modules. This module compiles against hidden APIs.
364 {
Cole Faustb1fc07e2024-09-05 11:37:57 -0700365 static_libs := transformArray(non_updatable_modules, "", ".impl")
366 static_libs = append(static_libs, "updatable-framework-module-impl")
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +0000367 props := libraryProps{}
368 props.Name = proptools.StringPtr("all-framework-module-impl")
Cole Faustb1fc07e2024-09-05 11:37:57 -0700369 props.Static_libs = proptools.NewSimpleConfigurable(static_libs)
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +0000370 props.Sdk_version = proptools.StringPtr("core_platform")
371 props.Visibility = []string{"//frameworks/base"}
372 ctx.CreateModule(java.LibraryFactory, &props)
373 }
Anton Hansson95e89a82022-01-28 11:31:50 +0000374}
375
Cole Faustb1fc07e2024-09-05 11:37:57 -0700376func createMergedFrameworkModuleLibExportableStubs(ctx android.LoadHookContext, modules proptools.Configurable[[]string]) {
377 modules = modules.Clone()
Jihoon Kang059b9492023-12-29 00:40:34 +0000378 // The user of this module compiles against the "core" SDK and against non-updatable modules,
379 // so remove to avoid dupes.
Cole Faustb1fc07e2024-09-05 11:37:57 -0700380 removeAll(modules, core_libraries_modules)
381 removeAll(modules, non_updatable_modules)
382 transformConfigurableArray(modules, "", ".stubs.exportable.module_lib")
Jihoon Kang059b9492023-12-29 00:40:34 +0000383 props := libraryProps{}
384 props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api-exportable")
Cole Faustb1fc07e2024-09-05 11:37:57 -0700385 props.Static_libs = modules
Jihoon Kang059b9492023-12-29 00:40:34 +0000386 props.Sdk_version = proptools.StringPtr("module_current")
387 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000388 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang059b9492023-12-29 00:40:34 +0000389 ctx.CreateModule(java.LibraryFactory, &props)
390}
391
Cole Faustb1fc07e2024-09-05 11:37:57 -0700392func createMergedFrameworkModuleLibStubs(ctx android.LoadHookContext, modules proptools.Configurable[[]string]) {
393 modules = modules.Clone()
Mark White3cc5e002023-08-07 11:18:09 +0000394 // The user of this module compiles against the "core" SDK and against non-updatable modules,
395 // so remove to avoid dupes.
Cole Faustb1fc07e2024-09-05 11:37:57 -0700396 removeAll(modules, core_libraries_modules)
397 removeAll(modules, non_updatable_modules)
398 transformConfigurableArray(modules, "", ".stubs.module_lib")
Anton Hanssoncb00f942022-01-13 09:45:12 +0000399 props := libraryProps{}
400 props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api")
Cole Faustb1fc07e2024-09-05 11:37:57 -0700401 props.Static_libs = modules
Anton Hanssoncb00f942022-01-13 09:45:12 +0000402 props.Sdk_version = proptools.StringPtr("module_current")
403 props.Visibility = []string{"//frameworks/base"}
Jihoon Kanga7073b52024-02-12 23:18:52 +0000404 props.Is_stubs_module = proptools.BoolPtr(true)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000405 ctx.CreateModule(java.LibraryFactory, &props)
406}
407
Cole Faustb1fc07e2024-09-05 11:37:57 -0700408func createMergedFrameworkSystemServerExportableStubs(ctx android.LoadHookContext, bootclasspath, system_server_classpath proptools.Configurable[[]string]) {
Paul Duffinfb5e07d2024-05-02 14:51:41 +0100409 // The user of this module compiles against the "core" SDK and against non-updatable bootclasspathModules,
410 // so remove to avoid dupes.
Cole Faustb1fc07e2024-09-05 11:37:57 -0700411 bootclasspathModules := bootclasspath.Clone()
412 removeAll(bootclasspathModules, core_libraries_modules)
413 removeAll(bootclasspathModules, non_updatable_modules)
414 transformConfigurableArray(bootclasspathModules, "", ".stubs.exportable.module_lib")
415
416 system_server_classpath = system_server_classpath.Clone()
417 transformConfigurableArray(system_server_classpath, "", ".stubs.exportable.system_server")
418
419 // Include all the module-lib APIs from the bootclasspath libraries.
420 // Then add all the system-server APIs from the service-* libraries.
421 bootclasspathModules.Append(system_server_classpath)
422
Paul Duffinfb5e07d2024-05-02 14:51:41 +0100423 props := libraryProps{}
424 props.Name = proptools.StringPtr("framework-updatable-stubs-system_server_api-exportable")
Cole Faustb1fc07e2024-09-05 11:37:57 -0700425 props.Static_libs = bootclasspathModules
Paul Duffinfb5e07d2024-05-02 14:51:41 +0100426 props.Sdk_version = proptools.StringPtr("system_server_current")
427 props.Visibility = []string{"//frameworks/base"}
428 props.Is_stubs_module = proptools.BoolPtr(true)
429 ctx.CreateModule(java.LibraryFactory, &props)
430}
431
Cole Faustb1fc07e2024-09-05 11:37:57 -0700432func createPublicStubsSourceFilegroup(ctx android.LoadHookContext, modules proptools.Configurable[[]string]) {
Anton Hansson4468d7c2022-01-14 12:10:01 +0000433 props := fgProps{}
Jihoon Kange4553d02024-11-07 19:40:30 +0000434 props.Name = proptools.StringPtr("all-modules-public-stubs-source-exportable")
435 transformConfigurableArray(modules, "", ".stubs.source")
436 props.Device_common_srcs = createSrcs(modules, "{.exportable}")
Anton Hansson4468d7c2022-01-14 12:10:01 +0000437 props.Visibility = []string{"//frameworks/base"}
Colin Crossc6420762023-12-07 12:38:40 -0800438 ctx.CreateModule(android.FileGroupFactory, &props)
Anton Hansson4468d7c2022-01-14 12:10:01 +0000439}
440
Cole Faustb1fc07e2024-09-05 11:37:57 -0700441func createMergedTxts(
442 ctx android.LoadHookContext,
443 bootclasspath proptools.Configurable[[]string],
444 system_server_classpath proptools.Configurable[[]string],
445 baseTxtModulePrefix string,
446 stubsTypeSuffix string,
447 doDist bool,
448) {
Anton Hansson0860aaf2021-10-08 16:48:03 +0100449 var textFiles []MergedTxtDefinition
Anton Hansson16ff3572022-01-11 18:36:35 +0000450
Anton Hansson0860aaf2021-10-08 16:48:03 +0100451 tagSuffix := []string{".api.txt}", ".removed-api.txt}"}
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100452 distFilename := []string{"android.txt", "android-removed.txt"}
Anton Hansson0860aaf2021-10-08 16:48:03 +0100453 for i, f := range []string{"current.txt", "removed.txt"} {
454 textFiles = append(textFiles, MergedTxtDefinition{
Colin Crossc6420762023-12-07 12:38:40 -0800455 TxtFilename: f,
456 DistFilename: distFilename[i],
Jihoon Kang31cf2742024-02-07 19:52:19 +0000457 BaseTxt: ":" + baseTxtModulePrefix + f,
Colin Crossc6420762023-12-07 12:38:40 -0800458 Modules: bootclasspath,
459 ModuleTag: "{.public" + tagSuffix[i],
460 Scope: "public",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100461 })
462 textFiles = append(textFiles, MergedTxtDefinition{
Colin Crossc6420762023-12-07 12:38:40 -0800463 TxtFilename: f,
464 DistFilename: distFilename[i],
Jihoon Kang31cf2742024-02-07 19:52:19 +0000465 BaseTxt: ":" + baseTxtModulePrefix + "system-" + f,
Colin Crossc6420762023-12-07 12:38:40 -0800466 Modules: bootclasspath,
467 ModuleTag: "{.system" + tagSuffix[i],
468 Scope: "system",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100469 })
470 textFiles = append(textFiles, MergedTxtDefinition{
Colin Crossc6420762023-12-07 12:38:40 -0800471 TxtFilename: f,
472 DistFilename: distFilename[i],
Jihoon Kang31cf2742024-02-07 19:52:19 +0000473 BaseTxt: ":" + baseTxtModulePrefix + "module-lib-" + f,
Colin Crossc6420762023-12-07 12:38:40 -0800474 Modules: bootclasspath,
475 ModuleTag: "{.module-lib" + tagSuffix[i],
476 Scope: "module-lib",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100477 })
478 textFiles = append(textFiles, MergedTxtDefinition{
Colin Crossc6420762023-12-07 12:38:40 -0800479 TxtFilename: f,
480 DistFilename: distFilename[i],
Jihoon Kang31cf2742024-02-07 19:52:19 +0000481 BaseTxt: ":" + baseTxtModulePrefix + "system-server-" + f,
Colin Crossc6420762023-12-07 12:38:40 -0800482 Modules: system_server_classpath,
483 ModuleTag: "{.system-server" + tagSuffix[i],
484 Scope: "system-server",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100485 })
486 }
487 for _, txt := range textFiles {
Jihoon Kang31cf2742024-02-07 19:52:19 +0000488 createMergedTxt(ctx, txt, stubsTypeSuffix, doDist)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100489 }
490}
491
492func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) {
Cole Faustb1fc07e2024-09-05 11:37:57 -0700493 bootclasspath := a.properties.Bootclasspath.Clone()
494 system_server_classpath := a.properties.System_server_classpath.Clone()
Anton Hansson07a12952022-01-12 17:28:39 +0000495 if ctx.Config().VendorConfig("ANDROID").Bool("include_nonpublic_framework_api") {
Cole Faustb1fc07e2024-09-05 11:37:57 -0700496 bootclasspath.AppendSimpleValue(a.properties.Conditional_bootclasspath)
Anton Hansson07a12952022-01-12 17:28:39 +0000497 }
Jihoon Kang31cf2742024-02-07 19:52:19 +0000498 createMergedTxts(ctx, bootclasspath, system_server_classpath, "non-updatable-", "-", false)
499 createMergedTxts(ctx, bootclasspath, system_server_classpath, "non-updatable-exportable-", "-exportable-", true)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100500
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000501 createMergedPublicStubs(ctx, bootclasspath)
502 createMergedSystemStubs(ctx, bootclasspath)
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000503 createMergedTestStubsForNonUpdatableModules(ctx)
Anton Hansson95e89a82022-01-28 11:31:50 +0000504 createMergedFrameworkModuleLibStubs(ctx, bootclasspath)
505 createMergedFrameworkImpl(ctx, bootclasspath)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000506
Jihoon Kang059b9492023-12-29 00:40:34 +0000507 createMergedPublicExportableStubs(ctx, bootclasspath)
508 createMergedSystemExportableStubs(ctx, bootclasspath)
509 createMergedTestExportableStubsForNonUpdatableModules(ctx)
510 createMergedFrameworkModuleLibExportableStubs(ctx, bootclasspath)
Paul Duffinfb5e07d2024-05-02 14:51:41 +0100511 createMergedFrameworkSystemServerExportableStubs(ctx, bootclasspath, system_server_classpath)
Jihoon Kang059b9492023-12-29 00:40:34 +0000512
Cole Faustdcda3702022-10-04 14:46:35 -0700513 createMergedAnnotationsFilegroups(ctx, bootclasspath, system_server_classpath)
Anton Hanssoncc18e032022-01-12 14:45:22 +0000514
Anton Hansson4468d7c2022-01-14 12:10:01 +0000515 createPublicStubsSourceFilegroup(ctx, bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100516}
517
518func combinedApisModuleFactory() android.Module {
519 module := &CombinedApis{}
520 module.AddProperties(&module.properties)
Cole Faustfbc4f882024-10-07 16:35:00 -0700521 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100522 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) })
523 return module
524}
Anton Hanssonfd316452022-01-14 11:15:52 +0000525
526// Various utility methods below.
527
528// Creates an array of ":<m><tag>" for each m in <modules>.
Cole Faustb1fc07e2024-09-05 11:37:57 -0700529func createSrcs(modules proptools.Configurable[[]string], tag string) proptools.Configurable[[]string] {
530 result := modules.Clone()
531 transformConfigurableArray(result, ":", tag)
532 return result
Anton Hanssonfd316452022-01-14 11:15:52 +0000533}
534
535// Creates an array of "<prefix><m><suffix>", for each m in <modules>.
536func transformArray(modules []string, prefix, suffix string) []string {
537 a := make([]string, 0, len(modules))
538 for _, module := range modules {
539 a = append(a, prefix+module+suffix)
540 }
541 return a
542}
543
Cole Faustb1fc07e2024-09-05 11:37:57 -0700544// Creates an array of "<prefix><m><suffix>", for each m in <modules>.
545func transformConfigurableArray(modules proptools.Configurable[[]string], prefix, suffix string) {
546 modules.AddPostProcessor(func(s []string) []string {
547 return transformArray(s, prefix, suffix)
548 })
549}
550
551func removeAll(s proptools.Configurable[[]string], vs []string) {
552 s.AddPostProcessor(func(s []string) []string {
553 a := make([]string, 0, len(s))
554 for _, module := range s {
555 if !slices.Contains(vs, module) {
556 a = append(a, module)
557 }
558 }
559 return a
560 })
Anton Hanssonfd316452022-01-14 11:15:52 +0000561}
562
563func remove(s []string, v string) []string {
564 s2 := make([]string, 0, len(s))
565 for _, sv := range s {
566 if sv != v {
567 s2 = append(s2, sv)
568 }
569 }
570 return s2
571}