blob: 59b2092d326f6e4b39a97824ad419eed0beb436d [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"
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//
33func 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 Ahn27eecb92018-06-21 13:47:17 +090041type prebuiltApisProperties struct {
42 // list of api version directories
43 Api_dirs []string
44}
45
Jiyong Park58c518b2018-05-12 22:29:12 +090046type prebuiltApis struct {
47 android.ModuleBase
Sundong Ahn27eecb92018-06-21 13:47:17 +090048 properties prebuiltApisProperties
Jiyong Park58c518b2018-05-12 22:29:12 +090049}
50
51func (module *prebuiltApis) DepsMutator(ctx android.BottomUpMutatorContext) {
52 // no need to implement
53}
54
55func (module *prebuiltApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
56 // no need to implement
57}
58
Sundong Ahna01c2a52018-06-07 21:42:16 +090059func 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 Ahna01c2a52018-06-07 21:42:16 +090064
65 module = strings.TrimSuffix(elements[2], ".jar")
66 return
67}
68
Jiyong Park58c518b2018-05-12 22:29:12 +090069func 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 Ahna01c2a52018-06-07 21:42:16 +090089func 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 Ahn27eecb92018-06-21 13:47:17 +090096 props.Name = proptools.StringPtr(mctx.ModuleName() + "_" + scope + "_" + apiver + "_" + module)
Sundong Ahna01c2a52018-06-07 21:42:16 +090097 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 Park58c518b2018-05-12 22:29:12 +0900105func 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 Ahna01c2a52018-06-07 21:42:16 +0900116func prebuiltSdkStubs(mctx android.TopDownMutatorContext) {
117 mydir := mctx.ModuleDir() + "/"
118 // <apiver>/<scope>/<module>.jar
Sundong Ahn27eecb92018-06-21 13:47:17 +0900119 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 Ahna01c2a52018-06-07 21:42:16 +0900128 }
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 Ahn27eecb92018-06-21 13:47:17 +0900134 createImport(mctx, module, scope, apiver, localPath)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900135 }
136}
137
138func 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 Park58c518b2018-05-12 22:29:12 +0900188func prebuiltApisMutator(mctx android.TopDownMutatorContext) {
189 if _, ok := mctx.Module().(*prebuiltApis); ok {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900190 prebuiltApiFiles(mctx)
191 prebuiltSdkStubs(mctx)
Jiyong Park58c518b2018-05-12 22:29:12 +0900192 }
193}
194
195func prebuiltApisFactory() android.Module {
196 module := &prebuiltApis{}
Sundong Ahn27eecb92018-06-21 13:47:17 +0900197 module.AddProperties(&module.properties)
Jiyong Park58c518b2018-05-12 22:29:12 +0900198 android.InitAndroidModule(module)
199 return module
200}