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 |
| 113 | } |
| 114 | |
Zi Wang | 0d6a530 | 2023-02-16 14:54:01 -0800 | [diff] [blame] | 115 | type Bazel_module struct { |
| 116 | Bp2build_available *bool |
| 117 | } |
| 118 | type bazelProperties struct { |
| 119 | *Bazel_module |
| 120 | } |
| 121 | |
| 122 | var bp2buildNotAvailable = bazelProperties{ |
| 123 | &Bazel_module{ |
| 124 | Bp2build_available: proptools.BoolPtr(false), |
| 125 | }, |
| 126 | } |
| 127 | |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 128 | // Struct to pass parameters for the various merged [current|removed].txt file modules we create. |
| 129 | type MergedTxtDefinition struct { |
| 130 | // "current.txt" or "removed.txt" |
| 131 | TxtFilename string |
Anton Hansson | 09a9c4e | 2022-04-08 10:59:46 +0100 | [diff] [blame] | 132 | // Filename in the new dist dir. "android.txt" or "android-removed.txt" |
| 133 | DistFilename string |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 134 | // The module for the non-updatable / non-module part of the api. |
| 135 | BaseTxt string |
| 136 | // The list of modules that are relevant for this merged txt. |
| 137 | Modules []string |
| 138 | // The output tag for each module to use.e.g. {.public.api.txt} for current.txt |
| 139 | ModuleTag string |
| 140 | // public, system, module-lib or system-server |
| 141 | Scope string |
| 142 | } |
| 143 | |
| 144 | func createMergedTxt(ctx android.LoadHookContext, txt MergedTxtDefinition) { |
| 145 | metalavaCmd := "$(location metalava)" |
| 146 | // Silence reflection warnings. See b/168689341 |
| 147 | metalavaCmd += " -J--add-opens=java.base/java.util=ALL-UNNAMED " |
| 148 | metalavaCmd += " --quiet --no-banner --format=v2 " |
| 149 | |
| 150 | filename := txt.TxtFilename |
| 151 | if txt.Scope != "public" { |
| 152 | filename = txt.Scope + "-" + filename |
| 153 | } |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 154 | props := genruleProps{} |
| 155 | props.Name = proptools.StringPtr(ctx.ModuleName() + "-" + filename) |
| 156 | props.Tools = []string{"metalava"} |
Anton Hansson | 16ff357 | 2022-01-11 18:36:35 +0000 | [diff] [blame] | 157 | props.Out = []string{filename} |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 158 | props.Cmd = proptools.StringPtr(metalavaCmd + "$(in) --api $(out)") |
Anton Hansson | fd31645 | 2022-01-14 11:15:52 +0000 | [diff] [blame] | 159 | props.Srcs = append([]string{txt.BaseTxt}, createSrcs(txt.Modules, txt.ModuleTag)...) |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 160 | props.Dists = []android.Dist{ |
| 161 | { |
| 162 | Targets: []string{"droidcore"}, |
| 163 | Dir: proptools.StringPtr("api"), |
| 164 | Dest: proptools.StringPtr(filename), |
| 165 | }, |
| 166 | { |
Anton Hansson | 09a9c4e | 2022-04-08 10:59:46 +0100 | [diff] [blame] | 167 | Targets: []string{"api_txt", "sdk"}, |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 168 | Dir: proptools.StringPtr("apistubs/android/" + txt.Scope + "/api"), |
Anton Hansson | 09a9c4e | 2022-04-08 10:59:46 +0100 | [diff] [blame] | 169 | Dest: proptools.StringPtr(txt.DistFilename), |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 170 | }, |
| 171 | } |
| 172 | props.Visibility = []string{"//visibility:public"} |
Zi Wang | 0d6a530 | 2023-02-16 14:54:01 -0800 | [diff] [blame] | 173 | ctx.CreateModule(genrule.GenRuleFactory, &props, &bp2buildNotAvailable) |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 174 | } |
| 175 | |
Cole Faust | dcda370 | 2022-10-04 14:46:35 -0700 | [diff] [blame] | 176 | func createMergedAnnotationsFilegroups(ctx android.LoadHookContext, modules, system_server_modules []string) { |
Jihoon Kang | 1e4ac1d | 2023-04-07 18:50:38 +0000 | [diff] [blame] | 177 | for _, i := range []struct { |
Cole Faust | dcda370 | 2022-10-04 14:46:35 -0700 | [diff] [blame] | 178 | name string |
| 179 | tag string |
| 180 | modules []string |
| 181 | }{ |
| 182 | { |
Jihoon Kang | 1e4ac1d | 2023-04-07 18:50:38 +0000 | [diff] [blame] | 183 | name: "all-modules-public-annotations", |
| 184 | tag: "{.public.annotations.zip}", |
Cole Faust | dcda370 | 2022-10-04 14:46:35 -0700 | [diff] [blame] | 185 | modules: modules, |
| 186 | }, { |
Jihoon Kang | 1e4ac1d | 2023-04-07 18:50:38 +0000 | [diff] [blame] | 187 | name: "all-modules-system-annotations", |
| 188 | tag: "{.system.annotations.zip}", |
Cole Faust | dcda370 | 2022-10-04 14:46:35 -0700 | [diff] [blame] | 189 | modules: modules, |
| 190 | }, { |
Jihoon Kang | 1e4ac1d | 2023-04-07 18:50:38 +0000 | [diff] [blame] | 191 | name: "all-modules-module-lib-annotations", |
| 192 | tag: "{.module-lib.annotations.zip}", |
Cole Faust | dcda370 | 2022-10-04 14:46:35 -0700 | [diff] [blame] | 193 | modules: modules, |
| 194 | }, { |
Jihoon Kang | 1e4ac1d | 2023-04-07 18:50:38 +0000 | [diff] [blame] | 195 | name: "all-modules-system-server-annotations", |
| 196 | tag: "{.system-server.annotations.zip}", |
Cole Faust | dcda370 | 2022-10-04 14:46:35 -0700 | [diff] [blame] | 197 | modules: system_server_modules, |
| 198 | }, |
| 199 | } { |
| 200 | props := fgProps{} |
| 201 | props.Name = proptools.StringPtr(i.name) |
| 202 | props.Srcs = createSrcs(i.modules, i.tag) |
Zi Wang | 0d6a530 | 2023-02-16 14:54:01 -0800 | [diff] [blame] | 203 | ctx.CreateModule(android.FileGroupFactory, &props, &bp2buildNotAvailable) |
Cole Faust | dcda370 | 2022-10-04 14:46:35 -0700 | [diff] [blame] | 204 | } |
Anton Hansson | 74b1564 | 2022-06-23 08:27:23 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Anton Hansson | c6e9d2f | 2022-01-25 15:53:43 +0000 | [diff] [blame] | 207 | func createMergedPublicStubs(ctx android.LoadHookContext, modules []string) { |
| 208 | props := libraryProps{} |
| 209 | props.Name = proptools.StringPtr("all-modules-public-stubs") |
| 210 | props.Static_libs = transformArray(modules, "", ".stubs") |
| 211 | props.Sdk_version = proptools.StringPtr("module_current") |
| 212 | props.Visibility = []string{"//frameworks/base"} |
| 213 | ctx.CreateModule(java.LibraryFactory, &props) |
| 214 | } |
| 215 | |
| 216 | func createMergedSystemStubs(ctx android.LoadHookContext, modules []string) { |
Nikita Ioffe | 5593fbb | 2022-12-01 14:52:34 +0000 | [diff] [blame] | 217 | // First create the all-updatable-modules-system-stubs |
| 218 | { |
| 219 | updatable_modules := removeAll(modules, non_updatable_modules) |
| 220 | props := libraryProps{} |
| 221 | props.Name = proptools.StringPtr("all-updatable-modules-system-stubs") |
| 222 | props.Static_libs = transformArray(updatable_modules, "", ".stubs.system") |
| 223 | props.Sdk_version = proptools.StringPtr("module_current") |
| 224 | props.Visibility = []string{"//frameworks/base"} |
| 225 | ctx.CreateModule(java.LibraryFactory, &props) |
| 226 | } |
| 227 | // Now merge all-updatable-modules-system-stubs and stubs from non-updatable modules |
| 228 | // into all-modules-system-stubs. |
| 229 | { |
| 230 | props := libraryProps{} |
| 231 | props.Name = proptools.StringPtr("all-modules-system-stubs") |
| 232 | props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.system") |
| 233 | props.Static_libs = append(props.Static_libs, "all-updatable-modules-system-stubs") |
| 234 | props.Sdk_version = proptools.StringPtr("module_current") |
| 235 | props.Visibility = []string{"//frameworks/base"} |
| 236 | ctx.CreateModule(java.LibraryFactory, &props) |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | func createMergedTestStubsForNonUpdatableModules(ctx android.LoadHookContext) { |
Anton Hansson | c6e9d2f | 2022-01-25 15:53:43 +0000 | [diff] [blame] | 241 | props := libraryProps{} |
Nikita Ioffe | 5593fbb | 2022-12-01 14:52:34 +0000 | [diff] [blame] | 242 | props.Name = proptools.StringPtr("all-non-updatable-modules-test-stubs") |
| 243 | props.Static_libs = transformArray(non_updatable_modules, "", ".stubs.test") |
Anton Hansson | c6e9d2f | 2022-01-25 15:53:43 +0000 | [diff] [blame] | 244 | props.Sdk_version = proptools.StringPtr("module_current") |
| 245 | props.Visibility = []string{"//frameworks/base"} |
| 246 | ctx.CreateModule(java.LibraryFactory, &props) |
| 247 | } |
| 248 | |
Anton Hansson | 95e89a8 | 2022-01-28 11:31:50 +0000 | [diff] [blame] | 249 | func createMergedFrameworkImpl(ctx android.LoadHookContext, modules []string) { |
| 250 | // This module is for the "framework-all" module, which should not include the core libraries. |
| 251 | modules = removeAll(modules, core_libraries_modules) |
Nikita Ioffe | d3f0a6f | 2022-11-15 11:26:53 +0000 | [diff] [blame] | 252 | // Remove the modules that belong to non-updatable APEXes since those are allowed to compile |
| 253 | // against unstable APIs. |
| 254 | modules = removeAll(modules, non_updatable_modules) |
| 255 | // First create updatable-framework-module-impl, which contains all updatable modules. |
| 256 | // This module compiles against module_lib SDK. |
| 257 | { |
| 258 | props := libraryProps{} |
| 259 | props.Name = proptools.StringPtr("updatable-framework-module-impl") |
| 260 | props.Static_libs = transformArray(modules, "", ".impl") |
| 261 | props.Sdk_version = proptools.StringPtr("module_current") |
| 262 | props.Visibility = []string{"//frameworks/base"} |
| 263 | ctx.CreateModule(java.LibraryFactory, &props) |
| 264 | } |
| 265 | |
| 266 | // Now create all-framework-module-impl, which contains updatable-framework-module-impl |
| 267 | // and all non-updatable modules. This module compiles against hidden APIs. |
| 268 | { |
| 269 | props := libraryProps{} |
| 270 | props.Name = proptools.StringPtr("all-framework-module-impl") |
| 271 | props.Static_libs = transformArray(non_updatable_modules, "", ".impl") |
| 272 | props.Static_libs = append(props.Static_libs, "updatable-framework-module-impl") |
| 273 | props.Sdk_version = proptools.StringPtr("core_platform") |
| 274 | props.Visibility = []string{"//frameworks/base"} |
| 275 | ctx.CreateModule(java.LibraryFactory, &props) |
| 276 | } |
Anton Hansson | 95e89a8 | 2022-01-28 11:31:50 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | func createMergedFrameworkModuleLibStubs(ctx android.LoadHookContext, modules []string) { |
Anton Hansson | cb00f94 | 2022-01-13 09:45:12 +0000 | [diff] [blame] | 280 | // 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] | 281 | modules = removeAll(modules, core_libraries_modules) |
Anton Hansson | cb00f94 | 2022-01-13 09:45:12 +0000 | [diff] [blame] | 282 | props := libraryProps{} |
| 283 | props.Name = proptools.StringPtr("framework-updatable-stubs-module_libs_api") |
Anton Hansson | fd31645 | 2022-01-14 11:15:52 +0000 | [diff] [blame] | 284 | props.Static_libs = transformArray(modules, "", ".stubs.module_lib") |
Anton Hansson | cb00f94 | 2022-01-13 09:45:12 +0000 | [diff] [blame] | 285 | props.Sdk_version = proptools.StringPtr("module_current") |
| 286 | props.Visibility = []string{"//frameworks/base"} |
| 287 | ctx.CreateModule(java.LibraryFactory, &props) |
| 288 | } |
| 289 | |
Anton Hansson | 4468d7c | 2022-01-14 12:10:01 +0000 | [diff] [blame] | 290 | func createPublicStubsSourceFilegroup(ctx android.LoadHookContext, modules []string) { |
| 291 | props := fgProps{} |
| 292 | props.Name = proptools.StringPtr("all-modules-public-stubs-source") |
| 293 | props.Srcs = createSrcs(modules, "{.public.stubs.source}") |
| 294 | props.Visibility = []string{"//frameworks/base"} |
Zi Wang | 0d6a530 | 2023-02-16 14:54:01 -0800 | [diff] [blame] | 295 | ctx.CreateModule(android.FileGroupFactory, &props, &bp2buildNotAvailable) |
Anton Hansson | 4468d7c | 2022-01-14 12:10:01 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Anton Hansson | 07a1295 | 2022-01-12 17:28:39 +0000 | [diff] [blame] | 298 | func createMergedTxts(ctx android.LoadHookContext, bootclasspath, system_server_classpath []string) { |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 299 | var textFiles []MergedTxtDefinition |
Anton Hansson | 16ff357 | 2022-01-11 18:36:35 +0000 | [diff] [blame] | 300 | |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 301 | tagSuffix := []string{".api.txt}", ".removed-api.txt}"} |
Anton Hansson | 09a9c4e | 2022-04-08 10:59:46 +0100 | [diff] [blame] | 302 | distFilename := []string{"android.txt", "android-removed.txt"} |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 303 | for i, f := range []string{"current.txt", "removed.txt"} { |
| 304 | textFiles = append(textFiles, MergedTxtDefinition{ |
Anton Hansson | 09a9c4e | 2022-04-08 10:59:46 +0100 | [diff] [blame] | 305 | TxtFilename: f, |
| 306 | DistFilename: distFilename[i], |
| 307 | BaseTxt: ":non-updatable-" + f, |
| 308 | Modules: bootclasspath, |
| 309 | ModuleTag: "{.public" + tagSuffix[i], |
| 310 | Scope: "public", |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 311 | }) |
| 312 | textFiles = append(textFiles, MergedTxtDefinition{ |
Anton Hansson | 09a9c4e | 2022-04-08 10:59:46 +0100 | [diff] [blame] | 313 | TxtFilename: f, |
| 314 | DistFilename: distFilename[i], |
| 315 | BaseTxt: ":non-updatable-system-" + f, |
| 316 | Modules: bootclasspath, |
| 317 | ModuleTag: "{.system" + tagSuffix[i], |
| 318 | Scope: "system", |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 319 | }) |
| 320 | textFiles = append(textFiles, MergedTxtDefinition{ |
Anton Hansson | 09a9c4e | 2022-04-08 10:59:46 +0100 | [diff] [blame] | 321 | TxtFilename: f, |
| 322 | DistFilename: distFilename[i], |
| 323 | BaseTxt: ":non-updatable-module-lib-" + f, |
| 324 | Modules: bootclasspath, |
| 325 | ModuleTag: "{.module-lib" + tagSuffix[i], |
| 326 | Scope: "module-lib", |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 327 | }) |
| 328 | textFiles = append(textFiles, MergedTxtDefinition{ |
Anton Hansson | 09a9c4e | 2022-04-08 10:59:46 +0100 | [diff] [blame] | 329 | TxtFilename: f, |
| 330 | DistFilename: distFilename[i], |
| 331 | BaseTxt: ":non-updatable-system-server-" + f, |
| 332 | Modules: system_server_classpath, |
| 333 | ModuleTag: "{.system-server" + tagSuffix[i], |
| 334 | Scope: "system-server", |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 335 | }) |
| 336 | } |
| 337 | for _, txt := range textFiles { |
| 338 | createMergedTxt(ctx, txt) |
| 339 | } |
| 340 | } |
| 341 | |
Jihoon Kang | 1e4ac1d | 2023-04-07 18:50:38 +0000 | [diff] [blame] | 342 | func createApiContributionDefaults(ctx android.LoadHookContext, modules []string) { |
| 343 | defaultsSdkKinds := []android.SdkKind{ |
| 344 | android.SdkPublic, android.SdkSystem, android.SdkModule, |
| 345 | } |
| 346 | for _, sdkKind := range defaultsSdkKinds { |
| 347 | props := defaultsProps{} |
| 348 | props.Name = proptools.StringPtr( |
| 349 | sdkKind.DefaultJavaLibraryName() + "_contributions") |
| 350 | if sdkKind == android.SdkModule { |
| 351 | props.Name = proptools.StringPtr( |
| 352 | sdkKind.DefaultJavaLibraryName() + "_contributions_full") |
| 353 | } |
| 354 | props.Api_surface = proptools.StringPtr(sdkKind.String()) |
| 355 | apiSuffix := "" |
| 356 | if sdkKind != android.SdkPublic { |
| 357 | apiSuffix = "." + strings.ReplaceAll(sdkKind.String(), "-", "_") |
| 358 | } |
| 359 | props.Api_contributions = transformArray( |
| 360 | modules, "", fmt.Sprintf(".stubs.source%s.api.contribution", apiSuffix)) |
| 361 | props.Defaults_visibility = []string{"//visibility:public"} |
| 362 | ctx.CreateModule(java.DefaultsFactory, &props) |
| 363 | } |
| 364 | } |
| 365 | |
Jihoon Kang | 1453baa | 2023-05-27 05:32:30 +0000 | [diff] [blame] | 366 | func createFullApiLibraries(ctx android.LoadHookContext) { |
| 367 | javaLibraryNames := []string{ |
| 368 | "android_stubs_current", |
| 369 | "android_system_stubs_current", |
| 370 | "android_test_stubs_current", |
| 371 | "android_module_lib_stubs_current", |
| 372 | "android_system_server_stubs_current", |
| 373 | } |
| 374 | |
| 375 | for _, libraryName := range javaLibraryNames { |
| 376 | props := libraryProps{} |
| 377 | props.Name = proptools.StringPtr(libraryName) |
| 378 | staticLib := libraryName + ".from-source" |
| 379 | if ctx.Config().BuildFromTextStub() { |
| 380 | staticLib = libraryName + ".from-text" |
| 381 | } |
| 382 | props.Static_libs = []string{staticLib} |
| 383 | props.Defaults = []string{"android.jar_defaults"} |
| 384 | props.Visibility = []string{"//visibility:public"} |
| 385 | |
| 386 | ctx.CreateModule(java.LibraryFactory, &props) |
| 387 | } |
| 388 | } |
| 389 | |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 390 | func (a *CombinedApis) createInternalModules(ctx android.LoadHookContext) { |
Anton Hansson | 07a1295 | 2022-01-12 17:28:39 +0000 | [diff] [blame] | 391 | bootclasspath := a.properties.Bootclasspath |
Cole Faust | dcda370 | 2022-10-04 14:46:35 -0700 | [diff] [blame] | 392 | system_server_classpath := a.properties.System_server_classpath |
Anton Hansson | 07a1295 | 2022-01-12 17:28:39 +0000 | [diff] [blame] | 393 | if ctx.Config().VendorConfig("ANDROID").Bool("include_nonpublic_framework_api") { |
| 394 | bootclasspath = append(bootclasspath, a.properties.Conditional_bootclasspath...) |
| 395 | sort.Strings(bootclasspath) |
| 396 | } |
Cole Faust | dcda370 | 2022-10-04 14:46:35 -0700 | [diff] [blame] | 397 | createMergedTxts(ctx, bootclasspath, system_server_classpath) |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 398 | |
Anton Hansson | c6e9d2f | 2022-01-25 15:53:43 +0000 | [diff] [blame] | 399 | createMergedPublicStubs(ctx, bootclasspath) |
| 400 | createMergedSystemStubs(ctx, bootclasspath) |
Nikita Ioffe | 5593fbb | 2022-12-01 14:52:34 +0000 | [diff] [blame] | 401 | createMergedTestStubsForNonUpdatableModules(ctx) |
Anton Hansson | 95e89a8 | 2022-01-28 11:31:50 +0000 | [diff] [blame] | 402 | createMergedFrameworkModuleLibStubs(ctx, bootclasspath) |
| 403 | createMergedFrameworkImpl(ctx, bootclasspath) |
Anton Hansson | cb00f94 | 2022-01-13 09:45:12 +0000 | [diff] [blame] | 404 | |
Cole Faust | dcda370 | 2022-10-04 14:46:35 -0700 | [diff] [blame] | 405 | createMergedAnnotationsFilegroups(ctx, bootclasspath, system_server_classpath) |
Anton Hansson | cc18e03 | 2022-01-12 14:45:22 +0000 | [diff] [blame] | 406 | |
Anton Hansson | 4468d7c | 2022-01-14 12:10:01 +0000 | [diff] [blame] | 407 | createPublicStubsSourceFilegroup(ctx, bootclasspath) |
Jihoon Kang | 1e4ac1d | 2023-04-07 18:50:38 +0000 | [diff] [blame] | 408 | |
| 409 | createApiContributionDefaults(ctx, bootclasspath) |
Jihoon Kang | 1453baa | 2023-05-27 05:32:30 +0000 | [diff] [blame] | 410 | |
| 411 | createFullApiLibraries(ctx) |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | func combinedApisModuleFactory() android.Module { |
| 415 | module := &CombinedApis{} |
| 416 | module.AddProperties(&module.properties) |
| 417 | android.InitAndroidModule(module) |
| 418 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.createInternalModules(ctx) }) |
Zi Wang | 0d6a530 | 2023-02-16 14:54:01 -0800 | [diff] [blame] | 419 | android.InitBazelModule(module) |
Anton Hansson | 0860aaf | 2021-10-08 16:48:03 +0100 | [diff] [blame] | 420 | return module |
| 421 | } |
Anton Hansson | fd31645 | 2022-01-14 11:15:52 +0000 | [diff] [blame] | 422 | |
Zi Wang | 0d6a530 | 2023-02-16 14:54:01 -0800 | [diff] [blame] | 423 | type bazelCombinedApisAttributes struct { |
| 424 | Scope bazel.StringAttribute |
| 425 | Base bazel.LabelAttribute |
| 426 | Deps bazel.LabelListAttribute |
| 427 | } |
| 428 | |
| 429 | // combined_apis bp2build converter |
| 430 | func (a *CombinedApis) ConvertWithBp2build(ctx android.TopDownMutatorContext) { |
| 431 | basePrefix := "non-updatable" |
Zi Wang | 0d6a530 | 2023-02-16 14:54:01 -0800 | [diff] [blame] | 432 | scopeToSuffix := map[string]string{ |
| 433 | "public": "-current.txt", |
| 434 | "system": "-system-current.txt", |
| 435 | "module-lib": "-module-lib-current.txt", |
| 436 | "system-server": "-system-server-current.txt", |
| 437 | } |
| 438 | |
Jihoon Kang | 1e4ac1d | 2023-04-07 18:50:38 +0000 | [diff] [blame] | 439 | for scopeName, suffix := range scopeToSuffix { |
Zi Wang | 0d6a530 | 2023-02-16 14:54:01 -0800 | [diff] [blame] | 440 | name := a.Name() + suffix |
| 441 | |
| 442 | var scope bazel.StringAttribute |
| 443 | scope.SetValue(scopeName) |
| 444 | |
| 445 | var base bazel.LabelAttribute |
| 446 | base.SetValue(android.BazelLabelForModuleDepSingle(ctx, basePrefix+suffix)) |
| 447 | |
| 448 | var deps bazel.LabelListAttribute |
| 449 | classpath := a.properties.Bootclasspath |
| 450 | if scopeName == "system-server" { |
| 451 | classpath = a.properties.System_server_classpath |
| 452 | } |
| 453 | deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, classpath)) |
| 454 | |
| 455 | attrs := bazelCombinedApisAttributes{ |
| 456 | Scope: scope, |
| 457 | Base: base, |
| 458 | Deps: deps, |
| 459 | } |
| 460 | props := bazel.BazelTargetModuleProperties{ |
| 461 | Rule_class: "merged_txts", |
| 462 | Bzl_load_location: "//build/bazel/rules/java:merged_txts.bzl", |
| 463 | } |
| 464 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, &attrs) |
| 465 | } |
| 466 | } |
| 467 | |
Anton Hansson | fd31645 | 2022-01-14 11:15:52 +0000 | [diff] [blame] | 468 | // Various utility methods below. |
| 469 | |
| 470 | // Creates an array of ":<m><tag>" for each m in <modules>. |
| 471 | func createSrcs(modules []string, tag string) []string { |
| 472 | return transformArray(modules, ":", tag) |
| 473 | } |
| 474 | |
| 475 | // Creates an array of "<prefix><m><suffix>", for each m in <modules>. |
| 476 | func transformArray(modules []string, prefix, suffix string) []string { |
| 477 | a := make([]string, 0, len(modules)) |
| 478 | for _, module := range modules { |
| 479 | a = append(a, prefix+module+suffix) |
| 480 | } |
| 481 | return a |
| 482 | } |
| 483 | |
| 484 | func removeAll(s []string, vs []string) []string { |
| 485 | for _, v := range vs { |
| 486 | s = remove(s, v) |
| 487 | } |
| 488 | return s |
| 489 | } |
| 490 | |
| 491 | func remove(s []string, v string) []string { |
| 492 | s2 := make([]string, 0, len(s)) |
| 493 | for _, sv := range s { |
| 494 | if sv != v { |
| 495 | s2 = append(s2, sv) |
| 496 | } |
| 497 | } |
| 498 | return s2 |
| 499 | } |