blob: c67e2bd591775907395041bbe2ac40d7b178d65e [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 Hanssonc79d4122021-02-17 14:21:33 +0000109func createApiModule(mctx android.LoadHookContext, name string, path string) {
110 genruleProps := struct {
Jiyong Park58c518b2018-05-12 22:29:12 +0900111 Name *string
112 Srcs []string
Anton Hanssonc79d4122021-02-17 14:21:33 +0000113 Out []string
114 Cmd *string
Jiyong Park58c518b2018-05-12 22:29:12 +0900115 }{}
Anton Hanssonc79d4122021-02-17 14:21:33 +0000116 genruleProps.Name = proptools.StringPtr(name)
117 genruleProps.Srcs = []string{path}
118 genruleProps.Out = []string{name}
119 genruleProps.Cmd = proptools.StringPtr("cp $(in) $(out)")
120 mctx.CreateModule(genrule.GenRuleFactory, &genruleProps)
Jiyong Park58c518b2018-05-12 22:29:12 +0900121}
122
Anton Hansson20ce41d2021-01-22 15:05:32 +0000123func createEmptyFile(mctx android.LoadHookContext, name string) {
124 props := struct {
125 Name *string
126 Cmd *string
127 Out []string
128 }{}
129 props.Name = proptools.StringPtr(name)
130 props.Out = []string{name}
131 props.Cmd = proptools.StringPtr("touch $(genDir)/" + name)
132 mctx.CreateModule(genrule.GenRuleFactory, &props)
133}
134
Liz Kammer2d2fd852020-08-12 14:42:30 -0700135func getPrebuiltFiles(mctx android.LoadHookContext, p *prebuiltApis, name string) []string {
Sundong Ahn27eecb92018-06-21 13:47:17 +0900136 var files []string
Liz Kammer2d2fd852020-08-12 14:42:30 -0700137 for _, apiver := range p.properties.Api_dirs {
Anton Hansson20ce41d2021-01-22 15:05:32 +0000138 files = append(files, getPrebuiltFilesInSubdir(mctx, apiver, name)...)
139 }
140 return files
141}
142
143func getPrebuiltFilesInSubdir(mctx android.LoadHookContext, subdir string, name string) []string {
144 var files []string
145 dir := mctx.ModuleDir() + "/" + subdir
146 for _, scope := range []string{"public", "system", "test", "core", "module-lib", "system-server"} {
147 glob := fmt.Sprintf("%s/%s/%s", dir, scope, name)
148 vfiles, err := mctx.GlobWithDeps(glob, nil)
149 if err != nil {
150 mctx.ModuleErrorf("failed to glob %s files under %q: %s", name, dir+"/"+scope, err)
Sundong Ahn27eecb92018-06-21 13:47:17 +0900151 }
Anton Hansson20ce41d2021-01-22 15:05:32 +0000152 files = append(files, vfiles...)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900153 }
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900154 return files
155}
156
Liz Kammer2d2fd852020-08-12 14:42:30 -0700157func prebuiltSdkStubs(mctx android.LoadHookContext, p *prebuiltApis) {
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900158 mydir := mctx.ModuleDir() + "/"
159 // <apiver>/<scope>/<module>.jar
Liz Kammer2d2fd852020-08-12 14:42:30 -0700160 files := getPrebuiltFiles(mctx, p, "*.jar")
161
Liz Kammer4e7f2602020-09-02 08:37:49 -0700162 sdkVersion := proptools.StringDefault(p.properties.Imports_sdk_version, "current")
163 compileDex := proptools.BoolDefault(p.properties.Imports_compile_dex, false)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900164
165 for _, f := range files {
166 // create a Import module for each jar file
167 localPath := strings.TrimPrefix(f, mydir)
Paul Duffind4c03562020-04-09 17:15:44 +0100168 module, apiver, scope := parseJarPath(localPath)
Liz Kammer4e7f2602020-09-02 08:37:49 -0700169 createImport(mctx, module, scope, apiver, localPath, sdkVersion, compileDex)
Paul Duffinb077bcc2021-10-28 13:27:37 +0100170
171 if module == "core-for-system-modules" {
172 createSystemModules(mctx, apiver, scope)
173 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900174 }
175}
176
Paul Duffinb077bcc2021-10-28 13:27:37 +0100177func createSystemModules(mctx android.LoadHookContext, apiver string, scope string) {
Colin Cross17dec172020-05-14 18:05:32 -0700178 props := struct {
179 Name *string
180 Libs []string
181 }{}
Paul Duffinb077bcc2021-10-28 13:27:37 +0100182 props.Name = proptools.StringPtr(prebuiltApiModuleName(mctx, "system_modules", scope, apiver))
183 props.Libs = append(props.Libs, prebuiltApiModuleName(mctx, "core-for-system-modules", scope, apiver))
Colin Cross17dec172020-05-14 18:05:32 -0700184
Paul Duffind6c2a652021-03-11 07:56:22 +0000185 mctx.CreateModule(systemModulesImportFactory, &props)
Colin Cross17dec172020-05-14 18:05:32 -0700186}
187
Liz Kammer2d2fd852020-08-12 14:42:30 -0700188func prebuiltApiFiles(mctx android.LoadHookContext, p *prebuiltApis) {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900189 mydir := mctx.ModuleDir() + "/"
190 // <apiver>/<scope>/api/<module>.txt
Liz Kammer2d2fd852020-08-12 14:42:30 -0700191 files := getPrebuiltFiles(mctx, p, "api/*.txt")
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900192
Sundong Ahna01c2a52018-06-07 21:42:16 +0900193 if len(files) == 0 {
194 mctx.ModuleErrorf("no api file found under %q", mydir)
195 }
196
197 // construct a map to find out the latest api file path
198 // for each (<module>, <scope>) pair.
199 type latestApiInfo struct {
Anton Hansson370fd0b2021-01-22 15:05:04 +0000200 module string
201 scope string
202 version int
203 path string
Sundong Ahna01c2a52018-06-07 21:42:16 +0900204 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900205
Anton Hanssonc79d4122021-02-17 14:21:33 +0000206 // Create modules for all (<module>, <scope, <version>) triplets,
207 // and a "latest" module variant for each (<module>, <scope>) pair
208 apiModuleName := func(module, scope, version string) string {
Anton Hansson20ce41d2021-01-22 15:05:32 +0000209 return module + ".api." + scope + "." + version
210 }
Anton Hansson370fd0b2021-01-22 15:05:04 +0000211 m := make(map[string]latestApiInfo)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900212 for _, f := range files {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900213 localPath := strings.TrimPrefix(f, mydir)
214 module, apiver, scope := parseApiFilePath(mctx, localPath)
Anton Hanssonc79d4122021-02-17 14:21:33 +0000215 createApiModule(mctx, apiModuleName(module, scope, apiver), localPath)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900216
Anton Hansson370fd0b2021-01-22 15:05:04 +0000217 version, err := strconv.Atoi(apiver)
218 if err != nil {
219 mctx.ModuleErrorf("Found finalized API files in non-numeric dir %v", apiver)
220 return
221 }
222
Anton Hansson20ce41d2021-01-22 15:05:32 +0000223 // Track latest version of each module/scope, except for incompatibilities
224 if !strings.HasSuffix(module, "incompatibilities") {
225 key := module + "." + scope
226 info, ok := m[key]
227 if !ok {
228 m[key] = latestApiInfo{module, scope, version, localPath}
229 } else if version > info.version {
230 info.version = version
231 info.path = localPath
232 m[key] = info
233 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900234 }
235 }
Anton Hansson20ce41d2021-01-22 15:05:32 +0000236
Anton Hansson370fd0b2021-01-22 15:05:04 +0000237 // Sort the keys in order to make build.ninja stable
238 for _, k := range android.SortedStringKeys(m) {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900239 info := m[k]
Anton Hanssonc79d4122021-02-17 14:21:33 +0000240 name := apiModuleName(info.module, info.scope, "latest")
241 createApiModule(mctx, name, info.path)
Anton Hansson20ce41d2021-01-22 15:05:32 +0000242 }
243
244 // Create incompatibilities tracking files for all modules, if we have a "next" api.
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800245 incompatibilities := make(map[string]bool)
Anton Hansson20ce41d2021-01-22 15:05:32 +0000246 if nextApiDir := String(p.properties.Next_api_dir); nextApiDir != "" {
247 files := getPrebuiltFilesInSubdir(mctx, nextApiDir, "api/*incompatibilities.txt")
Anton Hansson20ce41d2021-01-22 15:05:32 +0000248 for _, f := range files {
249 localPath := strings.TrimPrefix(f, mydir)
Anton Hanssonfa5e6b52021-04-13 19:09:48 +0100250 filename, _, scope := parseApiFilePath(mctx, localPath)
251 referencedModule := strings.TrimSuffix(filename, "-incompatibilities")
Anton Hansson20ce41d2021-01-22 15:05:32 +0000252
Anton Hanssonc79d4122021-02-17 14:21:33 +0000253 createApiModule(mctx, apiModuleName(referencedModule+"-incompatibilities", scope, "latest"), localPath)
Anton Hansson20ce41d2021-01-22 15:05:32 +0000254
255 incompatibilities[referencedModule+"."+scope] = true
256 }
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800257 }
258 // Create empty incompatibilities files for remaining modules
259 for _, k := range android.SortedStringKeys(m) {
260 if _, ok := incompatibilities[k]; !ok {
261 createEmptyFile(mctx, apiModuleName(m[k].module+"-incompatibilities", m[k].scope, "latest"))
Anton Hansson20ce41d2021-01-22 15:05:32 +0000262 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900263 }
264}
265
Paul Duffind4c03562020-04-09 17:15:44 +0100266func createPrebuiltApiModules(mctx android.LoadHookContext) {
Liz Kammer2d2fd852020-08-12 14:42:30 -0700267 if p, ok := mctx.Module().(*prebuiltApis); ok {
268 prebuiltApiFiles(mctx, p)
269 prebuiltSdkStubs(mctx, p)
Jiyong Park58c518b2018-05-12 22:29:12 +0900270 }
271}
272
Anton Hanssonc79d4122021-02-17 14:21:33 +0000273// prebuilt_apis is a meta-module that generates modules for all API txt files
274// found under the directory where the Android.bp is located.
Jaewoong Jung5fb5b2a2019-03-21 10:48:25 -0700275// Specifically, an API file located at ./<ver>/<scope>/api/<module>.txt
Anton Hanssonc79d4122021-02-17 14:21:33 +0000276// generates a module named <module>-api.<scope>.<ver>.
Jaewoong Jung5fb5b2a2019-03-21 10:48:25 -0700277//
278// It also creates <module>-api.<scope>.latest for the latest <ver>.
Paul Duffind4c03562020-04-09 17:15:44 +0100279//
280// Similarly, it generates a java_import for all API .jar files found under the
281// directory where the Android.bp is located. Specifically, an API file located
282// at ./<ver>/<scope>/api/<module>.jar generates a java_import module named
Colin Cross17dec172020-05-14 18:05:32 -0700283// <prebuilt-api-module>_<scope>_<ver>_<module>, and for SDK versions >= 30
284// a java_system_modules module named
285// <prebuilt-api-module>_public_<ver>_system_modules
Inseob Kimc0907f12019-02-08 21:00:45 +0900286func PrebuiltApisFactory() android.Module {
Jiyong Park58c518b2018-05-12 22:29:12 +0900287 module := &prebuiltApis{}
Sundong Ahn27eecb92018-06-21 13:47:17 +0900288 module.AddProperties(&module.properties)
Jiyong Park58c518b2018-05-12 22:29:12 +0900289 android.InitAndroidModule(module)
Paul Duffind4c03562020-04-09 17:15:44 +0100290 android.AddLoadHook(module, createPrebuiltApiModules)
Jiyong Park58c518b2018-05-12 22:29:12 +0900291 return module
292}