blob: 7f49871e90519a645aff73ca4f89aaa5fc135ae7 [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"
Paul Duffin46dc45a2020-05-14 15:39:10 +010023 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090024 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090025 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090026 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090027
Paul Duffind1b3a922020-01-22 11:57:20 +000028 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090029 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010030
31 "android/soong/android"
Ulya Trafimovichdbf31662020-12-17 12:07:54 +000032 "android/soong/dexpreopt"
Jiyong Parkc678ad32018-04-10 13:07:10 +090033)
34
Jooyung Han58f26ab2019-12-18 15:34:32 +090035const (
Pedro Loureiro9956e5e2021-09-07 17:21:59 +000036 sdkXmlFileSuffix = ".xml"
Jiyong Parkc678ad32018-04-10 13:07:10 +090037)
38
Paul Duffind1b3a922020-01-22 11:57:20 +000039// A tag to associated a dependency with a specific api scope.
40type scopeDependencyTag struct {
41 blueprint.BaseDependencyTag
42 name string
43 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010044
45 // Function for extracting appropriate path information from the dependency.
Colin Crossdcf71b22021-02-01 13:59:03 -080046 depInfoExtractor func(paths *scopePaths, ctx android.ModuleContext, dep android.Module) error
Paul Duffinc8782502020-04-29 20:45:27 +010047}
48
49// Extract tag specific information from the dependency.
50func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
Colin Crossdcf71b22021-02-01 13:59:03 -080051 err := tag.depInfoExtractor(paths, ctx, dep)
Paul Duffinc8782502020-04-29 20:45:27 +010052 if err != nil {
53 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
54 }
Paul Duffind1b3a922020-01-22 11:57:20 +000055}
56
Paul Duffin80342d72020-06-26 22:08:43 +010057var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
58
59func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
60 return false
61}
62
Paul Duffind1b3a922020-01-22 11:57:20 +000063// Provides information about an api scope, e.g. public, system, test.
64type apiScope struct {
65 // The name of the api scope, e.g. public, system, test
66 name string
67
Paul Duffin97b53b82020-05-05 14:40:52 +010068 // The api scope that this scope extends.
Paul Duffind0b9fca2022-09-30 18:11:41 +010069 //
70 // This organizes the scopes into an extension hierarchy.
71 //
72 // If set this means that the API provided by this scope includes the API provided by the scope
73 // set in this field.
Paul Duffin97b53b82020-05-05 14:40:52 +010074 extends *apiScope
75
Paul Duffind0b9fca2022-09-30 18:11:41 +010076 // The next api scope that a library that uses this scope can access.
77 //
78 // This organizes the scopes into an access hierarchy.
79 //
80 // If set this means that a library that can access this API can also access the API provided by
81 // the scope set in this field.
82 //
83 // A module that sets sdk_version: "<scope>_current" should have access to the <scope> API of
84 // every java_sdk_library that it depends on. If the library does not provide an API for <scope>
85 // then it will traverse up this access hierarchy to find an API that it does provide.
86 //
87 // If this is not set then it defaults to the scope set in extends.
88 canAccess *apiScope
89
Paul Duffin3375e352020-04-28 10:44:03 +010090 // The legacy enabled status for a specific scope can be dependent on other
91 // properties that have been specified on the library so it is provided by
92 // a function that can determine the status by examining those properties.
93 legacyEnabledStatus func(module *SdkLibrary) bool
94
95 // The default enabled status for non-legacy behavior, which is triggered by
96 // explicitly enabling at least one api scope.
97 defaultEnabledStatus bool
98
99 // Gets a pointer to the scope specific properties.
100 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
101
Paul Duffin46a26a82020-04-07 19:27:04 +0100102 // The name of the field in the dynamically created structure.
103 fieldName string
104
Paul Duffin6b836ba2020-05-13 19:19:49 +0100105 // The name of the property in the java_sdk_library_import
106 propertyName string
107
Jihoon Kangb7431552024-01-22 19:40:08 +0000108 // The tag to use to depend on the prebuilt stubs library module
109 prebuiltStubsTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000110
Jihoon Kangbd093452023-12-26 19:08:01 +0000111 // The tag to use to depend on the everything stubs library module.
112 everythingStubsTag scopeDependencyTag
113
114 // The tag to use to depend on the exportable stubs library module.
115 exportableStubsTag scopeDependencyTag
116
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100117 // The tag to use to depend on the stubs source module (if separate from the API module).
118 stubsSourceTag scopeDependencyTag
119
120 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
121 apiFileTag scopeDependencyTag
122
Paul Duffinc8782502020-04-29 20:45:27 +0100123 // The tag to use to depend on the stubs source and API module.
124 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000125
Paul Duffin958806b2022-05-16 13:10:47 +0000126 // The tag to use to depend on the module that provides the latest version of the API .txt file.
127 latestApiModuleTag scopeDependencyTag
128
129 // The tag to use to depend on the module that provides the latest version of the API removed.txt
130 // file.
131 latestRemovedApiModuleTag scopeDependencyTag
132
Paul Duffind1b3a922020-01-22 11:57:20 +0000133 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
134 apiFilePrefix string
135
Paul Duffind0b9fca2022-09-30 18:11:41 +0100136 // The scope specific suffix to add to the sdk library module name to construct a scope specific
Paul Duffind1b3a922020-01-22 11:57:20 +0000137 // module name.
138 moduleSuffix string
139
Paul Duffind1b3a922020-01-22 11:57:20 +0000140 // SDK version that the stubs library is built against. Note that this is always
141 // *current. Older stubs library built with a numbered SDK version is created from
142 // the prebuilt jar.
143 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100144
Paul Duffin15f34ef2020-07-20 18:04:44 +0100145 // The annotation that identifies this API level, empty for the public API scope.
146 annotation string
147
Paul Duffin1fb487d2020-04-07 18:50:10 +0100148 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100149 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100150 // This is not used directly but is used to construct the droidstubsArgs.
151 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100152
Paul Duffin15f34ef2020-07-20 18:04:44 +0100153 // The args that must be passed to droidstubs to generate the API and stubs source
154 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100155 //
156 // The API only includes the additional members that this scope adds over the scope
157 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100158 //
159 // The stubs source must include the definitions of everything that is in this
160 // api scope and all the scopes that this one extends.
161 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100162
Anton Hansson6478ac12020-05-02 11:19:36 +0100163 // Whether the api scope can be treated as unstable, and should skip compat checks.
164 unstable bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000165
166 // Represents the SDK kind of this scope.
167 kind android.SdkKind
Paul Duffind1b3a922020-01-22 11:57:20 +0000168}
169
170// Initialize a scope, creating and adding appropriate dependency tags
171func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100172 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100173 scopeByName[name] = scope
174 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100175 scope.propertyName = strings.ReplaceAll(name, "-", "_")
176 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Jihoon Kangb7431552024-01-22 19:40:08 +0000177 scope.prebuiltStubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100178 name: name + "-stubs",
179 apiScope: scope,
180 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000181 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000182 scope.everythingStubsTag = scopeDependencyTag{
183 name: name + "-stubs-everything",
184 apiScope: scope,
185 depInfoExtractor: (*scopePaths).extractEverythingStubsLibraryInfoFromDependency,
186 }
187 scope.exportableStubsTag = scopeDependencyTag{
188 name: name + "-stubs-exportable",
189 apiScope: scope,
190 depInfoExtractor: (*scopePaths).extractExportableStubsLibraryInfoFromDependency,
191 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100192 scope.stubsSourceTag = scopeDependencyTag{
193 name: name + "-stubs-source",
194 apiScope: scope,
195 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
196 }
197 scope.apiFileTag = scopeDependencyTag{
198 name: name + "-api",
199 apiScope: scope,
200 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
201 }
Paul Duffinc8782502020-04-29 20:45:27 +0100202 scope.stubsSourceAndApiTag = scopeDependencyTag{
203 name: name + "-stubs-source-and-api",
204 apiScope: scope,
205 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000206 }
Paul Duffin958806b2022-05-16 13:10:47 +0000207 scope.latestApiModuleTag = scopeDependencyTag{
208 name: name + "-latest-api",
209 apiScope: scope,
210 depInfoExtractor: (*scopePaths).extractLatestApiPath,
211 }
212 scope.latestRemovedApiModuleTag = scopeDependencyTag{
213 name: name + "-latest-removed-api",
214 apiScope: scope,
215 depInfoExtractor: (*scopePaths).extractLatestRemovedApiPath,
216 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100217
218 // To get the args needed to generate the stubs source append all the args from
219 // this scope and all the scopes it extends as each set of args adds additional
220 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100221 var scopeSpecificArgs []string
222 if scope.annotation != "" {
223 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100224 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100225 for s := scope; s != nil; s = s.extends {
226 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100227
Paul Duffin15f34ef2020-07-20 18:04:44 +0100228 // Ensure that the generated stubs includes all the API elements from the API scope
229 // that this scope extends.
230 if s != scope && s.annotation != "" {
231 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
232 }
233 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100234
Paul Duffind0b9fca2022-09-30 18:11:41 +0100235 // By default, a library that can access a scope can also access the scope it extends.
236 if scope.canAccess == nil {
237 scope.canAccess = scope.extends
238 }
239
Paul Duffin15f34ef2020-07-20 18:04:44 +0100240 // Escape any special characters in the arguments. This is needed because droidstubs
241 // passes these directly to the shell command.
242 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100243
Paul Duffind1b3a922020-01-22 11:57:20 +0000244 return scope
245}
246
Anton Hansson08f476b2021-04-07 15:32:19 +0100247func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
248 return ".stubs" + scope.moduleSuffix
249}
250
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000251func (scope *apiScope) exportableStubsLibraryModuleNameSuffix() string {
252 return ".stubs.exportable" + scope.moduleSuffix
253}
254
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000255func (scope *apiScope) apiLibraryModuleName(baseName string) string {
256 return scope.stubsLibraryModuleName(baseName) + ".from-text"
257}
258
Jihoon Kang1147b312023-06-08 23:25:57 +0000259func (scope *apiScope) sourceStubLibraryModuleName(baseName string) string {
260 return scope.stubsLibraryModuleName(baseName) + ".from-source"
261}
262
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000263func (scope *apiScope) exportableSourceStubsLibraryModuleName(baseName string) string {
264 return scope.exportableStubsLibraryModuleName(baseName) + ".from-source"
265}
266
Paul Duffinc3091c82020-05-08 14:16:20 +0100267func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100268 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000269}
270
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000271func (scope *apiScope) exportableStubsLibraryModuleName(baseName string) string {
272 return baseName + scope.exportableStubsLibraryModuleNameSuffix()
273}
274
Paul Duffinc8782502020-04-29 20:45:27 +0100275func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100276 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000277}
278
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100279func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100280 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100281}
282
Paul Duffin3375e352020-04-28 10:44:03 +0100283func (scope *apiScope) String() string {
284 return scope.name
285}
286
Paul Duffin958806b2022-05-16 13:10:47 +0000287// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
288// be stored.
289func (scope *apiScope) snapshotRelativeDir() string {
290 return filepath.Join("sdk_library", scope.name)
291}
292
293// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
294// library.
295func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
296 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
297}
298
299// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
300// named library.
301func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
302 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
303}
304
Paul Duffind1b3a922020-01-22 11:57:20 +0000305type apiScopes []*apiScope
306
307func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
308 var list []string
309 for _, scope := range scopes {
310 list = append(list, accessor(scope))
311 }
312 return list
313}
314
Jihoon Kanga96a7b12023-09-20 23:43:32 +0000315// Method that maps the apiScopes properties to the index of each apiScopes elements.
316// apiScopes property to be used as the key can be specified with the input accessor.
317// Only a string property of apiScope can be used as the key of the map.
318func (scopes apiScopes) MapToIndex(accessor func(*apiScope) string) map[string]int {
319 ret := make(map[string]int)
320 for i, scope := range scopes {
321 ret[accessor(scope)] = i
322 }
323 return ret
324}
325
Jiyong Parkc678ad32018-04-10 13:07:10 +0900326var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100327 scopeByName = make(map[string]*apiScope)
328 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000329 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100330 name: "public",
331
332 // Public scope is enabled by default for both legacy and non-legacy modes.
333 legacyEnabledStatus: func(module *SdkLibrary) bool {
334 return true
335 },
336 defaultEnabledStatus: true,
337
338 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
339 return &module.sdkLibraryProperties.Public
340 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000341 sdkVersion: "current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000342 kind: android.SdkPublic,
Paul Duffind1b3a922020-01-22 11:57:20 +0000343 })
344 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100345 name: "system",
346 extends: apiScopePublic,
347 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
348 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
349 return &module.sdkLibraryProperties.System
350 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100351 apiFilePrefix: "system-",
352 moduleSuffix: ".system",
353 sdkVersion: "system_current",
354 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000355 kind: android.SdkSystem,
Paul Duffind1b3a922020-01-22 11:57:20 +0000356 })
357 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100358 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100359 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100360 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
361 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
362 return &module.sdkLibraryProperties.Test
363 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100364 apiFilePrefix: "test-",
365 moduleSuffix: ".test",
366 sdkVersion: "test_current",
367 annotation: "android.annotation.TestApi",
368 unstable: true,
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000369 kind: android.SdkTest,
Paul Duffind1b3a922020-01-22 11:57:20 +0000370 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100371 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100372 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100373 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100374 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100375 //
376 // Enabling this would break existing usages.
377 legacyEnabledStatus: func(module *SdkLibrary) bool {
378 return false
379 },
380 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
381 return &module.sdkLibraryProperties.Module_lib
382 },
383 apiFilePrefix: "module-lib-",
384 moduleSuffix: ".module_lib",
385 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100386 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000387 kind: android.SdkModule,
Paul Duffin8f265b92020-04-28 14:13:56 +0100388 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100389 apiScopeSystemServer = initApiScope(&apiScope{
390 name: "system-server",
391 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100392
393 // The system-server scope can access the module-lib scope.
394 //
395 // A module that provides a system-server API is appended to the standard bootclasspath that is
396 // used by the system server. So, it should be able to access module-lib APIs provided by
397 // libraries on the bootclasspath.
398 canAccess: apiScopeModuleLib,
399
Paul Duffin0c5bae52020-06-02 13:00:08 +0100400 // The system-server scope is disabled by default in legacy mode.
401 //
402 // Enabling this would break existing usages.
403 legacyEnabledStatus: func(module *SdkLibrary) bool {
404 return false
405 },
406 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
407 return &module.sdkLibraryProperties.System_server
408 },
409 apiFilePrefix: "system-server-",
410 moduleSuffix: ".system_server",
411 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100412 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
413 extraArgs: []string{
414 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100415 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100416 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100417 },
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000418 kind: android.SdkSystemServer,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100419 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000420 allApiScopes = apiScopes{
421 apiScopePublic,
422 apiScopeSystem,
423 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100424 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100425 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000426 }
Jihoon Kang0c705a42023-08-02 06:44:57 +0000427 apiLibraryAdditionalProperties = map[string]struct {
428 FullApiSurfaceStubLib string
429 AdditionalApiContribution string
430 }{
431 "legacy.i18n.module.platform.api": {
432 FullApiSurfaceStubLib: "legacy.core.platform.api.stubs",
433 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
434 },
435 "stable.i18n.module.platform.api": {
436 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
437 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
438 },
439 "conscrypt.module.platform.api": {
440 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
441 AdditionalApiContribution: "conscrypt.module.public.api.stubs.source.api.contribution",
442 },
443 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900444)
445
Jiyong Park82484c02018-04-23 21:41:26 +0900446var (
447 javaSdkLibrariesLock sync.Mutex
448)
449
Jiyong Parkc678ad32018-04-10 13:07:10 +0900450// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900451// 1) disallowing linking to the runtime shared lib
452// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900453
454func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000455 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900456
Jiyong Park82484c02018-04-23 21:41:26 +0900457 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
458 javaSdkLibraries := javaSdkLibraries(ctx.Config())
459 sort.Strings(*javaSdkLibraries)
460 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
461 })
Paul Duffindd46f712020-02-10 13:37:10 +0000462
463 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100464 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900465}
466
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000467func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
468 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
469 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
470}
471
Paul Duffin3375e352020-04-28 10:44:03 +0100472// Properties associated with each api scope.
473type ApiScopeProperties struct {
474 // Indicates whether the api surface is generated.
475 //
476 // If this is set for any scope then all scopes must explicitly specify if they
477 // are enabled. This is to prevent new usages from depending on legacy behavior.
478 //
479 // Otherwise, if this is not set for any scope then the default behavior is
480 // scope specific so please refer to the scope specific property documentation.
481 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100482
483 // The sdk_version to use for building the stubs.
484 //
485 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000486 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100487 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000488 // will be none. This is used for java_sdk_library instances that are used
489 // to create stubs that contribute to the core_current sdk version.
490 // 2) Otherwise, it is assumed that this library extends but does not
491 // contribute directly to a specific sdk_version and so this uses the
492 // sdk_version appropriate for the api scope. e.g. public will use
493 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100494 //
495 // This does not affect the sdk_version used for either generating the stubs source
496 // or the API file. They both have to use the same sdk_version as is used for
497 // compiling the implementation library.
498 Sdk_version *string
Mark White9421c4c2023-08-10 00:07:03 +0000499
500 // Extra libs used when compiling stubs for this scope.
501 Libs []string
Paul Duffin3375e352020-04-28 10:44:03 +0100502}
503
Jiyong Parkc678ad32018-04-10 13:07:10 +0900504type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100505 // List of source files that are needed to compile the API, but are not part of runtime library.
506 Api_srcs []string `android:"arch_variant"`
507
Paul Duffin5df79302020-05-16 15:52:12 +0100508 // Visibility for impl library module. If not specified then defaults to the
509 // visibility property.
510 Impl_library_visibility []string
511
Paul Duffin4911a892020-04-29 23:35:13 +0100512 // Visibility for stubs library modules. If not specified then defaults to the
513 // visibility property.
514 Stubs_library_visibility []string
515
516 // Visibility for stubs source modules. If not specified then defaults to the
517 // visibility property.
518 Stubs_source_visibility []string
519
Anton Hansson7f66efa2020-10-08 14:47:23 +0100520 // List of Java libraries that will be in the classpath when building the implementation lib
521 Impl_only_libs []string `android:"arch_variant"`
522
Paul Duffin77590a82022-04-28 14:13:30 +0000523 // List of Java libraries that will included in the implementation lib.
524 Impl_only_static_libs []string `android:"arch_variant"`
525
Sundong Ahnf043cf62018-06-25 16:04:37 +0900526 // List of Java libraries that will be in the classpath when building stubs
527 Stub_only_libs []string `android:"arch_variant"`
528
Anton Hanssondae54cd2021-04-21 16:30:10 +0100529 // List of Java libraries that will included in stub libraries
530 Stub_only_static_libs []string `android:"arch_variant"`
531
Paul Duffin7a586d32019-12-30 17:09:34 +0000532 // list of package names that will be documented and publicized as API.
533 // This allows the API to be restricted to a subset of the source files provided.
534 // If this is unspecified then all the source files will be treated as being part
535 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900536 Api_packages []string
537
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900538 // list of package names that must be hidden from the API
539 Hidden_api_packages []string
540
Paul Duffin749f98f2019-12-30 17:23:46 +0000541 // the relative path to the directory containing the api specification files.
542 // Defaults to "api".
543 Api_dir *string
544
Paul Duffindfa131e2020-05-15 20:37:11 +0100545 // Determines whether a runtime implementation library is built; defaults to false.
546 //
547 // 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 +0200548 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000549 Api_only *bool
550
Paul Duffin11512472019-02-11 15:55:17 +0000551 // local files that are used within user customized droiddoc options.
552 Droiddoc_option_files []string
553
Spandan Das93e95992021-07-29 18:26:39 +0000554 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000555 // Available variables for substitution:
556 //
557 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900558 Droiddoc_options []string
559
Paul Duffine22c2ab2020-05-20 19:35:27 +0100560 // is set to true, Metalava will allow framework SDK to contain annotations.
561 Annotations_enabled *bool
562
Sundong Ahn054b19a2018-10-19 13:46:09 +0900563 // a list of top-level directories containing files to merge qualifier annotations
564 // (i.e. those intended to be included in the stubs written) from.
565 Merge_annotations_dirs []string
566
567 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
568 Merge_inclusion_annotations_dirs []string
569
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000570 // If set to true then don't create dist rules.
571 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900572
Paul Duffin31310252020-11-20 21:26:20 +0000573 // The stem for the artifacts that are copied to the dist, if not specified
574 // then defaults to the base module name.
575 //
576 // For each scope the following artifacts are copied to the apistubs/<scope>
577 // directory in the dist.
578 // * stubs impl jar -> <dist-stem>.jar
579 // * API specification file -> api/<dist-stem>.txt
580 // * Removed API specification file -> api/<dist-stem>-removed.txt
581 //
582 // Also used to construct the name of the filegroup (created by prebuilt_apis)
583 // that references the latest released API and remove API specification files.
584 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
585 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800586 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000587 Dist_stem *string
588
Colin Cross986b69a2021-06-01 13:13:40 -0700589 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700590 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700591 // in the public Android SDK.
592 Dist_group *string
593
Anton Hanssondff2c782020-12-21 17:10:01 +0000594 // A compatibility mode that allows historical API-tracking files to not exist.
595 // Do not use.
596 Unsafe_ignore_missing_latest_api bool
597
Paul Duffin3375e352020-04-28 10:44:03 +0100598 // indicates whether system and test apis should be generated.
599 Generate_system_and_test_apis bool `blueprint:"mutated"`
600
601 // The properties specific to the public api scope
602 //
603 // Unless explicitly specified by using public.enabled the public api scope is
604 // enabled by default in both legacy and non-legacy mode.
605 Public ApiScopeProperties
606
607 // The properties specific to the system api scope
608 //
609 // In legacy mode the system api scope is enabled by default when sdk_version
610 // is set to something other than "none".
611 //
612 // In non-legacy mode the system api scope is disabled by default.
613 System ApiScopeProperties
614
615 // The properties specific to the test api scope
616 //
617 // In legacy mode the test api scope is enabled by default when sdk_version
618 // is set to something other than "none".
619 //
620 // In non-legacy mode the test api scope is disabled by default.
621 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000622
Paul Duffin0c5bae52020-06-02 13:00:08 +0100623 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100624 //
Zi Wangb2179e32023-01-31 15:53:30 -0800625 // Unless explicitly specified by using module_lib.enabled the module_lib api
626 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100627 Module_lib ApiScopeProperties
628
Paul Duffin0c5bae52020-06-02 13:00:08 +0100629 // The properties specific to the system-server api scope
630 //
Zi Wangb2179e32023-01-31 15:53:30 -0800631 // Unless explicitly specified by using system_server.enabled the
632 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100633 System_server ApiScopeProperties
634
Jiyong Park932cdfe2020-05-28 00:19:53 +0900635 // Determines if the stubs are preferred over the implementation library
636 // for linking, even when the client doesn't specify sdk_version. When this
637 // is set to true, such clients are provided with the widest API surface that
638 // this lib provides. Note however that this option doesn't affect the clients
639 // that are in the same APEX as this library. In that case, the clients are
640 // always linked with the implementation library. Default is false.
641 Default_to_stubs *bool
642
Paul Duffin160fe412020-05-10 19:32:20 +0100643 // Properties related to api linting.
644 Api_lint struct {
645 // Enable api linting.
646 Enabled *bool
Anton Hanssonfd1c0d22023-11-02 15:18:09 +0000647
648 // If API lint is enabled, this flag controls whether a set of legitimate lint errors
649 // are turned off. The default is true.
650 Legacy_errors_allowed *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100651 }
652
Jihoon Kang80456fd2023-11-15 19:22:14 +0000653 // Determines if the module contributes to any api surfaces.
654 // This property should be set to true only if the module is listed under
655 // frameworks-base-api.bootclasspath in frameworks/base/api/Android.bp.
656 // Otherwise, this property should be set to false.
657 // Defaults to false.
658 Contribute_to_android_api *bool
659
Jihoon Kang6592e872023-12-19 01:13:16 +0000660 // a list of aconfig_declarations module names that the stubs generated in this module
661 // depend on.
662 Aconfig_declarations []string
663
Jiyong Parkc678ad32018-04-10 13:07:10 +0900664 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100665 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900666}
667
Paul Duffin0f8faff2020-05-20 16:18:00 +0100668// Paths to outputs from java_sdk_library and java_sdk_library_import.
669//
670// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
671// OptionalPaths are always set by java_sdk_library but may not be set by
672// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000673type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100674 // The path (represented as Paths for convenience when returning) to the stubs header jar.
675 //
676 // That is the jar that is created by turbine.
677 stubsHeaderPath android.Paths
678
679 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
680 //
681 // This is not the implementation jar, it still only contains stubs.
682 stubsImplPath android.Paths
683
Paul Duffin1267d872021-04-16 17:21:36 +0100684 // The dex jar for the stubs.
685 //
686 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100687 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100688
Jihoon Kangbd093452023-12-26 19:08:01 +0000689 // The exportable dex jar for the stubs.
690 // This is not the implementation jar, it still only contains stubs.
691 // Includes unflagged apis and flagged apis enabled by release configurations.
692 exportableStubsDexJarPath OptionalDexJarPath
693
Paul Duffin0f8faff2020-05-20 16:18:00 +0100694 // The API specification file, e.g. system_current.txt.
695 currentApiFilePath android.OptionalPath
696
697 // The specification of API elements removed since the last release.
698 removedApiFilePath android.OptionalPath
699
700 // The stubs source jar.
701 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100702
703 // Extracted annotations.
704 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000705
706 // The path to the latest API file.
707 latestApiPath android.OptionalPath
708
709 // The path to the latest removed API file.
710 latestRemovedApiPath android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000711}
712
Colin Crossdcf71b22021-02-01 13:59:03 -0800713func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Colin Cross313aa542023-12-13 13:47:44 -0800714 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800715 paths.stubsHeaderPath = lib.HeaderJars
716 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100717
718 libDep := dep.(UsesLibraryDependency)
Spandan Das59a4a2b2024-01-09 21:35:56 +0000719 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
Jihoon Kangbd093452023-12-26 19:08:01 +0000720 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
721 return nil
722 } else {
723 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
724 }
725}
726
727func (paths *scopePaths) extractEverythingStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
728 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
729 paths.stubsHeaderPath = lib.HeaderJars
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000730 if !ctx.Config().ReleaseHiddenApiExportableStubs() {
731 paths.stubsImplPath = lib.ImplementationJars
732 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000733
734 libDep := dep.(UsesLibraryDependency)
735 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
736 return nil
737 } else {
738 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
739 }
740}
741
742func (paths *scopePaths) extractExportableStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000743 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
744 if ctx.Config().ReleaseHiddenApiExportableStubs() {
745 paths.stubsImplPath = lib.ImplementationJars
746 }
747
Jihoon Kangbd093452023-12-26 19:08:01 +0000748 libDep := dep.(UsesLibraryDependency)
749 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
Paul Duffinc8782502020-04-29 20:45:27 +0100750 return nil
751 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800752 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100753 }
754}
755
Jihoon Kangee113282024-01-23 00:16:41 +0000756func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider) error) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100757 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000758 err := action(apiStubsProvider)
759 if err != nil {
760 return err
761 }
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000762 return nil
763 } else {
764 return fmt.Errorf("expected module that implements ExportableApiStubsSrcProvider, e.g. droidstubs")
765 }
766}
767
Jihoon Kangee113282024-01-23 00:16:41 +0000768func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider) error) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100769 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000770 err := action(apiStubsProvider)
771 if err != nil {
772 return err
773 }
Paul Duffin0f8faff2020-05-20 16:18:00 +0100774 return nil
775 } else {
776 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
777 }
778}
779
Jihoon Kangee113282024-01-23 00:16:41 +0000780func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider, stubsType StubsType) error {
781 var annotationsZip, currentApiFilePath, removedApiFilePath android.Path
782 annotationsZip, annotationsZipErr := provider.AnnotationsZip(stubsType)
783 currentApiFilePath, currentApiFilePathErr := provider.ApiFilePath(stubsType)
784 removedApiFilePath, removedApiFilePathErr := provider.RemovedApiFilePath(stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100785
Jihoon Kangee113282024-01-23 00:16:41 +0000786 combinedError := errors.Join(annotationsZipErr, currentApiFilePathErr, removedApiFilePathErr)
787
788 if combinedError == nil {
789 paths.annotationsZip = android.OptionalPathForPath(annotationsZip)
790 paths.currentApiFilePath = android.OptionalPathForPath(currentApiFilePath)
791 paths.removedApiFilePath = android.OptionalPathForPath(removedApiFilePath)
792 }
793 return combinedError
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000794}
795
Colin Crossdcf71b22021-02-01 13:59:03 -0800796func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangee113282024-01-23 00:16:41 +0000797 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
798 return paths.extractApiInfoFromApiStubsProvider(provider, Everything)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100799 })
800}
801
Jihoon Kangee113282024-01-23 00:16:41 +0000802func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider, stubsType StubsType) error {
803 stubsSrcJar, err := provider.StubsSrcJar(stubsType)
804 if err == nil {
805 paths.stubsSrcJar = android.OptionalPathForPath(stubsSrcJar)
806 }
807 return err
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000808}
809
Colin Crossdcf71b22021-02-01 13:59:03 -0800810func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangee113282024-01-23 00:16:41 +0000811 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) error {
812 return paths.extractStubsSourceInfoFromApiStubsProviders(provider, Everything)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100813 })
814}
815
Colin Crossdcf71b22021-02-01 13:59:03 -0800816func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000817 if ctx.Config().ReleaseHiddenApiExportableStubs() {
Jihoon Kangee113282024-01-23 00:16:41 +0000818 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
819 extractApiInfoErr := paths.extractApiInfoFromApiStubsProvider(provider, Exportable)
820 extractStubsSourceInfoErr := paths.extractStubsSourceInfoFromApiStubsProviders(provider, Exportable)
821 return errors.Join(extractApiInfoErr, extractStubsSourceInfoErr)
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000822 })
823 }
Jihoon Kangee113282024-01-23 00:16:41 +0000824 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
825 extractApiInfoErr := paths.extractApiInfoFromApiStubsProvider(provider, Everything)
826 extractStubsSourceInfoErr := paths.extractStubsSourceInfoFromApiStubsProviders(provider, Everything)
827 return errors.Join(extractApiInfoErr, extractStubsSourceInfoErr)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100828 })
829}
830
Paul Duffin958806b2022-05-16 13:10:47 +0000831func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
832 var paths android.Paths
833 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
834 paths = sourceFileProducer.Srcs()
835 } else {
836 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
837 }
838 if len(paths) != 1 {
839 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
840 }
841 return android.OptionalPathForPath(paths[0]), nil
842}
843
844func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
845 outputPath, err := extractSingleOptionalOutputPath(dep)
846 paths.latestApiPath = outputPath
847 return err
848}
849
850func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
851 outputPath, err := extractSingleOptionalOutputPath(dep)
852 paths.latestRemovedApiPath = outputPath
853 return err
854}
855
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100856type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100857 // The naming scheme to use for the components that this module creates.
858 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100859 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100860 //
861 // This is a temporary mechanism to simplify conversion from separate modules for each
862 // component that follow a different naming pattern to the default one.
863 //
864 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100865 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100866
867 // Specifies whether this module can be used as an Android shared library; defaults
868 // to true.
869 //
870 // An Android shared library is one that can be referenced in a <uses-library> element
871 // in an AndroidManifest.xml.
872 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100873
874 // Files containing information about supported java doc tags.
875 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000876
877 // Signals that this shared library is part of the bootclasspath starting
878 // on the version indicated in this attribute.
879 //
880 // This will make platforms at this level and above to ignore
881 // <uses-library> tags with this library name because the library is already
882 // available
883 On_bootclasspath_since *string
884
885 // Signals that this shared library was part of the bootclasspath before
886 // (but not including) the version indicated in this attribute.
887 //
888 // The system will automatically add a <uses-library> tag with this library to
889 // apps that target any SDK less than the version indicated in this attribute.
890 On_bootclasspath_before *string
891
892 // Indicates that PackageManager should ignore this shared library if the
893 // platform is below the version indicated in this attribute.
894 //
895 // This means that the device won't recognise this library as installed.
896 Min_device_sdk *string
897
898 // Indicates that PackageManager should ignore this shared library if the
899 // platform is above the version indicated in this attribute.
900 //
901 // This means that the device won't recognise this library as installed.
902 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100903}
904
Paul Duffin71b33cc2021-06-23 11:39:47 +0100905// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
906// embeds the commonToSdkLibraryAndImport struct.
907type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000908 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100909
Spandan Das23956d12024-01-19 00:22:22 +0000910 // Returns the name of the root java_sdk_library that creates the child stub libraries
911 // This is the `name` as it appears in Android.bp, and not the name in Soong's build graph
912 // (with the prebuilt_ prefix)
913 //
914 // e.g. in the following java_sdk_library_import
915 // java_sdk_library_import {
916 // name: "framework-foo.v1",
917 // source_module_name: "framework-foo",
918 // }
919 // the values returned by
920 // 1. Name(): prebuilt_framework-foo.v1 # unique
921 // 2. BaseModuleName(): framework-foo # the source
922 // 3. RootLibraryName: framework-foo.v1 # the undecordated `name` from Android.bp
923 RootLibraryName() string
924}
925
926func (m *SdkLibrary) RootLibraryName() string {
927 return m.BaseModuleName()
928}
929
930func (m *SdkLibraryImport) RootLibraryName() string {
931 // m.BaseModuleName refers to the source of the import
932 // use moduleBase.Name to get the name of the module as it appears in the .bp file
933 return m.ModuleBase.Name()
Paul Duffin71b33cc2021-06-23 11:39:47 +0100934}
935
Paul Duffin56d44902020-01-31 13:36:25 +0000936// Common code between sdk library and sdk library import
937type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100938 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100939
Paul Duffin56d44902020-01-31 13:36:25 +0000940 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100941
942 namingScheme sdkLibraryComponentNamingScheme
943
Paul Duffindfa131e2020-05-15 20:37:11 +0100944 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100945
Paul Duffina2ae7e02020-09-11 11:55:00 +0100946 // Paths to commonSdkLibraryProperties.Doctag_files
947 doctagPaths android.Paths
948
Paul Duffin859fe962020-05-15 10:20:31 +0100949 // Functionality related to this being used as a component of a java_sdk_library.
950 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000951}
952
Paul Duffin71b33cc2021-06-23 11:39:47 +0100953func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
954 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100955
Paul Duffin71b33cc2021-06-23 11:39:47 +0100956 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100957
958 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100959 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100960}
961
962func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100963 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100964 switch schemeProperty {
965 case "default":
966 c.namingScheme = &defaultNamingScheme{}
967 default:
968 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
969 return false
970 }
971
Spandan Das23956d12024-01-19 00:22:22 +0000972 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +0100973 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
974
Paul Duffindfa131e2020-05-15 20:37:11 +0100975 // Only track this sdk library if this can be used as a shared library.
976 if c.sharedLibrary() {
977 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100978 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100979 }
Paul Duffin859fe962020-05-15 10:20:31 +0100980
Paul Duffin1b1e8062020-05-08 13:44:43 +0100981 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100982}
983
Paul Duffinea8f8082021-06-24 13:25:57 +0100984// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
985// method.
986func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
987 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
988 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
989 // the APEX and so it needs a unique variation per APEX.
990 return c.sharedLibrary()
991}
992
Paul Duffina2ae7e02020-09-11 11:55:00 +0100993func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
994 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
995}
996
Paul Duffineedc5d52020-06-12 17:46:39 +0100997// Module name of the runtime implementation library
998func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +0000999 return c.module.RootLibraryName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +01001000}
1001
1002// Module name of the XML file for the lib
1003func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +00001004 return c.module.RootLibraryName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +01001005}
1006
Paul Duffinc3091c82020-05-08 14:16:20 +01001007// Name of the java_library module that compiles the stubs source.
1008func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001009 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001010 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001011}
1012
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001013// Name of the java_library module that compiles the exportable stubs source.
1014func (c *commonToSdkLibraryAndImport) exportableStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001015 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001016 return c.namingScheme.exportableStubsLibraryModuleName(apiScope, baseName)
1017}
1018
Paul Duffinc3091c82020-05-08 14:16:20 +01001019// Name of the droidstubs module that generates the stubs source and may also
1020// generate/check the API.
1021func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001022 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001023 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001024}
1025
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001026// Name of the java_api_library module that generates the from-text stubs source
1027// and compiles to a jar file.
1028func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001029 baseName := c.module.RootLibraryName()
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001030 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
1031}
1032
Jihoon Kang1147b312023-06-08 23:25:57 +00001033// Name of the java_library module that compiles the stubs
1034// generated from source Java files.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001035func (c *commonToSdkLibraryAndImport) sourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001036 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001037 return c.namingScheme.sourceStubsLibraryModuleName(apiScope, baseName)
1038}
1039
1040// Name of the java_library module that compiles the exportable stubs
1041// generated from source Java files.
1042func (c *commonToSdkLibraryAndImport) exportableSourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001043 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001044 return c.namingScheme.exportableSourceStubsLibraryModuleName(apiScope, baseName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001045}
1046
Paul Duffin46dc45a2020-05-14 15:39:10 +01001047// The component names for different outputs of the java_sdk_library.
1048//
1049// They are similar to the names used for the child modules it creates
1050const (
1051 stubsSourceComponentName = "stubs.source"
1052
1053 apiTxtComponentName = "api.txt"
1054
1055 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +01001056
1057 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +01001058)
1059
1060// A regular expression to match tags that reference a specific stubs component.
1061//
1062// It will only match if given a valid scope and a valid component. It is verfy strict
1063// to ensure it does not accidentally match a similar looking tag that should be processed
1064// by the embedded Library.
1065var tagSplitter = func() *regexp.Regexp {
1066 // Given a list of literal string items returns a regular expression that will
1067 // match any one of the items.
1068 choice := func(items ...string) string {
1069 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
1070 }
1071
1072 // Regular expression to match one of the scopes.
1073 scopesRegexp := choice(allScopeNames...)
1074
1075 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +01001076 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001077
1078 // Regular expression to match any combination of one scope and one component.
1079 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
1080}()
1081
1082// For OutputFileProducer interface
1083//
Anton Hanssond78eb762021-09-21 15:25:12 +01001084// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001085func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
1086 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
1087 scopeName := groups[1]
1088 component := groups[2]
1089
1090 if scope, ok := scopeByName[scopeName]; ok {
1091 paths := c.findScopePaths(scope)
1092 if paths == nil {
Spandan Das23956d12024-01-19 00:22:22 +00001093 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.RootLibraryName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001094 }
1095
1096 switch component {
1097 case stubsSourceComponentName:
1098 if paths.stubsSrcJar.Valid() {
1099 return android.Paths{paths.stubsSrcJar.Path()}, nil
1100 }
1101
1102 case apiTxtComponentName:
1103 if paths.currentApiFilePath.Valid() {
1104 return android.Paths{paths.currentApiFilePath.Path()}, nil
1105 }
1106
1107 case removedApiTxtComponentName:
1108 if paths.removedApiFilePath.Valid() {
1109 return android.Paths{paths.removedApiFilePath.Path()}, nil
1110 }
Anton Hanssond78eb762021-09-21 15:25:12 +01001111
1112 case annotationsComponentName:
1113 if paths.annotationsZip.Valid() {
1114 return android.Paths{paths.annotationsZip.Path()}, nil
1115 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001116 }
1117
1118 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
1119 } else {
1120 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
1121 }
1122
1123 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001124 switch tag {
1125 case ".doctags":
1126 if c.doctagPaths != nil {
1127 return c.doctagPaths, nil
1128 } else {
Spandan Das23956d12024-01-19 00:22:22 +00001129 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.RootLibraryName())
Paul Duffina2ae7e02020-09-11 11:55:00 +01001130 }
1131 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001132 return nil, nil
1133 }
1134}
1135
Paul Duffin803a9562020-05-20 11:52:25 +01001136func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001137 if c.scopePaths == nil {
1138 c.scopePaths = make(map[*apiScope]*scopePaths)
1139 }
1140 paths := c.scopePaths[scope]
1141 if paths == nil {
1142 paths = &scopePaths{}
1143 c.scopePaths[scope] = paths
1144 }
1145
1146 return paths
1147}
1148
Paul Duffin803a9562020-05-20 11:52:25 +01001149func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1150 if c.scopePaths == nil {
1151 return nil
1152 }
1153
1154 return c.scopePaths[scope]
1155}
1156
1157// If this does not support the requested api scope then find the closest available
1158// scope it does support. Returns nil if no such scope is available.
1159func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001160 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001161 if paths := c.findScopePaths(s); paths != nil {
1162 return paths
1163 }
1164 }
1165
1166 // This should never happen outside tests as public should be the base scope for every
1167 // scope and is enabled by default.
1168 return nil
1169}
1170
Jiyong Parkf1691d22021-03-29 20:11:58 +09001171func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001172
1173 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001174 if !sdkVersion.ApiLevel.IsPreview() {
Spandan Das23956d12024-01-19 00:22:22 +00001175 return PrebuiltJars(ctx, c.module.RootLibraryName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001176 }
1177
Paul Duffin1267d872021-04-16 17:21:36 +01001178 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1179 if paths == nil {
1180 return nil
1181 }
1182
1183 return paths.stubsHeaderPath
1184}
1185
1186// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1187//
1188// If the module does not support the specific kind then it will return the *scopePaths for the
1189// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1190// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1191func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001192 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001193
Paul Duffin803a9562020-05-20 11:52:25 +01001194 paths := c.findClosestScopePath(apiScope)
1195 if paths == nil {
1196 var scopes []string
1197 for _, s := range allApiScopes {
1198 if c.findScopePaths(s) != nil {
1199 scopes = append(scopes, s.name)
1200 }
1201 }
Spandan Das23956d12024-01-19 00:22:22 +00001202 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.module.RootLibraryName(), scopes)
Paul Duffin803a9562020-05-20 11:52:25 +01001203 return nil
1204 }
1205
Paul Duffin1267d872021-04-16 17:21:36 +01001206 return paths
1207}
1208
Paul Duffin32cf58a2021-05-18 16:32:50 +01001209// sdkKindToApiScope maps from android.SdkKind to apiScope.
1210func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1211 var apiScope *apiScope
1212 switch kind {
1213 case android.SdkSystem:
1214 apiScope = apiScopeSystem
1215 case android.SdkModule:
1216 apiScope = apiScopeModuleLib
1217 case android.SdkTest:
1218 apiScope = apiScopeTest
1219 case android.SdkSystemServer:
1220 apiScope = apiScopeSystemServer
1221 default:
1222 apiScope = apiScopePublic
1223 }
1224 return apiScope
1225}
1226
Paul Duffin1267d872021-04-16 17:21:36 +01001227// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001228func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001229 paths := c.selectScopePaths(ctx, kind)
1230 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001231 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001232 }
1233
1234 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001235}
1236
Paul Duffin32cf58a2021-05-18 16:32:50 +01001237// to satisfy SdkLibraryDependency interface
Jihoon Kangbd093452023-12-26 19:08:01 +00001238func (c *commonToSdkLibraryAndImport) SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
1239 paths := c.selectScopePaths(ctx, kind)
1240 if paths == nil {
1241 return makeUnsetDexJarPath()
1242 }
1243
1244 return paths.exportableStubsDexJarPath
1245}
1246
1247// to satisfy SdkLibraryDependency interface
Paul Duffin32cf58a2021-05-18 16:32:50 +01001248func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1249 apiScope := sdkKindToApiScope(kind)
1250 paths := c.findScopePaths(apiScope)
1251 if paths == nil {
1252 return android.OptionalPath{}
1253 }
1254
1255 return paths.removedApiFilePath
1256}
1257
Paul Duffin859fe962020-05-15 10:20:31 +01001258func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1259 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001260 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001261 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001262 }{}
1263
Spandan Das23956d12024-01-19 00:22:22 +00001264 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +01001265 componentProps.SdkLibraryName = namePtr
1266
Paul Duffindfa131e2020-05-15 20:37:11 +01001267 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001268 // Mark the stubs library as being components of this java_sdk_library so that
1269 // any app that includes code which depends (directly or indirectly) on the stubs
1270 // library will have the appropriate <uses-library> invocation inserted into its
1271 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001272 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001273 }
1274
1275 return componentProps
1276}
1277
Paul Duffindfa131e2020-05-15 20:37:11 +01001278func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1279 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1280}
1281
Paul Duffinf4600f62021-05-13 22:34:45 +01001282// Check if the stub libraries should be compiled for dex
1283func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1284 // Always compile the dex file files for the stub libraries if they will be used on the
1285 // bootclasspath.
1286 return !c.sharedLibrary()
1287}
1288
Paul Duffin859fe962020-05-15 10:20:31 +01001289// Properties related to the use of a module as an component of a java_sdk_library.
1290type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001291 // The name of the java_sdk_library/_import module.
1292 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001293
1294 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1295 // in the AndroidManifest.xml of any Android app that includes code that references
1296 // this module. If not set then no java_sdk_library/_import is tracked.
1297 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1298}
1299
1300// Structure to be embedded in a module struct that needs to support the
1301// SdkLibraryComponentDependency interface.
1302type EmbeddableSdkLibraryComponent struct {
1303 sdkLibraryComponentProperties SdkLibraryComponentProperties
1304}
1305
Paul Duffin71b33cc2021-06-23 11:39:47 +01001306func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1307 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001308}
1309
1310// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001311func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1312 return e.sdkLibraryComponentProperties.SdkLibraryName
1313}
1314
1315// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001316func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001317 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1318 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1319 // run-time library and the corresponding module that provides the implementation. This name is
1320 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1321 // in dexpreopt).
1322 //
1323 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1324 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001325 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1326}
1327
Paul Duffin859fe962020-05-15 10:20:31 +01001328// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1329// (including the java_sdk_library) itself.
1330type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001331 UsesLibraryDependency
1332
Paul Duffin3f0290e2021-06-30 18:25:36 +01001333 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1334 SdkLibraryName() *string
1335
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001336 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1337 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001338}
1339
1340// Make sure that all the module types that are components of java_sdk_library/_import
1341// and which can be referenced (directly or indirectly) from an android app implement
1342// the SdkLibraryComponentDependency interface.
1343var _ SdkLibraryComponentDependency = (*Library)(nil)
1344var _ SdkLibraryComponentDependency = (*Import)(nil)
1345var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001346var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001347
Paul Duffin32cf58a2021-05-18 16:32:50 +01001348// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001349type SdkLibraryDependency interface {
1350 SdkLibraryComponentDependency
1351
1352 // Get the header jars appropriate for the supplied sdk_version.
1353 //
1354 // These are turbine generated jars so they only change if the externals of the
1355 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001356 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001357
1358 // Get the implementation jars appropriate for the supplied sdk version.
1359 //
1360 // These are either the implementation jar for the whole sdk library or the implementation
1361 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1362 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001363 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001364
Jihoon Kangbd093452023-12-26 19:08:01 +00001365 // SdkApiStubDexJar returns the dex jar for the stubs for the prebuilt
1366 // java_sdk_library_import module. It is needed by the hiddenapi processing tool which
1367 // processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001368 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001369
Jihoon Kangbd093452023-12-26 19:08:01 +00001370 // SdkApiExportableStubDexJar returns the exportable dex jar for the stubs for
1371 // java_sdk_library module. It is needed by the hiddenapi processing tool which processes
1372 // dex files.
1373 SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
1374
Paul Duffin32cf58a2021-05-18 16:32:50 +01001375 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1376 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1377
Paul Duffinf4600f62021-05-13 22:34:45 +01001378 // sharedLibrary returns true if this can be used as a shared library.
1379 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001380}
1381
Inseob Kimc0907f12019-02-08 21:00:45 +09001382type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001383 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001384
Sundong Ahn054b19a2018-10-19 13:46:09 +09001385 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001386
Paul Duffin3375e352020-04-28 10:44:03 +01001387 // Map from api scope to the scope specific property structure.
1388 scopeToProperties map[*apiScope]*ApiScopeProperties
1389
Paul Duffin56d44902020-01-31 13:36:25 +00001390 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001391}
1392
Inseob Kimc0907f12019-02-08 21:00:45 +09001393var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001394
Paul Duffin3375e352020-04-28 10:44:03 +01001395func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1396 return module.sdkLibraryProperties.Generate_system_and_test_apis
1397}
1398
1399func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1400 // Check to see if any scopes have been explicitly enabled. If any have then all
1401 // must be.
1402 anyScopesExplicitlyEnabled := false
1403 for _, scope := range allApiScopes {
1404 scopeProperties := module.scopeToProperties[scope]
1405 if scopeProperties.Enabled != nil {
1406 anyScopesExplicitlyEnabled = true
1407 break
1408 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001409 }
Paul Duffin3375e352020-04-28 10:44:03 +01001410
1411 var generatedScopes apiScopes
1412 enabledScopes := make(map[*apiScope]struct{})
1413 for _, scope := range allApiScopes {
1414 scopeProperties := module.scopeToProperties[scope]
1415 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1416 // This is to ensure that any new usages of this module type do not rely on legacy
1417 // behaviour.
1418 defaultEnabledStatus := false
1419 if anyScopesExplicitlyEnabled {
1420 defaultEnabledStatus = scope.defaultEnabledStatus
1421 } else {
1422 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1423 }
1424 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1425 if enabled {
1426 enabledScopes[scope] = struct{}{}
1427 generatedScopes = append(generatedScopes, scope)
1428 }
1429 }
1430
1431 // Now check to make sure that any scope that is extended by an enabled scope is also
1432 // enabled.
1433 for _, scope := range allApiScopes {
1434 if _, ok := enabledScopes[scope]; ok {
1435 extends := scope.extends
1436 if extends != nil {
1437 if _, ok := enabledScopes[extends]; !ok {
1438 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1439 }
1440 }
1441 }
1442 }
1443
1444 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001445}
1446
satayev758968a2021-12-06 11:42:40 +00001447var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1448
satayev8f088b02021-12-06 11:40:46 +00001449func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001450 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001451 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1452 isExternal := !module.depIsInSameApex(ctx, child)
1453 if am, ok := child.(android.ApexModule); ok {
1454 if !do(ctx, parent, am, isExternal) {
1455 return false
1456 }
1457 }
1458 return !isExternal
1459 })
1460 })
1461}
1462
Paul Duffineedc5d52020-06-12 17:46:39 +01001463type sdkLibraryComponentTag struct {
1464 blueprint.BaseDependencyTag
1465 name string
1466}
1467
1468// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1469func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1470
1471var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001472
Jiyong Parke3833882020-02-17 17:28:10 +09001473func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001474 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001475 return dt == xmlPermissionsFileTag
1476 }
1477 return false
1478}
1479
Paul Duffineedc5d52020-06-12 17:46:39 +01001480var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001481
Paul Duffin44f1d842020-06-26 20:17:02 +01001482// Add the dependencies on the child modules in the component deps mutator.
1483func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001484 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001485 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001486 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kangbd093452023-12-26 19:08:01 +00001487 ctx.AddVariationDependencies(nil, apiScope.everythingStubsTag, stubModuleName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001488
Jihoon Kangbd093452023-12-26 19:08:01 +00001489 exportableStubModuleName := module.exportableStubsLibraryModuleName(apiScope)
1490 ctx.AddVariationDependencies(nil, apiScope.exportableStubsTag, exportableStubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001491
Paul Duffin15f34ef2020-07-20 18:04:44 +01001492 // Add a dependency on the stubs source in order to access both stubs source and api information.
1493 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001494
1495 if module.compareAgainstLatestApi(apiScope) {
1496 // Add dependencies on the latest finalized version of the API .txt file.
1497 latestApiModuleName := module.latestApiModuleName(apiScope)
1498 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1499
1500 // Add dependencies on the latest finalized version of the remove API .txt file.
1501 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1502 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1503 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001504 }
1505
Paul Duffindfa131e2020-05-15 20:37:11 +01001506 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001507 // Add dependency to the rule for generating the implementation library.
1508 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1509
Paul Duffindfa131e2020-05-15 20:37:11 +01001510 if module.sharedLibrary() {
1511 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001512 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001513 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001514 }
1515}
Paul Duffine74ac732020-02-06 13:51:46 +00001516
Paul Duffin44f1d842020-06-26 20:17:02 +01001517// Add other dependencies as normal.
1518func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001519 var missingApiModules []string
1520 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1521 if apiScope.unstable {
1522 continue
1523 }
Paul Duffin958806b2022-05-16 13:10:47 +00001524 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001525 missingApiModules = append(missingApiModules, m)
1526 }
Paul Duffin958806b2022-05-16 13:10:47 +00001527 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001528 missingApiModules = append(missingApiModules, m)
1529 }
Paul Duffin958806b2022-05-16 13:10:47 +00001530 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001531 missingApiModules = append(missingApiModules, m)
1532 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001533 }
1534 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1535 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1536 m += "You need to do one of the following:\n"
1537 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1538 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1539 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1540 m += "\n"
1541 m += "The following filegroup modules are missing:\n "
1542 m += strings.Join(missingApiModules, "\n ") + "\n"
1543 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."
1544 ctx.ModuleErrorf(m)
1545 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001546 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001547 // Only add the deps for the library if it is actually going to be built.
1548 module.Library.deps(ctx)
1549 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001550}
1551
Paul Duffin46dc45a2020-05-14 15:39:10 +01001552func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1553 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001554 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001555 return paths, err
1556 }
Colin Cross4acaea92021-12-10 23:05:02 +00001557 if module.requiresRuntimeImplementationLibrary() {
1558 return module.Library.OutputFiles(tag)
1559 }
1560 if tag == "" {
1561 return nil, nil
1562 }
1563 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001564}
1565
Inseob Kimc0907f12019-02-08 21:00:45 +09001566func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001567 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1568 module.CheckMinSdkVersion(ctx)
1569 }
1570
Paul Duffina2ae7e02020-09-11 11:55:00 +01001571 module.generateCommonBuildActions(ctx)
1572
Paul Duffindfa131e2020-05-15 20:37:11 +01001573 // Only build an implementation library if required.
1574 if module.requiresRuntimeImplementationLibrary() {
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001575 // stubsLinkType must be set before calling Library.GenerateAndroidBuildActions
1576 module.Library.stubsLinkType = Unknown
Paul Duffin43db9be2019-12-30 17:35:49 +00001577 module.Library.GenerateAndroidBuildActions(ctx)
1578 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001579
Paul Duffinb97b1572021-04-29 21:50:40 +01001580 // Collate the components exported by this module. All scope specific modules are exported but
1581 // the impl and xml component modules are not.
1582 exportedComponents := map[string]struct{}{}
1583
Sundong Ahn57368eb2018-07-06 11:20:23 +09001584 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001585 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001586 // the recorded paths will be returned depending on the link type of the caller.
1587 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001588 tag := ctx.OtherModuleDependencyTag(to)
1589
Paul Duffinc8782502020-04-29 20:45:27 +01001590 // Extract information from any of the scope specific dependencies.
1591 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1592 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001593 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001594
1595 // Extract information from the dependency. The exact information extracted
1596 // is determined by the nature of the dependency which is determined by the tag.
1597 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001598
1599 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001600 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001601 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001602
1603 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001604 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001605 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001606
1607 // Provide additional information for inclusion in an sdk's generated .info file.
1608 additionalSdkInfo := map[string]interface{}{}
1609 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001610 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001611 scopes := map[string]interface{}{}
1612 additionalSdkInfo["scopes"] = scopes
1613 for scope, scopePaths := range module.scopePaths {
1614 scopeInfo := map[string]interface{}{}
1615 scopes[scope.name] = scopeInfo
1616 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1617 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1618 if p := scopePaths.latestApiPath; p.Valid() {
1619 scopeInfo["latest_api"] = p.Path().String()
1620 }
1621 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1622 scopeInfo["latest_removed_api"] = p.Path().String()
1623 }
1624 }
Colin Cross40213022023-12-13 15:19:49 -08001625 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001626}
1627
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001628func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001629 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001630 return nil
1631 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001632 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001633 if module.sharedLibrary() {
1634 entries := &entriesList[0]
1635 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1636 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001637 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001638}
1639
Anton Hansson5fd5d242020-03-27 19:43:19 +00001640// The dist path of the stub artifacts
1641func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001642 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001643}
1644
Paul Duffin12ceb462019-12-24 20:31:31 +00001645// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001646func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001647 scopeProperties := module.scopeToProperties[apiScope]
1648 if scopeProperties.Sdk_version != nil {
1649 return proptools.String(scopeProperties.Sdk_version)
1650 }
1651
Jiyong Parkf1691d22021-03-29 20:11:58 +09001652 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001653 if sdkDep.hasStandardLibs() {
1654 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001655 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001656 } else {
1657 // Otherwise, use no system module.
1658 return "none"
1659 }
1660}
1661
Paul Duffin31310252020-11-20 21:26:20 +00001662func (module *SdkLibrary) distStem() string {
1663 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1664}
1665
Colin Cross986b69a2021-06-01 13:13:40 -07001666// distGroup returns the subdirectory of the dist path of the stub artifacts.
1667func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001668 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001669}
1670
Paul Duffin958806b2022-05-16 13:10:47 +00001671func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1672 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1673}
1674
Jihoon Kang748a24d2024-03-20 21:29:39 +00001675func latestPrebuiltApiCombinedModuleName(name string, apiScope *apiScope) string {
1676 return PrebuiltApiCombinedModuleName(name, apiScope.name, "latest")
1677}
1678
Paul Duffind1b3a922020-01-22 11:57:20 +00001679func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001680 return ":" + module.latestApiModuleName(apiScope)
1681}
1682
1683func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001684 return latestPrebuiltApiCombinedModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001685}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001686
Paul Duffind1b3a922020-01-22 11:57:20 +00001687func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001688 return ":" + module.latestRemovedApiModuleName(apiScope)
1689}
1690
1691func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001692 return latestPrebuiltApiCombinedModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001693}
1694
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001695func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001696 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1697}
1698
1699func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1700 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001701}
1702
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001703func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1704 _, exists := c.GetApiLibraries()[module.Name()]
1705 return exists
1706}
1707
Jihoon Kang0c705a42023-08-02 06:44:57 +00001708// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1709// api surface that the module contribute to. For example, the public droidstubs and java_library
1710// do not contribute to the public api surface, but contributes to the core platform api surface.
1711// This method returns the full api surface stub lib that
1712// the generated java_api_library should depend on.
1713func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1714 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1715 return val.FullApiSurfaceStubLib
1716 }
1717 return ""
1718}
1719
1720// The listed modules' stubs contents do not match the corresponding txt files,
1721// but require additional api contributions to generate the full stubs.
1722// This method returns the name of the additional api contribution module
1723// for corresponding sdk_library modules.
1724func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1725 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1726 return val.AdditionalApiContribution
1727 }
1728 return ""
1729}
1730
Anton Hansson944e77d2020-08-19 11:40:22 +01001731func childModuleVisibility(childVisibility []string) []string {
1732 if childVisibility == nil {
1733 // No child visibility set. The child will use the visibility of the sdk_library.
1734 return nil
1735 }
1736
1737 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1738 var visibility []string
1739 visibility = append(visibility, "//visibility:override")
1740 visibility = append(visibility, childVisibility...)
1741 return visibility
1742}
1743
Paul Duffin5df79302020-05-16 15:52:12 +01001744// Creates the implementation java library
1745func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001746 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1747
Paul Duffin5df79302020-05-16 15:52:12 +01001748 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001749 Name *string
1750 Visibility []string
1751 Instrument bool
1752 Libs []string
1753 Static_libs []string
1754 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001755 }{
1756 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001757 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001758 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1759 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001760 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1761 // addition of &module.properties below.
1762 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001763 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1764 // addition of &module.properties below.
1765 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1766 // Pass the apex_available settings down so that the impl library can be statically
1767 // embedded within a library that is added to an APEX. Needed for updatable-media.
1768 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001769 }
1770
1771 properties := []interface{}{
1772 &module.properties,
1773 &module.protoProperties,
1774 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001775 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001776 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001777 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001778 &props,
1779 module.sdkComponentPropertiesForChildLibrary(),
1780 }
1781 mctx.CreateModule(LibraryFactory, properties...)
1782}
1783
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001784type libraryProperties struct {
1785 Name *string
1786 Visibility []string
1787 Srcs []string
1788 Installable *bool
1789 Sdk_version *string
1790 System_modules *string
1791 Patch_module *string
1792 Libs []string
1793 Static_libs []string
1794 Compile_dex *bool
1795 Java_version *string
1796 Openjdk9 struct {
1797 Srcs []string
1798 Javacflags []string
1799 }
1800 Dist struct {
1801 Targets []string
1802 Dest *string
1803 Dir *string
1804 Tag *string
1805 }
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001806 Is_stubs_module *bool
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001807}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001808
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001809func (module *SdkLibrary) stubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
1810 props := libraryProperties{}
Jihoon Kang786df932023-09-07 01:18:31 +00001811 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001812 // sources are generated from the droiddoc
Paul Duffin12ceb462019-12-24 20:31:31 +00001813 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001814 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001815 props.System_modules = module.deviceProperties.System_modules
1816 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001817 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001818 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001819 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001820 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001821 // The stub-annotations library contains special versions of the annotations
1822 // with CLASS retention policy, so that they're kept.
1823 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1824 props.Libs = append(props.Libs, "stub-annotations")
1825 }
Paul Duffina18abc22020-05-16 18:54:24 +01001826 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1827 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001828 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1829 // interop with older developer tools that don't support 1.9.
1830 props.Java_version = proptools.StringPtr("1.8")
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001831 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffinf4600f62021-05-13 22:34:45 +01001832
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001833 return props
1834}
1835
1836// Creates a static java library that has API stubs
1837func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1838
1839 props := module.stubsLibraryProps(mctx, apiScope)
1840 props.Name = proptools.StringPtr(module.sourceStubsLibraryModuleName(apiScope))
1841 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
1842
1843 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1844}
1845
1846// Create a static java library that compiles the "exportable" stubs
1847func (module *SdkLibrary) createExportableStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1848 props := module.stubsLibraryProps(mctx, apiScope)
1849 props.Name = proptools.StringPtr(module.exportableSourceStubsLibraryModuleName(apiScope))
1850 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope) + "{.exportable}"}
1851
Paul Duffin859fe962020-05-15 10:20:31 +01001852 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001853}
1854
Paul Duffin6d0886e2020-04-07 18:49:53 +01001855// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001856// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001857func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001858 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001859 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001860 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001861 Srcs []string
1862 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001863 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001864 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001865 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001866 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001867 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001868 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001869 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001870 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001871 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001872 Merge_annotations_dirs []string
1873 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001874 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001875 Previous_api *string
Jihoon Kang6592e872023-12-19 01:13:16 +00001876 Aconfig_declarations []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001877 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001878 Current ApiToCheck
1879 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001880
1881 Api_lint struct {
1882 Enabled *bool
1883 New_since *string
1884 Baseline_file *string
1885 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001886 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001887 Aidl struct {
1888 Include_dirs []string
1889 Local_include_dirs []string
1890 }
Paul Duffin040e9062020-11-23 17:41:36 +00001891 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001892 }{}
1893
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001894 // The stubs source processing uses the same compile time classpath when extracting the
1895 // API from the implementation library as it does when compiling it. i.e. the same
1896 // * sdk version
1897 // * system_modules
1898 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001899
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001900 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001901 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001902 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001903 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001904 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001905 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001906 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001907 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001908 // A droiddoc module has only one Libs property and doesn't distinguish between
1909 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001910 props.Libs = module.properties.Libs
1911 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001912 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001913 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001914 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1915 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1916 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001917
Paul Duffine22c2ab2020-05-20 19:35:27 +01001918 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001919 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1920 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
Jihoon Kang6592e872023-12-19 01:13:16 +00001921 props.Aconfig_declarations = module.sdkLibraryProperties.Aconfig_declarations
Sundong Ahn054b19a2018-10-19 13:46:09 +09001922
Paul Duffin6d0886e2020-04-07 18:49:53 +01001923 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001924 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001925 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001926 }
1927 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001928 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001929 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1930 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001931 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00001932 disabledWarnings := []string{"HiddenSuperclass"}
1933 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
1934 disabledWarnings = append(disabledWarnings,
1935 "BroadcastBehavior",
1936 "DeprecationMismatch",
1937 "MissingPermission",
1938 "SdkConstant",
1939 "Todo",
1940 )
Paul Duffin235ffff2019-12-24 10:41:30 +00001941 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001942 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001943
Paul Duffin6877e6d2020-09-25 19:59:14 +01001944 // Output Javadoc comments for public scope.
1945 if apiScope == apiScopePublic {
1946 props.Output_javadoc_comments = proptools.BoolPtr(true)
1947 }
1948
Paul Duffin1fb487d2020-04-07 18:50:10 +01001949 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001950 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001951 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001952 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001953
Paul Duffin15f34ef2020-07-20 18:04:44 +01001954 // List of APIs identified from the provided source files are created. They are later
1955 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1956 // last-released (a.k.a numbered) list of API.
1957 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1958 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1959 apiDir := module.getApiDir()
1960 currentApiFileName = path.Join(apiDir, currentApiFileName)
1961 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001962
Paul Duffin15f34ef2020-07-20 18:04:44 +01001963 // check against the not-yet-release API
1964 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1965 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001966
Paul Duffin958806b2022-05-16 13:10:47 +00001967 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001968 // check against the latest released API
1969 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001970 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001971 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1972 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1973 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001974 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1975 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001976
Paul Duffin15f34ef2020-07-20 18:04:44 +01001977 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1978 // Enable api lint.
1979 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1980 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001981
Paul Duffin15f34ef2020-07-20 18:04:44 +01001982 // If it exists then pass a lint-baseline.txt through to droidstubs.
1983 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1984 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1985 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1986 if err != nil {
1987 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1988 }
1989 if len(paths) == 1 {
1990 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1991 } else if len(paths) != 0 {
1992 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001993 }
1994 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001995 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001996
Paul Duffin15f34ef2020-07-20 18:04:44 +01001997 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001998 // Dist the api txt and removed api txt artifacts for sdk builds.
1999 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
Jihoon Kang02168052024-03-20 00:44:54 +00002000 stubsTypeTagPrefix := ""
2001 if mctx.Config().ReleaseHiddenApiExportableStubs() {
2002 stubsTypeTagPrefix = ".exportable"
2003 }
Paul Duffin040e9062020-11-23 17:41:36 +00002004 for _, p := range []struct {
2005 tag string
2006 pattern string
2007 }{
Jihoon Kangd1799f62024-02-20 23:01:38 +00002008 // "exportable" api files are copied to the dist directory instead of the
Jihoon Kang02168052024-03-20 00:44:54 +00002009 // "everything" api files when "RELEASE_HIDDEN_API_EXPORTABLE_STUBS" build flag
2010 // is set. Otherwise, the "everything" api files are copied to the dist directory.
2011 {tag: "%s.api.txt", pattern: "%s.txt"},
2012 {tag: "%s.removed-api.txt", pattern: "%s-removed.txt"},
Paul Duffin040e9062020-11-23 17:41:36 +00002013 } {
2014 props.Dists = append(props.Dists, android.Dist{
2015 Targets: []string{"sdk", "win_sdk"},
2016 Dir: distDir,
2017 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
Jihoon Kang02168052024-03-20 00:44:54 +00002018 Tag: proptools.StringPtr(fmt.Sprintf(p.tag, stubsTypeTagPrefix)),
Paul Duffin040e9062020-11-23 17:41:36 +00002019 })
2020 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00002021 }
2022
Spandan Das2cc80ba2023-10-27 17:21:52 +00002023 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002024}
2025
Jihoon Kang0c705a42023-08-02 06:44:57 +00002026func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002027 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00002028 Name *string
2029 Visibility []string
2030 Api_contributions []string
2031 Libs []string
2032 Static_libs []string
2033 Full_api_surface_stub *string
Jihoon Kang4ec24872023-10-05 17:26:09 +00002034 System_modules *string
Jihoon Kang063ec002023-06-28 01:16:23 +00002035 Enable_validation *bool
Jihoon Kang5d701272024-02-15 21:53:49 +00002036 Stubs_type *string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002037 }{}
2038
2039 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00002040 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002041
2042 apiContributions := []string{}
2043
2044 // Api surfaces are not independent of each other, but have subset relationships,
2045 // and so does the api files. To generate from-text stubs for api surfaces other than public,
2046 // all subset api domains' api_contriubtions must be added as well.
2047 scope := apiScope
2048 for scope != nil {
2049 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
2050 scope = scope.extends
2051 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00002052 if apiScope == apiScopePublic {
2053 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
2054 if additionalApiContribution != "" {
2055 apiContributions = append(apiContributions, additionalApiContribution)
2056 }
2057 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002058
2059 props.Api_contributions = apiContributions
2060 props.Libs = module.properties.Libs
2061 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00002062 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002063 props.Libs = append(props.Libs, "stub-annotations")
2064 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kange7ee2562023-07-25 05:51:46 +00002065 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
Jihoon Kang0c705a42023-08-02 06:44:57 +00002066 if alternativeFullApiSurfaceStub != "" {
2067 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
2068 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002069
2070 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
2071 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
2072 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00002073 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002074 }
2075
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00002076 // java_sdk_library modules that set sdk_version as none does not depend on other api
2077 // domains. Therefore, java_api_library created from such modules should not depend on
2078 // full_api_surface_stubs but create and compile stubs by the java_api_library module
2079 // itself.
2080 if module.SdkVersion(mctx).Kind == android.SdkNone {
2081 props.Full_api_surface_stub = nil
2082 }
2083
Jihoon Kang4ec24872023-10-05 17:26:09 +00002084 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00002085 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang5d701272024-02-15 21:53:49 +00002086 props.Stubs_type = proptools.StringPtr("everything")
Jihoon Kang4ec24872023-10-05 17:26:09 +00002087
Spandan Das2cc80ba2023-10-27 17:21:52 +00002088 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002089}
2090
Jihoon Kang02168052024-03-20 00:44:54 +00002091func (module *SdkLibrary) topLevelStubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope, doDist bool) libraryProperties {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002092 props := libraryProperties{}
2093
Jihoon Kang1147b312023-06-08 23:25:57 +00002094 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
2095 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
2096 props.Sdk_version = proptools.StringPtr(sdkVersion)
2097
Jihoon Kang1147b312023-06-08 23:25:57 +00002098 props.System_modules = module.deviceProperties.System_modules
2099
Jihoon Kang1147b312023-06-08 23:25:57 +00002100 // The imports need to be compiled to dex if the java_sdk_library requests it.
2101 compileDex := module.dexProperties.Compile_dex
2102 if module.stubLibrariesCompiledForDex() {
2103 compileDex = proptools.BoolPtr(true)
2104 }
2105 props.Compile_dex = compileDex
2106
Jihoon Kang02168052024-03-20 00:44:54 +00002107 if !Bool(module.sdkLibraryProperties.No_dist) && doDist {
2108 props.Dist.Targets = []string{"sdk", "win_sdk"}
2109 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
2110 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
2111 props.Dist.Tag = proptools.StringPtr(".jar")
2112 }
2113
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002114 return props
2115}
2116
2117func (module *SdkLibrary) createTopLevelStubsLibrary(
2118 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
2119
Jihoon Kang02168052024-03-20 00:44:54 +00002120 // Dist the "everything" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is false
2121 doDist := !mctx.Config().ReleaseHiddenApiExportableStubs()
2122 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002123 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
2124
2125 // Add the stub compiling java_library/java_api_library as static lib based on build config
2126 staticLib := module.sourceStubsLibraryModuleName(apiScope)
2127 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
2128 staticLib = module.apiLibraryModuleName(apiScope)
2129 }
2130 props.Static_libs = append(props.Static_libs, staticLib)
2131
2132 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2133}
2134
2135func (module *SdkLibrary) createTopLevelExportableStubsLibrary(
2136 mctx android.DefaultableHookContext, apiScope *apiScope) {
2137
Jihoon Kang02168052024-03-20 00:44:54 +00002138 // Dist the "exportable" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is true
2139 doDist := mctx.Config().ReleaseHiddenApiExportableStubs()
2140 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002141 props.Name = proptools.StringPtr(module.exportableStubsLibraryModuleName(apiScope))
2142
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002143 staticLib := module.exportableSourceStubsLibraryModuleName(apiScope)
2144 props.Static_libs = append(props.Static_libs, staticLib)
2145
Jihoon Kang1147b312023-06-08 23:25:57 +00002146 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2147}
2148
Paul Duffin958806b2022-05-16 13:10:47 +00002149func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
2150 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
2151}
2152
Paul Duffinea8f8082021-06-24 13:25:57 +01002153// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09002154func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2155 depTag := mctx.OtherModuleDependencyTag(dep)
2156 if depTag == xmlPermissionsFileTag {
2157 return true
2158 }
2159 return module.Library.DepIsInSameApex(mctx, dep)
2160}
2161
Paul Duffinea8f8082021-06-24 13:25:57 +01002162// Implements android.ApexModule
2163func (module *SdkLibrary) UniqueApexVariations() bool {
2164 return module.uniqueApexVariations()
2165}
2166
Jihoon Kang80456fd2023-11-15 19:22:14 +00002167func (module *SdkLibrary) ContributeToApi() bool {
2168 return proptools.BoolDefault(module.sdkLibraryProperties.Contribute_to_android_api, false)
2169}
2170
Jiyong Parkc678ad32018-04-10 13:07:10 +09002171// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01002172func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002173 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002174 var moduleMinApiLevelStr = moduleMinApiLevel.String()
2175 if moduleMinApiLevel == android.NoneApiLevel {
2176 moduleMinApiLevelStr = "current"
2177 }
Jiyong Parke3833882020-02-17 17:28:10 +09002178 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00002179 Name *string
2180 Lib_name *string
2181 Apex_available []string
2182 On_bootclasspath_since *string
2183 On_bootclasspath_before *string
2184 Min_device_sdk *string
2185 Max_device_sdk *string
2186 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002187 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002188 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00002189 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
2190 Lib_name: proptools.StringPtr(module.BaseModuleName()),
2191 Apex_available: module.ApexProperties.Apex_available,
2192 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
2193 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
2194 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
2195 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
2196 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002197 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002198 }
Jiyong Parke3833882020-02-17 17:28:10 +09002199
Jiyong Parke3833882020-02-17 17:28:10 +09002200 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002201}
2202
Jiyong Parkf1691d22021-03-29 20:11:58 +09002203func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002204 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002205 var kind android.SdkKind
2206 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002207 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002208 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002209 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002210 // We don't have prebuilt SDK for the specific sdkVersion.
2211 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002212 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002213 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002214 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002215
2216 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002217 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002218 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002219 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002220 if ctx.Config().AllowMissingDependencies() {
2221 return android.Paths{android.PathForSource(ctx, jar)}
2222 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002223 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002224 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002225 return nil
2226 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002227 return android.Paths{jarPath.Path()}
2228}
2229
Colin Crossaede88c2020-08-11 12:17:01 -07002230// Check to see if the other module is within the same set of named APEXes as this module.
Paul Duffin9b879592020-05-26 13:21:35 +01002231//
2232// If either this or the other module are on the platform then this will return
2233// false.
Colin Cross56a83212020-09-15 18:30:11 -07002234func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
Colin Crossff694a82023-12-13 15:54:49 -08002235 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross313aa542023-12-13 13:47:44 -08002236 otherApexInfo, _ := android.OtherModuleProvider(ctx, other, android.ApexInfoProvider)
Jiyong Parkab50b072021-05-12 17:13:56 +09002237 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002238}
2239
Jiyong Parkf1691d22021-03-29 20:11:58 +09002240func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002241 // If the client doesn't set sdk_version, but if this library prefers stubs over
2242 // the impl library, let's provide the widest API surface possible. To do so,
2243 // force override sdk_version to module_current so that the closest possible API
2244 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002245 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002246 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002247 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002248
Paul Duffindaaa3322020-05-26 18:13:57 +01002249 // Only provide access to the implementation library if it is actually built.
2250 if module.requiresRuntimeImplementationLibrary() {
2251 // Check any special cases for java_sdk_library.
2252 //
2253 // Only allow access to the implementation library in the following condition:
2254 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002255 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002256 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01002257 if headerJars {
2258 return module.HeaderJars()
2259 } else {
2260 return module.ImplementationJars()
2261 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002262 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002263 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002264
Paul Duffin23970f42020-05-20 14:20:02 +01002265 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002266}
2267
Sundong Ahn241cd372018-07-13 16:16:44 +09002268// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002269func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002270 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
2271}
2272
2273// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002274func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002275 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09002276}
2277
Colin Cross571cccf2019-02-04 11:22:08 -08002278var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2279
Jiyong Park82484c02018-04-23 21:41:26 +09002280func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002281 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002282 return &[]string{}
2283 }).(*[]string)
2284}
2285
Paul Duffin749f98f2019-12-30 17:23:46 +00002286func (module *SdkLibrary) getApiDir() string {
2287 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2288}
2289
Jiyong Parkc678ad32018-04-10 13:07:10 +09002290// For a java_sdk_library module, create internal modules for stubs, docs,
2291// runtime libs and xml file. If requested, the stubs and docs are created twice
2292// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002293func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2294 // If the module has been disabled then don't create any child modules.
2295 if !module.Enabled() {
2296 return
2297 }
2298
Paul Duffina18abc22020-05-16 18:54:24 +01002299 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002300 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002301 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002302 }
2303
Paul Duffin37e0b772019-12-30 17:20:10 +00002304 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002305 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002306 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002307 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002308 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002309
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002310 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002311
Paul Duffin3375e352020-04-28 10:44:03 +01002312 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002313
Paul Duffin749f98f2019-12-30 17:23:46 +00002314 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002315 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002316 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002317 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002318 p := android.ExistentPathForSource(mctx, path)
2319 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002320 if mctx.Config().AllowMissingDependencies() {
2321 mctx.AddMissingDependencies([]string{path})
2322 } else {
2323 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2324 missingCurrentApi = true
2325 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002326 }
2327 }
2328 }
2329
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002330 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002331 script := "build/soong/scripts/gen-java-current-api-files.sh"
2332 p := android.ExistentPathForSource(mctx, script)
2333
2334 if !p.Valid() {
2335 panic(fmt.Sprintf("script file %s doesn't exist", script))
2336 }
2337
2338 mctx.ModuleErrorf("One or more current api files are missing. "+
2339 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002340 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002341 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002342 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002343 return
2344 }
2345
Paul Duffin3375e352020-04-28 10:44:03 +01002346 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002347 // Use the stubs source name for legacy reasons.
2348 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002349
Paul Duffind1b3a922020-01-22 11:57:20 +00002350 module.createStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002351 module.createExportableStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002352
Jihoon Kang0c705a42023-08-02 06:44:57 +00002353 alternativeFullApiSurfaceStubLib := ""
2354 if scope == apiScopePublic {
2355 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
2356 }
2357 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
Jihoon Kang1147b312023-06-08 23:25:57 +00002358 if contributesToApiSurface {
Jihoon Kang0c705a42023-08-02 06:44:57 +00002359 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002360 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002361
2362 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002363 module.createTopLevelExportableStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09002364 }
2365
Paul Duffindfa131e2020-05-15 20:37:11 +01002366 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002367 // Create child module to create an implementation library.
2368 //
2369 // This temporarily creates a second implementation library that can be explicitly
2370 // referenced.
2371 //
2372 // TODO(b/156618935) - update comment once only one implementation library is created.
2373 module.createImplLibrary(mctx)
2374
Paul Duffindfa131e2020-05-15 20:37:11 +01002375 // Only create an XML permissions file that declares the library as being usable
2376 // as a shared library if required.
2377 if module.sharedLibrary() {
2378 module.createXmlFile(mctx)
2379 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002380
2381 // record java_sdk_library modules so that they are exported to make
2382 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2383 javaSdkLibrariesLock.Lock()
2384 defer javaSdkLibrariesLock.Unlock()
2385 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2386 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002387
Paul Duffin77590a82022-04-28 14:13:30 +00002388 // 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 +01002389 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002390 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002391}
2392
2393func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002394 module.addHostAndDeviceProperties()
2395 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002396
Paul Duffin71b33cc2021-06-23 11:39:47 +01002397 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002398
Paul Duffina18abc22020-05-16 18:54:24 +01002399 module.properties.Installable = proptools.BoolPtr(true)
2400 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002401}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002402
Paul Duffindfa131e2020-05-15 20:37:11 +01002403func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2404 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2405}
2406
Jiyong Park932cdfe2020-05-28 00:19:53 +09002407func (module *SdkLibrary) defaultsToStubs() bool {
2408 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2409}
2410
Paul Duffin1b1e8062020-05-08 13:44:43 +01002411// Defines how to name the individual component modules the sdk library creates.
2412type sdkLibraryComponentNamingScheme interface {
2413 stubsLibraryModuleName(scope *apiScope, baseName string) string
2414
2415 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002416
2417 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002418
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002419 sourceStubsLibraryModuleName(scope *apiScope, baseName string) string
2420
2421 exportableStubsLibraryModuleName(scope *apiScope, baseName string) string
2422
2423 exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002424}
2425
2426type defaultNamingScheme struct {
2427}
2428
2429func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2430 return scope.stubsLibraryModuleName(baseName)
2431}
2432
2433func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2434 return scope.stubsSourceModuleName(baseName)
2435}
2436
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002437func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2438 return scope.apiLibraryModuleName(baseName)
2439}
2440
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002441func (s *defaultNamingScheme) sourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
Jihoon Kang1147b312023-06-08 23:25:57 +00002442 return scope.sourceStubLibraryModuleName(baseName)
2443}
2444
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002445func (s *defaultNamingScheme) exportableStubsLibraryModuleName(scope *apiScope, baseName string) string {
2446 return scope.exportableStubsLibraryModuleName(baseName)
2447}
2448
2449func (s *defaultNamingScheme) exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
2450 return scope.exportableSourceStubsLibraryModuleName(baseName)
2451}
2452
Paul Duffin1b1e8062020-05-08 13:44:43 +01002453var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2454
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002455func hasStubsLibrarySuffix(name string, apiScope *apiScope) bool {
2456 return strings.HasSuffix(name, apiScope.stubsLibraryModuleNameSuffix()) ||
2457 strings.HasSuffix(name, apiScope.exportableStubsLibraryModuleNameSuffix())
2458}
2459
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002460func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002461 name = strings.TrimSuffix(name, ".from-source")
2462
Anton Hansson2d0c1942020-05-25 12:20:51 +01002463 // This suffix-based approach is fragile and could potentially mis-trigger.
2464 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002465 if hasStubsLibrarySuffix(name, apiScopePublic) {
Anton Hansson08f476b2021-04-07 15:32:19 +01002466 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2467 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2468 return false, javaPlatform
2469 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002470 return true, javaSdk
2471 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002472 if hasStubsLibrarySuffix(name, apiScopeSystem) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002473 return true, javaSystem
2474 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002475 if hasStubsLibrarySuffix(name, apiScopeModuleLib) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002476 return true, javaModule
2477 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002478 if hasStubsLibrarySuffix(name, apiScopeTest) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002479 return true, javaSystem
2480 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002481 if hasStubsLibrarySuffix(name, apiScopeSystemServer) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002482 return true, javaSystemServer
2483 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002484 return false, javaPlatform
2485}
2486
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002487// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2488// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2489// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2490// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2491// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002492func SdkLibraryFactory() android.Module {
2493 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002494
2495 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002496 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002497
Inseob Kimc0907f12019-02-08 21:00:45 +09002498 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002499 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002500 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002501
2502 // Initialize the map from scope to scope specific properties.
2503 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2504 for _, scope := range allApiScopes {
2505 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2506 }
2507 module.scopeToProperties = scopeToProperties
2508
Paul Duffin4911a892020-04-29 23:35:13 +01002509 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002510 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002511 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2512 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2513
Paul Duffin1b1e8062020-05-08 13:44:43 +01002514 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002515 // If no implementation is required then it cannot be used as a shared library
2516 // either.
2517 if !module.requiresRuntimeImplementationLibrary() {
2518 // If shared_library has been explicitly set to true then it is incompatible
2519 // with api_only: true.
2520 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2521 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2522 }
2523 // Set shared_library: false.
2524 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2525 }
2526
Paul Duffin1b1e8062020-05-08 13:44:43 +01002527 if module.initCommonAfterDefaultsApplied(ctx) {
2528 module.CreateInternalModules(ctx)
2529 }
2530 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002531 return module
2532}
Colin Cross79c7c262019-04-17 11:11:46 -07002533
2534//
2535// SDK library prebuilts
2536//
2537
Paul Duffin56d44902020-01-31 13:36:25 +00002538// Properties associated with each api scope.
2539type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002540 Jars []string `android:"path"`
2541
2542 Sdk_version *string
2543
Colin Cross79c7c262019-04-17 11:11:46 -07002544 // List of shared java libs that this module has dependencies to
2545 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002546
Paul Duffinc8782502020-04-29 20:45:27 +01002547 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002548 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002549
2550 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002551 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002552
2553 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002554 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002555
2556 // Annotation zip
2557 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002558}
2559
Paul Duffin56d44902020-01-31 13:36:25 +00002560type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002561 // List of shared java libs, common to all scopes, that this module has
2562 // dependencies to
2563 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002564
2565 // If set to true, compile dex files for the stubs. Defaults to false.
2566 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002567
2568 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002569 Permitted_packages []string
Spandan Das23956d12024-01-19 00:22:22 +00002570
2571 // Name of the source soong module that gets shadowed by this prebuilt
2572 // If unspecified, follows the naming convention that the source module of
2573 // the prebuilt is Name() without "prebuilt_" prefix
2574 Source_module_name *string
Paul Duffin56d44902020-01-31 13:36:25 +00002575}
2576
Paul Duffineedc5d52020-06-12 17:46:39 +01002577type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002578 android.ModuleBase
2579 android.DefaultableModuleBase
2580 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002581 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002582
Paul Duffin37856732021-02-26 14:24:15 +00002583 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002584 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002585
Colin Cross79c7c262019-04-17 11:11:46 -07002586 properties sdkLibraryImportProperties
2587
Paul Duffin46a26a82020-04-07 19:27:04 +01002588 // Map from api scope to the scope specific property structure.
2589 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2590
Paul Duffin56d44902020-01-31 13:36:25 +00002591 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002592
2593 // The reference to the implementation library created by the source module.
2594 // Is nil if the source module does not exist.
2595 implLibraryModule *Library
2596
2597 // The reference to the xml permissions module created by the source module.
2598 // Is nil if the source module does not exist.
2599 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002600
Jeongik Chad5fe8782021-07-08 01:13:11 +09002601 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00002602 dexJarFile OptionalDexJarPath
2603 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09002604
2605 // Expected install file path of the source module(sdk_library)
2606 // or dex implementation jar obtained from the prebuilt_apex, if any.
2607 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002608}
2609
Paul Duffineedc5d52020-06-12 17:46:39 +01002610var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002611
Paul Duffin46a26a82020-04-07 19:27:04 +01002612// The type of a structure that contains a field of type sdkLibraryScopeProperties
2613// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002614//
2615// struct {
2616// Public sdkLibraryScopeProperties
2617// System sdkLibraryScopeProperties
2618// ...
2619// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002620var allScopeStructType = createAllScopePropertiesStructType()
2621
2622// Dynamically create a structure type for each apiscope in allApiScopes.
2623func createAllScopePropertiesStructType() reflect.Type {
2624 var fields []reflect.StructField
2625 for _, apiScope := range allApiScopes {
2626 field := reflect.StructField{
2627 Name: apiScope.fieldName,
2628 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2629 }
2630 fields = append(fields, field)
2631 }
2632
2633 return reflect.StructOf(fields)
2634}
2635
2636// Create an instance of the scope specific structure type and return a map
2637// from apiscope to a pointer to each scope specific field.
2638func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2639 allScopePropertiesPtr := reflect.New(allScopeStructType)
2640 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2641 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2642
2643 for _, apiScope := range allApiScopes {
2644 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2645 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2646 }
2647
2648 return allScopePropertiesPtr.Interface(), scopeProperties
2649}
2650
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002651// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002652func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002653 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002654
Paul Duffin46a26a82020-04-07 19:27:04 +01002655 allScopeProperties, scopeToProperties := createPropertiesInstance()
2656 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002657 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002658
Paul Duffinc3091c82020-05-08 14:16:20 +01002659 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002660 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002661
Paul Duffin0bdcb272020-02-06 15:24:57 +00002662 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002663 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002664 InitJavaModule(module, android.HostAndDeviceSupported)
2665
Paul Duffin1b1e8062020-05-08 13:44:43 +01002666 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2667 if module.initCommonAfterDefaultsApplied(mctx) {
2668 module.createInternalModules(mctx)
2669 }
2670 })
Colin Cross79c7c262019-04-17 11:11:46 -07002671 return module
2672}
2673
Paul Duffin630b11e2021-07-15 13:35:26 +01002674var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2675
2676func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2677 return module.properties.Permitted_packages
2678}
2679
Paul Duffineedc5d52020-06-12 17:46:39 +01002680func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002681 return &module.prebuilt
2682}
2683
Paul Duffineedc5d52020-06-12 17:46:39 +01002684func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002685 return module.prebuilt.Name(module.ModuleBase.Name())
2686}
2687
Spandan Das23956d12024-01-19 00:22:22 +00002688func (module *SdkLibraryImport) BaseModuleName() string {
2689 return proptools.StringDefault(module.properties.Source_module_name, module.ModuleBase.Name())
2690}
2691
Paul Duffineedc5d52020-06-12 17:46:39 +01002692func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002693
Paul Duffin50061512020-01-21 16:31:05 +00002694 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002695 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002696 module.prebuilt.ForcePrefer()
2697 }
2698
Paul Duffin46a26a82020-04-07 19:27:04 +01002699 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002700 if len(scopeProperties.Jars) == 0 {
2701 continue
2702 }
2703
Paul Duffinbbb546b2020-04-09 00:07:11 +01002704 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002705
Paul Duffin0f8faff2020-05-20 16:18:00 +01002706 if len(scopeProperties.Stub_srcs) > 0 {
2707 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2708 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002709
2710 if scopeProperties.Current_api != nil {
2711 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2712 }
Paul Duffin56d44902020-01-31 13:36:25 +00002713 }
Colin Cross79c7c262019-04-17 11:11:46 -07002714
2715 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2716 javaSdkLibrariesLock.Lock()
2717 defer javaSdkLibrariesLock.Unlock()
2718 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2719}
2720
Paul Duffineedc5d52020-06-12 17:46:39 +01002721func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002722 // Creates a java import for the jar with ".stubs" suffix
2723 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002724 Name *string
2725 Source_module_name *string
2726 Created_by_java_sdk_library_name *string
2727 Sdk_version *string
2728 Libs []string
2729 Jars []string
2730 Compile_dex *bool
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002731 Is_stubs_module *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002732
2733 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002734 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002735 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002736 props.Source_module_name = proptools.StringPtr(apiScope.stubsLibraryModuleName(module.BaseModuleName()))
2737 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002738 props.Sdk_version = scopeProperties.Sdk_version
2739 // Prepend any of the libs from the legacy public properties to the libs for each of the
2740 // scopes to avoid having to duplicate them in each scope.
2741 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2742 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002743
Paul Duffin38b57852020-05-13 16:08:09 +01002744 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002745 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002746
Paul Duffin1267d872021-04-16 17:21:36 +01002747 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002748 compileDex := module.properties.Compile_dex
2749 if module.stubLibrariesCompiledForDex() {
2750 compileDex = proptools.BoolPtr(true)
2751 }
2752 props.Compile_dex = compileDex
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002753 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffin1267d872021-04-16 17:21:36 +01002754
Paul Duffin859fe962020-05-15 10:20:31 +01002755 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002756}
2757
Paul Duffineedc5d52020-06-12 17:46:39 +01002758func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002759 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002760 Name *string
2761 Source_module_name *string
2762 Created_by_java_sdk_library_name *string
2763 Srcs []string
Paul Duffinbf4de042022-09-27 12:41:52 +01002764
2765 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002766 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002767 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002768 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()))
2769 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002770 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002771
2772 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002773 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2774
Spandan Das2cc80ba2023-10-27 17:21:52 +00002775 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002776}
2777
Jihoon Kang71c86832023-09-13 01:01:53 +00002778func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2779 api_file := scopeProperties.Current_api
2780 api_surface := &apiScope.name
2781
2782 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002783 Name *string
2784 Source_module_name *string
2785 Created_by_java_sdk_library_name *string
2786 Api_surface *string
2787 Api_file *string
2788 Visibility []string
Jihoon Kang71c86832023-09-13 01:01:53 +00002789 }{}
2790
2791 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
Spandan Das23956d12024-01-19 00:22:22 +00002792 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()) + ".api.contribution")
2793 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Jihoon Kang71c86832023-09-13 01:01:53 +00002794 props.Api_surface = api_surface
2795 props.Api_file = api_file
2796 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2797
Spandan Das2cc80ba2023-10-27 17:21:52 +00002798 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002799}
2800
Paul Duffin44f1d842020-06-26 20:17:02 +01002801// Add the dependencies on the child module in the component deps mutator so that it
2802// creates references to the prebuilt and not the source modules.
2803func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002804 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002805 if len(scopeProperties.Jars) == 0 {
2806 continue
2807 }
2808
2809 // Add dependencies to the prebuilt stubs library
Jihoon Kangb7431552024-01-22 19:40:08 +00002810 ctx.AddVariationDependencies(nil, apiScope.prebuiltStubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002811
2812 if len(scopeProperties.Stub_srcs) > 0 {
2813 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002814 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002815 }
Paul Duffin56d44902020-01-31 13:36:25 +00002816 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002817}
2818
2819// Add other dependencies as normal.
2820func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002821
2822 implName := module.implLibraryModuleName()
2823 if ctx.OtherModuleExists(implName) {
2824 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2825
2826 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2827 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2828 // Add dependency to the rule for generating the xml permissions file
2829 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2830 }
2831 }
Colin Cross79c7c262019-04-17 11:11:46 -07002832}
2833
Jiyong Park45bf82e2020-12-15 22:29:02 +09002834var _ android.ApexModule = (*SdkLibraryImport)(nil)
2835
2836// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002837func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2838 depTag := mctx.OtherModuleDependencyTag(dep)
2839 if depTag == xmlPermissionsFileTag {
2840 return true
2841 }
2842
2843 // None of the other dependencies of the java_sdk_library_import are in the same apex
2844 // as the one that references this module.
2845 return false
2846}
2847
Jiyong Park45bf82e2020-12-15 22:29:02 +09002848// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002849func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2850 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002851 // we don't check prebuilt modules for sdk_version
2852 return nil
2853}
2854
Paul Duffinea8f8082021-06-24 13:25:57 +01002855// Implements android.ApexModule
2856func (module *SdkLibraryImport) UniqueApexVariations() bool {
2857 return module.uniqueApexVariations()
2858}
2859
Paul Duffin09817d62022-04-28 17:45:11 +01002860// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002861func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2862 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002863}
2864
2865var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2866
Paul Duffineedc5d52020-06-12 17:46:39 +01002867func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002868 paths, err := module.commonOutputFiles(tag)
2869 if paths != nil || err != nil {
2870 return paths, err
2871 }
2872 if module.implLibraryModule != nil {
2873 return module.implLibraryModule.OutputFiles(tag)
2874 } else {
2875 return nil, nil
2876 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002877}
2878
Paul Duffineedc5d52020-06-12 17:46:39 +01002879func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002880 module.generateCommonBuildActions(ctx)
2881
Jeongik Chad5fe8782021-07-08 01:13:11 +09002882 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2883 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2884
Paul Duffin0f8faff2020-05-20 16:18:00 +01002885 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002886 ctx.VisitDirectDeps(func(to android.Module) {
2887 tag := ctx.OtherModuleDependencyTag(to)
2888
Paul Duffin0f8faff2020-05-20 16:18:00 +01002889 // Extract information from any of the scope specific dependencies.
2890 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2891 apiScope := scopeTag.apiScope
2892 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2893
2894 // Extract information from the dependency. The exact information extracted
2895 // is determined by the nature of the dependency which is determined by the tag.
2896 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002897 } else if tag == implLibraryTag {
2898 if implLibrary, ok := to.(*Library); ok {
2899 module.implLibraryModule = implLibrary
2900 } else {
2901 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2902 }
2903 } else if tag == xmlPermissionsFileTag {
2904 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2905 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2906 } else {
2907 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2908 }
Colin Cross79c7c262019-04-17 11:11:46 -07002909 }
2910 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002911
2912 // Populate the scope paths with information from the properties.
2913 for apiScope, scopeProperties := range module.scopeProperties {
2914 if len(scopeProperties.Jars) == 0 {
2915 continue
2916 }
2917
2918 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002919 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002920 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2921 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2922 }
Paul Duffin39853512021-02-26 11:09:39 +00002923
2924 if ctx.Device() {
2925 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2926 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08002927 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00002928 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002929 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00002930 di, err := android.FindDeapexerProviderForModule(ctx)
2931 if err != nil {
2932 // An error was found, possibly due to multiple apexes in the tree that export this library
2933 // Defer the error till a client tries to call DexJarBuildPath
2934 module.dexJarFileErr = err
Spandan Das3a392012024-01-17 18:26:27 +00002935 module.initHiddenAPIError(err)
Spandan Dasfae468e2023-12-12 23:23:53 +00002936 return
Martin Stjernholm44825602021-09-17 01:44:12 +01002937 }
Spandan Das5be63332023-12-13 00:06:32 +00002938 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(module.BaseModuleName())
Jiakai Zhang81e46812023-02-08 21:56:07 +08002939 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002940 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2941 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002942 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002943 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002944 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002945 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002946
Spandan Dase21a8d42024-01-23 23:56:29 +00002947 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), installPath)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002948 module.dexpreopter.isSDKLibrary = true
Spandan Dase21a8d42024-01-23 23:56:29 +00002949 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002950
2951 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2952 module.dexpreopter.inputProfilePathOnHost = profilePath
2953 }
Paul Duffin39853512021-02-26 11:09:39 +00002954 } else {
2955 // This should never happen as a variant for a prebuilt_apex is only created if the
2956 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002957 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002958 }
2959 }
2960 }
Colin Cross79c7c262019-04-17 11:11:46 -07002961}
2962
Jiyong Parkf1691d22021-03-29 20:11:58 +09002963func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002964
2965 // For consistency with SdkLibrary make the implementation jar available to libraries that
2966 // are within the same APEX.
2967 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002968 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002969 if headerJars {
2970 return implLibraryModule.HeaderJars()
2971 } else {
2972 return implLibraryModule.ImplementationJars()
2973 }
2974 }
2975
Paul Duffin23970f42020-05-20 14:20:02 +01002976 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002977}
2978
Colin Cross79c7c262019-04-17 11:11:46 -07002979// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002980func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002981 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002982 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002983}
2984
2985// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002986func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002987 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002988 return module.sdkJars(ctx, sdkVersion, false)
2989}
2990
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002991// to satisfy UsesLibraryDependency interface
Spandan Das59a4a2b2024-01-09 21:35:56 +00002992func (module *SdkLibraryImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002993 // The dex implementation jar extracted from the .apex file should be used in preference to the
2994 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00002995 if module.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002996 ctx.ModuleErrorf(module.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00002997 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002998 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002999 return module.dexJarFile
3000 }
Paul Duffineedc5d52020-06-12 17:46:39 +01003001 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01003002 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01003003 } else {
Spandan Das59a4a2b2024-01-09 21:35:56 +00003004 return module.implLibraryModule.DexJarBuildPath(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +01003005 }
3006}
3007
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003008// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01003009func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09003010 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01003011}
3012
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003013// to satisfy UsesLibraryDependency interface
3014func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
3015 return nil
3016}
3017
Paul Duffineedc5d52020-06-12 17:46:39 +01003018// to satisfy apex.javaDependency interface
3019func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
3020 if module.implLibraryModule == nil {
3021 return nil
3022 } else {
3023 return module.implLibraryModule.JacocoReportClassesFile()
3024 }
3025}
3026
3027// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07003028func (module *SdkLibraryImport) LintDepSets() LintDepSets {
3029 if module.implLibraryModule == nil {
3030 return LintDepSets{}
3031 } else {
3032 return module.implLibraryModule.LintDepSets()
3033 }
3034}
3035
Spandan Das17854f52022-01-14 21:19:14 +00003036func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003037 if module.implLibraryModule == nil {
3038 return false
3039 } else {
Spandan Das17854f52022-01-14 21:19:14 +00003040 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003041 }
3042}
3043
Spandan Das17854f52022-01-14 21:19:14 +00003044func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003045 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00003046 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003047 }
3048}
3049
Colin Cross08dca382020-07-21 20:31:17 -07003050// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01003051func (module *SdkLibraryImport) Stem() string {
3052 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07003053}
Jiyong Parke3833882020-02-17 17:28:10 +09003054
Paul Duffin44b481b2020-06-17 16:59:43 +01003055var _ ApexDependency = (*SdkLibraryImport)(nil)
3056
3057// to satisfy java.ApexDependency interface
3058func (module *SdkLibraryImport) HeaderJars() android.Paths {
3059 if module.implLibraryModule == nil {
3060 return nil
3061 } else {
3062 return module.implLibraryModule.HeaderJars()
3063 }
3064}
3065
3066// to satisfy java.ApexDependency interface
3067func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
3068 if module.implLibraryModule == nil {
3069 return nil
3070 } else {
3071 return module.implLibraryModule.ImplementationAndResourcesJars()
3072 }
3073}
3074
Jiakai Zhang204356f2021-09-09 08:12:46 +00003075// to satisfy java.DexpreopterInterface interface
3076func (module *SdkLibraryImport) IsInstallable() bool {
3077 return true
3078}
3079
Paul Duffinfef55002021-06-17 14:56:05 +01003080var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
3081
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01003082func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01003083 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08003084 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01003085}
3086
Spandan Das2ea84dd2024-01-25 22:12:50 +00003087func (j *SdkLibraryImport) UseProfileGuidedDexpreopt() bool {
3088 return proptools.Bool(j.importDexpreoptProperties.Dex_preopt.Profile_guided)
3089}
3090
Jiyong Parke3833882020-02-17 17:28:10 +09003091// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09003092type sdkLibraryXml struct {
3093 android.ModuleBase
3094 android.DefaultableModuleBase
3095 android.ApexModuleBase
3096
3097 properties sdkLibraryXmlProperties
3098
3099 outputFilePath android.OutputPath
3100 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07003101
3102 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09003103}
3104
3105type sdkLibraryXmlProperties struct {
3106 // canonical name of the lib
3107 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003108
3109 // Signals that this shared library is part of the bootclasspath starting
3110 // on the version indicated in this attribute.
3111 //
3112 // This will make platforms at this level and above to ignore
3113 // <uses-library> tags with this library name because the library is already
3114 // available
3115 On_bootclasspath_since *string
3116
3117 // Signals that this shared library was part of the bootclasspath before
3118 // (but not including) the version indicated in this attribute.
3119 //
3120 // The system will automatically add a <uses-library> tag with this library to
3121 // apps that target any SDK less than the version indicated in this attribute.
3122 On_bootclasspath_before *string
3123
3124 // Indicates that PackageManager should ignore this shared library if the
3125 // platform is below the version indicated in this attribute.
3126 //
3127 // This means that the device won't recognise this library as installed.
3128 Min_device_sdk *string
3129
3130 // Indicates that PackageManager should ignore this shared library if the
3131 // platform is above the version indicated in this attribute.
3132 //
3133 // This means that the device won't recognise this library as installed.
3134 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00003135
3136 // The SdkLibrary's min api level as a string
3137 //
3138 // This value comes from the ApiLevel of the MinSdkVersion property.
3139 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00003140
3141 // Uses-libs dependencies that the shared library requires to work correctly.
3142 //
3143 // This will add dependency="foo:bar" to the <library> section.
3144 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09003145}
3146
3147// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
3148// Not to be used directly by users. java_sdk_library internally uses this.
3149func sdkLibraryXmlFactory() android.Module {
3150 module := &sdkLibraryXml{}
3151
3152 module.AddProperties(&module.properties)
3153
3154 android.InitApexModule(module)
3155 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
3156
3157 return module
3158}
3159
Colin Crossaede88c2020-08-11 12:17:01 -07003160func (module *sdkLibraryXml) UniqueApexVariations() bool {
3161 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
3162 // mounted APEX, which contains the name of the APEX.
3163 return true
3164}
3165
Jiyong Parke3833882020-02-17 17:28:10 +09003166// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09003167func (module *sdkLibraryXml) BaseDir() string {
3168 return "etc"
3169}
3170
3171// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09003172func (module *sdkLibraryXml) SubDir() string {
3173 return "permissions"
3174}
3175
3176// from android.PrebuiltEtcModule
3177func (module *sdkLibraryXml) OutputFile() android.OutputPath {
3178 return module.outputFilePath
3179}
3180
3181// from android.ApexModule
3182func (module *sdkLibraryXml) AvailableFor(what string) bool {
3183 return true
3184}
3185
3186func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
3187 // do nothing
3188}
3189
Jiyong Park45bf82e2020-12-15 22:29:02 +09003190var _ android.ApexModule = (*sdkLibraryXml)(nil)
3191
3192// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003193func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3194 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003195 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
3196 return nil
3197}
3198
Jiyong Parke3833882020-02-17 17:28:10 +09003199// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07003200func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09003201 implName := proptools.String(module.properties.Lib_name)
Colin Crossff694a82023-12-13 15:54:49 -08003202 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07003203 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09003204 // In most cases, this works fine. But when apex_name is set or override_apex is used
3205 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07003206 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09003207 }
3208 partition := "system"
3209 if module.SocSpecific() {
3210 partition = "vendor"
3211 } else if module.DeviceSpecific() {
3212 partition = "odm"
3213 } else if module.ProductSpecific() {
3214 partition = "product"
3215 } else if module.SystemExtSpecific() {
3216 partition = "system_ext"
3217 }
3218 return "/" + partition + "/framework/" + implName + ".jar"
3219}
3220
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003221func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
3222 if value == nil {
3223 return ""
3224 }
3225 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
3226 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003227 // attributes in bp files have underscores but in the xml have dashes.
3228 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003229 return ""
3230 }
Pedro Loureirob638c622021-12-22 15:28:05 +00003231 if apiLevel.IsCurrent() {
3232 // passing "current" would always mean a future release, never the current (or the current in
3233 // progress) which means some conditions would never be triggered.
3234 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3235 `"current" is not an allowed value for this attribute`)
3236 return ""
3237 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003238 // "safeValue" is safe because it translates finalized codenames to a string
3239 // with their SDK int.
3240 safeValue := apiLevel.String()
3241 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003242}
3243
3244// formats an attribute for the xml permissions file if the value is not null
3245// returns empty string otherwise
3246func formattedOptionalAttribute(attrName string, value *string) string {
3247 if value == nil {
3248 return ""
3249 }
3250 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
3251}
3252
Jamie Garsidee570ace2023-11-27 12:07:36 +00003253func formattedDependenciesAttribute(dependencies []string) string {
3254 if dependencies == nil {
3255 return ""
3256 }
3257 return fmt.Sprintf(` dependency=\"%s\"\n`, strings.Join(dependencies, ":"))
3258}
3259
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003260func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3261 libName := proptools.String(module.properties.Lib_name)
3262 libNameAttr := formattedOptionalAttribute("name", &libName)
3263 filePath := module.implPath(ctx)
3264 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003265 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3266 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3267 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3268 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003269 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003270 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3271 // similarly, min_device_sdk is only understood from T. So if a library is using that, we need to use the apex-library to make sure this library is not loaded before T
Pedro Loureiroc3621422021-09-28 15:40:23 +00003272 var libraryTag string
3273 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003274 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00003275 } else {
3276 libraryTag = ` <library\n`
3277 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003278
3279 return strings.Join([]string{
3280 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
3281 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
3282 `\n`,
3283 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
3284 ` you may not use this file except in compliance with the License.\n`,
3285 ` You may obtain a copy of the License at\n`,
3286 `\n`,
3287 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
3288 `\n`,
3289 ` Unless required by applicable law or agreed to in writing, software\n`,
3290 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
3291 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
3292 ` See the License for the specific language governing permissions and\n`,
3293 ` limitations under the License.\n`,
3294 `-->\n`,
3295 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00003296 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003297 libNameAttr,
3298 filePathAttr,
3299 implicitFromAttr,
3300 implicitUntilAttr,
3301 minSdkAttr,
3302 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003303 dependenciesAttr,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003304 ` />\n`,
3305 `</permissions>\n`}, "")
3306}
3307
Jiyong Parke3833882020-02-17 17:28:10 +09003308func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossff694a82023-12-13 15:54:49 -08003309 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
3310 module.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07003311
Jiyong Parke3833882020-02-17 17:28:10 +09003312 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003313 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003314 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003315
3316 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08003317 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003318 rule.Command().
3319 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
3320 Output(module.outputFilePath)
3321
Colin Crossf1a035e2020-11-16 17:32:30 -08003322 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09003323
3324 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
3325}
3326
3327func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003328 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003329 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003330 Disabled: true,
3331 }}
3332 }
3333
satayev8f088b02021-12-06 11:40:46 +00003334 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003335 Class: "ETC",
3336 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3337 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003338 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003339 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003340 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003341 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3342 },
3343 },
3344 }}
3345}
Paul Duffindd46f712020-02-10 13:37:10 +00003346
Pedro Loureiroc3621422021-09-28 15:40:23 +00003347func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3348 module.validateAtLeastTAttributes(ctx)
3349 module.validateMinAndMaxDeviceSdk(ctx)
3350 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3351 module.validateOnBootclasspathBeforeRequirements(ctx)
3352}
3353
3354func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3355 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3356 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3357 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3358 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3359 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3360}
3361
3362func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3363 if attr != nil {
3364 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3365 // we will inform the user of invalid inputs when we try to write the
3366 // permissions xml file so we don't need to do it here
3367 if t.GreaterThan(level) {
3368 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3369 }
3370 }
3371 }
3372}
3373
3374func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3375 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3376 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3377 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3378 if minErr == nil && maxErr == nil {
3379 // we will inform the user of invalid inputs when we try to write the
3380 // permissions xml file so we don't need to do it here
3381 if min.GreaterThan(max) {
3382 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3383 }
3384 }
3385 }
3386}
3387
3388func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3389 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3390 if module.properties.Min_device_sdk != nil {
3391 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3392 if err == nil {
3393 if moduleMinApi.GreaterThan(api) {
3394 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3395 }
3396 }
3397 }
3398 if module.properties.Max_device_sdk != nil {
3399 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3400 if err == nil {
3401 if moduleMinApi.GreaterThan(api) {
3402 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3403 }
3404 }
3405 }
3406}
3407
3408func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3409 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3410 if module.properties.On_bootclasspath_before != nil {
3411 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3412 // if we use the attribute, then we need to do this validation
3413 if moduleMinApi.LessThan(t) {
3414 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3415 if module.properties.Min_device_sdk == nil {
3416 ctx.PropertyErrorf("on_bootclasspath_before", "Using this property requires that the module's min_sdk_version or the shared library's min_device_sdk is at least T")
3417 }
3418 }
3419 }
3420}
3421
Paul Duffindd46f712020-02-10 13:37:10 +00003422type sdkLibrarySdkMemberType struct {
3423 android.SdkMemberTypeBase
3424}
3425
Paul Duffin296701e2021-07-14 10:29:36 +01003426func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3427 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003428}
3429
3430func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3431 _, ok := module.(*SdkLibrary)
3432 return ok
3433}
3434
3435func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3436 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3437}
3438
3439func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3440 return &sdkLibrarySdkMemberProperties{}
3441}
3442
Paul Duffin976b0e52021-04-27 23:20:26 +01003443var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3444 android.SdkMemberTypeBase{
3445 PropertyName: "java_sdk_libs",
3446 SupportsSdk: true,
3447 },
3448}
3449
Paul Duffindd46f712020-02-10 13:37:10 +00003450type sdkLibrarySdkMemberProperties struct {
3451 android.SdkMemberPropertiesBase
3452
Paul Duffine8409952022-09-22 16:24:46 +01003453 // Stem name for files in the sdk snapshot.
3454 //
3455 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3456 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3457 //
3458 // This property is marked as keep so that it will be kept in all instances of this struct, will
3459 // not be cleared but will be copied to common structs. That is needed because this field is used
3460 // to construct many file names for other parts of this struct and so it needs to be present in
3461 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3462 // be unavailable for generating file names if there were other properties that were still set.
3463 Stem string `sdk:"keep"`
3464
Paul Duffindd46f712020-02-10 13:37:10 +00003465 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003466 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003467
Paul Duffin3d1248c2020-04-09 00:10:17 +01003468 // The Java stubs source files.
3469 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003470
3471 // The naming scheme.
3472 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003473
3474 // True if the java_sdk_library_import is for a shared library, false
3475 // otherwise.
3476 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003477
Paul Duffin1267d872021-04-16 17:21:36 +01003478 // True if the stub imports should produce dex jars.
3479 Compile_dex *bool
3480
Paul Duffina2ae7e02020-09-11 11:55:00 +01003481 // The paths to the doctag files to add to the prebuilt.
3482 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003483
3484 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003485
3486 // Signals that this shared library is part of the bootclasspath starting
3487 // on the version indicated in this attribute.
3488 //
3489 // This will make platforms at this level and above to ignore
3490 // <uses-library> tags with this library name because the library is already
3491 // available
3492 On_bootclasspath_since *string
3493
3494 // Signals that this shared library was part of the bootclasspath before
3495 // (but not including) the version indicated in this attribute.
3496 //
3497 // The system will automatically add a <uses-library> tag with this library to
3498 // apps that target any SDK less than the version indicated in this attribute.
3499 On_bootclasspath_before *string
3500
3501 // Indicates that PackageManager should ignore this shared library if the
3502 // platform is below the version indicated in this attribute.
3503 //
3504 // This means that the device won't recognise this library as installed.
3505 Min_device_sdk *string
3506
3507 // Indicates that PackageManager should ignore this shared library if the
3508 // platform is above the version indicated in this attribute.
3509 //
3510 // This means that the device won't recognise this library as installed.
3511 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003512
3513 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003514}
3515
3516type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003517 Jars android.Paths
3518 StubsSrcJar android.Path
3519 CurrentApiFile android.Path
3520 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003521 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003522 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003523}
3524
3525func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3526 sdk := variant.(*SdkLibrary)
3527
Paul Duffine8409952022-09-22 16:24:46 +01003528 // Copy the stem name for files in the sdk snapshot.
3529 s.Stem = sdk.distStem()
3530
Paul Duffin106a3a42022-01-27 16:39:06 +00003531 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003532 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003533 paths := sdk.findScopePaths(apiScope)
3534 if paths == nil {
3535 continue
3536 }
3537
Paul Duffindd46f712020-02-10 13:37:10 +00003538 jars := paths.stubsImplPath
3539 if len(jars) > 0 {
3540 properties := scopeProperties{}
3541 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003542 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003543 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003544 if paths.currentApiFilePath.Valid() {
3545 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3546 }
3547 if paths.removedApiFilePath.Valid() {
3548 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3549 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003550 // The annotations zip is only available for modules that set annotations_enabled: true.
3551 if paths.annotationsZip.Valid() {
3552 properties.AnnotationsZip = paths.annotationsZip.Path()
3553 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003554 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003555 }
3556 }
3557
Paul Duffindfa131e2020-05-15 20:37:11 +01003558 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003559 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003560 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003561 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003562 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003563 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3564 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3565 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3566 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003567
3568 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3569 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3570 }
Paul Duffindd46f712020-02-10 13:37:10 +00003571}
3572
3573func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003574 if s.Naming_scheme != nil {
3575 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3576 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003577 if s.Shared_library != nil {
3578 propertySet.AddProperty("shared_library", *s.Shared_library)
3579 }
Paul Duffin1267d872021-04-16 17:21:36 +01003580 if s.Compile_dex != nil {
3581 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3582 }
Paul Duffin869de142021-07-15 14:14:41 +01003583 if len(s.Permitted_packages) > 0 {
3584 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3585 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003586 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3587 if s.DexPreoptProfileGuided != nil {
3588 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3589 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003590
Paul Duffine8409952022-09-22 16:24:46 +01003591 stem := s.Stem
3592
Paul Duffindd46f712020-02-10 13:37:10 +00003593 for _, apiScope := range allApiScopes {
3594 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003595 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003596
Paul Duffin958806b2022-05-16 13:10:47 +00003597 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003598
Paul Duffindd46f712020-02-10 13:37:10 +00003599 var jars []string
3600 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003601 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003602 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3603 jars = append(jars, dest)
3604 }
3605 scopeSet.AddProperty("jars", jars)
3606
Paul Duffin22628d52021-05-12 23:13:22 +01003607 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3608 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003609 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003610 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3611 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3612 } else {
3613 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3614 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003615 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003616 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3617 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3618 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003619
Paul Duffin1fd005d2020-04-09 01:08:11 +01003620 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003621 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003622 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3623 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3624 }
3625
3626 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003627 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003628 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003629 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3630 }
3631
Anton Hanssond78eb762021-09-21 15:25:12 +01003632 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003633 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003634 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3635 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3636 }
3637
Paul Duffindd46f712020-02-10 13:37:10 +00003638 if properties.SdkVersion != "" {
3639 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3640 }
3641 }
3642 }
3643
Paul Duffina2ae7e02020-09-11 11:55:00 +01003644 if len(s.Doctag_paths) > 0 {
3645 dests := []string{}
3646 for _, p := range s.Doctag_paths {
3647 dest := filepath.Join("doctags", p.Rel())
3648 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3649 dests = append(dests, dest)
3650 }
3651 propertySet.AddProperty("doctag_files", dests)
3652 }
Paul Duffindd46f712020-02-10 13:37:10 +00003653}