blob: 0740467eb806d9fd6fa1b324855b72f372ef8502 [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 Hansson377318b2022-02-15 12:55:11 +000019 "path"
Anton Hansson370fd0b2021-01-22 15:05:04 +000020 "strconv"
Jiyong Park58c518b2018-05-12 22:29:12 +090021 "strings"
22
23 "github.com/google/blueprint/proptools"
Colin Cross17dec172020-05-14 18:05:32 -070024
25 "android/soong/android"
Anton Hansson20ce41d2021-01-22 15:05:32 +000026 "android/soong/genrule"
Jiyong Park58c518b2018-05-12 22:29:12 +090027)
28
Jiyong Park58c518b2018-05-12 22:29:12 +090029func init() {
Paul Duffina48f7582019-12-19 11:25:19 +000030 RegisterPrebuiltApisBuildComponents(android.InitRegistrationContext)
31}
Jiyong Park58c518b2018-05-12 22:29:12 +090032
Paul Duffina48f7582019-12-19 11:25:19 +000033func RegisterPrebuiltApisBuildComponents(ctx android.RegistrationContext) {
34 ctx.RegisterModuleType("prebuilt_apis", PrebuiltApisFactory)
Jiyong Park58c518b2018-05-12 22:29:12 +090035}
36
Sundong Ahn27eecb92018-06-21 13:47:17 +090037type prebuiltApisProperties struct {
38 // list of api version directories
39 Api_dirs []string
Liz Kammer2d2fd852020-08-12 14:42:30 -070040
Anton Hansson3a3f1692022-02-15 12:55:11 +000041 // Directory containing finalized api txt files for extension versions.
42 // Extension versions higher than the base sdk extension version will
43 // be assumed to be finalized later than all Api_dirs.
44 Extensions_dir *string
45
Anton Hansson20ce41d2021-01-22 15:05:32 +000046 // The next API directory can optionally point to a directory where
47 // files incompatibility-tracking files are stored for the current
48 // "in progress" API. Each module present in one of the api_dirs will have
49 // a <module>-incompatibilities.api.<scope>.latest module created.
50 Next_api_dir *string
51
Liz Kammer2d2fd852020-08-12 14:42:30 -070052 // The sdk_version of java_import modules generated based on jar files.
53 // Defaults to "current"
54 Imports_sdk_version *string
Liz Kammer4e7f2602020-09-02 08:37:49 -070055
56 // If set to true, compile dex for java_import modules. Defaults to false.
57 Imports_compile_dex *bool
Sundong Ahn27eecb92018-06-21 13:47:17 +090058}
59
Jiyong Park58c518b2018-05-12 22:29:12 +090060type prebuiltApis struct {
61 android.ModuleBase
Sundong Ahn27eecb92018-06-21 13:47:17 +090062 properties prebuiltApisProperties
Jiyong Park58c518b2018-05-12 22:29:12 +090063}
64
Jiyong Park58c518b2018-05-12 22:29:12 +090065func (module *prebuiltApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
66 // no need to implement
67}
68
Anton Hansson377318b2022-02-15 12:55:11 +000069// parsePrebuiltPath parses the relevant variables out of a variety of paths, e.g.
70// <version>/<scope>/<module>.jar
71// <version>/<scope>/api/<module>.txt
72// extensions/<version>/<scope>/<module>.jar
73// extensions/<version>/<scope>/api/<module>.txt
74func parsePrebuiltPath(ctx android.LoadHookContext, p string) (module string, version string, scope string) {
75 elements := strings.Split(p, "/")
Sundong Ahna01c2a52018-06-07 21:42:16 +090076
Anton Hansson377318b2022-02-15 12:55:11 +000077 scopeIdx := len(elements) - 2
78 if elements[scopeIdx] == "api" {
79 scopeIdx--
80 }
81 scope = elements[scopeIdx]
82 if scope != "core" && scope != "public" && scope != "system" && scope != "test" && scope != "module-lib" && scope != "system-server" {
83 ctx.ModuleErrorf("invalid scope %q found in path: %q", scope, p)
Jiyong Park58c518b2018-05-12 22:29:12 +090084 return
85 }
Anton Hansson377318b2022-02-15 12:55:11 +000086 version = elements[scopeIdx-1]
Jiyong Park58c518b2018-05-12 22:29:12 +090087
Anton Hansson377318b2022-02-15 12:55:11 +000088 module = strings.TrimSuffix(path.Base(p), path.Ext(p))
Jiyong Park58c518b2018-05-12 22:29:12 +090089 return
90}
91
Anton Hansson377318b2022-02-15 12:55:11 +000092// parseFinalizedPrebuiltPath is like parsePrebuiltPath, but verifies the version is numeric (a finalized version).
93func parseFinalizedPrebuiltPath(ctx android.LoadHookContext, p string) (module string, version int, scope string) {
94 module, v, scope := parsePrebuiltPath(ctx, p)
95 version, err := strconv.Atoi(v)
96 if err != nil {
97 ctx.ModuleErrorf("Found finalized API files in non-numeric dir '%v'", v)
98 return
99 }
100 return
Colin Cross17dec172020-05-14 18:05:32 -0700101}
102
Anton Hansson377318b2022-02-15 12:55:11 +0000103func prebuiltApiModuleName(mctx android.LoadHookContext, module, scope, version string) string {
104 return fmt.Sprintf("%s_%s_%s_%s", mctx.ModuleName(), scope, version, module)
105}
106
107func createImport(mctx android.LoadHookContext, module, scope, version, path, sdkVersion string, compileDex bool) {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900108 props := struct {
109 Name *string
110 Jars []string
111 Sdk_version *string
112 Installable *bool
Liz Kammer4e7f2602020-09-02 08:37:49 -0700113 Compile_dex *bool
Sundong Ahna01c2a52018-06-07 21:42:16 +0900114 }{}
Anton Hansson377318b2022-02-15 12:55:11 +0000115 props.Name = proptools.StringPtr(prebuiltApiModuleName(mctx, module, scope, version))
Sundong Ahna01c2a52018-06-07 21:42:16 +0900116 props.Jars = append(props.Jars, path)
Liz Kammer4e7f2602020-09-02 08:37:49 -0700117 props.Sdk_version = proptools.StringPtr(sdkVersion)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900118 props.Installable = proptools.BoolPtr(false)
Liz Kammer4e7f2602020-09-02 08:37:49 -0700119 props.Compile_dex = proptools.BoolPtr(compileDex)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900120
Colin Cross84dfc3d2019-09-25 11:33:01 -0700121 mctx.CreateModule(ImportFactory, &props)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900122}
123
Anton Hanssonc79d4122021-02-17 14:21:33 +0000124func createApiModule(mctx android.LoadHookContext, name string, path string) {
125 genruleProps := struct {
Jiyong Park58c518b2018-05-12 22:29:12 +0900126 Name *string
127 Srcs []string
Anton Hanssonc79d4122021-02-17 14:21:33 +0000128 Out []string
129 Cmd *string
Jiyong Park58c518b2018-05-12 22:29:12 +0900130 }{}
Anton Hanssonc79d4122021-02-17 14:21:33 +0000131 genruleProps.Name = proptools.StringPtr(name)
132 genruleProps.Srcs = []string{path}
133 genruleProps.Out = []string{name}
134 genruleProps.Cmd = proptools.StringPtr("cp $(in) $(out)")
135 mctx.CreateModule(genrule.GenRuleFactory, &genruleProps)
Jiyong Park58c518b2018-05-12 22:29:12 +0900136}
137
Gurpreet Singhdaa314a2023-04-21 16:30:03 +0000138func createLatestApiModuleExtensionVersionFile(mctx android.LoadHookContext, name string, version string) {
139 genruleProps := struct {
140 Name *string
141 Srcs []string
142 Out []string
143 Cmd *string
144 }{}
145 genruleProps.Name = proptools.StringPtr(name)
146 genruleProps.Out = []string{name}
147 genruleProps.Cmd = proptools.StringPtr("echo " + version + " > $(out)")
148 mctx.CreateModule(genrule.GenRuleFactory, &genruleProps)
149}
150
Anton Hansson20ce41d2021-01-22 15:05:32 +0000151func createEmptyFile(mctx android.LoadHookContext, name string) {
152 props := struct {
153 Name *string
154 Cmd *string
155 Out []string
156 }{}
157 props.Name = proptools.StringPtr(name)
158 props.Out = []string{name}
159 props.Cmd = proptools.StringPtr("touch $(genDir)/" + name)
160 mctx.CreateModule(genrule.GenRuleFactory, &props)
161}
162
Anton Hansson377318b2022-02-15 12:55:11 +0000163// globApiDirs collects all the files in all api_dirs and all scopes that match the given glob, e.g. '*.jar' or 'api/*.txt'.
164// <api-dir>/<scope>/<glob> for all api-dir and scope.
165func globApiDirs(mctx android.LoadHookContext, p *prebuiltApis, api_dir_glob string) []string {
Sundong Ahn27eecb92018-06-21 13:47:17 +0900166 var files []string
Liz Kammer2d2fd852020-08-12 14:42:30 -0700167 for _, apiver := range p.properties.Api_dirs {
Anton Hansson377318b2022-02-15 12:55:11 +0000168 files = append(files, globScopeDir(mctx, apiver, api_dir_glob)...)
Anton Hansson20ce41d2021-01-22 15:05:32 +0000169 }
170 return files
171}
172
Anton Hansson3a3f1692022-02-15 12:55:11 +0000173// globExtensionDirs collects all the files under the extension dir (for all versions and scopes) that match the given glob
174// <extension-dir>/<version>/<scope>/<glob> for all version and scope.
175func globExtensionDirs(mctx android.LoadHookContext, p *prebuiltApis, extension_dir_glob string) []string {
176 // <extensions-dir>/<num>/<extension-dir-glob>
177 return globScopeDir(mctx, *p.properties.Extensions_dir+"/*", extension_dir_glob)
178}
179
Anton Hansson377318b2022-02-15 12:55:11 +0000180// globScopeDir collects all the files in the given subdir across all scopes that match the given glob, e.g. '*.jar' or 'api/*.txt'.
181// <subdir>/<scope>/<glob> for all scope.
182func globScopeDir(mctx android.LoadHookContext, subdir string, subdir_glob string) []string {
Anton Hansson20ce41d2021-01-22 15:05:32 +0000183 var files []string
184 dir := mctx.ModuleDir() + "/" + subdir
185 for _, scope := range []string{"public", "system", "test", "core", "module-lib", "system-server"} {
Anton Hansson377318b2022-02-15 12:55:11 +0000186 glob := fmt.Sprintf("%s/%s/%s", dir, scope, subdir_glob)
Anton Hansson20ce41d2021-01-22 15:05:32 +0000187 vfiles, err := mctx.GlobWithDeps(glob, nil)
188 if err != nil {
Anton Hansson377318b2022-02-15 12:55:11 +0000189 mctx.ModuleErrorf("failed to glob %s files under %q: %s", subdir_glob, dir+"/"+scope, err)
Sundong Ahn27eecb92018-06-21 13:47:17 +0900190 }
Anton Hansson20ce41d2021-01-22 15:05:32 +0000191 files = append(files, vfiles...)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900192 }
Anton Hansson377318b2022-02-15 12:55:11 +0000193 for i, f := range files {
194 files[i] = strings.TrimPrefix(f, mctx.ModuleDir()+"/")
195 }
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900196 return files
197}
198
Liz Kammer2d2fd852020-08-12 14:42:30 -0700199func prebuiltSdkStubs(mctx android.LoadHookContext, p *prebuiltApis) {
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900200 // <apiver>/<scope>/<module>.jar
Anton Hansson377318b2022-02-15 12:55:11 +0000201 files := globApiDirs(mctx, p, "*.jar")
Liz Kammer2d2fd852020-08-12 14:42:30 -0700202
Liz Kammer4e7f2602020-09-02 08:37:49 -0700203 sdkVersion := proptools.StringDefault(p.properties.Imports_sdk_version, "current")
204 compileDex := proptools.BoolDefault(p.properties.Imports_compile_dex, false)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900205
206 for _, f := range files {
207 // create a Import module for each jar file
Anton Hansson377318b2022-02-15 12:55:11 +0000208 module, version, scope := parsePrebuiltPath(mctx, f)
209 createImport(mctx, module, scope, version, f, sdkVersion, compileDex)
Paul Duffinb077bcc2021-10-28 13:27:37 +0100210
211 if module == "core-for-system-modules" {
Anton Hansson377318b2022-02-15 12:55:11 +0000212 createSystemModules(mctx, version, scope)
Paul Duffinb077bcc2021-10-28 13:27:37 +0100213 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900214 }
215}
216
Anton Hansson377318b2022-02-15 12:55:11 +0000217func createSystemModules(mctx android.LoadHookContext, version, scope string) {
Colin Cross17dec172020-05-14 18:05:32 -0700218 props := struct {
219 Name *string
220 Libs []string
221 }{}
Anton Hansson377318b2022-02-15 12:55:11 +0000222 props.Name = proptools.StringPtr(prebuiltApiModuleName(mctx, "system_modules", scope, version))
223 props.Libs = append(props.Libs, prebuiltApiModuleName(mctx, "core-for-system-modules", scope, version))
Colin Cross17dec172020-05-14 18:05:32 -0700224
Paul Duffind6c2a652021-03-11 07:56:22 +0000225 mctx.CreateModule(systemModulesImportFactory, &props)
Colin Cross17dec172020-05-14 18:05:32 -0700226}
227
Paul Duffin958806b2022-05-16 13:10:47 +0000228func PrebuiltApiModuleName(module, scope, version string) string {
229 return module + ".api." + scope + "." + version
230}
231
Liz Kammer2d2fd852020-08-12 14:42:30 -0700232func prebuiltApiFiles(mctx android.LoadHookContext, p *prebuiltApis) {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900233 // <apiver>/<scope>/api/<module>.txt
Anton Hansson377318b2022-02-15 12:55:11 +0000234 apiLevelFiles := globApiDirs(mctx, p, "api/*.txt")
235 if len(apiLevelFiles) == 0 {
236 mctx.ModuleErrorf("no api file found under %q", mctx.ModuleDir())
Sundong Ahna01c2a52018-06-07 21:42:16 +0900237 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900238
Anton Hanssonc79d4122021-02-17 14:21:33 +0000239 // Create modules for all (<module>, <scope, <version>) triplets,
Anton Hansson377318b2022-02-15 12:55:11 +0000240 for _, f := range apiLevelFiles {
241 module, version, scope := parseFinalizedPrebuiltPath(mctx, f)
Paul Duffin958806b2022-05-16 13:10:47 +0000242 createApiModule(mctx, PrebuiltApiModuleName(module, scope, strconv.Itoa(version)), f)
Anton Hansson377318b2022-02-15 12:55:11 +0000243 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900244
Anton Hansson377318b2022-02-15 12:55:11 +0000245 // Figure out the latest version of each module/scope
246 type latestApiInfo struct {
247 module, scope, path string
248 version int
Gurpreet Singhdaa314a2023-04-21 16:30:03 +0000249 isExtensionApiFile bool
Anton Hansson377318b2022-02-15 12:55:11 +0000250 }
251
Gurpreet Singhdaa314a2023-04-21 16:30:03 +0000252 getLatest := func(files []string, isExtensionApiFile bool) map[string]latestApiInfo {
Anton Hansson3a3f1692022-02-15 12:55:11 +0000253 m := make(map[string]latestApiInfo)
254 for _, f := range files {
255 module, version, scope := parseFinalizedPrebuiltPath(mctx, f)
256 if strings.HasSuffix(module, "incompatibilities") {
257 continue
258 }
259 key := module + "." + scope
260 info, exists := m[key]
261 if !exists || version > info.version {
Gurpreet Singhdaa314a2023-04-21 16:30:03 +0000262 m[key] = latestApiInfo{module, scope, f, version, isExtensionApiFile}
Anton Hansson3a3f1692022-02-15 12:55:11 +0000263 }
Anton Hansson370fd0b2021-01-22 15:05:04 +0000264 }
Anton Hansson3a3f1692022-02-15 12:55:11 +0000265 return m
266 }
Anton Hansson370fd0b2021-01-22 15:05:04 +0000267
Gurpreet Singhdaa314a2023-04-21 16:30:03 +0000268 latest := getLatest(apiLevelFiles, false)
Anton Hansson3a3f1692022-02-15 12:55:11 +0000269 if p.properties.Extensions_dir != nil {
270 extensionApiFiles := globExtensionDirs(mctx, p, "api/*.txt")
Gurpreet Singhdaa314a2023-04-21 16:30:03 +0000271 for k, v := range getLatest(extensionApiFiles, true) {
Anton Hansson7212dbe2022-09-20 17:08:49 +0000272 if _, exists := latest[k]; !exists {
273 mctx.ModuleErrorf("Module %v finalized for extension %d but never during an API level; likely error", v.module, v.version)
Anton Hansson3a3f1692022-02-15 12:55:11 +0000274 }
Anton Hansson7212dbe2022-09-20 17:08:49 +0000275 // The extension version is always at least as new as the last sdk int version (potentially identical)
276 latest[k] = v
Sundong Ahna01c2a52018-06-07 21:42:16 +0900277 }
278 }
Anton Hansson20ce41d2021-01-22 15:05:32 +0000279
Anton Hansson370fd0b2021-01-22 15:05:04 +0000280 // Sort the keys in order to make build.ninja stable
Cole Faust18994c72023-02-28 16:02:16 -0800281 for _, k := range android.SortedKeys(latest) {
Anton Hansson377318b2022-02-15 12:55:11 +0000282 info := latest[k]
Paul Duffin958806b2022-05-16 13:10:47 +0000283 name := PrebuiltApiModuleName(info.module, info.scope, "latest")
Gurpreet Singhdaa314a2023-04-21 16:30:03 +0000284 latestExtensionVersionModuleName := PrebuiltApiModuleName(info.module, info.scope, "latest.extension_version")
285 if info.isExtensionApiFile {
286 createLatestApiModuleExtensionVersionFile(mctx, latestExtensionVersionModuleName, strconv.Itoa(info.version))
287 } else {
288 createLatestApiModuleExtensionVersionFile(mctx, latestExtensionVersionModuleName, "-1")
289 }
Anton Hanssonc79d4122021-02-17 14:21:33 +0000290 createApiModule(mctx, name, info.path)
Anton Hansson20ce41d2021-01-22 15:05:32 +0000291 }
292
293 // Create incompatibilities tracking files for all modules, if we have a "next" api.
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800294 incompatibilities := make(map[string]bool)
Anton Hansson20ce41d2021-01-22 15:05:32 +0000295 if nextApiDir := String(p.properties.Next_api_dir); nextApiDir != "" {
Anton Hansson377318b2022-02-15 12:55:11 +0000296 files := globScopeDir(mctx, nextApiDir, "api/*incompatibilities.txt")
Anton Hansson20ce41d2021-01-22 15:05:32 +0000297 for _, f := range files {
Anton Hansson377318b2022-02-15 12:55:11 +0000298 filename, _, scope := parsePrebuiltPath(mctx, f)
Anton Hanssonfa5e6b52021-04-13 19:09:48 +0100299 referencedModule := strings.TrimSuffix(filename, "-incompatibilities")
Anton Hansson20ce41d2021-01-22 15:05:32 +0000300
Paul Duffin958806b2022-05-16 13:10:47 +0000301 createApiModule(mctx, PrebuiltApiModuleName(referencedModule+"-incompatibilities", scope, "latest"), f)
Anton Hansson20ce41d2021-01-22 15:05:32 +0000302
303 incompatibilities[referencedModule+"."+scope] = true
304 }
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800305 }
306 // Create empty incompatibilities files for remaining modules
Cole Faust18994c72023-02-28 16:02:16 -0800307 for _, k := range android.SortedKeys(latest) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800308 if _, ok := incompatibilities[k]; !ok {
Paul Duffin958806b2022-05-16 13:10:47 +0000309 createEmptyFile(mctx, PrebuiltApiModuleName(latest[k].module+"-incompatibilities", latest[k].scope, "latest"))
Anton Hansson20ce41d2021-01-22 15:05:32 +0000310 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900311 }
312}
313
Paul Duffind4c03562020-04-09 17:15:44 +0100314func createPrebuiltApiModules(mctx android.LoadHookContext) {
Liz Kammer2d2fd852020-08-12 14:42:30 -0700315 if p, ok := mctx.Module().(*prebuiltApis); ok {
316 prebuiltApiFiles(mctx, p)
317 prebuiltSdkStubs(mctx, p)
Jiyong Park58c518b2018-05-12 22:29:12 +0900318 }
319}
320
Anton Hanssonc79d4122021-02-17 14:21:33 +0000321// prebuilt_apis is a meta-module that generates modules for all API txt files
322// found under the directory where the Android.bp is located.
Jaewoong Jung5fb5b2a2019-03-21 10:48:25 -0700323// Specifically, an API file located at ./<ver>/<scope>/api/<module>.txt
Anton Hanssonc79d4122021-02-17 14:21:33 +0000324// generates a module named <module>-api.<scope>.<ver>.
Jaewoong Jung5fb5b2a2019-03-21 10:48:25 -0700325//
326// It also creates <module>-api.<scope>.latest for the latest <ver>.
Paul Duffind4c03562020-04-09 17:15:44 +0100327//
328// Similarly, it generates a java_import for all API .jar files found under the
329// directory where the Android.bp is located. Specifically, an API file located
330// at ./<ver>/<scope>/api/<module>.jar generates a java_import module named
Colin Cross17dec172020-05-14 18:05:32 -0700331// <prebuilt-api-module>_<scope>_<ver>_<module>, and for SDK versions >= 30
332// a java_system_modules module named
333// <prebuilt-api-module>_public_<ver>_system_modules
Inseob Kimc0907f12019-02-08 21:00:45 +0900334func PrebuiltApisFactory() android.Module {
Jiyong Park58c518b2018-05-12 22:29:12 +0900335 module := &prebuiltApis{}
Sundong Ahn27eecb92018-06-21 13:47:17 +0900336 module.AddProperties(&module.properties)
Jiyong Park58c518b2018-05-12 22:29:12 +0900337 android.InitAndroidModule(module)
Paul Duffind4c03562020-04-09 17:15:44 +0100338 android.AddLoadHook(module, createPrebuiltApiModules)
Jiyong Park58c518b2018-05-12 22:29:12 +0900339 return module
340}