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