blob: 14fe09ee36dab3320b87cd66a943bbec7e4de6c4 [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"
24)
25
Anton Hansson05e944d2022-01-13 12:26:30 +000026const art = "art.module.public.api"
27const conscrypt = "conscrypt.module.public.api"
28const i18n = "i18n.module.public.api"
29
Anton Hansson0860aaf2021-10-08 16:48:03 +010030// The intention behind this soong plugin is to generate a number of "merged"
31// API-related modules that would otherwise require a large amount of very
32// similar Android.bp boilerplate to define. For example, the merged current.txt
33// API definitions (created by merging the non-updatable current.txt with all
34// the module current.txts). This simplifies the addition of new android
35// modules, by reducing the number of genrules etc a new module must be added to.
36
37// The properties of the combined_apis module type.
38type CombinedApisProperties struct {
Anton Hansson16ff3572022-01-11 18:36:35 +000039 // Module libraries in the bootclasspath
40 Bootclasspath []string
Anton Hansson07a12952022-01-12 17:28:39 +000041 // Module libraries on the bootclasspath if include_nonpublic_framework_api is true.
42 Conditional_bootclasspath []string
Anton Hansson16ff3572022-01-11 18:36:35 +000043 // Module libraries in system server
44 System_server_classpath []string
Anton Hansson0860aaf2021-10-08 16:48:03 +010045}
46
47type CombinedApis struct {
48 android.ModuleBase
49
50 properties CombinedApisProperties
51}
52
53func init() {
54 registerBuildComponents(android.InitRegistrationContext)
55}
56
57func registerBuildComponents(ctx android.RegistrationContext) {
58 ctx.RegisterModuleType("combined_apis", combinedApisModuleFactory)
59}
60
61var PrepareForCombinedApisTest = android.FixtureRegisterWithContext(registerBuildComponents)
62
63func (a *CombinedApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
64}
65
66type genruleProps struct {
67 Name *string
68 Cmd *string
69 Dists []android.Dist
70 Out []string
71 Srcs []string
72 Tools []string
73 Visibility []string
74}
75
76// Struct to pass parameters for the various merged [current|removed].txt file modules we create.
77type MergedTxtDefinition struct {
78 // "current.txt" or "removed.txt"
79 TxtFilename string
80 // The module for the non-updatable / non-module part of the api.
81 BaseTxt string
82 // The list of modules that are relevant for this merged txt.
83 Modules []string
84 // The output tag for each module to use.e.g. {.public.api.txt} for current.txt
85 ModuleTag string
86 // public, system, module-lib or system-server
87 Scope string
88}
89
90func createMergedTxt(ctx android.LoadHookContext, txt MergedTxtDefinition) {
91 metalavaCmd := "$(location metalava)"
92 // Silence reflection warnings. See b/168689341
93 metalavaCmd += " -J--add-opens=java.base/java.util=ALL-UNNAMED "
94 metalavaCmd += " --quiet --no-banner --format=v2 "
95
96 filename := txt.TxtFilename
97 if txt.Scope != "public" {
98 filename = txt.Scope + "-" + filename
99 }
100
101 props := genruleProps{}
102 props.Name = proptools.StringPtr(ctx.ModuleName() + "-" + filename)
103 props.Tools = []string{"metalava"}
Anton Hansson16ff3572022-01-11 18:36:35 +0000104 props.Out = []string{filename}
Anton Hansson0860aaf2021-10-08 16:48:03 +0100105 props.Cmd = proptools.StringPtr(metalavaCmd + "$(in) --api $(out)")
106 props.Srcs = createSrcs(txt.BaseTxt, txt.Modules, txt.ModuleTag)
107 props.Dists = []android.Dist{
108 {
109 Targets: []string{"droidcore"},
110 Dir: proptools.StringPtr("api"),
111 Dest: proptools.StringPtr(filename),
112 },
113 {
114 Targets: []string{"sdk"},
115 Dir: proptools.StringPtr("apistubs/android/" + txt.Scope + "/api"),
116 Dest: proptools.StringPtr(txt.TxtFilename),
117 },
118 }
119 props.Visibility = []string{"//visibility:public"}
120 ctx.CreateModule(genrule.GenRuleFactory, &props)
121}
122
123func createMergedStubsSrcjar(ctx android.LoadHookContext, modules []string) {
124 props := genruleProps{}
125 props.Name = proptools.StringPtr(ctx.ModuleName() + "-current.srcjar")
126 props.Tools = []string{"merge_zips"}
127 props.Out = []string{"current.srcjar"}
128 props.Cmd = proptools.StringPtr("$(location merge_zips) $(out) $(in)")
129 props.Srcs = createSrcs(":api-stubs-docs-non-updatable", modules, "{.public.stubs.source}")
130 props.Visibility = []string{"//visibility:private"} // Used by make module in //development, mind
131 ctx.CreateModule(genrule.GenRuleFactory, &props)
132}
133
Anton Hanssoncc18e032022-01-12 14:45:22 +0000134// This produces the same annotations.zip as framework-doc-stubs, but by using
135// outputs from individual modules instead of all the source code.
136func createMergedAnnotations(ctx android.LoadHookContext, modules []string) {
Anton Hansson05e944d2022-01-13 12:26:30 +0000137 // Conscrypt and i18n currently do not enable annotations
138 modules = removeAll(modules, []string{conscrypt, i18n})
Anton Hanssoncc18e032022-01-12 14:45:22 +0000139 props := genruleProps{}
140 props.Name = proptools.StringPtr("sdk-annotations.zip")
141 props.Tools = []string{"merge_annotation_zips", "soong_zip"}
142 props.Out = []string{"annotations.zip"}
143 props.Cmd = proptools.StringPtr("$(location merge_annotation_zips) $(genDir)/out $(in) && " +
144 "$(location soong_zip) -o $(out) -C $(genDir)/out -D $(genDir)/out")
145 props.Srcs = createSrcs(":android-non-updatable-doc-stubs{.annotations.zip}", modules, "{.public.annotations.zip}")
146 ctx.CreateModule(genrule.GenRuleFactory, &props)
147}
148
Anton Hansson0860aaf2021-10-08 16:48:03 +0100149func createFilteredApiVersions(ctx android.LoadHookContext, modules []string) {
Anton Hansson05e944d2022-01-13 12:26:30 +0000150 // For the filtered api versions, we prune all APIs except art module's APIs. because
151 // 1) ART apis are available by default to all modules, while other module-to-module deps are
152 // explicit and probably receive more scrutiny anyway
153 // 2) The number of ART/libcore APIs is large, so not linting them would create a large gap
154 // 3) It's a compromise. Ideally we wouldn't be filtering out any module APIs, and have
155 // per-module lint databases that excludes just that module's APIs. Alas, that's more
156 // difficult to achieve.
157 modules = remove(modules, art)
158
Anton Hansson0860aaf2021-10-08 16:48:03 +0100159 props := genruleProps{}
160 props.Name = proptools.StringPtr("api-versions-xml-public-filtered")
161 props.Tools = []string{"api_versions_trimmer"}
162 props.Out = []string{"api-versions-public-filtered.xml"}
163 props.Cmd = proptools.StringPtr("$(location api_versions_trimmer) $(out) $(in)")
164 // Note: order matters: first parameter is the full api-versions.xml
165 // after that the stubs files in any order
166 // stubs files are all modules that export API surfaces EXCEPT ART
167 props.Srcs = createSrcs(":framework-doc-stubs{.api_versions.xml}", modules, ".stubs{.jar}")
168 props.Dists = []android.Dist{{Targets: []string{"sdk"}}}
169 ctx.CreateModule(genrule.GenRuleFactory, &props)
170}
171
172func createSrcs(base string, modules []string, tag string) []string {
173 a := make([]string, 0, len(modules)+1)
174 a = append(a, base)
175 for _, module := range modules {
176 a = append(a, ":"+module+tag)
177 }
178 return a
179}
180
Anton Hansson05e944d2022-01-13 12:26:30 +0000181func removeAll(s []string, vs []string) []string {
182 for _, v := range vs {
183 s = remove(s, v)
184 }
185 return s
186}
187
Anton Hansson0860aaf2021-10-08 16:48:03 +0100188func remove(s []string, v string) []string {
189 s2 := make([]string, 0, len(s))
190 for _, sv := range s {
191 if sv != v {
192 s2 = append(s2, sv)
193 }
194 }
195 return s2
196}
197
Anton Hansson07a12952022-01-12 17:28:39 +0000198func createMergedTxts(ctx android.LoadHookContext, bootclasspath, system_server_classpath []string) {
Anton Hansson0860aaf2021-10-08 16:48:03 +0100199 var textFiles []MergedTxtDefinition
Anton Hansson16ff3572022-01-11 18:36:35 +0000200 // Two module libraries currently do not support @SystemApi so only have the public scope.
Anton Hansson05e944d2022-01-13 12:26:30 +0000201 bcpWithSystemApi := removeAll(bootclasspath, []string{conscrypt, i18n})
Anton Hansson16ff3572022-01-11 18:36:35 +0000202
Anton Hansson0860aaf2021-10-08 16:48:03 +0100203 tagSuffix := []string{".api.txt}", ".removed-api.txt}"}
204 for i, f := range []string{"current.txt", "removed.txt"} {
205 textFiles = append(textFiles, MergedTxtDefinition{
206 TxtFilename: f,
207 BaseTxt: ":non-updatable-" + f,
Anton Hansson07a12952022-01-12 17:28:39 +0000208 Modules: bootclasspath,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100209 ModuleTag: "{.public" + tagSuffix[i],
210 Scope: "public",
211 })
212 textFiles = append(textFiles, MergedTxtDefinition{
213 TxtFilename: f,
214 BaseTxt: ":non-updatable-system-" + f,
Anton Hansson16ff3572022-01-11 18:36:35 +0000215 Modules: bcpWithSystemApi,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100216 ModuleTag: "{.system" + tagSuffix[i],
217 Scope: "system",
218 })
219 textFiles = append(textFiles, MergedTxtDefinition{
220 TxtFilename: f,
221 BaseTxt: ":non-updatable-module-lib-" + f,
Anton Hansson16ff3572022-01-11 18:36:35 +0000222 Modules: bcpWithSystemApi,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100223 ModuleTag: "{.module-lib" + tagSuffix[i],
224 Scope: "module-lib",
225 })
226 textFiles = append(textFiles, MergedTxtDefinition{
227 TxtFilename: f,
228 BaseTxt: ":non-updatable-system-server-" + f,
Anton Hansson07a12952022-01-12 17:28:39 +0000229 Modules: system_server_classpath,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100230 ModuleTag: "{.system-server" + tagSuffix[i],
231 Scope: "system-server",
232 })
233 }
234 for _, txt := range textFiles {
235 createMergedTxt(ctx, txt)
236 }
237}
238
239func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) {
Anton Hansson07a12952022-01-12 17:28:39 +0000240 bootclasspath := a.properties.Bootclasspath
241 if ctx.Config().VendorConfig("ANDROID").Bool("include_nonpublic_framework_api") {
242 bootclasspath = append(bootclasspath, a.properties.Conditional_bootclasspath...)
243 sort.Strings(bootclasspath)
244 }
245 createMergedTxts(ctx, bootclasspath, a.properties.System_server_classpath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100246
Anton Hansson07a12952022-01-12 17:28:39 +0000247 createMergedStubsSrcjar(ctx, bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100248
Anton Hansson05e944d2022-01-13 12:26:30 +0000249 createMergedAnnotations(ctx, bootclasspath)
Anton Hanssoncc18e032022-01-12 14:45:22 +0000250
Anton Hansson05e944d2022-01-13 12:26:30 +0000251 createFilteredApiVersions(ctx, bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100252}
253
254func combinedApisModuleFactory() android.Module {
255 module := &CombinedApis{}
256 module.AddProperties(&module.properties)
257 android.InitAndroidModule(module)
258 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) })
259 return module
260}