blob: aa9e399e7272ee795c6fd9582343a12bdab9e2fb [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) {
Anton Hansson05e944d2022-01-13 12:26:30 +0000152 // Conscrypt and i18n currently do not enable annotations
153 modules = removeAll(modules, []string{conscrypt, i18n})
Anton Hanssoncc18e032022-01-12 14:45:22 +0000154 props := genruleProps{}
155 props.Name = proptools.StringPtr("sdk-annotations.zip")
156 props.Tools = []string{"merge_annotation_zips", "soong_zip"}
157 props.Out = []string{"annotations.zip"}
158 props.Cmd = proptools.StringPtr("$(location merge_annotation_zips) $(genDir)/out $(in) && " +
159 "$(location soong_zip) -o $(out) -C $(genDir)/out -D $(genDir)/out")
Anton Hanssonfd316452022-01-14 11:15:52 +0000160 props.Srcs = append([]string{":android-non-updatable-doc-stubs{.annotations.zip}"}, createSrcs(modules, "{.public.annotations.zip}")...)
Anton Hanssoncc18e032022-01-12 14:45:22 +0000161 ctx.CreateModule(genrule.GenRuleFactory, &props)
162}
163
Anton Hansson0860aaf2021-10-08 16:48:03 +0100164func createFilteredApiVersions(ctx android.LoadHookContext, modules []string) {
Anton Hansson05e944d2022-01-13 12:26:30 +0000165 // For the filtered api versions, we prune all APIs except art module's APIs. because
166 // 1) ART apis are available by default to all modules, while other module-to-module deps are
167 // explicit and probably receive more scrutiny anyway
168 // 2) The number of ART/libcore APIs is large, so not linting them would create a large gap
169 // 3) It's a compromise. Ideally we wouldn't be filtering out any module APIs, and have
170 // per-module lint databases that excludes just that module's APIs. Alas, that's more
171 // difficult to achieve.
172 modules = remove(modules, art)
173
Anton Hansson0860aaf2021-10-08 16:48:03 +0100174 props := genruleProps{}
175 props.Name = proptools.StringPtr("api-versions-xml-public-filtered")
176 props.Tools = []string{"api_versions_trimmer"}
177 props.Out = []string{"api-versions-public-filtered.xml"}
178 props.Cmd = proptools.StringPtr("$(location api_versions_trimmer) $(out) $(in)")
179 // Note: order matters: first parameter is the full api-versions.xml
180 // after that the stubs files in any order
181 // stubs files are all modules that export API surfaces EXCEPT ART
Anton Hanssonfd316452022-01-14 11:15:52 +0000182 props.Srcs = append([]string{":framework-doc-stubs{.api_versions.xml}"}, createSrcs(modules, ".stubs{.jar}")...)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100183 props.Dists = []android.Dist{{Targets: []string{"sdk"}}}
184 ctx.CreateModule(genrule.GenRuleFactory, &props)
185}
186
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000187func createMergedPublicStubs(ctx android.LoadHookContext, modules []string) {
188 props := libraryProps{}
189 props.Name = proptools.StringPtr("all-modules-public-stubs")
190 props.Static_libs = transformArray(modules, "", ".stubs")
191 props.Sdk_version = proptools.StringPtr("module_current")
192 props.Visibility = []string{"//frameworks/base"}
193 ctx.CreateModule(java.LibraryFactory, &props)
194}
195
196func createMergedSystemStubs(ctx android.LoadHookContext, modules []string) {
197 props := libraryProps{}
198 modules_with_system_stubs := removeAll(modules, modules_with_only_public_scope)
199 props.Name = proptools.StringPtr("all-modules-system-stubs")
200 props.Static_libs = append(
201 transformArray(modules_with_only_public_scope, "", ".stubs"),
202 transformArray(modules_with_system_stubs, "", ".stubs.system")...)
203 props.Sdk_version = proptools.StringPtr("module_current")
204 props.Visibility = []string{"//frameworks/base"}
205 ctx.CreateModule(java.LibraryFactory, &props)
206}
207
Anton Hanssoncb00f942022-01-13 09:45:12 +0000208func createMergedModuleLibStubs(ctx android.LoadHookContext, modules []string) {
209 // The user of this module compiles against the "core" SDK, so remove core libraries to avoid dupes.
210 modules = removeAll(modules, []string{art, conscrypt, i18n})
211 props := libraryProps{}
212 props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api")
Anton Hanssonfd316452022-01-14 11:15:52 +0000213 props.Static_libs = transformArray(modules, "", ".stubs.module_lib")
Anton Hanssoncb00f942022-01-13 09:45:12 +0000214 props.Sdk_version = proptools.StringPtr("module_current")
215 props.Visibility = []string{"//frameworks/base"}
216 ctx.CreateModule(java.LibraryFactory, &props)
217}
218
Anton Hansson4468d7c2022-01-14 12:10:01 +0000219func createPublicStubsSourceFilegroup(ctx android.LoadHookContext, modules []string) {
220 props := fgProps{}
221 props.Name = proptools.StringPtr("all-modules-public-stubs-source")
222 props.Srcs = createSrcs(modules, "{.public.stubs.source}")
223 props.Visibility = []string{"//frameworks/base"}
224 ctx.CreateModule(android.FileGroupFactory, &props)
225}
226
Anton Hansson07a12952022-01-12 17:28:39 +0000227func createMergedTxts(ctx android.LoadHookContext, bootclasspath, system_server_classpath []string) {
Anton Hansson0860aaf2021-10-08 16:48:03 +0100228 var textFiles []MergedTxtDefinition
Anton Hansson16ff3572022-01-11 18:36:35 +0000229 // Two module libraries currently do not support @SystemApi so only have the public scope.
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000230 bcpWithSystemApi := removeAll(bootclasspath, modules_with_only_public_scope)
Anton Hansson16ff3572022-01-11 18:36:35 +0000231
Anton Hansson0860aaf2021-10-08 16:48:03 +0100232 tagSuffix := []string{".api.txt}", ".removed-api.txt}"}
233 for i, f := range []string{"current.txt", "removed.txt"} {
234 textFiles = append(textFiles, MergedTxtDefinition{
235 TxtFilename: f,
236 BaseTxt: ":non-updatable-" + f,
Anton Hansson07a12952022-01-12 17:28:39 +0000237 Modules: bootclasspath,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100238 ModuleTag: "{.public" + tagSuffix[i],
239 Scope: "public",
240 })
241 textFiles = append(textFiles, MergedTxtDefinition{
242 TxtFilename: f,
243 BaseTxt: ":non-updatable-system-" + f,
Anton Hansson16ff3572022-01-11 18:36:35 +0000244 Modules: bcpWithSystemApi,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100245 ModuleTag: "{.system" + tagSuffix[i],
246 Scope: "system",
247 })
248 textFiles = append(textFiles, MergedTxtDefinition{
249 TxtFilename: f,
250 BaseTxt: ":non-updatable-module-lib-" + f,
Anton Hansson16ff3572022-01-11 18:36:35 +0000251 Modules: bcpWithSystemApi,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100252 ModuleTag: "{.module-lib" + tagSuffix[i],
253 Scope: "module-lib",
254 })
255 textFiles = append(textFiles, MergedTxtDefinition{
256 TxtFilename: f,
257 BaseTxt: ":non-updatable-system-server-" + f,
Anton Hansson07a12952022-01-12 17:28:39 +0000258 Modules: system_server_classpath,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100259 ModuleTag: "{.system-server" + tagSuffix[i],
260 Scope: "system-server",
261 })
262 }
263 for _, txt := range textFiles {
264 createMergedTxt(ctx, txt)
265 }
266}
267
268func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) {
Anton Hansson07a12952022-01-12 17:28:39 +0000269 bootclasspath := a.properties.Bootclasspath
270 if ctx.Config().VendorConfig("ANDROID").Bool("include_nonpublic_framework_api") {
271 bootclasspath = append(bootclasspath, a.properties.Conditional_bootclasspath...)
272 sort.Strings(bootclasspath)
273 }
274 createMergedTxts(ctx, bootclasspath, a.properties.System_server_classpath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100275
Anton Hansson07a12952022-01-12 17:28:39 +0000276 createMergedStubsSrcjar(ctx, bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100277
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000278 createMergedPublicStubs(ctx, bootclasspath)
279 createMergedSystemStubs(ctx, bootclasspath)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000280 createMergedModuleLibStubs(ctx, bootclasspath)
281
Anton Hansson05e944d2022-01-13 12:26:30 +0000282 createMergedAnnotations(ctx, bootclasspath)
Anton Hanssoncc18e032022-01-12 14:45:22 +0000283
Anton Hansson05e944d2022-01-13 12:26:30 +0000284 createFilteredApiVersions(ctx, bootclasspath)
Anton Hansson4468d7c2022-01-14 12:10:01 +0000285
286 createPublicStubsSourceFilegroup(ctx, bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100287}
288
289func combinedApisModuleFactory() android.Module {
290 module := &CombinedApis{}
291 module.AddProperties(&module.properties)
292 android.InitAndroidModule(module)
293 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) })
294 return module
295}
Anton Hanssonfd316452022-01-14 11:15:52 +0000296
297// Various utility methods below.
298
299// Creates an array of ":<m><tag>" for each m in <modules>.
300func createSrcs(modules []string, tag string) []string {
301 return transformArray(modules, ":", tag)
302}
303
304// Creates an array of "<prefix><m><suffix>", for each m in <modules>.
305func transformArray(modules []string, prefix, suffix string) []string {
306 a := make([]string, 0, len(modules))
307 for _, module := range modules {
308 a = append(a, prefix+module+suffix)
309 }
310 return a
311}
312
313func removeAll(s []string, vs []string) []string {
314 for _, v := range vs {
315 s = remove(s, v)
316 }
317 return s
318}
319
320func remove(s []string, v string) []string {
321 s2 := make([]string, 0, len(s))
322 for _, sv := range s {
323 if sv != v {
324 s2 = append(s2, sv)
325 }
326 }
327 return s2
328}