blob: 6a6c493e041a7440e9b215d273576d4100cf2a7e [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
Cole Faustdcda3702022-10-04 14:46:35 -0700151func createMergedAnnotationsFilegroups(ctx android.LoadHookContext, modules, system_server_modules []string) {
152 for _, i := range []struct{
153 name string
154 tag string
155 modules []string
156 }{
157 {
158 name: "all-modules-public-annotations",
159 tag: "{.public.annotations.zip}",
160 modules: modules,
161 }, {
162 name: "all-modules-system-annotations",
163 tag: "{.system.annotations.zip}",
164 modules: modules,
165 }, {
166 name: "all-modules-module-lib-annotations",
167 tag: "{.module-lib.annotations.zip}",
168 modules: modules,
169 }, {
170 name: "all-modules-system-server-annotations",
171 tag: "{.system-server.annotations.zip}",
172 modules: system_server_modules,
173 },
174 } {
175 props := fgProps{}
176 props.Name = proptools.StringPtr(i.name)
177 props.Srcs = createSrcs(i.modules, i.tag)
178 ctx.CreateModule(android.FileGroupFactory, &props)
179 }
Anton Hansson74b15642022-06-23 08:27:23 +0000180}
181
Anton Hansson0860aaf2021-10-08 16:48:03 +0100182func createFilteredApiVersions(ctx android.LoadHookContext, modules []string) {
Anton Hansson05e944d2022-01-13 12:26:30 +0000183 // For the filtered api versions, we prune all APIs except art module's APIs. because
184 // 1) ART apis are available by default to all modules, while other module-to-module deps are
185 // explicit and probably receive more scrutiny anyway
186 // 2) The number of ART/libcore APIs is large, so not linting them would create a large gap
187 // 3) It's a compromise. Ideally we wouldn't be filtering out any module APIs, and have
188 // per-module lint databases that excludes just that module's APIs. Alas, that's more
189 // difficult to achieve.
190 modules = remove(modules, art)
191
Cole Faustdcda3702022-10-04 14:46:35 -0700192 for _, i := range []struct{
193 name string
194 out string
195 in string
196 }{
197 {
198 // We shouldn't need public-filtered or system-filtered.
199 // public-filtered is currently used to lint things that
200 // use the module sdk or the system server sdk, but those
201 // should be switched over to module-filtered and
202 // system-server-filtered, and then public-filtered can
203 // be removed.
204 name: "api-versions-xml-public-filtered",
205 out: "api-versions-public-filtered.xml",
206 in: ":api_versions_public{.api_versions.xml}",
207 }, {
208 name: "api-versions-xml-module-lib-filtered",
209 out: "api-versions-module-lib-filtered.xml",
210 in: ":api_versions_module_lib{.api_versions.xml}",
211 }, {
212 name: "api-versions-xml-system-server-filtered",
213 out: "api-versions-system-server-filtered.xml",
214 in: ":api_versions_system_server{.api_versions.xml}",
215 },
216 } {
217 props := genruleProps{}
218 props.Name = proptools.StringPtr(i.name)
219 props.Out = []string{i.out}
220 // Note: order matters: first parameter is the full api-versions.xml
221 // after that the stubs files in any order
222 // stubs files are all modules that export API surfaces EXCEPT ART
223 props.Srcs = append([]string{i.in}, createSrcs(modules, ".stubs{.jar}")...)
224 props.Tools = []string{"api_versions_trimmer"}
225 props.Cmd = proptools.StringPtr("$(location api_versions_trimmer) $(out) $(in)")
226 props.Dists = []android.Dist{{Targets: []string{"sdk"}}}
227 ctx.CreateModule(genrule.GenRuleFactory, &props)
228 }
Anton Hansson0860aaf2021-10-08 16:48:03 +0100229}
230
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000231func createMergedPublicStubs(ctx android.LoadHookContext, modules []string) {
232 props := libraryProps{}
233 props.Name = proptools.StringPtr("all-modules-public-stubs")
234 props.Static_libs = transformArray(modules, "", ".stubs")
235 props.Sdk_version = proptools.StringPtr("module_current")
236 props.Visibility = []string{"//frameworks/base"}
237 ctx.CreateModule(java.LibraryFactory, &props)
238}
239
240func createMergedSystemStubs(ctx android.LoadHookContext, modules []string) {
241 props := libraryProps{}
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000242 props.Name = proptools.StringPtr("all-modules-system-stubs")
Paul Duffin57533b42022-01-25 16:25:35 +0000243 props.Static_libs = transformArray(modules, "", ".stubs.system")
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000244 props.Sdk_version = proptools.StringPtr("module_current")
245 props.Visibility = []string{"//frameworks/base"}
246 ctx.CreateModule(java.LibraryFactory, &props)
247}
248
Anton Hansson95e89a82022-01-28 11:31:50 +0000249func createMergedFrameworkImpl(ctx android.LoadHookContext, modules []string) {
250 // This module is for the "framework-all" module, which should not include the core libraries.
251 modules = removeAll(modules, core_libraries_modules)
Anton Hansson95e89a82022-01-28 11:31:50 +0000252 props := libraryProps{}
253 props.Name = proptools.StringPtr("all-framework-module-impl")
254 props.Static_libs = transformArray(modules, "", ".impl")
Anton Hansson95e89a82022-01-28 11:31:50 +0000255 props.Sdk_version = proptools.StringPtr("module_current")
256 props.Visibility = []string{"//frameworks/base"}
257 ctx.CreateModule(java.LibraryFactory, &props)
258}
259
260func createMergedFrameworkModuleLibStubs(ctx android.LoadHookContext, modules []string) {
Anton Hanssoncb00f942022-01-13 09:45:12 +0000261 // The user of this module compiles against the "core" SDK, so remove core libraries to avoid dupes.
Anton Hansson95e89a82022-01-28 11:31:50 +0000262 modules = removeAll(modules, core_libraries_modules)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000263 props := libraryProps{}
264 props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api")
Anton Hanssonfd316452022-01-14 11:15:52 +0000265 props.Static_libs = transformArray(modules, "", ".stubs.module_lib")
Anton Hanssoncb00f942022-01-13 09:45:12 +0000266 props.Sdk_version = proptools.StringPtr("module_current")
267 props.Visibility = []string{"//frameworks/base"}
268 ctx.CreateModule(java.LibraryFactory, &props)
269}
270
Anton Hansson4468d7c2022-01-14 12:10:01 +0000271func createPublicStubsSourceFilegroup(ctx android.LoadHookContext, modules []string) {
272 props := fgProps{}
273 props.Name = proptools.StringPtr("all-modules-public-stubs-source")
274 props.Srcs = createSrcs(modules, "{.public.stubs.source}")
275 props.Visibility = []string{"//frameworks/base"}
276 ctx.CreateModule(android.FileGroupFactory, &props)
277}
278
Anton Hansson07a12952022-01-12 17:28:39 +0000279func createMergedTxts(ctx android.LoadHookContext, bootclasspath, system_server_classpath []string) {
Anton Hansson0860aaf2021-10-08 16:48:03 +0100280 var textFiles []MergedTxtDefinition
Anton Hansson16ff3572022-01-11 18:36:35 +0000281
Anton Hansson0860aaf2021-10-08 16:48:03 +0100282 tagSuffix := []string{".api.txt}", ".removed-api.txt}"}
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100283 distFilename := []string{"android.txt", "android-removed.txt"}
Anton Hansson0860aaf2021-10-08 16:48:03 +0100284 for i, f := range []string{"current.txt", "removed.txt"} {
285 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100286 TxtFilename: f,
287 DistFilename: distFilename[i],
288 BaseTxt: ":non-updatable-" + f,
289 Modules: bootclasspath,
290 ModuleTag: "{.public" + tagSuffix[i],
291 Scope: "public",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100292 })
293 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100294 TxtFilename: f,
295 DistFilename: distFilename[i],
296 BaseTxt: ":non-updatable-system-" + f,
297 Modules: bootclasspath,
298 ModuleTag: "{.system" + tagSuffix[i],
299 Scope: "system",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100300 })
301 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100302 TxtFilename: f,
303 DistFilename: distFilename[i],
304 BaseTxt: ":non-updatable-module-lib-" + f,
305 Modules: bootclasspath,
306 ModuleTag: "{.module-lib" + tagSuffix[i],
307 Scope: "module-lib",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100308 })
309 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100310 TxtFilename: f,
311 DistFilename: distFilename[i],
312 BaseTxt: ":non-updatable-system-server-" + f,
313 Modules: system_server_classpath,
314 ModuleTag: "{.system-server" + tagSuffix[i],
315 Scope: "system-server",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100316 })
317 }
318 for _, txt := range textFiles {
319 createMergedTxt(ctx, txt)
320 }
321}
322
323func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) {
Anton Hansson07a12952022-01-12 17:28:39 +0000324 bootclasspath := a.properties.Bootclasspath
Cole Faustdcda3702022-10-04 14:46:35 -0700325 system_server_classpath := a.properties.System_server_classpath
Anton Hansson07a12952022-01-12 17:28:39 +0000326 if ctx.Config().VendorConfig("ANDROID").Bool("include_nonpublic_framework_api") {
327 bootclasspath = append(bootclasspath, a.properties.Conditional_bootclasspath...)
328 sort.Strings(bootclasspath)
329 }
Cole Faustdcda3702022-10-04 14:46:35 -0700330 createMergedTxts(ctx, bootclasspath, system_server_classpath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100331
Anton Hansson07a12952022-01-12 17:28:39 +0000332 createMergedStubsSrcjar(ctx, bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100333
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000334 createMergedPublicStubs(ctx, bootclasspath)
335 createMergedSystemStubs(ctx, bootclasspath)
Anton Hansson95e89a82022-01-28 11:31:50 +0000336 createMergedFrameworkModuleLibStubs(ctx, bootclasspath)
337 createMergedFrameworkImpl(ctx, bootclasspath)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000338
Cole Faustdcda3702022-10-04 14:46:35 -0700339 createMergedAnnotationsFilegroups(ctx, bootclasspath, system_server_classpath)
Anton Hanssoncc18e032022-01-12 14:45:22 +0000340
Anton Hansson05e944d2022-01-13 12:26:30 +0000341 createFilteredApiVersions(ctx, bootclasspath)
Anton Hansson4468d7c2022-01-14 12:10:01 +0000342
343 createPublicStubsSourceFilegroup(ctx, bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100344}
345
346func combinedApisModuleFactory() android.Module {
347 module := &CombinedApis{}
348 module.AddProperties(&module.properties)
349 android.InitAndroidModule(module)
350 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) })
351 return module
352}
Anton Hanssonfd316452022-01-14 11:15:52 +0000353
354// Various utility methods below.
355
356// Creates an array of ":<m><tag>" for each m in <modules>.
357func createSrcs(modules []string, tag string) []string {
358 return transformArray(modules, ":", tag)
359}
360
361// Creates an array of "<prefix><m><suffix>", for each m in <modules>.
362func transformArray(modules []string, prefix, suffix string) []string {
363 a := make([]string, 0, len(modules))
364 for _, module := range modules {
365 a = append(a, prefix+module+suffix)
366 }
367 return a
368}
369
370func removeAll(s []string, vs []string) []string {
371 for _, v := range vs {
372 s = remove(s, v)
373 }
374 return s
375}
376
377func remove(s []string, v string) []string {
378 s2 := make([]string, 0, len(s))
379 for _, sv := range s {
380 if sv != v {
381 s2 = append(s2, sv)
382 }
383 }
384 return s2
385}