blob: 5e5f60ee993fe1f9fa0a0fb4dc42f60e33282919 [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 Hansson95e89a82022-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 Hansson95e89a82022-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)
206 // TODO(b/214988855): remove the line below when framework-bluetooth has an impl jar.
207 modules = remove(modules, "framework-bluetooth")
208 props := libraryProps{}
209 props.Name = proptools.StringPtr("all-framework-module-impl")
210 props.Static_libs = transformArray(modules, "", ".impl")
211 // Media module's impl jar is called "updatable-media"
212 for i, v := range props.Static_libs {
213 if v == "framework-media.impl" {
214 props.Static_libs[i] = "updatable-media"
215 }
216 }
217 props.Sdk_version = proptools.StringPtr("module_current")
218 props.Visibility = []string{"//frameworks/base"}
219 ctx.CreateModule(java.LibraryFactory, &props)
220}
221
222func createMergedFrameworkModuleLibStubs(ctx android.LoadHookContext, modules []string) {
Anton Hanssoncb00f942022-01-13 09:45:12 +0000223 // The user of this module compiles against the "core" SDK, so remove core libraries to avoid dupes.
Anton Hansson95e89a82022-01-28 11:31:50 +0000224 modules = removeAll(modules, core_libraries_modules)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000225 props := libraryProps{}
226 props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api")
Anton Hanssonfd316452022-01-14 11:15:52 +0000227 props.Static_libs = transformArray(modules, "", ".stubs.module_lib")
Anton Hanssoncb00f942022-01-13 09:45:12 +0000228 props.Sdk_version = proptools.StringPtr("module_current")
229 props.Visibility = []string{"//frameworks/base"}
230 ctx.CreateModule(java.LibraryFactory, &props)
231}
232
Anton Hansson4468d7c2022-01-14 12:10:01 +0000233func createPublicStubsSourceFilegroup(ctx android.LoadHookContext, modules []string) {
234 props := fgProps{}
235 props.Name = proptools.StringPtr("all-modules-public-stubs-source")
236 props.Srcs = createSrcs(modules, "{.public.stubs.source}")
237 props.Visibility = []string{"//frameworks/base"}
238 ctx.CreateModule(android.FileGroupFactory, &props)
239}
240
Anton Hansson07a12952022-01-12 17:28:39 +0000241func createMergedTxts(ctx android.LoadHookContext, bootclasspath, system_server_classpath []string) {
Anton Hansson0860aaf2021-10-08 16:48:03 +0100242 var textFiles []MergedTxtDefinition
Anton Hansson16ff3572022-01-11 18:36:35 +0000243
Anton Hansson0860aaf2021-10-08 16:48:03 +0100244 tagSuffix := []string{".api.txt}", ".removed-api.txt}"}
245 for i, f := range []string{"current.txt", "removed.txt"} {
246 textFiles = append(textFiles, MergedTxtDefinition{
247 TxtFilename: f,
248 BaseTxt: ":non-updatable-" + f,
Anton Hansson07a12952022-01-12 17:28:39 +0000249 Modules: bootclasspath,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100250 ModuleTag: "{.public" + tagSuffix[i],
251 Scope: "public",
252 })
253 textFiles = append(textFiles, MergedTxtDefinition{
254 TxtFilename: f,
255 BaseTxt: ":non-updatable-system-" + f,
Paul Duffin57533b42022-01-25 16:25:35 +0000256 Modules: bootclasspath,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100257 ModuleTag: "{.system" + tagSuffix[i],
258 Scope: "system",
259 })
260 textFiles = append(textFiles, MergedTxtDefinition{
261 TxtFilename: f,
262 BaseTxt: ":non-updatable-module-lib-" + f,
Paul Duffin57533b42022-01-25 16:25:35 +0000263 Modules: bootclasspath,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100264 ModuleTag: "{.module-lib" + tagSuffix[i],
265 Scope: "module-lib",
266 })
267 textFiles = append(textFiles, MergedTxtDefinition{
268 TxtFilename: f,
269 BaseTxt: ":non-updatable-system-server-" + f,
Anton Hansson07a12952022-01-12 17:28:39 +0000270 Modules: system_server_classpath,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100271 ModuleTag: "{.system-server" + tagSuffix[i],
272 Scope: "system-server",
273 })
274 }
275 for _, txt := range textFiles {
276 createMergedTxt(ctx, txt)
277 }
278}
279
280func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) {
Anton Hansson07a12952022-01-12 17:28:39 +0000281 bootclasspath := a.properties.Bootclasspath
282 if ctx.Config().VendorConfig("ANDROID").Bool("include_nonpublic_framework_api") {
283 bootclasspath = append(bootclasspath, a.properties.Conditional_bootclasspath...)
284 sort.Strings(bootclasspath)
285 }
286 createMergedTxts(ctx, bootclasspath, a.properties.System_server_classpath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100287
Anton Hansson07a12952022-01-12 17:28:39 +0000288 createMergedStubsSrcjar(ctx, bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100289
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000290 createMergedPublicStubs(ctx, bootclasspath)
291 createMergedSystemStubs(ctx, bootclasspath)
Anton Hansson95e89a82022-01-28 11:31:50 +0000292 createMergedFrameworkModuleLibStubs(ctx, bootclasspath)
293 createMergedFrameworkImpl(ctx, bootclasspath)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000294
Anton Hansson05e944d2022-01-13 12:26:30 +0000295 createMergedAnnotations(ctx, bootclasspath)
Anton Hanssoncc18e032022-01-12 14:45:22 +0000296
Anton Hansson05e944d2022-01-13 12:26:30 +0000297 createFilteredApiVersions(ctx, bootclasspath)
Anton Hansson4468d7c2022-01-14 12:10:01 +0000298
299 createPublicStubsSourceFilegroup(ctx, bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100300}
301
302func combinedApisModuleFactory() android.Module {
303 module := &CombinedApis{}
304 module.AddProperties(&module.properties)
305 android.InitAndroidModule(module)
306 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) })
307 return module
308}
Anton Hanssonfd316452022-01-14 11:15:52 +0000309
310// Various utility methods below.
311
312// Creates an array of ":<m><tag>" for each m in <modules>.
313func createSrcs(modules []string, tag string) []string {
314 return transformArray(modules, ":", tag)
315}
316
317// Creates an array of "<prefix><m><suffix>", for each m in <modules>.
318func transformArray(modules []string, prefix, suffix string) []string {
319 a := make([]string, 0, len(modules))
320 for _, module := range modules {
321 a = append(a, prefix+module+suffix)
322 }
323 return a
324}
325
326func removeAll(s []string, vs []string) []string {
327 for _, v := range vs {
328 s = remove(s, v)
329 }
330 return s
331}
332
333func remove(s []string, v string) []string {
334 s2 := make([]string, 0, len(s))
335 for _, sv := range s {
336 if sv != v {
337 s2 = append(s2, sv)
338 }
339 }
340 return s2
341}