blob: 155bea4d7afbb00f818064691e3125a380b0d5ab [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 Duffin3375e352020-04-28 10:44:03 +0100483}
484
Jiyong Parkc678ad32018-04-10 13:07:10 +0900485type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100486 // List of source files that are needed to compile the API, but are not part of runtime library.
487 Api_srcs []string `android:"arch_variant"`
488
Paul Duffin5df79302020-05-16 15:52:12 +0100489 // Visibility for impl library module. If not specified then defaults to the
490 // visibility property.
491 Impl_library_visibility []string
492
Paul Duffin4911a892020-04-29 23:35:13 +0100493 // Visibility for stubs library modules. If not specified then defaults to the
494 // visibility property.
495 Stubs_library_visibility []string
496
497 // Visibility for stubs source modules. If not specified then defaults to the
498 // visibility property.
499 Stubs_source_visibility []string
500
Anton Hansson7f66efa2020-10-08 14:47:23 +0100501 // List of Java libraries that will be in the classpath when building the implementation lib
502 Impl_only_libs []string `android:"arch_variant"`
503
Paul Duffin77590a82022-04-28 14:13:30 +0000504 // List of Java libraries that will included in the implementation lib.
505 Impl_only_static_libs []string `android:"arch_variant"`
506
Sundong Ahnf043cf62018-06-25 16:04:37 +0900507 // List of Java libraries that will be in the classpath when building stubs
508 Stub_only_libs []string `android:"arch_variant"`
509
Anton Hanssondae54cd2021-04-21 16:30:10 +0100510 // List of Java libraries that will included in stub libraries
511 Stub_only_static_libs []string `android:"arch_variant"`
512
Paul Duffin7a586d32019-12-30 17:09:34 +0000513 // list of package names that will be documented and publicized as API.
514 // This allows the API to be restricted to a subset of the source files provided.
515 // If this is unspecified then all the source files will be treated as being part
516 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900517 Api_packages []string
518
Paul Duffin749f98f2019-12-30 17:23:46 +0000519 // the relative path to the directory containing the api specification files.
520 // Defaults to "api".
521 Api_dir *string
522
Paul Duffindfa131e2020-05-15 20:37:11 +0100523 // Determines whether a runtime implementation library is built; defaults to false.
524 //
525 // 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 +0200526 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000527 Api_only *bool
528
Paul Duffin11512472019-02-11 15:55:17 +0000529 // local files that are used within user customized droiddoc options.
530 Droiddoc_option_files []string
531
Spandan Das93e95992021-07-29 18:26:39 +0000532 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000533 // Available variables for substitution:
534 //
535 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900536 Droiddoc_options []string
537
Paul Duffine22c2ab2020-05-20 19:35:27 +0100538 // is set to true, Metalava will allow framework SDK to contain annotations.
539 Annotations_enabled *bool
540
Sundong Ahn054b19a2018-10-19 13:46:09 +0900541 // a list of top-level directories containing files to merge qualifier annotations
542 // (i.e. those intended to be included in the stubs written) from.
543 Merge_annotations_dirs []string
544
545 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
546 Merge_inclusion_annotations_dirs []string
547
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000548 // If set to true then don't create dist rules.
549 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900550
Paul Duffin31310252020-11-20 21:26:20 +0000551 // The stem for the artifacts that are copied to the dist, if not specified
552 // then defaults to the base module name.
553 //
554 // For each scope the following artifacts are copied to the apistubs/<scope>
555 // directory in the dist.
556 // * stubs impl jar -> <dist-stem>.jar
557 // * API specification file -> api/<dist-stem>.txt
558 // * Removed API specification file -> api/<dist-stem>-removed.txt
559 //
560 // Also used to construct the name of the filegroup (created by prebuilt_apis)
561 // that references the latest released API and remove API specification files.
562 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
563 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800564 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000565 Dist_stem *string
566
Colin Cross986b69a2021-06-01 13:13:40 -0700567 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700568 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700569 // in the public Android SDK.
570 Dist_group *string
571
Anton Hanssondff2c782020-12-21 17:10:01 +0000572 // A compatibility mode that allows historical API-tracking files to not exist.
573 // Do not use.
574 Unsafe_ignore_missing_latest_api bool
575
Paul Duffin3375e352020-04-28 10:44:03 +0100576 // indicates whether system and test apis should be generated.
577 Generate_system_and_test_apis bool `blueprint:"mutated"`
578
579 // The properties specific to the public api scope
580 //
581 // Unless explicitly specified by using public.enabled the public api scope is
582 // enabled by default in both legacy and non-legacy mode.
583 Public ApiScopeProperties
584
585 // The properties specific to the system api scope
586 //
587 // In legacy mode the system api scope is enabled by default when sdk_version
588 // is set to something other than "none".
589 //
590 // In non-legacy mode the system api scope is disabled by default.
591 System ApiScopeProperties
592
593 // The properties specific to the test api scope
594 //
595 // In legacy mode the test api scope is enabled by default when sdk_version
596 // is set to something other than "none".
597 //
598 // In non-legacy mode the test api scope is disabled by default.
599 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000600
Paul Duffin0c5bae52020-06-02 13:00:08 +0100601 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100602 //
Zi Wangb2179e32023-01-31 15:53:30 -0800603 // Unless explicitly specified by using module_lib.enabled the module_lib api
604 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100605 Module_lib ApiScopeProperties
606
Paul Duffin0c5bae52020-06-02 13:00:08 +0100607 // The properties specific to the system-server api scope
608 //
Zi Wangb2179e32023-01-31 15:53:30 -0800609 // Unless explicitly specified by using system_server.enabled the
610 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100611 System_server ApiScopeProperties
612
Jiyong Park932cdfe2020-05-28 00:19:53 +0900613 // Determines if the stubs are preferred over the implementation library
614 // for linking, even when the client doesn't specify sdk_version. When this
615 // is set to true, such clients are provided with the widest API surface that
616 // this lib provides. Note however that this option doesn't affect the clients
617 // that are in the same APEX as this library. In that case, the clients are
618 // always linked with the implementation library. Default is false.
619 Default_to_stubs *bool
620
Paul Duffin160fe412020-05-10 19:32:20 +0100621 // Properties related to api linting.
622 Api_lint struct {
623 // Enable api linting.
624 Enabled *bool
Anton Hanssonfd1c0d22023-11-02 15:18:09 +0000625
626 // If API lint is enabled, this flag controls whether a set of legitimate lint errors
627 // are turned off. The default is true.
628 Legacy_errors_allowed *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100629 }
630
Jihoon Kang6592e872023-12-19 01:13:16 +0000631 // a list of aconfig_declarations module names that the stubs generated in this module
632 // depend on.
633 Aconfig_declarations []string
634
Jihoon Kang48e2ac92024-07-29 21:18:46 +0000635 // Determines if the module generates the stubs from the api signature files
636 // instead of the source Java files. Defaults to true.
637 Build_from_text_stub *bool
638
Jiyong Parkc678ad32018-04-10 13:07:10 +0900639 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100640 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900641}
642
Paul Duffin0f8faff2020-05-20 16:18:00 +0100643// Paths to outputs from java_sdk_library and java_sdk_library_import.
644//
645// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
646// OptionalPaths are always set by java_sdk_library but may not be set by
647// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000648type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100649 // The path (represented as Paths for convenience when returning) to the stubs header jar.
650 //
651 // That is the jar that is created by turbine.
652 stubsHeaderPath android.Paths
653
654 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
655 //
656 // This is not the implementation jar, it still only contains stubs.
657 stubsImplPath android.Paths
658
Paul Duffin1267d872021-04-16 17:21:36 +0100659 // The dex jar for the stubs.
660 //
661 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100662 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100663
Jihoon Kangbd093452023-12-26 19:08:01 +0000664 // The exportable dex jar for the stubs.
665 // This is not the implementation jar, it still only contains stubs.
666 // Includes unflagged apis and flagged apis enabled by release configurations.
667 exportableStubsDexJarPath OptionalDexJarPath
668
Paul Duffin0f8faff2020-05-20 16:18:00 +0100669 // The API specification file, e.g. system_current.txt.
670 currentApiFilePath android.OptionalPath
671
672 // The specification of API elements removed since the last release.
673 removedApiFilePath android.OptionalPath
674
675 // The stubs source jar.
676 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100677
678 // Extracted annotations.
679 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000680
681 // The path to the latest API file.
Jihoon Kang5623e542024-01-31 23:27:26 +0000682 latestApiPaths android.Paths
Paul Duffin958806b2022-05-16 13:10:47 +0000683
684 // The path to the latest removed API file.
Jihoon Kang5623e542024-01-31 23:27:26 +0000685 latestRemovedApiPaths android.Paths
Paul Duffind1b3a922020-01-22 11:57:20 +0000686}
687
Colin Crossdcf71b22021-02-01 13:59:03 -0800688func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Colin Cross313aa542023-12-13 13:47:44 -0800689 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800690 paths.stubsHeaderPath = lib.HeaderJars
691 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100692
693 libDep := dep.(UsesLibraryDependency)
Spandan Das59a4a2b2024-01-09 21:35:56 +0000694 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
Jihoon Kangbd093452023-12-26 19:08:01 +0000695 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
696 return nil
697 } else {
698 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
699 }
700}
701
702func (paths *scopePaths) extractEverythingStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
703 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
704 paths.stubsHeaderPath = lib.HeaderJars
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000705 if !ctx.Config().ReleaseHiddenApiExportableStubs() {
706 paths.stubsImplPath = lib.ImplementationJars
707 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000708
709 libDep := dep.(UsesLibraryDependency)
710 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
711 return nil
712 } else {
713 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
714 }
715}
716
717func (paths *scopePaths) extractExportableStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000718 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
719 if ctx.Config().ReleaseHiddenApiExportableStubs() {
720 paths.stubsImplPath = lib.ImplementationJars
721 }
722
Jihoon Kangbd093452023-12-26 19:08:01 +0000723 libDep := dep.(UsesLibraryDependency)
724 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
Paul Duffinc8782502020-04-29 20:45:27 +0100725 return nil
726 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800727 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100728 }
729}
730
Jihoon Kangee113282024-01-23 00:16:41 +0000731func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider) error) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100732 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000733 err := action(apiStubsProvider)
734 if err != nil {
735 return err
736 }
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000737 return nil
738 } else {
739 return fmt.Errorf("expected module that implements ExportableApiStubsSrcProvider, e.g. droidstubs")
740 }
741}
742
Jihoon Kangee113282024-01-23 00:16:41 +0000743func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider) error) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100744 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000745 err := action(apiStubsProvider)
746 if err != nil {
747 return err
748 }
Paul Duffin0f8faff2020-05-20 16:18:00 +0100749 return nil
750 } else {
751 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
752 }
753}
754
Jihoon Kangee113282024-01-23 00:16:41 +0000755func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider, stubsType StubsType) error {
756 var annotationsZip, currentApiFilePath, removedApiFilePath android.Path
757 annotationsZip, annotationsZipErr := provider.AnnotationsZip(stubsType)
758 currentApiFilePath, currentApiFilePathErr := provider.ApiFilePath(stubsType)
759 removedApiFilePath, removedApiFilePathErr := provider.RemovedApiFilePath(stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100760
Jihoon Kangee113282024-01-23 00:16:41 +0000761 combinedError := errors.Join(annotationsZipErr, currentApiFilePathErr, removedApiFilePathErr)
762
763 if combinedError == nil {
764 paths.annotationsZip = android.OptionalPathForPath(annotationsZip)
765 paths.currentApiFilePath = android.OptionalPathForPath(currentApiFilePath)
766 paths.removedApiFilePath = android.OptionalPathForPath(removedApiFilePath)
767 }
768 return combinedError
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000769}
770
Jihoon Kangee113282024-01-23 00:16:41 +0000771func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider, stubsType StubsType) error {
772 stubsSrcJar, err := provider.StubsSrcJar(stubsType)
773 if err == nil {
774 paths.stubsSrcJar = android.OptionalPathForPath(stubsSrcJar)
775 }
776 return err
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000777}
778
Colin Crossdcf71b22021-02-01 13:59:03 -0800779func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000780 stubsType := Everything
781 if ctx.Config().ReleaseHiddenApiExportableStubs() {
782 stubsType = Exportable
783 }
Jihoon Kangee113282024-01-23 00:16:41 +0000784 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000785 return paths.extractStubsSourceInfoFromApiStubsProviders(provider, stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100786 })
787}
788
Colin Crossdcf71b22021-02-01 13:59:03 -0800789func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000790 stubsType := Everything
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000791 if ctx.Config().ReleaseHiddenApiExportableStubs() {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000792 stubsType = Exportable
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000793 }
Jihoon Kangee113282024-01-23 00:16:41 +0000794 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000795 extractApiInfoErr := paths.extractApiInfoFromApiStubsProvider(provider, stubsType)
796 extractStubsSourceInfoErr := paths.extractStubsSourceInfoFromApiStubsProviders(provider, stubsType)
Jihoon Kangee113282024-01-23 00:16:41 +0000797 return errors.Join(extractApiInfoErr, extractStubsSourceInfoErr)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100798 })
799}
800
Jihoon Kang5623e542024-01-31 23:27:26 +0000801func extractOutputPaths(dep android.Module) (android.Paths, error) {
Paul Duffin958806b2022-05-16 13:10:47 +0000802 var paths android.Paths
803 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
804 paths = sourceFileProducer.Srcs()
Jihoon Kang5623e542024-01-31 23:27:26 +0000805 return paths, nil
Paul Duffin958806b2022-05-16 13:10:47 +0000806 } else {
Jihoon Kang5623e542024-01-31 23:27:26 +0000807 return nil, fmt.Errorf("module %q does not produce source files", dep)
Paul Duffin958806b2022-05-16 13:10:47 +0000808 }
Paul Duffin958806b2022-05-16 13:10:47 +0000809}
810
811func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang5623e542024-01-31 23:27:26 +0000812 outputPaths, err := extractOutputPaths(dep)
813 paths.latestApiPaths = outputPaths
Paul Duffin958806b2022-05-16 13:10:47 +0000814 return err
815}
816
817func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang5623e542024-01-31 23:27:26 +0000818 outputPaths, err := extractOutputPaths(dep)
819 paths.latestRemovedApiPaths = outputPaths
Paul Duffin958806b2022-05-16 13:10:47 +0000820 return err
821}
822
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100823type commonToSdkLibraryAndImportProperties struct {
Paul Duffindfa131e2020-05-15 20:37:11 +0100824 // Specifies whether this module can be used as an Android shared library; defaults
825 // to true.
826 //
827 // An Android shared library is one that can be referenced in a <uses-library> element
828 // in an AndroidManifest.xml.
829 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100830
831 // Files containing information about supported java doc tags.
832 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000833
834 // Signals that this shared library is part of the bootclasspath starting
835 // on the version indicated in this attribute.
836 //
837 // This will make platforms at this level and above to ignore
838 // <uses-library> tags with this library name because the library is already
839 // available
840 On_bootclasspath_since *string
841
842 // Signals that this shared library was part of the bootclasspath before
843 // (but not including) the version indicated in this attribute.
844 //
845 // The system will automatically add a <uses-library> tag with this library to
846 // apps that target any SDK less than the version indicated in this attribute.
847 On_bootclasspath_before *string
848
849 // Indicates that PackageManager should ignore this shared library if the
850 // platform is below the version indicated in this attribute.
851 //
852 // This means that the device won't recognise this library as installed.
853 Min_device_sdk *string
854
855 // Indicates that PackageManager should ignore this shared library if the
856 // platform is above the version indicated in this attribute.
857 //
858 // This means that the device won't recognise this library as installed.
859 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100860}
861
Paul Duffin71b33cc2021-06-23 11:39:47 +0100862// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
863// embeds the commonToSdkLibraryAndImport struct.
864type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000865 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100866
Spandan Das23956d12024-01-19 00:22:22 +0000867 // Returns the name of the root java_sdk_library that creates the child stub libraries
868 // This is the `name` as it appears in Android.bp, and not the name in Soong's build graph
869 // (with the prebuilt_ prefix)
870 //
871 // e.g. in the following java_sdk_library_import
872 // java_sdk_library_import {
873 // name: "framework-foo.v1",
874 // source_module_name: "framework-foo",
875 // }
876 // the values returned by
877 // 1. Name(): prebuilt_framework-foo.v1 # unique
878 // 2. BaseModuleName(): framework-foo # the source
879 // 3. RootLibraryName: framework-foo.v1 # the undecordated `name` from Android.bp
880 RootLibraryName() string
881}
882
883func (m *SdkLibrary) RootLibraryName() string {
884 return m.BaseModuleName()
885}
886
887func (m *SdkLibraryImport) RootLibraryName() string {
888 // m.BaseModuleName refers to the source of the import
889 // use moduleBase.Name to get the name of the module as it appears in the .bp file
890 return m.ModuleBase.Name()
Paul Duffin71b33cc2021-06-23 11:39:47 +0100891}
892
Paul Duffin56d44902020-01-31 13:36:25 +0000893// Common code between sdk library and sdk library import
894type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100895 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100896
Paul Duffin56d44902020-01-31 13:36:25 +0000897 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100898
Paul Duffindfa131e2020-05-15 20:37:11 +0100899 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100900
Paul Duffina2ae7e02020-09-11 11:55:00 +0100901 // Paths to commonSdkLibraryProperties.Doctag_files
902 doctagPaths android.Paths
903
Paul Duffin859fe962020-05-15 10:20:31 +0100904 // Functionality related to this being used as a component of a java_sdk_library.
905 EmbeddableSdkLibraryComponent
Jihoon Kang8479dea2024-04-04 01:19:05 +0000906
907 // Path to the header jars of the implementation library
908 // This is non-empty only when api_only is false.
909 implLibraryHeaderJars android.Paths
Jihoon Kanga3a05462024-04-05 00:36:44 +0000910
911 // The reference to the implementation library created by the source module.
912 // Is nil if the source module does not exist.
913 implLibraryModule *Library
Paul Duffin56d44902020-01-31 13:36:25 +0000914}
915
Paul Duffin71b33cc2021-06-23 11:39:47 +0100916func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
917 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100918
Paul Duffin71b33cc2021-06-23 11:39:47 +0100919 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100920
921 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100922 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100923}
924
Jihoon Kang98e9ac62024-09-25 23:42:30 +0000925func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied() bool {
Spandan Das23956d12024-01-19 00:22:22 +0000926 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +0100927 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
928
Paul Duffindfa131e2020-05-15 20:37:11 +0100929 // Only track this sdk library if this can be used as a shared library.
930 if c.sharedLibrary() {
931 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100932 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100933 }
Paul Duffin859fe962020-05-15 10:20:31 +0100934
Paul Duffin1b1e8062020-05-08 13:44:43 +0100935 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100936}
937
Paul Duffinea8f8082021-06-24 13:25:57 +0100938// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
939// method.
940func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
941 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
942 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
943 // the APEX and so it needs a unique variation per APEX.
944 return c.sharedLibrary()
945}
946
Jihoon Kang98e9ac62024-09-25 23:42:30 +0000947func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) SdkLibraryInfo {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100948 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
Paul Duffina2ae7e02020-09-11 11:55:00 +0100949
Jihoon Kang98e9ac62024-09-25 23:42:30 +0000950 everythingStubPaths := make(map[android.SdkKind]OptionalDexJarPath)
951 exportableStubPaths := make(map[android.SdkKind]OptionalDexJarPath)
952 removedApiFilePaths := make(map[android.SdkKind]android.OptionalPath)
953 for kind := android.SdkNone; kind <= android.SdkPrivate; kind += 1 {
954 everythingStubPath := makeUnsetDexJarPath()
955 exportableStubPath := makeUnsetDexJarPath()
956 removedApiFilePath := android.OptionalPath{}
957 if scopePath := c.findClosestScopePath(sdkKindToApiScope(kind)); scopePath != nil {
958 everythingStubPath = scopePath.stubsDexJarPath
959 exportableStubPath = scopePath.exportableStubsDexJarPath
960 removedApiFilePath = scopePath.removedApiFilePath
961 }
962 everythingStubPaths[kind] = everythingStubPath
963 exportableStubPaths[kind] = exportableStubPath
964 removedApiFilePaths[kind] = removedApiFilePath
965 }
966
Yu Liu460cf372025-01-10 00:34:06 +0000967 javaInfo := &JavaInfo{}
968 setExtraJavaInfo(ctx, ctx.Module(), javaInfo)
969 android.SetProvider(ctx, JavaInfoProvider, javaInfo)
970
Jihoon Kang98e9ac62024-09-25 23:42:30 +0000971 return SdkLibraryInfo{
972 EverythingStubDexJarPaths: everythingStubPaths,
973 ExportableStubDexJarPaths: exportableStubPaths,
974 RemovedTxtFiles: removedApiFilePaths,
975 SharedLibrary: c.sharedLibrary(),
976 }
Jihoon Kanga3a05462024-04-05 00:36:44 +0000977}
978
Paul Duffin46dc45a2020-05-14 15:39:10 +0100979// The component names for different outputs of the java_sdk_library.
980//
981// They are similar to the names used for the child modules it creates
982const (
983 stubsSourceComponentName = "stubs.source"
984
985 apiTxtComponentName = "api.txt"
986
987 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +0100988
989 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +0100990)
991
mrziwang9f7b9f42024-07-10 12:18:06 -0700992func (module *commonToSdkLibraryAndImport) setOutputFiles(ctx android.ModuleContext) {
993 if module.doctagPaths != nil {
994 ctx.SetOutputFiles(module.doctagPaths, ".doctags")
995 }
996 for _, scopeName := range android.SortedKeys(scopeByName) {
997 paths := module.findScopePaths(scopeByName[scopeName])
998 if paths == nil {
999 continue
Paul Duffin46dc45a2020-05-14 15:39:10 +01001000 }
mrziwang9f7b9f42024-07-10 12:18:06 -07001001 componentToOutput := map[string]android.OptionalPath{
1002 stubsSourceComponentName: paths.stubsSrcJar,
1003 apiTxtComponentName: paths.currentApiFilePath,
1004 removedApiTxtComponentName: paths.removedApiFilePath,
1005 annotationsComponentName: paths.annotationsZip,
1006 }
1007 for _, component := range android.SortedKeys(componentToOutput) {
1008 if componentToOutput[component].Valid() {
1009 ctx.SetOutputFiles(android.Paths{componentToOutput[component].Path()}, "."+scopeName+"."+component)
Paul Duffina2ae7e02020-09-11 11:55:00 +01001010 }
1011 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001012 }
1013}
1014
Paul Duffin803a9562020-05-20 11:52:25 +01001015func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001016 if c.scopePaths == nil {
1017 c.scopePaths = make(map[*apiScope]*scopePaths)
1018 }
1019 paths := c.scopePaths[scope]
1020 if paths == nil {
1021 paths = &scopePaths{}
1022 c.scopePaths[scope] = paths
1023 }
1024
1025 return paths
1026}
1027
Paul Duffin803a9562020-05-20 11:52:25 +01001028func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1029 if c.scopePaths == nil {
1030 return nil
1031 }
1032
1033 return c.scopePaths[scope]
1034}
1035
1036// If this does not support the requested api scope then find the closest available
1037// scope it does support. Returns nil if no such scope is available.
1038func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001039 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001040 if paths := c.findScopePaths(s); paths != nil {
1041 return paths
1042 }
1043 }
1044
1045 // This should never happen outside tests as public should be the base scope for every
1046 // scope and is enabled by default.
1047 return nil
1048}
1049
Paul Duffin32cf58a2021-05-18 16:32:50 +01001050// sdkKindToApiScope maps from android.SdkKind to apiScope.
1051func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1052 var apiScope *apiScope
1053 switch kind {
1054 case android.SdkSystem:
1055 apiScope = apiScopeSystem
1056 case android.SdkModule:
1057 apiScope = apiScopeModuleLib
1058 case android.SdkTest:
1059 apiScope = apiScopeTest
1060 case android.SdkSystemServer:
1061 apiScope = apiScopeSystemServer
1062 default:
1063 apiScope = apiScopePublic
1064 }
1065 return apiScope
1066}
1067
Paul Duffin859fe962020-05-15 10:20:31 +01001068func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1069 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001070 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001071 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001072 }{}
1073
Spandan Das23956d12024-01-19 00:22:22 +00001074 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +01001075 componentProps.SdkLibraryName = namePtr
1076
Paul Duffindfa131e2020-05-15 20:37:11 +01001077 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001078 // Mark the stubs library as being components of this java_sdk_library so that
1079 // any app that includes code which depends (directly or indirectly) on the stubs
1080 // library will have the appropriate <uses-library> invocation inserted into its
1081 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001082 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001083 }
1084
1085 return componentProps
1086}
1087
Paul Duffindfa131e2020-05-15 20:37:11 +01001088func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1089 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1090}
1091
Paul Duffinf4600f62021-05-13 22:34:45 +01001092// Check if the stub libraries should be compiled for dex
1093func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1094 // Always compile the dex file files for the stub libraries if they will be used on the
1095 // bootclasspath.
1096 return !c.sharedLibrary()
1097}
1098
Paul Duffin859fe962020-05-15 10:20:31 +01001099// Properties related to the use of a module as an component of a java_sdk_library.
1100type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001101 // The name of the java_sdk_library/_import module.
1102 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001103
1104 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1105 // in the AndroidManifest.xml of any Android app that includes code that references
1106 // this module. If not set then no java_sdk_library/_import is tracked.
1107 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1108}
1109
1110// Structure to be embedded in a module struct that needs to support the
1111// SdkLibraryComponentDependency interface.
1112type EmbeddableSdkLibraryComponent struct {
1113 sdkLibraryComponentProperties SdkLibraryComponentProperties
1114}
1115
Paul Duffin71b33cc2021-06-23 11:39:47 +01001116func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1117 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001118}
1119
1120// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001121func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1122 return e.sdkLibraryComponentProperties.SdkLibraryName
1123}
1124
1125// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001126func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001127 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1128 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1129 // run-time library and the corresponding module that provides the implementation. This name is
1130 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1131 // in dexpreopt).
1132 //
1133 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1134 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001135 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1136}
1137
Paul Duffin859fe962020-05-15 10:20:31 +01001138// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1139// (including the java_sdk_library) itself.
1140type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001141 UsesLibraryDependency
1142
Paul Duffin3f0290e2021-06-30 18:25:36 +01001143 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1144 SdkLibraryName() *string
1145
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001146 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1147 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001148}
1149
1150// Make sure that all the module types that are components of java_sdk_library/_import
1151// and which can be referenced (directly or indirectly) from an android app implement
1152// the SdkLibraryComponentDependency interface.
1153var _ SdkLibraryComponentDependency = (*Library)(nil)
1154var _ SdkLibraryComponentDependency = (*Import)(nil)
1155var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001156var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001157
Jihoon Kang28c96572024-09-11 23:44:44 +00001158type SdkLibraryInfo struct {
1159 // GeneratingLibs is the names of the library modules that this sdk library
1160 // generates. Note that this only includes the name of the modules that other modules can
1161 // depend on, and is not a holistic list of generated modules.
1162 GeneratingLibs []string
Jihoon Kang98e9ac62024-09-25 23:42:30 +00001163
1164 // Map of sdk kind to the dex jar for the "everything" stubs.
1165 // It is needed by the hiddenapi processing tool which processes dex files.
1166 EverythingStubDexJarPaths map[android.SdkKind]OptionalDexJarPath
1167
1168 // Map of sdk kind to the dex jar for the "exportable" stubs.
1169 // It is needed by the hiddenapi processing tool which processes dex files.
1170 ExportableStubDexJarPaths map[android.SdkKind]OptionalDexJarPath
1171
1172 // Map of sdk kind to the optional path to the removed.txt file.
1173 RemovedTxtFiles map[android.SdkKind]android.OptionalPath
1174
1175 // Whether if this can be used as a shared library.
1176 SharedLibrary bool
Jihoon Kang28c96572024-09-11 23:44:44 +00001177}
1178
1179var SdkLibraryInfoProvider = blueprint.NewProvider[SdkLibraryInfo]()
1180
1181func getGeneratingLibs(ctx android.ModuleContext, sdkVersion android.SdkSpec, sdkLibraryModuleName string, sdkInfo SdkLibraryInfo) []string {
1182 apiLevel := sdkVersion.ApiLevel
1183 if apiLevel.IsPreview() {
1184 return sdkInfo.GeneratingLibs
1185 }
1186
1187 generatingPrebuilts := []string{}
1188 for _, apiScope := range AllApiScopes {
1189 scopePrebuiltModuleName := prebuiltApiModuleName("sdk", sdkLibraryModuleName, apiScope.name, apiLevel.String())
1190 if ctx.OtherModuleExists(scopePrebuiltModuleName) {
1191 generatingPrebuilts = append(generatingPrebuilts, scopePrebuiltModuleName)
1192 }
1193 }
1194 return generatingPrebuilts
1195}
1196
Inseob Kimc0907f12019-02-08 21:00:45 +09001197type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001198 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001199
Sundong Ahn054b19a2018-10-19 13:46:09 +09001200 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001201
Paul Duffin3375e352020-04-28 10:44:03 +01001202 // Map from api scope to the scope specific property structure.
1203 scopeToProperties map[*apiScope]*ApiScopeProperties
1204
Paul Duffin56d44902020-01-31 13:36:25 +00001205 commonToSdkLibraryAndImport
Jihoon Kanga3a05462024-04-05 00:36:44 +00001206
1207 builtInstalledForApex []dexpreopterInstall
Jiyong Parkc678ad32018-04-10 13:07:10 +09001208}
1209
Paul Duffin3375e352020-04-28 10:44:03 +01001210func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1211 return module.sdkLibraryProperties.Generate_system_and_test_apis
1212}
1213
Jihoon Kang98e9ac62024-09-25 23:42:30 +00001214var _ UsesLibraryDependency = (*SdkLibrary)(nil)
1215
1216// To satisfy the UsesLibraryDependency interface
Jihoon Kanga3a05462024-04-05 00:36:44 +00001217func (module *SdkLibrary) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
1218 if module.implLibraryModule != nil {
1219 return module.implLibraryModule.DexJarBuildPath(ctx)
1220 }
1221 return makeUnsetDexJarPath()
1222}
1223
Jihoon Kang98e9ac62024-09-25 23:42:30 +00001224// To satisfy the UsesLibraryDependency interface
Jihoon Kanga3a05462024-04-05 00:36:44 +00001225func (module *SdkLibrary) DexJarInstallPath() android.Path {
1226 if module.implLibraryModule != nil {
1227 return module.implLibraryModule.DexJarInstallPath()
1228 }
1229 return nil
1230}
1231
Paul Duffin3375e352020-04-28 10:44:03 +01001232func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1233 // Check to see if any scopes have been explicitly enabled. If any have then all
1234 // must be.
1235 anyScopesExplicitlyEnabled := false
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001236 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001237 scopeProperties := module.scopeToProperties[scope]
1238 if scopeProperties.Enabled != nil {
1239 anyScopesExplicitlyEnabled = true
1240 break
1241 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001242 }
Paul Duffin3375e352020-04-28 10:44:03 +01001243
1244 var generatedScopes apiScopes
1245 enabledScopes := make(map[*apiScope]struct{})
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001246 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001247 scopeProperties := module.scopeToProperties[scope]
1248 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1249 // This is to ensure that any new usages of this module type do not rely on legacy
1250 // behaviour.
1251 defaultEnabledStatus := false
1252 if anyScopesExplicitlyEnabled {
1253 defaultEnabledStatus = scope.defaultEnabledStatus
1254 } else {
1255 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1256 }
1257 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1258 if enabled {
1259 enabledScopes[scope] = struct{}{}
1260 generatedScopes = append(generatedScopes, scope)
1261 }
1262 }
1263
1264 // Now check to make sure that any scope that is extended by an enabled scope is also
1265 // enabled.
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001266 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001267 if _, ok := enabledScopes[scope]; ok {
1268 extends := scope.extends
1269 if extends != nil {
1270 if _, ok := enabledScopes[extends]; !ok {
1271 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1272 }
1273 }
1274 }
1275 }
1276
1277 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001278}
1279
satayev758968a2021-12-06 11:42:40 +00001280var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1281
satayev8f088b02021-12-06 11:40:46 +00001282func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Jihoon Kanga3a05462024-04-05 00:36:44 +00001283 CheckMinSdkVersion(ctx, &module.Library)
1284}
1285
1286func CheckMinSdkVersion(ctx android.ModuleContext, module *Library) {
Colin Cross8bf14fc2024-09-25 16:41:31 -07001287 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.BaseModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001288 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
Colin Crossf7bbd2f2024-12-05 13:57:10 -08001289 isExternal := !android.IsDepInSameApex(ctx, module, child)
satayev8f088b02021-12-06 11:40:46 +00001290 if am, ok := child.(android.ApexModule); ok {
1291 if !do(ctx, parent, am, isExternal) {
1292 return false
1293 }
1294 }
1295 return !isExternal
1296 })
1297 })
1298}
1299
Paul Duffineedc5d52020-06-12 17:46:39 +01001300type sdkLibraryComponentTag struct {
1301 blueprint.BaseDependencyTag
1302 name string
1303}
1304
1305// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1306func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1307
1308var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001309
Jiyong Parke3833882020-02-17 17:28:10 +09001310func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001311 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001312 return dt == xmlPermissionsFileTag
1313 }
1314 return false
1315}
1316
Paul Duffineedc5d52020-06-12 17:46:39 +01001317var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001318
Jeongik Chaaaa6dcd2024-05-22 00:41:28 +09001319var _ android.InstallNeededDependencyTag = sdkLibraryComponentTag{}
1320
1321func (t sdkLibraryComponentTag) InstallDepNeeded() bool {
1322 return t.name == "xml-permissions-file" || t.name == "impl-library"
1323}
1324
Paul Duffin44f1d842020-06-26 20:17:02 +01001325// Add the dependencies on the child modules in the component deps mutator.
1326func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001327 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001328 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001329 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kangbd093452023-12-26 19:08:01 +00001330 ctx.AddVariationDependencies(nil, apiScope.everythingStubsTag, stubModuleName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001331
Jihoon Kangbd093452023-12-26 19:08:01 +00001332 exportableStubModuleName := module.exportableStubsLibraryModuleName(apiScope)
1333 ctx.AddVariationDependencies(nil, apiScope.exportableStubsTag, exportableStubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001334
Paul Duffin15f34ef2020-07-20 18:04:44 +01001335 // Add a dependency on the stubs source in order to access both stubs source and api information.
Jihoon Kang96ce83b2024-09-23 22:09:44 +00001336 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.droidstubsModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001337
1338 if module.compareAgainstLatestApi(apiScope) {
1339 // Add dependencies on the latest finalized version of the API .txt file.
1340 latestApiModuleName := module.latestApiModuleName(apiScope)
1341 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1342
1343 // Add dependencies on the latest finalized version of the remove API .txt file.
1344 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1345 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1346 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001347 }
1348
Paul Duffindfa131e2020-05-15 20:37:11 +01001349 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001350 // Add dependency to the rule for generating the implementation library.
1351 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1352
Paul Duffindfa131e2020-05-15 20:37:11 +01001353 if module.sharedLibrary() {
1354 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001355 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001356 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001357 }
1358}
Paul Duffine74ac732020-02-06 13:51:46 +00001359
Paul Duffin44f1d842020-06-26 20:17:02 +01001360// Add other dependencies as normal.
1361func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Jihoon Kange4a90172024-07-18 22:49:08 +00001362 // If the module does not create an implementation library or defaults to stubs,
1363 // mark the top level sdk library as stubs module as the module will provide stubs via
1364 // "magic" when listed as a dependency in the Android.bp files.
1365 notCreateImplLib := proptools.Bool(module.sdkLibraryProperties.Api_only)
1366 preferStubs := proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1367 module.properties.Is_stubs_module = proptools.BoolPtr(notCreateImplLib || preferStubs)
1368
Anton Hanssone77fccc2021-01-20 16:52:41 +00001369 var missingApiModules []string
1370 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1371 if apiScope.unstable {
1372 continue
1373 }
Paul Duffin958806b2022-05-16 13:10:47 +00001374 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001375 missingApiModules = append(missingApiModules, m)
1376 }
Paul Duffin958806b2022-05-16 13:10:47 +00001377 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001378 missingApiModules = append(missingApiModules, m)
1379 }
Paul Duffin958806b2022-05-16 13:10:47 +00001380 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001381 missingApiModules = append(missingApiModules, m)
1382 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001383 }
1384 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1385 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1386 m += "You need to do one of the following:\n"
1387 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1388 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1389 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1390 m += "\n"
1391 m += "The following filegroup modules are missing:\n "
1392 m += strings.Join(missingApiModules, "\n ") + "\n"
1393 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."
1394 ctx.ModuleErrorf(m)
1395 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001396}
1397
Inseob Kimc0907f12019-02-08 21:00:45 +09001398func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Das5ae65ee2024-04-16 22:03:26 +00001399 if disableSourceApexVariant(ctx) {
1400 // Prebuilts are active, do not create the installation rules for the source javalib.
1401 // Even though the source javalib is not used, we need to hide it to prevent duplicate installation rules.
1402 // TODO (b/331665856): Implement a principled solution for this.
1403 module.HideFromMake()
Spandan Das034af2c2024-10-30 21:45:09 +00001404 module.SkipInstall()
Spandan Das5ae65ee2024-04-16 22:03:26 +00001405 }
satayev8f088b02021-12-06 11:40:46 +00001406
Jihoon Kanga3a05462024-04-05 00:36:44 +00001407 module.stem = proptools.StringDefault(module.overridableProperties.Stem, ctx.ModuleName())
1408
1409 module.provideHiddenAPIPropertyInfo(ctx)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001410
Paul Duffinb97b1572021-04-29 21:50:40 +01001411 // Collate the components exported by this module. All scope specific modules are exported but
1412 // the impl and xml component modules are not.
1413 exportedComponents := map[string]struct{}{}
1414
Sundong Ahn57368eb2018-07-06 11:20:23 +09001415 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001416 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001417 // the recorded paths will be returned depending on the link type of the caller.
1418 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001419 tag := ctx.OtherModuleDependencyTag(to)
1420
Paul Duffinc8782502020-04-29 20:45:27 +01001421 // Extract information from any of the scope specific dependencies.
1422 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1423 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001424 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001425
1426 // Extract information from the dependency. The exact information extracted
1427 // is determined by the nature of the dependency which is determined by the tag.
1428 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001429
1430 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Jihoon Kang4b9220a2024-08-22 22:11:04 +00001431
1432 ctx.Phony(ctx.ModuleName(), scopePaths.stubsHeaderPath...)
Sundong Ahn20e998b2018-07-24 11:19:26 +09001433 }
Jihoon Kang8479dea2024-04-04 01:19:05 +00001434
1435 if tag == implLibraryTag {
1436 if dep, ok := android.OtherModuleProvider(ctx, to, JavaInfoProvider); ok {
1437 module.implLibraryHeaderJars = append(module.implLibraryHeaderJars, dep.HeaderJars...)
Jihoon Kanga3a05462024-04-05 00:36:44 +00001438 module.implLibraryModule = to.(*Library)
Jihoon Kang8479dea2024-04-04 01:19:05 +00001439 }
1440 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001441 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001442
Jihoon Kang98e9ac62024-09-25 23:42:30 +00001443 sdkLibInfo := module.generateCommonBuildActions(ctx)
Jihoon Kanga3a05462024-04-05 00:36:44 +00001444 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
1445 if !apexInfo.IsForPlatform() {
1446 module.hideApexVariantFromMake = true
1447 }
1448
1449 if module.implLibraryModule != nil {
1450 if ctx.Device() {
1451 module.classesJarPaths = android.Paths{module.implLibraryModule.implementationJarFile}
1452 module.bootDexJarPath = module.implLibraryModule.bootDexJarPath
1453 module.uncompressDexState = module.implLibraryModule.uncompressDexState
1454 module.active = module.implLibraryModule.active
1455 }
1456
1457 module.outputFile = module.implLibraryModule.outputFile
1458 module.dexJarFile = makeDexJarPathFromPath(module.implLibraryModule.dexJarFile.Path())
1459 module.headerJarFile = module.implLibraryModule.headerJarFile
1460 module.implementationAndResourcesJar = module.implLibraryModule.implementationAndResourcesJar
1461 module.builtInstalledForApex = module.implLibraryModule.builtInstalledForApex
1462 module.dexpreopter.configPath = module.implLibraryModule.dexpreopter.configPath
1463 module.dexpreopter.outputProfilePathOnHost = module.implLibraryModule.dexpreopter.outputProfilePathOnHost
1464
Jihoon Kang34155e32024-05-20 19:08:49 +00001465 // Properties required for Library.AndroidMkEntries
1466 module.logtagsSrcs = module.implLibraryModule.logtagsSrcs
1467 module.dexpreopter.builtInstalled = module.implLibraryModule.dexpreopter.builtInstalled
1468 module.jacocoReportClassesFile = module.implLibraryModule.jacocoReportClassesFile
1469 module.dexer.proguardDictionary = module.implLibraryModule.dexer.proguardDictionary
1470 module.dexer.proguardUsageZip = module.implLibraryModule.dexer.proguardUsageZip
1471 module.linter.reports = module.implLibraryModule.linter.reports
Colin Crossb79aa8f2024-09-25 15:41:01 -07001472
1473 if lintInfo, ok := android.OtherModuleProvider(ctx, module.implLibraryModule, LintProvider); ok {
1474 android.SetProvider(ctx, LintProvider, lintInfo)
1475 }
Jihoon Kang34155e32024-05-20 19:08:49 +00001476
Jihoon Kanga3a05462024-04-05 00:36:44 +00001477 if !module.Host() {
1478 module.hostdexInstallFile = module.implLibraryModule.hostdexInstallFile
1479 }
1480
Colin Crossa6182ab2024-08-21 10:47:44 -07001481 if installFilesInfo, ok := android.OtherModuleProvider(ctx, module.implLibraryModule, android.InstallFilesProvider); ok {
1482 if installFilesInfo.CheckbuildTarget != nil {
1483 ctx.CheckbuildFile(installFilesInfo.CheckbuildTarget)
1484 }
1485 }
Jihoon Kanga3a05462024-04-05 00:36:44 +00001486 }
1487
Paul Duffinb97b1572021-04-29 21:50:40 +01001488 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001489 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001490 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001491
1492 // Provide additional information for inclusion in an sdk's generated .info file.
1493 additionalSdkInfo := map[string]interface{}{}
1494 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001495 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001496 scopes := map[string]interface{}{}
1497 additionalSdkInfo["scopes"] = scopes
1498 for scope, scopePaths := range module.scopePaths {
1499 scopeInfo := map[string]interface{}{}
1500 scopes[scope.name] = scopeInfo
1501 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1502 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
Jihoon Kang5623e542024-01-31 23:27:26 +00001503 if p := scopePaths.latestApiPaths; len(p) > 0 {
1504 // The last path in the list is the one that applies to this scope, the
1505 // preceding ones, if any, are for the scope(s) that it extends.
1506 scopeInfo["latest_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001507 }
Jihoon Kang5623e542024-01-31 23:27:26 +00001508 if p := scopePaths.latestRemovedApiPaths; len(p) > 0 {
1509 // The last path in the list is the one that applies to this scope, the
1510 // preceding ones, if any, are for the scope(s) that it extends.
1511 scopeInfo["latest_removed_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001512 }
1513 }
Colin Cross40213022023-12-13 15:19:49 -08001514 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
mrziwang9f7b9f42024-07-10 12:18:06 -07001515 module.setOutputFiles(ctx)
Jihoon Kang28c96572024-09-11 23:44:44 +00001516
1517 var generatingLibs []string
1518 for _, apiScope := range AllApiScopes {
1519 if _, ok := module.scopePaths[apiScope]; ok {
1520 generatingLibs = append(generatingLibs, module.stubsLibraryModuleName(apiScope))
1521 }
1522 }
1523
mrziwang9f7b9f42024-07-10 12:18:06 -07001524 if module.requiresRuntimeImplementationLibrary() && module.implLibraryModule != nil {
Jihoon Kang28c96572024-09-11 23:44:44 +00001525 generatingLibs = append(generatingLibs, module.implLibraryModuleName())
mrziwang9f7b9f42024-07-10 12:18:06 -07001526 setOutputFiles(ctx, module.implLibraryModule.Module)
1527 }
Jihoon Kang28c96572024-09-11 23:44:44 +00001528
Jihoon Kang98e9ac62024-09-25 23:42:30 +00001529 sdkLibInfo.GeneratingLibs = generatingLibs
1530 android.SetProvider(ctx, SdkLibraryInfoProvider, sdkLibInfo)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001531}
1532
Jihoon Kanga3a05462024-04-05 00:36:44 +00001533func (module *SdkLibrary) BuiltInstalledForApex() []dexpreopterInstall {
1534 return module.builtInstalledForApex
1535}
1536
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001537func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001538 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001539 return nil
1540 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001541 entriesList := module.Library.AndroidMkEntries()
Jihoon Kanga3a05462024-04-05 00:36:44 +00001542 entries := &entriesList[0]
1543 entries.Required = append(entries.Required, module.implLibraryModuleName())
Yo Chiang07d75072020-06-05 17:43:19 +08001544 if module.sharedLibrary() {
Yo Chiang07d75072020-06-05 17:43:19 +08001545 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1546 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001547 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001548}
1549
Anton Hansson5fd5d242020-03-27 19:43:19 +00001550// The dist path of the stub artifacts
1551func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001552 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001553}
1554
Paul Duffin12ceb462019-12-24 20:31:31 +00001555// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001556func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001557 scopeProperties := module.scopeToProperties[apiScope]
1558 if scopeProperties.Sdk_version != nil {
1559 return proptools.String(scopeProperties.Sdk_version)
1560 }
1561
Jiyong Parkf1691d22021-03-29 20:11:58 +09001562 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001563 if sdkDep.hasStandardLibs() {
1564 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001565 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001566 } else {
1567 // Otherwise, use no system module.
1568 return "none"
1569 }
1570}
1571
Paul Duffin31310252020-11-20 21:26:20 +00001572func (module *SdkLibrary) distStem() string {
1573 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1574}
1575
Colin Cross986b69a2021-06-01 13:13:40 -07001576// distGroup returns the subdirectory of the dist path of the stub artifacts.
1577func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001578 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001579}
1580
Paul Duffin958806b2022-05-16 13:10:47 +00001581func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1582 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1583}
1584
Jihoon Kang748a24d2024-03-20 21:29:39 +00001585func latestPrebuiltApiCombinedModuleName(name string, apiScope *apiScope) string {
1586 return PrebuiltApiCombinedModuleName(name, apiScope.name, "latest")
1587}
1588
Paul Duffind1b3a922020-01-22 11:57:20 +00001589func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001590 return ":" + module.latestApiModuleName(apiScope)
1591}
1592
1593func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001594 return latestPrebuiltApiCombinedModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001595}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001596
Paul Duffind1b3a922020-01-22 11:57:20 +00001597func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001598 return ":" + module.latestRemovedApiModuleName(apiScope)
1599}
1600
1601func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001602 return latestPrebuiltApiCombinedModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001603}
1604
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001605func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001606 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1607}
1608
1609func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1610 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001611}
1612
Jihoon Kang0c705a42023-08-02 06:44:57 +00001613// The listed modules' stubs contents do not match the corresponding txt files,
1614// but require additional api contributions to generate the full stubs.
1615// This method returns the name of the additional api contribution module
1616// for corresponding sdk_library modules.
1617func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1618 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
Jihoon Kangb0f4c022024-08-06 00:15:25 +00001619 return val
Jihoon Kang0c705a42023-08-02 06:44:57 +00001620 }
1621 return ""
1622}
1623
Anton Hansson944e77d2020-08-19 11:40:22 +01001624func childModuleVisibility(childVisibility []string) []string {
1625 if childVisibility == nil {
1626 // No child visibility set. The child will use the visibility of the sdk_library.
1627 return nil
1628 }
1629
1630 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1631 var visibility []string
1632 visibility = append(visibility, "//visibility:override")
1633 visibility = append(visibility, childVisibility...)
1634 return visibility
1635}
1636
Paul Duffin958806b2022-05-16 13:10:47 +00001637func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
1638 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
1639}
1640
Paul Duffinea8f8082021-06-24 13:25:57 +01001641// Implements android.ApexModule
Colin Crossf7bbd2f2024-12-05 13:57:10 -08001642func (module *SdkLibrary) OutgoingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
Jooyung Han5e9013b2020-03-10 06:23:13 +09001643 if depTag == xmlPermissionsFileTag {
1644 return true
1645 }
Colin Crossf7bbd2f2024-12-05 13:57:10 -08001646 if depTag == implLibraryTag {
Jihoon Kanga3a05462024-04-05 00:36:44 +00001647 return true
1648 }
Colin Crossf7bbd2f2024-12-05 13:57:10 -08001649 return module.Library.OutgoingDepIsInSameApex(depTag)
Jooyung Han5e9013b2020-03-10 06:23:13 +09001650}
1651
Paul Duffinea8f8082021-06-24 13:25:57 +01001652// Implements android.ApexModule
1653func (module *SdkLibrary) UniqueApexVariations() bool {
1654 return module.uniqueApexVariations()
1655}
1656
Jihoon Kangb0f4c022024-08-06 00:15:25 +00001657func (module *SdkLibrary) ModuleBuildFromTextStubs() bool {
1658 return proptools.BoolDefault(module.sdkLibraryProperties.Build_from_text_stub, true)
Jihoon Kang80456fd2023-11-15 19:22:14 +00001659}
1660
Colin Cross571cccf2019-02-04 11:22:08 -08001661var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1662
Jiyong Park82484c02018-04-23 21:41:26 +09001663func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001664 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001665 return &[]string{}
1666 }).(*[]string)
1667}
1668
Paul Duffin749f98f2019-12-30 17:23:46 +00001669func (module *SdkLibrary) getApiDir() string {
1670 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1671}
1672
Jiyong Parkc678ad32018-04-10 13:07:10 +09001673// For a java_sdk_library module, create internal modules for stubs, docs,
1674// runtime libs and xml file. If requested, the stubs and docs are created twice
1675// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001676func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
Paul Duffina18abc22020-05-16 18:54:24 +01001677 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001678 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001679 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001680 }
1681
Paul Duffin37e0b772019-12-30 17:20:10 +00001682 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001683 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001684 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00001685 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001686 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001687
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001688 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09001689
Paul Duffin3375e352020-04-28 10:44:03 +01001690 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001691
Paul Duffin749f98f2019-12-30 17:23:46 +00001692 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001693 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001694 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001695 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001696 p := android.ExistentPathForSource(mctx, path)
1697 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07001698 if mctx.Config().AllowMissingDependencies() {
1699 mctx.AddMissingDependencies([]string{path})
1700 } else {
1701 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1702 missingCurrentApi = true
1703 }
Inseob Kim8098faa2019-03-18 10:19:51 +09001704 }
1705 }
1706 }
1707
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001708 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09001709 script := "build/soong/scripts/gen-java-current-api-files.sh"
1710 p := android.ExistentPathForSource(mctx, script)
1711
1712 if !p.Valid() {
1713 panic(fmt.Sprintf("script file %s doesn't exist", script))
1714 }
1715
1716 mctx.ModuleErrorf("One or more current api files are missing. "+
1717 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001718 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001719 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001720 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001721 return
1722 }
1723
Paul Duffin3375e352020-04-28 10:44:03 +01001724 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001725 // Use the stubs source name for legacy reasons.
Jihoon Kang96ce83b2024-09-23 22:09:44 +00001726 module.createDroidstubs(mctx, scope, module.droidstubsModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001727
Jihoon Kang96ce83b2024-09-23 22:09:44 +00001728 module.createFromSourceStubsLibrary(mctx, scope)
1729 module.createExportableFromSourceStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001730
Jihoon Kangb0f4c022024-08-06 00:15:25 +00001731 if mctx.Config().BuildFromTextStub() && module.ModuleBuildFromTextStubs() {
1732 module.createApiLibrary(mctx, scope)
Jihoon Kang0c705a42023-08-02 06:44:57 +00001733 }
Jihoon Kangb0f4c022024-08-06 00:15:25 +00001734 module.createTopLevelStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001735 module.createTopLevelExportableStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001736 }
1737
Paul Duffindfa131e2020-05-15 20:37:11 +01001738 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001739 // Create child module to create an implementation library.
1740 //
1741 // This temporarily creates a second implementation library that can be explicitly
1742 // referenced.
1743 //
1744 // TODO(b/156618935) - update comment once only one implementation library is created.
1745 module.createImplLibrary(mctx)
1746
Paul Duffindfa131e2020-05-15 20:37:11 +01001747 // Only create an XML permissions file that declares the library as being usable
1748 // as a shared library if required.
1749 if module.sharedLibrary() {
1750 module.createXmlFile(mctx)
1751 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001752
1753 // record java_sdk_library modules so that they are exported to make
1754 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1755 javaSdkLibrariesLock.Lock()
1756 defer javaSdkLibrariesLock.Unlock()
1757 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1758 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01001759
Paul Duffin77590a82022-04-28 14:13:30 +00001760 // 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 +01001761 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Cole Faustb7493472024-08-28 11:55:52 -07001762 module.properties.Static_libs.AppendSimpleValue(module.sdkLibraryProperties.Impl_only_static_libs)
Inseob Kimc0907f12019-02-08 21:00:45 +09001763}
1764
1765func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07001766 module.addHostAndDeviceProperties()
1767 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001768
Paul Duffin71b33cc2021-06-23 11:39:47 +01001769 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001770
Paul Duffina18abc22020-05-16 18:54:24 +01001771 module.properties.Installable = proptools.BoolPtr(true)
1772 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001773}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001774
Paul Duffindfa131e2020-05-15 20:37:11 +01001775func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1776 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1777}
1778
Jihoon Kangfa3f0782024-08-21 20:42:18 +00001779func moduleStubLinkType(j *Module) (stub bool, ret sdkLinkType) {
1780 kind := android.ToSdkKind(proptools.String(j.properties.Stub_contributing_api))
1781 switch kind {
1782 case android.SdkPublic:
Anton Hansson2d0c1942020-05-25 12:20:51 +01001783 return true, javaSdk
Jihoon Kangfa3f0782024-08-21 20:42:18 +00001784 case android.SdkSystem:
Anton Hansson2d0c1942020-05-25 12:20:51 +01001785 return true, javaSystem
Jihoon Kangfa3f0782024-08-21 20:42:18 +00001786 case android.SdkModule:
Anton Hansson2d0c1942020-05-25 12:20:51 +01001787 return true, javaModule
Jihoon Kangfa3f0782024-08-21 20:42:18 +00001788 case android.SdkTest:
Anton Hansson2d0c1942020-05-25 12:20:51 +01001789 return true, javaSystem
Jihoon Kangfa3f0782024-08-21 20:42:18 +00001790 case android.SdkSystemServer:
Jihoon Kang1147b312023-06-08 23:25:57 +00001791 return true, javaSystemServer
Jihoon Kangfa3f0782024-08-21 20:42:18 +00001792 // Default value for all modules other than java_sdk_library-generated stub submodules
1793 case android.SdkInvalid:
1794 return false, javaPlatform
1795 default:
1796 panic(fmt.Sprintf("stub_contributing_api set as an unsupported sdk kind %s", kind.String()))
Jihoon Kang1147b312023-06-08 23:25:57 +00001797 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01001798}
1799
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001800// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1801// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1802// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1803// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1804// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09001805func SdkLibraryFactory() android.Module {
1806 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01001807
1808 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01001809 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01001810
Inseob Kimc0907f12019-02-08 21:00:45 +09001811 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09001812 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001813 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01001814
1815 // Initialize the map from scope to scope specific properties.
1816 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001817 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001818 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1819 }
1820 module.scopeToProperties = scopeToProperties
1821
Paul Duffin4911a892020-04-29 23:35:13 +01001822 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01001823 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01001824 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1825 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1826
Paul Duffin1b1e8062020-05-08 13:44:43 +01001827 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01001828 // If no implementation is required then it cannot be used as a shared library
1829 // either.
1830 if !module.requiresRuntimeImplementationLibrary() {
1831 // If shared_library has been explicitly set to true then it is incompatible
1832 // with api_only: true.
1833 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1834 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1835 }
1836 // Set shared_library: false.
1837 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1838 }
1839
Jihoon Kang98e9ac62024-09-25 23:42:30 +00001840 if module.initCommonAfterDefaultsApplied() {
Paul Duffin1b1e8062020-05-08 13:44:43 +01001841 module.CreateInternalModules(ctx)
1842 }
1843 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001844 return module
1845}
Colin Cross79c7c262019-04-17 11:11:46 -07001846
1847//
1848// SDK library prebuilts
1849//
1850
Paul Duffin56d44902020-01-31 13:36:25 +00001851// Properties associated with each api scope.
1852type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001853 Jars []string `android:"path"`
1854
1855 Sdk_version *string
1856
Colin Cross79c7c262019-04-17 11:11:46 -07001857 // List of shared java libs that this module has dependencies to
1858 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01001859
Paul Duffinc8782502020-04-29 20:45:27 +01001860 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01001861 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001862
1863 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001864 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01001865
1866 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01001867 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01001868
1869 // Annotation zip
1870 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07001871}
1872
Paul Duffin56d44902020-01-31 13:36:25 +00001873type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00001874 // List of shared java libs, common to all scopes, that this module has
1875 // dependencies to
1876 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01001877
1878 // If set to true, compile dex files for the stubs. Defaults to false.
1879 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01001880
1881 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01001882 Permitted_packages []string
Spandan Das23956d12024-01-19 00:22:22 +00001883
1884 // Name of the source soong module that gets shadowed by this prebuilt
1885 // If unspecified, follows the naming convention that the source module of
1886 // the prebuilt is Name() without "prebuilt_" prefix
1887 Source_module_name *string
Paul Duffin56d44902020-01-31 13:36:25 +00001888}
1889
Paul Duffineedc5d52020-06-12 17:46:39 +01001890type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07001891 android.ModuleBase
1892 android.DefaultableModuleBase
1893 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00001894 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07001895
Paul Duffin37856732021-02-26 14:24:15 +00001896 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00001897 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00001898
Colin Cross79c7c262019-04-17 11:11:46 -07001899 properties sdkLibraryImportProperties
1900
Paul Duffin46a26a82020-04-07 19:27:04 +01001901 // Map from api scope to the scope specific property structure.
1902 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1903
Paul Duffin56d44902020-01-31 13:36:25 +00001904 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01001905
Paul Duffineedc5d52020-06-12 17:46:39 +01001906 // The reference to the xml permissions module created by the source module.
1907 // Is nil if the source module does not exist.
1908 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00001909
Jeongik Chad5fe8782021-07-08 01:13:11 +09001910 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00001911 dexJarFile OptionalDexJarPath
1912 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09001913
1914 // Expected install file path of the source module(sdk_library)
1915 // or dex implementation jar obtained from the prebuilt_apex, if any.
1916 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07001917}
1918
Paul Duffin46a26a82020-04-07 19:27:04 +01001919// The type of a structure that contains a field of type sdkLibraryScopeProperties
1920// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07001921//
1922// struct {
1923// Public sdkLibraryScopeProperties
1924// System sdkLibraryScopeProperties
1925// ...
1926// }
Paul Duffin46a26a82020-04-07 19:27:04 +01001927var allScopeStructType = createAllScopePropertiesStructType()
1928
1929// Dynamically create a structure type for each apiscope in allApiScopes.
1930func createAllScopePropertiesStructType() reflect.Type {
1931 var fields []reflect.StructField
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001932 for _, apiScope := range AllApiScopes {
Paul Duffin46a26a82020-04-07 19:27:04 +01001933 field := reflect.StructField{
1934 Name: apiScope.fieldName,
1935 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1936 }
1937 fields = append(fields, field)
1938 }
1939
1940 return reflect.StructOf(fields)
1941}
1942
1943// Create an instance of the scope specific structure type and return a map
1944// from apiscope to a pointer to each scope specific field.
1945func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1946 allScopePropertiesPtr := reflect.New(allScopeStructType)
1947 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1948 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1949
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001950 for _, apiScope := range AllApiScopes {
Paul Duffin46a26a82020-04-07 19:27:04 +01001951 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1952 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1953 }
1954
1955 return allScopePropertiesPtr.Interface(), scopeProperties
1956}
1957
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07001958// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07001959func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01001960 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07001961
Paul Duffin46a26a82020-04-07 19:27:04 +01001962 allScopeProperties, scopeToProperties := createPropertiesInstance()
1963 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08001964 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07001965
Paul Duffinc3091c82020-05-08 14:16:20 +01001966 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01001967 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01001968
Paul Duffin0bdcb272020-02-06 15:24:57 +00001969 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00001970 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07001971 InitJavaModule(module, android.HostAndDeviceSupported)
1972
Paul Duffin1b1e8062020-05-08 13:44:43 +01001973 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
Jihoon Kang98e9ac62024-09-25 23:42:30 +00001974 if module.initCommonAfterDefaultsApplied() {
Paul Duffin1b1e8062020-05-08 13:44:43 +01001975 module.createInternalModules(mctx)
1976 }
1977 })
Colin Cross79c7c262019-04-17 11:11:46 -07001978 return module
1979}
1980
Paul Duffin630b11e2021-07-15 13:35:26 +01001981var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
1982
1983func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
1984 return module.properties.Permitted_packages
1985}
1986
Paul Duffineedc5d52020-06-12 17:46:39 +01001987func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07001988 return &module.prebuilt
1989}
1990
Paul Duffineedc5d52020-06-12 17:46:39 +01001991func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07001992 return module.prebuilt.Name(module.ModuleBase.Name())
1993}
1994
Spandan Das23956d12024-01-19 00:22:22 +00001995func (module *SdkLibraryImport) BaseModuleName() string {
1996 return proptools.StringDefault(module.properties.Source_module_name, module.ModuleBase.Name())
1997}
1998
Paul Duffineedc5d52020-06-12 17:46:39 +01001999func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002000
Paul Duffin50061512020-01-21 16:31:05 +00002001 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002002 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002003 module.prebuilt.ForcePrefer()
2004 }
2005
Paul Duffin46a26a82020-04-07 19:27:04 +01002006 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002007 if len(scopeProperties.Jars) == 0 {
2008 continue
2009 }
2010
Paul Duffinbbb546b2020-04-09 00:07:11 +01002011 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002012
Paul Duffin0f8faff2020-05-20 16:18:00 +01002013 if len(scopeProperties.Stub_srcs) > 0 {
2014 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2015 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002016
2017 if scopeProperties.Current_api != nil {
2018 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2019 }
Paul Duffin56d44902020-01-31 13:36:25 +00002020 }
Colin Cross79c7c262019-04-17 11:11:46 -07002021
2022 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2023 javaSdkLibrariesLock.Lock()
2024 defer javaSdkLibrariesLock.Unlock()
2025 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2026}
2027
Paul Duffin44f1d842020-06-26 20:17:02 +01002028// Add the dependencies on the child module in the component deps mutator so that it
2029// creates references to the prebuilt and not the source modules.
2030func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002031 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002032 if len(scopeProperties.Jars) == 0 {
2033 continue
2034 }
2035
2036 // Add dependencies to the prebuilt stubs library
Jihoon Kangb7431552024-01-22 19:40:08 +00002037 ctx.AddVariationDependencies(nil, apiScope.prebuiltStubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002038
2039 if len(scopeProperties.Stub_srcs) > 0 {
2040 // Add dependencies to the prebuilt stubs source library
Jihoon Kang96ce83b2024-09-23 22:09:44 +00002041 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.droidstubsModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002042 }
Paul Duffin56d44902020-01-31 13:36:25 +00002043 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002044}
2045
2046// Add other dependencies as normal.
2047func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002048
2049 implName := module.implLibraryModuleName()
2050 if ctx.OtherModuleExists(implName) {
2051 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2052
2053 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2054 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2055 // Add dependency to the rule for generating the xml permissions file
2056 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2057 }
2058 }
Colin Cross79c7c262019-04-17 11:11:46 -07002059}
2060
Jiyong Park45bf82e2020-12-15 22:29:02 +09002061var _ android.ApexModule = (*SdkLibraryImport)(nil)
2062
2063// Implements android.ApexModule
Colin Crossf7bbd2f2024-12-05 13:57:10 -08002064func (module *SdkLibraryImport) OutgoingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01002065 if depTag == xmlPermissionsFileTag {
2066 return true
2067 }
2068
2069 // None of the other dependencies of the java_sdk_library_import are in the same apex
2070 // as the one that references this module.
2071 return false
2072}
2073
Jiyong Park45bf82e2020-12-15 22:29:02 +09002074// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002075func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2076 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002077 // we don't check prebuilt modules for sdk_version
2078 return nil
2079}
2080
Paul Duffinea8f8082021-06-24 13:25:57 +01002081// Implements android.ApexModule
2082func (module *SdkLibraryImport) UniqueApexVariations() bool {
2083 return module.uniqueApexVariations()
2084}
2085
Paul Duffin09817d62022-04-28 17:45:11 +01002086// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002087func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2088 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002089}
2090
2091var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2092
Paul Duffineedc5d52020-06-12 17:46:39 +01002093func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002094 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2095 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2096
Paul Duffin0f8faff2020-05-20 16:18:00 +01002097 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002098 ctx.VisitDirectDeps(func(to android.Module) {
2099 tag := ctx.OtherModuleDependencyTag(to)
2100
Paul Duffin0f8faff2020-05-20 16:18:00 +01002101 // Extract information from any of the scope specific dependencies.
2102 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2103 apiScope := scopeTag.apiScope
2104 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2105
2106 // Extract information from the dependency. The exact information extracted
2107 // is determined by the nature of the dependency which is determined by the tag.
2108 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002109 } else if tag == implLibraryTag {
2110 if implLibrary, ok := to.(*Library); ok {
2111 module.implLibraryModule = implLibrary
2112 } else {
2113 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2114 }
2115 } else if tag == xmlPermissionsFileTag {
2116 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2117 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2118 } else {
2119 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2120 }
Colin Cross79c7c262019-04-17 11:11:46 -07002121 }
2122 })
Jihoon Kang98e9ac62024-09-25 23:42:30 +00002123 sdkLibInfo := module.generateCommonBuildActions(ctx)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002124
2125 // Populate the scope paths with information from the properties.
2126 for apiScope, scopeProperties := range module.scopeProperties {
2127 if len(scopeProperties.Jars) == 0 {
2128 continue
2129 }
2130
2131 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002132 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002133 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2134 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2135 }
Paul Duffin39853512021-02-26 11:09:39 +00002136
2137 if ctx.Device() {
Spandan Dasa326b322024-09-19 21:02:52 +00002138 // Shared libraries deapexed from prebuilt apexes are no longer supported.
2139 // Set the dexJarBuildPath to a fake path.
2140 // This allows soong analysis pass, but will be an error during ninja execution if there are
2141 // any rdeps.
Colin Crossff694a82023-12-13 15:54:49 -08002142 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00002143 if ai.ForPrebuiltApex {
Spandan Dasa326b322024-09-19 21:02:52 +00002144 module.dexJarFile = makeDexJarPathFromPath(android.PathForModuleInstall(ctx, "intentionally_no_longer_supported"))
2145 module.initHiddenAPI(ctx, module.dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Paul Duffin39853512021-02-26 11:09:39 +00002146 }
2147 }
mrziwang9f7b9f42024-07-10 12:18:06 -07002148
Jihoon Kang28c96572024-09-11 23:44:44 +00002149 var generatingLibs []string
2150 for _, apiScope := range AllApiScopes {
2151 if scopeProperties, ok := module.scopeProperties[apiScope]; ok {
2152 if len(scopeProperties.Jars) == 0 {
2153 continue
2154 }
2155 generatingLibs = append(generatingLibs, module.stubsLibraryModuleName(apiScope))
2156 }
2157 }
2158
mrziwang9f7b9f42024-07-10 12:18:06 -07002159 module.setOutputFiles(ctx)
2160 if module.implLibraryModule != nil {
Jihoon Kang28c96572024-09-11 23:44:44 +00002161 generatingLibs = append(generatingLibs, module.implLibraryModuleName())
mrziwang9f7b9f42024-07-10 12:18:06 -07002162 setOutputFiles(ctx, module.implLibraryModule.Module)
2163 }
Jihoon Kang28c96572024-09-11 23:44:44 +00002164
Jihoon Kang98e9ac62024-09-25 23:42:30 +00002165 sdkLibInfo.GeneratingLibs = generatingLibs
2166 android.SetProvider(ctx, SdkLibraryInfoProvider, sdkLibInfo)
Colin Cross79c7c262019-04-17 11:11:46 -07002167}
2168
Jihoon Kang98e9ac62024-09-25 23:42:30 +00002169var _ UsesLibraryDependency = (*SdkLibraryImport)(nil)
2170
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002171// to satisfy UsesLibraryDependency interface
Spandan Das59a4a2b2024-01-09 21:35:56 +00002172func (module *SdkLibraryImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002173 // The dex implementation jar extracted from the .apex file should be used in preference to the
2174 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00002175 if module.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002176 ctx.ModuleErrorf(module.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00002177 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002178 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002179 return module.dexJarFile
2180 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002181 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002182 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002183 } else {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002184 return module.implLibraryModule.DexJarBuildPath(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +01002185 }
2186}
2187
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002188// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002189func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002190 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002191}
2192
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002193// to satisfy UsesLibraryDependency interface
2194func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2195 return nil
2196}
2197
Paul Duffineedc5d52020-06-12 17:46:39 +01002198// to satisfy apex.javaDependency interface
2199func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2200 if module.implLibraryModule == nil {
2201 return nil
2202 } else {
2203 return module.implLibraryModule.JacocoReportClassesFile()
2204 }
2205}
2206
2207// to satisfy apex.javaDependency interface
2208func (module *SdkLibraryImport) Stem() string {
2209 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002210}
Jiyong Parke3833882020-02-17 17:28:10 +09002211
Paul Duffin44b481b2020-06-17 16:59:43 +01002212var _ ApexDependency = (*SdkLibraryImport)(nil)
2213
2214// to satisfy java.ApexDependency interface
2215func (module *SdkLibraryImport) HeaderJars() android.Paths {
2216 if module.implLibraryModule == nil {
2217 return nil
2218 } else {
2219 return module.implLibraryModule.HeaderJars()
2220 }
2221}
2222
2223// to satisfy java.ApexDependency interface
2224func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2225 if module.implLibraryModule == nil {
2226 return nil
2227 } else {
2228 return module.implLibraryModule.ImplementationAndResourcesJars()
2229 }
2230}
2231
Jiakai Zhang204356f2021-09-09 08:12:46 +00002232// to satisfy java.DexpreopterInterface interface
2233func (module *SdkLibraryImport) IsInstallable() bool {
2234 return true
2235}
2236
Paul Duffinfef55002021-06-17 14:56:05 +01002237var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2238
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002239func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002240 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08002241 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01002242}
2243
Spandan Das2ea84dd2024-01-25 22:12:50 +00002244func (j *SdkLibraryImport) UseProfileGuidedDexpreopt() bool {
2245 return proptools.Bool(j.importDexpreoptProperties.Dex_preopt.Profile_guided)
2246}
2247
Paul Duffindd46f712020-02-10 13:37:10 +00002248type sdkLibrarySdkMemberType struct {
2249 android.SdkMemberTypeBase
2250}
2251
Paul Duffin296701e2021-07-14 10:29:36 +01002252func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
2253 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00002254}
2255
2256func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2257 _, ok := module.(*SdkLibrary)
2258 return ok
2259}
2260
2261func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2262 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2263}
2264
2265func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2266 return &sdkLibrarySdkMemberProperties{}
2267}
2268
Paul Duffin976b0e52021-04-27 23:20:26 +01002269var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
2270 android.SdkMemberTypeBase{
2271 PropertyName: "java_sdk_libs",
2272 SupportsSdk: true,
2273 },
2274}
2275
Paul Duffindd46f712020-02-10 13:37:10 +00002276type sdkLibrarySdkMemberProperties struct {
2277 android.SdkMemberPropertiesBase
2278
Paul Duffine8409952022-09-22 16:24:46 +01002279 // Stem name for files in the sdk snapshot.
2280 //
2281 // This is used to construct the path names of various sdk library files in the sdk snapshot to
2282 // make sure that they match the finalized versions of those files in prebuilts/sdk.
2283 //
2284 // This property is marked as keep so that it will be kept in all instances of this struct, will
2285 // not be cleared but will be copied to common structs. That is needed because this field is used
2286 // to construct many file names for other parts of this struct and so it needs to be present in
2287 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
2288 // be unavailable for generating file names if there were other properties that were still set.
2289 Stem string `sdk:"keep"`
2290
Paul Duffindd46f712020-02-10 13:37:10 +00002291 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00002292 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00002293
Paul Duffin3d1248c2020-04-09 00:10:17 +01002294 // The Java stubs source files.
2295 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01002296
2297 // The naming scheme.
2298 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01002299
2300 // True if the java_sdk_library_import is for a shared library, false
2301 // otherwise.
2302 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01002303
Paul Duffin1267d872021-04-16 17:21:36 +01002304 // True if the stub imports should produce dex jars.
2305 Compile_dex *bool
2306
Paul Duffina2ae7e02020-09-11 11:55:00 +01002307 // The paths to the doctag files to add to the prebuilt.
2308 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01002309
2310 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002311
2312 // Signals that this shared library is part of the bootclasspath starting
2313 // on the version indicated in this attribute.
2314 //
2315 // This will make platforms at this level and above to ignore
2316 // <uses-library> tags with this library name because the library is already
2317 // available
2318 On_bootclasspath_since *string
2319
2320 // Signals that this shared library was part of the bootclasspath before
2321 // (but not including) the version indicated in this attribute.
2322 //
2323 // The system will automatically add a <uses-library> tag with this library to
2324 // apps that target any SDK less than the version indicated in this attribute.
2325 On_bootclasspath_before *string
2326
2327 // Indicates that PackageManager should ignore this shared library if the
2328 // platform is below the version indicated in this attribute.
2329 //
2330 // This means that the device won't recognise this library as installed.
2331 Min_device_sdk *string
2332
2333 // Indicates that PackageManager should ignore this shared library if the
2334 // platform is above the version indicated in this attribute.
2335 //
2336 // This means that the device won't recognise this library as installed.
2337 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002338
2339 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00002340}
2341
2342type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01002343 Jars android.Paths
2344 StubsSrcJar android.Path
2345 CurrentApiFile android.Path
2346 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00002347 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002348 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00002349}
2350
2351func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2352 sdk := variant.(*SdkLibrary)
2353
Paul Duffine8409952022-09-22 16:24:46 +01002354 // Copy the stem name for files in the sdk snapshot.
2355 s.Stem = sdk.distStem()
2356
Paul Duffin106a3a42022-01-27 16:39:06 +00002357 s.Scopes = make(map[*apiScope]*scopeProperties)
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002358 for _, apiScope := range AllApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01002359 paths := sdk.findScopePaths(apiScope)
2360 if paths == nil {
2361 continue
2362 }
2363
Paul Duffindd46f712020-02-10 13:37:10 +00002364 jars := paths.stubsImplPath
2365 if len(jars) > 0 {
2366 properties := scopeProperties{}
2367 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01002368 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002369 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01002370 if paths.currentApiFilePath.Valid() {
2371 properties.CurrentApiFile = paths.currentApiFilePath.Path()
2372 }
2373 if paths.removedApiFilePath.Valid() {
2374 properties.RemovedApiFile = paths.removedApiFilePath.Path()
2375 }
Anton Hanssond78eb762021-09-21 15:25:12 +01002376 // The annotations zip is only available for modules that set annotations_enabled: true.
2377 if paths.annotationsZip.Valid() {
2378 properties.AnnotationsZip = paths.annotationsZip.Path()
2379 }
Paul Duffin106a3a42022-01-27 16:39:06 +00002380 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00002381 }
2382 }
2383
Paul Duffind7eb1c22020-05-26 20:57:10 +01002384 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01002385 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01002386 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01002387 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002388 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
2389 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
2390 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
2391 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002392
Jihoon Kang98e9ac62024-09-25 23:42:30 +00002393 implLibrary := sdk.implLibraryModule
Jihoon Kanga3a05462024-04-05 00:36:44 +00002394 if implLibrary != nil && implLibrary.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002395 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
2396 }
Paul Duffindd46f712020-02-10 13:37:10 +00002397}
2398
2399func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01002400 if s.Naming_scheme != nil {
2401 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2402 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01002403 if s.Shared_library != nil {
2404 propertySet.AddProperty("shared_library", *s.Shared_library)
2405 }
Paul Duffin1267d872021-04-16 17:21:36 +01002406 if s.Compile_dex != nil {
2407 propertySet.AddProperty("compile_dex", *s.Compile_dex)
2408 }
Paul Duffin869de142021-07-15 14:14:41 +01002409 if len(s.Permitted_packages) > 0 {
2410 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
2411 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002412 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
2413 if s.DexPreoptProfileGuided != nil {
2414 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
2415 }
Paul Duffinf7a64332020-05-13 16:54:55 +01002416
Paul Duffine8409952022-09-22 16:24:46 +01002417 stem := s.Stem
2418
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002419 for _, apiScope := range AllApiScopes {
Paul Duffindd46f712020-02-10 13:37:10 +00002420 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01002421 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00002422
Paul Duffin958806b2022-05-16 13:10:47 +00002423 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01002424
Paul Duffindd46f712020-02-10 13:37:10 +00002425 var jars []string
2426 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01002427 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00002428 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2429 jars = append(jars, dest)
2430 }
2431 scopeSet.AddProperty("jars", jars)
2432
Paul Duffin22628d52021-05-12 23:13:22 +01002433 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
2434 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01002435 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01002436 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
2437 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
2438 } else {
2439 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
2440 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01002441 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01002442 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2443 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2444 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01002445
Paul Duffin1fd005d2020-04-09 01:08:11 +01002446 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01002447 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01002448 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2449 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2450 }
2451
2452 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01002453 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01002454 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01002455 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2456 }
2457
Anton Hanssond78eb762021-09-21 15:25:12 +01002458 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01002459 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01002460 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
2461 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
2462 }
2463
Paul Duffindd46f712020-02-10 13:37:10 +00002464 if properties.SdkVersion != "" {
2465 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2466 }
2467 }
2468 }
2469
Paul Duffina2ae7e02020-09-11 11:55:00 +01002470 if len(s.Doctag_paths) > 0 {
2471 dests := []string{}
2472 for _, p := range s.Doctag_paths {
2473 dest := filepath.Join("doctags", p.Rel())
2474 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2475 dests = append(dests, dest)
2476 }
2477 propertySet.AddProperty("doctag_files", dests)
2478 }
Paul Duffindd46f712020-02-10 13:37:10 +00002479}