Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 1 | // 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 | |
| 15 | package api |
| 16 | |
| 17 | import ( |
Anton Hansson | 07a1295 | 2022-01-12 17:28:39 +0000 | [diff] [blame] | 18 | "sort" |
| 19 | |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 20 | "github.com/google/blueprint/proptools" |
| 21 | |
| 22 | "android/soong/android" |
| 23 | "android/soong/genrule" |
Anton Hansson | cb00f94 | 2022-01-13 09:45:12 +0000 | [diff] [blame] | 24 | "android/soong/java" |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 25 | ) |
| 26 | |
Anton Hansson | 05e944d | 2022-01-13 12:26:30 +0000 | [diff] [blame] | 27 | const art = "art.module.public.api" |
| 28 | const conscrypt = "conscrypt.module.public.api" |
| 29 | const i18n = "i18n.module.public.api" |
| 30 | |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 31 | // 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. |
| 39 | type CombinedApisProperties struct { |
Anton Hansson | 16ff357 | 2022-01-11 18:36:35 +0000 | [diff] [blame] | 40 | // Module libraries in the bootclasspath |
| 41 | Bootclasspath []string |
Anton Hansson | 07a1295 | 2022-01-12 17:28:39 +0000 | [diff] [blame] | 42 | // Module libraries on the bootclasspath if include_nonpublic_framework_api is true. |
| 43 | Conditional_bootclasspath []string |
Anton Hansson | 16ff357 | 2022-01-11 18:36:35 +0000 | [diff] [blame] | 44 | // Module libraries in system server |
| 45 | System_server_classpath []string |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | type CombinedApis struct { |
| 49 | android.ModuleBase |
| 50 | |
| 51 | properties CombinedApisProperties |
| 52 | } |
| 53 | |
| 54 | func init() { |
| 55 | registerBuildComponents(android.InitRegistrationContext) |
| 56 | } |
| 57 | |
| 58 | func registerBuildComponents(ctx android.RegistrationContext) { |
| 59 | ctx.RegisterModuleType("combined_apis", combinedApisModuleFactory) |
| 60 | } |
| 61 | |
| 62 | var PrepareForCombinedApisTest = android.FixtureRegisterWithContext(registerBuildComponents) |
| 63 | |
| 64 | func (a *CombinedApis) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 65 | } |
| 66 | |
| 67 | type 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 Hansson | cb00f94 | 2022-01-13 09:45:12 +0000 | [diff] [blame] | 77 | type libraryProps struct { |
| 78 | Name *string |
| 79 | Sdk_version *string |
| 80 | Static_libs []string |
| 81 | Visibility []string |
| 82 | } |
| 83 | |
Anton Hansson | 4468d7c | 2022-01-14 12:10:01 +0000 | [diff] [blame] | 84 | type fgProps struct { |
| 85 | Name *string |
| 86 | Srcs []string |
| 87 | Visibility []string |
| 88 | } |
| 89 | |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 90 | // Struct to pass parameters for the various merged [current|removed].txt file modules we create. |
| 91 | type MergedTxtDefinition struct { |
| 92 | // "current.txt" or "removed.txt" |
| 93 | TxtFilename string |
| 94 | // The module for the non-updatable / non-module part of the api. |
| 95 | BaseTxt string |
| 96 | // The list of modules that are relevant for this merged txt. |
| 97 | Modules []string |
| 98 | // The output tag for each module to use.e.g. {.public.api.txt} for current.txt |
| 99 | ModuleTag string |
| 100 | // public, system, module-lib or system-server |
| 101 | Scope string |
| 102 | } |
| 103 | |
| 104 | func createMergedTxt(ctx android.LoadHookContext, txt MergedTxtDefinition) { |
| 105 | metalavaCmd := "$(location metalava)" |
| 106 | // Silence reflection warnings. See b/168689341 |
| 107 | metalavaCmd += " -J--add-opens=java.base/java.util=ALL-UNNAMED " |
| 108 | metalavaCmd += " --quiet --no-banner --format=v2 " |
| 109 | |
| 110 | filename := txt.TxtFilename |
| 111 | if txt.Scope != "public" { |
| 112 | filename = txt.Scope + "-" + filename |
| 113 | } |
| 114 | |
| 115 | props := genruleProps{} |
| 116 | props.Name = proptools.StringPtr(ctx.ModuleName() + "-" + filename) |
| 117 | props.Tools = []string{"metalava"} |
Anton Hansson | 16ff357 | 2022-01-11 18:36:35 +0000 | [diff] [blame] | 118 | props.Out = []string{filename} |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 119 | props.Cmd = proptools.StringPtr(metalavaCmd + "$(in) --api $(out)") |
Anton Hansson | fd31645 | 2022-01-14 11:15:52 +0000 | [diff] [blame] | 120 | props.Srcs = append([]string{txt.BaseTxt}, createSrcs(txt.Modules, txt.ModuleTag)...) |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 121 | props.Dists = []android.Dist{ |
| 122 | { |
| 123 | Targets: []string{"droidcore"}, |
| 124 | Dir: proptools.StringPtr("api"), |
| 125 | Dest: proptools.StringPtr(filename), |
| 126 | }, |
| 127 | { |
| 128 | Targets: []string{"sdk"}, |
| 129 | Dir: proptools.StringPtr("apistubs/android/" + txt.Scope + "/api"), |
| 130 | Dest: proptools.StringPtr(txt.TxtFilename), |
| 131 | }, |
| 132 | } |
| 133 | props.Visibility = []string{"//visibility:public"} |
| 134 | ctx.CreateModule(genrule.GenRuleFactory, &props) |
| 135 | } |
| 136 | |
| 137 | func createMergedStubsSrcjar(ctx android.LoadHookContext, modules []string) { |
| 138 | props := genruleProps{} |
| 139 | props.Name = proptools.StringPtr(ctx.ModuleName() + "-current.srcjar") |
| 140 | props.Tools = []string{"merge_zips"} |
| 141 | props.Out = []string{"current.srcjar"} |
| 142 | props.Cmd = proptools.StringPtr("$(location merge_zips) $(out) $(in)") |
Anton Hansson | fd31645 | 2022-01-14 11:15:52 +0000 | [diff] [blame] | 143 | props.Srcs = append([]string{":api-stubs-docs-non-updatable"}, createSrcs(modules, "{.public.stubs.source}")...) |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 144 | props.Visibility = []string{"//visibility:private"} // Used by make module in //development, mind |
| 145 | ctx.CreateModule(genrule.GenRuleFactory, &props) |
| 146 | } |
| 147 | |
Anton Hansson | cc18e03 | 2022-01-12 14:45:22 +0000 | [diff] [blame] | 148 | // This produces the same annotations.zip as framework-doc-stubs, but by using |
| 149 | // outputs from individual modules instead of all the source code. |
| 150 | func createMergedAnnotations(ctx android.LoadHookContext, modules []string) { |
| 151 | props := genruleProps{} |
| 152 | props.Name = proptools.StringPtr("sdk-annotations.zip") |
| 153 | props.Tools = []string{"merge_annotation_zips", "soong_zip"} |
| 154 | props.Out = []string{"annotations.zip"} |
| 155 | props.Cmd = proptools.StringPtr("$(location merge_annotation_zips) $(genDir)/out $(in) && " + |
| 156 | "$(location soong_zip) -o $(out) -C $(genDir)/out -D $(genDir)/out") |
Anton Hansson | fd31645 | 2022-01-14 11:15:52 +0000 | [diff] [blame] | 157 | props.Srcs = append([]string{":android-non-updatable-doc-stubs{.annotations.zip}"}, createSrcs(modules, "{.public.annotations.zip}")...) |
Anton Hansson | cc18e03 | 2022-01-12 14:45:22 +0000 | [diff] [blame] | 158 | ctx.CreateModule(genrule.GenRuleFactory, &props) |
| 159 | } |
| 160 | |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 161 | func createFilteredApiVersions(ctx android.LoadHookContext, modules []string) { |
Anton Hansson | 05e944d | 2022-01-13 12:26:30 +0000 | [diff] [blame] | 162 | // For the filtered api versions, we prune all APIs except art module's APIs. because |
| 163 | // 1) ART apis are available by default to all modules, while other module-to-module deps are |
| 164 | // explicit and probably receive more scrutiny anyway |
| 165 | // 2) The number of ART/libcore APIs is large, so not linting them would create a large gap |
| 166 | // 3) It's a compromise. Ideally we wouldn't be filtering out any module APIs, and have |
| 167 | // per-module lint databases that excludes just that module's APIs. Alas, that's more |
| 168 | // difficult to achieve. |
| 169 | modules = remove(modules, art) |
| 170 | |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 171 | props := genruleProps{} |
| 172 | props.Name = proptools.StringPtr("api-versions-xml-public-filtered") |
| 173 | props.Tools = []string{"api_versions_trimmer"} |
| 174 | props.Out = []string{"api-versions-public-filtered.xml"} |
| 175 | props.Cmd = proptools.StringPtr("$(location api_versions_trimmer) $(out) $(in)") |
| 176 | // Note: order matters: first parameter is the full api-versions.xml |
| 177 | // after that the stubs files in any order |
| 178 | // stubs files are all modules that export API surfaces EXCEPT ART |
Anton Hansson | fd31645 | 2022-01-14 11:15:52 +0000 | [diff] [blame] | 179 | props.Srcs = append([]string{":framework-doc-stubs{.api_versions.xml}"}, createSrcs(modules, ".stubs{.jar}")...) |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 180 | props.Dists = []android.Dist{{Targets: []string{"sdk"}}} |
| 181 | ctx.CreateModule(genrule.GenRuleFactory, &props) |
| 182 | } |
| 183 | |
Anton Hansson | c6e9d2f | 2022-01-25 15:53:43 +0000 | [diff] [blame] | 184 | func createMergedPublicStubs(ctx android.LoadHookContext, modules []string) { |
| 185 | props := libraryProps{} |
| 186 | props.Name = proptools.StringPtr("all-modules-public-stubs") |
| 187 | props.Static_libs = transformArray(modules, "", ".stubs") |
| 188 | props.Sdk_version = proptools.StringPtr("module_current") |
| 189 | props.Visibility = []string{"//frameworks/base"} |
| 190 | ctx.CreateModule(java.LibraryFactory, &props) |
| 191 | } |
| 192 | |
| 193 | func createMergedSystemStubs(ctx android.LoadHookContext, modules []string) { |
| 194 | props := libraryProps{} |
Anton Hansson | c6e9d2f | 2022-01-25 15:53:43 +0000 | [diff] [blame] | 195 | props.Name = proptools.StringPtr("all-modules-system-stubs") |
Paul Duffin | 57533b4 | 2022-01-25 16:25:35 +0000 | [diff] [blame] | 196 | props.Static_libs = transformArray(modules, "", ".stubs.system") |
Anton Hansson | c6e9d2f | 2022-01-25 15:53:43 +0000 | [diff] [blame] | 197 | props.Sdk_version = proptools.StringPtr("module_current") |
| 198 | props.Visibility = []string{"//frameworks/base"} |
| 199 | ctx.CreateModule(java.LibraryFactory, &props) |
| 200 | } |
| 201 | |
Anton Hansson | cb00f94 | 2022-01-13 09:45:12 +0000 | [diff] [blame] | 202 | func createMergedModuleLibStubs(ctx android.LoadHookContext, modules []string) { |
| 203 | // The user of this module compiles against the "core" SDK, so remove core libraries to avoid dupes. |
| 204 | modules = removeAll(modules, []string{art, conscrypt, i18n}) |
| 205 | props := libraryProps{} |
| 206 | props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api") |
Anton Hansson | fd31645 | 2022-01-14 11:15:52 +0000 | [diff] [blame] | 207 | props.Static_libs = transformArray(modules, "", ".stubs.module_lib") |
Anton Hansson | cb00f94 | 2022-01-13 09:45:12 +0000 | [diff] [blame] | 208 | props.Sdk_version = proptools.StringPtr("module_current") |
| 209 | props.Visibility = []string{"//frameworks/base"} |
| 210 | ctx.CreateModule(java.LibraryFactory, &props) |
| 211 | } |
| 212 | |
Anton Hansson | 4468d7c | 2022-01-14 12:10:01 +0000 | [diff] [blame] | 213 | func createPublicStubsSourceFilegroup(ctx android.LoadHookContext, modules []string) { |
| 214 | props := fgProps{} |
| 215 | props.Name = proptools.StringPtr("all-modules-public-stubs-source") |
| 216 | props.Srcs = createSrcs(modules, "{.public.stubs.source}") |
| 217 | props.Visibility = []string{"//frameworks/base"} |
| 218 | ctx.CreateModule(android.FileGroupFactory, &props) |
| 219 | } |
| 220 | |
Anton Hansson | 07a1295 | 2022-01-12 17:28:39 +0000 | [diff] [blame] | 221 | func createMergedTxts(ctx android.LoadHookContext, bootclasspath, system_server_classpath []string) { |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 222 | var textFiles []MergedTxtDefinition |
Anton Hansson | 16ff357 | 2022-01-11 18:36:35 +0000 | [diff] [blame] | 223 | |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 224 | tagSuffix := []string{".api.txt}", ".removed-api.txt}"} |
| 225 | for i, f := range []string{"current.txt", "removed.txt"} { |
| 226 | textFiles = append(textFiles, MergedTxtDefinition{ |
| 227 | TxtFilename: f, |
| 228 | BaseTxt: ":non-updatable-" + f, |
Anton Hansson | 07a1295 | 2022-01-12 17:28:39 +0000 | [diff] [blame] | 229 | Modules: bootclasspath, |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 230 | ModuleTag: "{.public" + tagSuffix[i], |
| 231 | Scope: "public", |
| 232 | }) |
| 233 | textFiles = append(textFiles, MergedTxtDefinition{ |
| 234 | TxtFilename: f, |
| 235 | BaseTxt: ":non-updatable-system-" + f, |
Paul Duffin | 57533b4 | 2022-01-25 16:25:35 +0000 | [diff] [blame] | 236 | Modules: bootclasspath, |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 237 | ModuleTag: "{.system" + tagSuffix[i], |
| 238 | Scope: "system", |
| 239 | }) |
| 240 | textFiles = append(textFiles, MergedTxtDefinition{ |
| 241 | TxtFilename: f, |
| 242 | BaseTxt: ":non-updatable-module-lib-" + f, |
Paul Duffin | 57533b4 | 2022-01-25 16:25:35 +0000 | [diff] [blame] | 243 | Modules: bootclasspath, |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 244 | ModuleTag: "{.module-lib" + tagSuffix[i], |
| 245 | Scope: "module-lib", |
| 246 | }) |
| 247 | textFiles = append(textFiles, MergedTxtDefinition{ |
| 248 | TxtFilename: f, |
| 249 | BaseTxt: ":non-updatable-system-server-" + f, |
Anton Hansson | 07a1295 | 2022-01-12 17:28:39 +0000 | [diff] [blame] | 250 | Modules: system_server_classpath, |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 251 | ModuleTag: "{.system-server" + tagSuffix[i], |
| 252 | Scope: "system-server", |
| 253 | }) |
| 254 | } |
| 255 | for _, txt := range textFiles { |
| 256 | createMergedTxt(ctx, txt) |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) { |
Anton Hansson | 07a1295 | 2022-01-12 17:28:39 +0000 | [diff] [blame] | 261 | bootclasspath := a.properties.Bootclasspath |
| 262 | if ctx.Config().VendorConfig("ANDROID").Bool("include_nonpublic_framework_api") { |
| 263 | bootclasspath = append(bootclasspath, a.properties.Conditional_bootclasspath...) |
| 264 | sort.Strings(bootclasspath) |
| 265 | } |
| 266 | createMergedTxts(ctx, bootclasspath, a.properties.System_server_classpath) |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 267 | |
Anton Hansson | 07a1295 | 2022-01-12 17:28:39 +0000 | [diff] [blame] | 268 | createMergedStubsSrcjar(ctx, bootclasspath) |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 269 | |
Anton Hansson | c6e9d2f | 2022-01-25 15:53:43 +0000 | [diff] [blame] | 270 | createMergedPublicStubs(ctx, bootclasspath) |
| 271 | createMergedSystemStubs(ctx, bootclasspath) |
Anton Hansson | cb00f94 | 2022-01-13 09:45:12 +0000 | [diff] [blame] | 272 | createMergedModuleLibStubs(ctx, bootclasspath) |
| 273 | |
Anton Hansson | 05e944d | 2022-01-13 12:26:30 +0000 | [diff] [blame] | 274 | createMergedAnnotations(ctx, bootclasspath) |
Anton Hansson | cc18e03 | 2022-01-12 14:45:22 +0000 | [diff] [blame] | 275 | |
Anton Hansson | 05e944d | 2022-01-13 12:26:30 +0000 | [diff] [blame] | 276 | createFilteredApiVersions(ctx, bootclasspath) |
Anton Hansson | 4468d7c | 2022-01-14 12:10:01 +0000 | [diff] [blame] | 277 | |
| 278 | createPublicStubsSourceFilegroup(ctx, bootclasspath) |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | func combinedApisModuleFactory() android.Module { |
| 282 | module := &CombinedApis{} |
| 283 | module.AddProperties(&module.properties) |
| 284 | android.InitAndroidModule(module) |
| 285 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) }) |
| 286 | return module |
| 287 | } |
Anton Hansson | fd31645 | 2022-01-14 11:15:52 +0000 | [diff] [blame] | 288 | |
| 289 | // Various utility methods below. |
| 290 | |
| 291 | // Creates an array of ":<m><tag>" for each m in <modules>. |
| 292 | func createSrcs(modules []string, tag string) []string { |
| 293 | return transformArray(modules, ":", tag) |
| 294 | } |
| 295 | |
| 296 | // Creates an array of "<prefix><m><suffix>", for each m in <modules>. |
| 297 | func transformArray(modules []string, prefix, suffix string) []string { |
| 298 | a := make([]string, 0, len(modules)) |
| 299 | for _, module := range modules { |
| 300 | a = append(a, prefix+module+suffix) |
| 301 | } |
| 302 | return a |
| 303 | } |
| 304 | |
| 305 | func removeAll(s []string, vs []string) []string { |
| 306 | for _, v := range vs { |
| 307 | s = remove(s, v) |
| 308 | } |
| 309 | return s |
| 310 | } |
| 311 | |
| 312 | func remove(s []string, v string) []string { |
| 313 | s2 := make([]string, 0, len(s)) |
| 314 | for _, sv := range s { |
| 315 | if sv != v { |
| 316 | s2 = append(s2, sv) |
| 317 | } |
| 318 | } |
| 319 | return s2 |
| 320 | } |