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