blob: 1e90149ead8bd03d765f97262e57e8f66a056718 [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 (
Anton Hansson20ce41d2021-01-22 15:05:32 +000018 "fmt"
Anton Hansson370fd0b2021-01-22 15:05:04 +000019 "strconv"
Jiyong Park58c518b2018-05-12 22:29:12 +090020 "strings"
21
22 "github.com/google/blueprint/proptools"
Colin Cross17dec172020-05-14 18:05:32 -070023
24 "android/soong/android"
Anton Hansson20ce41d2021-01-22 15:05:32 +000025 "android/soong/genrule"
Jiyong Park58c518b2018-05-12 22:29:12 +090026)
27
Jiyong Park58c518b2018-05-12 22:29:12 +090028func init() {
Paul Duffina48f7582019-12-19 11:25:19 +000029 RegisterPrebuiltApisBuildComponents(android.InitRegistrationContext)
30}
Jiyong Park58c518b2018-05-12 22:29:12 +090031
Paul Duffina48f7582019-12-19 11:25:19 +000032func RegisterPrebuiltApisBuildComponents(ctx android.RegistrationContext) {
33 ctx.RegisterModuleType("prebuilt_apis", PrebuiltApisFactory)
Jiyong Park58c518b2018-05-12 22:29:12 +090034}
35
Sundong Ahn27eecb92018-06-21 13:47:17 +090036type prebuiltApisProperties struct {
37 // list of api version directories
38 Api_dirs []string
Liz Kammer2d2fd852020-08-12 14:42:30 -070039
Anton Hansson20ce41d2021-01-22 15:05:32 +000040 // The next API directory can optionally point to a directory where
41 // files incompatibility-tracking files are stored for the current
42 // "in progress" API. Each module present in one of the api_dirs will have
43 // a <module>-incompatibilities.api.<scope>.latest module created.
44 Next_api_dir *string
45
Liz Kammer2d2fd852020-08-12 14:42:30 -070046 // The sdk_version of java_import modules generated based on jar files.
47 // Defaults to "current"
48 Imports_sdk_version *string
Liz Kammer4e7f2602020-09-02 08:37:49 -070049
50 // If set to true, compile dex for java_import modules. Defaults to false.
51 Imports_compile_dex *bool
Sundong Ahn27eecb92018-06-21 13:47:17 +090052}
53
Jiyong Park58c518b2018-05-12 22:29:12 +090054type prebuiltApis struct {
55 android.ModuleBase
Sundong Ahn27eecb92018-06-21 13:47:17 +090056 properties prebuiltApisProperties
Jiyong Park58c518b2018-05-12 22:29:12 +090057}
58
Jiyong Park58c518b2018-05-12 22:29:12 +090059func (module *prebuiltApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
60 // no need to implement
61}
62
Paul Duffind4c03562020-04-09 17:15:44 +010063func parseJarPath(path string) (module string, apiver string, scope string) {
Sundong Ahna01c2a52018-06-07 21:42:16 +090064 elements := strings.Split(path, "/")
65
66 apiver = elements[0]
67 scope = elements[1]
Sundong Ahna01c2a52018-06-07 21:42:16 +090068
69 module = strings.TrimSuffix(elements[2], ".jar")
70 return
71}
72
Paul Duffind4c03562020-04-09 17:15:44 +010073func parseApiFilePath(ctx android.LoadHookContext, path string) (module string, apiver string, scope string) {
Jiyong Park58c518b2018-05-12 22:29:12 +090074 elements := strings.Split(path, "/")
Sundong Ahn054b19a2018-10-19 13:46:09 +090075 apiver = elements[0]
Jiyong Park58c518b2018-05-12 22:29:12 +090076
77 scope = elements[1]
Anton Hansson8d239692020-05-01 18:37:15 +010078 if scope != "public" && scope != "system" && scope != "test" && scope != "module-lib" && scope != "system-server" {
Jiyong Park58c518b2018-05-12 22:29:12 +090079 ctx.ModuleErrorf("invalid scope %q found in path: %q", scope, path)
80 return
81 }
82
83 // elements[2] is string literal "api". skipping.
84 module = strings.TrimSuffix(elements[3], ".txt")
85 return
86}
87
Colin Cross17dec172020-05-14 18:05:32 -070088func prebuiltApiModuleName(mctx android.LoadHookContext, module string, scope string, apiver string) string {
89 return mctx.ModuleName() + "_" + scope + "_" + apiver + "_" + module
90}
91
Liz Kammer4e7f2602020-09-02 08:37:49 -070092func createImport(mctx android.LoadHookContext, module, scope, apiver, path, sdkVersion string, compileDex bool) {
Sundong Ahna01c2a52018-06-07 21:42:16 +090093 props := struct {
94 Name *string
95 Jars []string
96 Sdk_version *string
97 Installable *bool
Liz Kammer4e7f2602020-09-02 08:37:49 -070098 Compile_dex *bool
Sundong Ahna01c2a52018-06-07 21:42:16 +090099 }{}
Colin Cross17dec172020-05-14 18:05:32 -0700100 props.Name = proptools.StringPtr(prebuiltApiModuleName(mctx, module, scope, apiver))
Sundong Ahna01c2a52018-06-07 21:42:16 +0900101 props.Jars = append(props.Jars, path)
Liz Kammer4e7f2602020-09-02 08:37:49 -0700102 props.Sdk_version = proptools.StringPtr(sdkVersion)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900103 props.Installable = proptools.BoolPtr(false)
Liz Kammer4e7f2602020-09-02 08:37:49 -0700104 props.Compile_dex = proptools.BoolPtr(compileDex)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900105
Colin Cross84dfc3d2019-09-25 11:33:01 -0700106 mctx.CreateModule(ImportFactory, &props)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900107}
108
Anton Hansson20ce41d2021-01-22 15:05:32 +0000109func createFilegroup(mctx android.LoadHookContext, name string, path string) {
Jiyong Park58c518b2018-05-12 22:29:12 +0900110 filegroupProps := struct {
111 Name *string
112 Srcs []string
113 }{}
Anton Hansson20ce41d2021-01-22 15:05:32 +0000114 filegroupProps.Name = proptools.StringPtr(name)
Jiyong Park58c518b2018-05-12 22:29:12 +0900115 filegroupProps.Srcs = []string{path}
Colin Cross84dfc3d2019-09-25 11:33:01 -0700116 mctx.CreateModule(android.FileGroupFactory, &filegroupProps)
Jiyong Park58c518b2018-05-12 22:29:12 +0900117}
118
Anton Hansson20ce41d2021-01-22 15:05:32 +0000119func createEmptyFile(mctx android.LoadHookContext, name string) {
120 props := struct {
121 Name *string
122 Cmd *string
123 Out []string
124 }{}
125 props.Name = proptools.StringPtr(name)
126 props.Out = []string{name}
127 props.Cmd = proptools.StringPtr("touch $(genDir)/" + name)
128 mctx.CreateModule(genrule.GenRuleFactory, &props)
129}
130
Liz Kammer2d2fd852020-08-12 14:42:30 -0700131func getPrebuiltFiles(mctx android.LoadHookContext, p *prebuiltApis, name string) []string {
Sundong Ahn27eecb92018-06-21 13:47:17 +0900132 var files []string
Liz Kammer2d2fd852020-08-12 14:42:30 -0700133 for _, apiver := range p.properties.Api_dirs {
Anton Hansson20ce41d2021-01-22 15:05:32 +0000134 files = append(files, getPrebuiltFilesInSubdir(mctx, apiver, name)...)
135 }
136 return files
137}
138
139func getPrebuiltFilesInSubdir(mctx android.LoadHookContext, subdir string, name string) []string {
140 var files []string
141 dir := mctx.ModuleDir() + "/" + subdir
142 for _, scope := range []string{"public", "system", "test", "core", "module-lib", "system-server"} {
143 glob := fmt.Sprintf("%s/%s/%s", dir, scope, name)
144 vfiles, err := mctx.GlobWithDeps(glob, nil)
145 if err != nil {
146 mctx.ModuleErrorf("failed to glob %s files under %q: %s", name, dir+"/"+scope, err)
Sundong Ahn27eecb92018-06-21 13:47:17 +0900147 }
Anton Hansson20ce41d2021-01-22 15:05:32 +0000148 files = append(files, vfiles...)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900149 }
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900150 return files
151}
152
Liz Kammer2d2fd852020-08-12 14:42:30 -0700153func prebuiltSdkStubs(mctx android.LoadHookContext, p *prebuiltApis) {
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900154 mydir := mctx.ModuleDir() + "/"
155 // <apiver>/<scope>/<module>.jar
Liz Kammer2d2fd852020-08-12 14:42:30 -0700156 files := getPrebuiltFiles(mctx, p, "*.jar")
157
Liz Kammer4e7f2602020-09-02 08:37:49 -0700158 sdkVersion := proptools.StringDefault(p.properties.Imports_sdk_version, "current")
159 compileDex := proptools.BoolDefault(p.properties.Imports_compile_dex, false)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900160
161 for _, f := range files {
162 // create a Import module for each jar file
163 localPath := strings.TrimPrefix(f, mydir)
Paul Duffind4c03562020-04-09 17:15:44 +0100164 module, apiver, scope := parseJarPath(localPath)
Liz Kammer4e7f2602020-09-02 08:37:49 -0700165 createImport(mctx, module, scope, apiver, localPath, sdkVersion, compileDex)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900166 }
167}
168
Colin Cross17dec172020-05-14 18:05:32 -0700169func createSystemModules(mctx android.LoadHookContext, apiver string) {
170 props := struct {
171 Name *string
172 Libs []string
173 }{}
174 props.Name = proptools.StringPtr(prebuiltApiModuleName(mctx, "system_modules", "public", apiver))
175 props.Libs = append(props.Libs, prebuiltApiModuleName(mctx, "core-for-system-modules", "public", apiver))
176
177 mctx.CreateModule(SystemModulesFactory, &props)
178}
179
Liz Kammer2d2fd852020-08-12 14:42:30 -0700180func prebuiltSdkSystemModules(mctx android.LoadHookContext, p *prebuiltApis) {
181 for _, apiver := range p.properties.Api_dirs {
Colin Cross17dec172020-05-14 18:05:32 -0700182 jar := android.ExistentPathForSource(mctx,
183 mctx.ModuleDir(), apiver, "public", "core-for-system-modules.jar")
184 if jar.Valid() {
185 createSystemModules(mctx, apiver)
186 }
187 }
188}
189
Liz Kammer2d2fd852020-08-12 14:42:30 -0700190func prebuiltApiFiles(mctx android.LoadHookContext, p *prebuiltApis) {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900191 mydir := mctx.ModuleDir() + "/"
192 // <apiver>/<scope>/api/<module>.txt
Liz Kammer2d2fd852020-08-12 14:42:30 -0700193 files := getPrebuiltFiles(mctx, p, "api/*.txt")
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900194
Sundong Ahna01c2a52018-06-07 21:42:16 +0900195 if len(files) == 0 {
196 mctx.ModuleErrorf("no api file found under %q", mydir)
197 }
198
199 // construct a map to find out the latest api file path
200 // for each (<module>, <scope>) pair.
201 type latestApiInfo struct {
Anton Hansson370fd0b2021-01-22 15:05:04 +0000202 module string
203 scope string
204 version int
205 path string
Sundong Ahna01c2a52018-06-07 21:42:16 +0900206 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900207
Anton Hansson370fd0b2021-01-22 15:05:04 +0000208 // Create filegroups for all (<module>, <scope, <version>) triplets,
209 // and a "latest" filegroup variant for each (<module>, <scope>) pair
Anton Hansson20ce41d2021-01-22 15:05:32 +0000210 moduleName := func(module, scope, version string) string {
211 return module + ".api." + scope + "." + version
212 }
Anton Hansson370fd0b2021-01-22 15:05:04 +0000213 m := make(map[string]latestApiInfo)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900214 for _, f := range files {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900215 localPath := strings.TrimPrefix(f, mydir)
216 module, apiver, scope := parseApiFilePath(mctx, localPath)
Anton Hansson20ce41d2021-01-22 15:05:32 +0000217 createFilegroup(mctx, moduleName(module, scope, apiver), localPath)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900218
Anton Hansson370fd0b2021-01-22 15:05:04 +0000219 version, err := strconv.Atoi(apiver)
220 if err != nil {
221 mctx.ModuleErrorf("Found finalized API files in non-numeric dir %v", apiver)
222 return
223 }
224
Anton Hansson20ce41d2021-01-22 15:05:32 +0000225 // Track latest version of each module/scope, except for incompatibilities
226 if !strings.HasSuffix(module, "incompatibilities") {
227 key := module + "." + scope
228 info, ok := m[key]
229 if !ok {
230 m[key] = latestApiInfo{module, scope, version, localPath}
231 } else if version > info.version {
232 info.version = version
233 info.path = localPath
234 m[key] = info
235 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900236 }
237 }
Anton Hansson20ce41d2021-01-22 15:05:32 +0000238
Anton Hansson370fd0b2021-01-22 15:05:04 +0000239 // Sort the keys in order to make build.ninja stable
240 for _, k := range android.SortedStringKeys(m) {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900241 info := m[k]
Anton Hansson20ce41d2021-01-22 15:05:32 +0000242 name := moduleName(info.module, info.scope, "latest")
243 createFilegroup(mctx, name, info.path)
244 }
245
246 // Create incompatibilities tracking files for all modules, if we have a "next" api.
247 if nextApiDir := String(p.properties.Next_api_dir); nextApiDir != "" {
248 files := getPrebuiltFilesInSubdir(mctx, nextApiDir, "api/*incompatibilities.txt")
249 incompatibilities := make(map[string]bool)
250 for _, f := range files {
251 localPath := strings.TrimPrefix(f, mydir)
252 module, _, scope := parseApiFilePath(mctx, localPath)
253
254 // Figure out which module is referenced by this file. Special case for "android".
255 referencedModule := strings.TrimSuffix(module, "incompatibilities")
256 referencedModule = strings.TrimSuffix(referencedModule, "-")
257 if referencedModule == "" {
258 referencedModule = "android"
259 }
260
261 createFilegroup(mctx, moduleName(referencedModule+"-incompatibilities", scope, "latest"), localPath)
262
263 incompatibilities[referencedModule+"."+scope] = true
264 }
265 // Create empty incompatibilities files for remaining modules
266 for _, k := range android.SortedStringKeys(m) {
267 if _, ok := incompatibilities[k]; !ok {
268 createEmptyFile(mctx, moduleName(m[k].module+"-incompatibilities", m[k].scope, "latest"))
269 }
270 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900271 }
272}
273
Paul Duffind4c03562020-04-09 17:15:44 +0100274func createPrebuiltApiModules(mctx android.LoadHookContext) {
Liz Kammer2d2fd852020-08-12 14:42:30 -0700275 if p, ok := mctx.Module().(*prebuiltApis); ok {
276 prebuiltApiFiles(mctx, p)
277 prebuiltSdkStubs(mctx, p)
278 prebuiltSdkSystemModules(mctx, p)
Jiyong Park58c518b2018-05-12 22:29:12 +0900279 }
280}
281
Jaewoong Jung5fb5b2a2019-03-21 10:48:25 -0700282// prebuilt_apis is a meta-module that generates filegroup modules for all
283// API txt files found under the directory where the Android.bp is located.
284// Specifically, an API file located at ./<ver>/<scope>/api/<module>.txt
285// generates a filegroup module named <module>-api.<scope>.<ver>.
286//
287// It also creates <module>-api.<scope>.latest for the latest <ver>.
Paul Duffind4c03562020-04-09 17:15:44 +0100288//
289// Similarly, it generates a java_import for all API .jar files found under the
290// directory where the Android.bp is located. Specifically, an API file located
291// at ./<ver>/<scope>/api/<module>.jar generates a java_import module named
Colin Cross17dec172020-05-14 18:05:32 -0700292// <prebuilt-api-module>_<scope>_<ver>_<module>, and for SDK versions >= 30
293// a java_system_modules module named
294// <prebuilt-api-module>_public_<ver>_system_modules
Inseob Kimc0907f12019-02-08 21:00:45 +0900295func PrebuiltApisFactory() android.Module {
Jiyong Park58c518b2018-05-12 22:29:12 +0900296 module := &prebuiltApis{}
Sundong Ahn27eecb92018-06-21 13:47:17 +0900297 module.AddProperties(&module.properties)
Jiyong Park58c518b2018-05-12 22:29:12 +0900298 android.InitAndroidModule(module)
Paul Duffind4c03562020-04-09 17:15:44 +0100299 android.AddLoadHook(module, createPrebuiltApiModules)
Jiyong Park58c518b2018-05-12 22:29:12 +0900300 return module
301}