blob: b31a26c90789451d35a0df0c584d202107caee95 [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 Kangdaa35302024-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 Kangdaa35302024-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 Kangdaa35302024-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 Kangdaa35302024-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 Kangdaa35302024-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 Kangdaa35302024-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 Kangdaa35302024-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 Kangdaa35302024-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 Kangdaa35302024-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 Kangdaa35302024-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 Kangdaa35302024-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
Anton Hansson4468d7c2022-01-14 12:10:01 +0000389func createPublicStubsSourceFilegroup(ctx android.LoadHookContext, modules []string) {
390 props := fgProps{}
391 props.Name = proptools.StringPtr("all-modules-public-stubs-source")
392 props.Srcs = createSrcs(modules, "{.public.stubs.source}")
393 props.Visibility = []string{"//frameworks/base"}
Colin Crossc6420762023-12-07 12:38:40 -0800394 ctx.CreateModule(android.FileGroupFactory, &props)
Anton Hansson4468d7c2022-01-14 12:10:01 +0000395}
396
Jihoon Kang31cf2742024-02-07 19:52:19 +0000397func createMergedTxts(ctx android.LoadHookContext, bootclasspath, system_server_classpath []string, baseTxtModulePrefix, stubsTypeSuffix string, doDist bool) {
Anton Hansson0860aaf2021-10-08 16:48:03 +0100398 var textFiles []MergedTxtDefinition
Anton Hansson16ff3572022-01-11 18:36:35 +0000399
Anton Hansson0860aaf2021-10-08 16:48:03 +0100400 tagSuffix := []string{".api.txt}", ".removed-api.txt}"}
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100401 distFilename := []string{"android.txt", "android-removed.txt"}
Anton Hansson0860aaf2021-10-08 16:48:03 +0100402 for i, f := range []string{"current.txt", "removed.txt"} {
403 textFiles = append(textFiles, MergedTxtDefinition{
Colin Crossc6420762023-12-07 12:38:40 -0800404 TxtFilename: f,
405 DistFilename: distFilename[i],
Jihoon Kang31cf2742024-02-07 19:52:19 +0000406 BaseTxt: ":" + baseTxtModulePrefix + f,
Colin Crossc6420762023-12-07 12:38:40 -0800407 Modules: bootclasspath,
408 ModuleTag: "{.public" + tagSuffix[i],
409 Scope: "public",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100410 })
411 textFiles = append(textFiles, MergedTxtDefinition{
Colin Crossc6420762023-12-07 12:38:40 -0800412 TxtFilename: f,
413 DistFilename: distFilename[i],
Jihoon Kang31cf2742024-02-07 19:52:19 +0000414 BaseTxt: ":" + baseTxtModulePrefix + "system-" + f,
Colin Crossc6420762023-12-07 12:38:40 -0800415 Modules: bootclasspath,
416 ModuleTag: "{.system" + tagSuffix[i],
417 Scope: "system",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100418 })
419 textFiles = append(textFiles, MergedTxtDefinition{
Colin Crossc6420762023-12-07 12:38:40 -0800420 TxtFilename: f,
421 DistFilename: distFilename[i],
Jihoon Kang31cf2742024-02-07 19:52:19 +0000422 BaseTxt: ":" + baseTxtModulePrefix + "module-lib-" + f,
Colin Crossc6420762023-12-07 12:38:40 -0800423 Modules: bootclasspath,
424 ModuleTag: "{.module-lib" + tagSuffix[i],
425 Scope: "module-lib",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100426 })
427 textFiles = append(textFiles, MergedTxtDefinition{
Colin Crossc6420762023-12-07 12:38:40 -0800428 TxtFilename: f,
429 DistFilename: distFilename[i],
Jihoon Kang31cf2742024-02-07 19:52:19 +0000430 BaseTxt: ":" + baseTxtModulePrefix + "system-server-" + f,
Colin Crossc6420762023-12-07 12:38:40 -0800431 Modules: system_server_classpath,
432 ModuleTag: "{.system-server" + tagSuffix[i],
433 Scope: "system-server",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100434 })
435 }
436 for _, txt := range textFiles {
Jihoon Kang31cf2742024-02-07 19:52:19 +0000437 createMergedTxt(ctx, txt, stubsTypeSuffix, doDist)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100438 }
439}
440
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000441func createApiContributionDefaults(ctx android.LoadHookContext, modules []string) {
442 defaultsSdkKinds := []android.SdkKind{
443 android.SdkPublic, android.SdkSystem, android.SdkModule,
444 }
445 for _, sdkKind := range defaultsSdkKinds {
446 props := defaultsProps{}
447 props.Name = proptools.StringPtr(
448 sdkKind.DefaultJavaLibraryName() + "_contributions")
449 if sdkKind == android.SdkModule {
450 props.Name = proptools.StringPtr(
451 sdkKind.DefaultJavaLibraryName() + "_contributions_full")
452 }
453 props.Api_surface = proptools.StringPtr(sdkKind.String())
454 apiSuffix := ""
455 if sdkKind != android.SdkPublic {
456 apiSuffix = "." + strings.ReplaceAll(sdkKind.String(), "-", "_")
457 }
458 props.Api_contributions = transformArray(
459 modules, "", fmt.Sprintf(".stubs.source%s.api.contribution", apiSuffix))
460 props.Defaults_visibility = []string{"//visibility:public"}
Jihoon Kang471a05b2023-08-01 06:37:17 +0000461 props.Previous_api = proptools.StringPtr(":android.api.public.latest")
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000462 ctx.CreateModule(java.DefaultsFactory, &props)
463 }
464}
465
Jihoon Kang1453baa2023-05-27 05:32:30 +0000466func createFullApiLibraries(ctx android.LoadHookContext) {
467 javaLibraryNames := []string{
468 "android_stubs_current",
469 "android_system_stubs_current",
470 "android_test_stubs_current",
Mark Whitee35b1382023-08-12 01:31:26 +0000471 "android_test_frameworks_core_stubs_current",
Jihoon Kang1453baa2023-05-27 05:32:30 +0000472 "android_module_lib_stubs_current",
473 "android_system_server_stubs_current",
474 }
475
476 for _, libraryName := range javaLibraryNames {
477 props := libraryProps{}
478 props.Name = proptools.StringPtr(libraryName)
479 staticLib := libraryName + ".from-source"
480 if ctx.Config().BuildFromTextStub() {
481 staticLib = libraryName + ".from-text"
482 }
483 props.Static_libs = []string{staticLib}
484 props.Defaults = []string{"android.jar_defaults"}
485 props.Visibility = []string{"//visibility:public"}
Jihoon Kangdaa35302024-02-12 23:18:52 +0000486 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang1453baa2023-05-27 05:32:30 +0000487
488 ctx.CreateModule(java.LibraryFactory, &props)
489 }
490}
491
Jihoon Kang059b9492023-12-29 00:40:34 +0000492func createFullExportableApiLibraries(ctx android.LoadHookContext) {
493 javaLibraryNames := []string{
494 "android_stubs_current_exportable",
495 "android_system_stubs_current_exportable",
496 "android_test_stubs_current_exportable",
497 "android_module_lib_stubs_current_exportable",
498 "android_system_server_stubs_current_exportable",
499 }
500
501 for _, libraryName := range javaLibraryNames {
502 props := libraryProps{}
503 props.Name = proptools.StringPtr(libraryName)
504 staticLib := libraryName + ".from-source"
505 props.Static_libs = []string{staticLib}
506 props.Defaults = []string{"android.jar_defaults"}
507 props.Visibility = []string{"//visibility:public"}
Jihoon Kangdaa35302024-02-12 23:18:52 +0000508 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang059b9492023-12-29 00:40:34 +0000509
510 ctx.CreateModule(java.LibraryFactory, &props)
511 }
512}
513
Anton Hansson0860aaf2021-10-08 16:48:03 +0100514func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) {
Anton Hansson07a12952022-01-12 17:28:39 +0000515 bootclasspath := a.properties.Bootclasspath
Cole Faustdcda3702022-10-04 14:46:35 -0700516 system_server_classpath := a.properties.System_server_classpath
Anton Hansson07a12952022-01-12 17:28:39 +0000517 if ctx.Config().VendorConfig("ANDROID").Bool("include_nonpublic_framework_api") {
518 bootclasspath = append(bootclasspath, a.properties.Conditional_bootclasspath...)
519 sort.Strings(bootclasspath)
520 }
Jihoon Kang31cf2742024-02-07 19:52:19 +0000521 createMergedTxts(ctx, bootclasspath, system_server_classpath, "non-updatable-", "-", false)
522 createMergedTxts(ctx, bootclasspath, system_server_classpath, "non-updatable-exportable-", "-exportable-", true)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100523
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000524 createMergedPublicStubs(ctx, bootclasspath)
525 createMergedSystemStubs(ctx, bootclasspath)
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000526 createMergedTestStubsForNonUpdatableModules(ctx)
Anton Hansson95e89a82022-01-28 11:31:50 +0000527 createMergedFrameworkModuleLibStubs(ctx, bootclasspath)
528 createMergedFrameworkImpl(ctx, bootclasspath)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000529
Jihoon Kang059b9492023-12-29 00:40:34 +0000530 createMergedPublicExportableStubs(ctx, bootclasspath)
531 createMergedSystemExportableStubs(ctx, bootclasspath)
532 createMergedTestExportableStubsForNonUpdatableModules(ctx)
533 createMergedFrameworkModuleLibExportableStubs(ctx, bootclasspath)
534
Cole Faustdcda3702022-10-04 14:46:35 -0700535 createMergedAnnotationsFilegroups(ctx, bootclasspath, system_server_classpath)
Anton Hanssoncc18e032022-01-12 14:45:22 +0000536
Anton Hansson4468d7c2022-01-14 12:10:01 +0000537 createPublicStubsSourceFilegroup(ctx, bootclasspath)
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000538
539 createApiContributionDefaults(ctx, bootclasspath)
Jihoon Kang1453baa2023-05-27 05:32:30 +0000540
541 createFullApiLibraries(ctx)
Jihoon Kang059b9492023-12-29 00:40:34 +0000542
543 createFullExportableApiLibraries(ctx)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100544}
545
546func combinedApisModuleFactory() android.Module {
547 module := &CombinedApis{}
548 module.AddProperties(&module.properties)
549 android.InitAndroidModule(module)
Harshit Mahajanb52adbc2023-12-15 21:56:42 +0000550 android.InitDefaultableModule(module)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100551 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) })
552 return module
553}
Anton Hanssonfd316452022-01-14 11:15:52 +0000554
555// Various utility methods below.
556
557// Creates an array of ":<m><tag>" for each m in <modules>.
558func createSrcs(modules []string, tag string) []string {
559 return transformArray(modules, ":", tag)
560}
561
562// Creates an array of "<prefix><m><suffix>", for each m in <modules>.
563func transformArray(modules []string, prefix, suffix string) []string {
564 a := make([]string, 0, len(modules))
565 for _, module := range modules {
566 a = append(a, prefix+module+suffix)
567 }
568 return a
569}
570
571func removeAll(s []string, vs []string) []string {
572 for _, v := range vs {
573 s = remove(s, v)
574 }
575 return s
576}
577
578func remove(s []string, v string) []string {
579 s2 := make([]string, 0, len(s))
580 for _, sv := range s {
581 if sv != v {
582 s2 = append(s2, sv)
583 }
584 }
585 return s2
586}
Harshit Mahajanb52adbc2023-12-15 21:56:42 +0000587
588// Defaults
589type CombinedApisModuleDefaults struct {
590 android.ModuleBase
591 android.DefaultsModuleBase
592}
593
594func CombinedApisModuleDefaultsFactory() android.Module {
595 module := &CombinedApisModuleDefaults{}
596 module.AddProperties(&CombinedApisProperties{})
597 android.InitDefaultsModule(module)
598 return module
599}