blob: 3f4b076b5e0f636bfb716ab31af496cddd1d04b7 [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
41type prebuiltApis struct {
42 android.ModuleBase
43}
44
45func (module *prebuiltApis) DepsMutator(ctx android.BottomUpMutatorContext) {
46 // no need to implement
47}
48
49func (module *prebuiltApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
50 // no need to implement
51}
52
Sundong Ahna01c2a52018-06-07 21:42:16 +090053func parseJarPath(ctx android.BaseModuleContext, path string) (module string, apiver string, scope string) {
54 elements := strings.Split(path, "/")
55
56 apiver = elements[0]
57 scope = elements[1]
58 if scope != "public" && scope != "system" && scope != "test" && scope != "core" {
59 // scope must be public, system or test
60 return
61 }
62
63 module = strings.TrimSuffix(elements[2], ".jar")
64 return
65}
66
Jiyong Park58c518b2018-05-12 22:29:12 +090067func parseApiFilePath(ctx android.BaseModuleContext, path string) (module string, apiver int, scope string) {
68 elements := strings.Split(path, "/")
69 ver, err := strconv.Atoi(elements[0])
70 if err != nil {
71 ctx.ModuleErrorf("invalid version %q found in path: %q", elements[0], path)
72 return
73 }
74 apiver = ver
75
76 scope = elements[1]
77 if scope != "public" && scope != "system" && scope != "test" {
78 ctx.ModuleErrorf("invalid scope %q found in path: %q", scope, path)
79 return
80 }
81
82 // elements[2] is string literal "api". skipping.
83 module = strings.TrimSuffix(elements[3], ".txt")
84 return
85}
86
Sundong Ahna01c2a52018-06-07 21:42:16 +090087func createImport(mctx android.TopDownMutatorContext, module string, scope string, apiver string, path string) {
88 props := struct {
89 Name *string
90 Jars []string
91 Sdk_version *string
92 Installable *bool
93 }{}
94 props.Name = proptools.StringPtr("sdk_" + scope + "_" + apiver + "_" + module)
95 props.Jars = append(props.Jars, path)
96 // TODO(hansson): change to scope after migration is done.
97 props.Sdk_version = proptools.StringPtr("current")
98 props.Installable = proptools.BoolPtr(false)
99
100 mctx.CreateModule(android.ModuleFactoryAdaptor(ImportFactory), &props)
101}
102
Jiyong Park58c518b2018-05-12 22:29:12 +0900103func createFilegroup(mctx android.TopDownMutatorContext, module string, scope string, apiver string, path string) {
104 fgName := module + ".api." + scope + "." + apiver
105 filegroupProps := struct {
106 Name *string
107 Srcs []string
108 }{}
109 filegroupProps.Name = proptools.StringPtr(fgName)
110 filegroupProps.Srcs = []string{path}
111 mctx.CreateModule(android.ModuleFactoryAdaptor(android.FileGroupFactory), &filegroupProps)
112}
113
Sundong Ahna01c2a52018-06-07 21:42:16 +0900114func prebuiltSdkStubs(mctx android.TopDownMutatorContext) {
115 mydir := mctx.ModuleDir() + "/"
116 // <apiver>/<scope>/<module>.jar
117 files, err := mctx.GlobWithDeps(mydir+"*/*/*.jar", nil)
118 if err != nil {
119 mctx.ModuleErrorf("failed to glob jar files under %q: %s", mydir, err)
120 }
121 if len(files) == 0 {
122 mctx.ModuleErrorf("no jar file found under %q", mydir)
123 }
124
125 for _, f := range files {
126 // create a Import module for each jar file
127 localPath := strings.TrimPrefix(f, mydir)
128 module, apiver, scope := parseJarPath(mctx, localPath)
129
130 if len(module) != 0 {
131 createImport(mctx, module, scope, apiver, localPath)
132 }
133 }
134}
135
136func prebuiltApiFiles(mctx android.TopDownMutatorContext) {
137 mydir := mctx.ModuleDir() + "/"
138 // <apiver>/<scope>/api/<module>.txt
139 files, err := mctx.GlobWithDeps(mydir+"*/*/api/*.txt", nil)
140 if err != nil {
141 mctx.ModuleErrorf("failed to glob api txt files under %q: %s", mydir, err)
142 }
143 if len(files) == 0 {
144 mctx.ModuleErrorf("no api file found under %q", mydir)
145 }
146
147 // construct a map to find out the latest api file path
148 // for each (<module>, <scope>) pair.
149 type latestApiInfo struct {
150 module string
151 scope string
152 apiver int
153 path string
154 }
155 m := make(map[string]latestApiInfo)
156
157 for _, f := range files {
158 // create a filegroup for each api txt file
159 localPath := strings.TrimPrefix(f, mydir)
160 module, apiver, scope := parseApiFilePath(mctx, localPath)
161 createFilegroup(mctx, module, scope, strconv.Itoa(apiver), localPath)
162
163 // find the latest apiver
164 key := module + "." + scope
165 info, ok := m[key]
166 if !ok {
167 m[key] = latestApiInfo{module, scope, apiver, localPath}
168 } else if apiver > info.apiver {
169 info.apiver = apiver
170 info.path = localPath
171 }
172 }
173 // create filegroups for the latest version of (<module>, <scope>) pairs
174 // sort the keys in order to make build.ninja stable
175 keys := make([]string, 0, len(m))
176 for k := range m {
177 keys = append(keys, k)
178 }
179 sort.Strings(keys)
180 for _, k := range keys {
181 info := m[k]
182 createFilegroup(mctx, info.module, info.scope, "latest", info.path)
183 }
184}
185
Jiyong Park58c518b2018-05-12 22:29:12 +0900186func prebuiltApisMutator(mctx android.TopDownMutatorContext) {
187 if _, ok := mctx.Module().(*prebuiltApis); ok {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900188 prebuiltApiFiles(mctx)
189 prebuiltSdkStubs(mctx)
Jiyong Park58c518b2018-05-12 22:29:12 +0900190 }
191}
192
193func prebuiltApisFactory() android.Module {
194 module := &prebuiltApis{}
195 android.InitAndroidModule(module)
196 return module
197}