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