blob: 50db83e60a36c77c0018f4428202143ed3e74358 [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 Hanssonc6e9d2f2022-01-25 15:53:43 +000030var modules_with_only_public_scope = []string{i18n, conscrypt}
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{}
196 modules_with_system_stubs := removeAll(modules, modules_with_only_public_scope)
197 props.Name = proptools.StringPtr("all-modules-system-stubs")
198 props.Static_libs = append(
199 transformArray(modules_with_only_public_scope, "", ".stubs"),
200 transformArray(modules_with_system_stubs, "", ".stubs.system")...)
201 props.Sdk_version = proptools.StringPtr("module_current")
202 props.Visibility = []string{"//frameworks/base"}
203 ctx.CreateModule(java.LibraryFactory, &props)
204}
205
Anton Hanssoncb00f942022-01-13 09:45:12 +0000206func createMergedModuleLibStubs(ctx android.LoadHookContext, modules []string) {
207 // The user of this module compiles against the "core" SDK, so remove core libraries to avoid dupes.
208 modules = removeAll(modules, []string{art, conscrypt, i18n})
209 props := libraryProps{}
210 props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api")
Anton Hanssonfd316452022-01-14 11:15:52 +0000211 props.Static_libs = transformArray(modules, "", ".stubs.module_lib")
Anton Hanssoncb00f942022-01-13 09:45:12 +0000212 props.Sdk_version = proptools.StringPtr("module_current")
213 props.Visibility = []string{"//frameworks/base"}
214 ctx.CreateModule(java.LibraryFactory, &props)
215}
216
Anton Hansson4468d7c2022-01-14 12:10:01 +0000217func createPublicStubsSourceFilegroup(ctx android.LoadHookContext, modules []string) {
218 props := fgProps{}
219 props.Name = proptools.StringPtr("all-modules-public-stubs-source")
220 props.Srcs = createSrcs(modules, "{.public.stubs.source}")
221 props.Visibility = []string{"//frameworks/base"}
222 ctx.CreateModule(android.FileGroupFactory, &props)
223}
224
Anton Hansson07a12952022-01-12 17:28:39 +0000225func createMergedTxts(ctx android.LoadHookContext, bootclasspath, system_server_classpath []string) {
Anton Hansson0860aaf2021-10-08 16:48:03 +0100226 var textFiles []MergedTxtDefinition
Anton Hansson16ff3572022-01-11 18:36:35 +0000227 // Two module libraries currently do not support @SystemApi so only have the public scope.
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000228 bcpWithSystemApi := removeAll(bootclasspath, modules_with_only_public_scope)
Anton Hansson16ff3572022-01-11 18:36:35 +0000229
Anton Hansson0860aaf2021-10-08 16:48:03 +0100230 tagSuffix := []string{".api.txt}", ".removed-api.txt}"}
231 for i, f := range []string{"current.txt", "removed.txt"} {
232 textFiles = append(textFiles, MergedTxtDefinition{
233 TxtFilename: f,
234 BaseTxt: ":non-updatable-" + f,
Anton Hansson07a12952022-01-12 17:28:39 +0000235 Modules: bootclasspath,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100236 ModuleTag: "{.public" + tagSuffix[i],
237 Scope: "public",
238 })
239 textFiles = append(textFiles, MergedTxtDefinition{
240 TxtFilename: f,
241 BaseTxt: ":non-updatable-system-" + f,
Anton Hansson16ff3572022-01-11 18:36:35 +0000242 Modules: bcpWithSystemApi,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100243 ModuleTag: "{.system" + tagSuffix[i],
244 Scope: "system",
245 })
246 textFiles = append(textFiles, MergedTxtDefinition{
247 TxtFilename: f,
248 BaseTxt: ":non-updatable-module-lib-" + f,
Anton Hansson16ff3572022-01-11 18:36:35 +0000249 Modules: bcpWithSystemApi,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100250 ModuleTag: "{.module-lib" + tagSuffix[i],
251 Scope: "module-lib",
252 })
253 textFiles = append(textFiles, MergedTxtDefinition{
254 TxtFilename: f,
255 BaseTxt: ":non-updatable-system-server-" + f,
Anton Hansson07a12952022-01-12 17:28:39 +0000256 Modules: system_server_classpath,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100257 ModuleTag: "{.system-server" + tagSuffix[i],
258 Scope: "system-server",
259 })
260 }
261 for _, txt := range textFiles {
262 createMergedTxt(ctx, txt)
263 }
264}
265
266func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) {
Anton Hansson07a12952022-01-12 17:28:39 +0000267 bootclasspath := a.properties.Bootclasspath
268 if ctx.Config().VendorConfig("ANDROID").Bool("include_nonpublic_framework_api") {
269 bootclasspath = append(bootclasspath, a.properties.Conditional_bootclasspath...)
270 sort.Strings(bootclasspath)
271 }
272 createMergedTxts(ctx, bootclasspath, a.properties.System_server_classpath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100273
Anton Hansson07a12952022-01-12 17:28:39 +0000274 createMergedStubsSrcjar(ctx, bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100275
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000276 createMergedPublicStubs(ctx, bootclasspath)
277 createMergedSystemStubs(ctx, bootclasspath)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000278 createMergedModuleLibStubs(ctx, bootclasspath)
279
Anton Hansson05e944d2022-01-13 12:26:30 +0000280 createMergedAnnotations(ctx, bootclasspath)
Anton Hanssoncc18e032022-01-12 14:45:22 +0000281
Anton Hansson05e944d2022-01-13 12:26:30 +0000282 createFilteredApiVersions(ctx, bootclasspath)
Anton Hansson4468d7c2022-01-14 12:10:01 +0000283
284 createPublicStubsSourceFilegroup(ctx, bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100285}
286
287func combinedApisModuleFactory() android.Module {
288 module := &CombinedApis{}
289 module.AddProperties(&module.properties)
290 android.InitAndroidModule(module)
291 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) })
292 return module
293}
Anton Hanssonfd316452022-01-14 11:15:52 +0000294
295// Various utility methods below.
296
297// Creates an array of ":<m><tag>" for each m in <modules>.
298func createSrcs(modules []string, tag string) []string {
299 return transformArray(modules, ":", tag)
300}
301
302// Creates an array of "<prefix><m><suffix>", for each m in <modules>.
303func transformArray(modules []string, prefix, suffix string) []string {
304 a := make([]string, 0, len(modules))
305 for _, module := range modules {
306 a = append(a, prefix+module+suffix)
307 }
308 return a
309}
310
311func removeAll(s []string, vs []string) []string {
312 for _, v := range vs {
313 s = remove(s, v)
314 }
315 return s
316}
317
318func remove(s []string, v string) []string {
319 s2 := make([]string, 0, len(s))
320 for _, sv := range s {
321 if sv != v {
322 s2 = append(s2, sv)
323 }
324 }
325 return s2
326}