blob: 6cc129a360fd58929f6651136ddffc03612b25b4 [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
26// The intention behind this soong plugin is to generate a number of "merged"
27// API-related modules that would otherwise require a large amount of very
28// similar Android.bp boilerplate to define. For example, the merged current.txt
29// API definitions (created by merging the non-updatable current.txt with all
30// the module current.txts). This simplifies the addition of new android
31// modules, by reducing the number of genrules etc a new module must be added to.
32
33// The properties of the combined_apis module type.
34type CombinedApisProperties struct {
Anton Hansson16ff3572022-01-11 18:36:35 +000035 // Module libraries in the bootclasspath
36 Bootclasspath []string
Anton Hansson07a12952022-01-12 17:28:39 +000037 // Module libraries on the bootclasspath if include_nonpublic_framework_api is true.
38 Conditional_bootclasspath []string
Anton Hansson16ff3572022-01-11 18:36:35 +000039 // Module libraries in system server
40 System_server_classpath []string
Anton Hansson0860aaf2021-10-08 16:48:03 +010041}
42
43type CombinedApis struct {
44 android.ModuleBase
45
46 properties CombinedApisProperties
47}
48
49func init() {
50 registerBuildComponents(android.InitRegistrationContext)
51}
52
53func registerBuildComponents(ctx android.RegistrationContext) {
54 ctx.RegisterModuleType("combined_apis", combinedApisModuleFactory)
55}
56
57var PrepareForCombinedApisTest = android.FixtureRegisterWithContext(registerBuildComponents)
58
59func (a *CombinedApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
60}
61
62type genruleProps struct {
63 Name *string
64 Cmd *string
65 Dists []android.Dist
66 Out []string
67 Srcs []string
68 Tools []string
69 Visibility []string
70}
71
72// Struct to pass parameters for the various merged [current|removed].txt file modules we create.
73type MergedTxtDefinition struct {
74 // "current.txt" or "removed.txt"
75 TxtFilename string
76 // The module for the non-updatable / non-module part of the api.
77 BaseTxt string
78 // The list of modules that are relevant for this merged txt.
79 Modules []string
80 // The output tag for each module to use.e.g. {.public.api.txt} for current.txt
81 ModuleTag string
82 // public, system, module-lib or system-server
83 Scope string
84}
85
86func createMergedTxt(ctx android.LoadHookContext, txt MergedTxtDefinition) {
87 metalavaCmd := "$(location metalava)"
88 // Silence reflection warnings. See b/168689341
89 metalavaCmd += " -J--add-opens=java.base/java.util=ALL-UNNAMED "
90 metalavaCmd += " --quiet --no-banner --format=v2 "
91
92 filename := txt.TxtFilename
93 if txt.Scope != "public" {
94 filename = txt.Scope + "-" + filename
95 }
96
97 props := genruleProps{}
98 props.Name = proptools.StringPtr(ctx.ModuleName() + "-" + filename)
99 props.Tools = []string{"metalava"}
Anton Hansson16ff3572022-01-11 18:36:35 +0000100 props.Out = []string{filename}
Anton Hansson0860aaf2021-10-08 16:48:03 +0100101 props.Cmd = proptools.StringPtr(metalavaCmd + "$(in) --api $(out)")
102 props.Srcs = createSrcs(txt.BaseTxt, txt.Modules, txt.ModuleTag)
103 props.Dists = []android.Dist{
104 {
105 Targets: []string{"droidcore"},
106 Dir: proptools.StringPtr("api"),
107 Dest: proptools.StringPtr(filename),
108 },
109 {
110 Targets: []string{"sdk"},
111 Dir: proptools.StringPtr("apistubs/android/" + txt.Scope + "/api"),
112 Dest: proptools.StringPtr(txt.TxtFilename),
113 },
114 }
115 props.Visibility = []string{"//visibility:public"}
116 ctx.CreateModule(genrule.GenRuleFactory, &props)
117}
118
119func createMergedStubsSrcjar(ctx android.LoadHookContext, modules []string) {
120 props := genruleProps{}
121 props.Name = proptools.StringPtr(ctx.ModuleName() + "-current.srcjar")
122 props.Tools = []string{"merge_zips"}
123 props.Out = []string{"current.srcjar"}
124 props.Cmd = proptools.StringPtr("$(location merge_zips) $(out) $(in)")
125 props.Srcs = createSrcs(":api-stubs-docs-non-updatable", modules, "{.public.stubs.source}")
126 props.Visibility = []string{"//visibility:private"} // Used by make module in //development, mind
127 ctx.CreateModule(genrule.GenRuleFactory, &props)
128}
129
Anton Hanssoncc18e032022-01-12 14:45:22 +0000130// This produces the same annotations.zip as framework-doc-stubs, but by using
131// outputs from individual modules instead of all the source code.
132func createMergedAnnotations(ctx android.LoadHookContext, modules []string) {
133 props := genruleProps{}
134 props.Name = proptools.StringPtr("sdk-annotations.zip")
135 props.Tools = []string{"merge_annotation_zips", "soong_zip"}
136 props.Out = []string{"annotations.zip"}
137 props.Cmd = proptools.StringPtr("$(location merge_annotation_zips) $(genDir)/out $(in) && " +
138 "$(location soong_zip) -o $(out) -C $(genDir)/out -D $(genDir)/out")
139 props.Srcs = createSrcs(":android-non-updatable-doc-stubs{.annotations.zip}", modules, "{.public.annotations.zip}")
140 ctx.CreateModule(genrule.GenRuleFactory, &props)
141}
142
Anton Hansson0860aaf2021-10-08 16:48:03 +0100143func createFilteredApiVersions(ctx android.LoadHookContext, modules []string) {
144 props := genruleProps{}
145 props.Name = proptools.StringPtr("api-versions-xml-public-filtered")
146 props.Tools = []string{"api_versions_trimmer"}
147 props.Out = []string{"api-versions-public-filtered.xml"}
148 props.Cmd = proptools.StringPtr("$(location api_versions_trimmer) $(out) $(in)")
149 // Note: order matters: first parameter is the full api-versions.xml
150 // after that the stubs files in any order
151 // stubs files are all modules that export API surfaces EXCEPT ART
152 props.Srcs = createSrcs(":framework-doc-stubs{.api_versions.xml}", modules, ".stubs{.jar}")
153 props.Dists = []android.Dist{{Targets: []string{"sdk"}}}
154 ctx.CreateModule(genrule.GenRuleFactory, &props)
155}
156
157func createSrcs(base string, modules []string, tag string) []string {
158 a := make([]string, 0, len(modules)+1)
159 a = append(a, base)
160 for _, module := range modules {
161 a = append(a, ":"+module+tag)
162 }
163 return a
164}
165
166func remove(s []string, v string) []string {
167 s2 := make([]string, 0, len(s))
168 for _, sv := range s {
169 if sv != v {
170 s2 = append(s2, sv)
171 }
172 }
173 return s2
174}
175
Anton Hansson07a12952022-01-12 17:28:39 +0000176func createMergedTxts(ctx android.LoadHookContext, bootclasspath, system_server_classpath []string) {
Anton Hansson0860aaf2021-10-08 16:48:03 +0100177 var textFiles []MergedTxtDefinition
Anton Hansson16ff3572022-01-11 18:36:35 +0000178 // Two module libraries currently do not support @SystemApi so only have the public scope.
Anton Hansson07a12952022-01-12 17:28:39 +0000179 bcpWithSystemApi := bootclasspath
Anton Hansson16ff3572022-01-11 18:36:35 +0000180 bcpWithSystemApi = remove(bcpWithSystemApi, "conscrypt.module.public.api")
181 bcpWithSystemApi = remove(bcpWithSystemApi, "i18n.module.public.api")
182
Anton Hansson0860aaf2021-10-08 16:48:03 +0100183 tagSuffix := []string{".api.txt}", ".removed-api.txt}"}
184 for i, f := range []string{"current.txt", "removed.txt"} {
185 textFiles = append(textFiles, MergedTxtDefinition{
186 TxtFilename: f,
187 BaseTxt: ":non-updatable-" + f,
Anton Hansson07a12952022-01-12 17:28:39 +0000188 Modules: bootclasspath,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100189 ModuleTag: "{.public" + tagSuffix[i],
190 Scope: "public",
191 })
192 textFiles = append(textFiles, MergedTxtDefinition{
193 TxtFilename: f,
194 BaseTxt: ":non-updatable-system-" + f,
Anton Hansson16ff3572022-01-11 18:36:35 +0000195 Modules: bcpWithSystemApi,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100196 ModuleTag: "{.system" + tagSuffix[i],
197 Scope: "system",
198 })
199 textFiles = append(textFiles, MergedTxtDefinition{
200 TxtFilename: f,
201 BaseTxt: ":non-updatable-module-lib-" + f,
Anton Hansson16ff3572022-01-11 18:36:35 +0000202 Modules: bcpWithSystemApi,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100203 ModuleTag: "{.module-lib" + tagSuffix[i],
204 Scope: "module-lib",
205 })
206 textFiles = append(textFiles, MergedTxtDefinition{
207 TxtFilename: f,
208 BaseTxt: ":non-updatable-system-server-" + f,
Anton Hansson07a12952022-01-12 17:28:39 +0000209 Modules: system_server_classpath,
Anton Hansson0860aaf2021-10-08 16:48:03 +0100210 ModuleTag: "{.system-server" + tagSuffix[i],
211 Scope: "system-server",
212 })
213 }
214 for _, txt := range textFiles {
215 createMergedTxt(ctx, txt)
216 }
217}
218
219func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) {
Anton Hansson07a12952022-01-12 17:28:39 +0000220 bootclasspath := a.properties.Bootclasspath
221 if ctx.Config().VendorConfig("ANDROID").Bool("include_nonpublic_framework_api") {
222 bootclasspath = append(bootclasspath, a.properties.Conditional_bootclasspath...)
223 sort.Strings(bootclasspath)
224 }
225 createMergedTxts(ctx, bootclasspath, a.properties.System_server_classpath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100226
Anton Hansson07a12952022-01-12 17:28:39 +0000227 createMergedStubsSrcjar(ctx, bootclasspath)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100228
Anton Hanssoncc18e032022-01-12 14:45:22 +0000229 // Conscrypt and i18n currently do not enable annotations
Anton Hansson07a12952022-01-12 17:28:39 +0000230 annotationModules := bootclasspath
Anton Hanssoncc18e032022-01-12 14:45:22 +0000231 annotationModules = remove(annotationModules, "conscrypt.module.public.api")
232 annotationModules = remove(annotationModules, "i18n.module.public.api")
233 createMergedAnnotations(ctx, annotationModules)
234
Anton Hansson16ff3572022-01-11 18:36:35 +0000235 // For the filtered api versions, we prune all APIs except art module's APIs. because
236 // 1) ART apis are available by default to all modules, while other module-to-module deps are
237 // explicit and probably receive more scrutiny anyway
238 // 2) The number of ART/libcore APIs is large, so not linting them would create a large gap
239 // 3) It's a compromise. Ideally we wouldn't be filtering out any module APIs, and have
240 // per-module lint databases that excludes just that module's APIs. Alas, that's more
241 // difficult to achieve.
Anton Hansson07a12952022-01-12 17:28:39 +0000242 filteredModules := bootclasspath
Anton Hansson16ff3572022-01-11 18:36:35 +0000243 filteredModules = remove(filteredModules, "art.module.public.api")
244 createFilteredApiVersions(ctx, filteredModules)
Anton Hansson0860aaf2021-10-08 16:48:03 +0100245}
246
247func combinedApisModuleFactory() android.Module {
248 module := &CombinedApis{}
249 module.AddProperties(&module.properties)
250 android.InitAndroidModule(module)
251 android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) })
252 return module
253}