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