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