blob: 94bccaaa8250f90d3b1856d072d8184fce60a08e [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 Hansson23274ee2022-01-28 11:31:50 +000030var core_libraries_modules = []string{art, conscrypt, i18n}
Anton Hansson05e944d2022-01-13 12:26:30 +000031
Anton Hansson0860aaf2021-10-08 16:48:03 +010032// The intention behind this soong plugin is to generate a number of "merged"
33// API-related modules that would otherwise require a large amount of very
34// similar Android.bp boilerplate to define. For example, the merged current.txt
35// API definitions (created by merging the non-updatable current.txt with all
36// the module current.txts). This simplifies the addition of new android
37// modules, by reducing the number of genrules etc a new module must be added to.
38
39// The properties of the combined_apis module type.
40type CombinedApisProperties struct {
Anton Hansson16ff3572022-01-11 18:36:35 +000041 // Module libraries in the bootclasspath
42 Bootclasspath []string
Anton Hansson07a12952022-01-12 17:28:39 +000043 // Module libraries on the bootclasspath if include_nonpublic_framework_api is true.
44 Conditional_bootclasspath []string
Anton Hansson16ff3572022-01-11 18:36:35 +000045 // Module libraries in system server
46 System_server_classpath []string
Anton Hansson0860aaf2021-10-08 16:48:03 +010047}
48
49type CombinedApis struct {
50 android.ModuleBase
51
52 properties CombinedApisProperties
53}
54
55func init() {
56 registerBuildComponents(android.InitRegistrationContext)
57}
58
59func registerBuildComponents(ctx android.RegistrationContext) {
60 ctx.RegisterModuleType("combined_apis", combinedApisModuleFactory)
61}
62
63var PrepareForCombinedApisTest = android.FixtureRegisterWithContext(registerBuildComponents)
64
65func (a *CombinedApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
66}
67
68type genruleProps struct {
69 Name *string
70 Cmd *string
71 Dists []android.Dist
72 Out []string
73 Srcs []string
74 Tools []string
75 Visibility []string
76}
77
Anton Hanssoncb00f942022-01-13 09:45:12 +000078type libraryProps struct {
79 Name *string
80 Sdk_version *string
81 Static_libs []string
82 Visibility []string
83}
84
Anton Hansson4468d7c2022-01-14 12:10:01 +000085type fgProps struct {
86 Name *string
87 Srcs []string
88 Visibility []string
89}
90
Anton Hansson0860aaf2021-10-08 16:48:03 +010091// Struct to pass parameters for the various merged [current|removed].txt file modules we create.
92type MergedTxtDefinition struct {
93 // "current.txt" or "removed.txt"
94 TxtFilename string
95 // The module for the non-updatable / non-module part of the api.
96 BaseTxt string
97 // The list of modules that are relevant for this merged txt.
98 Modules []string
99 // The output tag for each module to use.e.g. {.public.api.txt} for current.txt
100 ModuleTag string
101 // public, system, module-lib or system-server
102 Scope string
103}
104
105func createMergedTxt(ctx android.LoadHookContext, txt MergedTxtDefinition) {
106 metalavaCmd := "$(location metalava)"
107 // Silence reflection warnings. See b/168689341
108 metalavaCmd += " -J--add-opens=java.base/java.util=ALL-UNNAMED "
109 metalavaCmd += " --quiet --no-banner --format=v2 "
110
111 filename := txt.TxtFilename
112 if txt.Scope != "public" {
113 filename = txt.Scope + "-" + filename
114 }
115
116 props := genruleProps{}
117 props.Name = proptools.StringPtr(ctx.ModuleName() + "-" + filename)
118 props.Tools = []string{"metalava"}
Anton Hansson16ff3572022-01-11 18:36:35 +0000119 props.Out = []string{filename}
Anton Hansson0860aaf2021-10-08 16:48:03 +0100120 props.Cmd = proptools.StringPtr(metalavaCmd + "$(in) --api $(out)")
Anton Hanssonfd316452022-01-14 11:15:52 +0000121 props.Srcs = append([]string{txt.BaseTxt}, createSrcs(txt.Modules, txt.ModuleTag)...)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100122 props.Dists = []android.Dist{
123 {
124 Targets: []string{"droidcore"},
125 Dir: proptools.StringPtr("api"),
126 Dest: proptools.StringPtr(filename),
127 },
128 {
129 Targets: []string{"sdk"},
130 Dir: proptools.StringPtr("apistubs/android/" + txt.Scope + "/api"),
131 Dest: proptools.StringPtr(txt.TxtFilename),
132 },
133 }
134 props.Visibility = []string{"//visibility:public"}
135 ctx.CreateModule(genrule.GenRuleFactory, &props)
136}
137
138func createMergedStubsSrcjar(ctx android.LoadHookContext, modules []string) {
139 props := genruleProps{}
140 props.Name = proptools.StringPtr(ctx.ModuleName() + "-current.srcjar")
141 props.Tools = []string{"merge_zips"}
142 props.Out = []string{"current.srcjar"}
143 props.Cmd = proptools.StringPtr("$(location merge_zips) $(out) $(in)")
Anton Hanssonfd316452022-01-14 11:15:52 +0000144 props.Srcs = append([]string{":api-stubs-docs-non-updatable"}, createSrcs(modules, "{.public.stubs.source}")...)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100145 props.Visibility = []string{"//visibility:private"} // Used by make module in //development, mind
146 ctx.CreateModule(genrule.GenRuleFactory, &props)
147}
148
Anton Hanssoncc18e032022-01-12 14:45:22 +0000149// This produces the same annotations.zip as framework-doc-stubs, but by using
150// outputs from individual modules instead of all the source code.
151func createMergedAnnotations(ctx android.LoadHookContext, modules []string) {
152 props := genruleProps{}
153 props.Name = proptools.StringPtr("sdk-annotations.zip")
154 props.Tools = []string{"merge_annotation_zips", "soong_zip"}
155 props.Out = []string{"annotations.zip"}
156 props.Cmd = proptools.StringPtr("$(location merge_annotation_zips) $(genDir)/out $(in) && " +
157 "$(location soong_zip) -o $(out) -C $(genDir)/out -D $(genDir)/out")
Anton Hanssonfd316452022-01-14 11:15:52 +0000158 props.Srcs = append([]string{":android-non-updatable-doc-stubs{.annotations.zip}"}, createSrcs(modules, "{.public.annotations.zip}")...)
Anton Hanssoncc18e032022-01-12 14:45:22 +0000159 ctx.CreateModule(genrule.GenRuleFactory, &props)
160}
161
Anton Hansson0860aaf2021-10-08 16:48:03 +0100162func createFilteredApiVersions(ctx android.LoadHookContext, modules []string) {
Anton Hansson05e944d2022-01-13 12:26:30 +0000163 // For the filtered api versions, we prune all APIs except art module's APIs. because
164 // 1) ART apis are available by default to all modules, while other module-to-module deps are
165 // explicit and probably receive more scrutiny anyway
166 // 2) The number of ART/libcore APIs is large, so not linting them would create a large gap
167 // 3) It's a compromise. Ideally we wouldn't be filtering out any module APIs, and have
168 // per-module lint databases that excludes just that module's APIs. Alas, that's more
169 // difficult to achieve.
170 modules = remove(modules, art)
171
Anton Hansson0860aaf2021-10-08 16:48:03 +0100172 props := genruleProps{}
173 props.Name = proptools.StringPtr("api-versions-xml-public-filtered")
174 props.Tools = []string{"api_versions_trimmer"}
175 props.Out = []string{"api-versions-public-filtered.xml"}
176 props.Cmd = proptools.StringPtr("$(location api_versions_trimmer) $(out) $(in)")
177 // Note: order matters: first parameter is the full api-versions.xml
178 // after that the stubs files in any order
179 // stubs files are all modules that export API surfaces EXCEPT ART
Anton Hanssonfd316452022-01-14 11:15:52 +0000180 props.Srcs = append([]string{":framework-doc-stubs{.api_versions.xml}"}, createSrcs(modules, ".stubs{.jar}")...)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100181 props.Dists = []android.Dist{{Targets: []string{"sdk"}}}
182 ctx.CreateModule(genrule.GenRuleFactory, &props)
183}
184
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000185func createMergedPublicStubs(ctx android.LoadHookContext, modules []string) {
186 props := libraryProps{}
187 props.Name = proptools.StringPtr("all-modules-public-stubs")
188 props.Static_libs = transformArray(modules, "", ".stubs")
189 props.Sdk_version = proptools.StringPtr("module_current")
190 props.Visibility = []string{"//frameworks/base"}
191 ctx.CreateModule(java.LibraryFactory, &props)
192}
193
194func createMergedSystemStubs(ctx android.LoadHookContext, modules []string) {
195 props := libraryProps{}
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000196 props.Name = proptools.StringPtr("all-modules-system-stubs")
Paul Duffin57533b42022-01-25 16:25:35 +0000197 props.Static_libs = transformArray(modules, "", ".stubs.system")
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000198 props.Sdk_version = proptools.StringPtr("module_current")
199 props.Visibility = []string{"//frameworks/base"}
200 ctx.CreateModule(java.LibraryFactory, &props)
201}
202
Anton Hansson23274ee2022-01-28 11:31:50 +0000203func createMergedFrameworkImpl(ctx android.LoadHookContext, modules []string) {
204 // This module is for the "framework-all" module, which should not include the core libraries.
205 modules = removeAll(modules, core_libraries_modules)
Anton Hansson23274ee2022-01-28 11:31:50 +0000206 props := libraryProps{}
207 props.Name = proptools.StringPtr("all-framework-module-impl")
208 props.Static_libs = transformArray(modules, "", ".impl")
209 // Media module's impl jar is called "updatable-media"
210 for i, v := range props.Static_libs {
211 if v == "framework-media.impl" {
212 props.Static_libs[i] = "updatable-media"
213 }
214 }
215 props.Sdk_version = proptools.StringPtr("module_current")
216 props.Visibility = []string{"//frameworks/base"}
217 ctx.CreateModule(java.LibraryFactory, &props)
218}
219
220func createMergedFrameworkModuleLibStubs(ctx android.LoadHookContext, modules []string) {
Anton Hanssoncb00f942022-01-13 09:45:12 +0000221 // The user of this module compiles against the "core" SDK, so remove core libraries to avoid dupes.
Anton Hansson23274ee2022-01-28 11:31:50 +0000222 modules = removeAll(modules, core_libraries_modules)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000223 props := libraryProps{}
224 props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api")
Anton Hanssonfd316452022-01-14 11:15:52 +0000225 props.Static_libs = transformArray(modules, "", ".stubs.module_lib")
Anton Hanssoncb00f942022-01-13 09:45:12 +0000226 props.Sdk_version = proptools.StringPtr("module_current")
227 props.Visibility = []string{"//frameworks/base"}
228 ctx.CreateModule(java.LibraryFactory, &props)
229}
230
Anton Hansson4468d7c2022-01-14 12:10:01 +0000231func createPublicStubsSourceFilegroup(ctx android.LoadHookContext, modules []string) {
232 props := fgProps{}
233 props.Name = proptools.StringPtr("all-modules-public-stubs-source")
234 props.Srcs = createSrcs(modules, "{.public.stubs.source}")
235 props.Visibility = []string{"//frameworks/base"}
236 ctx.CreateModule(android.FileGroupFactory, &props)
237}
238
Anton Hansson07a12952022-01-12 17:28:39 +0000239func createMergedTxts(ctx android.LoadHookContext, bootclasspath, system_server_classpath []string) {
Anton Hansson0860aaf2021-10-08 16:48:03 +0100240 var textFiles []MergedTxtDefinition
Anton Hansson16ff3572022-01-11 18:36:35 +0000241
Anton Hansson0860aaf2021-10-08 16:48:03 +0100242 tagSuffix := []string{".api.txt}", ".removed-api.txt}"}
243 for i, f := range []string{"current.txt", "removed.txt"} {
244 textFiles = append(textFiles, MergedTxtDefinition{
245 TxtFilename: f,
246 BaseTxt: ":non-updatable-" + f,
Anton Hansson07a12952022-01-12 17:28:39 +0000247 Modules: bootclasspath,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100248 ModuleTag: "{.public" + tagSuffix[i],
249 Scope: "public",
250 })
251 textFiles = append(textFiles, MergedTxtDefinition{
252 TxtFilename: f,
253 BaseTxt: ":non-updatable-system-" + f,
Paul Duffin57533b42022-01-25 16:25:35 +0000254 Modules: bootclasspath,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100255 ModuleTag: "{.system" + tagSuffix[i],
256 Scope: "system",
257 })
258 textFiles = append(textFiles, MergedTxtDefinition{
259 TxtFilename: f,
260 BaseTxt: ":non-updatable-module-lib-" + f,
Paul Duffin57533b42022-01-25 16:25:35 +0000261 Modules: bootclasspath,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100262 ModuleTag: "{.module-lib" + tagSuffix[i],
263 Scope: "module-lib",
264 })
265 textFiles = append(textFiles, MergedTxtDefinition{
266 TxtFilename: f,
267 BaseTxt: ":non-updatable-system-server-" + f,
Anton Hansson07a12952022-01-12 17:28:39 +0000268 Modules: system_server_classpath,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100269 ModuleTag: "{.system-server" + tagSuffix[i],
270 Scope: "system-server",
271 })
272 }
273 for _, txt := range textFiles {
274 createMergedTxt(ctx, txt)
275 }
276}
277
278func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) {
Anton Hansson07a12952022-01-12 17:28:39 +0000279 bootclasspath := a.properties.Bootclasspath
280 if ctx.Config().VendorConfig("ANDROID").Bool("include_nonpublic_framework_api") {
281 bootclasspath = append(bootclasspath, a.properties.Conditional_bootclasspath...)
282 sort.Strings(bootclasspath)
283 }
284 createMergedTxts(ctx, bootclasspath, a.properties.System_server_classpath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100285
Anton Hansson07a12952022-01-12 17:28:39 +0000286 createMergedStubsSrcjar(ctx, bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100287
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000288 createMergedPublicStubs(ctx, bootclasspath)
289 createMergedSystemStubs(ctx, bootclasspath)
Anton Hansson23274ee2022-01-28 11:31:50 +0000290 createMergedFrameworkModuleLibStubs(ctx, bootclasspath)
291 createMergedFrameworkImpl(ctx, bootclasspath)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000292
Anton Hansson05e944d2022-01-13 12:26:30 +0000293 createMergedAnnotations(ctx, bootclasspath)
Anton Hanssoncc18e032022-01-12 14:45:22 +0000294
Anton Hansson05e944d2022-01-13 12:26:30 +0000295 createFilteredApiVersions(ctx, bootclasspath)
Anton Hansson4468d7c2022-01-14 12:10:01 +0000296
297 createPublicStubsSourceFilegroup(ctx, bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100298}
299
300func combinedApisModuleFactory() android.Module {
301 module := &CombinedApis{}
302 module.AddProperties(&module.properties)
303 android.InitAndroidModule(module)
304 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) })
305 return module
306}
Anton Hanssonfd316452022-01-14 11:15:52 +0000307
308// Various utility methods below.
309
310// Creates an array of ":<m><tag>" for each m in <modules>.
311func createSrcs(modules []string, tag string) []string {
312 return transformArray(modules, ":", tag)
313}
314
315// Creates an array of "<prefix><m><suffix>", for each m in <modules>.
316func transformArray(modules []string, prefix, suffix string) []string {
317 a := make([]string, 0, len(modules))
318 for _, module := range modules {
319 a = append(a, prefix+module+suffix)
320 }
321 return a
322}
323
324func removeAll(s []string, vs []string) []string {
325 for _, v := range vs {
326 s = remove(s, v)
327 }
328 return s
329}
330
331func remove(s []string, v string) []string {
332 s2 := make([]string, 0, len(s))
333 for _, sv := range s {
334 if sv != v {
335 s2 = append(s2, sv)
336 }
337 }
338 return s2
339}