| 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 |  | 
|  | 41 | type prebuiltApis struct { | 
|  | 42 | android.ModuleBase | 
|  | 43 | } | 
|  | 44 |  | 
|  | 45 | func (module *prebuiltApis) DepsMutator(ctx android.BottomUpMutatorContext) { | 
|  | 46 | // no need to implement | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | func (module *prebuiltApis) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
|  | 50 | // no need to implement | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | func parseApiFilePath(ctx android.BaseModuleContext, path string) (module string, apiver int, scope string) { | 
|  | 54 | elements := strings.Split(path, "/") | 
|  | 55 | ver, err := strconv.Atoi(elements[0]) | 
|  | 56 | if err != nil { | 
|  | 57 | ctx.ModuleErrorf("invalid version %q found in path: %q", elements[0], path) | 
|  | 58 | return | 
|  | 59 | } | 
|  | 60 | apiver = ver | 
|  | 61 |  | 
|  | 62 | scope = elements[1] | 
|  | 63 | if scope != "public" && scope != "system" && scope != "test" { | 
|  | 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 |  | 
|  | 73 | func createFilegroup(mctx android.TopDownMutatorContext, module string, scope string, apiver string, path string) { | 
|  | 74 | fgName := module + ".api." + scope + "." + apiver | 
|  | 75 | filegroupProps := struct { | 
|  | 76 | Name *string | 
|  | 77 | Srcs []string | 
|  | 78 | }{} | 
|  | 79 | filegroupProps.Name = proptools.StringPtr(fgName) | 
|  | 80 | filegroupProps.Srcs = []string{path} | 
|  | 81 | mctx.CreateModule(android.ModuleFactoryAdaptor(android.FileGroupFactory), &filegroupProps) | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | func prebuiltApisMutator(mctx android.TopDownMutatorContext) { | 
|  | 85 | if _, ok := mctx.Module().(*prebuiltApis); ok { | 
|  | 86 | mydir := mctx.ModuleDir() + "/" | 
|  | 87 | // <apiver>/<scope>/api/<module>.txt | 
|  | 88 | files, err := mctx.GlobWithDeps(mydir+"*/*/api/*.txt", nil) | 
|  | 89 | if err != nil { | 
|  | 90 | mctx.ModuleErrorf("failed to glob api txt files under %q: %s", mydir, err) | 
|  | 91 | } | 
|  | 92 | if len(files) == 0 { | 
|  | 93 | mctx.ModuleErrorf("no api file found under %q", mydir) | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | // construct a map to find out the latest api file path | 
|  | 97 | // for each (<module>, <scope>) pair. | 
|  | 98 | type latestApiInfo struct { | 
|  | 99 | module string | 
|  | 100 | scope  string | 
|  | 101 | apiver int | 
|  | 102 | path   string | 
|  | 103 | } | 
|  | 104 | m := make(map[string]latestApiInfo) | 
|  | 105 |  | 
|  | 106 | for _, f := range files { | 
|  | 107 | // create a filegroup for each api txt file | 
|  | 108 | localPath := strings.TrimPrefix(f, mydir) | 
|  | 109 | module, apiver, scope := parseApiFilePath(mctx, localPath) | 
|  | 110 | createFilegroup(mctx, module, scope, strconv.Itoa(apiver), localPath) | 
|  | 111 |  | 
|  | 112 | // find the latest apiver | 
|  | 113 | key := module + "." + scope | 
|  | 114 | info, ok := m[key] | 
|  | 115 | if !ok { | 
|  | 116 | m[key] = latestApiInfo{module, scope, apiver, localPath} | 
|  | 117 | } else if apiver > info.apiver { | 
|  | 118 | info.apiver = apiver | 
|  | 119 | info.path = localPath | 
|  | 120 | } | 
|  | 121 | } | 
|  | 122 | // create filegroups for the latest version of (<module>, <scope>) pairs | 
|  | 123 | // sort the keys in order to make build.ninja stable | 
|  | 124 | keys := make([]string, 0, len(m)) | 
|  | 125 | for k := range m { | 
|  | 126 | keys = append(keys, k) | 
|  | 127 | } | 
|  | 128 | sort.Strings(keys) | 
|  | 129 | for _, k := range keys { | 
|  | 130 | info := m[k] | 
|  | 131 | createFilegroup(mctx, info.module, info.scope, "latest", info.path) | 
|  | 132 | } | 
|  | 133 | } | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | func prebuiltApisFactory() android.Module { | 
|  | 137 | module := &prebuiltApis{} | 
|  | 138 | android.InitAndroidModule(module) | 
|  | 139 | return module | 
|  | 140 | } |