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