blob: c91b3211785d4e0b836207c7ccee3fe73ba134fb [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)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900170 }
171}
172
Colin Cross17dec172020-05-14 18:05:32 -0700173func createSystemModules(mctx android.LoadHookContext, apiver string) {
174 props := struct {
175 Name *string
176 Libs []string
177 }{}
178 props.Name = proptools.StringPtr(prebuiltApiModuleName(mctx, "system_modules", "public", apiver))
179 props.Libs = append(props.Libs, prebuiltApiModuleName(mctx, "core-for-system-modules", "public", apiver))
180
181 mctx.CreateModule(SystemModulesFactory, &props)
182}
183
Liz Kammer2d2fd852020-08-12 14:42:30 -0700184func prebuiltSdkSystemModules(mctx android.LoadHookContext, p *prebuiltApis) {
185 for _, apiver := range p.properties.Api_dirs {
Colin Cross17dec172020-05-14 18:05:32 -0700186 jar := android.ExistentPathForSource(mctx,
187 mctx.ModuleDir(), apiver, "public", "core-for-system-modules.jar")
188 if jar.Valid() {
189 createSystemModules(mctx, apiver)
190 }
191 }
192}
193
Liz Kammer2d2fd852020-08-12 14:42:30 -0700194func prebuiltApiFiles(mctx android.LoadHookContext, p *prebuiltApis) {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900195 mydir := mctx.ModuleDir() + "/"
196 // <apiver>/<scope>/api/<module>.txt
Liz Kammer2d2fd852020-08-12 14:42:30 -0700197 files := getPrebuiltFiles(mctx, p, "api/*.txt")
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900198
Sundong Ahna01c2a52018-06-07 21:42:16 +0900199 if len(files) == 0 {
200 mctx.ModuleErrorf("no api file found under %q", mydir)
201 }
202
203 // construct a map to find out the latest api file path
204 // for each (<module>, <scope>) pair.
205 type latestApiInfo struct {
Anton Hansson370fd0b2021-01-22 15:05:04 +0000206 module string
207 scope string
208 version int
209 path string
Sundong Ahna01c2a52018-06-07 21:42:16 +0900210 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900211
Anton Hanssonc79d4122021-02-17 14:21:33 +0000212 // Create modules for all (<module>, <scope, <version>) triplets,
213 // and a "latest" module variant for each (<module>, <scope>) pair
214 apiModuleName := func(module, scope, version string) string {
Anton Hansson20ce41d2021-01-22 15:05:32 +0000215 return module + ".api." + scope + "." + version
216 }
Anton Hansson370fd0b2021-01-22 15:05:04 +0000217 m := make(map[string]latestApiInfo)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900218 for _, f := range files {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900219 localPath := strings.TrimPrefix(f, mydir)
220 module, apiver, scope := parseApiFilePath(mctx, localPath)
Anton Hanssonc79d4122021-02-17 14:21:33 +0000221 createApiModule(mctx, apiModuleName(module, scope, apiver), localPath)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900222
Anton Hansson370fd0b2021-01-22 15:05:04 +0000223 version, err := strconv.Atoi(apiver)
224 if err != nil {
225 mctx.ModuleErrorf("Found finalized API files in non-numeric dir %v", apiver)
226 return
227 }
228
Anton Hansson20ce41d2021-01-22 15:05:32 +0000229 // Track latest version of each module/scope, except for incompatibilities
230 if !strings.HasSuffix(module, "incompatibilities") {
231 key := module + "." + scope
232 info, ok := m[key]
233 if !ok {
234 m[key] = latestApiInfo{module, scope, version, localPath}
235 } else if version > info.version {
236 info.version = version
237 info.path = localPath
238 m[key] = info
239 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900240 }
241 }
Anton Hansson20ce41d2021-01-22 15:05:32 +0000242
Anton Hansson370fd0b2021-01-22 15:05:04 +0000243 // Sort the keys in order to make build.ninja stable
244 for _, k := range android.SortedStringKeys(m) {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900245 info := m[k]
Anton Hanssonc79d4122021-02-17 14:21:33 +0000246 name := apiModuleName(info.module, info.scope, "latest")
247 createApiModule(mctx, name, info.path)
Anton Hansson20ce41d2021-01-22 15:05:32 +0000248 }
249
250 // Create incompatibilities tracking files for all modules, if we have a "next" api.
251 if nextApiDir := String(p.properties.Next_api_dir); nextApiDir != "" {
252 files := getPrebuiltFilesInSubdir(mctx, nextApiDir, "api/*incompatibilities.txt")
253 incompatibilities := make(map[string]bool)
254 for _, f := range files {
255 localPath := strings.TrimPrefix(f, mydir)
256 module, _, scope := parseApiFilePath(mctx, localPath)
257
258 // Figure out which module is referenced by this file. Special case for "android".
259 referencedModule := strings.TrimSuffix(module, "incompatibilities")
260 referencedModule = strings.TrimSuffix(referencedModule, "-")
261 if referencedModule == "" {
262 referencedModule = "android"
263 }
264
Anton Hanssonc79d4122021-02-17 14:21:33 +0000265 createApiModule(mctx, apiModuleName(referencedModule+"-incompatibilities", scope, "latest"), localPath)
Anton Hansson20ce41d2021-01-22 15:05:32 +0000266
267 incompatibilities[referencedModule+"."+scope] = true
268 }
269 // Create empty incompatibilities files for remaining modules
270 for _, k := range android.SortedStringKeys(m) {
271 if _, ok := incompatibilities[k]; !ok {
Anton Hanssonc79d4122021-02-17 14:21:33 +0000272 createEmptyFile(mctx, apiModuleName(m[k].module+"-incompatibilities", m[k].scope, "latest"))
Anton Hansson20ce41d2021-01-22 15:05:32 +0000273 }
274 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900275 }
276}
277
Paul Duffind4c03562020-04-09 17:15:44 +0100278func createPrebuiltApiModules(mctx android.LoadHookContext) {
Liz Kammer2d2fd852020-08-12 14:42:30 -0700279 if p, ok := mctx.Module().(*prebuiltApis); ok {
280 prebuiltApiFiles(mctx, p)
281 prebuiltSdkStubs(mctx, p)
282 prebuiltSdkSystemModules(mctx, p)
Jiyong Park58c518b2018-05-12 22:29:12 +0900283 }
284}
285
Anton Hanssonc79d4122021-02-17 14:21:33 +0000286// prebuilt_apis is a meta-module that generates modules for all API txt files
287// found under the directory where the Android.bp is located.
Jaewoong Jung5fb5b2a2019-03-21 10:48:25 -0700288// Specifically, an API file located at ./<ver>/<scope>/api/<module>.txt
Anton Hanssonc79d4122021-02-17 14:21:33 +0000289// generates a module named <module>-api.<scope>.<ver>.
Jaewoong Jung5fb5b2a2019-03-21 10:48:25 -0700290//
291// It also creates <module>-api.<scope>.latest for the latest <ver>.
Paul Duffind4c03562020-04-09 17:15:44 +0100292//
293// Similarly, it generates a java_import for all API .jar files found under the
294// directory where the Android.bp is located. Specifically, an API file located
295// at ./<ver>/<scope>/api/<module>.jar generates a java_import module named
Colin Cross17dec172020-05-14 18:05:32 -0700296// <prebuilt-api-module>_<scope>_<ver>_<module>, and for SDK versions >= 30
297// a java_system_modules module named
298// <prebuilt-api-module>_public_<ver>_system_modules
Inseob Kimc0907f12019-02-08 21:00:45 +0900299func PrebuiltApisFactory() android.Module {
Jiyong Park58c518b2018-05-12 22:29:12 +0900300 module := &prebuiltApis{}
Sundong Ahn27eecb92018-06-21 13:47:17 +0900301 module.AddProperties(&module.properties)
Jiyong Park58c518b2018-05-12 22:29:12 +0900302 android.InitAndroidModule(module)
Paul Duffind4c03562020-04-09 17:15:44 +0100303 android.AddLoadHook(module, createPrebuiltApiModules)
Jiyong Park58c518b2018-05-12 22:29:12 +0900304 return module
305}