blob: a61d5c2c34976ea58e5d30c296da5984edbe4101 [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 (
18 "github.com/google/blueprint/proptools"
19
20 "android/soong/android"
21 "android/soong/genrule"
22)
23
24// The intention behind this soong plugin is to generate a number of "merged"
25// API-related modules that would otherwise require a large amount of very
26// similar Android.bp boilerplate to define. For example, the merged current.txt
27// API definitions (created by merging the non-updatable current.txt with all
28// the module current.txts). This simplifies the addition of new android
29// modules, by reducing the number of genrules etc a new module must be added to.
30
31// The properties of the combined_apis module type.
32type CombinedApisProperties struct {
Anton Hansson16ff3572022-01-11 18:36:35 +000033 // Module libraries in the bootclasspath
34 Bootclasspath []string
35 // Module libraries in system server
36 System_server_classpath []string
Anton Hansson0860aaf2021-10-08 16:48:03 +010037}
38
39type CombinedApis struct {
40 android.ModuleBase
41
42 properties CombinedApisProperties
43}
44
45func init() {
46 registerBuildComponents(android.InitRegistrationContext)
47}
48
49func registerBuildComponents(ctx android.RegistrationContext) {
50 ctx.RegisterModuleType("combined_apis", combinedApisModuleFactory)
51}
52
53var PrepareForCombinedApisTest = android.FixtureRegisterWithContext(registerBuildComponents)
54
55func (a *CombinedApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
56}
57
58type genruleProps struct {
59 Name *string
60 Cmd *string
61 Dists []android.Dist
62 Out []string
63 Srcs []string
64 Tools []string
65 Visibility []string
66}
67
68// Struct to pass parameters for the various merged [current|removed].txt file modules we create.
69type MergedTxtDefinition struct {
70 // "current.txt" or "removed.txt"
71 TxtFilename string
72 // The module for the non-updatable / non-module part of the api.
73 BaseTxt string
74 // The list of modules that are relevant for this merged txt.
75 Modules []string
76 // The output tag for each module to use.e.g. {.public.api.txt} for current.txt
77 ModuleTag string
78 // public, system, module-lib or system-server
79 Scope string
80}
81
82func createMergedTxt(ctx android.LoadHookContext, txt MergedTxtDefinition) {
83 metalavaCmd := "$(location metalava)"
84 // Silence reflection warnings. See b/168689341
85 metalavaCmd += " -J--add-opens=java.base/java.util=ALL-UNNAMED "
86 metalavaCmd += " --quiet --no-banner --format=v2 "
87
88 filename := txt.TxtFilename
89 if txt.Scope != "public" {
90 filename = txt.Scope + "-" + filename
91 }
92
93 props := genruleProps{}
94 props.Name = proptools.StringPtr(ctx.ModuleName() + "-" + filename)
95 props.Tools = []string{"metalava"}
Anton Hansson16ff3572022-01-11 18:36:35 +000096 props.Out = []string{filename}
Anton Hansson0860aaf2021-10-08 16:48:03 +010097 props.Cmd = proptools.StringPtr(metalavaCmd + "$(in) --api $(out)")
98 props.Srcs = createSrcs(txt.BaseTxt, txt.Modules, txt.ModuleTag)
99 props.Dists = []android.Dist{
100 {
101 Targets: []string{"droidcore"},
102 Dir: proptools.StringPtr("api"),
103 Dest: proptools.StringPtr(filename),
104 },
105 {
106 Targets: []string{"sdk"},
107 Dir: proptools.StringPtr("apistubs/android/" + txt.Scope + "/api"),
108 Dest: proptools.StringPtr(txt.TxtFilename),
109 },
110 }
111 props.Visibility = []string{"//visibility:public"}
112 ctx.CreateModule(genrule.GenRuleFactory, &props)
113}
114
115func createMergedStubsSrcjar(ctx android.LoadHookContext, modules []string) {
116 props := genruleProps{}
117 props.Name = proptools.StringPtr(ctx.ModuleName() + "-current.srcjar")
118 props.Tools = []string{"merge_zips"}
119 props.Out = []string{"current.srcjar"}
120 props.Cmd = proptools.StringPtr("$(location merge_zips) $(out) $(in)")
121 props.Srcs = createSrcs(":api-stubs-docs-non-updatable", modules, "{.public.stubs.source}")
122 props.Visibility = []string{"//visibility:private"} // Used by make module in //development, mind
123 ctx.CreateModule(genrule.GenRuleFactory, &props)
124}
125
Anton Hanssoncc18e032022-01-12 14:45:22 +0000126// This produces the same annotations.zip as framework-doc-stubs, but by using
127// outputs from individual modules instead of all the source code.
128func createMergedAnnotations(ctx android.LoadHookContext, modules []string) {
129 props := genruleProps{}
130 props.Name = proptools.StringPtr("sdk-annotations.zip")
131 props.Tools = []string{"merge_annotation_zips", "soong_zip"}
132 props.Out = []string{"annotations.zip"}
133 props.Cmd = proptools.StringPtr("$(location merge_annotation_zips) $(genDir)/out $(in) && " +
134 "$(location soong_zip) -o $(out) -C $(genDir)/out -D $(genDir)/out")
135 props.Srcs = createSrcs(":android-non-updatable-doc-stubs{.annotations.zip}", modules, "{.public.annotations.zip}")
136 ctx.CreateModule(genrule.GenRuleFactory, &props)
137}
138
Anton Hansson0860aaf2021-10-08 16:48:03 +0100139func createFilteredApiVersions(ctx android.LoadHookContext, modules []string) {
140 props := genruleProps{}
141 props.Name = proptools.StringPtr("api-versions-xml-public-filtered")
142 props.Tools = []string{"api_versions_trimmer"}
143 props.Out = []string{"api-versions-public-filtered.xml"}
144 props.Cmd = proptools.StringPtr("$(location api_versions_trimmer) $(out) $(in)")
145 // Note: order matters: first parameter is the full api-versions.xml
146 // after that the stubs files in any order
147 // stubs files are all modules that export API surfaces EXCEPT ART
148 props.Srcs = createSrcs(":framework-doc-stubs{.api_versions.xml}", modules, ".stubs{.jar}")
149 props.Dists = []android.Dist{{Targets: []string{"sdk"}}}
150 ctx.CreateModule(genrule.GenRuleFactory, &props)
151}
152
153func createSrcs(base string, modules []string, tag string) []string {
154 a := make([]string, 0, len(modules)+1)
155 a = append(a, base)
156 for _, module := range modules {
157 a = append(a, ":"+module+tag)
158 }
159 return a
160}
161
162func remove(s []string, v string) []string {
163 s2 := make([]string, 0, len(s))
164 for _, sv := range s {
165 if sv != v {
166 s2 = append(s2, sv)
167 }
168 }
169 return s2
170}
171
172func createMergedTxts(ctx android.LoadHookContext, props CombinedApisProperties) {
173 var textFiles []MergedTxtDefinition
Anton Hansson16ff3572022-01-11 18:36:35 +0000174 // Two module libraries currently do not support @SystemApi so only have the public scope.
175 bcpWithSystemApi := props.Bootclasspath
176 bcpWithSystemApi = remove(bcpWithSystemApi, "conscrypt.module.public.api")
177 bcpWithSystemApi = remove(bcpWithSystemApi, "i18n.module.public.api")
178
Anton Hansson0860aaf2021-10-08 16:48:03 +0100179 tagSuffix := []string{".api.txt}", ".removed-api.txt}"}
180 for i, f := range []string{"current.txt", "removed.txt"} {
181 textFiles = append(textFiles, MergedTxtDefinition{
182 TxtFilename: f,
183 BaseTxt: ":non-updatable-" + f,
Anton Hansson16ff3572022-01-11 18:36:35 +0000184 Modules: props.Bootclasspath,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100185 ModuleTag: "{.public" + tagSuffix[i],
186 Scope: "public",
187 })
188 textFiles = append(textFiles, MergedTxtDefinition{
189 TxtFilename: f,
190 BaseTxt: ":non-updatable-system-" + f,
Anton Hansson16ff3572022-01-11 18:36:35 +0000191 Modules: bcpWithSystemApi,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100192 ModuleTag: "{.system" + tagSuffix[i],
193 Scope: "system",
194 })
195 textFiles = append(textFiles, MergedTxtDefinition{
196 TxtFilename: f,
197 BaseTxt: ":non-updatable-module-lib-" + f,
Anton Hansson16ff3572022-01-11 18:36:35 +0000198 Modules: bcpWithSystemApi,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100199 ModuleTag: "{.module-lib" + tagSuffix[i],
200 Scope: "module-lib",
201 })
202 textFiles = append(textFiles, MergedTxtDefinition{
203 TxtFilename: f,
204 BaseTxt: ":non-updatable-system-server-" + f,
Anton Hansson16ff3572022-01-11 18:36:35 +0000205 Modules: props.System_server_classpath,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100206 ModuleTag: "{.system-server" + tagSuffix[i],
207 Scope: "system-server",
208 })
209 }
210 for _, txt := range textFiles {
211 createMergedTxt(ctx, txt)
212 }
213}
214
215func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) {
216 createMergedTxts(ctx, a.properties)
217
Anton Hansson16ff3572022-01-11 18:36:35 +0000218 createMergedStubsSrcjar(ctx, a.properties.Bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100219
Anton Hanssoncc18e032022-01-12 14:45:22 +0000220 // Conscrypt and i18n currently do not enable annotations
221 annotationModules := a.properties.Bootclasspath
222 annotationModules = remove(annotationModules, "conscrypt.module.public.api")
223 annotationModules = remove(annotationModules, "i18n.module.public.api")
224 createMergedAnnotations(ctx, annotationModules)
225
Anton Hansson16ff3572022-01-11 18:36:35 +0000226 // For the filtered api versions, we prune all APIs except art module's APIs. because
227 // 1) ART apis are available by default to all modules, while other module-to-module deps are
228 // explicit and probably receive more scrutiny anyway
229 // 2) The number of ART/libcore APIs is large, so not linting them would create a large gap
230 // 3) It's a compromise. Ideally we wouldn't be filtering out any module APIs, and have
231 // per-module lint databases that excludes just that module's APIs. Alas, that's more
232 // difficult to achieve.
233 filteredModules := a.properties.Bootclasspath
234 filteredModules = remove(filteredModules, "art.module.public.api")
235 createFilteredApiVersions(ctx, filteredModules)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100236}
237
238func combinedApisModuleFactory() android.Module {
239 module := &CombinedApis{}
240 module.AddProperties(&module.properties)
241 android.InitAndroidModule(module)
242 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) })
243 return module
244}