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 ( |
| 18 | "android/soong/android" |
| 19 | "sort" |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 20 | "strings" |
| 21 | |
| 22 | "github.com/google/blueprint/proptools" |
| 23 | ) |
| 24 | |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 25 | func init() { |
Inseob Kim | c0907f1 | 2019-02-08 21:00:45 +0900 | [diff] [blame] | 26 | android.RegisterModuleType("prebuilt_apis", PrebuiltApisFactory) |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 27 | |
| 28 | android.PreArchMutators(func(ctx android.RegisterMutatorsContext) { |
Inseob Kim | c0907f1 | 2019-02-08 21:00:45 +0900 | [diff] [blame] | 29 | ctx.TopDown("prebuilt_apis", PrebuiltApisMutator).Parallel() |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 30 | }) |
| 31 | } |
| 32 | |
Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 33 | type prebuiltApisProperties struct { |
| 34 | // list of api version directories |
| 35 | Api_dirs []string |
| 36 | } |
| 37 | |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 38 | type prebuiltApis struct { |
| 39 | android.ModuleBase |
Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 40 | properties prebuiltApisProperties |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 41 | } |
| 42 | |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 43 | func (module *prebuiltApis) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 44 | // no need to implement |
| 45 | } |
| 46 | |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 47 | func parseJarPath(ctx android.BaseModuleContext, path string) (module string, apiver string, scope string) { |
| 48 | elements := strings.Split(path, "/") |
| 49 | |
| 50 | apiver = elements[0] |
| 51 | scope = elements[1] |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 52 | |
| 53 | module = strings.TrimSuffix(elements[2], ".jar") |
| 54 | return |
| 55 | } |
| 56 | |
Sundong Ahn | 054b19a | 2018-10-19 13:46:09 +0900 | [diff] [blame] | 57 | func parseApiFilePath(ctx android.BaseModuleContext, path string) (module string, apiver string, scope string) { |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 58 | elements := strings.Split(path, "/") |
Sundong Ahn | 054b19a | 2018-10-19 13:46:09 +0900 | [diff] [blame] | 59 | apiver = elements[0] |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 60 | |
| 61 | scope = elements[1] |
| 62 | if scope != "public" && scope != "system" && scope != "test" { |
| 63 | ctx.ModuleErrorf("invalid scope %q found in path: %q", scope, path) |
| 64 | return |
| 65 | } |
| 66 | |
| 67 | // elements[2] is string literal "api". skipping. |
| 68 | module = strings.TrimSuffix(elements[3], ".txt") |
| 69 | return |
| 70 | } |
| 71 | |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 72 | func createImport(mctx android.TopDownMutatorContext, module string, scope string, apiver string, path string) { |
| 73 | props := struct { |
| 74 | Name *string |
| 75 | Jars []string |
| 76 | Sdk_version *string |
| 77 | Installable *bool |
| 78 | }{} |
Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 79 | props.Name = proptools.StringPtr(mctx.ModuleName() + "_" + scope + "_" + apiver + "_" + module) |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 80 | props.Jars = append(props.Jars, path) |
| 81 | // TODO(hansson): change to scope after migration is done. |
| 82 | props.Sdk_version = proptools.StringPtr("current") |
| 83 | props.Installable = proptools.BoolPtr(false) |
| 84 | |
| 85 | mctx.CreateModule(android.ModuleFactoryAdaptor(ImportFactory), &props) |
| 86 | } |
| 87 | |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 88 | func createFilegroup(mctx android.TopDownMutatorContext, module string, scope string, apiver string, path string) { |
| 89 | fgName := module + ".api." + scope + "." + apiver |
| 90 | filegroupProps := struct { |
| 91 | Name *string |
| 92 | Srcs []string |
| 93 | }{} |
| 94 | filegroupProps.Name = proptools.StringPtr(fgName) |
| 95 | filegroupProps.Srcs = []string{path} |
| 96 | mctx.CreateModule(android.ModuleFactoryAdaptor(android.FileGroupFactory), &filegroupProps) |
| 97 | } |
| 98 | |
Sundong Ahn | 8faab8a | 2019-02-14 11:49:24 +0900 | [diff] [blame] | 99 | func getPrebuiltFiles(mctx android.TopDownMutatorContext, name string) []string { |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 100 | mydir := mctx.ModuleDir() + "/" |
Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 101 | var files []string |
| 102 | for _, apiver := range mctx.Module().(*prebuiltApis).properties.Api_dirs { |
| 103 | for _, scope := range []string{"public", "system", "test", "core"} { |
Sundong Ahn | 8faab8a | 2019-02-14 11:49:24 +0900 | [diff] [blame] | 104 | vfiles, err := mctx.GlobWithDeps(mydir+apiver+"/"+scope+"/"+name, nil) |
Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 105 | if err != nil { |
Sundong Ahn | 8faab8a | 2019-02-14 11:49:24 +0900 | [diff] [blame] | 106 | 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] | 107 | } |
| 108 | files = append(files, vfiles...) |
| 109 | } |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 110 | } |
Sundong Ahn | 8faab8a | 2019-02-14 11:49:24 +0900 | [diff] [blame] | 111 | return files |
| 112 | } |
| 113 | |
| 114 | func prebuiltSdkStubs(mctx android.TopDownMutatorContext) { |
| 115 | mydir := mctx.ModuleDir() + "/" |
| 116 | // <apiver>/<scope>/<module>.jar |
| 117 | files := getPrebuiltFiles(mctx, "*.jar") |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 118 | |
| 119 | for _, f := range files { |
| 120 | // create a Import module for each jar file |
| 121 | localPath := strings.TrimPrefix(f, mydir) |
| 122 | module, apiver, scope := parseJarPath(mctx, localPath) |
Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 123 | createImport(mctx, module, scope, apiver, localPath) |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | func prebuiltApiFiles(mctx android.TopDownMutatorContext) { |
| 128 | mydir := mctx.ModuleDir() + "/" |
| 129 | // <apiver>/<scope>/api/<module>.txt |
Sundong Ahn | 8faab8a | 2019-02-14 11:49:24 +0900 | [diff] [blame] | 130 | files := getPrebuiltFiles(mctx, "api/*.txt") |
| 131 | |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 132 | if len(files) == 0 { |
| 133 | mctx.ModuleErrorf("no api file found under %q", mydir) |
| 134 | } |
| 135 | |
| 136 | // construct a map to find out the latest api file path |
| 137 | // for each (<module>, <scope>) pair. |
| 138 | type latestApiInfo struct { |
| 139 | module string |
| 140 | scope string |
Sundong Ahn | 054b19a | 2018-10-19 13:46:09 +0900 | [diff] [blame] | 141 | apiver string |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 142 | path string |
| 143 | } |
| 144 | m := make(map[string]latestApiInfo) |
| 145 | |
| 146 | for _, f := range files { |
| 147 | // create a filegroup for each api txt file |
| 148 | localPath := strings.TrimPrefix(f, mydir) |
| 149 | module, apiver, scope := parseApiFilePath(mctx, localPath) |
Sundong Ahn | 054b19a | 2018-10-19 13:46:09 +0900 | [diff] [blame] | 150 | createFilegroup(mctx, module, scope, apiver, localPath) |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 151 | |
| 152 | // find the latest apiver |
| 153 | key := module + "." + scope |
| 154 | info, ok := m[key] |
| 155 | if !ok { |
| 156 | m[key] = latestApiInfo{module, scope, apiver, localPath} |
Sundong Ahn | 054b19a | 2018-10-19 13:46:09 +0900 | [diff] [blame] | 157 | } else if len(apiver) > len(info.apiver) || (len(apiver) == len(info.apiver) && |
| 158 | strings.Compare(apiver, info.apiver) > 0) { |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 159 | info.apiver = apiver |
| 160 | info.path = localPath |
Sundong Ahn | 8faab8a | 2019-02-14 11:49:24 +0900 | [diff] [blame] | 161 | m[key] = info |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | // create filegroups for the latest version of (<module>, <scope>) pairs |
| 165 | // sort the keys in order to make build.ninja stable |
| 166 | keys := make([]string, 0, len(m)) |
| 167 | for k := range m { |
| 168 | keys = append(keys, k) |
| 169 | } |
| 170 | sort.Strings(keys) |
| 171 | for _, k := range keys { |
| 172 | info := m[k] |
| 173 | createFilegroup(mctx, info.module, info.scope, "latest", info.path) |
| 174 | } |
| 175 | } |
| 176 | |
Inseob Kim | c0907f1 | 2019-02-08 21:00:45 +0900 | [diff] [blame] | 177 | func PrebuiltApisMutator(mctx android.TopDownMutatorContext) { |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 178 | if _, ok := mctx.Module().(*prebuiltApis); ok { |
Sundong Ahn | a01c2a5 | 2018-06-07 21:42:16 +0900 | [diff] [blame] | 179 | prebuiltApiFiles(mctx) |
| 180 | prebuiltSdkStubs(mctx) |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Jaewoong Jung | 5fb5b2a | 2019-03-21 10:48:25 -0700 | [diff] [blame^] | 184 | // prebuilt_apis is a meta-module that generates filegroup modules for all |
| 185 | // API txt files found under the directory where the Android.bp is located. |
| 186 | // Specifically, an API file located at ./<ver>/<scope>/api/<module>.txt |
| 187 | // generates a filegroup module named <module>-api.<scope>.<ver>. |
| 188 | // |
| 189 | // It also creates <module>-api.<scope>.latest for the latest <ver>. |
Inseob Kim | c0907f1 | 2019-02-08 21:00:45 +0900 | [diff] [blame] | 190 | func PrebuiltApisFactory() android.Module { |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 191 | module := &prebuiltApis{} |
Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 192 | module.AddProperties(&module.properties) |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 193 | android.InitAndroidModule(module) |
| 194 | return module |
| 195 | } |