blob: 02b9b455d8257df4ea693abd426f3ad8914d6be4 [file] [log] [blame]
Jiyong Park58c518b2018-05-12 22:29:12 +09001// 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
15package java
16
17import (
18 "android/soong/android"
19 "sort"
Jiyong Park58c518b2018-05-12 22:29:12 +090020 "strings"
21
22 "github.com/google/blueprint/proptools"
23)
24
25// prebuilt_apis is a meta-module that generates filegroup modules for all
26// API txt files found under the directory where the Android.bp is located.
27// Specificaly, an API file located at ./<ver>/<scope>/api/<module>.txt
28// generates a filegroup module named <module>-api.<scope>.<ver>.
29//
30// It also creates <module>-api.<scope>.latest for the lastest <ver>.
31//
32func init() {
Inseob Kimc0907f12019-02-08 21:00:45 +090033 android.RegisterModuleType("prebuilt_apis", PrebuiltApisFactory)
Jiyong Park58c518b2018-05-12 22:29:12 +090034
35 android.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
Inseob Kimc0907f12019-02-08 21:00:45 +090036 ctx.TopDown("prebuilt_apis", PrebuiltApisMutator).Parallel()
Jiyong Park58c518b2018-05-12 22:29:12 +090037 })
38}
39
Sundong Ahn27eecb92018-06-21 13:47:17 +090040type prebuiltApisProperties struct {
41 // list of api version directories
42 Api_dirs []string
43}
44
Jiyong Park58c518b2018-05-12 22:29:12 +090045type prebuiltApis struct {
46 android.ModuleBase
Sundong Ahn27eecb92018-06-21 13:47:17 +090047 properties prebuiltApisProperties
Jiyong Park58c518b2018-05-12 22:29:12 +090048}
49
Jiyong Park58c518b2018-05-12 22:29:12 +090050func (module *prebuiltApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
51 // no need to implement
52}
53
Sundong Ahna01c2a52018-06-07 21:42:16 +090054func parseJarPath(ctx android.BaseModuleContext, path string) (module string, apiver string, scope string) {
55 elements := strings.Split(path, "/")
56
57 apiver = elements[0]
58 scope = elements[1]
Sundong Ahna01c2a52018-06-07 21:42:16 +090059
60 module = strings.TrimSuffix(elements[2], ".jar")
61 return
62}
63
Sundong Ahn054b19a2018-10-19 13:46:09 +090064func parseApiFilePath(ctx android.BaseModuleContext, path string) (module string, apiver string, scope string) {
Jiyong Park58c518b2018-05-12 22:29:12 +090065 elements := strings.Split(path, "/")
Sundong Ahn054b19a2018-10-19 13:46:09 +090066 apiver = elements[0]
Jiyong Park58c518b2018-05-12 22:29:12 +090067
68 scope = elements[1]
69 if scope != "public" && scope != "system" && scope != "test" {
70 ctx.ModuleErrorf("invalid scope %q found in path: %q", scope, path)
71 return
72 }
73
74 // elements[2] is string literal "api". skipping.
75 module = strings.TrimSuffix(elements[3], ".txt")
76 return
77}
78
Sundong Ahna01c2a52018-06-07 21:42:16 +090079func createImport(mctx android.TopDownMutatorContext, module string, scope string, apiver string, path string) {
80 props := struct {
81 Name *string
82 Jars []string
83 Sdk_version *string
84 Installable *bool
85 }{}
Sundong Ahn27eecb92018-06-21 13:47:17 +090086 props.Name = proptools.StringPtr(mctx.ModuleName() + "_" + scope + "_" + apiver + "_" + module)
Sundong Ahna01c2a52018-06-07 21:42:16 +090087 props.Jars = append(props.Jars, path)
88 // TODO(hansson): change to scope after migration is done.
89 props.Sdk_version = proptools.StringPtr("current")
90 props.Installable = proptools.BoolPtr(false)
91
92 mctx.CreateModule(android.ModuleFactoryAdaptor(ImportFactory), &props)
93}
94
Jiyong Park58c518b2018-05-12 22:29:12 +090095func createFilegroup(mctx android.TopDownMutatorContext, module string, scope string, apiver string, path string) {
96 fgName := module + ".api." + scope + "." + apiver
97 filegroupProps := struct {
98 Name *string
99 Srcs []string
100 }{}
101 filegroupProps.Name = proptools.StringPtr(fgName)
102 filegroupProps.Srcs = []string{path}
103 mctx.CreateModule(android.ModuleFactoryAdaptor(android.FileGroupFactory), &filegroupProps)
104}
105
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900106func getPrebuiltFiles(mctx android.TopDownMutatorContext, name string) []string {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900107 mydir := mctx.ModuleDir() + "/"
Sundong Ahn27eecb92018-06-21 13:47:17 +0900108 var files []string
109 for _, apiver := range mctx.Module().(*prebuiltApis).properties.Api_dirs {
110 for _, scope := range []string{"public", "system", "test", "core"} {
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900111 vfiles, err := mctx.GlobWithDeps(mydir+apiver+"/"+scope+"/"+name, nil)
Sundong Ahn27eecb92018-06-21 13:47:17 +0900112 if err != nil {
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900113 mctx.ModuleErrorf("failed to glob %s files under %q: %s", name, mydir+apiver+"/"+scope, err)
Sundong Ahn27eecb92018-06-21 13:47:17 +0900114 }
115 files = append(files, vfiles...)
116 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900117 }
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900118 return files
119}
120
121func prebuiltSdkStubs(mctx android.TopDownMutatorContext) {
122 mydir := mctx.ModuleDir() + "/"
123 // <apiver>/<scope>/<module>.jar
124 files := getPrebuiltFiles(mctx, "*.jar")
Sundong Ahna01c2a52018-06-07 21:42:16 +0900125
126 for _, f := range files {
127 // create a Import module for each jar file
128 localPath := strings.TrimPrefix(f, mydir)
129 module, apiver, scope := parseJarPath(mctx, localPath)
Sundong Ahn27eecb92018-06-21 13:47:17 +0900130 createImport(mctx, module, scope, apiver, localPath)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900131 }
132}
133
134func prebuiltApiFiles(mctx android.TopDownMutatorContext) {
135 mydir := mctx.ModuleDir() + "/"
136 // <apiver>/<scope>/api/<module>.txt
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900137 files := getPrebuiltFiles(mctx, "api/*.txt")
138
Sundong Ahna01c2a52018-06-07 21:42:16 +0900139 if len(files) == 0 {
140 mctx.ModuleErrorf("no api file found under %q", mydir)
141 }
142
143 // construct a map to find out the latest api file path
144 // for each (<module>, <scope>) pair.
145 type latestApiInfo struct {
146 module string
147 scope string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900148 apiver string
Sundong Ahna01c2a52018-06-07 21:42:16 +0900149 path string
150 }
151 m := make(map[string]latestApiInfo)
152
153 for _, f := range files {
154 // create a filegroup for each api txt file
155 localPath := strings.TrimPrefix(f, mydir)
156 module, apiver, scope := parseApiFilePath(mctx, localPath)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900157 createFilegroup(mctx, module, scope, apiver, localPath)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900158
159 // find the latest apiver
160 key := module + "." + scope
161 info, ok := m[key]
162 if !ok {
163 m[key] = latestApiInfo{module, scope, apiver, localPath}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900164 } else if len(apiver) > len(info.apiver) || (len(apiver) == len(info.apiver) &&
165 strings.Compare(apiver, info.apiver) > 0) {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900166 info.apiver = apiver
167 info.path = localPath
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900168 m[key] = info
Sundong Ahna01c2a52018-06-07 21:42:16 +0900169 }
170 }
171 // create filegroups for the latest version of (<module>, <scope>) pairs
172 // sort the keys in order to make build.ninja stable
173 keys := make([]string, 0, len(m))
174 for k := range m {
175 keys = append(keys, k)
176 }
177 sort.Strings(keys)
178 for _, k := range keys {
179 info := m[k]
180 createFilegroup(mctx, info.module, info.scope, "latest", info.path)
181 }
182}
183
Inseob Kimc0907f12019-02-08 21:00:45 +0900184func PrebuiltApisMutator(mctx android.TopDownMutatorContext) {
Jiyong Park58c518b2018-05-12 22:29:12 +0900185 if _, ok := mctx.Module().(*prebuiltApis); ok {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900186 prebuiltApiFiles(mctx)
187 prebuiltSdkStubs(mctx)
Jiyong Park58c518b2018-05-12 22:29:12 +0900188 }
189}
190
Inseob Kimc0907f12019-02-08 21:00:45 +0900191func PrebuiltApisFactory() android.Module {
Jiyong Park58c518b2018-05-12 22:29:12 +0900192 module := &prebuiltApis{}
Sundong Ahn27eecb92018-06-21 13:47:17 +0900193 module.AddProperties(&module.properties)
Jiyong Park58c518b2018-05-12 22:29:12 +0900194 android.InitAndroidModule(module)
195 return module
196}