blob: 431d6d8d06ff02b7adc420269f7af430b53d4994 [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"
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.
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +000045var non_updatable_modules = []string{virtualization}
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
Zi Wang0d6a5302023-02-16 14:54:01 -080066 android.BazelModuleBase
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)
77}
78
79var PrepareForCombinedApisTest = android.FixtureRegisterWithContext(registerBuildComponents)
80
81func (a *CombinedApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
82}
83
84type genruleProps struct {
85 Name *string
86 Cmd *string
87 Dists []android.Dist
88 Out []string
89 Srcs []string
90 Tools []string
91 Visibility []string
92}
93
Anton Hanssoncb00f942022-01-13 09:45:12 +000094type libraryProps struct {
95 Name *string
96 Sdk_version *string
97 Static_libs []string
98 Visibility []string
Jihoon Kang1453baa2023-05-27 05:32:30 +000099 Defaults []string
Anton Hanssoncb00f942022-01-13 09:45:12 +0000100}
101
Anton Hansson4468d7c2022-01-14 12:10:01 +0000102type fgProps struct {
103 Name *string
104 Srcs []string
105 Visibility []string
106}
107
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000108type defaultsProps struct {
109 Name *string
110 Api_surface *string
111 Api_contributions []string
112 Defaults_visibility []string
Jihoon Kang471a05b2023-08-01 06:37:17 +0000113 Previous_api *string
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000114}
115
Zi Wang0d6a5302023-02-16 14:54:01 -0800116type Bazel_module struct {
117 Bp2build_available *bool
118}
119type bazelProperties struct {
120 *Bazel_module
121}
122
123var bp2buildNotAvailable = bazelProperties{
124 &Bazel_module{
125 Bp2build_available: proptools.BoolPtr(false),
126 },
127}
128
Anton Hansson0860aaf2021-10-08 16:48:03 +0100129// Struct to pass parameters for the various merged [current|removed].txt file modules we create.
130type MergedTxtDefinition struct {
131 // "current.txt" or "removed.txt"
132 TxtFilename string
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100133 // Filename in the new dist dir. "android.txt" or "android-removed.txt"
134 DistFilename string
Anton Hansson0860aaf2021-10-08 16:48:03 +0100135 // The module for the non-updatable / non-module part of the api.
136 BaseTxt string
137 // The list of modules that are relevant for this merged txt.
138 Modules []string
139 // The output tag for each module to use.e.g. {.public.api.txt} for current.txt
140 ModuleTag string
141 // public, system, module-lib or system-server
142 Scope string
143}
144
145func createMergedTxt(ctx android.LoadHookContext, txt MergedTxtDefinition) {
146 metalavaCmd := "$(location metalava)"
147 // Silence reflection warnings. See b/168689341
148 metalavaCmd += " -J--add-opens=java.base/java.util=ALL-UNNAMED "
Paul Duffinf3b1fc42023-08-14 22:03:06 +0100149 metalavaCmd += " --quiet merge-signatures --format=v2 "
Anton Hansson0860aaf2021-10-08 16:48:03 +0100150
151 filename := txt.TxtFilename
152 if txt.Scope != "public" {
153 filename = txt.Scope + "-" + filename
154 }
Anton Hansson0860aaf2021-10-08 16:48:03 +0100155 props := genruleProps{}
156 props.Name = proptools.StringPtr(ctx.ModuleName() + "-" + filename)
157 props.Tools = []string{"metalava"}
Anton Hansson16ff3572022-01-11 18:36:35 +0000158 props.Out = []string{filename}
Paul Duffinf3b1fc42023-08-14 22:03:06 +0100159 props.Cmd = proptools.StringPtr(metalavaCmd + "$(in) --out $(out)")
Anton Hanssonfd316452022-01-14 11:15:52 +0000160 props.Srcs = append([]string{txt.BaseTxt}, createSrcs(txt.Modules, txt.ModuleTag)...)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100161 props.Dists = []android.Dist{
162 {
163 Targets: []string{"droidcore"},
164 Dir: proptools.StringPtr("api"),
165 Dest: proptools.StringPtr(filename),
166 },
167 {
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100168 Targets: []string{"api_txt", "sdk"},
Anton Hansson0860aaf2021-10-08 16:48:03 +0100169 Dir: proptools.StringPtr("apistubs/android/" + txt.Scope + "/api"),
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100170 Dest: proptools.StringPtr(txt.DistFilename),
Anton Hansson0860aaf2021-10-08 16:48:03 +0100171 },
172 }
173 props.Visibility = []string{"//visibility:public"}
Zi Wang0d6a5302023-02-16 14:54:01 -0800174 ctx.CreateModule(genrule.GenRuleFactory, &props, &bp2buildNotAvailable)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100175}
176
Cole Faustdcda3702022-10-04 14:46:35 -0700177func createMergedAnnotationsFilegroups(ctx android.LoadHookContext, modules, system_server_modules []string) {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000178 for _, i := range []struct {
Cole Faustdcda3702022-10-04 14:46:35 -0700179 name string
180 tag string
181 modules []string
182 }{
183 {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000184 name: "all-modules-public-annotations",
185 tag: "{.public.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700186 modules: modules,
187 }, {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000188 name: "all-modules-system-annotations",
189 tag: "{.system.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700190 modules: modules,
191 }, {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000192 name: "all-modules-module-lib-annotations",
193 tag: "{.module-lib.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700194 modules: modules,
195 }, {
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000196 name: "all-modules-system-server-annotations",
197 tag: "{.system-server.annotations.zip}",
Cole Faustdcda3702022-10-04 14:46:35 -0700198 modules: system_server_modules,
199 },
200 } {
201 props := fgProps{}
202 props.Name = proptools.StringPtr(i.name)
203 props.Srcs = createSrcs(i.modules, i.tag)
Zi Wang0d6a5302023-02-16 14:54:01 -0800204 ctx.CreateModule(android.FileGroupFactory, &props, &bp2buildNotAvailable)
Cole Faustdcda3702022-10-04 14:46:35 -0700205 }
Anton Hansson74b15642022-06-23 08:27:23 +0000206}
207
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000208func createMergedPublicStubs(ctx android.LoadHookContext, modules []string) {
209 props := libraryProps{}
210 props.Name = proptools.StringPtr("all-modules-public-stubs")
211 props.Static_libs = transformArray(modules, "", ".stubs")
212 props.Sdk_version = proptools.StringPtr("module_current")
213 props.Visibility = []string{"//frameworks/base"}
214 ctx.CreateModule(java.LibraryFactory, &props)
215}
216
217func createMergedSystemStubs(ctx android.LoadHookContext, modules []string) {
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000218 // First create the all-updatable-modules-system-stubs
219 {
220 updatable_modules := removeAll(modules, non_updatable_modules)
221 props := libraryProps{}
222 props.Name = proptools.StringPtr("all-updatable-modules-system-stubs")
223 props.Static_libs = transformArray(updatable_modules, "", ".stubs.system")
224 props.Sdk_version = proptools.StringPtr("module_current")
225 props.Visibility = []string{"//frameworks/base"}
226 ctx.CreateModule(java.LibraryFactory, &props)
227 }
228 // Now merge all-updatable-modules-system-stubs and stubs from non-updatable modules
229 // into all-modules-system-stubs.
230 {
231 props := libraryProps{}
232 props.Name = proptools.StringPtr("all-modules-system-stubs")
233 props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.system")
234 props.Static_libs = append(props.Static_libs, "all-updatable-modules-system-stubs")
235 props.Sdk_version = proptools.StringPtr("module_current")
236 props.Visibility = []string{"//frameworks/base"}
237 ctx.CreateModule(java.LibraryFactory, &props)
238 }
239}
240
241func createMergedTestStubsForNonUpdatableModules(ctx android.LoadHookContext) {
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000242 props := libraryProps{}
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000243 props.Name = proptools.StringPtr("all-non-updatable-modules-test-stubs")
244 props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.test")
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000245 props.Sdk_version = proptools.StringPtr("module_current")
246 props.Visibility = []string{"//frameworks/base"}
247 ctx.CreateModule(java.LibraryFactory, &props)
248}
249
Anton Hansson95e89a82022-01-28 11:31:50 +0000250func createMergedFrameworkImpl(ctx android.LoadHookContext, modules []string) {
251 // This module is for the "framework-all" module, which should not include the core libraries.
252 modules = removeAll(modules, core_libraries_modules)
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +0000253 // Remove the modules that belong to non-updatable APEXes since those are allowed to compile
254 // against unstable APIs.
255 modules = removeAll(modules, non_updatable_modules)
256 // First create updatable-framework-module-impl, which contains all updatable modules.
257 // This module compiles against module_lib SDK.
258 {
259 props := libraryProps{}
260 props.Name = proptools.StringPtr("updatable-framework-module-impl")
261 props.Static_libs = transformArray(modules, "", ".impl")
262 props.Sdk_version = proptools.StringPtr("module_current")
263 props.Visibility = []string{"//frameworks/base"}
264 ctx.CreateModule(java.LibraryFactory, &props)
265 }
266
267 // Now create all-framework-module-impl, which contains updatable-framework-module-impl
268 // and all non-updatable modules. This module compiles against hidden APIs.
269 {
270 props := libraryProps{}
271 props.Name = proptools.StringPtr("all-framework-module-impl")
272 props.Static_libs = transformArray(non_updatable_modules, "", ".impl")
273 props.Static_libs = append(props.Static_libs, "updatable-framework-module-impl")
274 props.Sdk_version = proptools.StringPtr("core_platform")
275 props.Visibility = []string{"//frameworks/base"}
276 ctx.CreateModule(java.LibraryFactory, &props)
277 }
Anton Hansson95e89a82022-01-28 11:31:50 +0000278}
279
280func createMergedFrameworkModuleLibStubs(ctx android.LoadHookContext, modules []string) {
Anton Hanssoncb00f942022-01-13 09:45:12 +0000281 // The user of this module compiles against the "core" SDK, so remove core libraries to avoid dupes.
Anton Hansson95e89a82022-01-28 11:31:50 +0000282 modules = removeAll(modules, core_libraries_modules)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000283 props := libraryProps{}
284 props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api")
Anton Hanssonfd316452022-01-14 11:15:52 +0000285 props.Static_libs = transformArray(modules, "", ".stubs.module_lib")
Anton Hanssoncb00f942022-01-13 09:45:12 +0000286 props.Sdk_version = proptools.StringPtr("module_current")
287 props.Visibility = []string{"//frameworks/base"}
288 ctx.CreateModule(java.LibraryFactory, &props)
289}
290
Anton Hansson4468d7c2022-01-14 12:10:01 +0000291func createPublicStubsSourceFilegroup(ctx android.LoadHookContext, modules []string) {
292 props := fgProps{}
293 props.Name = proptools.StringPtr("all-modules-public-stubs-source")
294 props.Srcs = createSrcs(modules, "{.public.stubs.source}")
295 props.Visibility = []string{"//frameworks/base"}
Zi Wang0d6a5302023-02-16 14:54:01 -0800296 ctx.CreateModule(android.FileGroupFactory, &props, &bp2buildNotAvailable)
Anton Hansson4468d7c2022-01-14 12:10:01 +0000297}
298
Anton Hansson07a12952022-01-12 17:28:39 +0000299func createMergedTxts(ctx android.LoadHookContext, bootclasspath, system_server_classpath []string) {
Anton Hansson0860aaf2021-10-08 16:48:03 +0100300 var textFiles []MergedTxtDefinition
Anton Hansson16ff3572022-01-11 18:36:35 +0000301
Anton Hansson0860aaf2021-10-08 16:48:03 +0100302 tagSuffix := []string{".api.txt}", ".removed-api.txt}"}
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100303 distFilename := []string{"android.txt", "android-removed.txt"}
Anton Hansson0860aaf2021-10-08 16:48:03 +0100304 for i, f := range []string{"current.txt", "removed.txt"} {
305 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100306 TxtFilename: f,
307 DistFilename: distFilename[i],
308 BaseTxt: ":non-updatable-" + f,
309 Modules: bootclasspath,
310 ModuleTag: "{.public" + tagSuffix[i],
311 Scope: "public",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100312 })
313 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100314 TxtFilename: f,
315 DistFilename: distFilename[i],
316 BaseTxt: ":non-updatable-system-" + f,
317 Modules: bootclasspath,
318 ModuleTag: "{.system" + tagSuffix[i],
319 Scope: "system",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100320 })
321 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100322 TxtFilename: f,
323 DistFilename: distFilename[i],
324 BaseTxt: ":non-updatable-module-lib-" + f,
325 Modules: bootclasspath,
326 ModuleTag: "{.module-lib" + tagSuffix[i],
327 Scope: "module-lib",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100328 })
329 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100330 TxtFilename: f,
331 DistFilename: distFilename[i],
332 BaseTxt: ":non-updatable-system-server-" + f,
333 Modules: system_server_classpath,
334 ModuleTag: "{.system-server" + tagSuffix[i],
335 Scope: "system-server",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100336 })
337 }
338 for _, txt := range textFiles {
339 createMergedTxt(ctx, txt)
340 }
341}
342
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000343func createApiContributionDefaults(ctx android.LoadHookContext, modules []string) {
344 defaultsSdkKinds := []android.SdkKind{
345 android.SdkPublic, android.SdkSystem, android.SdkModule,
346 }
347 for _, sdkKind := range defaultsSdkKinds {
348 props := defaultsProps{}
349 props.Name = proptools.StringPtr(
350 sdkKind.DefaultJavaLibraryName() + "_contributions")
351 if sdkKind == android.SdkModule {
352 props.Name = proptools.StringPtr(
353 sdkKind.DefaultJavaLibraryName() + "_contributions_full")
354 }
355 props.Api_surface = proptools.StringPtr(sdkKind.String())
356 apiSuffix := ""
357 if sdkKind != android.SdkPublic {
358 apiSuffix = "." + strings.ReplaceAll(sdkKind.String(), "-", "_")
359 }
360 props.Api_contributions = transformArray(
361 modules, "", fmt.Sprintf(".stubs.source%s.api.contribution", apiSuffix))
362 props.Defaults_visibility = []string{"//visibility:public"}
Jihoon Kang471a05b2023-08-01 06:37:17 +0000363 props.Previous_api = proptools.StringPtr(":android.api.public.latest")
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000364 ctx.CreateModule(java.DefaultsFactory, &props)
365 }
366}
367
Jihoon Kang1453baa2023-05-27 05:32:30 +0000368func createFullApiLibraries(ctx android.LoadHookContext) {
369 javaLibraryNames := []string{
370 "android_stubs_current",
371 "android_system_stubs_current",
372 "android_test_stubs_current",
373 "android_module_lib_stubs_current",
374 "android_system_server_stubs_current",
375 }
376
377 for _, libraryName := range javaLibraryNames {
378 props := libraryProps{}
379 props.Name = proptools.StringPtr(libraryName)
380 staticLib := libraryName + ".from-source"
381 if ctx.Config().BuildFromTextStub() {
382 staticLib = libraryName + ".from-text"
383 }
384 props.Static_libs = []string{staticLib}
385 props.Defaults = []string{"android.jar_defaults"}
386 props.Visibility = []string{"//visibility:public"}
387
388 ctx.CreateModule(java.LibraryFactory, &props)
389 }
390}
391
Anton Hansson0860aaf2021-10-08 16:48:03 +0100392func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) {
Anton Hansson07a12952022-01-12 17:28:39 +0000393 bootclasspath := a.properties.Bootclasspath
Cole Faustdcda3702022-10-04 14:46:35 -0700394 system_server_classpath := a.properties.System_server_classpath
Anton Hansson07a12952022-01-12 17:28:39 +0000395 if ctx.Config().VendorConfig("ANDROID").Bool("include_nonpublic_framework_api") {
396 bootclasspath = append(bootclasspath, a.properties.Conditional_bootclasspath...)
397 sort.Strings(bootclasspath)
398 }
Cole Faustdcda3702022-10-04 14:46:35 -0700399 createMergedTxts(ctx, bootclasspath, system_server_classpath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100400
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000401 createMergedPublicStubs(ctx, bootclasspath)
402 createMergedSystemStubs(ctx, bootclasspath)
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000403 createMergedTestStubsForNonUpdatableModules(ctx)
Anton Hansson95e89a82022-01-28 11:31:50 +0000404 createMergedFrameworkModuleLibStubs(ctx, bootclasspath)
405 createMergedFrameworkImpl(ctx, bootclasspath)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000406
Cole Faustdcda3702022-10-04 14:46:35 -0700407 createMergedAnnotationsFilegroups(ctx, bootclasspath, system_server_classpath)
Anton Hanssoncc18e032022-01-12 14:45:22 +0000408
Anton Hansson4468d7c2022-01-14 12:10:01 +0000409 createPublicStubsSourceFilegroup(ctx, bootclasspath)
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000410
411 createApiContributionDefaults(ctx, bootclasspath)
Jihoon Kang1453baa2023-05-27 05:32:30 +0000412
413 createFullApiLibraries(ctx)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100414}
415
416func combinedApisModuleFactory() android.Module {
417 module := &CombinedApis{}
418 module.AddProperties(&module.properties)
419 android.InitAndroidModule(module)
420 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) })
Zi Wang0d6a5302023-02-16 14:54:01 -0800421 android.InitBazelModule(module)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100422 return module
423}
Anton Hanssonfd316452022-01-14 11:15:52 +0000424
Zi Wang0d6a5302023-02-16 14:54:01 -0800425type bazelCombinedApisAttributes struct {
426 Scope bazel.StringAttribute
427 Base bazel.LabelAttribute
428 Deps bazel.LabelListAttribute
429}
430
431// combined_apis bp2build converter
Chris Parsons52f9ad02023-09-13 21:30:55 +0000432func (a *CombinedApis) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Zi Wang0d6a5302023-02-16 14:54:01 -0800433 basePrefix := "non-updatable"
Zi Wang0d6a5302023-02-16 14:54:01 -0800434 scopeToSuffix := map[string]string{
435 "public": "-current.txt",
436 "system": "-system-current.txt",
437 "module-lib": "-module-lib-current.txt",
438 "system-server": "-system-server-current.txt",
439 }
440
Jihoon Kang1e4ac1d2023-04-07 18:50:38 +0000441 for scopeName, suffix := range scopeToSuffix {
Zi Wang0d6a5302023-02-16 14:54:01 -0800442 name := a.Name() + suffix
443
444 var scope bazel.StringAttribute
445 scope.SetValue(scopeName)
446
447 var base bazel.LabelAttribute
448 base.SetValue(android.BazelLabelForModuleDepSingle(ctx, basePrefix+suffix))
449
450 var deps bazel.LabelListAttribute
451 classpath := a.properties.Bootclasspath
452 if scopeName == "system-server" {
453 classpath = a.properties.System_server_classpath
454 }
455 deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, classpath))
456
457 attrs := bazelCombinedApisAttributes{
458 Scope: scope,
459 Base: base,
460 Deps: deps,
461 }
462 props := bazel.BazelTargetModuleProperties{
463 Rule_class: "merged_txts",
464 Bzl_load_location: "//build/bazel/rules/java:merged_txts.bzl",
465 }
466 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, &attrs)
467 }
468}
469
Anton Hanssonfd316452022-01-14 11:15:52 +0000470// Various utility methods below.
471
472// Creates an array of ":<m><tag>" for each m in <modules>.
473func createSrcs(modules []string, tag string) []string {
474 return transformArray(modules, ":", tag)
475}
476
477// Creates an array of "<prefix><m><suffix>", for each m in <modules>.
478func transformArray(modules []string, prefix, suffix string) []string {
479 a := make([]string, 0, len(modules))
480 for _, module := range modules {
481 a = append(a, prefix+module+suffix)
482 }
483 return a
484}
485
486func removeAll(s []string, vs []string) []string {
487 for _, v := range vs {
488 s = remove(s, v)
489 }
490 return s
491}
492
493func remove(s []string, v string) []string {
494 s2 := make([]string, 0, len(s))
495 for _, sv := range s {
496 if sv != v {
497 s2 = append(s2, sv)
498 }
499 }
500 return s2
501}