blob: 6095a9a781d83bd8f5444d67330d44a549e2dc50 [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"
Zi Wang0d6a5302023-02-16 14:54:01 -080025 "android/soong/bazel"
Anton Hansson0860aaf2021-10-08 16:48:03 +010026 "android/soong/genrule"
Anton Hanssoncb00f942022-01-13 09:45:12 +000027 "android/soong/java"
Anton Hansson0860aaf2021-10-08 16:48:03 +010028)
29
Anton Hansson05e944d2022-01-13 12:26:30 +000030const art = "art.module.public.api"
31const conscrypt = "conscrypt.module.public.api"
32const i18n = "i18n.module.public.api"
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +000033const virtualization = "framework-virtualization"
Mark White9ee18662023-08-07 11:18:09 +000034const location = "framework-location"
Anton Hansson09a9c4e2022-04-08 10:59:46 +010035
Anton Hansson95e89a82022-01-28 11:31:50 +000036var core_libraries_modules = []string{art, conscrypt, i18n}
Zi Wang0d6a5302023-02-16 14:54:01 -080037
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +000038// List of modules that are not yet updatable, and hence they can still compile
39// against hidden APIs. These modules are filtered out when building the
40// updatable-framework-module-impl (because updatable-framework-module-impl is
41// built against module_current SDK). Instead they are directly statically
42// linked into the all-framework-module-lib, which is building against hidden
43// APIs.
Nikita Ioffe5593fbb2022-12-01 14:52:34 +000044// In addition, the modules in this list are allowed to contribute to test APIs
45// stubs.
Mark White9ee18662023-08-07 11:18:09 +000046var non_updatable_modules = []string{virtualization, location}
Anton Hansson05e944d2022-01-13 12:26:30 +000047
Anton Hansson0860aaf2021-10-08 16:48:03 +010048// The intention behind this soong plugin is to generate a number of "merged"
49// API-related modules that would otherwise require a large amount of very
50// similar Android.bp boilerplate to define. For example, the merged current.txt
51// API definitions (created by merging the non-updatable current.txt with all
52// the module current.txts). This simplifies the addition of new android
53// modules, by reducing the number of genrules etc a new module must be added to.
54
55// The properties of the combined_apis module type.
56type CombinedApisProperties struct {
Anton Hansson16ff3572022-01-11 18:36:35 +000057 // Module libraries in the bootclasspath
58 Bootclasspath []string
Anton Hansson07a12952022-01-12 17:28:39 +000059 // Module libraries on the bootclasspath if include_nonpublic_framework_api is true.
60 Conditional_bootclasspath []string
Anton Hansson16ff3572022-01-11 18:36:35 +000061 // Module libraries in system server
62 System_server_classpath []string
Anton Hansson0860aaf2021-10-08 16:48:03 +010063}
64
65type CombinedApis struct {
66 android.ModuleBase
Zi Wang0d6a5302023-02-16 14:54:01 -080067 android.BazelModuleBase
Anton Hansson0860aaf2021-10-08 16:48:03 +010068
69 properties CombinedApisProperties
70}
71
72func init() {
73 registerBuildComponents(android.InitRegistrationContext)
74}
75
76func registerBuildComponents(ctx android.RegistrationContext) {
77 ctx.RegisterModuleType("combined_apis", combinedApisModuleFactory)
78}
79
80var PrepareForCombinedApisTest = android.FixtureRegisterWithContext(registerBuildComponents)
81
82func (a *CombinedApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
83}
84
85type genruleProps struct {
86 Name *string
87 Cmd *string
88 Dists []android.Dist
89 Out []string
90 Srcs []string
91 Tools []string
92 Visibility []string
93}
94
Anton Hanssoncb00f942022-01-13 09:45:12 +000095type libraryProps struct {
96 Name *string
97 Sdk_version *string
98 Static_libs []string
99 Visibility []string
Jihoon Kang1453baa2023-05-27 05:32:30 +0000100 Defaults []string
Anton Hanssoncb00f942022-01-13 09:45:12 +0000101}
102
Anton Hansson4468d7c2022-01-14 12:10:01 +0000103type fgProps struct {
104 Name *string
105 Srcs []string
106 Visibility []string
107}
108
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000109type defaultsProps struct {
110 Name *string
111 Api_surface *string
112 Api_contributions []string
113 Defaults_visibility []string
Jihoon Kang471a05b2023-08-01 06:37:17 +0000114 Previous_api *string
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000115}
116
Zi Wang0d6a5302023-02-16 14:54:01 -0800117type Bazel_module struct {
118 Bp2build_available *bool
119}
120type bazelProperties struct {
121 *Bazel_module
122}
123
124var bp2buildNotAvailable = bazelProperties{
125 &Bazel_module{
126 Bp2build_available: proptools.BoolPtr(false),
127 },
128}
129
Anton Hansson0860aaf2021-10-08 16:48:03 +0100130// Struct to pass parameters for the various merged [current|removed].txt file modules we create.
131type MergedTxtDefinition struct {
132 // "current.txt" or "removed.txt"
133 TxtFilename string
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100134 // Filename in the new dist dir. "android.txt" or "android-removed.txt"
135 DistFilename string
Anton Hansson0860aaf2021-10-08 16:48:03 +0100136 // The module for the non-updatable / non-module part of the api.
137 BaseTxt string
138 // The list of modules that are relevant for this merged txt.
139 Modules []string
140 // The output tag for each module to use.e.g. {.public.api.txt} for current.txt
141 ModuleTag string
142 // public, system, module-lib or system-server
143 Scope string
144}
145
146func createMergedTxt(ctx android.LoadHookContext, txt MergedTxtDefinition) {
147 metalavaCmd := "$(location metalava)"
148 // Silence reflection warnings. See b/168689341
149 metalavaCmd += " -J--add-opens=java.base/java.util=ALL-UNNAMED "
Paul Duffinf3b1fc42023-08-14 22:03:06 +0100150 metalavaCmd += " --quiet merge-signatures --format=v2 "
Anton Hansson0860aaf2021-10-08 16:48:03 +0100151
152 filename := txt.TxtFilename
153 if txt.Scope != "public" {
154 filename = txt.Scope + "-" + filename
155 }
Anton Hansson0860aaf2021-10-08 16:48:03 +0100156 props := genruleProps{}
157 props.Name = proptools.StringPtr(ctx.ModuleName() + "-" + filename)
158 props.Tools = []string{"metalava"}
Anton Hansson16ff3572022-01-11 18:36:35 +0000159 props.Out = []string{filename}
Paul Duffinf3b1fc42023-08-14 22:03:06 +0100160 props.Cmd = proptools.StringPtr(metalavaCmd + "$(in) --out $(out)")
Anton Hanssonfd316452022-01-14 11:15:52 +0000161 props.Srcs = append([]string{txt.BaseTxt}, createSrcs(txt.Modules, txt.ModuleTag)...)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100162 props.Dists = []android.Dist{
163 {
164 Targets: []string{"droidcore"},
165 Dir: proptools.StringPtr("api"),
166 Dest: proptools.StringPtr(filename),
167 },
168 {
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100169 Targets: []string{"api_txt", "sdk"},
Anton Hansson0860aaf2021-10-08 16:48:03 +0100170 Dir: proptools.StringPtr("apistubs/android/" + txt.Scope + "/api"),
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100171 Dest: proptools.StringPtr(txt.DistFilename),
Anton Hansson0860aaf2021-10-08 16:48:03 +0100172 },
173 }
174 props.Visibility = []string{"//visibility:public"}
Zi Wang0d6a5302023-02-16 14:54:01 -0800175 ctx.CreateModule(genrule.GenRuleFactory, &props, &bp2buildNotAvailable)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100176}
177
Cole Faustdcda3702022-10-04 14:46:35 -0700178func createMergedAnnotationsFilegroups(ctx android.LoadHookContext, modules, system_server_modules []string) {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000179 for _, i := range []struct {
Cole Faustdcda3702022-10-04 14:46:35 -0700180 name string
181 tag string
182 modules []string
183 }{
184 {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000185 name: "all-modules-public-annotations",
186 tag: "{.public.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700187 modules: modules,
188 }, {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000189 name: "all-modules-system-annotations",
190 tag: "{.system.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700191 modules: modules,
192 }, {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000193 name: "all-modules-module-lib-annotations",
194 tag: "{.module-lib.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700195 modules: modules,
196 }, {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000197 name: "all-modules-system-server-annotations",
198 tag: "{.system-server.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700199 modules: system_server_modules,
200 },
201 } {
202 props := fgProps{}
203 props.Name = proptools.StringPtr(i.name)
204 props.Srcs = createSrcs(i.modules, i.tag)
Zi Wang0d6a5302023-02-16 14:54:01 -0800205 ctx.CreateModule(android.FileGroupFactory, &props, &bp2buildNotAvailable)
Cole Faustdcda3702022-10-04 14:46:35 -0700206 }
Anton Hansson74b15642022-06-23 08:27:23 +0000207}
208
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000209func createMergedPublicStubs(ctx android.LoadHookContext, modules []string) {
210 props := libraryProps{}
211 props.Name = proptools.StringPtr("all-modules-public-stubs")
212 props.Static_libs = transformArray(modules, "", ".stubs")
213 props.Sdk_version = proptools.StringPtr("module_current")
214 props.Visibility = []string{"//frameworks/base"}
215 ctx.CreateModule(java.LibraryFactory, &props)
216}
217
218func createMergedSystemStubs(ctx android.LoadHookContext, modules []string) {
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000219 // First create the all-updatable-modules-system-stubs
220 {
221 updatable_modules := removeAll(modules, non_updatable_modules)
222 props := libraryProps{}
223 props.Name = proptools.StringPtr("all-updatable-modules-system-stubs")
224 props.Static_libs = transformArray(updatable_modules, "", ".stubs.system")
225 props.Sdk_version = proptools.StringPtr("module_current")
226 props.Visibility = []string{"//frameworks/base"}
227 ctx.CreateModule(java.LibraryFactory, &props)
228 }
229 // Now merge all-updatable-modules-system-stubs and stubs from non-updatable modules
230 // into all-modules-system-stubs.
231 {
232 props := libraryProps{}
233 props.Name = proptools.StringPtr("all-modules-system-stubs")
234 props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.system")
235 props.Static_libs = append(props.Static_libs, "all-updatable-modules-system-stubs")
236 props.Sdk_version = proptools.StringPtr("module_current")
237 props.Visibility = []string{"//frameworks/base"}
238 ctx.CreateModule(java.LibraryFactory, &props)
239 }
240}
241
242func createMergedTestStubsForNonUpdatableModules(ctx android.LoadHookContext) {
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000243 props := libraryProps{}
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000244 props.Name = proptools.StringPtr("all-non-updatable-modules-test-stubs")
245 props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.test")
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000246 props.Sdk_version = proptools.StringPtr("module_current")
247 props.Visibility = []string{"//frameworks/base"}
248 ctx.CreateModule(java.LibraryFactory, &props)
249}
250
Anton Hansson95e89a82022-01-28 11:31:50 +0000251func createMergedFrameworkImpl(ctx android.LoadHookContext, modules []string) {
252 // This module is for the "framework-all" module, which should not include the core libraries.
253 modules = removeAll(modules, core_libraries_modules)
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +0000254 // Remove the modules that belong to non-updatable APEXes since those are allowed to compile
255 // against unstable APIs.
256 modules = removeAll(modules, non_updatable_modules)
257 // First create updatable-framework-module-impl, which contains all updatable modules.
258 // This module compiles against module_lib SDK.
259 {
260 props := libraryProps{}
261 props.Name = proptools.StringPtr("updatable-framework-module-impl")
262 props.Static_libs = transformArray(modules, "", ".impl")
263 props.Sdk_version = proptools.StringPtr("module_current")
264 props.Visibility = []string{"//frameworks/base"}
265 ctx.CreateModule(java.LibraryFactory, &props)
266 }
267
268 // Now create all-framework-module-impl, which contains updatable-framework-module-impl
269 // and all non-updatable modules. This module compiles against hidden APIs.
270 {
271 props := libraryProps{}
272 props.Name = proptools.StringPtr("all-framework-module-impl")
273 props.Static_libs = transformArray(non_updatable_modules, "", ".impl")
274 props.Static_libs = append(props.Static_libs, "updatable-framework-module-impl")
275 props.Sdk_version = proptools.StringPtr("core_platform")
276 props.Visibility = []string{"//frameworks/base"}
277 ctx.CreateModule(java.LibraryFactory, &props)
278 }
Anton Hansson95e89a82022-01-28 11:31:50 +0000279}
280
281func createMergedFrameworkModuleLibStubs(ctx android.LoadHookContext, modules []string) {
Mark White9ee18662023-08-07 11:18:09 +0000282 // The user of this module compiles against the "core" SDK and against non-updatable modules,
283 // so remove to avoid dupes.
Anton Hansson95e89a82022-01-28 11:31:50 +0000284 modules = removeAll(modules, core_libraries_modules)
Mark White9ee18662023-08-07 11:18:09 +0000285 modules = removeAll(modules, non_updatable_modules)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000286 props := libraryProps{}
287 props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api")
Anton Hanssonfd316452022-01-14 11:15:52 +0000288 props.Static_libs = transformArray(modules, "", ".stubs.module_lib")
Anton Hanssoncb00f942022-01-13 09:45:12 +0000289 props.Sdk_version = proptools.StringPtr("module_current")
290 props.Visibility = []string{"//frameworks/base"}
291 ctx.CreateModule(java.LibraryFactory, &props)
292}
293
Anton Hansson4468d7c2022-01-14 12:10:01 +0000294func createPublicStubsSourceFilegroup(ctx android.LoadHookContext, modules []string) {
295 props := fgProps{}
296 props.Name = proptools.StringPtr("all-modules-public-stubs-source")
297 props.Srcs = createSrcs(modules, "{.public.stubs.source}")
298 props.Visibility = []string{"//frameworks/base"}
Zi Wang0d6a5302023-02-16 14:54:01 -0800299 ctx.CreateModule(android.FileGroupFactory, &props, &bp2buildNotAvailable)
Anton Hansson4468d7c2022-01-14 12:10:01 +0000300}
301
Anton Hansson07a12952022-01-12 17:28:39 +0000302func createMergedTxts(ctx android.LoadHookContext, bootclasspath, system_server_classpath []string) {
Anton Hansson0860aaf2021-10-08 16:48:03 +0100303 var textFiles []MergedTxtDefinition
Anton Hansson16ff3572022-01-11 18:36:35 +0000304
Anton Hansson0860aaf2021-10-08 16:48:03 +0100305 tagSuffix := []string{".api.txt}", ".removed-api.txt}"}
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100306 distFilename := []string{"android.txt", "android-removed.txt"}
Anton Hansson0860aaf2021-10-08 16:48:03 +0100307 for i, f := range []string{"current.txt", "removed.txt"} {
308 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100309 TxtFilename: f,
310 DistFilename: distFilename[i],
311 BaseTxt: ":non-updatable-" + f,
312 Modules: bootclasspath,
313 ModuleTag: "{.public" + tagSuffix[i],
314 Scope: "public",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100315 })
316 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100317 TxtFilename: f,
318 DistFilename: distFilename[i],
319 BaseTxt: ":non-updatable-system-" + f,
320 Modules: bootclasspath,
321 ModuleTag: "{.system" + tagSuffix[i],
322 Scope: "system",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100323 })
324 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100325 TxtFilename: f,
326 DistFilename: distFilename[i],
327 BaseTxt: ":non-updatable-module-lib-" + f,
328 Modules: bootclasspath,
329 ModuleTag: "{.module-lib" + tagSuffix[i],
330 Scope: "module-lib",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100331 })
332 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100333 TxtFilename: f,
334 DistFilename: distFilename[i],
335 BaseTxt: ":non-updatable-system-server-" + f,
336 Modules: system_server_classpath,
337 ModuleTag: "{.system-server" + tagSuffix[i],
338 Scope: "system-server",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100339 })
340 }
341 for _, txt := range textFiles {
342 createMergedTxt(ctx, txt)
343 }
344}
345
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000346func createApiContributionDefaults(ctx android.LoadHookContext, modules []string) {
347 defaultsSdkKinds := []android.SdkKind{
348 android.SdkPublic, android.SdkSystem, android.SdkModule,
349 }
350 for _, sdkKind := range defaultsSdkKinds {
351 props := defaultsProps{}
352 props.Name = proptools.StringPtr(
353 sdkKind.DefaultJavaLibraryName() + "_contributions")
354 if sdkKind == android.SdkModule {
355 props.Name = proptools.StringPtr(
356 sdkKind.DefaultJavaLibraryName() + "_contributions_full")
357 }
358 props.Api_surface = proptools.StringPtr(sdkKind.String())
359 apiSuffix := ""
360 if sdkKind != android.SdkPublic {
361 apiSuffix = "." + strings.ReplaceAll(sdkKind.String(), "-", "_")
362 }
363 props.Api_contributions = transformArray(
364 modules, "", fmt.Sprintf(".stubs.source%s.api.contribution", apiSuffix))
365 props.Defaults_visibility = []string{"//visibility:public"}
Jihoon Kang471a05b2023-08-01 06:37:17 +0000366 props.Previous_api = proptools.StringPtr(":android.api.public.latest")
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000367 ctx.CreateModule(java.DefaultsFactory, &props)
368 }
369}
370
Jihoon Kang1453baa2023-05-27 05:32:30 +0000371func createFullApiLibraries(ctx android.LoadHookContext) {
372 javaLibraryNames := []string{
373 "android_stubs_current",
374 "android_system_stubs_current",
375 "android_test_stubs_current",
Mark Whiteb03c1fc2023-08-12 01:31:26 +0000376 "android_test_frameworks_core_stubs_current",
Jihoon Kang1453baa2023-05-27 05:32:30 +0000377 "android_module_lib_stubs_current",
378 "android_system_server_stubs_current",
379 }
380
381 for _, libraryName := range javaLibraryNames {
382 props := libraryProps{}
383 props.Name = proptools.StringPtr(libraryName)
384 staticLib := libraryName + ".from-source"
385 if ctx.Config().BuildFromTextStub() {
386 staticLib = libraryName + ".from-text"
387 }
388 props.Static_libs = []string{staticLib}
389 props.Defaults = []string{"android.jar_defaults"}
390 props.Visibility = []string{"//visibility:public"}
391
392 ctx.CreateModule(java.LibraryFactory, &props)
393 }
394}
395
Anton Hansson0860aaf2021-10-08 16:48:03 +0100396func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) {
Anton Hansson07a12952022-01-12 17:28:39 +0000397 bootclasspath := a.properties.Bootclasspath
Cole Faustdcda3702022-10-04 14:46:35 -0700398 system_server_classpath := a.properties.System_server_classpath
Anton Hansson07a12952022-01-12 17:28:39 +0000399 if ctx.Config().VendorConfig("ANDROID").Bool("include_nonpublic_framework_api") {
400 bootclasspath = append(bootclasspath, a.properties.Conditional_bootclasspath...)
401 sort.Strings(bootclasspath)
402 }
Cole Faustdcda3702022-10-04 14:46:35 -0700403 createMergedTxts(ctx, bootclasspath, system_server_classpath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100404
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000405 createMergedPublicStubs(ctx, bootclasspath)
406 createMergedSystemStubs(ctx, bootclasspath)
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000407 createMergedTestStubsForNonUpdatableModules(ctx)
Anton Hansson95e89a82022-01-28 11:31:50 +0000408 createMergedFrameworkModuleLibStubs(ctx, bootclasspath)
409 createMergedFrameworkImpl(ctx, bootclasspath)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000410
Cole Faustdcda3702022-10-04 14:46:35 -0700411 createMergedAnnotationsFilegroups(ctx, bootclasspath, system_server_classpath)
Anton Hanssoncc18e032022-01-12 14:45:22 +0000412
Anton Hansson4468d7c2022-01-14 12:10:01 +0000413 createPublicStubsSourceFilegroup(ctx, bootclasspath)
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000414
415 createApiContributionDefaults(ctx, bootclasspath)
Jihoon Kang1453baa2023-05-27 05:32:30 +0000416
417 createFullApiLibraries(ctx)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100418}
419
420func combinedApisModuleFactory() android.Module {
421 module := &CombinedApis{}
422 module.AddProperties(&module.properties)
423 android.InitAndroidModule(module)
424 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) })
Zi Wang0d6a5302023-02-16 14:54:01 -0800425 android.InitBazelModule(module)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100426 return module
427}
Anton Hanssonfd316452022-01-14 11:15:52 +0000428
Zi Wang0d6a5302023-02-16 14:54:01 -0800429type bazelCombinedApisAttributes struct {
430 Scope bazel.StringAttribute
431 Base bazel.LabelAttribute
432 Deps bazel.LabelListAttribute
433}
434
435// combined_apis bp2build converter
436func (a *CombinedApis) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
437 basePrefix := "non-updatable"
Zi Wang0d6a5302023-02-16 14:54:01 -0800438 scopeToSuffix := map[string]string{
439 "public": "-current.txt",
440 "system": "-system-current.txt",
441 "module-lib": "-module-lib-current.txt",
442 "system-server": "-system-server-current.txt",
443 }
444
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000445 for scopeName, suffix := range scopeToSuffix {
Zi Wang0d6a5302023-02-16 14:54:01 -0800446 name := a.Name() + suffix
447
448 var scope bazel.StringAttribute
449 scope.SetValue(scopeName)
450
451 var base bazel.LabelAttribute
452 base.SetValue(android.BazelLabelForModuleDepSingle(ctx, basePrefix+suffix))
453
454 var deps bazel.LabelListAttribute
455 classpath := a.properties.Bootclasspath
456 if scopeName == "system-server" {
457 classpath = a.properties.System_server_classpath
458 }
459 deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, classpath))
460
461 attrs := bazelCombinedApisAttributes{
462 Scope: scope,
463 Base: base,
464 Deps: deps,
465 }
466 props := bazel.BazelTargetModuleProperties{
467 Rule_class: "merged_txts",
468 Bzl_load_location: "//build/bazel/rules/java:merged_txts.bzl",
469 }
470 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, &attrs)
471 }
472}
473
Anton Hanssonfd316452022-01-14 11:15:52 +0000474// Various utility methods below.
475
476// Creates an array of ":<m><tag>" for each m in <modules>.
477func createSrcs(modules []string, tag string) []string {
478 return transformArray(modules, ":", tag)
479}
480
481// Creates an array of "<prefix><m><suffix>", for each m in <modules>.
482func transformArray(modules []string, prefix, suffix string) []string {
483 a := make([]string, 0, len(modules))
484 for _, module := range modules {
485 a = append(a, prefix+module+suffix)
486 }
487 return a
488}
489
490func removeAll(s []string, vs []string) []string {
491 for _, v := range vs {
492 s = remove(s, v)
493 }
494 return s
495}
496
497func remove(s []string, v string) []string {
498 s2 := make([]string, 0, len(s))
499 for _, sv := range s {
500 if sv != v {
501 s2 = append(s2, sv)
502 }
503 }
504 return s2
505}