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