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 ( |
Jihoon Kang | 1e4ac1d | 2023-04-07 18:50:38 +0000 | [diff] [blame] | 18 | "fmt" |
Anton Hansson | 07a1295 | 2022-01-12 17:28:39 +0000 | [diff] [blame] | 19 | "sort" |
Jihoon Kang | 1e4ac1d | 2023-04-07 18:50:38 +0000 | [diff] [blame] | 20 | "strings" |
Anton Hansson | 07a1295 | 2022-01-12 17:28:39 +0000 | [diff] [blame] | 21 | |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
| 23 | |
| 24 | "android/soong/android" |
Zi Wang | 0d6a530 | 2023-02-16 14:54:01 -0800 | [diff] [blame] | 25 | "android/soong/bazel" |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 26 | "android/soong/genrule" |
Anton Hansson | cb00f94 | 2022-01-13 09:45:12 +0000 | [diff] [blame] | 27 | "android/soong/java" |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 28 | ) |
| 29 | |
Anton Hansson | 05e944d | 2022-01-13 12:26:30 +0000 | [diff] [blame] | 30 | const art = "art.module.public.api" |
| 31 | const conscrypt = "conscrypt.module.public.api" |
| 32 | const i18n = "i18n.module.public.api" |
Nikita Ioffe | d3f0a6f | 2022-11-15 11:26:53 +0000 | [diff] [blame] | 33 | const virtualization = "framework-virtualization" |
Anton Hansson | 09a9c4e | 2022-04-08 10:59:46 +0100 | [diff] [blame] | 34 | |
Anton Hansson | 95e89a8 | 2022-01-28 11:31:50 +0000 | [diff] [blame] | 35 | var core_libraries_modules = []string{art, conscrypt, i18n} |
Zi Wang | 0d6a530 | 2023-02-16 14:54:01 -0800 | [diff] [blame] | 36 | |
Nikita Ioffe | d3f0a6f | 2022-11-15 11:26:53 +0000 | [diff] [blame] | 37 | // List of modules that are not yet updatable, and hence they can still compile |
| 38 | // against hidden APIs. These modules are filtered out when building the |
| 39 | // updatable-framework-module-impl (because updatable-framework-module-impl is |
| 40 | // built against module_current SDK). Instead they are directly statically |
| 41 | // linked into the all-framework-module-lib, which is building against hidden |
| 42 | // APIs. |
Nikita Ioffe | 5593fbb | 2022-12-01 14:52:34 +0000 | [diff] [blame] | 43 | // In addition, the modules in this list are allowed to contribute to test APIs |
| 44 | // stubs. |
Nikita Ioffe | d3f0a6f | 2022-11-15 11:26:53 +0000 | [diff] [blame] | 45 | var non_updatable_modules = []string{virtualization} |
Anton Hansson | 05e944d | 2022-01-13 12:26:30 +0000 | [diff] [blame] | 46 | |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 47 | // The intention behind this soong plugin is to generate a number of "merged" |
| 48 | // API-related modules that would otherwise require a large amount of very |
| 49 | // similar Android.bp boilerplate to define. For example, the merged current.txt |
| 50 | // API definitions (created by merging the non-updatable current.txt with all |
| 51 | // the module current.txts). This simplifies the addition of new android |
| 52 | // modules, by reducing the number of genrules etc a new module must be added to. |
| 53 | |
| 54 | // The properties of the combined_apis module type. |
| 55 | type CombinedApisProperties struct { |
Anton Hansson | 16ff357 | 2022-01-11 18:36:35 +0000 | [diff] [blame] | 56 | // Module libraries in the bootclasspath |
| 57 | Bootclasspath []string |
Anton Hansson | 07a1295 | 2022-01-12 17:28:39 +0000 | [diff] [blame] | 58 | // Module libraries on the bootclasspath if include_nonpublic_framework_api is true. |
| 59 | Conditional_bootclasspath []string |
Anton Hansson | 16ff357 | 2022-01-11 18:36:35 +0000 | [diff] [blame] | 60 | // Module libraries in system server |
| 61 | System_server_classpath []string |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | type CombinedApis struct { |
| 65 | android.ModuleBase |
Zi Wang | 0d6a530 | 2023-02-16 14:54:01 -0800 | [diff] [blame] | 66 | android.BazelModuleBase |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 67 | |
| 68 | properties CombinedApisProperties |
| 69 | } |
| 70 | |
| 71 | func init() { |
| 72 | registerBuildComponents(android.InitRegistrationContext) |
| 73 | } |
| 74 | |
| 75 | func registerBuildComponents(ctx android.RegistrationContext) { |
| 76 | ctx.RegisterModuleType("combined_apis", combinedApisModuleFactory) |
| 77 | } |
| 78 | |
| 79 | var PrepareForCombinedApisTest = android.FixtureRegisterWithContext(registerBuildComponents) |
| 80 | |
| 81 | func (a *CombinedApis) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 82 | } |
| 83 | |
| 84 | type genruleProps struct { |
| 85 | Name *string |
| 86 | Cmd *string |
| 87 | Dists []android.Dist |
| 88 | Out []string |
| 89 | Srcs []string |
| 90 | Tools []string |
| 91 | Visibility []string |
| 92 | } |
| 93 | |
Anton Hansson | cb00f94 | 2022-01-13 09:45:12 +0000 | [diff] [blame] | 94 | type libraryProps struct { |
| 95 | Name *string |
| 96 | Sdk_version *string |
| 97 | Static_libs []string |
| 98 | Visibility []string |
Jihoon Kang | 1453baa | 2023-05-27 05:32:30 +0000 | [diff] [blame] | 99 | Defaults []string |
Anton Hansson | cb00f94 | 2022-01-13 09:45:12 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Anton Hansson | 4468d7c | 2022-01-14 12:10:01 +0000 | [diff] [blame] | 102 | type fgProps struct { |
| 103 | Name *string |
| 104 | Srcs []string |
| 105 | Visibility []string |
| 106 | } |
| 107 | |
Jihoon Kang | 1e4ac1d | 2023-04-07 18:50:38 +0000 | [diff] [blame] | 108 | type defaultsProps struct { |
| 109 | Name *string |
| 110 | Api_surface *string |
| 111 | Api_contributions []string |
| 112 | Defaults_visibility []string |
Jihoon Kang | 471a05b | 2023-08-01 06:37:17 +0000 | [diff] [blame] | 113 | Previous_api *string |
Jihoon Kang | 1e4ac1d | 2023-04-07 18:50:38 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Zi Wang | 0d6a530 | 2023-02-16 14:54:01 -0800 | [diff] [blame] | 116 | type Bazel_module struct { |
Chris Parsons | 3b7e34b | 2023-09-27 22:34:57 +0000 | [diff] [blame] | 117 | Label *string |
Zi Wang | 0d6a530 | 2023-02-16 14:54:01 -0800 | [diff] [blame] | 118 | Bp2build_available *bool |
| 119 | } |
| 120 | type bazelProperties struct { |
| 121 | *Bazel_module |
| 122 | } |
| 123 | |
| 124 | var bp2buildNotAvailable = bazelProperties{ |
| 125 | &Bazel_module{ |
| 126 | Bp2build_available: proptools.BoolPtr(false), |
| 127 | }, |
| 128 | } |
| 129 | |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 130 | // Struct to pass parameters for the various merged [current|removed].txt file modules we create. |
| 131 | type MergedTxtDefinition struct { |
| 132 | // "current.txt" or "removed.txt" |
| 133 | TxtFilename string |
Anton Hansson | 09a9c4e | 2022-04-08 10:59:46 +0100 | [diff] [blame] | 134 | // Filename in the new dist dir. "android.txt" or "android-removed.txt" |
| 135 | DistFilename string |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 136 | // The module for the non-updatable / non-module part of the api. |
| 137 | BaseTxt string |
| 138 | // The list of modules that are relevant for this merged txt. |
| 139 | Modules []string |
| 140 | // The output tag for each module to use.e.g. {.public.api.txt} for current.txt |
| 141 | ModuleTag string |
| 142 | // public, system, module-lib or system-server |
| 143 | Scope string |
Chris Parsons | 3b7e34b | 2023-09-27 22:34:57 +0000 | [diff] [blame] | 144 | // True if there is a bp2build definition for this module |
| 145 | Bp2buildDefined bool |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | func createMergedTxt(ctx android.LoadHookContext, txt MergedTxtDefinition) { |
| 149 | metalavaCmd := "$(location metalava)" |
| 150 | // Silence reflection warnings. See b/168689341 |
| 151 | metalavaCmd += " -J--add-opens=java.base/java.util=ALL-UNNAMED " |
Paul Duffin | f3b1fc4 | 2023-08-14 22:03:06 +0100 | [diff] [blame] | 152 | metalavaCmd += " --quiet merge-signatures --format=v2 " |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 153 | |
| 154 | filename := txt.TxtFilename |
| 155 | if txt.Scope != "public" { |
| 156 | filename = txt.Scope + "-" + filename |
| 157 | } |
Chris Parsons | 3b7e34b | 2023-09-27 22:34:57 +0000 | [diff] [blame] | 158 | moduleName := ctx.ModuleName() + "-" + filename |
| 159 | |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 160 | props := genruleProps{} |
Chris Parsons | 3b7e34b | 2023-09-27 22:34:57 +0000 | [diff] [blame] | 161 | props.Name = proptools.StringPtr(moduleName) |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 162 | props.Tools = []string{"metalava"} |
Anton Hansson | 16ff357 | 2022-01-11 18:36:35 +0000 | [diff] [blame] | 163 | props.Out = []string{filename} |
Paul Duffin | f3b1fc4 | 2023-08-14 22:03:06 +0100 | [diff] [blame] | 164 | props.Cmd = proptools.StringPtr(metalavaCmd + "$(in) --out $(out)") |
Anton Hansson | fd31645 | 2022-01-14 11:15:52 +0000 | [diff] [blame] | 165 | props.Srcs = append([]string{txt.BaseTxt}, createSrcs(txt.Modules, txt.ModuleTag)...) |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 166 | props.Dists = []android.Dist{ |
| 167 | { |
| 168 | Targets: []string{"droidcore"}, |
| 169 | Dir: proptools.StringPtr("api"), |
| 170 | Dest: proptools.StringPtr(filename), |
| 171 | }, |
| 172 | { |
Anton Hansson | 09a9c4e | 2022-04-08 10:59:46 +0100 | [diff] [blame] | 173 | Targets: []string{"api_txt", "sdk"}, |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 174 | Dir: proptools.StringPtr("apistubs/android/" + txt.Scope + "/api"), |
Anton Hansson | 09a9c4e | 2022-04-08 10:59:46 +0100 | [diff] [blame] | 175 | Dest: proptools.StringPtr(txt.DistFilename), |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 176 | }, |
| 177 | } |
| 178 | props.Visibility = []string{"//visibility:public"} |
Chris Parsons | 3b7e34b | 2023-09-27 22:34:57 +0000 | [diff] [blame] | 179 | bazelProps := bazelProperties{ |
| 180 | &Bazel_module{ |
| 181 | Bp2build_available: proptools.BoolPtr(false), |
| 182 | }, |
| 183 | } |
| 184 | if txt.Bp2buildDefined { |
| 185 | moduleDir := ctx.ModuleDir() |
| 186 | if moduleDir == android.Bp2BuildTopLevel { |
| 187 | moduleDir = "" |
| 188 | } |
| 189 | label := fmt.Sprintf("//%s:%s", moduleDir, moduleName) |
| 190 | bazelProps.Label = &label |
| 191 | } |
| 192 | ctx.CreateModule(genrule.GenRuleFactory, &props, &bazelProps) |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Cole Faust | dcda370 | 2022-10-04 14:46:35 -0700 | [diff] [blame] | 195 | func createMergedAnnotationsFilegroups(ctx android.LoadHookContext, modules, system_server_modules []string) { |
Jihoon Kang | 1e4ac1d | 2023-04-07 18:50:38 +0000 | [diff] [blame] | 196 | for _, i := range []struct { |
Cole Faust | dcda370 | 2022-10-04 14:46:35 -0700 | [diff] [blame] | 197 | name string |
| 198 | tag string |
| 199 | modules []string |
| 200 | }{ |
| 201 | { |
Jihoon Kang | 1e4ac1d | 2023-04-07 18:50:38 +0000 | [diff] [blame] | 202 | name: "all-modules-public-annotations", |
| 203 | tag: "{.public.annotations.zip}", |
Cole Faust | dcda370 | 2022-10-04 14:46:35 -0700 | [diff] [blame] | 204 | modules: modules, |
| 205 | }, { |
Jihoon Kang | 1e4ac1d | 2023-04-07 18:50:38 +0000 | [diff] [blame] | 206 | name: "all-modules-system-annotations", |
| 207 | tag: "{.system.annotations.zip}", |
Cole Faust | dcda370 | 2022-10-04 14:46:35 -0700 | [diff] [blame] | 208 | modules: modules, |
| 209 | }, { |
Jihoon Kang | 1e4ac1d | 2023-04-07 18:50:38 +0000 | [diff] [blame] | 210 | name: "all-modules-module-lib-annotations", |
| 211 | tag: "{.module-lib.annotations.zip}", |
Cole Faust | dcda370 | 2022-10-04 14:46:35 -0700 | [diff] [blame] | 212 | modules: modules, |
| 213 | }, { |
Jihoon Kang | 1e4ac1d | 2023-04-07 18:50:38 +0000 | [diff] [blame] | 214 | name: "all-modules-system-server-annotations", |
| 215 | tag: "{.system-server.annotations.zip}", |
Cole Faust | dcda370 | 2022-10-04 14:46:35 -0700 | [diff] [blame] | 216 | modules: system_server_modules, |
| 217 | }, |
| 218 | } { |
| 219 | props := fgProps{} |
| 220 | props.Name = proptools.StringPtr(i.name) |
| 221 | props.Srcs = createSrcs(i.modules, i.tag) |
Zi Wang | 0d6a530 | 2023-02-16 14:54:01 -0800 | [diff] [blame] | 222 | ctx.CreateModule(android.FileGroupFactory, &props, &bp2buildNotAvailable) |
Cole Faust | dcda370 | 2022-10-04 14:46:35 -0700 | [diff] [blame] | 223 | } |
Anton Hansson | 74b1564 | 2022-06-23 08:27:23 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Anton Hansson | c6e9d2f | 2022-01-25 15:53:43 +0000 | [diff] [blame] | 226 | func createMergedPublicStubs(ctx android.LoadHookContext, modules []string) { |
| 227 | props := libraryProps{} |
| 228 | props.Name = proptools.StringPtr("all-modules-public-stubs") |
| 229 | props.Static_libs = transformArray(modules, "", ".stubs") |
| 230 | props.Sdk_version = proptools.StringPtr("module_current") |
| 231 | props.Visibility = []string{"//frameworks/base"} |
| 232 | ctx.CreateModule(java.LibraryFactory, &props) |
| 233 | } |
| 234 | |
| 235 | func createMergedSystemStubs(ctx android.LoadHookContext, modules []string) { |
Nikita Ioffe | 5593fbb | 2022-12-01 14:52:34 +0000 | [diff] [blame] | 236 | // First create the all-updatable-modules-system-stubs |
| 237 | { |
| 238 | updatable_modules := removeAll(modules, non_updatable_modules) |
| 239 | props := libraryProps{} |
| 240 | props.Name = proptools.StringPtr("all-updatable-modules-system-stubs") |
| 241 | props.Static_libs = transformArray(updatable_modules, "", ".stubs.system") |
| 242 | props.Sdk_version = proptools.StringPtr("module_current") |
| 243 | props.Visibility = []string{"//frameworks/base"} |
| 244 | ctx.CreateModule(java.LibraryFactory, &props) |
| 245 | } |
| 246 | // Now merge all-updatable-modules-system-stubs and stubs from non-updatable modules |
| 247 | // into all-modules-system-stubs. |
| 248 | { |
| 249 | props := libraryProps{} |
| 250 | props.Name = proptools.StringPtr("all-modules-system-stubs") |
| 251 | props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.system") |
| 252 | props.Static_libs = append(props.Static_libs, "all-updatable-modules-system-stubs") |
| 253 | props.Sdk_version = proptools.StringPtr("module_current") |
| 254 | props.Visibility = []string{"//frameworks/base"} |
| 255 | ctx.CreateModule(java.LibraryFactory, &props) |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | func createMergedTestStubsForNonUpdatableModules(ctx android.LoadHookContext) { |
Anton Hansson | c6e9d2f | 2022-01-25 15:53:43 +0000 | [diff] [blame] | 260 | props := libraryProps{} |
Nikita Ioffe | 5593fbb | 2022-12-01 14:52:34 +0000 | [diff] [blame] | 261 | props.Name = proptools.StringPtr("all-non-updatable-modules-test-stubs") |
| 262 | props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.test") |
Anton Hansson | c6e9d2f | 2022-01-25 15:53:43 +0000 | [diff] [blame] | 263 | props.Sdk_version = proptools.StringPtr("module_current") |
| 264 | props.Visibility = []string{"//frameworks/base"} |
| 265 | ctx.CreateModule(java.LibraryFactory, &props) |
| 266 | } |
| 267 | |
Anton Hansson | 95e89a8 | 2022-01-28 11:31:50 +0000 | [diff] [blame] | 268 | func createMergedFrameworkImpl(ctx android.LoadHookContext, modules []string) { |
| 269 | // This module is for the "framework-all" module, which should not include the core libraries. |
| 270 | modules = removeAll(modules, core_libraries_modules) |
Nikita Ioffe | d3f0a6f | 2022-11-15 11:26:53 +0000 | [diff] [blame] | 271 | // Remove the modules that belong to non-updatable APEXes since those are allowed to compile |
| 272 | // against unstable APIs. |
| 273 | modules = removeAll(modules, non_updatable_modules) |
| 274 | // First create updatable-framework-module-impl, which contains all updatable modules. |
| 275 | // This module compiles against module_lib SDK. |
| 276 | { |
| 277 | props := libraryProps{} |
| 278 | props.Name = proptools.StringPtr("updatable-framework-module-impl") |
| 279 | props.Static_libs = transformArray(modules, "", ".impl") |
| 280 | props.Sdk_version = proptools.StringPtr("module_current") |
| 281 | props.Visibility = []string{"//frameworks/base"} |
| 282 | ctx.CreateModule(java.LibraryFactory, &props) |
| 283 | } |
| 284 | |
| 285 | // Now create all-framework-module-impl, which contains updatable-framework-module-impl |
| 286 | // and all non-updatable modules. This module compiles against hidden APIs. |
| 287 | { |
| 288 | props := libraryProps{} |
| 289 | props.Name = proptools.StringPtr("all-framework-module-impl") |
| 290 | props.Static_libs = transformArray(non_updatable_modules, "", ".impl") |
| 291 | props.Static_libs = append(props.Static_libs, "updatable-framework-module-impl") |
| 292 | props.Sdk_version = proptools.StringPtr("core_platform") |
| 293 | props.Visibility = []string{"//frameworks/base"} |
| 294 | ctx.CreateModule(java.LibraryFactory, &props) |
| 295 | } |
Anton Hansson | 95e89a8 | 2022-01-28 11:31:50 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | func createMergedFrameworkModuleLibStubs(ctx android.LoadHookContext, modules []string) { |
Anton Hansson | cb00f94 | 2022-01-13 09:45:12 +0000 | [diff] [blame] | 299 | // 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] | 300 | modules = removeAll(modules, core_libraries_modules) |
Anton Hansson | cb00f94 | 2022-01-13 09:45:12 +0000 | [diff] [blame] | 301 | props := libraryProps{} |
| 302 | props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api") |
Anton Hansson | fd31645 | 2022-01-14 11:15:52 +0000 | [diff] [blame] | 303 | props.Static_libs = transformArray(modules, "", ".stubs.module_lib") |
Anton Hansson | cb00f94 | 2022-01-13 09:45:12 +0000 | [diff] [blame] | 304 | props.Sdk_version = proptools.StringPtr("module_current") |
| 305 | props.Visibility = []string{"//frameworks/base"} |
| 306 | ctx.CreateModule(java.LibraryFactory, &props) |
| 307 | } |
| 308 | |
Anton Hansson | 4468d7c | 2022-01-14 12:10:01 +0000 | [diff] [blame] | 309 | func createPublicStubsSourceFilegroup(ctx android.LoadHookContext, modules []string) { |
| 310 | props := fgProps{} |
| 311 | props.Name = proptools.StringPtr("all-modules-public-stubs-source") |
| 312 | props.Srcs = createSrcs(modules, "{.public.stubs.source}") |
| 313 | props.Visibility = []string{"//frameworks/base"} |
Zi Wang | 0d6a530 | 2023-02-16 14:54:01 -0800 | [diff] [blame] | 314 | ctx.CreateModule(android.FileGroupFactory, &props, &bp2buildNotAvailable) |
Anton Hansson | 4468d7c | 2022-01-14 12:10:01 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Anton Hansson | 07a1295 | 2022-01-12 17:28:39 +0000 | [diff] [blame] | 317 | func createMergedTxts(ctx android.LoadHookContext, bootclasspath, system_server_classpath []string) { |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 318 | var textFiles []MergedTxtDefinition |
Anton Hansson | 16ff357 | 2022-01-11 18:36:35 +0000 | [diff] [blame] | 319 | |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 320 | tagSuffix := []string{".api.txt}", ".removed-api.txt}"} |
Anton Hansson | 09a9c4e | 2022-04-08 10:59:46 +0100 | [diff] [blame] | 321 | distFilename := []string{"android.txt", "android-removed.txt"} |
Chris Parsons | 3b7e34b | 2023-09-27 22:34:57 +0000 | [diff] [blame] | 322 | bp2BuildDefined := []bool{true, false} |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 323 | for i, f := range []string{"current.txt", "removed.txt"} { |
| 324 | textFiles = append(textFiles, MergedTxtDefinition{ |
Chris Parsons | 3b7e34b | 2023-09-27 22:34:57 +0000 | [diff] [blame] | 325 | TxtFilename: f, |
| 326 | DistFilename: distFilename[i], |
| 327 | BaseTxt: ":non-updatable-" + f, |
| 328 | Modules: bootclasspath, |
| 329 | ModuleTag: "{.public" + tagSuffix[i], |
| 330 | Scope: "public", |
| 331 | Bp2buildDefined: bp2BuildDefined[i], |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 332 | }) |
| 333 | textFiles = append(textFiles, MergedTxtDefinition{ |
Chris Parsons | 3b7e34b | 2023-09-27 22:34:57 +0000 | [diff] [blame] | 334 | TxtFilename: f, |
| 335 | DistFilename: distFilename[i], |
| 336 | BaseTxt: ":non-updatable-system-" + f, |
| 337 | Modules: bootclasspath, |
| 338 | ModuleTag: "{.system" + tagSuffix[i], |
| 339 | Scope: "system", |
| 340 | Bp2buildDefined: bp2BuildDefined[i], |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 341 | }) |
| 342 | textFiles = append(textFiles, MergedTxtDefinition{ |
Chris Parsons | 3b7e34b | 2023-09-27 22:34:57 +0000 | [diff] [blame] | 343 | TxtFilename: f, |
| 344 | DistFilename: distFilename[i], |
| 345 | BaseTxt: ":non-updatable-module-lib-" + f, |
| 346 | Modules: bootclasspath, |
| 347 | ModuleTag: "{.module-lib" + tagSuffix[i], |
| 348 | Scope: "module-lib", |
| 349 | Bp2buildDefined: bp2BuildDefined[i], |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 350 | }) |
| 351 | textFiles = append(textFiles, MergedTxtDefinition{ |
Chris Parsons | 3b7e34b | 2023-09-27 22:34:57 +0000 | [diff] [blame] | 352 | TxtFilename: f, |
| 353 | DistFilename: distFilename[i], |
| 354 | BaseTxt: ":non-updatable-system-server-" + f, |
| 355 | Modules: system_server_classpath, |
| 356 | ModuleTag: "{.system-server" + tagSuffix[i], |
| 357 | Scope: "system-server", |
| 358 | Bp2buildDefined: bp2BuildDefined[i], |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 359 | }) |
| 360 | } |
| 361 | for _, txt := range textFiles { |
| 362 | createMergedTxt(ctx, txt) |
| 363 | } |
| 364 | } |
| 365 | |
Jihoon Kang | 1e4ac1d | 2023-04-07 18:50:38 +0000 | [diff] [blame] | 366 | func createApiContributionDefaults(ctx android.LoadHookContext, modules []string) { |
| 367 | defaultsSdkKinds := []android.SdkKind{ |
| 368 | android.SdkPublic, android.SdkSystem, android.SdkModule, |
| 369 | } |
| 370 | for _, sdkKind := range defaultsSdkKinds { |
| 371 | props := defaultsProps{} |
| 372 | props.Name = proptools.StringPtr( |
| 373 | sdkKind.DefaultJavaLibraryName() + "_contributions") |
| 374 | if sdkKind == android.SdkModule { |
| 375 | props.Name = proptools.StringPtr( |
| 376 | sdkKind.DefaultJavaLibraryName() + "_contributions_full") |
| 377 | } |
| 378 | props.Api_surface = proptools.StringPtr(sdkKind.String()) |
| 379 | apiSuffix := "" |
| 380 | if sdkKind != android.SdkPublic { |
| 381 | apiSuffix = "." + strings.ReplaceAll(sdkKind.String(), "-", "_") |
| 382 | } |
| 383 | props.Api_contributions = transformArray( |
| 384 | modules, "", fmt.Sprintf(".stubs.source%s.api.contribution", apiSuffix)) |
| 385 | props.Defaults_visibility = []string{"//visibility:public"} |
Jihoon Kang | 471a05b | 2023-08-01 06:37:17 +0000 | [diff] [blame] | 386 | props.Previous_api = proptools.StringPtr(":android.api.public.latest") |
Jihoon Kang | 1e4ac1d | 2023-04-07 18:50:38 +0000 | [diff] [blame] | 387 | ctx.CreateModule(java.DefaultsFactory, &props) |
| 388 | } |
| 389 | } |
| 390 | |
Jihoon Kang | 1453baa | 2023-05-27 05:32:30 +0000 | [diff] [blame] | 391 | func createFullApiLibraries(ctx android.LoadHookContext) { |
| 392 | javaLibraryNames := []string{ |
| 393 | "android_stubs_current", |
| 394 | "android_system_stubs_current", |
| 395 | "android_test_stubs_current", |
Mark White | e35b138 | 2023-08-12 01:31:26 +0000 | [diff] [blame^] | 396 | "android_test_frameworks_core_stubs_current", |
Jihoon Kang | 1453baa | 2023-05-27 05:32:30 +0000 | [diff] [blame] | 397 | "android_module_lib_stubs_current", |
| 398 | "android_system_server_stubs_current", |
| 399 | } |
| 400 | |
| 401 | for _, libraryName := range javaLibraryNames { |
| 402 | props := libraryProps{} |
| 403 | props.Name = proptools.StringPtr(libraryName) |
| 404 | staticLib := libraryName + ".from-source" |
| 405 | if ctx.Config().BuildFromTextStub() { |
| 406 | staticLib = libraryName + ".from-text" |
| 407 | } |
| 408 | props.Static_libs = []string{staticLib} |
| 409 | props.Defaults = []string{"android.jar_defaults"} |
| 410 | props.Visibility = []string{"//visibility:public"} |
| 411 | |
| 412 | ctx.CreateModule(java.LibraryFactory, &props) |
| 413 | } |
| 414 | } |
| 415 | |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 416 | func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) { |
Anton Hansson | 07a1295 | 2022-01-12 17:28:39 +0000 | [diff] [blame] | 417 | bootclasspath := a.properties.Bootclasspath |
Cole Faust | dcda370 | 2022-10-04 14:46:35 -0700 | [diff] [blame] | 418 | system_server_classpath := a.properties.System_server_classpath |
Anton Hansson | 07a1295 | 2022-01-12 17:28:39 +0000 | [diff] [blame] | 419 | if ctx.Config().VendorConfig("ANDROID").Bool("include_nonpublic_framework_api") { |
| 420 | bootclasspath = append(bootclasspath, a.properties.Conditional_bootclasspath...) |
| 421 | sort.Strings(bootclasspath) |
| 422 | } |
Cole Faust | dcda370 | 2022-10-04 14:46:35 -0700 | [diff] [blame] | 423 | createMergedTxts(ctx, bootclasspath, system_server_classpath) |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 424 | |
Anton Hansson | c6e9d2f | 2022-01-25 15:53:43 +0000 | [diff] [blame] | 425 | createMergedPublicStubs(ctx, bootclasspath) |
| 426 | createMergedSystemStubs(ctx, bootclasspath) |
Nikita Ioffe | 5593fbb | 2022-12-01 14:52:34 +0000 | [diff] [blame] | 427 | createMergedTestStubsForNonUpdatableModules(ctx) |
Anton Hansson | 95e89a8 | 2022-01-28 11:31:50 +0000 | [diff] [blame] | 428 | createMergedFrameworkModuleLibStubs(ctx, bootclasspath) |
| 429 | createMergedFrameworkImpl(ctx, bootclasspath) |
Anton Hansson | cb00f94 | 2022-01-13 09:45:12 +0000 | [diff] [blame] | 430 | |
Cole Faust | dcda370 | 2022-10-04 14:46:35 -0700 | [diff] [blame] | 431 | createMergedAnnotationsFilegroups(ctx, bootclasspath, system_server_classpath) |
Anton Hansson | cc18e03 | 2022-01-12 14:45:22 +0000 | [diff] [blame] | 432 | |
Anton Hansson | 4468d7c | 2022-01-14 12:10:01 +0000 | [diff] [blame] | 433 | createPublicStubsSourceFilegroup(ctx, bootclasspath) |
Jihoon Kang | 1e4ac1d | 2023-04-07 18:50:38 +0000 | [diff] [blame] | 434 | |
| 435 | createApiContributionDefaults(ctx, bootclasspath) |
Jihoon Kang | 1453baa | 2023-05-27 05:32:30 +0000 | [diff] [blame] | 436 | |
| 437 | createFullApiLibraries(ctx) |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | func combinedApisModuleFactory() android.Module { |
| 441 | module := &CombinedApis{} |
| 442 | module.AddProperties(&module.properties) |
| 443 | android.InitAndroidModule(module) |
| 444 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) }) |
Zi Wang | 0d6a530 | 2023-02-16 14:54:01 -0800 | [diff] [blame] | 445 | android.InitBazelModule(module) |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 446 | return module |
| 447 | } |
Anton Hansson | fd31645 | 2022-01-14 11:15:52 +0000 | [diff] [blame] | 448 | |
Zi Wang | 0d6a530 | 2023-02-16 14:54:01 -0800 | [diff] [blame] | 449 | type bazelCombinedApisAttributes struct { |
| 450 | Scope bazel.StringAttribute |
| 451 | Base bazel.LabelAttribute |
| 452 | Deps bazel.LabelListAttribute |
| 453 | } |
| 454 | |
| 455 | // combined_apis bp2build converter |
Chris Parsons | 52f9ad0 | 2023-09-13 21:30:55 +0000 | [diff] [blame] | 456 | func (a *CombinedApis) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { |
Zi Wang | 0d6a530 | 2023-02-16 14:54:01 -0800 | [diff] [blame] | 457 | basePrefix := "non-updatable" |
Zi Wang | 0d6a530 | 2023-02-16 14:54:01 -0800 | [diff] [blame] | 458 | scopeToSuffix := map[string]string{ |
| 459 | "public": "-current.txt", |
| 460 | "system": "-system-current.txt", |
| 461 | "module-lib": "-module-lib-current.txt", |
| 462 | "system-server": "-system-server-current.txt", |
| 463 | } |
| 464 | |
Jihoon Kang | 1e4ac1d | 2023-04-07 18:50:38 +0000 | [diff] [blame] | 465 | for scopeName, suffix := range scopeToSuffix { |
Zi Wang | 0d6a530 | 2023-02-16 14:54:01 -0800 | [diff] [blame] | 466 | name := a.Name() + suffix |
| 467 | |
| 468 | var scope bazel.StringAttribute |
| 469 | scope.SetValue(scopeName) |
| 470 | |
| 471 | var base bazel.LabelAttribute |
| 472 | base.SetValue(android.BazelLabelForModuleDepSingle(ctx, basePrefix+suffix)) |
| 473 | |
| 474 | var deps bazel.LabelListAttribute |
| 475 | classpath := a.properties.Bootclasspath |
| 476 | if scopeName == "system-server" { |
| 477 | classpath = a.properties.System_server_classpath |
| 478 | } |
| 479 | deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, classpath)) |
| 480 | |
| 481 | attrs := bazelCombinedApisAttributes{ |
| 482 | Scope: scope, |
| 483 | Base: base, |
| 484 | Deps: deps, |
| 485 | } |
| 486 | props := bazel.BazelTargetModuleProperties{ |
| 487 | Rule_class: "merged_txts", |
| 488 | Bzl_load_location: "//build/bazel/rules/java:merged_txts.bzl", |
| 489 | } |
| 490 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, &attrs) |
| 491 | } |
| 492 | } |
| 493 | |
Anton Hansson | fd31645 | 2022-01-14 11:15:52 +0000 | [diff] [blame] | 494 | // Various utility methods below. |
| 495 | |
| 496 | // Creates an array of ":<m><tag>" for each m in <modules>. |
| 497 | func createSrcs(modules []string, tag string) []string { |
| 498 | return transformArray(modules, ":", tag) |
| 499 | } |
| 500 | |
| 501 | // Creates an array of "<prefix><m><suffix>", for each m in <modules>. |
| 502 | func transformArray(modules []string, prefix, suffix string) []string { |
| 503 | a := make([]string, 0, len(modules)) |
| 504 | for _, module := range modules { |
| 505 | a = append(a, prefix+module+suffix) |
| 506 | } |
| 507 | return a |
| 508 | } |
| 509 | |
| 510 | func removeAll(s []string, vs []string) []string { |
| 511 | for _, v := range vs { |
| 512 | s = remove(s, v) |
| 513 | } |
| 514 | return s |
| 515 | } |
| 516 | |
| 517 | func remove(s []string, v string) []string { |
| 518 | s2 := make([]string, 0, len(s)) |
| 519 | for _, sv := range s { |
| 520 | if sv != v { |
| 521 | s2 = append(s2, sv) |
| 522 | } |
| 523 | } |
| 524 | return s2 |
| 525 | } |