blob: 3b0e300c88f3cf3aa47bd7ef4795a725f5cac945 [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"
30
Anton Hansson0860aaf2021-10-08 16:48:03 +010031// The intention behind this soong plugin is to generate a number of "merged"
32// API-related modules that would otherwise require a large amount of very
33// similar Android.bp boilerplate to define. For example, the merged current.txt
34// API definitions (created by merging the non-updatable current.txt with all
35// the module current.txts). This simplifies the addition of new android
36// modules, by reducing the number of genrules etc a new module must be added to.
37
38// The properties of the combined_apis module type.
39type CombinedApisProperties struct {
Anton Hansson16ff3572022-01-11 18:36:35 +000040 // Module libraries in the bootclasspath
41 Bootclasspath []string
Anton Hansson07a12952022-01-12 17:28:39 +000042 // Module libraries on the bootclasspath if include_nonpublic_framework_api is true.
43 Conditional_bootclasspath []string
Anton Hansson16ff3572022-01-11 18:36:35 +000044 // Module libraries in system server
45 System_server_classpath []string
Anton Hansson0860aaf2021-10-08 16:48:03 +010046}
47
48type CombinedApis struct {
49 android.ModuleBase
50
51 properties CombinedApisProperties
52}
53
54func init() {
55 registerBuildComponents(android.InitRegistrationContext)
56}
57
58func registerBuildComponents(ctx android.RegistrationContext) {
59 ctx.RegisterModuleType("combined_apis", combinedApisModuleFactory)
60}
61
62var PrepareForCombinedApisTest = android.FixtureRegisterWithContext(registerBuildComponents)
63
64func (a *CombinedApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
65}
66
67type genruleProps struct {
68 Name *string
69 Cmd *string
70 Dists []android.Dist
71 Out []string
72 Srcs []string
73 Tools []string
74 Visibility []string
75}
76
Anton Hanssoncb00f942022-01-13 09:45:12 +000077type libraryProps struct {
78 Name *string
79 Sdk_version *string
80 Static_libs []string
81 Visibility []string
82}
83
Anton Hansson0860aaf2021-10-08 16:48:03 +010084// Struct to pass parameters for the various merged [current|removed].txt file modules we create.
85type MergedTxtDefinition struct {
86 // "current.txt" or "removed.txt"
87 TxtFilename string
88 // The module for the non-updatable / non-module part of the api.
89 BaseTxt string
90 // The list of modules that are relevant for this merged txt.
91 Modules []string
92 // The output tag for each module to use.e.g. {.public.api.txt} for current.txt
93 ModuleTag string
94 // public, system, module-lib or system-server
95 Scope string
96}
97
98func createMergedTxt(ctx android.LoadHookContext, txt MergedTxtDefinition) {
99 metalavaCmd := "$(location metalava)"
100 // Silence reflection warnings. See b/168689341
101 metalavaCmd += " -J--add-opens=java.base/java.util=ALL-UNNAMED "
102 metalavaCmd += " --quiet --no-banner --format=v2 "
103
104 filename := txt.TxtFilename
105 if txt.Scope != "public" {
106 filename = txt.Scope + "-" + filename
107 }
108
109 props := genruleProps{}
110 props.Name = proptools.StringPtr(ctx.ModuleName() + "-" + filename)
111 props.Tools = []string{"metalava"}
Anton Hansson16ff3572022-01-11 18:36:35 +0000112 props.Out = []string{filename}
Anton Hansson0860aaf2021-10-08 16:48:03 +0100113 props.Cmd = proptools.StringPtr(metalavaCmd + "$(in) --api $(out)")
114 props.Srcs = createSrcs(txt.BaseTxt, txt.Modules, txt.ModuleTag)
115 props.Dists = []android.Dist{
116 {
117 Targets: []string{"droidcore"},
118 Dir: proptools.StringPtr("api"),
119 Dest: proptools.StringPtr(filename),
120 },
121 {
122 Targets: []string{"sdk"},
123 Dir: proptools.StringPtr("apistubs/android/" + txt.Scope + "/api"),
124 Dest: proptools.StringPtr(txt.TxtFilename),
125 },
126 }
127 props.Visibility = []string{"//visibility:public"}
128 ctx.CreateModule(genrule.GenRuleFactory, &props)
129}
130
131func createMergedStubsSrcjar(ctx android.LoadHookContext, modules []string) {
132 props := genruleProps{}
133 props.Name = proptools.StringPtr(ctx.ModuleName() + "-current.srcjar")
134 props.Tools = []string{"merge_zips"}
135 props.Out = []string{"current.srcjar"}
136 props.Cmd = proptools.StringPtr("$(location merge_zips) $(out) $(in)")
137 props.Srcs = createSrcs(":api-stubs-docs-non-updatable", modules, "{.public.stubs.source}")
138 props.Visibility = []string{"//visibility:private"} // Used by make module in //development, mind
139 ctx.CreateModule(genrule.GenRuleFactory, &props)
140}
141
Anton Hanssoncc18e032022-01-12 14:45:22 +0000142// This produces the same annotations.zip as framework-doc-stubs, but by using
143// outputs from individual modules instead of all the source code.
144func createMergedAnnotations(ctx android.LoadHookContext, modules []string) {
Anton Hansson05e944d2022-01-13 12:26:30 +0000145 // Conscrypt and i18n currently do not enable annotations
146 modules = removeAll(modules, []string{conscrypt, i18n})
Anton Hanssoncc18e032022-01-12 14:45:22 +0000147 props := genruleProps{}
148 props.Name = proptools.StringPtr("sdk-annotations.zip")
149 props.Tools = []string{"merge_annotation_zips", "soong_zip"}
150 props.Out = []string{"annotations.zip"}
151 props.Cmd = proptools.StringPtr("$(location merge_annotation_zips) $(genDir)/out $(in) && " +
152 "$(location soong_zip) -o $(out) -C $(genDir)/out -D $(genDir)/out")
153 props.Srcs = createSrcs(":android-non-updatable-doc-stubs{.annotations.zip}", modules, "{.public.annotations.zip}")
154 ctx.CreateModule(genrule.GenRuleFactory, &props)
155}
156
Anton Hansson0860aaf2021-10-08 16:48:03 +0100157func createFilteredApiVersions(ctx android.LoadHookContext, modules []string) {
Anton Hansson05e944d2022-01-13 12:26:30 +0000158 // For the filtered api versions, we prune all APIs except art module's APIs. because
159 // 1) ART apis are available by default to all modules, while other module-to-module deps are
160 // explicit and probably receive more scrutiny anyway
161 // 2) The number of ART/libcore APIs is large, so not linting them would create a large gap
162 // 3) It's a compromise. Ideally we wouldn't be filtering out any module APIs, and have
163 // per-module lint databases that excludes just that module's APIs. Alas, that's more
164 // difficult to achieve.
165 modules = remove(modules, art)
166
Anton Hansson0860aaf2021-10-08 16:48:03 +0100167 props := genruleProps{}
168 props.Name = proptools.StringPtr("api-versions-xml-public-filtered")
169 props.Tools = []string{"api_versions_trimmer"}
170 props.Out = []string{"api-versions-public-filtered.xml"}
171 props.Cmd = proptools.StringPtr("$(location api_versions_trimmer) $(out) $(in)")
172 // Note: order matters: first parameter is the full api-versions.xml
173 // after that the stubs files in any order
174 // stubs files are all modules that export API surfaces EXCEPT ART
175 props.Srcs = createSrcs(":framework-doc-stubs{.api_versions.xml}", modules, ".stubs{.jar}")
176 props.Dists = []android.Dist{{Targets: []string{"sdk"}}}
177 ctx.CreateModule(genrule.GenRuleFactory, &props)
178}
179
Anton Hanssoncb00f942022-01-13 09:45:12 +0000180func createMergedModuleLibStubs(ctx android.LoadHookContext, modules []string) {
181 // The user of this module compiles against the "core" SDK, so remove core libraries to avoid dupes.
182 modules = removeAll(modules, []string{art, conscrypt, i18n})
183 props := libraryProps{}
184 props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api")
185 props.Static_libs = appendStr(modules, ".stubs.module_lib")
186 props.Sdk_version = proptools.StringPtr("module_current")
187 props.Visibility = []string{"//frameworks/base"}
188 ctx.CreateModule(java.LibraryFactory, &props)
189}
190
191func appendStr(modules []string, s string) []string {
192 a := make([]string, 0, len(modules))
193 for _, module := range modules {
194 a = append(a, module+s)
195 }
196 return a
197}
198
Anton Hansson0860aaf2021-10-08 16:48:03 +0100199func createSrcs(base string, modules []string, tag string) []string {
200 a := make([]string, 0, len(modules)+1)
201 a = append(a, base)
202 for _, module := range modules {
203 a = append(a, ":"+module+tag)
204 }
205 return a
206}
207
Anton Hansson05e944d2022-01-13 12:26:30 +0000208func removeAll(s []string, vs []string) []string {
209 for _, v := range vs {
210 s = remove(s, v)
211 }
212 return s
213}
214
Anton Hansson0860aaf2021-10-08 16:48:03 +0100215func remove(s []string, v string) []string {
216 s2 := make([]string, 0, len(s))
217 for _, sv := range s {
218 if sv != v {
219 s2 = append(s2, sv)
220 }
221 }
222 return s2
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 Hansson05e944d2022-01-13 12:26:30 +0000228 bcpWithSystemApi := removeAll(bootclasspath, []string{conscrypt, i18n})
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 Hanssoncb00f942022-01-13 09:45:12 +0000276 createMergedModuleLibStubs(ctx, bootclasspath)
277
Anton Hansson05e944d2022-01-13 12:26:30 +0000278 createMergedAnnotations(ctx, bootclasspath)
Anton Hanssoncc18e032022-01-12 14:45:22 +0000279
Anton Hansson05e944d2022-01-13 12:26:30 +0000280 createFilteredApiVersions(ctx, bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100281}
282
283func combinedApisModuleFactory() android.Module {
284 module := &CombinedApis{}
285 module.AddProperties(&module.properties)
286 android.InitAndroidModule(module)
287 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) })
288 return module
289}