blob: e8858230ba4ef956a59f9f6d14ca8e0268c2e3ea [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 {
134 Name *string
135 Sdk_version *string
136 Static_libs []string
137 Visibility []string
Jihoon Kang1453baa2023-05-27 05:32:30 +0000138 Defaults []string
Anton Hanssoncb00f942022-01-13 09:45:12 +0000139}
140
Anton Hansson4468d7c2022-01-14 12:10:01 +0000141type fgProps struct {
142 Name *string
143 Srcs []string
144 Visibility []string
145}
146
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000147type defaultsProps struct {
148 Name *string
149 Api_surface *string
150 Api_contributions []string
151 Defaults_visibility []string
Jihoon Kang471a05b2023-08-01 06:37:17 +0000152 Previous_api *string
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000153}
154
Anton Hansson0860aaf2021-10-08 16:48:03 +0100155// Struct to pass parameters for the various merged [current|removed].txt file modules we create.
156type MergedTxtDefinition struct {
157 // "current.txt" or "removed.txt"
158 TxtFilename string
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100159 // Filename in the new dist dir. "android.txt" or "android-removed.txt"
160 DistFilename string
Anton Hansson0860aaf2021-10-08 16:48:03 +0100161 // The module for the non-updatable / non-module part of the api.
162 BaseTxt string
163 // The list of modules that are relevant for this merged txt.
164 Modules []string
165 // The output tag for each module to use.e.g. {.public.api.txt} for current.txt
166 ModuleTag string
167 // public, system, module-lib or system-server
168 Scope string
169}
170
Jihoon Kang31cf2742024-02-07 19:52:19 +0000171func createMergedTxt(ctx android.LoadHookContext, txt MergedTxtDefinition, stubsTypeSuffix string, doDist bool) {
Anton Hansson0860aaf2021-10-08 16:48:03 +0100172 metalavaCmd := "$(location metalava)"
173 // Silence reflection warnings. See b/168689341
174 metalavaCmd += " -J--add-opens=java.base/java.util=ALL-UNNAMED "
Paul Duffinf3b1fc42023-08-14 22:03:06 +0100175 metalavaCmd += " --quiet merge-signatures --format=v2 "
Anton Hansson0860aaf2021-10-08 16:48:03 +0100176
177 filename := txt.TxtFilename
178 if txt.Scope != "public" {
179 filename = txt.Scope + "-" + filename
180 }
Jihoon Kang31cf2742024-02-07 19:52:19 +0000181 moduleName := ctx.ModuleName() + stubsTypeSuffix + filename
Chris Parsons3b7e34b2023-09-27 22:34:57 +0000182
Anton Hansson0860aaf2021-10-08 16:48:03 +0100183 props := genruleProps{}
Chris Parsons3b7e34b2023-09-27 22:34:57 +0000184 props.Name = proptools.StringPtr(moduleName)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100185 props.Tools = []string{"metalava"}
Anton Hansson16ff3572022-01-11 18:36:35 +0000186 props.Out = []string{filename}
Paul Duffinf3b1fc42023-08-14 22:03:06 +0100187 props.Cmd = proptools.StringPtr(metalavaCmd + "$(in) --out $(out)")
Anton Hanssonfd316452022-01-14 11:15:52 +0000188 props.Srcs = append([]string{txt.BaseTxt}, createSrcs(txt.Modules, txt.ModuleTag)...)
Jihoon Kang31cf2742024-02-07 19:52:19 +0000189 if doDist {
190 props.Dists = []android.Dist{
191 {
192 Targets: []string{"droidcore"},
193 Dir: proptools.StringPtr("api"),
194 Dest: proptools.StringPtr(filename),
195 },
196 {
197 Targets: []string{"api_txt", "sdk"},
198 Dir: proptools.StringPtr("apistubs/android/" + txt.Scope + "/api"),
199 Dest: proptools.StringPtr(txt.DistFilename),
200 },
201 }
Anton Hansson0860aaf2021-10-08 16:48:03 +0100202 }
203 props.Visibility = []string{"//visibility:public"}
Colin Crossc6420762023-12-07 12:38:40 -0800204 ctx.CreateModule(genrule.GenRuleFactory, &props)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100205}
206
Cole Faustdcda3702022-10-04 14:46:35 -0700207func createMergedAnnotationsFilegroups(ctx android.LoadHookContext, modules, system_server_modules []string) {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000208 for _, i := range []struct {
Cole Faustdcda3702022-10-04 14:46:35 -0700209 name string
210 tag string
211 modules []string
212 }{
213 {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000214 name: "all-modules-public-annotations",
215 tag: "{.public.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700216 modules: modules,
217 }, {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000218 name: "all-modules-system-annotations",
219 tag: "{.system.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700220 modules: modules,
221 }, {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000222 name: "all-modules-module-lib-annotations",
223 tag: "{.module-lib.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700224 modules: modules,
225 }, {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000226 name: "all-modules-system-server-annotations",
227 tag: "{.system-server.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700228 modules: system_server_modules,
229 },
230 } {
231 props := fgProps{}
232 props.Name = proptools.StringPtr(i.name)
233 props.Srcs = createSrcs(i.modules, i.tag)
Colin Crossc6420762023-12-07 12:38:40 -0800234 ctx.CreateModule(android.FileGroupFactory, &props)
Cole Faustdcda3702022-10-04 14:46:35 -0700235 }
Anton Hansson74b15642022-06-23 08:27:23 +0000236}
237
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000238func createMergedPublicStubs(ctx android.LoadHookContext, modules []string) {
239 props := libraryProps{}
240 props.Name = proptools.StringPtr("all-modules-public-stubs")
241 props.Static_libs = transformArray(modules, "", ".stubs")
242 props.Sdk_version = proptools.StringPtr("module_current")
243 props.Visibility = []string{"//frameworks/base"}
244 ctx.CreateModule(java.LibraryFactory, &props)
245}
246
Jihoon Kang059b9492023-12-29 00:40:34 +0000247func createMergedPublicExportableStubs(ctx android.LoadHookContext, modules []string) {
248 props := libraryProps{}
249 props.Name = proptools.StringPtr("all-modules-public-stubs-exportable")
250 props.Static_libs = transformArray(modules, "", ".stubs.exportable")
251 props.Sdk_version = proptools.StringPtr("module_current")
252 props.Visibility = []string{"//frameworks/base"}
253 ctx.CreateModule(java.LibraryFactory, &props)
254}
255
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000256func createMergedSystemStubs(ctx android.LoadHookContext, modules []string) {
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000257 // First create the all-updatable-modules-system-stubs
258 {
259 updatable_modules := removeAll(modules, non_updatable_modules)
260 props := libraryProps{}
261 props.Name = proptools.StringPtr("all-updatable-modules-system-stubs")
262 props.Static_libs = transformArray(updatable_modules, "", ".stubs.system")
263 props.Sdk_version = proptools.StringPtr("module_current")
264 props.Visibility = []string{"//frameworks/base"}
265 ctx.CreateModule(java.LibraryFactory, &props)
266 }
267 // Now merge all-updatable-modules-system-stubs and stubs from non-updatable modules
268 // into all-modules-system-stubs.
269 {
270 props := libraryProps{}
271 props.Name = proptools.StringPtr("all-modules-system-stubs")
272 props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.system")
273 props.Static_libs = append(props.Static_libs, "all-updatable-modules-system-stubs")
274 props.Sdk_version = proptools.StringPtr("module_current")
275 props.Visibility = []string{"//frameworks/base"}
276 ctx.CreateModule(java.LibraryFactory, &props)
277 }
278}
279
Jihoon Kang059b9492023-12-29 00:40:34 +0000280func createMergedSystemExportableStubs(ctx android.LoadHookContext, modules []string) {
281 // First create the all-updatable-modules-system-stubs
282 {
283 updatable_modules := removeAll(modules, non_updatable_modules)
284 props := libraryProps{}
285 props.Name = proptools.StringPtr("all-updatable-modules-system-stubs-exportable")
286 props.Static_libs = transformArray(updatable_modules, "", ".stubs.exportable.system")
287 props.Sdk_version = proptools.StringPtr("module_current")
288 props.Visibility = []string{"//frameworks/base"}
289 ctx.CreateModule(java.LibraryFactory, &props)
290 }
291 // Now merge all-updatable-modules-system-stubs and stubs from non-updatable modules
292 // into all-modules-system-stubs.
293 {
294 props := libraryProps{}
295 props.Name = proptools.StringPtr("all-modules-system-stubs-exportable")
296 props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.exportable.system")
297 props.Static_libs = append(props.Static_libs, "all-updatable-modules-system-stubs-exportable")
298 props.Sdk_version = proptools.StringPtr("module_current")
299 props.Visibility = []string{"//frameworks/base"}
300 ctx.CreateModule(java.LibraryFactory, &props)
301 }
302}
303
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000304func createMergedTestStubsForNonUpdatableModules(ctx android.LoadHookContext) {
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000305 props := libraryProps{}
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000306 props.Name = proptools.StringPtr("all-non-updatable-modules-test-stubs")
307 props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.test")
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000308 props.Sdk_version = proptools.StringPtr("module_current")
309 props.Visibility = []string{"//frameworks/base"}
310 ctx.CreateModule(java.LibraryFactory, &props)
311}
312
Jihoon Kang059b9492023-12-29 00:40:34 +0000313func createMergedTestExportableStubsForNonUpdatableModules(ctx android.LoadHookContext) {
314 props := libraryProps{}
315 props.Name = proptools.StringPtr("all-non-updatable-modules-test-stubs-exportable")
316 props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.exportable.test")
317 props.Sdk_version = proptools.StringPtr("module_current")
318 props.Visibility = []string{"//frameworks/base"}
319 ctx.CreateModule(java.LibraryFactory, &props)
320}
321
Anton Hansson95e89a82022-01-28 11:31:50 +0000322func createMergedFrameworkImpl(ctx android.LoadHookContext, modules []string) {
323 // This module is for the "framework-all" module, which should not include the core libraries.
324 modules = removeAll(modules, core_libraries_modules)
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +0000325 // Remove the modules that belong to non-updatable APEXes since those are allowed to compile
326 // against unstable APIs.
327 modules = removeAll(modules, non_updatable_modules)
328 // First create updatable-framework-module-impl, which contains all updatable modules.
329 // This module compiles against module_lib SDK.
330 {
331 props := libraryProps{}
332 props.Name = proptools.StringPtr("updatable-framework-module-impl")
333 props.Static_libs = transformArray(modules, "", ".impl")
334 props.Sdk_version = proptools.StringPtr("module_current")
335 props.Visibility = []string{"//frameworks/base"}
336 ctx.CreateModule(java.LibraryFactory, &props)
337 }
338
339 // Now create all-framework-module-impl, which contains updatable-framework-module-impl
340 // and all non-updatable modules. This module compiles against hidden APIs.
341 {
342 props := libraryProps{}
343 props.Name = proptools.StringPtr("all-framework-module-impl")
344 props.Static_libs = transformArray(non_updatable_modules, "", ".impl")
345 props.Static_libs = append(props.Static_libs, "updatable-framework-module-impl")
346 props.Sdk_version = proptools.StringPtr("core_platform")
347 props.Visibility = []string{"//frameworks/base"}
348 ctx.CreateModule(java.LibraryFactory, &props)
349 }
Anton Hansson95e89a82022-01-28 11:31:50 +0000350}
351
Jihoon Kang059b9492023-12-29 00:40:34 +0000352func createMergedFrameworkModuleLibExportableStubs(ctx android.LoadHookContext, modules []string) {
353 // The user of this module compiles against the "core" SDK and against non-updatable modules,
354 // so remove to avoid dupes.
355 modules = removeAll(modules, core_libraries_modules)
356 modules = removeAll(modules, non_updatable_modules)
357 props := libraryProps{}
358 props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api-exportable")
359 props.Static_libs = transformArray(modules, "", ".stubs.exportable.module_lib")
360 props.Sdk_version = proptools.StringPtr("module_current")
361 props.Visibility = []string{"//frameworks/base"}
362 ctx.CreateModule(java.LibraryFactory, &props)
363}
364
Anton Hansson95e89a82022-01-28 11:31:50 +0000365func createMergedFrameworkModuleLibStubs(ctx android.LoadHookContext, modules []string) {
Mark White3cc5e002023-08-07 11:18:09 +0000366 // The user of this module compiles against the "core" SDK and against non-updatable modules,
367 // so remove to avoid dupes.
Anton Hansson95e89a82022-01-28 11:31:50 +0000368 modules = removeAll(modules, core_libraries_modules)
Mark White3cc5e002023-08-07 11:18:09 +0000369 modules = removeAll(modules, non_updatable_modules)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000370 props := libraryProps{}
371 props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api")
Anton Hanssonfd316452022-01-14 11:15:52 +0000372 props.Static_libs = transformArray(modules, "", ".stubs.module_lib")
Anton Hanssoncb00f942022-01-13 09:45:12 +0000373 props.Sdk_version = proptools.StringPtr("module_current")
374 props.Visibility = []string{"//frameworks/base"}
375 ctx.CreateModule(java.LibraryFactory, &props)
376}
377
Anton Hansson4468d7c2022-01-14 12:10:01 +0000378func createPublicStubsSourceFilegroup(ctx android.LoadHookContext, modules []string) {
379 props := fgProps{}
380 props.Name = proptools.StringPtr("all-modules-public-stubs-source")
381 props.Srcs = createSrcs(modules, "{.public.stubs.source}")
382 props.Visibility = []string{"//frameworks/base"}
Colin Crossc6420762023-12-07 12:38:40 -0800383 ctx.CreateModule(android.FileGroupFactory, &props)
Anton Hansson4468d7c2022-01-14 12:10:01 +0000384}
385
Jihoon Kang31cf2742024-02-07 19:52:19 +0000386func createMergedTxts(ctx android.LoadHookContext, bootclasspath, system_server_classpath []string, baseTxtModulePrefix, stubsTypeSuffix string, doDist bool) {
Anton Hansson0860aaf2021-10-08 16:48:03 +0100387 var textFiles []MergedTxtDefinition
Anton Hansson16ff3572022-01-11 18:36:35 +0000388
Anton Hansson0860aaf2021-10-08 16:48:03 +0100389 tagSuffix := []string{".api.txt}", ".removed-api.txt}"}
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100390 distFilename := []string{"android.txt", "android-removed.txt"}
Anton Hansson0860aaf2021-10-08 16:48:03 +0100391 for i, f := range []string{"current.txt", "removed.txt"} {
392 textFiles = append(textFiles, MergedTxtDefinition{
Colin Crossc6420762023-12-07 12:38:40 -0800393 TxtFilename: f,
394 DistFilename: distFilename[i],
Jihoon Kang31cf2742024-02-07 19:52:19 +0000395 BaseTxt: ":" + baseTxtModulePrefix + f,
Colin Crossc6420762023-12-07 12:38:40 -0800396 Modules: bootclasspath,
397 ModuleTag: "{.public" + tagSuffix[i],
398 Scope: "public",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100399 })
400 textFiles = append(textFiles, MergedTxtDefinition{
Colin Crossc6420762023-12-07 12:38:40 -0800401 TxtFilename: f,
402 DistFilename: distFilename[i],
Jihoon Kang31cf2742024-02-07 19:52:19 +0000403 BaseTxt: ":" + baseTxtModulePrefix + "system-" + f,
Colin Crossc6420762023-12-07 12:38:40 -0800404 Modules: bootclasspath,
405 ModuleTag: "{.system" + tagSuffix[i],
406 Scope: "system",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100407 })
408 textFiles = append(textFiles, MergedTxtDefinition{
Colin Crossc6420762023-12-07 12:38:40 -0800409 TxtFilename: f,
410 DistFilename: distFilename[i],
Jihoon Kang31cf2742024-02-07 19:52:19 +0000411 BaseTxt: ":" + baseTxtModulePrefix + "module-lib-" + f,
Colin Crossc6420762023-12-07 12:38:40 -0800412 Modules: bootclasspath,
413 ModuleTag: "{.module-lib" + tagSuffix[i],
414 Scope: "module-lib",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100415 })
416 textFiles = append(textFiles, MergedTxtDefinition{
Colin Crossc6420762023-12-07 12:38:40 -0800417 TxtFilename: f,
418 DistFilename: distFilename[i],
Jihoon Kang31cf2742024-02-07 19:52:19 +0000419 BaseTxt: ":" + baseTxtModulePrefix + "system-server-" + f,
Colin Crossc6420762023-12-07 12:38:40 -0800420 Modules: system_server_classpath,
421 ModuleTag: "{.system-server" + tagSuffix[i],
422 Scope: "system-server",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100423 })
424 }
425 for _, txt := range textFiles {
Jihoon Kang31cf2742024-02-07 19:52:19 +0000426 createMergedTxt(ctx, txt, stubsTypeSuffix, doDist)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100427 }
428}
429
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000430func createApiContributionDefaults(ctx android.LoadHookContext, modules []string) {
431 defaultsSdkKinds := []android.SdkKind{
432 android.SdkPublic, android.SdkSystem, android.SdkModule,
433 }
434 for _, sdkKind := range defaultsSdkKinds {
435 props := defaultsProps{}
436 props.Name = proptools.StringPtr(
437 sdkKind.DefaultJavaLibraryName() + "_contributions")
438 if sdkKind == android.SdkModule {
439 props.Name = proptools.StringPtr(
440 sdkKind.DefaultJavaLibraryName() + "_contributions_full")
441 }
442 props.Api_surface = proptools.StringPtr(sdkKind.String())
443 apiSuffix := ""
444 if sdkKind != android.SdkPublic {
445 apiSuffix = "." + strings.ReplaceAll(sdkKind.String(), "-", "_")
446 }
447 props.Api_contributions = transformArray(
448 modules, "", fmt.Sprintf(".stubs.source%s.api.contribution", apiSuffix))
449 props.Defaults_visibility = []string{"//visibility:public"}
Jihoon Kang471a05b2023-08-01 06:37:17 +0000450 props.Previous_api = proptools.StringPtr(":android.api.public.latest")
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000451 ctx.CreateModule(java.DefaultsFactory, &props)
452 }
453}
454
Jihoon Kang1453baa2023-05-27 05:32:30 +0000455func createFullApiLibraries(ctx android.LoadHookContext) {
456 javaLibraryNames := []string{
457 "android_stubs_current",
458 "android_system_stubs_current",
459 "android_test_stubs_current",
Mark Whitee35b1382023-08-12 01:31:26 +0000460 "android_test_frameworks_core_stubs_current",
Jihoon Kang1453baa2023-05-27 05:32:30 +0000461 "android_module_lib_stubs_current",
462 "android_system_server_stubs_current",
463 }
464
465 for _, libraryName := range javaLibraryNames {
466 props := libraryProps{}
467 props.Name = proptools.StringPtr(libraryName)
468 staticLib := libraryName + ".from-source"
469 if ctx.Config().BuildFromTextStub() {
470 staticLib = libraryName + ".from-text"
471 }
472 props.Static_libs = []string{staticLib}
473 props.Defaults = []string{"android.jar_defaults"}
474 props.Visibility = []string{"//visibility:public"}
475
476 ctx.CreateModule(java.LibraryFactory, &props)
477 }
478}
479
Jihoon Kang059b9492023-12-29 00:40:34 +0000480func createFullExportableApiLibraries(ctx android.LoadHookContext) {
481 javaLibraryNames := []string{
482 "android_stubs_current_exportable",
483 "android_system_stubs_current_exportable",
484 "android_test_stubs_current_exportable",
485 "android_module_lib_stubs_current_exportable",
486 "android_system_server_stubs_current_exportable",
487 }
488
489 for _, libraryName := range javaLibraryNames {
490 props := libraryProps{}
491 props.Name = proptools.StringPtr(libraryName)
492 staticLib := libraryName + ".from-source"
493 props.Static_libs = []string{staticLib}
494 props.Defaults = []string{"android.jar_defaults"}
495 props.Visibility = []string{"//visibility:public"}
496
497 ctx.CreateModule(java.LibraryFactory, &props)
498 }
499}
500
Anton Hansson0860aaf2021-10-08 16:48:03 +0100501func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) {
Anton Hansson07a12952022-01-12 17:28:39 +0000502 bootclasspath := a.properties.Bootclasspath
Cole Faustdcda3702022-10-04 14:46:35 -0700503 system_server_classpath := a.properties.System_server_classpath
Anton Hansson07a12952022-01-12 17:28:39 +0000504 if ctx.Config().VendorConfig("ANDROID").Bool("include_nonpublic_framework_api") {
505 bootclasspath = append(bootclasspath, a.properties.Conditional_bootclasspath...)
506 sort.Strings(bootclasspath)
507 }
Jihoon Kang31cf2742024-02-07 19:52:19 +0000508 createMergedTxts(ctx, bootclasspath, system_server_classpath, "non-updatable-", "-", false)
509 createMergedTxts(ctx, bootclasspath, system_server_classpath, "non-updatable-exportable-", "-exportable-", true)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100510
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000511 createMergedPublicStubs(ctx, bootclasspath)
512 createMergedSystemStubs(ctx, bootclasspath)
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000513 createMergedTestStubsForNonUpdatableModules(ctx)
Anton Hansson95e89a82022-01-28 11:31:50 +0000514 createMergedFrameworkModuleLibStubs(ctx, bootclasspath)
515 createMergedFrameworkImpl(ctx, bootclasspath)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000516
Jihoon Kang059b9492023-12-29 00:40:34 +0000517 createMergedPublicExportableStubs(ctx, bootclasspath)
518 createMergedSystemExportableStubs(ctx, bootclasspath)
519 createMergedTestExportableStubsForNonUpdatableModules(ctx)
520 createMergedFrameworkModuleLibExportableStubs(ctx, bootclasspath)
521
Cole Faustdcda3702022-10-04 14:46:35 -0700522 createMergedAnnotationsFilegroups(ctx, bootclasspath, system_server_classpath)
Anton Hanssoncc18e032022-01-12 14:45:22 +0000523
Anton Hansson4468d7c2022-01-14 12:10:01 +0000524 createPublicStubsSourceFilegroup(ctx, bootclasspath)
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000525
526 createApiContributionDefaults(ctx, bootclasspath)
Jihoon Kang1453baa2023-05-27 05:32:30 +0000527
528 createFullApiLibraries(ctx)
Jihoon Kang059b9492023-12-29 00:40:34 +0000529
530 createFullExportableApiLibraries(ctx)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100531}
532
533func combinedApisModuleFactory() android.Module {
534 module := &CombinedApis{}
535 module.AddProperties(&module.properties)
536 android.InitAndroidModule(module)
Harshit Mahajanb52adbc2023-12-15 21:56:42 +0000537 android.InitDefaultableModule(module)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100538 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) })
539 return module
540}
Anton Hanssonfd316452022-01-14 11:15:52 +0000541
542// Various utility methods below.
543
544// Creates an array of ":<m><tag>" for each m in <modules>.
545func createSrcs(modules []string, tag string) []string {
546 return transformArray(modules, ":", tag)
547}
548
549// Creates an array of "<prefix><m><suffix>", for each m in <modules>.
550func transformArray(modules []string, prefix, suffix string) []string {
551 a := make([]string, 0, len(modules))
552 for _, module := range modules {
553 a = append(a, prefix+module+suffix)
554 }
555 return a
556}
557
558func removeAll(s []string, vs []string) []string {
559 for _, v := range vs {
560 s = remove(s, v)
561 }
562 return s
563}
564
565func remove(s []string, v string) []string {
566 s2 := make([]string, 0, len(s))
567 for _, sv := range s {
568 if sv != v {
569 s2 = append(s2, sv)
570 }
571 }
572 return s2
573}
Harshit Mahajanb52adbc2023-12-15 21:56:42 +0000574
575// Defaults
576type CombinedApisModuleDefaults struct {
577 android.ModuleBase
578 android.DefaultsModuleBase
579}
580
581func CombinedApisModuleDefaultsFactory() android.Module {
582 module := &CombinedApisModuleDefaults{}
583 module.AddProperties(&CombinedApisProperties{})
584 android.InitDefaultsModule(module)
585 return module
586}