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 ( |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 18 | "sort" |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 19 | "strings" |
| 20 | |
| 21 | "github.com/google/blueprint/proptools" |
Colin Cross | 17dec17 | 2020-05-14 18:05:32 -0700 | [diff] [blame] | 22 | |
| 23 | "android/soong/android" |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 24 | ) |
| 25 | |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 26 | func init() { |
Paul Duffin | a48f758 | 2019-12-19 11:25:19 +0000 | [diff] [blame] | 27 | RegisterPrebuiltApisBuildComponents(android.InitRegistrationContext) |
| 28 | } |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 29 | |
Paul Duffin | a48f758 | 2019-12-19 11:25:19 +0000 | [diff] [blame] | 30 | func RegisterPrebuiltApisBuildComponents(ctx android.RegistrationContext) { |
| 31 | ctx.RegisterModuleType("prebuilt_apis", PrebuiltApisFactory) |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 32 | } |
| 33 | |
Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 34 | type prebuiltApisProperties struct { |
| 35 | // list of api version directories |
| 36 | Api_dirs []string |
Liz Kammer | 2d2fd85 | 2020-08-12 14:42:30 -0700 | [diff] [blame^] | 37 | |
| 38 | // The sdk_version of java_import modules generated based on jar files. |
| 39 | // Defaults to "current" |
| 40 | Imports_sdk_version *string |
Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 41 | } |
| 42 | |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 43 | type prebuiltApis struct { |
| 44 | android.ModuleBase |
Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 45 | properties prebuiltApisProperties |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 46 | } |
| 47 | |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 48 | func (module *prebuiltApis) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 49 | // no need to implement |
| 50 | } |
| 51 | |
Paul Duffin | d4c0356 | 2020-04-09 17:15:44 +0100 | [diff] [blame] | 52 | func parseJarPath(path string) (module string, apiver string, scope string) { |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 53 | elements := strings.Split(path, "/") |
| 54 | |
| 55 | apiver = elements[0] |
| 56 | scope = elements[1] |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 57 | |
| 58 | module = strings.TrimSuffix(elements[2], ".jar") |
| 59 | return |
| 60 | } |
| 61 | |
Paul Duffin | d4c0356 | 2020-04-09 17:15:44 +0100 | [diff] [blame] | 62 | func parseApiFilePath(ctx android.LoadHookContext, path string) (module string, apiver string, scope string) { |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 63 | elements := strings.Split(path, "/") |
Sundong Ahn | 054b19a | 2018-10-19 13:46:09 +0900 | [diff] [blame] | 64 | apiver = elements[0] |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 65 | |
| 66 | scope = elements[1] |
Anton Hansson | 8d23969 | 2020-05-01 18:37:15 +0100 | [diff] [blame] | 67 | if scope != "public" && scope != "system" && scope != "test" && scope != "module-lib" && scope != "system-server" { |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 68 | ctx.ModuleErrorf("invalid scope %q found in path: %q", scope, path) |
| 69 | return |
| 70 | } |
| 71 | |
| 72 | // elements[2] is string literal "api". skipping. |
| 73 | module = strings.TrimSuffix(elements[3], ".txt") |
| 74 | return |
| 75 | } |
| 76 | |
Colin Cross | 17dec17 | 2020-05-14 18:05:32 -0700 | [diff] [blame] | 77 | func prebuiltApiModuleName(mctx android.LoadHookContext, module string, scope string, apiver string) string { |
| 78 | return mctx.ModuleName() + "_" + scope + "_" + apiver + "_" + module |
| 79 | } |
| 80 | |
Liz Kammer | 2d2fd85 | 2020-08-12 14:42:30 -0700 | [diff] [blame^] | 81 | func createImport(mctx android.LoadHookContext, module, scope, apiver, path, sdk_version string) { |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 82 | props := struct { |
| 83 | Name *string |
| 84 | Jars []string |
| 85 | Sdk_version *string |
| 86 | Installable *bool |
| 87 | }{} |
Colin Cross | 17dec17 | 2020-05-14 18:05:32 -0700 | [diff] [blame] | 88 | props.Name = proptools.StringPtr(prebuiltApiModuleName(mctx, module, scope, apiver)) |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 89 | props.Jars = append(props.Jars, path) |
Liz Kammer | 2d2fd85 | 2020-08-12 14:42:30 -0700 | [diff] [blame^] | 90 | props.Sdk_version = proptools.StringPtr(sdk_version) |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 91 | props.Installable = proptools.BoolPtr(false) |
| 92 | |
Colin Cross | 84dfc3d | 2019-09-25 11:33:01 -0700 | [diff] [blame] | 93 | mctx.CreateModule(ImportFactory, &props) |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 94 | } |
| 95 | |
Paul Duffin | d4c0356 | 2020-04-09 17:15:44 +0100 | [diff] [blame] | 96 | func createFilegroup(mctx android.LoadHookContext, module string, scope string, apiver string, path string) { |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 97 | fgName := module + ".api." + scope + "." + apiver |
| 98 | filegroupProps := struct { |
| 99 | Name *string |
| 100 | Srcs []string |
| 101 | }{} |
| 102 | filegroupProps.Name = proptools.StringPtr(fgName) |
| 103 | filegroupProps.Srcs = []string{path} |
Colin Cross | 84dfc3d | 2019-09-25 11:33:01 -0700 | [diff] [blame] | 104 | mctx.CreateModule(android.FileGroupFactory, &filegroupProps) |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 105 | } |
| 106 | |
Liz Kammer | 2d2fd85 | 2020-08-12 14:42:30 -0700 | [diff] [blame^] | 107 | func getPrebuiltFiles(mctx android.LoadHookContext, p *prebuiltApis, name string) []string { |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 108 | mydir := mctx.ModuleDir() + "/" |
Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 109 | var files []string |
Liz Kammer | 2d2fd85 | 2020-08-12 14:42:30 -0700 | [diff] [blame^] | 110 | for _, apiver := range p.properties.Api_dirs { |
Anton Hansson | 8d23969 | 2020-05-01 18:37:15 +0100 | [diff] [blame] | 111 | for _, scope := range []string{"public", "system", "test", "core", "module-lib", "system-server"} { |
Sundong Ahn | 8faab8a | 2019-02-14 11:49:24 +0900 | [diff] [blame] | 112 | vfiles, err := mctx.GlobWithDeps(mydir+apiver+"/"+scope+"/"+name, nil) |
Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 113 | if err != nil { |
Sundong Ahn | 8faab8a | 2019-02-14 11:49:24 +0900 | [diff] [blame] | 114 | mctx.ModuleErrorf("failed to glob %s files under %q: %s", name, mydir+apiver+"/"+scope, err) |
Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 115 | } |
| 116 | files = append(files, vfiles...) |
| 117 | } |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 118 | } |
Sundong Ahn | 8faab8a | 2019-02-14 11:49:24 +0900 | [diff] [blame] | 119 | return files |
| 120 | } |
| 121 | |
Liz Kammer | 2d2fd85 | 2020-08-12 14:42:30 -0700 | [diff] [blame^] | 122 | func prebuiltSdkStubs(mctx android.LoadHookContext, p *prebuiltApis) { |
Sundong Ahn | 8faab8a | 2019-02-14 11:49:24 +0900 | [diff] [blame] | 123 | mydir := mctx.ModuleDir() + "/" |
| 124 | // <apiver>/<scope>/<module>.jar |
Liz Kammer | 2d2fd85 | 2020-08-12 14:42:30 -0700 | [diff] [blame^] | 125 | files := getPrebuiltFiles(mctx, p, "*.jar") |
| 126 | |
| 127 | sdk_version := proptools.StringDefault(p.properties.Imports_sdk_version, "current") |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 128 | |
| 129 | for _, f := range files { |
| 130 | // create a Import module for each jar file |
| 131 | localPath := strings.TrimPrefix(f, mydir) |
Paul Duffin | d4c0356 | 2020-04-09 17:15:44 +0100 | [diff] [blame] | 132 | module, apiver, scope := parseJarPath(localPath) |
Liz Kammer | 2d2fd85 | 2020-08-12 14:42:30 -0700 | [diff] [blame^] | 133 | createImport(mctx, module, scope, apiver, localPath, sdk_version) |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
Colin Cross | 17dec17 | 2020-05-14 18:05:32 -0700 | [diff] [blame] | 137 | func createSystemModules(mctx android.LoadHookContext, apiver string) { |
| 138 | props := struct { |
| 139 | Name *string |
| 140 | Libs []string |
| 141 | }{} |
| 142 | props.Name = proptools.StringPtr(prebuiltApiModuleName(mctx, "system_modules", "public", apiver)) |
| 143 | props.Libs = append(props.Libs, prebuiltApiModuleName(mctx, "core-for-system-modules", "public", apiver)) |
| 144 | |
| 145 | mctx.CreateModule(SystemModulesFactory, &props) |
| 146 | } |
| 147 | |
Liz Kammer | 2d2fd85 | 2020-08-12 14:42:30 -0700 | [diff] [blame^] | 148 | func prebuiltSdkSystemModules(mctx android.LoadHookContext, p *prebuiltApis) { |
| 149 | for _, apiver := range p.properties.Api_dirs { |
Colin Cross | 17dec17 | 2020-05-14 18:05:32 -0700 | [diff] [blame] | 150 | jar := android.ExistentPathForSource(mctx, |
| 151 | mctx.ModuleDir(), apiver, "public", "core-for-system-modules.jar") |
| 152 | if jar.Valid() { |
| 153 | createSystemModules(mctx, apiver) |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
Liz Kammer | 2d2fd85 | 2020-08-12 14:42:30 -0700 | [diff] [blame^] | 158 | func prebuiltApiFiles(mctx android.LoadHookContext, p *prebuiltApis) { |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 159 | mydir := mctx.ModuleDir() + "/" |
| 160 | // <apiver>/<scope>/api/<module>.txt |
Liz Kammer | 2d2fd85 | 2020-08-12 14:42:30 -0700 | [diff] [blame^] | 161 | files := getPrebuiltFiles(mctx, p, "api/*.txt") |
Sundong Ahn | 8faab8a | 2019-02-14 11:49:24 +0900 | [diff] [blame] | 162 | |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 163 | if len(files) == 0 { |
| 164 | mctx.ModuleErrorf("no api file found under %q", mydir) |
| 165 | } |
| 166 | |
| 167 | // construct a map to find out the latest api file path |
| 168 | // for each (<module>, <scope>) pair. |
| 169 | type latestApiInfo struct { |
| 170 | module string |
| 171 | scope string |
Sundong Ahn | 054b19a | 2018-10-19 13:46:09 +0900 | [diff] [blame] | 172 | apiver string |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 173 | path string |
| 174 | } |
| 175 | m := make(map[string]latestApiInfo) |
| 176 | |
| 177 | for _, f := range files { |
| 178 | // create a filegroup for each api txt file |
| 179 | localPath := strings.TrimPrefix(f, mydir) |
| 180 | module, apiver, scope := parseApiFilePath(mctx, localPath) |
Sundong Ahn | 054b19a | 2018-10-19 13:46:09 +0900 | [diff] [blame] | 181 | createFilegroup(mctx, module, scope, apiver, localPath) |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 182 | |
| 183 | // find the latest apiver |
| 184 | key := module + "." + scope |
| 185 | info, ok := m[key] |
| 186 | if !ok { |
| 187 | m[key] = latestApiInfo{module, scope, apiver, localPath} |
Sundong Ahn | 054b19a | 2018-10-19 13:46:09 +0900 | [diff] [blame] | 188 | } else if len(apiver) > len(info.apiver) || (len(apiver) == len(info.apiver) && |
| 189 | strings.Compare(apiver, info.apiver) > 0) { |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 190 | info.apiver = apiver |
| 191 | info.path = localPath |
Sundong Ahn | 8faab8a | 2019-02-14 11:49:24 +0900 | [diff] [blame] | 192 | m[key] = info |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | // create filegroups for the latest version of (<module>, <scope>) pairs |
| 196 | // sort the keys in order to make build.ninja stable |
| 197 | keys := make([]string, 0, len(m)) |
| 198 | for k := range m { |
| 199 | keys = append(keys, k) |
| 200 | } |
| 201 | sort.Strings(keys) |
| 202 | for _, k := range keys { |
| 203 | info := m[k] |
| 204 | createFilegroup(mctx, info.module, info.scope, "latest", info.path) |
| 205 | } |
| 206 | } |
| 207 | |
Paul Duffin | d4c0356 | 2020-04-09 17:15:44 +0100 | [diff] [blame] | 208 | func createPrebuiltApiModules(mctx android.LoadHookContext) { |
Liz Kammer | 2d2fd85 | 2020-08-12 14:42:30 -0700 | [diff] [blame^] | 209 | if p, ok := mctx.Module().(*prebuiltApis); ok { |
| 210 | prebuiltApiFiles(mctx, p) |
| 211 | prebuiltSdkStubs(mctx, p) |
| 212 | prebuiltSdkSystemModules(mctx, p) |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Jaewoong Jung | 5fb5b2a | 2019-03-21 10:48:25 -0700 | [diff] [blame] | 216 | // prebuilt_apis is a meta-module that generates filegroup modules for all |
| 217 | // API txt files found under the directory where the Android.bp is located. |
| 218 | // Specifically, an API file located at ./<ver>/<scope>/api/<module>.txt |
| 219 | // generates a filegroup module named <module>-api.<scope>.<ver>. |
| 220 | // |
| 221 | // It also creates <module>-api.<scope>.latest for the latest <ver>. |
Paul Duffin | d4c0356 | 2020-04-09 17:15:44 +0100 | [diff] [blame] | 222 | // |
| 223 | // Similarly, it generates a java_import for all API .jar files found under the |
| 224 | // directory where the Android.bp is located. Specifically, an API file located |
| 225 | // at ./<ver>/<scope>/api/<module>.jar generates a java_import module named |
Colin Cross | 17dec17 | 2020-05-14 18:05:32 -0700 | [diff] [blame] | 226 | // <prebuilt-api-module>_<scope>_<ver>_<module>, and for SDK versions >= 30 |
| 227 | // a java_system_modules module named |
| 228 | // <prebuilt-api-module>_public_<ver>_system_modules |
Inseob Kim | c0907f1 | 2019-02-08 21:00:45 +0900 | [diff] [blame] | 229 | func PrebuiltApisFactory() android.Module { |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 230 | module := &prebuiltApis{} |
Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 231 | module.AddProperties(&module.properties) |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 232 | android.InitAndroidModule(module) |
Paul Duffin | d4c0356 | 2020-04-09 17:15:44 +0100 | [diff] [blame] | 233 | android.AddLoadHook(module, createPrebuiltApiModules) |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 234 | return module |
| 235 | } |