blob: 74cbece7ebf574e9824c8e970179573e4d2bd9ea [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
126func createFilteredApiVersions(ctx android.LoadHookContext, modules []string) {
127 props := genruleProps{}
128 props.Name = proptools.StringPtr("api-versions-xml-public-filtered")
129 props.Tools = []string{"api_versions_trimmer"}
130 props.Out = []string{"api-versions-public-filtered.xml"}
131 props.Cmd = proptools.StringPtr("$(location api_versions_trimmer) $(out) $(in)")
132 // Note: order matters: first parameter is the full api-versions.xml
133 // after that the stubs files in any order
134 // stubs files are all modules that export API surfaces EXCEPT ART
135 props.Srcs = createSrcs(":framework-doc-stubs{.api_versions.xml}", modules, ".stubs{.jar}")
136 props.Dists = []android.Dist{{Targets: []string{"sdk"}}}
137 ctx.CreateModule(genrule.GenRuleFactory, &props)
138}
139
140func createSrcs(base string, modules []string, tag string) []string {
141 a := make([]string, 0, len(modules)+1)
142 a = append(a, base)
143 for _, module := range modules {
144 a = append(a, ":"+module+tag)
145 }
146 return a
147}
148
149func remove(s []string, v string) []string {
150 s2 := make([]string, 0, len(s))
151 for _, sv := range s {
152 if sv != v {
153 s2 = append(s2, sv)
154 }
155 }
156 return s2
157}
158
159func createMergedTxts(ctx android.LoadHookContext, props CombinedApisProperties) {
160 var textFiles []MergedTxtDefinition
Anton Hansson16ff3572022-01-11 18:36:35 +0000161 // Two module libraries currently do not support @SystemApi so only have the public scope.
162 bcpWithSystemApi := props.Bootclasspath
163 bcpWithSystemApi = remove(bcpWithSystemApi, "conscrypt.module.public.api")
164 bcpWithSystemApi = remove(bcpWithSystemApi, "i18n.module.public.api")
165
Anton Hansson0860aaf2021-10-08 16:48:03 +0100166 tagSuffix := []string{".api.txt}", ".removed-api.txt}"}
167 for i, f := range []string{"current.txt", "removed.txt"} {
168 textFiles = append(textFiles, MergedTxtDefinition{
169 TxtFilename: f,
170 BaseTxt: ":non-updatable-" + f,
Anton Hansson16ff3572022-01-11 18:36:35 +0000171 Modules: props.Bootclasspath,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100172 ModuleTag: "{.public" + tagSuffix[i],
173 Scope: "public",
174 })
175 textFiles = append(textFiles, MergedTxtDefinition{
176 TxtFilename: f,
177 BaseTxt: ":non-updatable-system-" + f,
Anton Hansson16ff3572022-01-11 18:36:35 +0000178 Modules: bcpWithSystemApi,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100179 ModuleTag: "{.system" + tagSuffix[i],
180 Scope: "system",
181 })
182 textFiles = append(textFiles, MergedTxtDefinition{
183 TxtFilename: f,
184 BaseTxt: ":non-updatable-module-lib-" + f,
Anton Hansson16ff3572022-01-11 18:36:35 +0000185 Modules: bcpWithSystemApi,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100186 ModuleTag: "{.module-lib" + tagSuffix[i],
187 Scope: "module-lib",
188 })
189 textFiles = append(textFiles, MergedTxtDefinition{
190 TxtFilename: f,
191 BaseTxt: ":non-updatable-system-server-" + f,
Anton Hansson16ff3572022-01-11 18:36:35 +0000192 Modules: props.System_server_classpath,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100193 ModuleTag: "{.system-server" + tagSuffix[i],
194 Scope: "system-server",
195 })
196 }
197 for _, txt := range textFiles {
198 createMergedTxt(ctx, txt)
199 }
200}
201
202func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) {
203 createMergedTxts(ctx, a.properties)
204
Anton Hansson16ff3572022-01-11 18:36:35 +0000205 createMergedStubsSrcjar(ctx, a.properties.Bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100206
Anton Hansson16ff3572022-01-11 18:36:35 +0000207 // For the filtered api versions, we prune all APIs except art module's APIs. because
208 // 1) ART apis are available by default to all modules, while other module-to-module deps are
209 // explicit and probably receive more scrutiny anyway
210 // 2) The number of ART/libcore APIs is large, so not linting them would create a large gap
211 // 3) It's a compromise. Ideally we wouldn't be filtering out any module APIs, and have
212 // per-module lint databases that excludes just that module's APIs. Alas, that's more
213 // difficult to achieve.
214 filteredModules := a.properties.Bootclasspath
215 filteredModules = remove(filteredModules, "art.module.public.api")
216 createFilteredApiVersions(ctx, filteredModules)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100217}
218
219func combinedApisModuleFactory() android.Module {
220 module := &CombinedApis{}
221 module.AddProperties(&module.properties)
222 android.InitAndroidModule(module)
223 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) })
224 return module
225}