blob: ba0fdc18d23e68046953b0fd366a3f9ebbf84019 [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"
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +000030const virtualization = "framework-virtualization"
Anton Hansson09a9c4e2022-04-08 10:59:46 +010031
Anton Hansson95e89a82022-01-28 11:31:50 +000032var core_libraries_modules = []string{art, conscrypt, i18n}
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +000033// List of modules that are not yet updatable, and hence they can still compile
34// against hidden APIs. These modules are filtered out when building the
35// updatable-framework-module-impl (because updatable-framework-module-impl is
36// built against module_current SDK). Instead they are directly statically
37// linked into the all-framework-module-lib, which is building against hidden
38// APIs.
39var non_updatable_modules = []string{virtualization}
Anton Hansson05e944d2022-01-13 12:26:30 +000040
Anton Hansson0860aaf2021-10-08 16:48:03 +010041// The intention behind this soong plugin is to generate a number of "merged"
42// API-related modules that would otherwise require a large amount of very
43// similar Android.bp boilerplate to define. For example, the merged current.txt
44// API definitions (created by merging the non-updatable current.txt with all
45// the module current.txts). This simplifies the addition of new android
46// modules, by reducing the number of genrules etc a new module must be added to.
47
48// The properties of the combined_apis module type.
49type CombinedApisProperties struct {
Anton Hansson16ff3572022-01-11 18:36:35 +000050 // Module libraries in the bootclasspath
51 Bootclasspath []string
Anton Hansson07a12952022-01-12 17:28:39 +000052 // Module libraries on the bootclasspath if include_nonpublic_framework_api is true.
53 Conditional_bootclasspath []string
Anton Hansson16ff3572022-01-11 18:36:35 +000054 // Module libraries in system server
55 System_server_classpath []string
Anton Hansson0860aaf2021-10-08 16:48:03 +010056}
57
58type CombinedApis struct {
59 android.ModuleBase
60
61 properties CombinedApisProperties
62}
63
64func init() {
65 registerBuildComponents(android.InitRegistrationContext)
66}
67
68func registerBuildComponents(ctx android.RegistrationContext) {
69 ctx.RegisterModuleType("combined_apis", combinedApisModuleFactory)
70}
71
72var PrepareForCombinedApisTest = android.FixtureRegisterWithContext(registerBuildComponents)
73
74func (a *CombinedApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
75}
76
77type genruleProps struct {
78 Name *string
79 Cmd *string
80 Dists []android.Dist
81 Out []string
82 Srcs []string
83 Tools []string
84 Visibility []string
85}
86
Anton Hanssoncb00f942022-01-13 09:45:12 +000087type libraryProps struct {
88 Name *string
89 Sdk_version *string
90 Static_libs []string
91 Visibility []string
92}
93
Anton Hansson4468d7c2022-01-14 12:10:01 +000094type fgProps struct {
95 Name *string
96 Srcs []string
97 Visibility []string
98}
99
Anton Hansson0860aaf2021-10-08 16:48:03 +0100100// Struct to pass parameters for the various merged [current|removed].txt file modules we create.
101type MergedTxtDefinition struct {
102 // "current.txt" or "removed.txt"
103 TxtFilename string
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100104 // Filename in the new dist dir. "android.txt" or "android-removed.txt"
105 DistFilename string
Anton Hansson0860aaf2021-10-08 16:48:03 +0100106 // The module for the non-updatable / non-module part of the api.
107 BaseTxt string
108 // The list of modules that are relevant for this merged txt.
109 Modules []string
110 // The output tag for each module to use.e.g. {.public.api.txt} for current.txt
111 ModuleTag string
112 // public, system, module-lib or system-server
113 Scope string
114}
115
116func createMergedTxt(ctx android.LoadHookContext, txt MergedTxtDefinition) {
117 metalavaCmd := "$(location metalava)"
118 // Silence reflection warnings. See b/168689341
119 metalavaCmd += " -J--add-opens=java.base/java.util=ALL-UNNAMED "
120 metalavaCmd += " --quiet --no-banner --format=v2 "
121
122 filename := txt.TxtFilename
123 if txt.Scope != "public" {
124 filename = txt.Scope + "-" + filename
125 }
Anton Hansson0860aaf2021-10-08 16:48:03 +0100126 props := genruleProps{}
127 props.Name = proptools.StringPtr(ctx.ModuleName() + "-" + filename)
128 props.Tools = []string{"metalava"}
Anton Hansson16ff3572022-01-11 18:36:35 +0000129 props.Out = []string{filename}
Anton Hansson0860aaf2021-10-08 16:48:03 +0100130 props.Cmd = proptools.StringPtr(metalavaCmd + "$(in) --api $(out)")
Anton Hanssonfd316452022-01-14 11:15:52 +0000131 props.Srcs = append([]string{txt.BaseTxt}, createSrcs(txt.Modules, txt.ModuleTag)...)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100132 props.Dists = []android.Dist{
133 {
134 Targets: []string{"droidcore"},
135 Dir: proptools.StringPtr("api"),
136 Dest: proptools.StringPtr(filename),
137 },
138 {
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100139 Targets: []string{"api_txt", "sdk"},
Anton Hansson0860aaf2021-10-08 16:48:03 +0100140 Dir: proptools.StringPtr("apistubs/android/" + txt.Scope + "/api"),
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100141 Dest: proptools.StringPtr(txt.DistFilename),
Anton Hansson0860aaf2021-10-08 16:48:03 +0100142 },
143 }
144 props.Visibility = []string{"//visibility:public"}
145 ctx.CreateModule(genrule.GenRuleFactory, &props)
146}
147
148func createMergedStubsSrcjar(ctx android.LoadHookContext, modules []string) {
149 props := genruleProps{}
150 props.Name = proptools.StringPtr(ctx.ModuleName() + "-current.srcjar")
151 props.Tools = []string{"merge_zips"}
152 props.Out = []string{"current.srcjar"}
153 props.Cmd = proptools.StringPtr("$(location merge_zips) $(out) $(in)")
Anton Hanssonfd316452022-01-14 11:15:52 +0000154 props.Srcs = append([]string{":api-stubs-docs-non-updatable"}, createSrcs(modules, "{.public.stubs.source}")...)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100155 props.Visibility = []string{"//visibility:private"} // Used by make module in //development, mind
156 ctx.CreateModule(genrule.GenRuleFactory, &props)
157}
158
Cole Faustdcda3702022-10-04 14:46:35 -0700159func createMergedAnnotationsFilegroups(ctx android.LoadHookContext, modules, system_server_modules []string) {
160 for _, i := range []struct{
161 name string
162 tag string
163 modules []string
164 }{
165 {
166 name: "all-modules-public-annotations",
167 tag: "{.public.annotations.zip}",
168 modules: modules,
169 }, {
170 name: "all-modules-system-annotations",
171 tag: "{.system.annotations.zip}",
172 modules: modules,
173 }, {
174 name: "all-modules-module-lib-annotations",
175 tag: "{.module-lib.annotations.zip}",
176 modules: modules,
177 }, {
178 name: "all-modules-system-server-annotations",
179 tag: "{.system-server.annotations.zip}",
180 modules: system_server_modules,
181 },
182 } {
183 props := fgProps{}
184 props.Name = proptools.StringPtr(i.name)
185 props.Srcs = createSrcs(i.modules, i.tag)
186 ctx.CreateModule(android.FileGroupFactory, &props)
187 }
Anton Hansson74b15642022-06-23 08:27:23 +0000188}
189
Anton Hansson0860aaf2021-10-08 16:48:03 +0100190func createFilteredApiVersions(ctx android.LoadHookContext, modules []string) {
Anton Hansson05e944d2022-01-13 12:26:30 +0000191 // For the filtered api versions, we prune all APIs except art module's APIs. because
192 // 1) ART apis are available by default to all modules, while other module-to-module deps are
193 // explicit and probably receive more scrutiny anyway
194 // 2) The number of ART/libcore APIs is large, so not linting them would create a large gap
195 // 3) It's a compromise. Ideally we wouldn't be filtering out any module APIs, and have
196 // per-module lint databases that excludes just that module's APIs. Alas, that's more
197 // difficult to achieve.
198 modules = remove(modules, art)
199
Cole Faustdcda3702022-10-04 14:46:35 -0700200 for _, i := range []struct{
201 name string
202 out string
203 in string
204 }{
205 {
206 // We shouldn't need public-filtered or system-filtered.
207 // public-filtered is currently used to lint things that
208 // use the module sdk or the system server sdk, but those
209 // should be switched over to module-filtered and
210 // system-server-filtered, and then public-filtered can
211 // be removed.
212 name: "api-versions-xml-public-filtered",
213 out: "api-versions-public-filtered.xml",
214 in: ":api_versions_public{.api_versions.xml}",
215 }, {
216 name: "api-versions-xml-module-lib-filtered",
217 out: "api-versions-module-lib-filtered.xml",
218 in: ":api_versions_module_lib{.api_versions.xml}",
219 }, {
220 name: "api-versions-xml-system-server-filtered",
221 out: "api-versions-system-server-filtered.xml",
222 in: ":api_versions_system_server{.api_versions.xml}",
223 },
224 } {
225 props := genruleProps{}
226 props.Name = proptools.StringPtr(i.name)
227 props.Out = []string{i.out}
228 // Note: order matters: first parameter is the full api-versions.xml
229 // after that the stubs files in any order
230 // stubs files are all modules that export API surfaces EXCEPT ART
231 props.Srcs = append([]string{i.in}, createSrcs(modules, ".stubs{.jar}")...)
232 props.Tools = []string{"api_versions_trimmer"}
233 props.Cmd = proptools.StringPtr("$(location api_versions_trimmer) $(out) $(in)")
234 props.Dists = []android.Dist{{Targets: []string{"sdk"}}}
235 ctx.CreateModule(genrule.GenRuleFactory, &props)
236 }
Anton Hansson0860aaf2021-10-08 16:48:03 +0100237}
238
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000239func createMergedPublicStubs(ctx android.LoadHookContext, modules []string) {
240 props := libraryProps{}
241 props.Name = proptools.StringPtr("all-modules-public-stubs")
242 props.Static_libs = transformArray(modules, "", ".stubs")
243 props.Sdk_version = proptools.StringPtr("module_current")
244 props.Visibility = []string{"//frameworks/base"}
245 ctx.CreateModule(java.LibraryFactory, &props)
246}
247
248func createMergedSystemStubs(ctx android.LoadHookContext, modules []string) {
249 props := libraryProps{}
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000250 props.Name = proptools.StringPtr("all-modules-system-stubs")
Paul Duffin57533b42022-01-25 16:25:35 +0000251 props.Static_libs = transformArray(modules, "", ".stubs.system")
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000252 props.Sdk_version = proptools.StringPtr("module_current")
253 props.Visibility = []string{"//frameworks/base"}
254 ctx.CreateModule(java.LibraryFactory, &props)
255}
256
Anton Hansson95e89a82022-01-28 11:31:50 +0000257func createMergedFrameworkImpl(ctx android.LoadHookContext, modules []string) {
258 // This module is for the "framework-all" module, which should not include the core libraries.
259 modules = removeAll(modules, core_libraries_modules)
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +0000260 // Remove the modules that belong to non-updatable APEXes since those are allowed to compile
261 // against unstable APIs.
262 modules = removeAll(modules, non_updatable_modules)
263 // First create updatable-framework-module-impl, which contains all updatable modules.
264 // This module compiles against module_lib SDK.
265 {
266 props := libraryProps{}
267 props.Name = proptools.StringPtr("updatable-framework-module-impl")
268 props.Static_libs = transformArray(modules, "", ".impl")
269 props.Sdk_version = proptools.StringPtr("module_current")
270 props.Visibility = []string{"//frameworks/base"}
271 ctx.CreateModule(java.LibraryFactory, &props)
272 }
273
274 // Now create all-framework-module-impl, which contains updatable-framework-module-impl
275 // and all non-updatable modules. This module compiles against hidden APIs.
276 {
277 props := libraryProps{}
278 props.Name = proptools.StringPtr("all-framework-module-impl")
279 props.Static_libs = transformArray(non_updatable_modules, "", ".impl")
280 props.Static_libs = append(props.Static_libs, "updatable-framework-module-impl")
281 props.Sdk_version = proptools.StringPtr("core_platform")
282 props.Visibility = []string{"//frameworks/base"}
283 ctx.CreateModule(java.LibraryFactory, &props)
284 }
Anton Hansson95e89a82022-01-28 11:31:50 +0000285}
286
287func createMergedFrameworkModuleLibStubs(ctx android.LoadHookContext, modules []string) {
Anton Hanssoncb00f942022-01-13 09:45:12 +0000288 // The user of this module compiles against the "core" SDK, so remove core libraries to avoid dupes.
Anton Hansson95e89a82022-01-28 11:31:50 +0000289 modules = removeAll(modules, core_libraries_modules)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000290 props := libraryProps{}
291 props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api")
Anton Hanssonfd316452022-01-14 11:15:52 +0000292 props.Static_libs = transformArray(modules, "", ".stubs.module_lib")
Anton Hanssoncb00f942022-01-13 09:45:12 +0000293 props.Sdk_version = proptools.StringPtr("module_current")
294 props.Visibility = []string{"//frameworks/base"}
295 ctx.CreateModule(java.LibraryFactory, &props)
296}
297
Anton Hansson4468d7c2022-01-14 12:10:01 +0000298func createPublicStubsSourceFilegroup(ctx android.LoadHookContext, modules []string) {
299 props := fgProps{}
300 props.Name = proptools.StringPtr("all-modules-public-stubs-source")
301 props.Srcs = createSrcs(modules, "{.public.stubs.source}")
302 props.Visibility = []string{"//frameworks/base"}
303 ctx.CreateModule(android.FileGroupFactory, &props)
304}
305
Anton Hansson07a12952022-01-12 17:28:39 +0000306func createMergedTxts(ctx android.LoadHookContext, bootclasspath, system_server_classpath []string) {
Anton Hansson0860aaf2021-10-08 16:48:03 +0100307 var textFiles []MergedTxtDefinition
Anton Hansson16ff3572022-01-11 18:36:35 +0000308
Anton Hansson0860aaf2021-10-08 16:48:03 +0100309 tagSuffix := []string{".api.txt}", ".removed-api.txt}"}
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100310 distFilename := []string{"android.txt", "android-removed.txt"}
Anton Hansson0860aaf2021-10-08 16:48:03 +0100311 for i, f := range []string{"current.txt", "removed.txt"} {
312 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100313 TxtFilename: f,
314 DistFilename: distFilename[i],
315 BaseTxt: ":non-updatable-" + f,
316 Modules: bootclasspath,
317 ModuleTag: "{.public" + tagSuffix[i],
318 Scope: "public",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100319 })
320 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100321 TxtFilename: f,
322 DistFilename: distFilename[i],
323 BaseTxt: ":non-updatable-system-" + f,
324 Modules: bootclasspath,
325 ModuleTag: "{.system" + tagSuffix[i],
326 Scope: "system",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100327 })
328 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100329 TxtFilename: f,
330 DistFilename: distFilename[i],
331 BaseTxt: ":non-updatable-module-lib-" + f,
332 Modules: bootclasspath,
333 ModuleTag: "{.module-lib" + tagSuffix[i],
334 Scope: "module-lib",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100335 })
336 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100337 TxtFilename: f,
338 DistFilename: distFilename[i],
339 BaseTxt: ":non-updatable-system-server-" + f,
340 Modules: system_server_classpath,
341 ModuleTag: "{.system-server" + tagSuffix[i],
342 Scope: "system-server",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100343 })
344 }
345 for _, txt := range textFiles {
346 createMergedTxt(ctx, txt)
347 }
348}
349
350func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) {
Anton Hansson07a12952022-01-12 17:28:39 +0000351 bootclasspath := a.properties.Bootclasspath
Cole Faustdcda3702022-10-04 14:46:35 -0700352 system_server_classpath := a.properties.System_server_classpath
Anton Hansson07a12952022-01-12 17:28:39 +0000353 if ctx.Config().VendorConfig("ANDROID").Bool("include_nonpublic_framework_api") {
354 bootclasspath = append(bootclasspath, a.properties.Conditional_bootclasspath...)
355 sort.Strings(bootclasspath)
356 }
Cole Faustdcda3702022-10-04 14:46:35 -0700357 createMergedTxts(ctx, bootclasspath, system_server_classpath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100358
Anton Hansson07a12952022-01-12 17:28:39 +0000359 createMergedStubsSrcjar(ctx, bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100360
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000361 createMergedPublicStubs(ctx, bootclasspath)
362 createMergedSystemStubs(ctx, bootclasspath)
Anton Hansson95e89a82022-01-28 11:31:50 +0000363 createMergedFrameworkModuleLibStubs(ctx, bootclasspath)
364 createMergedFrameworkImpl(ctx, bootclasspath)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000365
Cole Faustdcda3702022-10-04 14:46:35 -0700366 createMergedAnnotationsFilegroups(ctx, bootclasspath, system_server_classpath)
Anton Hanssoncc18e032022-01-12 14:45:22 +0000367
Anton Hansson05e944d2022-01-13 12:26:30 +0000368 createFilteredApiVersions(ctx, bootclasspath)
Anton Hansson4468d7c2022-01-14 12:10:01 +0000369
370 createPublicStubsSourceFilegroup(ctx, bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100371}
372
373func combinedApisModuleFactory() android.Module {
374 module := &CombinedApis{}
375 module.AddProperties(&module.properties)
376 android.InitAndroidModule(module)
377 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) })
378 return module
379}
Anton Hanssonfd316452022-01-14 11:15:52 +0000380
381// Various utility methods below.
382
383// Creates an array of ":<m><tag>" for each m in <modules>.
384func createSrcs(modules []string, tag string) []string {
385 return transformArray(modules, ":", tag)
386}
387
388// Creates an array of "<prefix><m><suffix>", for each m in <modules>.
389func transformArray(modules []string, prefix, suffix string) []string {
390 a := make([]string, 0, len(modules))
391 for _, module := range modules {
392 a = append(a, prefix+module+suffix)
393 }
394 return a
395}
396
397func removeAll(s []string, vs []string) []string {
398 for _, v := range vs {
399 s = remove(s, v)
400 }
401 return s
402}
403
404func remove(s []string, v string) []string {
405 s2 := make([]string, 0, len(s))
406 for _, sv := range s {
407 if sv != v {
408 s2 = append(s2, sv)
409 }
410 }
411 return s2
412}