blob: 31f149ea9939df4d102ed79c4816af195e05b04f [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
Todd Lee2ec7e1c2023-08-25 18:02:13 +000072// *Note when using incremental platform API, <version> may be of the form MM.m where MM is the
73// API level and m is an incremental release, otherwise <version> is a single integer corresponding to the API level only.
Anton Hansson377318b2022-02-15 12:55:11 +000074// extensions/<version>/<scope>/<module>.jar
75// extensions/<version>/<scope>/api/<module>.txt
76func parsePrebuiltPath(ctx android.LoadHookContext, p string) (module string, version string, scope string) {
77 elements := strings.Split(p, "/")
Sundong Ahna01c2a52018-06-07 21:42:16 +090078
Anton Hansson377318b2022-02-15 12:55:11 +000079 scopeIdx := len(elements) - 2
80 if elements[scopeIdx] == "api" {
81 scopeIdx--
82 }
83 scope = elements[scopeIdx]
84 if scope != "core" && scope != "public" && scope != "system" && scope != "test" && scope != "module-lib" && scope != "system-server" {
85 ctx.ModuleErrorf("invalid scope %q found in path: %q", scope, p)
Jiyong Park58c518b2018-05-12 22:29:12 +090086 return
87 }
Anton Hansson377318b2022-02-15 12:55:11 +000088 version = elements[scopeIdx-1]
Jiyong Park58c518b2018-05-12 22:29:12 +090089
Anton Hansson377318b2022-02-15 12:55:11 +000090 module = strings.TrimSuffix(path.Base(p), path.Ext(p))
Jiyong Park58c518b2018-05-12 22:29:12 +090091 return
92}
93
Anton Hansson377318b2022-02-15 12:55:11 +000094// parseFinalizedPrebuiltPath is like parsePrebuiltPath, but verifies the version is numeric (a finalized version).
Mårten Kongstad7ceaad12025-03-03 09:10:01 +010095func parseFinalizedPrebuiltPath(ctx android.LoadHookContext, p string) (module string, version int, release int, scope string) {
Anton Hansson377318b2022-02-15 12:55:11 +000096 module, v, scope := parsePrebuiltPath(ctx, p)
Mårten Kongstad7ceaad12025-03-03 09:10:01 +010097
98 // assume a major.minor version code
99 parts := strings.Split(v, ".")
100 if len(parts) == 2 {
Todd Lee2ec7e1c2023-08-25 18:02:13 +0000101 sdk, sdk_err := strconv.Atoi(parts[0])
102 qpr, qpr_err := strconv.Atoi(parts[1])
103 if sdk_err != nil || qpr_err != nil {
Mårten Kongstad7ceaad12025-03-03 09:10:01 +0100104 ctx.ModuleErrorf("Unable to read major.minor version for prebuilt api '%v'", v)
Todd Lee2ec7e1c2023-08-25 18:02:13 +0000105 return
106 }
107 version = sdk
108 release = qpr
109 return
110 }
Mårten Kongstad7ceaad12025-03-03 09:10:01 +0100111
112 // assume a legacy integer only api level
Todd Lee2ec7e1c2023-08-25 18:02:13 +0000113 release = 0
Anton Hansson377318b2022-02-15 12:55:11 +0000114 version, err := strconv.Atoi(v)
115 if err != nil {
Mårten Kongstad7ceaad12025-03-03 09:10:01 +0100116 ctx.ModuleErrorf("Unable to read API level for prebuilt api '%v'", v)
Anton Hansson377318b2022-02-15 12:55:11 +0000117 return
118 }
119 return
Colin Cross17dec172020-05-14 18:05:32 -0700120}
121
Jihoon Kang28c96572024-09-11 23:44:44 +0000122func prebuiltApiModuleName(moduleName, module, scope, version string) string {
123 return fmt.Sprintf("%s_%s_%s_%s", moduleName, scope, version, module)
Anton Hansson377318b2022-02-15 12:55:11 +0000124}
Anton Hansson377318b2022-02-15 12:55:11 +0000125func createImport(mctx android.LoadHookContext, module, scope, version, path, sdkVersion string, compileDex bool) {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900126 props := struct {
Colin Cross8ff10582023-12-07 13:10:56 -0800127 Name *string
128 Jars []string
129 Sdk_version *string
130 Installable *bool
131 Compile_dex *bool
Romain Jobredeaux8242b432023-05-04 10:16:26 -0400132 }{
Jihoon Kang28c96572024-09-11 23:44:44 +0000133 Name: proptools.StringPtr(prebuiltApiModuleName(mctx.ModuleName(), module, scope, version)),
Romain Jobredeaux8242b432023-05-04 10:16:26 -0400134 Jars: []string{path},
135 Sdk_version: proptools.StringPtr(sdkVersion),
136 Installable: proptools.BoolPtr(false),
137 Compile_dex: proptools.BoolPtr(compileDex),
138 }
Colin Cross84dfc3d2019-09-25 11:33:01 -0700139 mctx.CreateModule(ImportFactory, &props)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900140}
141
Anton Hanssonc79d4122021-02-17 14:21:33 +0000142func createApiModule(mctx android.LoadHookContext, name string, path string) {
143 genruleProps := struct {
Jiyong Park58c518b2018-05-12 22:29:12 +0900144 Name *string
145 Srcs []string
Anton Hanssonc79d4122021-02-17 14:21:33 +0000146 Out []string
147 Cmd *string
Jiyong Park58c518b2018-05-12 22:29:12 +0900148 }{}
Anton Hanssonc79d4122021-02-17 14:21:33 +0000149 genruleProps.Name = proptools.StringPtr(name)
150 genruleProps.Srcs = []string{path}
151 genruleProps.Out = []string{name}
152 genruleProps.Cmd = proptools.StringPtr("cp $(in) $(out)")
153 mctx.CreateModule(genrule.GenRuleFactory, &genruleProps)
Jiyong Park58c518b2018-05-12 22:29:12 +0900154}
155
Jihoon Kang748a24d2024-03-20 21:29:39 +0000156func createCombinedApiFilegroupModule(mctx android.LoadHookContext, name string, srcs []string) {
157 filegroupProps := struct {
158 Name *string
159 Srcs []string
160 }{}
161 filegroupProps.Name = proptools.StringPtr(name)
162
163 var transformedSrcs []string
164 for _, src := range srcs {
165 transformedSrcs = append(transformedSrcs, ":"+src)
166 }
167 filegroupProps.Srcs = transformedSrcs
168 mctx.CreateModule(android.FileGroupFactory, &filegroupProps)
169}
170
Gurpreet Singhdaa314a2023-04-21 16:30:03 +0000171func createLatestApiModuleExtensionVersionFile(mctx android.LoadHookContext, name string, version string) {
172 genruleProps := struct {
173 Name *string
174 Srcs []string
175 Out []string
176 Cmd *string
177 }{}
178 genruleProps.Name = proptools.StringPtr(name)
179 genruleProps.Out = []string{name}
180 genruleProps.Cmd = proptools.StringPtr("echo " + version + " > $(out)")
181 mctx.CreateModule(genrule.GenRuleFactory, &genruleProps)
182}
183
Anton Hansson20ce41d2021-01-22 15:05:32 +0000184func createEmptyFile(mctx android.LoadHookContext, name string) {
185 props := struct {
186 Name *string
187 Cmd *string
188 Out []string
189 }{}
190 props.Name = proptools.StringPtr(name)
191 props.Out = []string{name}
192 props.Cmd = proptools.StringPtr("touch $(genDir)/" + name)
193 mctx.CreateModule(genrule.GenRuleFactory, &props)
194}
195
Anton Hansson377318b2022-02-15 12:55:11 +0000196// globApiDirs collects all the files in all api_dirs and all scopes that match the given glob, e.g. '*.jar' or 'api/*.txt'.
197// <api-dir>/<scope>/<glob> for all api-dir and scope.
198func globApiDirs(mctx android.LoadHookContext, p *prebuiltApis, api_dir_glob string) []string {
Sundong Ahn27eecb92018-06-21 13:47:17 +0900199 var files []string
Liz Kammer2d2fd852020-08-12 14:42:30 -0700200 for _, apiver := range p.properties.Api_dirs {
Anton Hansson377318b2022-02-15 12:55:11 +0000201 files = append(files, globScopeDir(mctx, apiver, api_dir_glob)...)
Anton Hansson20ce41d2021-01-22 15:05:32 +0000202 }
203 return files
204}
205
Anton Hansson3a3f1692022-02-15 12:55:11 +0000206// globExtensionDirs collects all the files under the extension dir (for all versions and scopes) that match the given glob
207// <extension-dir>/<version>/<scope>/<glob> for all version and scope.
208func globExtensionDirs(mctx android.LoadHookContext, p *prebuiltApis, extension_dir_glob string) []string {
209 // <extensions-dir>/<num>/<extension-dir-glob>
210 return globScopeDir(mctx, *p.properties.Extensions_dir+"/*", extension_dir_glob)
211}
212
Anton Hansson377318b2022-02-15 12:55:11 +0000213// globScopeDir collects all the files in the given subdir across all scopes that match the given glob, e.g. '*.jar' or 'api/*.txt'.
214// <subdir>/<scope>/<glob> for all scope.
215func globScopeDir(mctx android.LoadHookContext, subdir string, subdir_glob string) []string {
Anton Hansson20ce41d2021-01-22 15:05:32 +0000216 var files []string
217 dir := mctx.ModuleDir() + "/" + subdir
218 for _, scope := range []string{"public", "system", "test", "core", "module-lib", "system-server"} {
Anton Hansson377318b2022-02-15 12:55:11 +0000219 glob := fmt.Sprintf("%s/%s/%s", dir, scope, subdir_glob)
Anton Hansson20ce41d2021-01-22 15:05:32 +0000220 vfiles, err := mctx.GlobWithDeps(glob, nil)
221 if err != nil {
Anton Hansson377318b2022-02-15 12:55:11 +0000222 mctx.ModuleErrorf("failed to glob %s files under %q: %s", subdir_glob, dir+"/"+scope, err)
Sundong Ahn27eecb92018-06-21 13:47:17 +0900223 }
Anton Hansson20ce41d2021-01-22 15:05:32 +0000224 files = append(files, vfiles...)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900225 }
Anton Hansson377318b2022-02-15 12:55:11 +0000226 for i, f := range files {
227 files[i] = strings.TrimPrefix(f, mctx.ModuleDir()+"/")
228 }
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900229 return files
230}
231
Liz Kammer2d2fd852020-08-12 14:42:30 -0700232func prebuiltSdkStubs(mctx android.LoadHookContext, p *prebuiltApis) {
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900233 // <apiver>/<scope>/<module>.jar
Anton Hansson377318b2022-02-15 12:55:11 +0000234 files := globApiDirs(mctx, p, "*.jar")
Liz Kammer2d2fd852020-08-12 14:42:30 -0700235
Liz Kammer4e7f2602020-09-02 08:37:49 -0700236 sdkVersion := proptools.StringDefault(p.properties.Imports_sdk_version, "current")
237 compileDex := proptools.BoolDefault(p.properties.Imports_compile_dex, false)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900238
239 for _, f := range files {
240 // create a Import module for each jar file
Anton Hansson377318b2022-02-15 12:55:11 +0000241 module, version, scope := parsePrebuiltPath(mctx, f)
242 createImport(mctx, module, scope, version, f, sdkVersion, compileDex)
Paul Duffinb077bcc2021-10-28 13:27:37 +0100243
244 if module == "core-for-system-modules" {
Anton Hansson377318b2022-02-15 12:55:11 +0000245 createSystemModules(mctx, version, scope)
Paul Duffinb077bcc2021-10-28 13:27:37 +0100246 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900247 }
248}
249
Anton Hansson377318b2022-02-15 12:55:11 +0000250func createSystemModules(mctx android.LoadHookContext, version, scope string) {
Colin Cross17dec172020-05-14 18:05:32 -0700251 props := struct {
252 Name *string
253 Libs []string
254 }{}
Jihoon Kang28c96572024-09-11 23:44:44 +0000255 props.Name = proptools.StringPtr(prebuiltApiModuleName(mctx.ModuleName(), "system_modules", scope, version))
256 props.Libs = append(props.Libs, prebuiltApiModuleName(mctx.ModuleName(), "core-for-system-modules", scope, version))
Colin Cross17dec172020-05-14 18:05:32 -0700257
Paul Duffind6c2a652021-03-11 07:56:22 +0000258 mctx.CreateModule(systemModulesImportFactory, &props)
Colin Cross17dec172020-05-14 18:05:32 -0700259}
260
Paul Duffin958806b2022-05-16 13:10:47 +0000261func PrebuiltApiModuleName(module, scope, version string) string {
262 return module + ".api." + scope + "." + version
263}
264
Jihoon Kang748a24d2024-03-20 21:29:39 +0000265func PrebuiltApiCombinedModuleName(module, scope, version string) string {
266 return module + ".api.combined." + scope + "." + version
267}
268
Liz Kammer2d2fd852020-08-12 14:42:30 -0700269func prebuiltApiFiles(mctx android.LoadHookContext, p *prebuiltApis) {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900270 // <apiver>/<scope>/api/<module>.txt
Anton Hansson377318b2022-02-15 12:55:11 +0000271 apiLevelFiles := globApiDirs(mctx, p, "api/*.txt")
272 if len(apiLevelFiles) == 0 {
273 mctx.ModuleErrorf("no api file found under %q", mctx.ModuleDir())
Sundong Ahna01c2a52018-06-07 21:42:16 +0900274 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900275
Anton Hanssonc79d4122021-02-17 14:21:33 +0000276 // Create modules for all (<module>, <scope, <version>) triplets,
Anton Hansson377318b2022-02-15 12:55:11 +0000277 for _, f := range apiLevelFiles {
Mårten Kongstad7ceaad12025-03-03 09:10:01 +0100278 module, version, release, scope := parseFinalizedPrebuiltPath(mctx, f)
279 if release != 0 {
280 majorDotMinorVersion := strconv.Itoa(version) + "." + strconv.Itoa(release)
281 createApiModule(mctx, PrebuiltApiModuleName(module, scope, majorDotMinorVersion), f)
Todd Lee2ec7e1c2023-08-25 18:02:13 +0000282 } else {
283 createApiModule(mctx, PrebuiltApiModuleName(module, scope, strconv.Itoa(version)), f)
284 }
Anton Hansson377318b2022-02-15 12:55:11 +0000285 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900286
Anton Hansson377318b2022-02-15 12:55:11 +0000287 // Figure out the latest version of each module/scope
288 type latestApiInfo struct {
289 module, scope, path string
Todd Lee2ec7e1c2023-08-25 18:02:13 +0000290 version, release int
Gurpreet Singhdaa314a2023-04-21 16:30:03 +0000291 isExtensionApiFile bool
Anton Hansson377318b2022-02-15 12:55:11 +0000292 }
293
Gurpreet Singhdaa314a2023-04-21 16:30:03 +0000294 getLatest := func(files []string, isExtensionApiFile bool) map[string]latestApiInfo {
Anton Hansson3a3f1692022-02-15 12:55:11 +0000295 m := make(map[string]latestApiInfo)
296 for _, f := range files {
Mårten Kongstad7ceaad12025-03-03 09:10:01 +0100297 module, version, release, scope := parseFinalizedPrebuiltPath(mctx, f)
Anton Hansson3a3f1692022-02-15 12:55:11 +0000298 if strings.HasSuffix(module, "incompatibilities") {
299 continue
300 }
301 key := module + "." + scope
302 info, exists := m[key]
Todd Lee2ec7e1c2023-08-25 18:02:13 +0000303 if !exists || version > info.version || (version == info.version && release > info.release) {
304 m[key] = latestApiInfo{module, scope, f, version, release, isExtensionApiFile}
Anton Hansson3a3f1692022-02-15 12:55:11 +0000305 }
Anton Hansson370fd0b2021-01-22 15:05:04 +0000306 }
Anton Hansson3a3f1692022-02-15 12:55:11 +0000307 return m
308 }
Anton Hansson370fd0b2021-01-22 15:05:04 +0000309
Gurpreet Singhdaa314a2023-04-21 16:30:03 +0000310 latest := getLatest(apiLevelFiles, false)
Anton Hansson3a3f1692022-02-15 12:55:11 +0000311 if p.properties.Extensions_dir != nil {
312 extensionApiFiles := globExtensionDirs(mctx, p, "api/*.txt")
Gurpreet Singhdaa314a2023-04-21 16:30:03 +0000313 for k, v := range getLatest(extensionApiFiles, true) {
Anton Hansson7212dbe2022-09-20 17:08:49 +0000314 if _, exists := latest[k]; !exists {
315 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 +0000316 }
Anton Hansson7212dbe2022-09-20 17:08:49 +0000317 // The extension version is always at least as new as the last sdk int version (potentially identical)
318 latest[k] = v
Sundong Ahna01c2a52018-06-07 21:42:16 +0900319 }
320 }
Anton Hansson20ce41d2021-01-22 15:05:32 +0000321
Anton Hansson370fd0b2021-01-22 15:05:04 +0000322 // Sort the keys in order to make build.ninja stable
Jihoon Kang748a24d2024-03-20 21:29:39 +0000323 sortedLatestKeys := android.SortedKeys(latest)
324
325 for _, k := range sortedLatestKeys {
Anton Hansson377318b2022-02-15 12:55:11 +0000326 info := latest[k]
Paul Duffin958806b2022-05-16 13:10:47 +0000327 name := PrebuiltApiModuleName(info.module, info.scope, "latest")
Gurpreet Singhdaa314a2023-04-21 16:30:03 +0000328 latestExtensionVersionModuleName := PrebuiltApiModuleName(info.module, info.scope, "latest.extension_version")
329 if info.isExtensionApiFile {
330 createLatestApiModuleExtensionVersionFile(mctx, latestExtensionVersionModuleName, strconv.Itoa(info.version))
331 } else {
332 createLatestApiModuleExtensionVersionFile(mctx, latestExtensionVersionModuleName, "-1")
333 }
Anton Hanssonc79d4122021-02-17 14:21:33 +0000334 createApiModule(mctx, name, info.path)
Anton Hansson20ce41d2021-01-22 15:05:32 +0000335 }
336
337 // Create incompatibilities tracking files for all modules, if we have a "next" api.
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800338 incompatibilities := make(map[string]bool)
Anton Hansson20ce41d2021-01-22 15:05:32 +0000339 if nextApiDir := String(p.properties.Next_api_dir); nextApiDir != "" {
Anton Hansson377318b2022-02-15 12:55:11 +0000340 files := globScopeDir(mctx, nextApiDir, "api/*incompatibilities.txt")
Anton Hansson20ce41d2021-01-22 15:05:32 +0000341 for _, f := range files {
Anton Hansson377318b2022-02-15 12:55:11 +0000342 filename, _, scope := parsePrebuiltPath(mctx, f)
Anton Hanssonfa5e6b52021-04-13 19:09:48 +0100343 referencedModule := strings.TrimSuffix(filename, "-incompatibilities")
Anton Hansson20ce41d2021-01-22 15:05:32 +0000344
Paul Duffin958806b2022-05-16 13:10:47 +0000345 createApiModule(mctx, PrebuiltApiModuleName(referencedModule+"-incompatibilities", scope, "latest"), f)
Anton Hansson20ce41d2021-01-22 15:05:32 +0000346
347 incompatibilities[referencedModule+"."+scope] = true
348 }
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800349 }
350 // Create empty incompatibilities files for remaining modules
Jihoon Kang748a24d2024-03-20 21:29:39 +0000351 // If the incompatibility module has been created, create a corresponding combined module
352 for _, k := range sortedLatestKeys {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800353 if _, ok := incompatibilities[k]; !ok {
Paul Duffin958806b2022-05-16 13:10:47 +0000354 createEmptyFile(mctx, PrebuiltApiModuleName(latest[k].module+"-incompatibilities", latest[k].scope, "latest"))
Anton Hansson20ce41d2021-01-22 15:05:32 +0000355 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900356 }
Jihoon Kang748a24d2024-03-20 21:29:39 +0000357
358 // Create combined latest api and removed api files modules.
359 // The combined modules list all api files of the api scope and its subset api scopes.
360 for _, k := range sortedLatestKeys {
361 info := latest[k]
362 name := PrebuiltApiCombinedModuleName(info.module, info.scope, "latest")
363
Jihoon Kang5623e542024-01-31 23:27:26 +0000364 // Iterate until the currentApiScope does not extend any other api scopes
365 // i.e. is not a superset of any other api scopes
366 // the relationship between the api scopes is defined in java/sdk_library.go
Jihoon Kang748a24d2024-03-20 21:29:39 +0000367 var srcs []string
368 currentApiScope := scopeByName[info.scope]
Jihoon Kang5623e542024-01-31 23:27:26 +0000369 for currentApiScope != nil {
370 if _, ok := latest[fmt.Sprintf("%s.%s", info.module, currentApiScope.name)]; ok {
371 srcs = append(srcs, PrebuiltApiModuleName(info.module, currentApiScope.name, "latest"))
372 }
373 currentApiScope = currentApiScope.extends
374 }
Jihoon Kang748a24d2024-03-20 21:29:39 +0000375
Jihoon Kang5623e542024-01-31 23:27:26 +0000376 // srcs is currently listed in the order from the widest api scope to the narrowest api scopes
377 // e.g. module lib -> system -> public
378 // In order to pass the files in metalava from the narrowest api scope to the widest api scope,
379 // the list has to be reversed.
380 android.ReverseSliceInPlace(srcs)
Jihoon Kang748a24d2024-03-20 21:29:39 +0000381 createCombinedApiFilegroupModule(mctx, name, srcs)
382 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900383}
384
Paul Duffind4c03562020-04-09 17:15:44 +0100385func createPrebuiltApiModules(mctx android.LoadHookContext) {
Liz Kammer2d2fd852020-08-12 14:42:30 -0700386 if p, ok := mctx.Module().(*prebuiltApis); ok {
387 prebuiltApiFiles(mctx, p)
388 prebuiltSdkStubs(mctx, p)
Jiyong Park58c518b2018-05-12 22:29:12 +0900389 }
390}
391
Anton Hanssonc79d4122021-02-17 14:21:33 +0000392// prebuilt_apis is a meta-module that generates modules for all API txt files
393// found under the directory where the Android.bp is located.
Jaewoong Jung5fb5b2a2019-03-21 10:48:25 -0700394// Specifically, an API file located at ./<ver>/<scope>/api/<module>.txt
Anton Hanssonc79d4122021-02-17 14:21:33 +0000395// generates a module named <module>-api.<scope>.<ver>.
Jaewoong Jung5fb5b2a2019-03-21 10:48:25 -0700396//
397// It also creates <module>-api.<scope>.latest for the latest <ver>.
Paul Duffind4c03562020-04-09 17:15:44 +0100398//
399// Similarly, it generates a java_import for all API .jar files found under the
400// directory where the Android.bp is located. Specifically, an API file located
401// at ./<ver>/<scope>/api/<module>.jar generates a java_import module named
Colin Cross17dec172020-05-14 18:05:32 -0700402// <prebuilt-api-module>_<scope>_<ver>_<module>, and for SDK versions >= 30
403// a java_system_modules module named
404// <prebuilt-api-module>_public_<ver>_system_modules
Inseob Kimc0907f12019-02-08 21:00:45 +0900405func PrebuiltApisFactory() android.Module {
Jiyong Park58c518b2018-05-12 22:29:12 +0900406 module := &prebuiltApis{}
Sundong Ahn27eecb92018-06-21 13:47:17 +0900407 module.AddProperties(&module.properties)
Jiyong Park58c518b2018-05-12 22:29:12 +0900408 android.InitAndroidModule(module)
Paul Duffind4c03562020-04-09 17:15:44 +0100409 android.AddLoadHook(module, createPrebuiltApiModules)
Jiyong Park58c518b2018-05-12 22:29:12 +0900410 return module
411}