blob: 077ab9679ec9a7494c17c33f237d1993532570cd [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.
Nikita Ioffe5593fbb2022-12-01 14:52:34 +000039// In addition, the modules in this list are allowed to contribute to test APIs
40// stubs.
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +000041var non_updatable_modules = []string{virtualization}
Anton Hansson05e944d2022-01-13 12:26:30 +000042
Anton Hansson0860aaf2021-10-08 16:48:03 +010043// The intention behind this soong plugin is to generate a number of "merged"
44// API-related modules that would otherwise require a large amount of very
45// similar Android.bp boilerplate to define. For example, the merged current.txt
46// API definitions (created by merging the non-updatable current.txt with all
47// the module current.txts). This simplifies the addition of new android
48// modules, by reducing the number of genrules etc a new module must be added to.
49
50// The properties of the combined_apis module type.
51type CombinedApisProperties struct {
Anton Hansson16ff3572022-01-11 18:36:35 +000052 // Module libraries in the bootclasspath
53 Bootclasspath []string
Anton Hansson07a12952022-01-12 17:28:39 +000054 // Module libraries on the bootclasspath if include_nonpublic_framework_api is true.
55 Conditional_bootclasspath []string
Anton Hansson16ff3572022-01-11 18:36:35 +000056 // Module libraries in system server
57 System_server_classpath []string
Anton Hansson0860aaf2021-10-08 16:48:03 +010058}
59
60type CombinedApis struct {
61 android.ModuleBase
62
63 properties CombinedApisProperties
64}
65
66func init() {
67 registerBuildComponents(android.InitRegistrationContext)
68}
69
70func registerBuildComponents(ctx android.RegistrationContext) {
71 ctx.RegisterModuleType("combined_apis", combinedApisModuleFactory)
72}
73
74var PrepareForCombinedApisTest = android.FixtureRegisterWithContext(registerBuildComponents)
75
76func (a *CombinedApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
77}
78
79type genruleProps struct {
80 Name *string
81 Cmd *string
82 Dists []android.Dist
83 Out []string
84 Srcs []string
85 Tools []string
86 Visibility []string
87}
88
Anton Hanssoncb00f942022-01-13 09:45:12 +000089type libraryProps struct {
90 Name *string
91 Sdk_version *string
92 Static_libs []string
93 Visibility []string
94}
95
Anton Hansson4468d7c2022-01-14 12:10:01 +000096type fgProps struct {
97 Name *string
98 Srcs []string
99 Visibility []string
100}
101
Anton Hansson0860aaf2021-10-08 16:48:03 +0100102// Struct to pass parameters for the various merged [current|removed].txt file modules we create.
103type MergedTxtDefinition struct {
104 // "current.txt" or "removed.txt"
105 TxtFilename string
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100106 // Filename in the new dist dir. "android.txt" or "android-removed.txt"
107 DistFilename string
Anton Hansson0860aaf2021-10-08 16:48:03 +0100108 // The module for the non-updatable / non-module part of the api.
109 BaseTxt string
110 // The list of modules that are relevant for this merged txt.
111 Modules []string
112 // The output tag for each module to use.e.g. {.public.api.txt} for current.txt
113 ModuleTag string
114 // public, system, module-lib or system-server
115 Scope string
116}
117
118func createMergedTxt(ctx android.LoadHookContext, txt MergedTxtDefinition) {
119 metalavaCmd := "$(location metalava)"
120 // Silence reflection warnings. See b/168689341
121 metalavaCmd += " -J--add-opens=java.base/java.util=ALL-UNNAMED "
122 metalavaCmd += " --quiet --no-banner --format=v2 "
123
124 filename := txt.TxtFilename
125 if txt.Scope != "public" {
126 filename = txt.Scope + "-" + filename
127 }
Anton Hansson0860aaf2021-10-08 16:48:03 +0100128 props := genruleProps{}
129 props.Name = proptools.StringPtr(ctx.ModuleName() + "-" + filename)
130 props.Tools = []string{"metalava"}
Anton Hansson16ff3572022-01-11 18:36:35 +0000131 props.Out = []string{filename}
Anton Hansson0860aaf2021-10-08 16:48:03 +0100132 props.Cmd = proptools.StringPtr(metalavaCmd + "$(in) --api $(out)")
Anton Hanssonfd316452022-01-14 11:15:52 +0000133 props.Srcs = append([]string{txt.BaseTxt}, createSrcs(txt.Modules, txt.ModuleTag)...)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100134 props.Dists = []android.Dist{
135 {
136 Targets: []string{"droidcore"},
137 Dir: proptools.StringPtr("api"),
138 Dest: proptools.StringPtr(filename),
139 },
140 {
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100141 Targets: []string{"api_txt", "sdk"},
Anton Hansson0860aaf2021-10-08 16:48:03 +0100142 Dir: proptools.StringPtr("apistubs/android/" + txt.Scope + "/api"),
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100143 Dest: proptools.StringPtr(txt.DistFilename),
Anton Hansson0860aaf2021-10-08 16:48:03 +0100144 },
145 }
146 props.Visibility = []string{"//visibility:public"}
147 ctx.CreateModule(genrule.GenRuleFactory, &props)
148}
149
Cole Faustdcda3702022-10-04 14:46:35 -0700150func createMergedAnnotationsFilegroups(ctx android.LoadHookContext, modules, system_server_modules []string) {
151 for _, i := range []struct{
152 name string
153 tag string
154 modules []string
155 }{
156 {
157 name: "all-modules-public-annotations",
158 tag: "{.public.annotations.zip}",
159 modules: modules,
160 }, {
161 name: "all-modules-system-annotations",
162 tag: "{.system.annotations.zip}",
163 modules: modules,
164 }, {
165 name: "all-modules-module-lib-annotations",
166 tag: "{.module-lib.annotations.zip}",
167 modules: modules,
168 }, {
169 name: "all-modules-system-server-annotations",
170 tag: "{.system-server.annotations.zip}",
171 modules: system_server_modules,
172 },
173 } {
174 props := fgProps{}
175 props.Name = proptools.StringPtr(i.name)
176 props.Srcs = createSrcs(i.modules, i.tag)
177 ctx.CreateModule(android.FileGroupFactory, &props)
178 }
Anton Hansson74b15642022-06-23 08:27:23 +0000179}
180
Anton Hansson0860aaf2021-10-08 16:48:03 +0100181func createFilteredApiVersions(ctx android.LoadHookContext, modules []string) {
Anton Hansson05e944d2022-01-13 12:26:30 +0000182 // For the filtered api versions, we prune all APIs except art module's APIs. because
183 // 1) ART apis are available by default to all modules, while other module-to-module deps are
184 // explicit and probably receive more scrutiny anyway
185 // 2) The number of ART/libcore APIs is large, so not linting them would create a large gap
186 // 3) It's a compromise. Ideally we wouldn't be filtering out any module APIs, and have
187 // per-module lint databases that excludes just that module's APIs. Alas, that's more
188 // difficult to achieve.
189 modules = remove(modules, art)
190
Cole Faustdcda3702022-10-04 14:46:35 -0700191 for _, i := range []struct{
192 name string
193 out string
194 in string
195 }{
196 {
197 // We shouldn't need public-filtered or system-filtered.
198 // public-filtered is currently used to lint things that
199 // use the module sdk or the system server sdk, but those
200 // should be switched over to module-filtered and
201 // system-server-filtered, and then public-filtered can
202 // be removed.
203 name: "api-versions-xml-public-filtered",
204 out: "api-versions-public-filtered.xml",
205 in: ":api_versions_public{.api_versions.xml}",
206 }, {
207 name: "api-versions-xml-module-lib-filtered",
208 out: "api-versions-module-lib-filtered.xml",
209 in: ":api_versions_module_lib{.api_versions.xml}",
210 }, {
211 name: "api-versions-xml-system-server-filtered",
212 out: "api-versions-system-server-filtered.xml",
213 in: ":api_versions_system_server{.api_versions.xml}",
214 },
215 } {
216 props := genruleProps{}
217 props.Name = proptools.StringPtr(i.name)
218 props.Out = []string{i.out}
219 // Note: order matters: first parameter is the full api-versions.xml
220 // after that the stubs files in any order
221 // stubs files are all modules that export API surfaces EXCEPT ART
222 props.Srcs = append([]string{i.in}, createSrcs(modules, ".stubs{.jar}")...)
223 props.Tools = []string{"api_versions_trimmer"}
224 props.Cmd = proptools.StringPtr("$(location api_versions_trimmer) $(out) $(in)")
225 props.Dists = []android.Dist{{Targets: []string{"sdk"}}}
226 ctx.CreateModule(genrule.GenRuleFactory, &props)
227 }
Anton Hansson0860aaf2021-10-08 16:48:03 +0100228}
229
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000230func createMergedPublicStubs(ctx android.LoadHookContext, modules []string) {
231 props := libraryProps{}
232 props.Name = proptools.StringPtr("all-modules-public-stubs")
233 props.Static_libs = transformArray(modules, "", ".stubs")
234 props.Sdk_version = proptools.StringPtr("module_current")
235 props.Visibility = []string{"//frameworks/base"}
236 ctx.CreateModule(java.LibraryFactory, &props)
237}
238
239func createMergedSystemStubs(ctx android.LoadHookContext, modules []string) {
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000240 // First create the all-updatable-modules-system-stubs
241 {
242 updatable_modules := removeAll(modules, non_updatable_modules)
243 props := libraryProps{}
244 props.Name = proptools.StringPtr("all-updatable-modules-system-stubs")
245 props.Static_libs = transformArray(updatable_modules, "", ".stubs.system")
246 props.Sdk_version = proptools.StringPtr("module_current")
247 props.Visibility = []string{"//frameworks/base"}
248 ctx.CreateModule(java.LibraryFactory, &props)
249 }
250 // Now merge all-updatable-modules-system-stubs and stubs from non-updatable modules
251 // into all-modules-system-stubs.
252 {
253 props := libraryProps{}
254 props.Name = proptools.StringPtr("all-modules-system-stubs")
255 props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.system")
256 props.Static_libs = append(props.Static_libs, "all-updatable-modules-system-stubs")
257 props.Sdk_version = proptools.StringPtr("module_current")
258 props.Visibility = []string{"//frameworks/base"}
259 ctx.CreateModule(java.LibraryFactory, &props)
260 }
261}
262
263func createMergedTestStubsForNonUpdatableModules(ctx android.LoadHookContext) {
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000264 props := libraryProps{}
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000265 props.Name = proptools.StringPtr("all-non-updatable-modules-test-stubs")
266 props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.test")
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000267 props.Sdk_version = proptools.StringPtr("module_current")
268 props.Visibility = []string{"//frameworks/base"}
269 ctx.CreateModule(java.LibraryFactory, &props)
270}
271
Anton Hansson95e89a82022-01-28 11:31:50 +0000272func createMergedFrameworkImpl(ctx android.LoadHookContext, modules []string) {
273 // This module is for the "framework-all" module, which should not include the core libraries.
274 modules = removeAll(modules, core_libraries_modules)
Nikita Ioffed3f0a6f2022-11-15 11:26:53 +0000275 // Remove the modules that belong to non-updatable APEXes since those are allowed to compile
276 // against unstable APIs.
277 modules = removeAll(modules, non_updatable_modules)
278 // First create updatable-framework-module-impl, which contains all updatable modules.
279 // This module compiles against module_lib SDK.
280 {
281 props := libraryProps{}
282 props.Name = proptools.StringPtr("updatable-framework-module-impl")
283 props.Static_libs = transformArray(modules, "", ".impl")
284 props.Sdk_version = proptools.StringPtr("module_current")
285 props.Visibility = []string{"//frameworks/base"}
286 ctx.CreateModule(java.LibraryFactory, &props)
287 }
288
289 // Now create all-framework-module-impl, which contains updatable-framework-module-impl
290 // and all non-updatable modules. This module compiles against hidden APIs.
291 {
292 props := libraryProps{}
293 props.Name = proptools.StringPtr("all-framework-module-impl")
294 props.Static_libs = transformArray(non_updatable_modules, "", ".impl")
295 props.Static_libs = append(props.Static_libs, "updatable-framework-module-impl")
296 props.Sdk_version = proptools.StringPtr("core_platform")
297 props.Visibility = []string{"//frameworks/base"}
298 ctx.CreateModule(java.LibraryFactory, &props)
299 }
Anton Hansson95e89a82022-01-28 11:31:50 +0000300}
301
302func createMergedFrameworkModuleLibStubs(ctx android.LoadHookContext, modules []string) {
Anton Hanssoncb00f942022-01-13 09:45:12 +0000303 // The user of this module compiles against the "core" SDK, so remove core libraries to avoid dupes.
Anton Hansson95e89a82022-01-28 11:31:50 +0000304 modules = removeAll(modules, core_libraries_modules)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000305 props := libraryProps{}
306 props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api")
Anton Hanssonfd316452022-01-14 11:15:52 +0000307 props.Static_libs = transformArray(modules, "", ".stubs.module_lib")
Anton Hanssoncb00f942022-01-13 09:45:12 +0000308 props.Sdk_version = proptools.StringPtr("module_current")
309 props.Visibility = []string{"//frameworks/base"}
310 ctx.CreateModule(java.LibraryFactory, &props)
311}
312
Anton Hansson4468d7c2022-01-14 12:10:01 +0000313func createPublicStubsSourceFilegroup(ctx android.LoadHookContext, modules []string) {
314 props := fgProps{}
315 props.Name = proptools.StringPtr("all-modules-public-stubs-source")
316 props.Srcs = createSrcs(modules, "{.public.stubs.source}")
317 props.Visibility = []string{"//frameworks/base"}
318 ctx.CreateModule(android.FileGroupFactory, &props)
319}
320
Anton Hansson07a12952022-01-12 17:28:39 +0000321func createMergedTxts(ctx android.LoadHookContext, bootclasspath, system_server_classpath []string) {
Anton Hansson0860aaf2021-10-08 16:48:03 +0100322 var textFiles []MergedTxtDefinition
Anton Hansson16ff3572022-01-11 18:36:35 +0000323
Anton Hansson0860aaf2021-10-08 16:48:03 +0100324 tagSuffix := []string{".api.txt}", ".removed-api.txt}"}
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100325 distFilename := []string{"android.txt", "android-removed.txt"}
Anton Hansson0860aaf2021-10-08 16:48:03 +0100326 for i, f := range []string{"current.txt", "removed.txt"} {
327 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100328 TxtFilename: f,
329 DistFilename: distFilename[i],
330 BaseTxt: ":non-updatable-" + f,
331 Modules: bootclasspath,
332 ModuleTag: "{.public" + tagSuffix[i],
333 Scope: "public",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100334 })
335 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100336 TxtFilename: f,
337 DistFilename: distFilename[i],
338 BaseTxt: ":non-updatable-system-" + f,
339 Modules: bootclasspath,
340 ModuleTag: "{.system" + tagSuffix[i],
341 Scope: "system",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100342 })
343 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100344 TxtFilename: f,
345 DistFilename: distFilename[i],
346 BaseTxt: ":non-updatable-module-lib-" + f,
347 Modules: bootclasspath,
348 ModuleTag: "{.module-lib" + tagSuffix[i],
349 Scope: "module-lib",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100350 })
351 textFiles = append(textFiles, MergedTxtDefinition{
Anton Hansson09a9c4e2022-04-08 10:59:46 +0100352 TxtFilename: f,
353 DistFilename: distFilename[i],
354 BaseTxt: ":non-updatable-system-server-" + f,
355 Modules: system_server_classpath,
356 ModuleTag: "{.system-server" + tagSuffix[i],
357 Scope: "system-server",
Anton Hansson0860aaf2021-10-08 16:48:03 +0100358 })
359 }
360 for _, txt := range textFiles {
361 createMergedTxt(ctx, txt)
362 }
363}
364
365func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) {
Anton Hansson07a12952022-01-12 17:28:39 +0000366 bootclasspath := a.properties.Bootclasspath
Cole Faustdcda3702022-10-04 14:46:35 -0700367 system_server_classpath := a.properties.System_server_classpath
Anton Hansson07a12952022-01-12 17:28:39 +0000368 if ctx.Config().VendorConfig("ANDROID").Bool("include_nonpublic_framework_api") {
369 bootclasspath = append(bootclasspath, a.properties.Conditional_bootclasspath...)
370 sort.Strings(bootclasspath)
371 }
Cole Faustdcda3702022-10-04 14:46:35 -0700372 createMergedTxts(ctx, bootclasspath, system_server_classpath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100373
Anton Hanssonc6e9d2f2022-01-25 15:53:43 +0000374 createMergedPublicStubs(ctx, bootclasspath)
375 createMergedSystemStubs(ctx, bootclasspath)
Nikita Ioffe5593fbb2022-12-01 14:52:34 +0000376 createMergedTestStubsForNonUpdatableModules(ctx)
Anton Hansson95e89a82022-01-28 11:31:50 +0000377 createMergedFrameworkModuleLibStubs(ctx, bootclasspath)
378 createMergedFrameworkImpl(ctx, bootclasspath)
Anton Hanssoncb00f942022-01-13 09:45:12 +0000379
Cole Faustdcda3702022-10-04 14:46:35 -0700380 createMergedAnnotationsFilegroups(ctx, bootclasspath, system_server_classpath)
Anton Hanssoncc18e032022-01-12 14:45:22 +0000381
Anton Hansson05e944d2022-01-13 12:26:30 +0000382 createFilteredApiVersions(ctx, bootclasspath)
Anton Hansson4468d7c2022-01-14 12:10:01 +0000383
384 createPublicStubsSourceFilegroup(ctx, bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100385}
386
387func combinedApisModuleFactory() android.Module {
388 module := &CombinedApis{}
389 module.AddProperties(&module.properties)
390 android.InitAndroidModule(module)
391 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) })
392 return module
393}
Anton Hanssonfd316452022-01-14 11:15:52 +0000394
395// Various utility methods below.
396
397// Creates an array of ":<m><tag>" for each m in <modules>.
398func createSrcs(modules []string, tag string) []string {
399 return transformArray(modules, ":", tag)
400}
401
402// Creates an array of "<prefix><m><suffix>", for each m in <modules>.
403func transformArray(modules []string, prefix, suffix string) []string {
404 a := make([]string, 0, len(modules))
405 for _, module := range modules {
406 a = append(a, prefix+module+suffix)
407 }
408 return a
409}
410
411func removeAll(s []string, vs []string) []string {
412 for _, v := range vs {
413 s = remove(s, v)
414 }
415 return s
416}
417
418func remove(s []string, v string) []string {
419 s2 := make([]string, 0, len(s))
420 for _, sv := range s {
421 if sv != v {
422 s2 = append(s2, sv)
423 }
424 }
425 return s2
426}