| Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 1 | // Copyright 2018 Google Inc. All rights reserved. | 
|  | 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 java | 
|  | 16 |  | 
|  | 17 | import ( | 
| Anton Hansson | 20ce41d | 2021-01-22 15:05:32 +0000 | [diff] [blame] | 18 | "fmt" | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 19 | "path" | 
| Anton Hansson | 370fd0b | 2021-01-22 15:05:04 +0000 | [diff] [blame] | 20 | "strconv" | 
| Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 21 | "strings" | 
|  | 22 |  | 
|  | 23 | "github.com/google/blueprint/proptools" | 
| Colin Cross | 17dec17 | 2020-05-14 18:05:32 -0700 | [diff] [blame] | 24 |  | 
|  | 25 | "android/soong/android" | 
| Anton Hansson | 20ce41d | 2021-01-22 15:05:32 +0000 | [diff] [blame] | 26 | "android/soong/genrule" | 
| Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 27 | ) | 
|  | 28 |  | 
| Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 29 | func init() { | 
| Paul Duffin | a48f758 | 2019-12-19 11:25:19 +0000 | [diff] [blame] | 30 | RegisterPrebuiltApisBuildComponents(android.InitRegistrationContext) | 
|  | 31 | } | 
| Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 32 |  | 
| Paul Duffin | a48f758 | 2019-12-19 11:25:19 +0000 | [diff] [blame] | 33 | func RegisterPrebuiltApisBuildComponents(ctx android.RegistrationContext) { | 
|  | 34 | ctx.RegisterModuleType("prebuilt_apis", PrebuiltApisFactory) | 
| Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 35 | } | 
|  | 36 |  | 
| Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 37 | type prebuiltApisProperties struct { | 
|  | 38 | // list of api version directories | 
|  | 39 | Api_dirs []string | 
| Liz Kammer | 2d2fd85 | 2020-08-12 14:42:30 -0700 | [diff] [blame] | 40 |  | 
| Anton Hansson | 3a3f169 | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 41 | // Directory containing finalized api txt files for extension versions. | 
|  | 42 | // Extension versions higher than the base sdk extension version will | 
|  | 43 | // be assumed to be finalized later than all Api_dirs. | 
|  | 44 | Extensions_dir *string | 
|  | 45 |  | 
| Anton Hansson | 20ce41d | 2021-01-22 15:05:32 +0000 | [diff] [blame] | 46 | // The next API directory can optionally point to a directory where | 
|  | 47 | // files incompatibility-tracking files are stored for the current | 
|  | 48 | // "in progress" API. Each module present in one of the api_dirs will have | 
|  | 49 | // a <module>-incompatibilities.api.<scope>.latest module created. | 
|  | 50 | Next_api_dir *string | 
|  | 51 |  | 
| Liz Kammer | 2d2fd85 | 2020-08-12 14:42:30 -0700 | [diff] [blame] | 52 | // The sdk_version of java_import modules generated based on jar files. | 
|  | 53 | // Defaults to "current" | 
|  | 54 | Imports_sdk_version *string | 
| Liz Kammer | 4e7f260 | 2020-09-02 08:37:49 -0700 | [diff] [blame] | 55 |  | 
|  | 56 | // If set to true, compile dex for java_import modules. Defaults to false. | 
|  | 57 | Imports_compile_dex *bool | 
| Todd Lee | 2ec7e1c | 2023-08-25 18:02:13 +0000 | [diff] [blame] | 58 |  | 
|  | 59 | // If set to true, allow incremental platform API of the form MM.m where MM is the major release | 
|  | 60 | // version corresponding to the API level/SDK_INT and m is an incremental release version | 
|  | 61 | // (e.g. API changes associated with QPR). Defaults to false. | 
|  | 62 | Allow_incremental_platform_api *bool | 
| Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 63 | } | 
|  | 64 |  | 
| Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 65 | type prebuiltApis struct { | 
|  | 66 | android.ModuleBase | 
| Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 67 | properties prebuiltApisProperties | 
| Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 68 | } | 
|  | 69 |  | 
| Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 70 | func (module *prebuiltApis) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
|  | 71 | // no need to implement | 
|  | 72 | } | 
|  | 73 |  | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 74 | // parsePrebuiltPath parses the relevant variables out of a variety of paths, e.g. | 
|  | 75 | // <version>/<scope>/<module>.jar | 
|  | 76 | // <version>/<scope>/api/<module>.txt | 
| Todd Lee | 2ec7e1c | 2023-08-25 18:02:13 +0000 | [diff] [blame] | 77 | // *Note when using incremental platform API, <version> may be of the form MM.m where MM is the | 
|  | 78 | // API level and m is an incremental release, otherwise <version> is a single integer corresponding to the API level only. | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 79 | // extensions/<version>/<scope>/<module>.jar | 
|  | 80 | // extensions/<version>/<scope>/api/<module>.txt | 
|  | 81 | func parsePrebuiltPath(ctx android.LoadHookContext, p string) (module string, version string, scope string) { | 
|  | 82 | elements := strings.Split(p, "/") | 
| Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 83 |  | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 84 | scopeIdx := len(elements) - 2 | 
|  | 85 | if elements[scopeIdx] == "api" { | 
|  | 86 | scopeIdx-- | 
|  | 87 | } | 
|  | 88 | scope = elements[scopeIdx] | 
|  | 89 | if scope != "core" && scope != "public" && scope != "system" && scope != "test" && scope != "module-lib" && scope != "system-server" { | 
|  | 90 | ctx.ModuleErrorf("invalid scope %q found in path: %q", scope, p) | 
| Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 91 | return | 
|  | 92 | } | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 93 | version = elements[scopeIdx-1] | 
| Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 94 |  | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 95 | module = strings.TrimSuffix(path.Base(p), path.Ext(p)) | 
| Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 96 | return | 
|  | 97 | } | 
|  | 98 |  | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 99 | // parseFinalizedPrebuiltPath is like parsePrebuiltPath, but verifies the version is numeric (a finalized version). | 
| Todd Lee | 2ec7e1c | 2023-08-25 18:02:13 +0000 | [diff] [blame] | 100 | func parseFinalizedPrebuiltPath(ctx android.LoadHookContext, p string, allowIncremental bool) (module string, version int, release int, scope string) { | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 101 | module, v, scope := parsePrebuiltPath(ctx, p) | 
| Todd Lee | 2ec7e1c | 2023-08-25 18:02:13 +0000 | [diff] [blame] | 102 | if allowIncremental { | 
|  | 103 | parts := strings.Split(v, ".") | 
|  | 104 | if len(parts) != 2 { | 
|  | 105 | ctx.ModuleErrorf("Found unexpected version '%v' for incremental prebuilts - expect MM.m format for incremental API with both major (MM) an minor (m) revision.", v) | 
|  | 106 | return | 
|  | 107 | } | 
|  | 108 | sdk, sdk_err := strconv.Atoi(parts[0]) | 
|  | 109 | qpr, qpr_err := strconv.Atoi(parts[1]) | 
|  | 110 | if sdk_err != nil || qpr_err != nil { | 
|  | 111 | ctx.ModuleErrorf("Unable to read version number for incremental prebuilt api '%v'", v) | 
|  | 112 | return | 
|  | 113 | } | 
|  | 114 | version = sdk | 
|  | 115 | release = qpr | 
|  | 116 | return | 
|  | 117 | } | 
|  | 118 | release = 0 | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 119 | version, err := strconv.Atoi(v) | 
|  | 120 | if err != nil { | 
|  | 121 | ctx.ModuleErrorf("Found finalized API files in non-numeric dir '%v'", v) | 
|  | 122 | return | 
|  | 123 | } | 
|  | 124 | return | 
| Colin Cross | 17dec17 | 2020-05-14 18:05:32 -0700 | [diff] [blame] | 125 | } | 
|  | 126 |  | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 127 | func prebuiltApiModuleName(mctx android.LoadHookContext, module, scope, version string) string { | 
|  | 128 | return fmt.Sprintf("%s_%s_%s_%s", mctx.ModuleName(), scope, version, module) | 
|  | 129 | } | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 130 | func createImport(mctx android.LoadHookContext, module, scope, version, path, sdkVersion string, compileDex bool) { | 
| Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 131 | props := struct { | 
| Colin Cross | 8ff1058 | 2023-12-07 13:10:56 -0800 | [diff] [blame] | 132 | Name        *string | 
|  | 133 | Jars        []string | 
|  | 134 | Sdk_version *string | 
|  | 135 | Installable *bool | 
|  | 136 | Compile_dex *bool | 
| Romain Jobredeaux | 8242b43 | 2023-05-04 10:16:26 -0400 | [diff] [blame] | 137 | }{ | 
|  | 138 | Name:        proptools.StringPtr(prebuiltApiModuleName(mctx, module, scope, version)), | 
|  | 139 | Jars:        []string{path}, | 
|  | 140 | Sdk_version: proptools.StringPtr(sdkVersion), | 
|  | 141 | Installable: proptools.BoolPtr(false), | 
|  | 142 | Compile_dex: proptools.BoolPtr(compileDex), | 
|  | 143 | } | 
| Colin Cross | 84dfc3d | 2019-09-25 11:33:01 -0700 | [diff] [blame] | 144 | mctx.CreateModule(ImportFactory, &props) | 
| Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 145 | } | 
|  | 146 |  | 
| Anton Hansson | c79d412 | 2021-02-17 14:21:33 +0000 | [diff] [blame] | 147 | func createApiModule(mctx android.LoadHookContext, name string, path string) { | 
|  | 148 | genruleProps := struct { | 
| Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 149 | Name *string | 
|  | 150 | Srcs []string | 
| Anton Hansson | c79d412 | 2021-02-17 14:21:33 +0000 | [diff] [blame] | 151 | Out  []string | 
|  | 152 | Cmd  *string | 
| Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 153 | }{} | 
| Anton Hansson | c79d412 | 2021-02-17 14:21:33 +0000 | [diff] [blame] | 154 | genruleProps.Name = proptools.StringPtr(name) | 
|  | 155 | genruleProps.Srcs = []string{path} | 
|  | 156 | genruleProps.Out = []string{name} | 
|  | 157 | genruleProps.Cmd = proptools.StringPtr("cp $(in) $(out)") | 
|  | 158 | mctx.CreateModule(genrule.GenRuleFactory, &genruleProps) | 
| Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 159 | } | 
|  | 160 |  | 
| Jihoon Kang | 748a24d | 2024-03-20 21:29:39 +0000 | [diff] [blame] | 161 | func createCombinedApiFilegroupModule(mctx android.LoadHookContext, name string, srcs []string) { | 
|  | 162 | filegroupProps := struct { | 
|  | 163 | Name *string | 
|  | 164 | Srcs []string | 
|  | 165 | }{} | 
|  | 166 | filegroupProps.Name = proptools.StringPtr(name) | 
|  | 167 |  | 
|  | 168 | var transformedSrcs []string | 
|  | 169 | for _, src := range srcs { | 
|  | 170 | transformedSrcs = append(transformedSrcs, ":"+src) | 
|  | 171 | } | 
|  | 172 | filegroupProps.Srcs = transformedSrcs | 
|  | 173 | mctx.CreateModule(android.FileGroupFactory, &filegroupProps) | 
|  | 174 | } | 
|  | 175 |  | 
| Gurpreet Singh | daa314a | 2023-04-21 16:30:03 +0000 | [diff] [blame] | 176 | func createLatestApiModuleExtensionVersionFile(mctx android.LoadHookContext, name string, version string) { | 
|  | 177 | genruleProps := struct { | 
|  | 178 | Name *string | 
|  | 179 | Srcs []string | 
|  | 180 | Out  []string | 
|  | 181 | Cmd  *string | 
|  | 182 | }{} | 
|  | 183 | genruleProps.Name = proptools.StringPtr(name) | 
|  | 184 | genruleProps.Out = []string{name} | 
|  | 185 | genruleProps.Cmd = proptools.StringPtr("echo " + version + " > $(out)") | 
|  | 186 | mctx.CreateModule(genrule.GenRuleFactory, &genruleProps) | 
|  | 187 | } | 
|  | 188 |  | 
| Anton Hansson | 20ce41d | 2021-01-22 15:05:32 +0000 | [diff] [blame] | 189 | func createEmptyFile(mctx android.LoadHookContext, name string) { | 
|  | 190 | props := struct { | 
|  | 191 | Name *string | 
|  | 192 | Cmd  *string | 
|  | 193 | Out  []string | 
|  | 194 | }{} | 
|  | 195 | props.Name = proptools.StringPtr(name) | 
|  | 196 | props.Out = []string{name} | 
|  | 197 | props.Cmd = proptools.StringPtr("touch $(genDir)/" + name) | 
|  | 198 | mctx.CreateModule(genrule.GenRuleFactory, &props) | 
|  | 199 | } | 
|  | 200 |  | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 201 | // globApiDirs collects all the files in all api_dirs and all scopes that match the given glob, e.g. '*.jar' or 'api/*.txt'. | 
|  | 202 | // <api-dir>/<scope>/<glob> for all api-dir and scope. | 
|  | 203 | func globApiDirs(mctx android.LoadHookContext, p *prebuiltApis, api_dir_glob string) []string { | 
| Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 204 | var files []string | 
| Liz Kammer | 2d2fd85 | 2020-08-12 14:42:30 -0700 | [diff] [blame] | 205 | for _, apiver := range p.properties.Api_dirs { | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 206 | files = append(files, globScopeDir(mctx, apiver, api_dir_glob)...) | 
| Anton Hansson | 20ce41d | 2021-01-22 15:05:32 +0000 | [diff] [blame] | 207 | } | 
|  | 208 | return files | 
|  | 209 | } | 
|  | 210 |  | 
| Anton Hansson | 3a3f169 | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 211 | // globExtensionDirs collects all the files under the extension dir (for all versions and scopes) that match the given glob | 
|  | 212 | // <extension-dir>/<version>/<scope>/<glob> for all version and scope. | 
|  | 213 | func globExtensionDirs(mctx android.LoadHookContext, p *prebuiltApis, extension_dir_glob string) []string { | 
|  | 214 | // <extensions-dir>/<num>/<extension-dir-glob> | 
|  | 215 | return globScopeDir(mctx, *p.properties.Extensions_dir+"/*", extension_dir_glob) | 
|  | 216 | } | 
|  | 217 |  | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 218 | // globScopeDir collects all the files in the given subdir across all scopes that match the given glob, e.g. '*.jar' or 'api/*.txt'. | 
|  | 219 | // <subdir>/<scope>/<glob> for all scope. | 
|  | 220 | func globScopeDir(mctx android.LoadHookContext, subdir string, subdir_glob string) []string { | 
| Anton Hansson | 20ce41d | 2021-01-22 15:05:32 +0000 | [diff] [blame] | 221 | var files []string | 
|  | 222 | dir := mctx.ModuleDir() + "/" + subdir | 
|  | 223 | for _, scope := range []string{"public", "system", "test", "core", "module-lib", "system-server"} { | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 224 | glob := fmt.Sprintf("%s/%s/%s", dir, scope, subdir_glob) | 
| Anton Hansson | 20ce41d | 2021-01-22 15:05:32 +0000 | [diff] [blame] | 225 | vfiles, err := mctx.GlobWithDeps(glob, nil) | 
|  | 226 | if err != nil { | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 227 | mctx.ModuleErrorf("failed to glob %s files under %q: %s", subdir_glob, dir+"/"+scope, err) | 
| Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 228 | } | 
| Anton Hansson | 20ce41d | 2021-01-22 15:05:32 +0000 | [diff] [blame] | 229 | files = append(files, vfiles...) | 
| Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 230 | } | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 231 | for i, f := range files { | 
|  | 232 | files[i] = strings.TrimPrefix(f, mctx.ModuleDir()+"/") | 
|  | 233 | } | 
| Sundong Ahn | 8faab8a | 2019-02-14 11:49:24 +0900 | [diff] [blame] | 234 | return files | 
|  | 235 | } | 
|  | 236 |  | 
| Liz Kammer | 2d2fd85 | 2020-08-12 14:42:30 -0700 | [diff] [blame] | 237 | func prebuiltSdkStubs(mctx android.LoadHookContext, p *prebuiltApis) { | 
| Sundong Ahn | 8faab8a | 2019-02-14 11:49:24 +0900 | [diff] [blame] | 238 | // <apiver>/<scope>/<module>.jar | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 239 | files := globApiDirs(mctx, p, "*.jar") | 
| Liz Kammer | 2d2fd85 | 2020-08-12 14:42:30 -0700 | [diff] [blame] | 240 |  | 
| Liz Kammer | 4e7f260 | 2020-09-02 08:37:49 -0700 | [diff] [blame] | 241 | sdkVersion := proptools.StringDefault(p.properties.Imports_sdk_version, "current") | 
|  | 242 | compileDex := proptools.BoolDefault(p.properties.Imports_compile_dex, false) | 
| Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 243 |  | 
|  | 244 | for _, f := range files { | 
|  | 245 | // create a Import module for each jar file | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 246 | module, version, scope := parsePrebuiltPath(mctx, f) | 
|  | 247 | createImport(mctx, module, scope, version, f, sdkVersion, compileDex) | 
| Paul Duffin | b077bcc | 2021-10-28 13:27:37 +0100 | [diff] [blame] | 248 |  | 
|  | 249 | if module == "core-for-system-modules" { | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 250 | createSystemModules(mctx, version, scope) | 
| Paul Duffin | b077bcc | 2021-10-28 13:27:37 +0100 | [diff] [blame] | 251 | } | 
| Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 252 | } | 
|  | 253 | } | 
|  | 254 |  | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 255 | func createSystemModules(mctx android.LoadHookContext, version, scope string) { | 
| Colin Cross | 17dec17 | 2020-05-14 18:05:32 -0700 | [diff] [blame] | 256 | props := struct { | 
|  | 257 | Name *string | 
|  | 258 | Libs []string | 
|  | 259 | }{} | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 260 | props.Name = proptools.StringPtr(prebuiltApiModuleName(mctx, "system_modules", scope, version)) | 
|  | 261 | props.Libs = append(props.Libs, prebuiltApiModuleName(mctx, "core-for-system-modules", scope, version)) | 
| Colin Cross | 17dec17 | 2020-05-14 18:05:32 -0700 | [diff] [blame] | 262 |  | 
| Paul Duffin | d6c2a65 | 2021-03-11 07:56:22 +0000 | [diff] [blame] | 263 | mctx.CreateModule(systemModulesImportFactory, &props) | 
| Colin Cross | 17dec17 | 2020-05-14 18:05:32 -0700 | [diff] [blame] | 264 | } | 
|  | 265 |  | 
| Paul Duffin | 958806b | 2022-05-16 13:10:47 +0000 | [diff] [blame] | 266 | func PrebuiltApiModuleName(module, scope, version string) string { | 
|  | 267 | return module + ".api." + scope + "." + version | 
|  | 268 | } | 
|  | 269 |  | 
| Jihoon Kang | 748a24d | 2024-03-20 21:29:39 +0000 | [diff] [blame] | 270 | func PrebuiltApiCombinedModuleName(module, scope, version string) string { | 
|  | 271 | return module + ".api.combined." + scope + "." + version | 
|  | 272 | } | 
|  | 273 |  | 
| Liz Kammer | 2d2fd85 | 2020-08-12 14:42:30 -0700 | [diff] [blame] | 274 | func prebuiltApiFiles(mctx android.LoadHookContext, p *prebuiltApis) { | 
| Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 275 | // <apiver>/<scope>/api/<module>.txt | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 276 | apiLevelFiles := globApiDirs(mctx, p, "api/*.txt") | 
|  | 277 | if len(apiLevelFiles) == 0 { | 
|  | 278 | mctx.ModuleErrorf("no api file found under %q", mctx.ModuleDir()) | 
| Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 279 | } | 
| Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 280 |  | 
| Anton Hansson | c79d412 | 2021-02-17 14:21:33 +0000 | [diff] [blame] | 281 | // Create modules for all (<module>, <scope, <version>) triplets, | 
| Todd Lee | 2ec7e1c | 2023-08-25 18:02:13 +0000 | [diff] [blame] | 282 | allowIncremental := proptools.BoolDefault(p.properties.Allow_incremental_platform_api, false) | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 283 | for _, f := range apiLevelFiles { | 
| Todd Lee | 2ec7e1c | 2023-08-25 18:02:13 +0000 | [diff] [blame] | 284 | module, version, release, scope := parseFinalizedPrebuiltPath(mctx, f, allowIncremental) | 
|  | 285 | if allowIncremental { | 
|  | 286 | incrementalVersion := strconv.Itoa(version) + "." + strconv.Itoa(release) | 
|  | 287 | createApiModule(mctx, PrebuiltApiModuleName(module, scope, incrementalVersion), f) | 
|  | 288 | } else { | 
|  | 289 | createApiModule(mctx, PrebuiltApiModuleName(module, scope, strconv.Itoa(version)), f) | 
|  | 290 | } | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 291 | } | 
| Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 292 |  | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 293 | // Figure out the latest version of each module/scope | 
|  | 294 | type latestApiInfo struct { | 
|  | 295 | module, scope, path string | 
| Todd Lee | 2ec7e1c | 2023-08-25 18:02:13 +0000 | [diff] [blame] | 296 | version, release    int | 
| Gurpreet Singh | daa314a | 2023-04-21 16:30:03 +0000 | [diff] [blame] | 297 | isExtensionApiFile  bool | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 298 | } | 
|  | 299 |  | 
| Gurpreet Singh | daa314a | 2023-04-21 16:30:03 +0000 | [diff] [blame] | 300 | getLatest := func(files []string, isExtensionApiFile bool) map[string]latestApiInfo { | 
| Anton Hansson | 3a3f169 | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 301 | m := make(map[string]latestApiInfo) | 
|  | 302 | for _, f := range files { | 
| Todd Lee | 2ec7e1c | 2023-08-25 18:02:13 +0000 | [diff] [blame] | 303 | module, version, release, scope := parseFinalizedPrebuiltPath(mctx, f, allowIncremental) | 
| Anton Hansson | 3a3f169 | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 304 | if strings.HasSuffix(module, "incompatibilities") { | 
|  | 305 | continue | 
|  | 306 | } | 
|  | 307 | key := module + "." + scope | 
|  | 308 | info, exists := m[key] | 
| Todd Lee | 2ec7e1c | 2023-08-25 18:02:13 +0000 | [diff] [blame] | 309 | if !exists || version > info.version || (version == info.version && release > info.release) { | 
|  | 310 | m[key] = latestApiInfo{module, scope, f, version, release, isExtensionApiFile} | 
| Anton Hansson | 3a3f169 | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 311 | } | 
| Anton Hansson | 370fd0b | 2021-01-22 15:05:04 +0000 | [diff] [blame] | 312 | } | 
| Anton Hansson | 3a3f169 | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 313 | return m | 
|  | 314 | } | 
| Anton Hansson | 370fd0b | 2021-01-22 15:05:04 +0000 | [diff] [blame] | 315 |  | 
| Gurpreet Singh | daa314a | 2023-04-21 16:30:03 +0000 | [diff] [blame] | 316 | latest := getLatest(apiLevelFiles, false) | 
| Anton Hansson | 3a3f169 | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 317 | if p.properties.Extensions_dir != nil { | 
|  | 318 | extensionApiFiles := globExtensionDirs(mctx, p, "api/*.txt") | 
| Gurpreet Singh | daa314a | 2023-04-21 16:30:03 +0000 | [diff] [blame] | 319 | for k, v := range getLatest(extensionApiFiles, true) { | 
| Anton Hansson | 7212dbe | 2022-09-20 17:08:49 +0000 | [diff] [blame] | 320 | if _, exists := latest[k]; !exists { | 
|  | 321 | mctx.ModuleErrorf("Module %v finalized for extension %d but never during an API level; likely error", v.module, v.version) | 
| Anton Hansson | 3a3f169 | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 322 | } | 
| Anton Hansson | 7212dbe | 2022-09-20 17:08:49 +0000 | [diff] [blame] | 323 | // The extension version is always at least as new as the last sdk int version (potentially identical) | 
|  | 324 | latest[k] = v | 
| Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 325 | } | 
|  | 326 | } | 
| Anton Hansson | 20ce41d | 2021-01-22 15:05:32 +0000 | [diff] [blame] | 327 |  | 
| Anton Hansson | 370fd0b | 2021-01-22 15:05:04 +0000 | [diff] [blame] | 328 | // Sort the keys in order to make build.ninja stable | 
| Jihoon Kang | 748a24d | 2024-03-20 21:29:39 +0000 | [diff] [blame] | 329 | sortedLatestKeys := android.SortedKeys(latest) | 
|  | 330 |  | 
|  | 331 | for _, k := range sortedLatestKeys { | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 332 | info := latest[k] | 
| Paul Duffin | 958806b | 2022-05-16 13:10:47 +0000 | [diff] [blame] | 333 | name := PrebuiltApiModuleName(info.module, info.scope, "latest") | 
| Gurpreet Singh | daa314a | 2023-04-21 16:30:03 +0000 | [diff] [blame] | 334 | latestExtensionVersionModuleName := PrebuiltApiModuleName(info.module, info.scope, "latest.extension_version") | 
|  | 335 | if info.isExtensionApiFile { | 
|  | 336 | createLatestApiModuleExtensionVersionFile(mctx, latestExtensionVersionModuleName, strconv.Itoa(info.version)) | 
|  | 337 | } else { | 
|  | 338 | createLatestApiModuleExtensionVersionFile(mctx, latestExtensionVersionModuleName, "-1") | 
|  | 339 | } | 
| Anton Hansson | c79d412 | 2021-02-17 14:21:33 +0000 | [diff] [blame] | 340 | createApiModule(mctx, name, info.path) | 
| Anton Hansson | 20ce41d | 2021-01-22 15:05:32 +0000 | [diff] [blame] | 341 | } | 
|  | 342 |  | 
|  | 343 | // Create incompatibilities tracking files for all modules, if we have a "next" api. | 
| Jaewoong Jung | 1a97ee0 | 2021-03-09 13:25:02 -0800 | [diff] [blame] | 344 | incompatibilities := make(map[string]bool) | 
| Anton Hansson | 20ce41d | 2021-01-22 15:05:32 +0000 | [diff] [blame] | 345 | if nextApiDir := String(p.properties.Next_api_dir); nextApiDir != "" { | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 346 | files := globScopeDir(mctx, nextApiDir, "api/*incompatibilities.txt") | 
| Anton Hansson | 20ce41d | 2021-01-22 15:05:32 +0000 | [diff] [blame] | 347 | for _, f := range files { | 
| Anton Hansson | 377318b | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 348 | filename, _, scope := parsePrebuiltPath(mctx, f) | 
| Anton Hansson | fa5e6b5 | 2021-04-13 19:09:48 +0100 | [diff] [blame] | 349 | referencedModule := strings.TrimSuffix(filename, "-incompatibilities") | 
| Anton Hansson | 20ce41d | 2021-01-22 15:05:32 +0000 | [diff] [blame] | 350 |  | 
| Paul Duffin | 958806b | 2022-05-16 13:10:47 +0000 | [diff] [blame] | 351 | createApiModule(mctx, PrebuiltApiModuleName(referencedModule+"-incompatibilities", scope, "latest"), f) | 
| Anton Hansson | 20ce41d | 2021-01-22 15:05:32 +0000 | [diff] [blame] | 352 |  | 
|  | 353 | incompatibilities[referencedModule+"."+scope] = true | 
|  | 354 | } | 
| Jaewoong Jung | 1a97ee0 | 2021-03-09 13:25:02 -0800 | [diff] [blame] | 355 | } | 
|  | 356 | // Create empty incompatibilities files for remaining modules | 
| Jihoon Kang | 748a24d | 2024-03-20 21:29:39 +0000 | [diff] [blame] | 357 | // If the incompatibility module has been created, create a corresponding combined module | 
|  | 358 | for _, k := range sortedLatestKeys { | 
| Jaewoong Jung | 1a97ee0 | 2021-03-09 13:25:02 -0800 | [diff] [blame] | 359 | if _, ok := incompatibilities[k]; !ok { | 
| Paul Duffin | 958806b | 2022-05-16 13:10:47 +0000 | [diff] [blame] | 360 | createEmptyFile(mctx, PrebuiltApiModuleName(latest[k].module+"-incompatibilities", latest[k].scope, "latest")) | 
| Anton Hansson | 20ce41d | 2021-01-22 15:05:32 +0000 | [diff] [blame] | 361 | } | 
| Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 362 | } | 
| Jihoon Kang | 748a24d | 2024-03-20 21:29:39 +0000 | [diff] [blame] | 363 |  | 
|  | 364 | // Create combined latest api and removed api files modules. | 
|  | 365 | // The combined modules list all api files of the api scope and its subset api scopes. | 
|  | 366 | for _, k := range sortedLatestKeys { | 
|  | 367 | info := latest[k] | 
|  | 368 | name := PrebuiltApiCombinedModuleName(info.module, info.scope, "latest") | 
|  | 369 |  | 
| Jihoon Kang | 5623e54 | 2024-01-31 23:27:26 +0000 | [diff] [blame] | 370 | // Iterate until the currentApiScope does not extend any other api scopes | 
|  | 371 | // i.e. is not a superset of any other api scopes | 
|  | 372 | // the relationship between the api scopes is defined in java/sdk_library.go | 
| Jihoon Kang | 748a24d | 2024-03-20 21:29:39 +0000 | [diff] [blame] | 373 | var srcs []string | 
|  | 374 | currentApiScope := scopeByName[info.scope] | 
| Jihoon Kang | 5623e54 | 2024-01-31 23:27:26 +0000 | [diff] [blame] | 375 | for currentApiScope != nil { | 
|  | 376 | if _, ok := latest[fmt.Sprintf("%s.%s", info.module, currentApiScope.name)]; ok { | 
|  | 377 | srcs = append(srcs, PrebuiltApiModuleName(info.module, currentApiScope.name, "latest")) | 
|  | 378 | } | 
|  | 379 | currentApiScope = currentApiScope.extends | 
|  | 380 | } | 
| Jihoon Kang | 748a24d | 2024-03-20 21:29:39 +0000 | [diff] [blame] | 381 |  | 
| Jihoon Kang | 5623e54 | 2024-01-31 23:27:26 +0000 | [diff] [blame] | 382 | // srcs is currently listed in the order from the widest api scope to the narrowest api scopes | 
|  | 383 | // e.g. module lib -> system -> public | 
|  | 384 | // In order to pass the files in metalava from the narrowest api scope to the widest api scope, | 
|  | 385 | // the list has to be reversed. | 
|  | 386 | android.ReverseSliceInPlace(srcs) | 
| Jihoon Kang | 748a24d | 2024-03-20 21:29:39 +0000 | [diff] [blame] | 387 | createCombinedApiFilegroupModule(mctx, name, srcs) | 
|  | 388 | } | 
| Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 389 | } | 
|  | 390 |  | 
| Paul Duffin | d4c0356 | 2020-04-09 17:15:44 +0100 | [diff] [blame] | 391 | func createPrebuiltApiModules(mctx android.LoadHookContext) { | 
| Liz Kammer | 2d2fd85 | 2020-08-12 14:42:30 -0700 | [diff] [blame] | 392 | if p, ok := mctx.Module().(*prebuiltApis); ok { | 
|  | 393 | prebuiltApiFiles(mctx, p) | 
|  | 394 | prebuiltSdkStubs(mctx, p) | 
| Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 395 | } | 
|  | 396 | } | 
|  | 397 |  | 
| Anton Hansson | c79d412 | 2021-02-17 14:21:33 +0000 | [diff] [blame] | 398 | // prebuilt_apis is a meta-module that generates modules for all API txt files | 
|  | 399 | // found under the directory where the Android.bp is located. | 
| Jaewoong Jung | 5fb5b2a | 2019-03-21 10:48:25 -0700 | [diff] [blame] | 400 | // Specifically, an API file located at ./<ver>/<scope>/api/<module>.txt | 
| Anton Hansson | c79d412 | 2021-02-17 14:21:33 +0000 | [diff] [blame] | 401 | // generates a module named <module>-api.<scope>.<ver>. | 
| Jaewoong Jung | 5fb5b2a | 2019-03-21 10:48:25 -0700 | [diff] [blame] | 402 | // | 
|  | 403 | // It also creates <module>-api.<scope>.latest for the latest <ver>. | 
| Paul Duffin | d4c0356 | 2020-04-09 17:15:44 +0100 | [diff] [blame] | 404 | // | 
|  | 405 | // Similarly, it generates a java_import for all API .jar files found under the | 
|  | 406 | // directory where the Android.bp is located. Specifically, an API file located | 
|  | 407 | // at ./<ver>/<scope>/api/<module>.jar generates a java_import module named | 
| Colin Cross | 17dec17 | 2020-05-14 18:05:32 -0700 | [diff] [blame] | 408 | // <prebuilt-api-module>_<scope>_<ver>_<module>, and for SDK versions >= 30 | 
|  | 409 | // a java_system_modules module named | 
|  | 410 | // <prebuilt-api-module>_public_<ver>_system_modules | 
| Inseob Kim | c0907f1 | 2019-02-08 21:00:45 +0900 | [diff] [blame] | 411 | func PrebuiltApisFactory() android.Module { | 
| Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 412 | module := &prebuiltApis{} | 
| Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 413 | module.AddProperties(&module.properties) | 
| Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 414 | android.InitAndroidModule(module) | 
| Paul Duffin | d4c0356 | 2020-04-09 17:15:44 +0100 | [diff] [blame] | 415 | android.AddLoadHook(module, createPrebuiltApiModules) | 
| Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 416 | return module | 
|  | 417 | } |