blob: 22120001af351aa2c6160f9c84fa1157fb1c1f99 [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 (
Anton Hansson07a12952022-01-12 17:28:39 +000018 "sort"
19
Anton Hansson0860aaf2021-10-08 16:48:03 +010020 "github.com/google/blueprint/proptools"
21
22 "android/soong/android"
23 "android/soong/genrule"
Anton Hanssoncb00f942022-01-13 09:45:12 +000024 "android/soong/java"
Anton Hansson0860aaf2021-10-08 16:48:03 +010025)
26
Anton Hansson05e944d2022-01-13 12:26:30 +000027const art = "art.module.public.api"
28const conscrypt = "conscrypt.module.public.api"
29const i18n = "i18n.module.public.api"
Anton Hansson09a9c4e2022-04-08 10:59:46 +010030
Anton Hansson95e89a82022-01-28 11:31:50 +000031var core_libraries_modules = []string{art, conscrypt, i18n}
Anton Hansson05e944d2022-01-13 12:26:30 +000032
Anton Hansson0860aaf2021-10-08 16:48:03 +010033// The intention behind this soong plugin is to generate a number of "merged"
34// API-related modules that would otherwise require a large amount of very
35// similar Android.bp boilerplate to define. For example, the merged current.txt
36// API definitions (created by merging the non-updatable current.txt with all
37// the module current.txts). This simplifies the addition of new android
38// modules, by reducing the number of genrules etc a new module must be added to.
39
40// The properties of the combined_apis module type.
41type CombinedApisProperties struct {
Anton Hansson16ff3572022-01-11 18:36:35 +000042 // Module libraries in the bootclasspath
43 Bootclasspath []string
Anton Hansson07a12952022-01-12 17:28:39 +000044 // Module libraries on the bootclasspath if include_nonpublic_framework_api is true.
45 Conditional_bootclasspath []string
Anton Hansson16ff3572022-01-11 18:36:35 +000046 // Module libraries in system server
47 System_server_classpath []string
Anton Hansson0860aaf2021-10-08 16:48:03 +010048}
49
50type CombinedApis struct {
51 android.ModuleBase
52
53 properties CombinedApisProperties
54}
55
56func init() {
57 registerBuildComponents(android.InitRegistrationContext)
58}
59
60func registerBuildComponents(ctx android.RegistrationContext) {
61 ctx.RegisterModuleType("combined_apis", combinedApisModuleFactory)
62}
63
64var PrepareForCombinedApisTest = android.FixtureRegisterWithContext(registerBuildComponents)
65
66func (a *CombinedApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
67}
68
69type genruleProps struct {
70 Name *string
71 Cmd *string
72 Dists []android.Dist
73 Out []string
74 Srcs []string
75 Tools []string
76 Visibility []string
77}
78
Anton Hanssoncb00f942022-01-13 09:45:12 +000079type libraryProps struct {
80 Name *string
81 Sdk_version *string
82 Static_libs []string
83 Visibility []string
84}
85
Anton Hansson4468d7c2022-01-14 12:10:01 +000086type fgProps struct {
87 Name *string
88 Srcs []string
89 Visibility []string
90}
91
Anton Hansson0860aaf2021-10-08 16:48:03 +010092// Struct to pass parameters for the various merged [current|removed].txt file modules we create.
93type MergedTxtDefinition struct {
94 // "current.txt" or "removed.txt"
95 TxtFilename string
Anton Hansson09a9c4e2022-04-08 10:59:46 +010096 // Filename in the new dist dir. "android.txt" or "android-removed.txt"
97 DistFilename string
Anton Hansson0860aaf2021-10-08 16:48:03 +010098 // The module for the non-updatable / non-module part of the api.
99 BaseTxt string
100 // The list of modules that are relevant for this merged txt.
101 Modules []string
102 // The output tag for each module to use.e.g. {.public.api.txt} for current.txt
103 ModuleTag string
104 // public, system, module-lib or system-server
105 Scope string
106}
107
108func createMergedTxt(ctx android.LoadHookContext, txt MergedTxtDefinition) {
109 metalavaCmd := "$(location metalava)"
110 // Silence reflection warnings. See b/168689341
111 metalavaCmd += " -J--add-opens=java.base/java.util=ALL-UNNAMED "
112 metalavaCmd += " --quiet --no-banner --format=v2 "
113
114 filename := txt.TxtFilename
115 if txt.Scope != "public" {
116 filename = txt.Scope + "-" + filename
117 }
Anton Hansson0860aaf2021-10-08 16:48:03 +0100118 props := genruleProps{}
119 props.Name = proptools.StringPtr(ctx.ModuleName() + "-" + filename)
120 props.Tools = []string{"metalava"}
Anton Hansson16ff3572022-01-11 18:36:35 +0000121 props.Out = []string{filename}
Anton Hansson0860aaf2021-10-08 16:48:03 +0100122 props.Cmd = proptools.StringPtr(metalavaCmd + "$(in) --api $(out)")
Anton Hanssonfd316452022-01-14 11:15:52 +0000123 props.Srcs = append([]string{txt.BaseTxt}, createSrcs(txt.Modules, txt.ModuleTag)...)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100124 props.Dists = []android.Dist{
125 {
126 Targets: []string{"droidcore"},
127 Dir: proptools.StringPtr("api"),
128 Dest: proptools.StringPtr(filename),
129 },
130 {
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100131 Targets: []string{"api_txt", "sdk"},
Anton Hansson0860aaf2021-10-08 16:48:03 +0100132 Dir: proptools.StringPtr("apistubs/android/" + txt.Scope + "/api"),
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100133 Dest: proptools.StringPtr(txt.DistFilename),
Anton Hansson0860aaf2021-10-08 16:48:03 +0100134 },
135 }
136 props.Visibility = []string{"//visibility:public"}
137 ctx.CreateModule(genrule.GenRuleFactory, &props)
138}
139
140func createMergedStubsSrcjar(ctx android.LoadHookContext, modules []string) {
141 props := genruleProps{}
142 props.Name = proptools.StringPtr(ctx.ModuleName() + "-current.srcjar")
143 props.Tools = []string{"merge_zips"}
144 props.Out = []string{"current.srcjar"}
145 props.Cmd = proptools.StringPtr("$(location merge_zips) $(out) $(in)")
Anton Hanssonfd316452022-01-14 11:15:52 +0000146 props.Srcs = append([]string{":api-stubs-docs-non-updatable"}, createSrcs(modules, "{.public.stubs.source}")...)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100147 props.Visibility = []string{"//visibility:private"} // Used by make module in //development, mind
148 ctx.CreateModule(genrule.GenRuleFactory, &props)
149}
150
Anton Hansson0c5d9ab2022-06-23 07:58:51 +0000151func createMergedPublicAnnotationsFilegroup(ctx android.LoadHookContext, modules []string) {
152 props := fgProps{}
153 props.Name = proptools.StringPtr("all-modules-public-annotations")
154 props.Srcs = createSrcs(modules, "{.public.annotations.zip}")
155 ctx.CreateModule(android.FileGroupFactory, &props)
Anton Hanssoncc18e032022-01-12 14:45:22 +0000156}
157
Anton Hansson0860aaf2021-10-08 16:48:03 +0100158func createFilteredApiVersions(ctx android.LoadHookContext, modules []string) {
Anton Hansson05e944d2022-01-13 12:26:30 +0000159 // For the filtered api versions, we prune all APIs except art module's APIs. because
160 // 1) ART apis are available by default to all modules, while other module-to-module deps are
161 // explicit and probably receive more scrutiny anyway
162 // 2) The number of ART/libcore APIs is large, so not linting them would create a large gap
163 // 3) It's a compromise. Ideally we wouldn't be filtering out any module APIs, and have
164 // per-module lint databases that excludes just that module's APIs. Alas, that's more
165 // difficult to achieve.
166 modules = remove(modules, art)
167
Anton Hansson0860aaf2021-10-08 16:48:03 +0100168 props := genruleProps{}
169 props.Name = proptools.StringPtr("api-versions-xml-public-filtered")
170 props.Tools = []string{"api_versions_trimmer"}
171 props.Out = []string{"api-versions-public-filtered.xml"}
172 props.Cmd = proptools.StringPtr("$(location api_versions_trimmer) $(out) $(in)")
173 // Note: order matters: first parameter is the full api-versions.xml
174 // after that the stubs files in any order
175 // stubs files are all modules that export API surfaces EXCEPT ART
Anton Hansson247c5f22022-05-09 09:53:12 +0000176 props.Srcs = append([]string{":api_versions_public{.api_versions.xml}"}, createSrcs(modules, ".stubs{.jar}")...)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100177 props.Dists = []android.Dist{{Targets: []string{"sdk"}}}
178 ctx.CreateModule(genrule.GenRuleFactory, &props)
179}
180
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000181func createMergedPublicStubs(ctx android.LoadHookContext, modules []string) {
182 props := libraryProps{}
183 props.Name = proptools.StringPtr("all-modules-public-stubs")
184 props.Static_libs = transformArray(modules, "", ".stubs")
185 props.Sdk_version = proptools.StringPtr("module_current")
186 props.Visibility = []string{"//frameworks/base"}
187 ctx.CreateModule(java.LibraryFactory, &props)
188}
189
190func createMergedSystemStubs(ctx android.LoadHookContext, modules []string) {
191 props := libraryProps{}
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000192 props.Name = proptools.StringPtr("all-modules-system-stubs")
Paul Duffin57533b42022-01-25 16:25:35 +0000193 props.Static_libs = transformArray(modules, "", ".stubs.system")
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000194 props.Sdk_version = proptools.StringPtr("module_current")
195 props.Visibility = []string{"//frameworks/base"}
196 ctx.CreateModule(java.LibraryFactory, &props)
197}
198
Anton Hansson95e89a82022-01-28 11:31:50 +0000199func createMergedFrameworkImpl(ctx android.LoadHookContext, modules []string) {
200 // This module is for the "framework-all" module, which should not include the core libraries.
201 modules = removeAll(modules, core_libraries_modules)
202 // TODO(b/214988855): remove the line below when framework-bluetooth has an impl jar.
203 modules = remove(modules, "framework-bluetooth")
204 props := libraryProps{}
205 props.Name = proptools.StringPtr("all-framework-module-impl")
206 props.Static_libs = transformArray(modules, "", ".impl")
207 // Media module's impl jar is called "updatable-media"
208 for i, v := range props.Static_libs {
209 if v == "framework-media.impl" {
210 props.Static_libs[i] = "updatable-media"
211 }
212 }
213 props.Sdk_version = proptools.StringPtr("module_current")
214 props.Visibility = []string{"//frameworks/base"}
215 ctx.CreateModule(java.LibraryFactory, &props)
216}
217
218func createMergedFrameworkModuleLibStubs(ctx android.LoadHookContext, modules []string) {
Anton Hanssoncb00f942022-01-13 09:45:12 +0000219 // The user of this module compiles against the "core" SDK, so remove core libraries to avoid dupes.
Anton Hansson95e89a82022-01-28 11:31:50 +0000220 modules = removeAll(modules, core_libraries_modules)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000221 props := libraryProps{}
222 props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api")
Anton Hanssonfd316452022-01-14 11:15:52 +0000223 props.Static_libs = transformArray(modules, "", ".stubs.module_lib")
Anton Hanssoncb00f942022-01-13 09:45:12 +0000224 props.Sdk_version = proptools.StringPtr("module_current")
225 props.Visibility = []string{"//frameworks/base"}
226 ctx.CreateModule(java.LibraryFactory, &props)
227}
228
Anton Hansson4468d7c2022-01-14 12:10:01 +0000229func createPublicStubsSourceFilegroup(ctx android.LoadHookContext, modules []string) {
230 props := fgProps{}
231 props.Name = proptools.StringPtr("all-modules-public-stubs-source")
232 props.Srcs = createSrcs(modules, "{.public.stubs.source}")
233 props.Visibility = []string{"//frameworks/base"}
234 ctx.CreateModule(android.FileGroupFactory, &props)
235}
236
Anton Hansson07a12952022-01-12 17:28:39 +0000237func createMergedTxts(ctx android.LoadHookContext, bootclasspath, system_server_classpath []string) {
Anton Hansson0860aaf2021-10-08 16:48:03 +0100238 var textFiles []MergedTxtDefinition
Anton Hansson16ff3572022-01-11 18:36:35 +0000239
Anton Hansson0860aaf2021-10-08 16:48:03 +0100240 tagSuffix := []string{".api.txt}", ".removed-api.txt}"}
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100241 distFilename := []string{"android.txt", "android-removed.txt"}
Anton Hansson0860aaf2021-10-08 16:48:03 +0100242 for i, f := range []string{"current.txt", "removed.txt"} {
243 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100244 TxtFilename: f,
245 DistFilename: distFilename[i],
246 BaseTxt: ":non-updatable-" + f,
247 Modules: bootclasspath,
248 ModuleTag: "{.public" + tagSuffix[i],
249 Scope: "public",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100250 })
251 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100252 TxtFilename: f,
253 DistFilename: distFilename[i],
254 BaseTxt: ":non-updatable-system-" + f,
255 Modules: bootclasspath,
256 ModuleTag: "{.system" + tagSuffix[i],
257 Scope: "system",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100258 })
259 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100260 TxtFilename: f,
261 DistFilename: distFilename[i],
262 BaseTxt: ":non-updatable-module-lib-" + f,
263 Modules: bootclasspath,
264 ModuleTag: "{.module-lib" + tagSuffix[i],
265 Scope: "module-lib",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100266 })
267 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100268 TxtFilename: f,
269 DistFilename: distFilename[i],
270 BaseTxt: ":non-updatable-system-server-" + f,
271 Modules: system_server_classpath,
272 ModuleTag: "{.system-server" + tagSuffix[i],
273 Scope: "system-server",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100274 })
275 }
276 for _, txt := range textFiles {
277 createMergedTxt(ctx, txt)
278 }
279}
280
281func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) {
Anton Hansson07a12952022-01-12 17:28:39 +0000282 bootclasspath := a.properties.Bootclasspath
283 if ctx.Config().VendorConfig("ANDROID").Bool("include_nonpublic_framework_api") {
284 bootclasspath = append(bootclasspath, a.properties.Conditional_bootclasspath...)
285 sort.Strings(bootclasspath)
286 }
287 createMergedTxts(ctx, bootclasspath, a.properties.System_server_classpath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100288
Anton Hansson07a12952022-01-12 17:28:39 +0000289 createMergedStubsSrcjar(ctx, bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100290
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000291 createMergedPublicStubs(ctx, bootclasspath)
292 createMergedSystemStubs(ctx, bootclasspath)
Anton Hansson95e89a82022-01-28 11:31:50 +0000293 createMergedFrameworkModuleLibStubs(ctx, bootclasspath)
294 createMergedFrameworkImpl(ctx, bootclasspath)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000295
Anton Hansson0c5d9ab2022-06-23 07:58:51 +0000296 createMergedPublicAnnotationsFilegroup(ctx, bootclasspath)
Anton Hanssoncc18e032022-01-12 14:45:22 +0000297
Anton Hansson05e944d2022-01-13 12:26:30 +0000298 createFilteredApiVersions(ctx, bootclasspath)
Anton Hansson4468d7c2022-01-14 12:10:01 +0000299
300 createPublicStubsSourceFilegroup(ctx, bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100301}
302
303func combinedApisModuleFactory() android.Module {
304 module := &CombinedApis{}
305 module.AddProperties(&module.properties)
306 android.InitAndroidModule(module)
307 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) })
308 return module
309}
Anton Hanssonfd316452022-01-14 11:15:52 +0000310
311// Various utility methods below.
312
313// Creates an array of ":<m><tag>" for each m in <modules>.
314func createSrcs(modules []string, tag string) []string {
315 return transformArray(modules, ":", tag)
316}
317
318// Creates an array of "<prefix><m><suffix>", for each m in <modules>.
319func transformArray(modules []string, prefix, suffix string) []string {
320 a := make([]string, 0, len(modules))
321 for _, module := range modules {
322 a = append(a, prefix+module+suffix)
323 }
324 return a
325}
326
327func removeAll(s []string, vs []string) []string {
328 for _, v := range vs {
329 s = remove(s, v)
330 }
331 return s
332}
333
334func remove(s []string, v string) []string {
335 s2 := make([]string, 0, len(s))
336 for _, sv := range s {
337 if sv != v {
338 s2 = append(s2, sv)
339 }
340 }
341 return s2
342}