blob: 05a5b4920d8bf3a9ca8b2bbbdf014231202cb0c0 [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +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 (
Jihoon Kangee113282024-01-23 00:16:41 +000018 "errors"
Jiyong Parkc678ad32018-04-10 13:07:10 +090019 "fmt"
20 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090021 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010022 "reflect"
Jiyong Park82484c02018-04-23 21:41:26 +090023 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090024 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090025 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090026
Paul Duffind1b3a922020-01-22 11:57:20 +000027 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090028 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010029
30 "android/soong/android"
Ulya Trafimovichdbf31662020-12-17 12:07:54 +000031 "android/soong/dexpreopt"
Jiyong Parkc678ad32018-04-10 13:07:10 +090032)
33
Paul Duffind1b3a922020-01-22 11:57:20 +000034// A tag to associated a dependency with a specific api scope.
35type scopeDependencyTag struct {
36 blueprint.BaseDependencyTag
37 name string
38 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010039
40 // Function for extracting appropriate path information from the dependency.
Colin Crossdcf71b22021-02-01 13:59:03 -080041 depInfoExtractor func(paths *scopePaths, ctx android.ModuleContext, dep android.Module) error
Paul Duffinc8782502020-04-29 20:45:27 +010042}
43
44// Extract tag specific information from the dependency.
45func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
Colin Crossdcf71b22021-02-01 13:59:03 -080046 err := tag.depInfoExtractor(paths, ctx, dep)
Paul Duffinc8782502020-04-29 20:45:27 +010047 if err != nil {
48 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
49 }
Paul Duffind1b3a922020-01-22 11:57:20 +000050}
51
Paul Duffin80342d72020-06-26 22:08:43 +010052var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
53
54func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
55 return false
56}
57
Paul Duffind1b3a922020-01-22 11:57:20 +000058// Provides information about an api scope, e.g. public, system, test.
59type apiScope struct {
60 // The name of the api scope, e.g. public, system, test
61 name string
62
Paul Duffin97b53b82020-05-05 14:40:52 +010063 // The api scope that this scope extends.
Paul Duffind0b9fca2022-09-30 18:11:41 +010064 //
65 // This organizes the scopes into an extension hierarchy.
66 //
67 // If set this means that the API provided by this scope includes the API provided by the scope
68 // set in this field.
Paul Duffin97b53b82020-05-05 14:40:52 +010069 extends *apiScope
70
Paul Duffind0b9fca2022-09-30 18:11:41 +010071 // The next api scope that a library that uses this scope can access.
72 //
73 // This organizes the scopes into an access hierarchy.
74 //
75 // If set this means that a library that can access this API can also access the API provided by
76 // the scope set in this field.
77 //
78 // A module that sets sdk_version: "<scope>_current" should have access to the <scope> API of
79 // every java_sdk_library that it depends on. If the library does not provide an API for <scope>
80 // then it will traverse up this access hierarchy to find an API that it does provide.
81 //
82 // If this is not set then it defaults to the scope set in extends.
83 canAccess *apiScope
84
Paul Duffin3375e352020-04-28 10:44:03 +010085 // The legacy enabled status for a specific scope can be dependent on other
86 // properties that have been specified on the library so it is provided by
87 // a function that can determine the status by examining those properties.
88 legacyEnabledStatus func(module *SdkLibrary) bool
89
90 // The default enabled status for non-legacy behavior, which is triggered by
91 // explicitly enabling at least one api scope.
92 defaultEnabledStatus bool
93
94 // Gets a pointer to the scope specific properties.
95 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
96
Paul Duffin46a26a82020-04-07 19:27:04 +010097 // The name of the field in the dynamically created structure.
98 fieldName string
99
Paul Duffin6b836ba2020-05-13 19:19:49 +0100100 // The name of the property in the java_sdk_library_import
101 propertyName string
102
Jihoon Kangb7431552024-01-22 19:40:08 +0000103 // The tag to use to depend on the prebuilt stubs library module
104 prebuiltStubsTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000105
Jihoon Kangbd093452023-12-26 19:08:01 +0000106 // The tag to use to depend on the everything stubs library module.
107 everythingStubsTag scopeDependencyTag
108
109 // The tag to use to depend on the exportable stubs library module.
110 exportableStubsTag scopeDependencyTag
111
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100112 // The tag to use to depend on the stubs source module (if separate from the API module).
113 stubsSourceTag scopeDependencyTag
114
Paul Duffinc8782502020-04-29 20:45:27 +0100115 // The tag to use to depend on the stubs source and API module.
116 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000117
Paul Duffin958806b2022-05-16 13:10:47 +0000118 // The tag to use to depend on the module that provides the latest version of the API .txt file.
119 latestApiModuleTag scopeDependencyTag
120
121 // The tag to use to depend on the module that provides the latest version of the API removed.txt
122 // file.
123 latestRemovedApiModuleTag scopeDependencyTag
124
Paul Duffind1b3a922020-01-22 11:57:20 +0000125 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
126 apiFilePrefix string
127
Paul Duffind0b9fca2022-09-30 18:11:41 +0100128 // The scope specific suffix to add to the sdk library module name to construct a scope specific
Paul Duffind1b3a922020-01-22 11:57:20 +0000129 // module name.
130 moduleSuffix string
131
Paul Duffind1b3a922020-01-22 11:57:20 +0000132 // SDK version that the stubs library is built against. Note that this is always
133 // *current. Older stubs library built with a numbered SDK version is created from
134 // the prebuilt jar.
135 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100136
Paul Duffin15f34ef2020-07-20 18:04:44 +0100137 // The annotation that identifies this API level, empty for the public API scope.
138 annotation string
139
Paul Duffin1fb487d2020-04-07 18:50:10 +0100140 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100141 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100142 // This is not used directly but is used to construct the droidstubsArgs.
143 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100144
Paul Duffin15f34ef2020-07-20 18:04:44 +0100145 // The args that must be passed to droidstubs to generate the API and stubs source
146 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100147 //
148 // The API only includes the additional members that this scope adds over the scope
149 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100150 //
151 // The stubs source must include the definitions of everything that is in this
152 // api scope and all the scopes that this one extends.
153 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100154
Anton Hansson6478ac12020-05-02 11:19:36 +0100155 // Whether the api scope can be treated as unstable, and should skip compat checks.
156 unstable bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000157
158 // Represents the SDK kind of this scope.
159 kind android.SdkKind
Paul Duffind1b3a922020-01-22 11:57:20 +0000160}
161
162// Initialize a scope, creating and adding appropriate dependency tags
163func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100164 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100165 scopeByName[name] = scope
166 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100167 scope.propertyName = strings.ReplaceAll(name, "-", "_")
168 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Jihoon Kangb7431552024-01-22 19:40:08 +0000169 scope.prebuiltStubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100170 name: name + "-stubs",
171 apiScope: scope,
172 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000173 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000174 scope.everythingStubsTag = scopeDependencyTag{
175 name: name + "-stubs-everything",
176 apiScope: scope,
177 depInfoExtractor: (*scopePaths).extractEverythingStubsLibraryInfoFromDependency,
178 }
179 scope.exportableStubsTag = scopeDependencyTag{
180 name: name + "-stubs-exportable",
181 apiScope: scope,
182 depInfoExtractor: (*scopePaths).extractExportableStubsLibraryInfoFromDependency,
183 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100184 scope.stubsSourceTag = scopeDependencyTag{
185 name: name + "-stubs-source",
186 apiScope: scope,
187 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
188 }
Paul Duffinc8782502020-04-29 20:45:27 +0100189 scope.stubsSourceAndApiTag = scopeDependencyTag{
190 name: name + "-stubs-source-and-api",
191 apiScope: scope,
192 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000193 }
Paul Duffin958806b2022-05-16 13:10:47 +0000194 scope.latestApiModuleTag = scopeDependencyTag{
195 name: name + "-latest-api",
196 apiScope: scope,
197 depInfoExtractor: (*scopePaths).extractLatestApiPath,
198 }
199 scope.latestRemovedApiModuleTag = scopeDependencyTag{
200 name: name + "-latest-removed-api",
201 apiScope: scope,
202 depInfoExtractor: (*scopePaths).extractLatestRemovedApiPath,
203 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100204
205 // To get the args needed to generate the stubs source append all the args from
206 // this scope and all the scopes it extends as each set of args adds additional
207 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100208 var scopeSpecificArgs []string
209 if scope.annotation != "" {
210 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100211 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100212 for s := scope; s != nil; s = s.extends {
213 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100214
Paul Duffin15f34ef2020-07-20 18:04:44 +0100215 // Ensure that the generated stubs includes all the API elements from the API scope
216 // that this scope extends.
217 if s != scope && s.annotation != "" {
218 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
219 }
220 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100221
Paul Duffind0b9fca2022-09-30 18:11:41 +0100222 // By default, a library that can access a scope can also access the scope it extends.
223 if scope.canAccess == nil {
224 scope.canAccess = scope.extends
225 }
226
Paul Duffin15f34ef2020-07-20 18:04:44 +0100227 // Escape any special characters in the arguments. This is needed because droidstubs
228 // passes these directly to the shell command.
229 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100230
Paul Duffind1b3a922020-01-22 11:57:20 +0000231 return scope
232}
233
Anton Hansson08f476b2021-04-07 15:32:19 +0100234func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
235 return ".stubs" + scope.moduleSuffix
236}
237
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000238func (scope *apiScope) exportableStubsLibraryModuleNameSuffix() string {
239 return ".stubs.exportable" + scope.moduleSuffix
240}
241
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000242func (scope *apiScope) apiLibraryModuleName(baseName string) string {
243 return scope.stubsLibraryModuleName(baseName) + ".from-text"
244}
245
Jihoon Kang2261a822024-09-12 00:01:54 +0000246func (scope *apiScope) sourceStubsLibraryModuleName(baseName string) string {
Jihoon Kang1147b312023-06-08 23:25:57 +0000247 return scope.stubsLibraryModuleName(baseName) + ".from-source"
248}
249
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000250func (scope *apiScope) exportableSourceStubsLibraryModuleName(baseName string) string {
251 return scope.exportableStubsLibraryModuleName(baseName) + ".from-source"
252}
253
Paul Duffinc3091c82020-05-08 14:16:20 +0100254func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100255 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000256}
257
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000258func (scope *apiScope) exportableStubsLibraryModuleName(baseName string) string {
259 return baseName + scope.exportableStubsLibraryModuleNameSuffix()
260}
261
Paul Duffinc8782502020-04-29 20:45:27 +0100262func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100263 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000264}
265
Paul Duffin3375e352020-04-28 10:44:03 +0100266func (scope *apiScope) String() string {
267 return scope.name
268}
269
Paul Duffin958806b2022-05-16 13:10:47 +0000270// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
271// be stored.
272func (scope *apiScope) snapshotRelativeDir() string {
273 return filepath.Join("sdk_library", scope.name)
274}
275
276// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
277// library.
278func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
279 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
280}
281
282// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
283// named library.
284func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
285 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
286}
287
Paul Duffind1b3a922020-01-22 11:57:20 +0000288type apiScopes []*apiScope
289
290func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
291 var list []string
292 for _, scope := range scopes {
293 list = append(list, accessor(scope))
294 }
295 return list
296}
297
Jihoon Kanga96a7b12023-09-20 23:43:32 +0000298// Method that maps the apiScopes properties to the index of each apiScopes elements.
299// apiScopes property to be used as the key can be specified with the input accessor.
300// Only a string property of apiScope can be used as the key of the map.
301func (scopes apiScopes) MapToIndex(accessor func(*apiScope) string) map[string]int {
302 ret := make(map[string]int)
303 for i, scope := range scopes {
304 ret[accessor(scope)] = i
305 }
306 return ret
307}
308
Jihoon Kang98aa8fa2024-06-07 11:06:57 +0000309func (scopes apiScopes) ConvertStubsLibraryExportableToEverything(name string) string {
310 for _, scope := range scopes {
311 if strings.HasSuffix(name, scope.exportableStubsLibraryModuleNameSuffix()) {
312 return strings.TrimSuffix(name, scope.exportableStubsLibraryModuleNameSuffix()) +
313 scope.stubsLibraryModuleNameSuffix()
314 }
315 }
316 return name
317}
318
Jiyong Parkc678ad32018-04-10 13:07:10 +0900319var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100320 scopeByName = make(map[string]*apiScope)
321 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000322 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100323 name: "public",
324
325 // Public scope is enabled by default for both legacy and non-legacy modes.
326 legacyEnabledStatus: func(module *SdkLibrary) bool {
327 return true
328 },
329 defaultEnabledStatus: true,
330
331 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
332 return &module.sdkLibraryProperties.Public
333 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000334 sdkVersion: "current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000335 kind: android.SdkPublic,
Paul Duffind1b3a922020-01-22 11:57:20 +0000336 })
337 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100338 name: "system",
339 extends: apiScopePublic,
340 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
341 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
342 return &module.sdkLibraryProperties.System
343 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100344 apiFilePrefix: "system-",
345 moduleSuffix: ".system",
346 sdkVersion: "system_current",
347 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000348 kind: android.SdkSystem,
Paul Duffind1b3a922020-01-22 11:57:20 +0000349 })
350 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100351 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100352 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100353 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
354 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
355 return &module.sdkLibraryProperties.Test
356 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100357 apiFilePrefix: "test-",
358 moduleSuffix: ".test",
359 sdkVersion: "test_current",
360 annotation: "android.annotation.TestApi",
361 unstable: true,
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000362 kind: android.SdkTest,
Paul Duffind1b3a922020-01-22 11:57:20 +0000363 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100364 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100365 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100366 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100367 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100368 //
369 // Enabling this would break existing usages.
370 legacyEnabledStatus: func(module *SdkLibrary) bool {
371 return false
372 },
373 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
374 return &module.sdkLibraryProperties.Module_lib
375 },
376 apiFilePrefix: "module-lib-",
377 moduleSuffix: ".module_lib",
378 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100379 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000380 kind: android.SdkModule,
Paul Duffin8f265b92020-04-28 14:13:56 +0100381 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100382 apiScopeSystemServer = initApiScope(&apiScope{
383 name: "system-server",
384 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100385
386 // The system-server scope can access the module-lib scope.
387 //
388 // A module that provides a system-server API is appended to the standard bootclasspath that is
389 // used by the system server. So, it should be able to access module-lib APIs provided by
390 // libraries on the bootclasspath.
391 canAccess: apiScopeModuleLib,
392
Paul Duffin0c5bae52020-06-02 13:00:08 +0100393 // The system-server scope is disabled by default in legacy mode.
394 //
395 // Enabling this would break existing usages.
396 legacyEnabledStatus: func(module *SdkLibrary) bool {
397 return false
398 },
399 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
400 return &module.sdkLibraryProperties.System_server
401 },
402 apiFilePrefix: "system-server-",
403 moduleSuffix: ".system_server",
404 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100405 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
406 extraArgs: []string{
407 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100408 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100409 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100410 },
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000411 kind: android.SdkSystemServer,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100412 })
Jihoon Kang98aa8fa2024-06-07 11:06:57 +0000413 AllApiScopes = apiScopes{
Paul Duffind1b3a922020-01-22 11:57:20 +0000414 apiScopePublic,
415 apiScopeSystem,
416 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100417 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100418 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000419 }
Jihoon Kangb0f4c022024-08-06 00:15:25 +0000420 apiLibraryAdditionalProperties = map[string]string{
421 "legacy.i18n.module.platform.api": "i18n.module.public.api.stubs.source.api.contribution",
422 "stable.i18n.module.platform.api": "i18n.module.public.api.stubs.source.api.contribution",
423 "conscrypt.module.platform.api": "conscrypt.module.public.api.stubs.source.api.contribution",
Jihoon Kang0c705a42023-08-02 06:44:57 +0000424 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900425)
426
Jiyong Park82484c02018-04-23 21:41:26 +0900427var (
428 javaSdkLibrariesLock sync.Mutex
429)
430
Jiyong Parkc678ad32018-04-10 13:07:10 +0900431// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900432// 1) disallowing linking to the runtime shared lib
433// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900434
435func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000436 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900437
Jiyong Park82484c02018-04-23 21:41:26 +0900438 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
439 javaSdkLibraries := javaSdkLibraries(ctx.Config())
440 sort.Strings(*javaSdkLibraries)
441 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
442 })
Paul Duffindd46f712020-02-10 13:37:10 +0000443
444 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100445 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900446}
447
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000448func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
449 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
450 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
451}
452
Paul Duffin3375e352020-04-28 10:44:03 +0100453// Properties associated with each api scope.
454type ApiScopeProperties struct {
455 // Indicates whether the api surface is generated.
456 //
457 // If this is set for any scope then all scopes must explicitly specify if they
458 // are enabled. This is to prevent new usages from depending on legacy behavior.
459 //
460 // Otherwise, if this is not set for any scope then the default behavior is
461 // scope specific so please refer to the scope specific property documentation.
462 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100463
464 // The sdk_version to use for building the stubs.
465 //
466 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000467 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100468 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000469 // will be none. This is used for java_sdk_library instances that are used
470 // to create stubs that contribute to the core_current sdk version.
471 // 2) Otherwise, it is assumed that this library extends but does not
472 // contribute directly to a specific sdk_version and so this uses the
473 // sdk_version appropriate for the api scope. e.g. public will use
474 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100475 //
476 // This does not affect the sdk_version used for either generating the stubs source
477 // or the API file. They both have to use the same sdk_version as is used for
478 // compiling the implementation library.
479 Sdk_version *string
Mark White9421c4c2023-08-10 00:07:03 +0000480
481 // Extra libs used when compiling stubs for this scope.
482 Libs []string
Paul Duffind01f0b22025-01-24 18:27:07 +0000483
484 // Name to override the api_surface that is passed down to droidstubs.
485 Api_surface *string
Paul Duffin3375e352020-04-28 10:44:03 +0100486}
487
Jiyong Parkc678ad32018-04-10 13:07:10 +0900488type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100489 // List of source files that are needed to compile the API, but are not part of runtime library.
490 Api_srcs []string `android:"arch_variant"`
491
Paul Duffin5df79302020-05-16 15:52:12 +0100492 // Visibility for impl library module. If not specified then defaults to the
493 // visibility property.
494 Impl_library_visibility []string
495
Paul Duffin4911a892020-04-29 23:35:13 +0100496 // Visibility for stubs library modules. If not specified then defaults to the
497 // visibility property.
498 Stubs_library_visibility []string
499
500 // Visibility for stubs source modules. If not specified then defaults to the
501 // visibility property.
502 Stubs_source_visibility []string
503
Anton Hansson7f66efa2020-10-08 14:47:23 +0100504 // List of Java libraries that will be in the classpath when building the implementation lib
505 Impl_only_libs []string `android:"arch_variant"`
506
Paul Duffin77590a82022-04-28 14:13:30 +0000507 // List of Java libraries that will included in the implementation lib.
508 Impl_only_static_libs []string `android:"arch_variant"`
509
Sundong Ahnf043cf62018-06-25 16:04:37 +0900510 // List of Java libraries that will be in the classpath when building stubs
511 Stub_only_libs []string `android:"arch_variant"`
512
Anton Hanssondae54cd2021-04-21 16:30:10 +0100513 // List of Java libraries that will included in stub libraries
514 Stub_only_static_libs []string `android:"arch_variant"`
515
Paul Duffin7a586d32019-12-30 17:09:34 +0000516 // list of package names that will be documented and publicized as API.
517 // This allows the API to be restricted to a subset of the source files provided.
518 // If this is unspecified then all the source files will be treated as being part
519 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900520 Api_packages []string
521
Paul Duffin749f98f2019-12-30 17:23:46 +0000522 // the relative path to the directory containing the api specification files.
523 // Defaults to "api".
524 Api_dir *string
525
Paul Duffindfa131e2020-05-15 20:37:11 +0100526 // Determines whether a runtime implementation library is built; defaults to false.
527 //
528 // If true then it also prevents the module from being used as a shared module, i.e.
MÃ¥rten Kongstad81d90952022-05-25 16:27:11 +0200529 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000530 Api_only *bool
531
Paul Duffin11512472019-02-11 15:55:17 +0000532 // local files that are used within user customized droiddoc options.
533 Droiddoc_option_files []string
534
Spandan Das93e95992021-07-29 18:26:39 +0000535 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000536 // Available variables for substitution:
537 //
538 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900539 Droiddoc_options []string
540
Paul Duffine22c2ab2020-05-20 19:35:27 +0100541 // is set to true, Metalava will allow framework SDK to contain annotations.
542 Annotations_enabled *bool
543
Sundong Ahn054b19a2018-10-19 13:46:09 +0900544 // a list of top-level directories containing files to merge qualifier annotations
545 // (i.e. those intended to be included in the stubs written) from.
546 Merge_annotations_dirs []string
547
548 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
549 Merge_inclusion_annotations_dirs []string
550
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000551 // If set to true then don't create dist rules.
552 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900553
Paul Duffin31310252020-11-20 21:26:20 +0000554 // The stem for the artifacts that are copied to the dist, if not specified
555 // then defaults to the base module name.
556 //
557 // For each scope the following artifacts are copied to the apistubs/<scope>
558 // directory in the dist.
559 // * stubs impl jar -> <dist-stem>.jar
560 // * API specification file -> api/<dist-stem>.txt
561 // * Removed API specification file -> api/<dist-stem>-removed.txt
562 //
563 // Also used to construct the name of the filegroup (created by prebuilt_apis)
564 // that references the latest released API and remove API specification files.
565 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
566 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800567 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000568 Dist_stem *string
569
Colin Cross986b69a2021-06-01 13:13:40 -0700570 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700571 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700572 // in the public Android SDK.
573 Dist_group *string
574
Anton Hanssondff2c782020-12-21 17:10:01 +0000575 // A compatibility mode that allows historical API-tracking files to not exist.
576 // Do not use.
577 Unsafe_ignore_missing_latest_api bool
578
Paul Duffin3375e352020-04-28 10:44:03 +0100579 // indicates whether system and test apis should be generated.
580 Generate_system_and_test_apis bool `blueprint:"mutated"`
581
582 // The properties specific to the public api scope
583 //
584 // Unless explicitly specified by using public.enabled the public api scope is
585 // enabled by default in both legacy and non-legacy mode.
586 Public ApiScopeProperties
587
588 // The properties specific to the system api scope
589 //
590 // In legacy mode the system api scope is enabled by default when sdk_version
591 // is set to something other than "none".
592 //
593 // In non-legacy mode the system api scope is disabled by default.
594 System ApiScopeProperties
595
596 // The properties specific to the test api scope
597 //
598 // In legacy mode the test api scope is enabled by default when sdk_version
599 // is set to something other than "none".
600 //
601 // In non-legacy mode the test api scope is disabled by default.
602 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000603
Paul Duffin0c5bae52020-06-02 13:00:08 +0100604 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100605 //
Zi Wangb2179e32023-01-31 15:53:30 -0800606 // Unless explicitly specified by using module_lib.enabled the module_lib api
607 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100608 Module_lib ApiScopeProperties
609
Paul Duffin0c5bae52020-06-02 13:00:08 +0100610 // The properties specific to the system-server api scope
611 //
Zi Wangb2179e32023-01-31 15:53:30 -0800612 // Unless explicitly specified by using system_server.enabled the
613 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100614 System_server ApiScopeProperties
615
Jiyong Park932cdfe2020-05-28 00:19:53 +0900616 // Determines if the stubs are preferred over the implementation library
617 // for linking, even when the client doesn't specify sdk_version. When this
618 // is set to true, such clients are provided with the widest API surface that
619 // this lib provides. Note however that this option doesn't affect the clients
620 // that are in the same APEX as this library. In that case, the clients are
621 // always linked with the implementation library. Default is false.
622 Default_to_stubs *bool
623
Paul Duffin160fe412020-05-10 19:32:20 +0100624 // Properties related to api linting.
625 Api_lint struct {
626 // Enable api linting.
627 Enabled *bool
Anton Hanssonfd1c0d22023-11-02 15:18:09 +0000628
629 // If API lint is enabled, this flag controls whether a set of legitimate lint errors
630 // are turned off. The default is true.
631 Legacy_errors_allowed *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100632 }
633
Jihoon Kang6592e872023-12-19 01:13:16 +0000634 // a list of aconfig_declarations module names that the stubs generated in this module
635 // depend on.
636 Aconfig_declarations []string
637
Jihoon Kang48e2ac92024-07-29 21:18:46 +0000638 // Determines if the module generates the stubs from the api signature files
639 // instead of the source Java files. Defaults to true.
640 Build_from_text_stub *bool
641
Jiyong Parkc678ad32018-04-10 13:07:10 +0900642 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100643 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900644}
645
Paul Duffin0f8faff2020-05-20 16:18:00 +0100646// Paths to outputs from java_sdk_library and java_sdk_library_import.
647//
648// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
649// OptionalPaths are always set by java_sdk_library but may not be set by
650// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000651type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100652 // The path (represented as Paths for convenience when returning) to the stubs header jar.
653 //
654 // That is the jar that is created by turbine.
655 stubsHeaderPath android.Paths
656
657 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
658 //
659 // This is not the implementation jar, it still only contains stubs.
660 stubsImplPath android.Paths
661
Paul Duffin1267d872021-04-16 17:21:36 +0100662 // The dex jar for the stubs.
663 //
664 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100665 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100666
Jihoon Kangbd093452023-12-26 19:08:01 +0000667 // The exportable dex jar for the stubs.
668 // This is not the implementation jar, it still only contains stubs.
669 // Includes unflagged apis and flagged apis enabled by release configurations.
670 exportableStubsDexJarPath OptionalDexJarPath
671
Paul Duffin0f8faff2020-05-20 16:18:00 +0100672 // The API specification file, e.g. system_current.txt.
673 currentApiFilePath android.OptionalPath
674
675 // The specification of API elements removed since the last release.
676 removedApiFilePath android.OptionalPath
677
678 // The stubs source jar.
679 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100680
681 // Extracted annotations.
682 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000683
684 // The path to the latest API file.
Jihoon Kang5623e542024-01-31 23:27:26 +0000685 latestApiPaths android.Paths
Paul Duffin958806b2022-05-16 13:10:47 +0000686
687 // The path to the latest removed API file.
Jihoon Kang5623e542024-01-31 23:27:26 +0000688 latestRemovedApiPaths android.Paths
Paul Duffind1b3a922020-01-22 11:57:20 +0000689}
690
Colin Crossdcf71b22021-02-01 13:59:03 -0800691func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Colin Cross313aa542023-12-13 13:47:44 -0800692 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800693 paths.stubsHeaderPath = lib.HeaderJars
694 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100695
Yu Liu35acd332025-01-24 23:11:22 +0000696 libDep := android.OtherModuleProviderOrDefault(ctx, dep, JavaInfoProvider).UsesLibraryDependencyInfo
697 paths.stubsDexJarPath = libDep.DexJarBuildPath
698 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath
Jihoon Kangbd093452023-12-26 19:08:01 +0000699 return nil
700 } else {
701 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
702 }
703}
704
705func (paths *scopePaths) extractEverythingStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
706 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
707 paths.stubsHeaderPath = lib.HeaderJars
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000708 if !ctx.Config().ReleaseHiddenApiExportableStubs() {
709 paths.stubsImplPath = lib.ImplementationJars
710 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000711
Yu Liu35acd332025-01-24 23:11:22 +0000712 libDep := android.OtherModuleProviderOrDefault(ctx, dep, JavaInfoProvider).UsesLibraryDependencyInfo
713 paths.stubsDexJarPath = libDep.DexJarBuildPath
Jihoon Kangbd093452023-12-26 19:08:01 +0000714 return nil
715 } else {
716 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
717 }
718}
719
720func (paths *scopePaths) extractExportableStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000721 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
722 if ctx.Config().ReleaseHiddenApiExportableStubs() {
723 paths.stubsImplPath = lib.ImplementationJars
724 }
725
Yu Liu35acd332025-01-24 23:11:22 +0000726 libDep := android.OtherModuleProviderOrDefault(ctx, dep, JavaInfoProvider).UsesLibraryDependencyInfo
727 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath
Paul Duffinc8782502020-04-29 20:45:27 +0100728 return nil
729 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800730 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100731 }
732}
733
Yu Liu35acd332025-01-24 23:11:22 +0000734func (paths *scopePaths) treatDepAsApiStubsProvider(ctx android.ModuleContext, dep android.Module,
735 action func(*DroidStubsInfo, *StubsSrcInfo) error) error {
736 apiStubsProvider, ok := android.OtherModuleProvider(ctx, dep, DroidStubsInfoProvider)
737 if !ok {
738 return fmt.Errorf("expected module that provides DroidStubsInfo, e.g. droidstubs")
739 }
740
741 apiStubsSrcProvider, ok := android.OtherModuleProvider(ctx, dep, StubsSrcInfoProvider)
742 if !ok {
743 return fmt.Errorf("expected module that provides StubsSrcInfo, e.g. droidstubs")
744 }
745 return action(&apiStubsProvider, &apiStubsSrcProvider)
746}
747
748func (paths *scopePaths) treatDepAsApiStubsSrcProvider(
749 ctx android.ModuleContext, dep android.Module, action func(provider *StubsSrcInfo) error) error {
750 if apiStubsProvider, ok := android.OtherModuleProvider(ctx, dep, StubsSrcInfoProvider); ok {
751 err := action(&apiStubsProvider)
Jihoon Kangee113282024-01-23 00:16:41 +0000752 if err != nil {
753 return err
754 }
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000755 return nil
756 } else {
Yu Liu35acd332025-01-24 23:11:22 +0000757 return fmt.Errorf("expected module that provides DroidStubsInfo, e.g. droidstubs")
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000758 }
759}
760
Yu Liu35acd332025-01-24 23:11:22 +0000761func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider *DroidStubsInfo, stubsType StubsType) error {
762 var currentApiFilePathErr, removedApiFilePathErr error
763 info, err := getStubsInfoForType(provider, stubsType)
764 if err != nil {
765 return err
Paul Duffin0f8faff2020-05-20 16:18:00 +0100766 }
Yu Liu35acd332025-01-24 23:11:22 +0000767 if info.ApiFile == nil {
768 currentApiFilePathErr = fmt.Errorf("expected module that provides ApiFile")
769 }
770 if info.RemovedApiFile == nil {
771 removedApiFilePathErr = fmt.Errorf("expected module that provides RemovedApiFile")
772 }
773 combinedError := errors.Join(currentApiFilePathErr, removedApiFilePathErr)
Jihoon Kangee113282024-01-23 00:16:41 +0000774
775 if combinedError == nil {
Yu Liu35acd332025-01-24 23:11:22 +0000776 paths.annotationsZip = android.OptionalPathForPath(info.AnnotationsZip)
777 paths.currentApiFilePath = android.OptionalPathForPath(info.ApiFile)
778 paths.removedApiFilePath = android.OptionalPathForPath(info.RemovedApiFile)
Jihoon Kangee113282024-01-23 00:16:41 +0000779 }
780 return combinedError
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000781}
782
Yu Liu35acd332025-01-24 23:11:22 +0000783func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider *StubsSrcInfo, stubsType StubsType) error {
784 path, err := getStubsSrcInfoForType(provider, stubsType)
Jihoon Kangee113282024-01-23 00:16:41 +0000785 if err == nil {
Yu Liu35acd332025-01-24 23:11:22 +0000786 paths.stubsSrcJar = android.OptionalPathForPath(path)
Jihoon Kangee113282024-01-23 00:16:41 +0000787 }
788 return err
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000789}
790
Colin Crossdcf71b22021-02-01 13:59:03 -0800791func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000792 stubsType := Everything
793 if ctx.Config().ReleaseHiddenApiExportableStubs() {
794 stubsType = Exportable
795 }
Yu Liu35acd332025-01-24 23:11:22 +0000796 return paths.treatDepAsApiStubsSrcProvider(ctx, dep, func(provider *StubsSrcInfo) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000797 return paths.extractStubsSourceInfoFromApiStubsProviders(provider, stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100798 })
799}
800
Colin Crossdcf71b22021-02-01 13:59:03 -0800801func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000802 stubsType := Everything
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000803 if ctx.Config().ReleaseHiddenApiExportableStubs() {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000804 stubsType = Exportable
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000805 }
Yu Liu35acd332025-01-24 23:11:22 +0000806 return paths.treatDepAsApiStubsProvider(ctx, dep, func(apiStubsProvider *DroidStubsInfo, apiStubsSrcProvider *StubsSrcInfo) error {
807 extractApiInfoErr := paths.extractApiInfoFromApiStubsProvider(apiStubsProvider, stubsType)
808 extractStubsSourceInfoErr := paths.extractStubsSourceInfoFromApiStubsProviders(apiStubsSrcProvider, stubsType)
Jihoon Kangee113282024-01-23 00:16:41 +0000809 return errors.Join(extractApiInfoErr, extractStubsSourceInfoErr)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100810 })
811}
812
Yu Liu35acd332025-01-24 23:11:22 +0000813func extractOutputPaths(ctx android.ModuleContext, dep android.Module) (android.Paths, error) {
Paul Duffin958806b2022-05-16 13:10:47 +0000814 var paths android.Paths
Yu Liu35acd332025-01-24 23:11:22 +0000815 if sourceFileProducer, ok := android.OtherModuleProvider(ctx, dep, android.SourceFilesInfoProvider); ok {
816 paths = sourceFileProducer.Srcs
Jihoon Kang5623e542024-01-31 23:27:26 +0000817 return paths, nil
Paul Duffin958806b2022-05-16 13:10:47 +0000818 } else {
Jihoon Kang5623e542024-01-31 23:27:26 +0000819 return nil, fmt.Errorf("module %q does not produce source files", dep)
Paul Duffin958806b2022-05-16 13:10:47 +0000820 }
Paul Duffin958806b2022-05-16 13:10:47 +0000821}
822
823func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
Yu Liu35acd332025-01-24 23:11:22 +0000824 outputPaths, err := extractOutputPaths(ctx, dep)
Jihoon Kang5623e542024-01-31 23:27:26 +0000825 paths.latestApiPaths = outputPaths
Paul Duffin958806b2022-05-16 13:10:47 +0000826 return err
827}
828
829func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
Yu Liu35acd332025-01-24 23:11:22 +0000830 outputPaths, err := extractOutputPaths(ctx, dep)
Jihoon Kang5623e542024-01-31 23:27:26 +0000831 paths.latestRemovedApiPaths = outputPaths
Paul Duffin958806b2022-05-16 13:10:47 +0000832 return err
833}
834
Yu Liu35acd332025-01-24 23:11:22 +0000835func getStubsInfoForType(info *DroidStubsInfo, stubsType StubsType) (ret *StubsInfo, err error) {
836 switch stubsType {
837 case Everything:
838 ret, err = &info.EverythingStubsInfo, nil
839 case Exportable:
840 ret, err = &info.ExportableStubsInfo, nil
841 default:
842 ret, err = nil, fmt.Errorf("stubs info not supported for the stub type %s", stubsType.String())
843 }
844 if ret == nil && err == nil {
845 err = fmt.Errorf("stubs info is null for the stub type %s", stubsType.String())
846 }
847 return ret, err
848}
849
850func getStubsSrcInfoForType(info *StubsSrcInfo, stubsType StubsType) (ret android.Path, err error) {
851 switch stubsType {
852 case Everything:
853 ret, err = info.EverythingStubsSrcJar, nil
854 case Exportable:
855 ret, err = info.ExportableStubsSrcJar, nil
856 default:
857 ret, err = nil, fmt.Errorf("stubs src info not supported for the stub type %s", stubsType.String())
858 }
859 if ret == nil && err == nil {
860 err = fmt.Errorf("stubs src info is null for the stub type %s", stubsType.String())
861 }
862 return ret, err
863}
864
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100865type commonToSdkLibraryAndImportProperties struct {
Paul Duffindfa131e2020-05-15 20:37:11 +0100866 // Specifies whether this module can be used as an Android shared library; defaults
867 // to true.
868 //
869 // An Android shared library is one that can be referenced in a <uses-library> element
870 // in an AndroidManifest.xml.
871 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100872
873 // Files containing information about supported java doc tags.
874 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000875
876 // Signals that this shared library is part of the bootclasspath starting
877 // on the version indicated in this attribute.
878 //
879 // This will make platforms at this level and above to ignore
880 // <uses-library> tags with this library name because the library is already
881 // available
882 On_bootclasspath_since *string
883
884 // Signals that this shared library was part of the bootclasspath before
885 // (but not including) the version indicated in this attribute.
886 //
887 // The system will automatically add a <uses-library> tag with this library to
888 // apps that target any SDK less than the version indicated in this attribute.
889 On_bootclasspath_before *string
890
891 // Indicates that PackageManager should ignore this shared library if the
892 // platform is below the version indicated in this attribute.
893 //
894 // This means that the device won't recognise this library as installed.
895 Min_device_sdk *string
896
897 // Indicates that PackageManager should ignore this shared library if the
898 // platform is above the version indicated in this attribute.
899 //
900 // This means that the device won't recognise this library as installed.
901 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100902}
903
Paul Duffin71b33cc2021-06-23 11:39:47 +0100904// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
905// embeds the commonToSdkLibraryAndImport struct.
906type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000907 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100908
Spandan Das23956d12024-01-19 00:22:22 +0000909 // Returns the name of the root java_sdk_library that creates the child stub libraries
910 // This is the `name` as it appears in Android.bp, and not the name in Soong's build graph
911 // (with the prebuilt_ prefix)
912 //
913 // e.g. in the following java_sdk_library_import
914 // java_sdk_library_import {
915 // name: "framework-foo.v1",
916 // source_module_name: "framework-foo",
917 // }
918 // the values returned by
919 // 1. Name(): prebuilt_framework-foo.v1 # unique
920 // 2. BaseModuleName(): framework-foo # the source
921 // 3. RootLibraryName: framework-foo.v1 # the undecordated `name` from Android.bp
922 RootLibraryName() string
923}
924
925func (m *SdkLibrary) RootLibraryName() string {
926 return m.BaseModuleName()
927}
928
929func (m *SdkLibraryImport) RootLibraryName() string {
930 // m.BaseModuleName refers to the source of the import
931 // use moduleBase.Name to get the name of the module as it appears in the .bp file
932 return m.ModuleBase.Name()
Paul Duffin71b33cc2021-06-23 11:39:47 +0100933}
934
Paul Duffin56d44902020-01-31 13:36:25 +0000935// Common code between sdk library and sdk library import
936type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100937 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100938
Paul Duffin56d44902020-01-31 13:36:25 +0000939 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100940
Paul Duffindfa131e2020-05-15 20:37:11 +0100941 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100942
Paul Duffina2ae7e02020-09-11 11:55:00 +0100943 // Paths to commonSdkLibraryProperties.Doctag_files
944 doctagPaths android.Paths
945
Paul Duffin859fe962020-05-15 10:20:31 +0100946 // Functionality related to this being used as a component of a java_sdk_library.
947 EmbeddableSdkLibraryComponent
Jihoon Kang8479dea2024-04-04 01:19:05 +0000948
949 // Path to the header jars of the implementation library
950 // This is non-empty only when api_only is false.
951 implLibraryHeaderJars android.Paths
Jihoon Kanga3a05462024-04-05 00:36:44 +0000952
Yu Liu35acd332025-01-24 23:11:22 +0000953 // The reference to the JavaInfo provided by implementation library created by
954 // the source module. Is nil if the source module does not exist.
955 implLibraryInfo *JavaInfo
Paul Duffin56d44902020-01-31 13:36:25 +0000956}
957
Paul Duffin71b33cc2021-06-23 11:39:47 +0100958func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
959 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100960
Paul Duffin71b33cc2021-06-23 11:39:47 +0100961 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100962
963 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100964 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100965}
966
Jihoon Kang98e9ac62024-09-25 23:42:30 +0000967func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied() bool {
Spandan Das23956d12024-01-19 00:22:22 +0000968 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +0100969 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
970
Paul Duffindfa131e2020-05-15 20:37:11 +0100971 // Only track this sdk library if this can be used as a shared library.
972 if c.sharedLibrary() {
973 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100974 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100975 }
Paul Duffin859fe962020-05-15 10:20:31 +0100976
Paul Duffin1b1e8062020-05-08 13:44:43 +0100977 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100978}
979
Paul Duffinea8f8082021-06-24 13:25:57 +0100980// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
981// method.
982func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
983 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
984 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
985 // the APEX and so it needs a unique variation per APEX.
986 return c.sharedLibrary()
987}
988
Jihoon Kang98e9ac62024-09-25 23:42:30 +0000989func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) SdkLibraryInfo {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100990 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
Paul Duffina2ae7e02020-09-11 11:55:00 +0100991
Jihoon Kang98e9ac62024-09-25 23:42:30 +0000992 everythingStubPaths := make(map[android.SdkKind]OptionalDexJarPath)
993 exportableStubPaths := make(map[android.SdkKind]OptionalDexJarPath)
994 removedApiFilePaths := make(map[android.SdkKind]android.OptionalPath)
995 for kind := android.SdkNone; kind <= android.SdkPrivate; kind += 1 {
996 everythingStubPath := makeUnsetDexJarPath()
997 exportableStubPath := makeUnsetDexJarPath()
998 removedApiFilePath := android.OptionalPath{}
999 if scopePath := c.findClosestScopePath(sdkKindToApiScope(kind)); scopePath != nil {
1000 everythingStubPath = scopePath.stubsDexJarPath
1001 exportableStubPath = scopePath.exportableStubsDexJarPath
1002 removedApiFilePath = scopePath.removedApiFilePath
1003 }
1004 everythingStubPaths[kind] = everythingStubPath
1005 exportableStubPaths[kind] = exportableStubPath
1006 removedApiFilePaths[kind] = removedApiFilePath
1007 }
1008
Yu Liu460cf372025-01-10 00:34:06 +00001009 javaInfo := &JavaInfo{}
1010 setExtraJavaInfo(ctx, ctx.Module(), javaInfo)
1011 android.SetProvider(ctx, JavaInfoProvider, javaInfo)
1012
Jihoon Kang98e9ac62024-09-25 23:42:30 +00001013 return SdkLibraryInfo{
1014 EverythingStubDexJarPaths: everythingStubPaths,
1015 ExportableStubDexJarPaths: exportableStubPaths,
1016 RemovedTxtFiles: removedApiFilePaths,
1017 SharedLibrary: c.sharedLibrary(),
1018 }
Jihoon Kanga3a05462024-04-05 00:36:44 +00001019}
1020
Paul Duffin46dc45a2020-05-14 15:39:10 +01001021// The component names for different outputs of the java_sdk_library.
1022//
1023// They are similar to the names used for the child modules it creates
1024const (
1025 stubsSourceComponentName = "stubs.source"
1026
1027 apiTxtComponentName = "api.txt"
1028
1029 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +01001030
1031 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +01001032)
1033
mrziwang9f7b9f42024-07-10 12:18:06 -07001034func (module *commonToSdkLibraryAndImport) setOutputFiles(ctx android.ModuleContext) {
1035 if module.doctagPaths != nil {
1036 ctx.SetOutputFiles(module.doctagPaths, ".doctags")
1037 }
1038 for _, scopeName := range android.SortedKeys(scopeByName) {
1039 paths := module.findScopePaths(scopeByName[scopeName])
1040 if paths == nil {
1041 continue
Paul Duffin46dc45a2020-05-14 15:39:10 +01001042 }
mrziwang9f7b9f42024-07-10 12:18:06 -07001043 componentToOutput := map[string]android.OptionalPath{
1044 stubsSourceComponentName: paths.stubsSrcJar,
1045 apiTxtComponentName: paths.currentApiFilePath,
1046 removedApiTxtComponentName: paths.removedApiFilePath,
1047 annotationsComponentName: paths.annotationsZip,
1048 }
1049 for _, component := range android.SortedKeys(componentToOutput) {
1050 if componentToOutput[component].Valid() {
1051 ctx.SetOutputFiles(android.Paths{componentToOutput[component].Path()}, "."+scopeName+"."+component)
Paul Duffina2ae7e02020-09-11 11:55:00 +01001052 }
1053 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001054 }
1055}
1056
Paul Duffin803a9562020-05-20 11:52:25 +01001057func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001058 if c.scopePaths == nil {
1059 c.scopePaths = make(map[*apiScope]*scopePaths)
1060 }
1061 paths := c.scopePaths[scope]
1062 if paths == nil {
1063 paths = &scopePaths{}
1064 c.scopePaths[scope] = paths
1065 }
1066
1067 return paths
1068}
1069
Paul Duffin803a9562020-05-20 11:52:25 +01001070func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1071 if c.scopePaths == nil {
1072 return nil
1073 }
1074
1075 return c.scopePaths[scope]
1076}
1077
1078// If this does not support the requested api scope then find the closest available
1079// scope it does support. Returns nil if no such scope is available.
1080func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001081 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001082 if paths := c.findScopePaths(s); paths != nil {
1083 return paths
1084 }
1085 }
1086
1087 // This should never happen outside tests as public should be the base scope for every
1088 // scope and is enabled by default.
1089 return nil
1090}
1091
Paul Duffin32cf58a2021-05-18 16:32:50 +01001092// sdkKindToApiScope maps from android.SdkKind to apiScope.
1093func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1094 var apiScope *apiScope
1095 switch kind {
1096 case android.SdkSystem:
1097 apiScope = apiScopeSystem
1098 case android.SdkModule:
1099 apiScope = apiScopeModuleLib
1100 case android.SdkTest:
1101 apiScope = apiScopeTest
1102 case android.SdkSystemServer:
1103 apiScope = apiScopeSystemServer
1104 default:
1105 apiScope = apiScopePublic
1106 }
1107 return apiScope
1108}
1109
Paul Duffin859fe962020-05-15 10:20:31 +01001110func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1111 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001112 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001113 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001114 }{}
1115
Spandan Das23956d12024-01-19 00:22:22 +00001116 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +01001117 componentProps.SdkLibraryName = namePtr
1118
Paul Duffindfa131e2020-05-15 20:37:11 +01001119 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001120 // Mark the stubs library as being components of this java_sdk_library so that
1121 // any app that includes code which depends (directly or indirectly) on the stubs
1122 // library will have the appropriate <uses-library> invocation inserted into its
1123 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001124 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001125 }
1126
1127 return componentProps
1128}
1129
Paul Duffindfa131e2020-05-15 20:37:11 +01001130func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1131 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1132}
1133
Paul Duffinf4600f62021-05-13 22:34:45 +01001134// Check if the stub libraries should be compiled for dex
1135func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1136 // Always compile the dex file files for the stub libraries if they will be used on the
1137 // bootclasspath.
1138 return !c.sharedLibrary()
1139}
1140
Paul Duffin859fe962020-05-15 10:20:31 +01001141// Properties related to the use of a module as an component of a java_sdk_library.
1142type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001143 // The name of the java_sdk_library/_import module.
1144 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001145
1146 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1147 // in the AndroidManifest.xml of any Android app that includes code that references
1148 // this module. If not set then no java_sdk_library/_import is tracked.
1149 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1150}
1151
1152// Structure to be embedded in a module struct that needs to support the
1153// SdkLibraryComponentDependency interface.
1154type EmbeddableSdkLibraryComponent struct {
1155 sdkLibraryComponentProperties SdkLibraryComponentProperties
1156}
1157
Paul Duffin71b33cc2021-06-23 11:39:47 +01001158func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1159 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001160}
1161
1162// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001163func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1164 return e.sdkLibraryComponentProperties.SdkLibraryName
1165}
1166
1167// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001168func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001169 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1170 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1171 // run-time library and the corresponding module that provides the implementation. This name is
1172 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1173 // in dexpreopt).
1174 //
1175 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1176 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001177 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1178}
1179
Paul Duffin859fe962020-05-15 10:20:31 +01001180// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1181// (including the java_sdk_library) itself.
1182type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001183 UsesLibraryDependency
1184
Paul Duffin3f0290e2021-06-30 18:25:36 +01001185 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1186 SdkLibraryName() *string
1187
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001188 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1189 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001190}
1191
1192// Make sure that all the module types that are components of java_sdk_library/_import
1193// and which can be referenced (directly or indirectly) from an android app implement
1194// the SdkLibraryComponentDependency interface.
1195var _ SdkLibraryComponentDependency = (*Library)(nil)
1196var _ SdkLibraryComponentDependency = (*Import)(nil)
1197var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001198var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001199
Jihoon Kang28c96572024-09-11 23:44:44 +00001200type SdkLibraryInfo struct {
1201 // GeneratingLibs is the names of the library modules that this sdk library
1202 // generates. Note that this only includes the name of the modules that other modules can
1203 // depend on, and is not a holistic list of generated modules.
1204 GeneratingLibs []string
Jihoon Kang98e9ac62024-09-25 23:42:30 +00001205
1206 // Map of sdk kind to the dex jar for the "everything" stubs.
1207 // It is needed by the hiddenapi processing tool which processes dex files.
1208 EverythingStubDexJarPaths map[android.SdkKind]OptionalDexJarPath
1209
1210 // Map of sdk kind to the dex jar for the "exportable" stubs.
1211 // It is needed by the hiddenapi processing tool which processes dex files.
1212 ExportableStubDexJarPaths map[android.SdkKind]OptionalDexJarPath
1213
1214 // Map of sdk kind to the optional path to the removed.txt file.
1215 RemovedTxtFiles map[android.SdkKind]android.OptionalPath
1216
1217 // Whether if this can be used as a shared library.
1218 SharedLibrary bool
Jihoon Kang28c96572024-09-11 23:44:44 +00001219}
1220
1221var SdkLibraryInfoProvider = blueprint.NewProvider[SdkLibraryInfo]()
1222
1223func getGeneratingLibs(ctx android.ModuleContext, sdkVersion android.SdkSpec, sdkLibraryModuleName string, sdkInfo SdkLibraryInfo) []string {
1224 apiLevel := sdkVersion.ApiLevel
1225 if apiLevel.IsPreview() {
1226 return sdkInfo.GeneratingLibs
1227 }
1228
1229 generatingPrebuilts := []string{}
1230 for _, apiScope := range AllApiScopes {
1231 scopePrebuiltModuleName := prebuiltApiModuleName("sdk", sdkLibraryModuleName, apiScope.name, apiLevel.String())
1232 if ctx.OtherModuleExists(scopePrebuiltModuleName) {
1233 generatingPrebuilts = append(generatingPrebuilts, scopePrebuiltModuleName)
1234 }
1235 }
1236 return generatingPrebuilts
1237}
1238
Inseob Kimc0907f12019-02-08 21:00:45 +09001239type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001240 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001241
Sundong Ahn054b19a2018-10-19 13:46:09 +09001242 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001243
Paul Duffin3375e352020-04-28 10:44:03 +01001244 // Map from api scope to the scope specific property structure.
1245 scopeToProperties map[*apiScope]*ApiScopeProperties
1246
Paul Duffin56d44902020-01-31 13:36:25 +00001247 commonToSdkLibraryAndImport
Jihoon Kanga3a05462024-04-05 00:36:44 +00001248
1249 builtInstalledForApex []dexpreopterInstall
Jiyong Parkc678ad32018-04-10 13:07:10 +09001250}
1251
Paul Duffin3375e352020-04-28 10:44:03 +01001252func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1253 return module.sdkLibraryProperties.Generate_system_and_test_apis
1254}
1255
Jihoon Kang98e9ac62024-09-25 23:42:30 +00001256var _ UsesLibraryDependency = (*SdkLibrary)(nil)
1257
1258// To satisfy the UsesLibraryDependency interface
Jihoon Kanga3a05462024-04-05 00:36:44 +00001259func (module *SdkLibrary) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Yu Liu35acd332025-01-24 23:11:22 +00001260 if module.implLibraryInfo != nil {
1261 return module.implLibraryInfo.DexJarFile
Jihoon Kanga3a05462024-04-05 00:36:44 +00001262 }
1263 return makeUnsetDexJarPath()
1264}
1265
Jihoon Kang98e9ac62024-09-25 23:42:30 +00001266// To satisfy the UsesLibraryDependency interface
Jihoon Kanga3a05462024-04-05 00:36:44 +00001267func (module *SdkLibrary) DexJarInstallPath() android.Path {
Yu Liu35acd332025-01-24 23:11:22 +00001268 if module.implLibraryInfo != nil {
1269 return module.implLibraryInfo.InstallFile
Jihoon Kanga3a05462024-04-05 00:36:44 +00001270 }
1271 return nil
1272}
1273
Paul Duffin3375e352020-04-28 10:44:03 +01001274func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1275 // Check to see if any scopes have been explicitly enabled. If any have then all
1276 // must be.
1277 anyScopesExplicitlyEnabled := false
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001278 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001279 scopeProperties := module.scopeToProperties[scope]
1280 if scopeProperties.Enabled != nil {
1281 anyScopesExplicitlyEnabled = true
1282 break
1283 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001284 }
Paul Duffin3375e352020-04-28 10:44:03 +01001285
1286 var generatedScopes apiScopes
1287 enabledScopes := make(map[*apiScope]struct{})
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001288 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001289 scopeProperties := module.scopeToProperties[scope]
1290 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1291 // This is to ensure that any new usages of this module type do not rely on legacy
1292 // behaviour.
1293 defaultEnabledStatus := false
1294 if anyScopesExplicitlyEnabled {
1295 defaultEnabledStatus = scope.defaultEnabledStatus
1296 } else {
1297 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1298 }
1299 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1300 if enabled {
1301 enabledScopes[scope] = struct{}{}
1302 generatedScopes = append(generatedScopes, scope)
1303 }
1304 }
1305
1306 // Now check to make sure that any scope that is extended by an enabled scope is also
1307 // enabled.
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001308 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001309 if _, ok := enabledScopes[scope]; ok {
1310 extends := scope.extends
1311 if extends != nil {
1312 if _, ok := enabledScopes[extends]; !ok {
1313 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1314 }
1315 }
1316 }
1317 }
1318
1319 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001320}
1321
satayev758968a2021-12-06 11:42:40 +00001322var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1323
satayev8f088b02021-12-06 11:40:46 +00001324func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Jihoon Kanga3a05462024-04-05 00:36:44 +00001325 CheckMinSdkVersion(ctx, &module.Library)
1326}
1327
1328func CheckMinSdkVersion(ctx android.ModuleContext, module *Library) {
Colin Cross8bf14fc2024-09-25 16:41:31 -07001329 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.BaseModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001330 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
Colin Crossf7bbd2f2024-12-05 13:57:10 -08001331 isExternal := !android.IsDepInSameApex(ctx, module, child)
satayev8f088b02021-12-06 11:40:46 +00001332 if am, ok := child.(android.ApexModule); ok {
1333 if !do(ctx, parent, am, isExternal) {
1334 return false
1335 }
1336 }
1337 return !isExternal
1338 })
1339 })
1340}
1341
Paul Duffineedc5d52020-06-12 17:46:39 +01001342type sdkLibraryComponentTag struct {
1343 blueprint.BaseDependencyTag
1344 name string
1345}
1346
1347// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1348func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1349
1350var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001351
Jiyong Parke3833882020-02-17 17:28:10 +09001352func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001353 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001354 return dt == xmlPermissionsFileTag
1355 }
1356 return false
1357}
1358
Paul Duffineedc5d52020-06-12 17:46:39 +01001359var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001360
Jeongik Chaaaa6dcd2024-05-22 00:41:28 +09001361var _ android.InstallNeededDependencyTag = sdkLibraryComponentTag{}
1362
1363func (t sdkLibraryComponentTag) InstallDepNeeded() bool {
1364 return t.name == "xml-permissions-file" || t.name == "impl-library"
1365}
1366
Paul Duffin44f1d842020-06-26 20:17:02 +01001367// Add the dependencies on the child modules in the component deps mutator.
1368func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001369 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001370 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001371 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kangbd093452023-12-26 19:08:01 +00001372 ctx.AddVariationDependencies(nil, apiScope.everythingStubsTag, stubModuleName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001373
Jihoon Kangbd093452023-12-26 19:08:01 +00001374 exportableStubModuleName := module.exportableStubsLibraryModuleName(apiScope)
1375 ctx.AddVariationDependencies(nil, apiScope.exportableStubsTag, exportableStubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001376
Paul Duffin15f34ef2020-07-20 18:04:44 +01001377 // Add a dependency on the stubs source in order to access both stubs source and api information.
Jihoon Kang96ce83b2024-09-23 22:09:44 +00001378 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.droidstubsModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001379
1380 if module.compareAgainstLatestApi(apiScope) {
1381 // Add dependencies on the latest finalized version of the API .txt file.
1382 latestApiModuleName := module.latestApiModuleName(apiScope)
1383 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1384
1385 // Add dependencies on the latest finalized version of the remove API .txt file.
1386 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1387 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1388 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001389 }
1390
Paul Duffindfa131e2020-05-15 20:37:11 +01001391 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001392 // Add dependency to the rule for generating the implementation library.
1393 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1394
Paul Duffindfa131e2020-05-15 20:37:11 +01001395 if module.sharedLibrary() {
1396 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001397 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001398 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001399 }
1400}
Paul Duffine74ac732020-02-06 13:51:46 +00001401
Paul Duffin44f1d842020-06-26 20:17:02 +01001402// Add other dependencies as normal.
1403func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Jihoon Kange4a90172024-07-18 22:49:08 +00001404 // If the module does not create an implementation library or defaults to stubs,
1405 // mark the top level sdk library as stubs module as the module will provide stubs via
1406 // "magic" when listed as a dependency in the Android.bp files.
1407 notCreateImplLib := proptools.Bool(module.sdkLibraryProperties.Api_only)
1408 preferStubs := proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1409 module.properties.Is_stubs_module = proptools.BoolPtr(notCreateImplLib || preferStubs)
1410
Anton Hanssone77fccc2021-01-20 16:52:41 +00001411 var missingApiModules []string
1412 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1413 if apiScope.unstable {
1414 continue
1415 }
Paul Duffin958806b2022-05-16 13:10:47 +00001416 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001417 missingApiModules = append(missingApiModules, m)
1418 }
Paul Duffin958806b2022-05-16 13:10:47 +00001419 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001420 missingApiModules = append(missingApiModules, m)
1421 }
Paul Duffin958806b2022-05-16 13:10:47 +00001422 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001423 missingApiModules = append(missingApiModules, m)
1424 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001425 }
1426 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1427 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1428 m += "You need to do one of the following:\n"
1429 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1430 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1431 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1432 m += "\n"
1433 m += "The following filegroup modules are missing:\n "
1434 m += strings.Join(missingApiModules, "\n ") + "\n"
1435 m += "Please see the documentation of the prebuilt_apis module type (and a usage example in prebuilts/sdk) for a convenient way to generate these."
1436 ctx.ModuleErrorf(m)
1437 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001438}
1439
Inseob Kimc0907f12019-02-08 21:00:45 +09001440func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Das5ae65ee2024-04-16 22:03:26 +00001441 if disableSourceApexVariant(ctx) {
1442 // Prebuilts are active, do not create the installation rules for the source javalib.
1443 // Even though the source javalib is not used, we need to hide it to prevent duplicate installation rules.
1444 // TODO (b/331665856): Implement a principled solution for this.
1445 module.HideFromMake()
Spandan Das034af2c2024-10-30 21:45:09 +00001446 module.SkipInstall()
Spandan Das5ae65ee2024-04-16 22:03:26 +00001447 }
satayev8f088b02021-12-06 11:40:46 +00001448
Jihoon Kanga3a05462024-04-05 00:36:44 +00001449 module.stem = proptools.StringDefault(module.overridableProperties.Stem, ctx.ModuleName())
1450
1451 module.provideHiddenAPIPropertyInfo(ctx)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001452
Paul Duffinb97b1572021-04-29 21:50:40 +01001453 // Collate the components exported by this module. All scope specific modules are exported but
1454 // the impl and xml component modules are not.
1455 exportedComponents := map[string]struct{}{}
Yu Liu35acd332025-01-24 23:11:22 +00001456 var implLib android.ModuleProxy
Sundong Ahn57368eb2018-07-06 11:20:23 +09001457 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001458 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001459 // the recorded paths will be returned depending on the link type of the caller.
Yu Liu35acd332025-01-24 23:11:22 +00001460 ctx.VisitDirectDepsProxy(func(to android.ModuleProxy) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001461 tag := ctx.OtherModuleDependencyTag(to)
1462
Paul Duffinc8782502020-04-29 20:45:27 +01001463 // Extract information from any of the scope specific dependencies.
1464 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1465 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001466 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001467
1468 // Extract information from the dependency. The exact information extracted
1469 // is determined by the nature of the dependency which is determined by the tag.
1470 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001471
1472 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Jihoon Kang4b9220a2024-08-22 22:11:04 +00001473
1474 ctx.Phony(ctx.ModuleName(), scopePaths.stubsHeaderPath...)
Sundong Ahn20e998b2018-07-24 11:19:26 +09001475 }
Jihoon Kang8479dea2024-04-04 01:19:05 +00001476
1477 if tag == implLibraryTag {
1478 if dep, ok := android.OtherModuleProvider(ctx, to, JavaInfoProvider); ok {
1479 module.implLibraryHeaderJars = append(module.implLibraryHeaderJars, dep.HeaderJars...)
Yu Liu35acd332025-01-24 23:11:22 +00001480 module.implLibraryInfo = dep
1481 implLib = to
Jihoon Kang8479dea2024-04-04 01:19:05 +00001482 }
1483 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001484 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001485
Jihoon Kang98e9ac62024-09-25 23:42:30 +00001486 sdkLibInfo := module.generateCommonBuildActions(ctx)
Jihoon Kanga3a05462024-04-05 00:36:44 +00001487 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
1488 if !apexInfo.IsForPlatform() {
1489 module.hideApexVariantFromMake = true
1490 }
1491
Yu Liu35acd332025-01-24 23:11:22 +00001492 if module.implLibraryInfo != nil {
Jihoon Kanga3a05462024-04-05 00:36:44 +00001493 if ctx.Device() {
Yu Liu35acd332025-01-24 23:11:22 +00001494 module.classesJarPaths = module.implLibraryInfo.ImplementationJars
1495 module.bootDexJarPath = module.implLibraryInfo.BootDexJarPath
1496 module.uncompressDexState = module.implLibraryInfo.UncompressDexState
1497 module.active = module.implLibraryInfo.Active
Jihoon Kanga3a05462024-04-05 00:36:44 +00001498 }
1499
Yu Liu35acd332025-01-24 23:11:22 +00001500 module.outputFile = module.implLibraryInfo.OutputFile
1501 module.dexJarFile = makeDexJarPathFromPath(module.implLibraryInfo.DexJarFile.Path())
1502 module.headerJarFile = module.implLibraryInfo.HeaderJars[0]
1503 module.implementationAndResourcesJar = module.implLibraryInfo.ImplementationAndResourcesJars[0]
1504 module.builtInstalledForApex = module.implLibraryInfo.BuiltInstalledForApex
1505 module.dexpreopter.configPath = module.implLibraryInfo.ConfigPath
1506 module.dexpreopter.outputProfilePathOnHost = module.implLibraryInfo.OutputProfilePathOnHost
Jihoon Kanga3a05462024-04-05 00:36:44 +00001507
Jihoon Kang34155e32024-05-20 19:08:49 +00001508 // Properties required for Library.AndroidMkEntries
Yu Liu35acd332025-01-24 23:11:22 +00001509 module.logtagsSrcs = module.implLibraryInfo.LogtagsSrcs
1510 module.dexpreopter.builtInstalled = module.implLibraryInfo.BuiltInstalled
1511 module.jacocoReportClassesFile = module.implLibraryInfo.JacocoReportClassesFile
1512 module.dexer.proguardDictionary = module.implLibraryInfo.ProguardDictionary
1513 module.dexer.proguardUsageZip = module.implLibraryInfo.ProguardUsageZip
1514 module.linter.reports = module.implLibraryInfo.LinterReports
Colin Crossb79aa8f2024-09-25 15:41:01 -07001515
Yu Liu35acd332025-01-24 23:11:22 +00001516 if lintInfo, ok := android.OtherModuleProvider(ctx, implLib, LintProvider); ok {
Colin Crossb79aa8f2024-09-25 15:41:01 -07001517 android.SetProvider(ctx, LintProvider, lintInfo)
1518 }
Jihoon Kang34155e32024-05-20 19:08:49 +00001519
Jihoon Kanga3a05462024-04-05 00:36:44 +00001520 if !module.Host() {
Yu Liu35acd332025-01-24 23:11:22 +00001521 module.hostdexInstallFile = module.implLibraryInfo.HostdexInstallFile
Jihoon Kanga3a05462024-04-05 00:36:44 +00001522 }
1523
Yu Liu35acd332025-01-24 23:11:22 +00001524 if installFilesInfo, ok := android.OtherModuleProvider(ctx, implLib, android.InstallFilesProvider); ok {
Colin Crossa6182ab2024-08-21 10:47:44 -07001525 if installFilesInfo.CheckbuildTarget != nil {
1526 ctx.CheckbuildFile(installFilesInfo.CheckbuildTarget)
1527 }
1528 }
Jihoon Kanga3a05462024-04-05 00:36:44 +00001529 }
1530
Paul Duffinb97b1572021-04-29 21:50:40 +01001531 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001532 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001533 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001534
1535 // Provide additional information for inclusion in an sdk's generated .info file.
1536 additionalSdkInfo := map[string]interface{}{}
1537 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001538 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001539 scopes := map[string]interface{}{}
1540 additionalSdkInfo["scopes"] = scopes
1541 for scope, scopePaths := range module.scopePaths {
1542 scopeInfo := map[string]interface{}{}
1543 scopes[scope.name] = scopeInfo
1544 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1545 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
Jihoon Kang5623e542024-01-31 23:27:26 +00001546 if p := scopePaths.latestApiPaths; len(p) > 0 {
1547 // The last path in the list is the one that applies to this scope, the
1548 // preceding ones, if any, are for the scope(s) that it extends.
1549 scopeInfo["latest_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001550 }
Jihoon Kang5623e542024-01-31 23:27:26 +00001551 if p := scopePaths.latestRemovedApiPaths; len(p) > 0 {
1552 // The last path in the list is the one that applies to this scope, the
1553 // preceding ones, if any, are for the scope(s) that it extends.
1554 scopeInfo["latest_removed_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001555 }
1556 }
Colin Cross40213022023-12-13 15:19:49 -08001557 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
mrziwang9f7b9f42024-07-10 12:18:06 -07001558 module.setOutputFiles(ctx)
Jihoon Kang28c96572024-09-11 23:44:44 +00001559
1560 var generatingLibs []string
1561 for _, apiScope := range AllApiScopes {
1562 if _, ok := module.scopePaths[apiScope]; ok {
1563 generatingLibs = append(generatingLibs, module.stubsLibraryModuleName(apiScope))
1564 }
1565 }
1566
Yu Liu35acd332025-01-24 23:11:22 +00001567 if module.requiresRuntimeImplementationLibrary() && module.implLibraryInfo != nil {
Jihoon Kang28c96572024-09-11 23:44:44 +00001568 generatingLibs = append(generatingLibs, module.implLibraryModuleName())
Yu Liu35acd332025-01-24 23:11:22 +00001569 setOutputFilesFromJavaInfo(ctx, module.implLibraryInfo)
mrziwang9f7b9f42024-07-10 12:18:06 -07001570 }
Jihoon Kang28c96572024-09-11 23:44:44 +00001571
Jihoon Kang98e9ac62024-09-25 23:42:30 +00001572 sdkLibInfo.GeneratingLibs = generatingLibs
1573 android.SetProvider(ctx, SdkLibraryInfoProvider, sdkLibInfo)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001574}
1575
Yu Liu35acd332025-01-24 23:11:22 +00001576func setOutputFilesFromJavaInfo(ctx android.ModuleContext, info *JavaInfo) {
1577 ctx.SetOutputFiles(append(android.PathsIfNonNil(info.OutputFile), info.ExtraOutputFiles...), "")
1578 ctx.SetOutputFiles(android.PathsIfNonNil(info.OutputFile), android.DefaultDistTag)
1579 ctx.SetOutputFiles(info.ImplementationAndResourcesJars, ".jar")
1580 ctx.SetOutputFiles(info.HeaderJars, ".hjar")
1581 if info.ProguardDictionary.Valid() {
1582 ctx.SetOutputFiles(android.Paths{info.ProguardDictionary.Path()}, ".proguard_map")
1583 }
1584 ctx.SetOutputFiles(info.GeneratedSrcjars, ".generated_srcjars")
1585}
1586
Jihoon Kanga3a05462024-04-05 00:36:44 +00001587func (module *SdkLibrary) BuiltInstalledForApex() []dexpreopterInstall {
1588 return module.builtInstalledForApex
1589}
1590
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001591func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001592 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001593 return nil
1594 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001595 entriesList := module.Library.AndroidMkEntries()
Jihoon Kanga3a05462024-04-05 00:36:44 +00001596 entries := &entriesList[0]
1597 entries.Required = append(entries.Required, module.implLibraryModuleName())
Yo Chiang07d75072020-06-05 17:43:19 +08001598 if module.sharedLibrary() {
Yo Chiang07d75072020-06-05 17:43:19 +08001599 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1600 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001601 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001602}
1603
Anton Hansson5fd5d242020-03-27 19:43:19 +00001604// The dist path of the stub artifacts
1605func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001606 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001607}
1608
Paul Duffin12ceb462019-12-24 20:31:31 +00001609// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001610func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001611 scopeProperties := module.scopeToProperties[apiScope]
1612 if scopeProperties.Sdk_version != nil {
1613 return proptools.String(scopeProperties.Sdk_version)
1614 }
1615
Jiyong Parkf1691d22021-03-29 20:11:58 +09001616 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001617 if sdkDep.hasStandardLibs() {
1618 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001619 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001620 } else {
1621 // Otherwise, use no system module.
1622 return "none"
1623 }
1624}
1625
Paul Duffin31310252020-11-20 21:26:20 +00001626func (module *SdkLibrary) distStem() string {
1627 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1628}
1629
Colin Cross986b69a2021-06-01 13:13:40 -07001630// distGroup returns the subdirectory of the dist path of the stub artifacts.
1631func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001632 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001633}
1634
Paul Duffin958806b2022-05-16 13:10:47 +00001635func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1636 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1637}
1638
Jihoon Kang748a24d2024-03-20 21:29:39 +00001639func latestPrebuiltApiCombinedModuleName(name string, apiScope *apiScope) string {
1640 return PrebuiltApiCombinedModuleName(name, apiScope.name, "latest")
1641}
1642
Paul Duffind1b3a922020-01-22 11:57:20 +00001643func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001644 return ":" + module.latestApiModuleName(apiScope)
1645}
1646
1647func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001648 return latestPrebuiltApiCombinedModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001649}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001650
Paul Duffind1b3a922020-01-22 11:57:20 +00001651func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001652 return ":" + module.latestRemovedApiModuleName(apiScope)
1653}
1654
1655func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001656 return latestPrebuiltApiCombinedModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001657}
1658
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001659func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001660 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1661}
1662
1663func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1664 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001665}
1666
Jihoon Kang0c705a42023-08-02 06:44:57 +00001667// The listed modules' stubs contents do not match the corresponding txt files,
1668// but require additional api contributions to generate the full stubs.
1669// This method returns the name of the additional api contribution module
1670// for corresponding sdk_library modules.
1671func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1672 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
Jihoon Kangb0f4c022024-08-06 00:15:25 +00001673 return val
Jihoon Kang0c705a42023-08-02 06:44:57 +00001674 }
1675 return ""
1676}
1677
Anton Hansson944e77d2020-08-19 11:40:22 +01001678func childModuleVisibility(childVisibility []string) []string {
1679 if childVisibility == nil {
1680 // No child visibility set. The child will use the visibility of the sdk_library.
1681 return nil
1682 }
1683
1684 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1685 var visibility []string
1686 visibility = append(visibility, "//visibility:override")
1687 visibility = append(visibility, childVisibility...)
1688 return visibility
1689}
1690
Paul Duffin958806b2022-05-16 13:10:47 +00001691func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
1692 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
1693}
1694
Paul Duffinea8f8082021-06-24 13:25:57 +01001695// Implements android.ApexModule
Colin Crossf7bbd2f2024-12-05 13:57:10 -08001696func (module *SdkLibrary) OutgoingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
Jooyung Han5e9013b2020-03-10 06:23:13 +09001697 if depTag == xmlPermissionsFileTag {
1698 return true
1699 }
Colin Crossf7bbd2f2024-12-05 13:57:10 -08001700 if depTag == implLibraryTag {
Jihoon Kanga3a05462024-04-05 00:36:44 +00001701 return true
1702 }
Colin Crossf7bbd2f2024-12-05 13:57:10 -08001703 return module.Library.OutgoingDepIsInSameApex(depTag)
Jooyung Han5e9013b2020-03-10 06:23:13 +09001704}
1705
Paul Duffinea8f8082021-06-24 13:25:57 +01001706// Implements android.ApexModule
1707func (module *SdkLibrary) UniqueApexVariations() bool {
1708 return module.uniqueApexVariations()
1709}
1710
Jihoon Kangb0f4c022024-08-06 00:15:25 +00001711func (module *SdkLibrary) ModuleBuildFromTextStubs() bool {
1712 return proptools.BoolDefault(module.sdkLibraryProperties.Build_from_text_stub, true)
Jihoon Kang80456fd2023-11-15 19:22:14 +00001713}
1714
Colin Cross571cccf2019-02-04 11:22:08 -08001715var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1716
Jiyong Park82484c02018-04-23 21:41:26 +09001717func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001718 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001719 return &[]string{}
1720 }).(*[]string)
1721}
1722
Paul Duffin749f98f2019-12-30 17:23:46 +00001723func (module *SdkLibrary) getApiDir() string {
1724 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1725}
1726
Jiyong Parkc678ad32018-04-10 13:07:10 +09001727// For a java_sdk_library module, create internal modules for stubs, docs,
1728// runtime libs and xml file. If requested, the stubs and docs are created twice
1729// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001730func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
Paul Duffina18abc22020-05-16 18:54:24 +01001731 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001732 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001733 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001734 }
1735
Paul Duffin37e0b772019-12-30 17:20:10 +00001736 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001737 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001738 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00001739 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001740 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001741
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001742 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09001743
Paul Duffin3375e352020-04-28 10:44:03 +01001744 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001745
Paul Duffin749f98f2019-12-30 17:23:46 +00001746 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001747 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001748 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001749 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001750 p := android.ExistentPathForSource(mctx, path)
1751 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07001752 if mctx.Config().AllowMissingDependencies() {
1753 mctx.AddMissingDependencies([]string{path})
1754 } else {
1755 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1756 missingCurrentApi = true
1757 }
Inseob Kim8098faa2019-03-18 10:19:51 +09001758 }
1759 }
1760 }
1761
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001762 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09001763 script := "build/soong/scripts/gen-java-current-api-files.sh"
1764 p := android.ExistentPathForSource(mctx, script)
1765
1766 if !p.Valid() {
1767 panic(fmt.Sprintf("script file %s doesn't exist", script))
1768 }
1769
1770 mctx.ModuleErrorf("One or more current api files are missing. "+
1771 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001772 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001773 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001774 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001775 return
1776 }
1777
Paul Duffin3375e352020-04-28 10:44:03 +01001778 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001779 // Use the stubs source name for legacy reasons.
Jihoon Kang96ce83b2024-09-23 22:09:44 +00001780 module.createDroidstubs(mctx, scope, module.droidstubsModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001781
Jihoon Kang96ce83b2024-09-23 22:09:44 +00001782 module.createFromSourceStubsLibrary(mctx, scope)
1783 module.createExportableFromSourceStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001784
Jihoon Kangb0f4c022024-08-06 00:15:25 +00001785 if mctx.Config().BuildFromTextStub() && module.ModuleBuildFromTextStubs() {
1786 module.createApiLibrary(mctx, scope)
Jihoon Kang0c705a42023-08-02 06:44:57 +00001787 }
Jihoon Kangb0f4c022024-08-06 00:15:25 +00001788 module.createTopLevelStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001789 module.createTopLevelExportableStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001790 }
1791
Paul Duffindfa131e2020-05-15 20:37:11 +01001792 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001793 // Create child module to create an implementation library.
1794 //
1795 // This temporarily creates a second implementation library that can be explicitly
1796 // referenced.
1797 //
1798 // TODO(b/156618935) - update comment once only one implementation library is created.
1799 module.createImplLibrary(mctx)
1800
Paul Duffindfa131e2020-05-15 20:37:11 +01001801 // Only create an XML permissions file that declares the library as being usable
1802 // as a shared library if required.
1803 if module.sharedLibrary() {
1804 module.createXmlFile(mctx)
1805 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001806
1807 // record java_sdk_library modules so that they are exported to make
1808 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1809 javaSdkLibrariesLock.Lock()
1810 defer javaSdkLibrariesLock.Unlock()
1811 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1812 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01001813
Paul Duffin77590a82022-04-28 14:13:30 +00001814 // Add the impl_only_libs and impl_only_static_libs *after* we're done using them in submodules.
Anton Hansson7f66efa2020-10-08 14:47:23 +01001815 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Cole Faustb7493472024-08-28 11:55:52 -07001816 module.properties.Static_libs.AppendSimpleValue(module.sdkLibraryProperties.Impl_only_static_libs)
Inseob Kimc0907f12019-02-08 21:00:45 +09001817}
1818
1819func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07001820 module.addHostAndDeviceProperties()
1821 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001822
Paul Duffin71b33cc2021-06-23 11:39:47 +01001823 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001824
Paul Duffina18abc22020-05-16 18:54:24 +01001825 module.properties.Installable = proptools.BoolPtr(true)
1826 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001827}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001828
Paul Duffindfa131e2020-05-15 20:37:11 +01001829func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1830 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1831}
1832
Jihoon Kangfa3f0782024-08-21 20:42:18 +00001833func moduleStubLinkType(j *Module) (stub bool, ret sdkLinkType) {
1834 kind := android.ToSdkKind(proptools.String(j.properties.Stub_contributing_api))
1835 switch kind {
1836 case android.SdkPublic:
Anton Hansson2d0c1942020-05-25 12:20:51 +01001837 return true, javaSdk
Jihoon Kangfa3f0782024-08-21 20:42:18 +00001838 case android.SdkSystem:
Anton Hansson2d0c1942020-05-25 12:20:51 +01001839 return true, javaSystem
Jihoon Kangfa3f0782024-08-21 20:42:18 +00001840 case android.SdkModule:
Anton Hansson2d0c1942020-05-25 12:20:51 +01001841 return true, javaModule
Jihoon Kangfa3f0782024-08-21 20:42:18 +00001842 case android.SdkTest:
Anton Hansson2d0c1942020-05-25 12:20:51 +01001843 return true, javaSystem
Jihoon Kangfa3f0782024-08-21 20:42:18 +00001844 case android.SdkSystemServer:
Jihoon Kang1147b312023-06-08 23:25:57 +00001845 return true, javaSystemServer
Jihoon Kangfa3f0782024-08-21 20:42:18 +00001846 // Default value for all modules other than java_sdk_library-generated stub submodules
1847 case android.SdkInvalid:
1848 return false, javaPlatform
1849 default:
1850 panic(fmt.Sprintf("stub_contributing_api set as an unsupported sdk kind %s", kind.String()))
Jihoon Kang1147b312023-06-08 23:25:57 +00001851 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01001852}
1853
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001854// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1855// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1856// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1857// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1858// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001859func SdkLibraryFactory() android.Module {
1860 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001861
1862 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01001863 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01001864
Inseob Kimc0907f12019-02-08 21:00:45 +09001865 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001866 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001867 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001868
1869 // Initialize the map from scope to scope specific properties.
1870 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001871 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001872 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1873 }
1874 module.scopeToProperties = scopeToProperties
1875
Paul Duffin4911a892020-04-29 23:35:13 +01001876 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01001877 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01001878 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1879 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1880
Paul Duffin1b1e8062020-05-08 13:44:43 +01001881 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001882 // If no implementation is required then it cannot be used as a shared library
1883 // either.
1884 if !module.requiresRuntimeImplementationLibrary() {
1885 // If shared_library has been explicitly set to true then it is incompatible
1886 // with api_only: true.
1887 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1888 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1889 }
1890 // Set shared_library: false.
1891 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1892 }
1893
Jihoon Kang98e9ac62024-09-25 23:42:30 +00001894 if module.initCommonAfterDefaultsApplied() {
Paul Duffin1b1e8062020-05-08 13:44:43 +01001895 module.CreateInternalModules(ctx)
1896 }
1897 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001898 return module
1899}
Colin Cross79c7c262019-04-17 11:11:46 -07001900
1901//
1902// SDK library prebuilts
1903//
1904
Paul Duffin56d44902020-01-31 13:36:25 +00001905// Properties associated with each api scope.
1906type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001907 Jars []string `android:"path"`
1908
1909 Sdk_version *string
1910
Colin Cross79c7c262019-04-17 11:11:46 -07001911 // List of shared java libs that this module has dependencies to
1912 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001913
Paul Duffinc8782502020-04-29 20:45:27 +01001914 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001915 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001916
1917 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001918 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001919
1920 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001921 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01001922
1923 // Annotation zip
1924 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001925}
1926
Paul Duffin56d44902020-01-31 13:36:25 +00001927type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001928 // List of shared java libs, common to all scopes, that this module has
1929 // dependencies to
1930 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01001931
1932 // If set to true, compile dex files for the stubs. Defaults to false.
1933 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01001934
1935 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01001936 Permitted_packages []string
Spandan Das23956d12024-01-19 00:22:22 +00001937
1938 // Name of the source soong module that gets shadowed by this prebuilt
1939 // If unspecified, follows the naming convention that the source module of
1940 // the prebuilt is Name() without "prebuilt_" prefix
1941 Source_module_name *string
Paul Duffin56d44902020-01-31 13:36:25 +00001942}
1943
Paul Duffineedc5d52020-06-12 17:46:39 +01001944type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001945 android.ModuleBase
1946 android.DefaultableModuleBase
1947 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001948 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07001949
Paul Duffin37856732021-02-26 14:24:15 +00001950 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00001951 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00001952
Colin Cross79c7c262019-04-17 11:11:46 -07001953 properties sdkLibraryImportProperties
1954
Paul Duffin46a26a82020-04-07 19:27:04 +01001955 // Map from api scope to the scope specific property structure.
1956 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1957
Paul Duffin56d44902020-01-31 13:36:25 +00001958 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01001959
Jeongik Chad5fe8782021-07-08 01:13:11 +09001960 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00001961 dexJarFile OptionalDexJarPath
1962 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09001963
1964 // Expected install file path of the source module(sdk_library)
1965 // or dex implementation jar obtained from the prebuilt_apex, if any.
1966 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07001967}
1968
Paul Duffin46a26a82020-04-07 19:27:04 +01001969// The type of a structure that contains a field of type sdkLibraryScopeProperties
1970// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07001971//
1972// struct {
1973// Public sdkLibraryScopeProperties
1974// System sdkLibraryScopeProperties
1975// ...
1976// }
Paul Duffin46a26a82020-04-07 19:27:04 +01001977var allScopeStructType = createAllScopePropertiesStructType()
1978
1979// Dynamically create a structure type for each apiscope in allApiScopes.
1980func createAllScopePropertiesStructType() reflect.Type {
1981 var fields []reflect.StructField
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001982 for _, apiScope := range AllApiScopes {
Paul Duffin46a26a82020-04-07 19:27:04 +01001983 field := reflect.StructField{
1984 Name: apiScope.fieldName,
1985 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1986 }
1987 fields = append(fields, field)
1988 }
1989
1990 return reflect.StructOf(fields)
1991}
1992
1993// Create an instance of the scope specific structure type and return a map
1994// from apiscope to a pointer to each scope specific field.
1995func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1996 allScopePropertiesPtr := reflect.New(allScopeStructType)
1997 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1998 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1999
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002000 for _, apiScope := range AllApiScopes {
Paul Duffin46a26a82020-04-07 19:27:04 +01002001 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2002 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2003 }
2004
2005 return allScopePropertiesPtr.Interface(), scopeProperties
2006}
2007
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002008// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002009func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002010 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002011
Paul Duffin46a26a82020-04-07 19:27:04 +01002012 allScopeProperties, scopeToProperties := createPropertiesInstance()
2013 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002014 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002015
Paul Duffinc3091c82020-05-08 14:16:20 +01002016 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002017 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002018
Paul Duffin0bdcb272020-02-06 15:24:57 +00002019 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002020 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002021 InitJavaModule(module, android.HostAndDeviceSupported)
2022
Paul Duffin1b1e8062020-05-08 13:44:43 +01002023 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
Jihoon Kang98e9ac62024-09-25 23:42:30 +00002024 if module.initCommonAfterDefaultsApplied() {
Paul Duffin1b1e8062020-05-08 13:44:43 +01002025 module.createInternalModules(mctx)
2026 }
2027 })
Colin Cross79c7c262019-04-17 11:11:46 -07002028 return module
2029}
2030
Paul Duffin630b11e2021-07-15 13:35:26 +01002031var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2032
2033func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2034 return module.properties.Permitted_packages
2035}
2036
Paul Duffineedc5d52020-06-12 17:46:39 +01002037func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002038 return &module.prebuilt
2039}
2040
Paul Duffineedc5d52020-06-12 17:46:39 +01002041func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002042 return module.prebuilt.Name(module.ModuleBase.Name())
2043}
2044
Spandan Das23956d12024-01-19 00:22:22 +00002045func (module *SdkLibraryImport) BaseModuleName() string {
2046 return proptools.StringDefault(module.properties.Source_module_name, module.ModuleBase.Name())
2047}
2048
Paul Duffineedc5d52020-06-12 17:46:39 +01002049func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002050
Paul Duffin50061512020-01-21 16:31:05 +00002051 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002052 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002053 module.prebuilt.ForcePrefer()
2054 }
2055
Paul Duffin46a26a82020-04-07 19:27:04 +01002056 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002057 if len(scopeProperties.Jars) == 0 {
2058 continue
2059 }
2060
Paul Duffinbbb546b2020-04-09 00:07:11 +01002061 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002062
Paul Duffin0f8faff2020-05-20 16:18:00 +01002063 if len(scopeProperties.Stub_srcs) > 0 {
2064 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2065 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002066
2067 if scopeProperties.Current_api != nil {
2068 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2069 }
Paul Duffin56d44902020-01-31 13:36:25 +00002070 }
Colin Cross79c7c262019-04-17 11:11:46 -07002071
2072 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2073 javaSdkLibrariesLock.Lock()
2074 defer javaSdkLibrariesLock.Unlock()
2075 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2076}
2077
Paul Duffin44f1d842020-06-26 20:17:02 +01002078// Add the dependencies on the child module in the component deps mutator so that it
2079// creates references to the prebuilt and not the source modules.
2080func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002081 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002082 if len(scopeProperties.Jars) == 0 {
2083 continue
2084 }
2085
2086 // Add dependencies to the prebuilt stubs library
Jihoon Kangb7431552024-01-22 19:40:08 +00002087 ctx.AddVariationDependencies(nil, apiScope.prebuiltStubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002088
2089 if len(scopeProperties.Stub_srcs) > 0 {
2090 // Add dependencies to the prebuilt stubs source library
Jihoon Kang96ce83b2024-09-23 22:09:44 +00002091 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.droidstubsModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002092 }
Paul Duffin56d44902020-01-31 13:36:25 +00002093 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002094}
2095
2096// Add other dependencies as normal.
2097func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002098
2099 implName := module.implLibraryModuleName()
2100 if ctx.OtherModuleExists(implName) {
2101 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2102
2103 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2104 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2105 // Add dependency to the rule for generating the xml permissions file
2106 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2107 }
2108 }
Colin Cross79c7c262019-04-17 11:11:46 -07002109}
2110
Jiyong Park45bf82e2020-12-15 22:29:02 +09002111var _ android.ApexModule = (*SdkLibraryImport)(nil)
2112
2113// Implements android.ApexModule
Colin Crossf7bbd2f2024-12-05 13:57:10 -08002114func (module *SdkLibraryImport) OutgoingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01002115 if depTag == xmlPermissionsFileTag {
2116 return true
2117 }
2118
2119 // None of the other dependencies of the java_sdk_library_import are in the same apex
2120 // as the one that references this module.
2121 return false
2122}
2123
Jiyong Park45bf82e2020-12-15 22:29:02 +09002124// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002125func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2126 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002127 // we don't check prebuilt modules for sdk_version
2128 return nil
2129}
2130
Paul Duffinea8f8082021-06-24 13:25:57 +01002131// Implements android.ApexModule
2132func (module *SdkLibraryImport) UniqueApexVariations() bool {
2133 return module.uniqueApexVariations()
2134}
2135
Paul Duffin09817d62022-04-28 17:45:11 +01002136// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002137func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2138 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002139}
2140
2141var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2142
Paul Duffineedc5d52020-06-12 17:46:39 +01002143func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002144 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2145 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2146
Paul Duffin0f8faff2020-05-20 16:18:00 +01002147 // Record the paths to the prebuilt stubs library and stubs source.
Yu Liu35acd332025-01-24 23:11:22 +00002148 ctx.VisitDirectDepsProxy(func(to android.ModuleProxy) {
Colin Cross79c7c262019-04-17 11:11:46 -07002149 tag := ctx.OtherModuleDependencyTag(to)
2150
Paul Duffin0f8faff2020-05-20 16:18:00 +01002151 // Extract information from any of the scope specific dependencies.
2152 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2153 apiScope := scopeTag.apiScope
2154 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2155
2156 // Extract information from the dependency. The exact information extracted
2157 // is determined by the nature of the dependency which is determined by the tag.
2158 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002159 } else if tag == implLibraryTag {
Yu Liu35acd332025-01-24 23:11:22 +00002160 if implInfo, ok := android.OtherModuleProvider(ctx, to, JavaInfoProvider); ok {
2161 module.implLibraryInfo = implInfo
Paul Duffineedc5d52020-06-12 17:46:39 +01002162 } else {
2163 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2164 }
Colin Cross79c7c262019-04-17 11:11:46 -07002165 }
2166 })
Jihoon Kang98e9ac62024-09-25 23:42:30 +00002167 sdkLibInfo := module.generateCommonBuildActions(ctx)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002168
2169 // Populate the scope paths with information from the properties.
2170 for apiScope, scopeProperties := range module.scopeProperties {
2171 if len(scopeProperties.Jars) == 0 {
2172 continue
2173 }
2174
2175 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002176 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002177 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2178 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2179 }
Paul Duffin39853512021-02-26 11:09:39 +00002180
2181 if ctx.Device() {
Spandan Dasa326b322024-09-19 21:02:52 +00002182 // Shared libraries deapexed from prebuilt apexes are no longer supported.
2183 // Set the dexJarBuildPath to a fake path.
2184 // This allows soong analysis pass, but will be an error during ninja execution if there are
2185 // any rdeps.
Colin Crossff694a82023-12-13 15:54:49 -08002186 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00002187 if ai.ForPrebuiltApex {
Spandan Dasa326b322024-09-19 21:02:52 +00002188 module.dexJarFile = makeDexJarPathFromPath(android.PathForModuleInstall(ctx, "intentionally_no_longer_supported"))
2189 module.initHiddenAPI(ctx, module.dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Paul Duffin39853512021-02-26 11:09:39 +00002190 }
2191 }
mrziwang9f7b9f42024-07-10 12:18:06 -07002192
Jihoon Kang28c96572024-09-11 23:44:44 +00002193 var generatingLibs []string
2194 for _, apiScope := range AllApiScopes {
2195 if scopeProperties, ok := module.scopeProperties[apiScope]; ok {
2196 if len(scopeProperties.Jars) == 0 {
2197 continue
2198 }
2199 generatingLibs = append(generatingLibs, module.stubsLibraryModuleName(apiScope))
2200 }
2201 }
2202
mrziwang9f7b9f42024-07-10 12:18:06 -07002203 module.setOutputFiles(ctx)
Yu Liu35acd332025-01-24 23:11:22 +00002204 if module.implLibraryInfo != nil {
Jihoon Kang28c96572024-09-11 23:44:44 +00002205 generatingLibs = append(generatingLibs, module.implLibraryModuleName())
Yu Liu35acd332025-01-24 23:11:22 +00002206 setOutputFilesFromJavaInfo(ctx, module.implLibraryInfo)
mrziwang9f7b9f42024-07-10 12:18:06 -07002207 }
Jihoon Kang28c96572024-09-11 23:44:44 +00002208
Jihoon Kang98e9ac62024-09-25 23:42:30 +00002209 sdkLibInfo.GeneratingLibs = generatingLibs
2210 android.SetProvider(ctx, SdkLibraryInfoProvider, sdkLibInfo)
Colin Cross79c7c262019-04-17 11:11:46 -07002211}
2212
Jihoon Kang98e9ac62024-09-25 23:42:30 +00002213var _ UsesLibraryDependency = (*SdkLibraryImport)(nil)
2214
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002215// to satisfy UsesLibraryDependency interface
Spandan Das59a4a2b2024-01-09 21:35:56 +00002216func (module *SdkLibraryImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002217 // The dex implementation jar extracted from the .apex file should be used in preference to the
2218 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00002219 if module.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002220 ctx.ModuleErrorf(module.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00002221 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002222 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002223 return module.dexJarFile
2224 }
Yu Liu35acd332025-01-24 23:11:22 +00002225 if module.implLibraryInfo == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002226 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002227 } else {
Yu Liu35acd332025-01-24 23:11:22 +00002228 return module.implLibraryInfo.DexJarFile
Paul Duffineedc5d52020-06-12 17:46:39 +01002229 }
2230}
2231
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002232// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002233func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002234 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002235}
2236
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002237// to satisfy UsesLibraryDependency interface
2238func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2239 return nil
2240}
2241
Paul Duffineedc5d52020-06-12 17:46:39 +01002242// to satisfy apex.javaDependency interface
2243func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
Yu Liu35acd332025-01-24 23:11:22 +00002244 if module.implLibraryInfo == nil {
Paul Duffineedc5d52020-06-12 17:46:39 +01002245 return nil
2246 } else {
Yu Liu35acd332025-01-24 23:11:22 +00002247 return module.implLibraryInfo.JacocoReportClassesFile
Paul Duffineedc5d52020-06-12 17:46:39 +01002248 }
2249}
2250
2251// to satisfy apex.javaDependency interface
2252func (module *SdkLibraryImport) Stem() string {
2253 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002254}
Jiyong Parke3833882020-02-17 17:28:10 +09002255
Paul Duffin44b481b2020-06-17 16:59:43 +01002256var _ ApexDependency = (*SdkLibraryImport)(nil)
2257
2258// to satisfy java.ApexDependency interface
2259func (module *SdkLibraryImport) HeaderJars() android.Paths {
Yu Liu35acd332025-01-24 23:11:22 +00002260 if module.implLibraryInfo == nil {
Paul Duffin44b481b2020-06-17 16:59:43 +01002261 return nil
2262 } else {
Yu Liu35acd332025-01-24 23:11:22 +00002263 return module.implLibraryInfo.HeaderJars
Paul Duffin44b481b2020-06-17 16:59:43 +01002264 }
2265}
2266
2267// to satisfy java.ApexDependency interface
2268func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
Yu Liu35acd332025-01-24 23:11:22 +00002269 if module.implLibraryInfo == nil {
Paul Duffin44b481b2020-06-17 16:59:43 +01002270 return nil
2271 } else {
Yu Liu35acd332025-01-24 23:11:22 +00002272 return module.implLibraryInfo.ImplementationAndResourcesJars
Paul Duffin44b481b2020-06-17 16:59:43 +01002273 }
2274}
2275
Jiakai Zhang204356f2021-09-09 08:12:46 +00002276// to satisfy java.DexpreopterInterface interface
2277func (module *SdkLibraryImport) IsInstallable() bool {
2278 return true
2279}
2280
Paul Duffinfef55002021-06-17 14:56:05 +01002281var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2282
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002283func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002284 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08002285 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01002286}
2287
Spandan Das2ea84dd2024-01-25 22:12:50 +00002288func (j *SdkLibraryImport) UseProfileGuidedDexpreopt() bool {
2289 return proptools.Bool(j.importDexpreoptProperties.Dex_preopt.Profile_guided)
2290}
2291
Paul Duffindd46f712020-02-10 13:37:10 +00002292type sdkLibrarySdkMemberType struct {
2293 android.SdkMemberTypeBase
2294}
2295
Paul Duffin296701e2021-07-14 10:29:36 +01002296func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
2297 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00002298}
2299
2300func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2301 _, ok := module.(*SdkLibrary)
2302 return ok
2303}
2304
2305func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2306 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2307}
2308
2309func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2310 return &sdkLibrarySdkMemberProperties{}
2311}
2312
Paul Duffin976b0e52021-04-27 23:20:26 +01002313var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
2314 android.SdkMemberTypeBase{
2315 PropertyName: "java_sdk_libs",
2316 SupportsSdk: true,
2317 },
2318}
2319
Paul Duffindd46f712020-02-10 13:37:10 +00002320type sdkLibrarySdkMemberProperties struct {
2321 android.SdkMemberPropertiesBase
2322
Paul Duffine8409952022-09-22 16:24:46 +01002323 // Stem name for files in the sdk snapshot.
2324 //
2325 // This is used to construct the path names of various sdk library files in the sdk snapshot to
2326 // make sure that they match the finalized versions of those files in prebuilts/sdk.
2327 //
2328 // This property is marked as keep so that it will be kept in all instances of this struct, will
2329 // not be cleared but will be copied to common structs. That is needed because this field is used
2330 // to construct many file names for other parts of this struct and so it needs to be present in
2331 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
2332 // be unavailable for generating file names if there were other properties that were still set.
2333 Stem string `sdk:"keep"`
2334
Paul Duffindd46f712020-02-10 13:37:10 +00002335 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00002336 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00002337
Paul Duffin3d1248c2020-04-09 00:10:17 +01002338 // The Java stubs source files.
2339 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01002340
2341 // The naming scheme.
2342 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01002343
2344 // True if the java_sdk_library_import is for a shared library, false
2345 // otherwise.
2346 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01002347
Paul Duffin1267d872021-04-16 17:21:36 +01002348 // True if the stub imports should produce dex jars.
2349 Compile_dex *bool
2350
Paul Duffina2ae7e02020-09-11 11:55:00 +01002351 // The paths to the doctag files to add to the prebuilt.
2352 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01002353
2354 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002355
2356 // Signals that this shared library is part of the bootclasspath starting
2357 // on the version indicated in this attribute.
2358 //
2359 // This will make platforms at this level and above to ignore
2360 // <uses-library> tags with this library name because the library is already
2361 // available
2362 On_bootclasspath_since *string
2363
2364 // Signals that this shared library was part of the bootclasspath before
2365 // (but not including) the version indicated in this attribute.
2366 //
2367 // The system will automatically add a <uses-library> tag with this library to
2368 // apps that target any SDK less than the version indicated in this attribute.
2369 On_bootclasspath_before *string
2370
2371 // Indicates that PackageManager should ignore this shared library if the
2372 // platform is below the version indicated in this attribute.
2373 //
2374 // This means that the device won't recognise this library as installed.
2375 Min_device_sdk *string
2376
2377 // Indicates that PackageManager should ignore this shared library if the
2378 // platform is above the version indicated in this attribute.
2379 //
2380 // This means that the device won't recognise this library as installed.
2381 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002382
2383 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00002384}
2385
2386type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01002387 Jars android.Paths
2388 StubsSrcJar android.Path
2389 CurrentApiFile android.Path
2390 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00002391 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002392 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00002393}
2394
2395func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2396 sdk := variant.(*SdkLibrary)
2397
Paul Duffine8409952022-09-22 16:24:46 +01002398 // Copy the stem name for files in the sdk snapshot.
2399 s.Stem = sdk.distStem()
2400
Paul Duffin106a3a42022-01-27 16:39:06 +00002401 s.Scopes = make(map[*apiScope]*scopeProperties)
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002402 for _, apiScope := range AllApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01002403 paths := sdk.findScopePaths(apiScope)
2404 if paths == nil {
2405 continue
2406 }
2407
Paul Duffindd46f712020-02-10 13:37:10 +00002408 jars := paths.stubsImplPath
2409 if len(jars) > 0 {
2410 properties := scopeProperties{}
2411 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01002412 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002413 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01002414 if paths.currentApiFilePath.Valid() {
2415 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2416 }
2417 if paths.removedApiFilePath.Valid() {
2418 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2419 }
Anton Hanssond78eb762021-09-21 15:25:12 +01002420 // The annotations zip is only available for modules that set annotations_enabled: true.
2421 if paths.annotationsZip.Valid() {
2422 properties.AnnotationsZip = paths.annotationsZip.Path()
2423 }
Paul Duffin106a3a42022-01-27 16:39:06 +00002424 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00002425 }
2426 }
2427
Paul Duffind7eb1c22020-05-26 20:57:10 +01002428 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01002429 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01002430 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01002431 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002432 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
2433 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
2434 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
2435 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002436
Yu Liu35acd332025-01-24 23:11:22 +00002437 if sdk.implLibraryInfo != nil && sdk.implLibraryInfo.ProfileGuided {
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002438 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
2439 }
Paul Duffindd46f712020-02-10 13:37:10 +00002440}
2441
2442func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01002443 if s.Naming_scheme != nil {
2444 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2445 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01002446 if s.Shared_library != nil {
2447 propertySet.AddProperty("shared_library", *s.Shared_library)
2448 }
Paul Duffin1267d872021-04-16 17:21:36 +01002449 if s.Compile_dex != nil {
2450 propertySet.AddProperty("compile_dex", *s.Compile_dex)
2451 }
Paul Duffin869de142021-07-15 14:14:41 +01002452 if len(s.Permitted_packages) > 0 {
2453 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
2454 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002455 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
2456 if s.DexPreoptProfileGuided != nil {
2457 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
2458 }
Paul Duffinf7a64332020-05-13 16:54:55 +01002459
Paul Duffine8409952022-09-22 16:24:46 +01002460 stem := s.Stem
2461
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002462 for _, apiScope := range AllApiScopes {
Paul Duffindd46f712020-02-10 13:37:10 +00002463 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01002464 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00002465
Paul Duffin958806b2022-05-16 13:10:47 +00002466 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01002467
Paul Duffindd46f712020-02-10 13:37:10 +00002468 var jars []string
2469 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01002470 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00002471 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2472 jars = append(jars, dest)
2473 }
2474 scopeSet.AddProperty("jars", jars)
2475
Paul Duffin22628d52021-05-12 23:13:22 +01002476 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
2477 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01002478 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01002479 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
2480 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
2481 } else {
2482 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2483 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01002484 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01002485 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2486 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2487 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01002488
Paul Duffin1fd005d2020-04-09 01:08:11 +01002489 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01002490 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01002491 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2492 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2493 }
2494
2495 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01002496 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01002497 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01002498 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2499 }
2500
Anton Hanssond78eb762021-09-21 15:25:12 +01002501 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01002502 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01002503 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
2504 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
2505 }
2506
Paul Duffindd46f712020-02-10 13:37:10 +00002507 if properties.SdkVersion != "" {
2508 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2509 }
2510 }
2511 }
2512
Paul Duffina2ae7e02020-09-11 11:55:00 +01002513 if len(s.Doctag_paths) > 0 {
2514 dests := []string{}
2515 for _, p := range s.Doctag_paths {
2516 dest := filepath.Join("doctags", p.Rel())
2517 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2518 dests = append(dests, dest)
2519 }
2520 propertySet.AddProperty("doctag_files", dests)
2521 }
Paul Duffindd46f712020-02-10 13:37:10 +00002522}