blob: 74e2da42caa2ee60c6d3728f1cf44a6ce3b30d3d [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 (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010021 "reflect"
Paul Duffin46dc45a2020-05-14 15:39:10 +010022 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090023 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090024 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090025 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090026
Paul Duffind1b3a922020-01-22 11:57:20 +000027 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090028 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010029
30 "android/soong/android"
Ulya Trafimovichdbf31662020-12-17 12:07:54 +000031 "android/soong/dexpreopt"
Jiyong Parkc678ad32018-04-10 13:07:10 +090032)
33
Jooyung Han58f26ab2019-12-18 15:34:32 +090034const (
Pedro Loureiro9956e5e2021-09-07 17:21:59 +000035 sdkXmlFileSuffix = ".xml"
Jiyong Parkc678ad32018-04-10 13:07:10 +090036)
37
Paul Duffind1b3a922020-01-22 11:57:20 +000038// A tag to associated a dependency with a specific api scope.
39type scopeDependencyTag struct {
40 blueprint.BaseDependencyTag
41 name string
42 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010043
44 // Function for extracting appropriate path information from the dependency.
Colin Crossdcf71b22021-02-01 13:59:03 -080045 depInfoExtractor func(paths *scopePaths, ctx android.ModuleContext, dep android.Module) error
Paul Duffinc8782502020-04-29 20:45:27 +010046}
47
48// Extract tag specific information from the dependency.
49func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
Colin Crossdcf71b22021-02-01 13:59:03 -080050 err := tag.depInfoExtractor(paths, ctx, dep)
Paul Duffinc8782502020-04-29 20:45:27 +010051 if err != nil {
52 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
53 }
Paul Duffind1b3a922020-01-22 11:57:20 +000054}
55
Paul Duffin80342d72020-06-26 22:08:43 +010056var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
57
58func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
59 return false
60}
61
Paul Duffind1b3a922020-01-22 11:57:20 +000062// Provides information about an api scope, e.g. public, system, test.
63type apiScope struct {
64 // The name of the api scope, e.g. public, system, test
65 name string
66
Paul Duffin97b53b82020-05-05 14:40:52 +010067 // The api scope that this scope extends.
Paul Duffind0b9fca2022-09-30 18:11:41 +010068 //
69 // This organizes the scopes into an extension hierarchy.
70 //
71 // If set this means that the API provided by this scope includes the API provided by the scope
72 // set in this field.
Paul Duffin97b53b82020-05-05 14:40:52 +010073 extends *apiScope
74
Paul Duffind0b9fca2022-09-30 18:11:41 +010075 // The next api scope that a library that uses this scope can access.
76 //
77 // This organizes the scopes into an access hierarchy.
78 //
79 // If set this means that a library that can access this API can also access the API provided by
80 // the scope set in this field.
81 //
82 // A module that sets sdk_version: "<scope>_current" should have access to the <scope> API of
83 // every java_sdk_library that it depends on. If the library does not provide an API for <scope>
84 // then it will traverse up this access hierarchy to find an API that it does provide.
85 //
86 // If this is not set then it defaults to the scope set in extends.
87 canAccess *apiScope
88
Paul Duffin3375e352020-04-28 10:44:03 +010089 // The legacy enabled status for a specific scope can be dependent on other
90 // properties that have been specified on the library so it is provided by
91 // a function that can determine the status by examining those properties.
92 legacyEnabledStatus func(module *SdkLibrary) bool
93
94 // The default enabled status for non-legacy behavior, which is triggered by
95 // explicitly enabling at least one api scope.
96 defaultEnabledStatus bool
97
98 // Gets a pointer to the scope specific properties.
99 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
100
Paul Duffin46a26a82020-04-07 19:27:04 +0100101 // The name of the field in the dynamically created structure.
102 fieldName string
103
Paul Duffin6b836ba2020-05-13 19:19:49 +0100104 // The name of the property in the java_sdk_library_import
105 propertyName string
106
Jihoon Kangbd093452023-12-26 19:08:01 +0000107 // The tag to use to depend on the stubs library module if the parent module
108 // does not differentiate everything and exportable stubs (e.g. sdk_library_import).
Paul Duffind1b3a922020-01-22 11:57:20 +0000109 stubsTag scopeDependencyTag
110
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)
Paul Duffind1b3a922020-01-22 11:57:20 +0000177 scope.stubsTag = 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
730 paths.stubsImplPath = lib.ImplementationJars
731
732 libDep := dep.(UsesLibraryDependency)
733 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
734 return nil
735 } else {
736 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
737 }
738}
739
740func (paths *scopePaths) extractExportableStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
741 if _, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
742 libDep := dep.(UsesLibraryDependency)
743 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
Paul Duffinc8782502020-04-29 20:45:27 +0100744 return nil
745 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800746 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100747 }
748}
749
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100750func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
751 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
752 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100753 return nil
754 } else {
755 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
756 }
757}
758
Paul Duffin0f8faff2020-05-20 16:18:00 +0100759func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
760 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
761 action(apiStubsProvider)
762 return nil
763 } else {
764 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
765 }
766}
767
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100768func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hanssond78eb762021-09-21 15:25:12 +0100769 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100770 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
771 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100772}
773
Colin Crossdcf71b22021-02-01 13:59:03 -0800774func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100775 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
776 paths.extractApiInfoFromApiStubsProvider(provider)
777 })
778}
779
Paul Duffin0f8faff2020-05-20 16:18:00 +0100780func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
781 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100782}
783
Colin Crossdcf71b22021-02-01 13:59:03 -0800784func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100785 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100786 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
787 })
788}
789
Colin Crossdcf71b22021-02-01 13:59:03 -0800790func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100791 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
792 paths.extractApiInfoFromApiStubsProvider(provider)
793 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
794 })
795}
796
Paul Duffin958806b2022-05-16 13:10:47 +0000797func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
798 var paths android.Paths
799 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
800 paths = sourceFileProducer.Srcs()
801 } else {
802 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
803 }
804 if len(paths) != 1 {
805 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
806 }
807 return android.OptionalPathForPath(paths[0]), nil
808}
809
810func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
811 outputPath, err := extractSingleOptionalOutputPath(dep)
812 paths.latestApiPath = outputPath
813 return err
814}
815
816func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
817 outputPath, err := extractSingleOptionalOutputPath(dep)
818 paths.latestRemovedApiPath = outputPath
819 return err
820}
821
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100822type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100823 // The naming scheme to use for the components that this module creates.
824 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100825 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100826 //
827 // This is a temporary mechanism to simplify conversion from separate modules for each
828 // component that follow a different naming pattern to the default one.
829 //
830 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100831 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100832
833 // Specifies whether this module can be used as an Android shared library; defaults
834 // to true.
835 //
836 // An Android shared library is one that can be referenced in a <uses-library> element
837 // in an AndroidManifest.xml.
838 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100839
840 // Files containing information about supported java doc tags.
841 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000842
843 // Signals that this shared library is part of the bootclasspath starting
844 // on the version indicated in this attribute.
845 //
846 // This will make platforms at this level and above to ignore
847 // <uses-library> tags with this library name because the library is already
848 // available
849 On_bootclasspath_since *string
850
851 // Signals that this shared library was part of the bootclasspath before
852 // (but not including) the version indicated in this attribute.
853 //
854 // The system will automatically add a <uses-library> tag with this library to
855 // apps that target any SDK less than the version indicated in this attribute.
856 On_bootclasspath_before *string
857
858 // Indicates that PackageManager should ignore this shared library if the
859 // platform is below the version indicated in this attribute.
860 //
861 // This means that the device won't recognise this library as installed.
862 Min_device_sdk *string
863
864 // Indicates that PackageManager should ignore this shared library if the
865 // platform is above the version indicated in this attribute.
866 //
867 // This means that the device won't recognise this library as installed.
868 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100869}
870
Paul Duffin71b33cc2021-06-23 11:39:47 +0100871// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
872// embeds the commonToSdkLibraryAndImport struct.
873type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000874 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100875
876 BaseModuleName() string
877}
878
Paul Duffin56d44902020-01-31 13:36:25 +0000879// Common code between sdk library and sdk library import
880type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100881 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100882
Paul Duffin56d44902020-01-31 13:36:25 +0000883 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100884
885 namingScheme sdkLibraryComponentNamingScheme
886
Paul Duffindfa131e2020-05-15 20:37:11 +0100887 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100888
Paul Duffina2ae7e02020-09-11 11:55:00 +0100889 // Paths to commonSdkLibraryProperties.Doctag_files
890 doctagPaths android.Paths
891
Paul Duffin859fe962020-05-15 10:20:31 +0100892 // Functionality related to this being used as a component of a java_sdk_library.
893 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000894}
895
Paul Duffin71b33cc2021-06-23 11:39:47 +0100896func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
897 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100898
Paul Duffin71b33cc2021-06-23 11:39:47 +0100899 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100900
901 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100902 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100903}
904
905func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100906 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100907 switch schemeProperty {
908 case "default":
909 c.namingScheme = &defaultNamingScheme{}
910 default:
911 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
912 return false
913 }
914
Paul Duffin3f0290e2021-06-30 18:25:36 +0100915 namePtr := proptools.StringPtr(c.module.BaseModuleName())
916 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
917
Paul Duffindfa131e2020-05-15 20:37:11 +0100918 // Only track this sdk library if this can be used as a shared library.
919 if c.sharedLibrary() {
920 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100921 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100922 }
Paul Duffin859fe962020-05-15 10:20:31 +0100923
Paul Duffin1b1e8062020-05-08 13:44:43 +0100924 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100925}
926
Paul Duffinea8f8082021-06-24 13:25:57 +0100927// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
928// method.
929func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
930 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
931 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
932 // the APEX and so it needs a unique variation per APEX.
933 return c.sharedLibrary()
934}
935
Paul Duffina2ae7e02020-09-11 11:55:00 +0100936func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
937 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
938}
939
Paul Duffineedc5d52020-06-12 17:46:39 +0100940// Module name of the runtime implementation library
941func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100942 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100943}
944
945// Module name of the XML file for the lib
946func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100947 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100948}
949
Paul Duffinc3091c82020-05-08 14:16:20 +0100950// Name of the java_library module that compiles the stubs source.
951func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100952 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000953 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100954}
955
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000956// Name of the java_library module that compiles the exportable stubs source.
957func (c *commonToSdkLibraryAndImport) exportableStubsLibraryModuleName(apiScope *apiScope) string {
958 baseName := c.module.BaseModuleName()
959 return c.namingScheme.exportableStubsLibraryModuleName(apiScope, baseName)
960}
961
Paul Duffinc3091c82020-05-08 14:16:20 +0100962// Name of the droidstubs module that generates the stubs source and may also
963// generate/check the API.
964func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100965 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000966 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100967}
968
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000969// Name of the java_api_library module that generates the from-text stubs source
970// and compiles to a jar file.
971func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
972 baseName := c.module.BaseModuleName()
973 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
974}
975
Jihoon Kang1147b312023-06-08 23:25:57 +0000976// Name of the java_library module that compiles the stubs
977// generated from source Java files.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000978func (c *commonToSdkLibraryAndImport) sourceStubsLibraryModuleName(apiScope *apiScope) string {
Jihoon Kang1147b312023-06-08 23:25:57 +0000979 baseName := c.module.BaseModuleName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000980 return c.namingScheme.sourceStubsLibraryModuleName(apiScope, baseName)
981}
982
983// Name of the java_library module that compiles the exportable stubs
984// generated from source Java files.
985func (c *commonToSdkLibraryAndImport) exportableSourceStubsLibraryModuleName(apiScope *apiScope) string {
986 baseName := c.module.BaseModuleName()
987 return c.namingScheme.exportableSourceStubsLibraryModuleName(apiScope, baseName)
Jihoon Kang1147b312023-06-08 23:25:57 +0000988}
989
Paul Duffin46dc45a2020-05-14 15:39:10 +0100990// The component names for different outputs of the java_sdk_library.
991//
992// They are similar to the names used for the child modules it creates
993const (
994 stubsSourceComponentName = "stubs.source"
995
996 apiTxtComponentName = "api.txt"
997
998 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +0100999
1000 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +01001001)
1002
1003// A regular expression to match tags that reference a specific stubs component.
1004//
1005// It will only match if given a valid scope and a valid component. It is verfy strict
1006// to ensure it does not accidentally match a similar looking tag that should be processed
1007// by the embedded Library.
1008var tagSplitter = func() *regexp.Regexp {
1009 // Given a list of literal string items returns a regular expression that will
1010 // match any one of the items.
1011 choice := func(items ...string) string {
1012 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
1013 }
1014
1015 // Regular expression to match one of the scopes.
1016 scopesRegexp := choice(allScopeNames...)
1017
1018 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +01001019 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001020
1021 // Regular expression to match any combination of one scope and one component.
1022 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
1023}()
1024
1025// For OutputFileProducer interface
1026//
Anton Hanssond78eb762021-09-21 15:25:12 +01001027// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001028func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
1029 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
1030 scopeName := groups[1]
1031 component := groups[2]
1032
1033 if scope, ok := scopeByName[scopeName]; ok {
1034 paths := c.findScopePaths(scope)
1035 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001036 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001037 }
1038
1039 switch component {
1040 case stubsSourceComponentName:
1041 if paths.stubsSrcJar.Valid() {
1042 return android.Paths{paths.stubsSrcJar.Path()}, nil
1043 }
1044
1045 case apiTxtComponentName:
1046 if paths.currentApiFilePath.Valid() {
1047 return android.Paths{paths.currentApiFilePath.Path()}, nil
1048 }
1049
1050 case removedApiTxtComponentName:
1051 if paths.removedApiFilePath.Valid() {
1052 return android.Paths{paths.removedApiFilePath.Path()}, nil
1053 }
Anton Hanssond78eb762021-09-21 15:25:12 +01001054
1055 case annotationsComponentName:
1056 if paths.annotationsZip.Valid() {
1057 return android.Paths{paths.annotationsZip.Path()}, nil
1058 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001059 }
1060
1061 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
1062 } else {
1063 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
1064 }
1065
1066 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001067 switch tag {
1068 case ".doctags":
1069 if c.doctagPaths != nil {
1070 return c.doctagPaths, nil
1071 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001072 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +01001073 }
1074 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001075 return nil, nil
1076 }
1077}
1078
Paul Duffin803a9562020-05-20 11:52:25 +01001079func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001080 if c.scopePaths == nil {
1081 c.scopePaths = make(map[*apiScope]*scopePaths)
1082 }
1083 paths := c.scopePaths[scope]
1084 if paths == nil {
1085 paths = &scopePaths{}
1086 c.scopePaths[scope] = paths
1087 }
1088
1089 return paths
1090}
1091
Paul Duffin803a9562020-05-20 11:52:25 +01001092func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1093 if c.scopePaths == nil {
1094 return nil
1095 }
1096
1097 return c.scopePaths[scope]
1098}
1099
1100// If this does not support the requested api scope then find the closest available
1101// scope it does support. Returns nil if no such scope is available.
1102func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001103 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001104 if paths := c.findScopePaths(s); paths != nil {
1105 return paths
1106 }
1107 }
1108
1109 // This should never happen outside tests as public should be the base scope for every
1110 // scope and is enabled by default.
1111 return nil
1112}
1113
Jiyong Parkf1691d22021-03-29 20:11:58 +09001114func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001115
1116 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001117 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001118 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001119 }
1120
Paul Duffin1267d872021-04-16 17:21:36 +01001121 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1122 if paths == nil {
1123 return nil
1124 }
1125
1126 return paths.stubsHeaderPath
1127}
1128
1129// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1130//
1131// If the module does not support the specific kind then it will return the *scopePaths for the
1132// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1133// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1134func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001135 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001136
Paul Duffin803a9562020-05-20 11:52:25 +01001137 paths := c.findClosestScopePath(apiScope)
1138 if paths == nil {
1139 var scopes []string
1140 for _, s := range allApiScopes {
1141 if c.findScopePaths(s) != nil {
1142 scopes = append(scopes, s.name)
1143 }
1144 }
Paul Duffin71b33cc2021-06-23 11:39:47 +01001145 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.module.BaseModuleName(), scopes)
Paul Duffin803a9562020-05-20 11:52:25 +01001146 return nil
1147 }
1148
Paul Duffin1267d872021-04-16 17:21:36 +01001149 return paths
1150}
1151
Paul Duffin32cf58a2021-05-18 16:32:50 +01001152// sdkKindToApiScope maps from android.SdkKind to apiScope.
1153func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1154 var apiScope *apiScope
1155 switch kind {
1156 case android.SdkSystem:
1157 apiScope = apiScopeSystem
1158 case android.SdkModule:
1159 apiScope = apiScopeModuleLib
1160 case android.SdkTest:
1161 apiScope = apiScopeTest
1162 case android.SdkSystemServer:
1163 apiScope = apiScopeSystemServer
1164 default:
1165 apiScope = apiScopePublic
1166 }
1167 return apiScope
1168}
1169
Paul Duffin1267d872021-04-16 17:21:36 +01001170// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001171func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001172 paths := c.selectScopePaths(ctx, kind)
1173 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001174 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001175 }
1176
1177 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001178}
1179
Paul Duffin32cf58a2021-05-18 16:32:50 +01001180// to satisfy SdkLibraryDependency interface
Jihoon Kangbd093452023-12-26 19:08:01 +00001181func (c *commonToSdkLibraryAndImport) SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
1182 paths := c.selectScopePaths(ctx, kind)
1183 if paths == nil {
1184 return makeUnsetDexJarPath()
1185 }
1186
1187 return paths.exportableStubsDexJarPath
1188}
1189
1190// to satisfy SdkLibraryDependency interface
Paul Duffin32cf58a2021-05-18 16:32:50 +01001191func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1192 apiScope := sdkKindToApiScope(kind)
1193 paths := c.findScopePaths(apiScope)
1194 if paths == nil {
1195 return android.OptionalPath{}
1196 }
1197
1198 return paths.removedApiFilePath
1199}
1200
Paul Duffin859fe962020-05-15 10:20:31 +01001201func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1202 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001203 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001204 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001205 }{}
1206
Paul Duffin3f0290e2021-06-30 18:25:36 +01001207 namePtr := proptools.StringPtr(c.module.BaseModuleName())
1208 componentProps.SdkLibraryName = namePtr
1209
Paul Duffindfa131e2020-05-15 20:37:11 +01001210 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001211 // Mark the stubs library as being components of this java_sdk_library so that
1212 // any app that includes code which depends (directly or indirectly) on the stubs
1213 // library will have the appropriate <uses-library> invocation inserted into its
1214 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001215 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001216 }
1217
1218 return componentProps
1219}
1220
Paul Duffindfa131e2020-05-15 20:37:11 +01001221func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1222 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1223}
1224
Paul Duffinf4600f62021-05-13 22:34:45 +01001225// Check if the stub libraries should be compiled for dex
1226func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1227 // Always compile the dex file files for the stub libraries if they will be used on the
1228 // bootclasspath.
1229 return !c.sharedLibrary()
1230}
1231
Paul Duffin859fe962020-05-15 10:20:31 +01001232// Properties related to the use of a module as an component of a java_sdk_library.
1233type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001234 // The name of the java_sdk_library/_import module.
1235 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001236
1237 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1238 // in the AndroidManifest.xml of any Android app that includes code that references
1239 // this module. If not set then no java_sdk_library/_import is tracked.
1240 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1241}
1242
1243// Structure to be embedded in a module struct that needs to support the
1244// SdkLibraryComponentDependency interface.
1245type EmbeddableSdkLibraryComponent struct {
1246 sdkLibraryComponentProperties SdkLibraryComponentProperties
1247}
1248
Paul Duffin71b33cc2021-06-23 11:39:47 +01001249func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1250 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001251}
1252
1253// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001254func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1255 return e.sdkLibraryComponentProperties.SdkLibraryName
1256}
1257
1258// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001259func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001260 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1261 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1262 // run-time library and the corresponding module that provides the implementation. This name is
1263 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1264 // in dexpreopt).
1265 //
1266 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1267 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001268 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1269}
1270
Paul Duffin859fe962020-05-15 10:20:31 +01001271// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1272// (including the java_sdk_library) itself.
1273type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001274 UsesLibraryDependency
1275
Paul Duffin3f0290e2021-06-30 18:25:36 +01001276 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1277 SdkLibraryName() *string
1278
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001279 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1280 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001281}
1282
1283// Make sure that all the module types that are components of java_sdk_library/_import
1284// and which can be referenced (directly or indirectly) from an android app implement
1285// the SdkLibraryComponentDependency interface.
1286var _ SdkLibraryComponentDependency = (*Library)(nil)
1287var _ SdkLibraryComponentDependency = (*Import)(nil)
1288var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001289var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001290
Paul Duffin32cf58a2021-05-18 16:32:50 +01001291// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001292type SdkLibraryDependency interface {
1293 SdkLibraryComponentDependency
1294
1295 // Get the header jars appropriate for the supplied sdk_version.
1296 //
1297 // These are turbine generated jars so they only change if the externals of the
1298 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001299 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001300
1301 // Get the implementation jars appropriate for the supplied sdk version.
1302 //
1303 // These are either the implementation jar for the whole sdk library or the implementation
1304 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1305 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001306 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001307
Jihoon Kangbd093452023-12-26 19:08:01 +00001308 // SdkApiStubDexJar returns the dex jar for the stubs for the prebuilt
1309 // java_sdk_library_import module. It is needed by the hiddenapi processing tool which
1310 // processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001311 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001312
Jihoon Kangbd093452023-12-26 19:08:01 +00001313 // SdkApiExportableStubDexJar returns the exportable dex jar for the stubs for
1314 // java_sdk_library module. It is needed by the hiddenapi processing tool which processes
1315 // dex files.
1316 SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
1317
Paul Duffin32cf58a2021-05-18 16:32:50 +01001318 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1319 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1320
Paul Duffinf4600f62021-05-13 22:34:45 +01001321 // sharedLibrary returns true if this can be used as a shared library.
1322 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001323}
1324
Inseob Kimc0907f12019-02-08 21:00:45 +09001325type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001326 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001327
Sundong Ahn054b19a2018-10-19 13:46:09 +09001328 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001329
Paul Duffin3375e352020-04-28 10:44:03 +01001330 // Map from api scope to the scope specific property structure.
1331 scopeToProperties map[*apiScope]*ApiScopeProperties
1332
Paul Duffin56d44902020-01-31 13:36:25 +00001333 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001334}
1335
Inseob Kimc0907f12019-02-08 21:00:45 +09001336var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001337
Paul Duffin3375e352020-04-28 10:44:03 +01001338func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1339 return module.sdkLibraryProperties.Generate_system_and_test_apis
1340}
1341
1342func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1343 // Check to see if any scopes have been explicitly enabled. If any have then all
1344 // must be.
1345 anyScopesExplicitlyEnabled := false
1346 for _, scope := range allApiScopes {
1347 scopeProperties := module.scopeToProperties[scope]
1348 if scopeProperties.Enabled != nil {
1349 anyScopesExplicitlyEnabled = true
1350 break
1351 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001352 }
Paul Duffin3375e352020-04-28 10:44:03 +01001353
1354 var generatedScopes apiScopes
1355 enabledScopes := make(map[*apiScope]struct{})
1356 for _, scope := range allApiScopes {
1357 scopeProperties := module.scopeToProperties[scope]
1358 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1359 // This is to ensure that any new usages of this module type do not rely on legacy
1360 // behaviour.
1361 defaultEnabledStatus := false
1362 if anyScopesExplicitlyEnabled {
1363 defaultEnabledStatus = scope.defaultEnabledStatus
1364 } else {
1365 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1366 }
1367 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1368 if enabled {
1369 enabledScopes[scope] = struct{}{}
1370 generatedScopes = append(generatedScopes, scope)
1371 }
1372 }
1373
1374 // Now check to make sure that any scope that is extended by an enabled scope is also
1375 // enabled.
1376 for _, scope := range allApiScopes {
1377 if _, ok := enabledScopes[scope]; ok {
1378 extends := scope.extends
1379 if extends != nil {
1380 if _, ok := enabledScopes[extends]; !ok {
1381 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1382 }
1383 }
1384 }
1385 }
1386
1387 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001388}
1389
satayev758968a2021-12-06 11:42:40 +00001390var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1391
satayev8f088b02021-12-06 11:40:46 +00001392func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001393 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001394 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1395 isExternal := !module.depIsInSameApex(ctx, child)
1396 if am, ok := child.(android.ApexModule); ok {
1397 if !do(ctx, parent, am, isExternal) {
1398 return false
1399 }
1400 }
1401 return !isExternal
1402 })
1403 })
1404}
1405
Paul Duffineedc5d52020-06-12 17:46:39 +01001406type sdkLibraryComponentTag struct {
1407 blueprint.BaseDependencyTag
1408 name string
1409}
1410
1411// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1412func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1413
1414var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001415
Jiyong Parke3833882020-02-17 17:28:10 +09001416func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001417 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001418 return dt == xmlPermissionsFileTag
1419 }
1420 return false
1421}
1422
Paul Duffineedc5d52020-06-12 17:46:39 +01001423var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001424
Paul Duffin44f1d842020-06-26 20:17:02 +01001425// Add the dependencies on the child modules in the component deps mutator.
1426func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001427 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001428 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001429 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kangbd093452023-12-26 19:08:01 +00001430 ctx.AddVariationDependencies(nil, apiScope.everythingStubsTag, stubModuleName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001431
Jihoon Kangbd093452023-12-26 19:08:01 +00001432 exportableStubModuleName := module.exportableStubsLibraryModuleName(apiScope)
1433 ctx.AddVariationDependencies(nil, apiScope.exportableStubsTag, exportableStubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001434
Paul Duffin15f34ef2020-07-20 18:04:44 +01001435 // Add a dependency on the stubs source in order to access both stubs source and api information.
1436 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001437
1438 if module.compareAgainstLatestApi(apiScope) {
1439 // Add dependencies on the latest finalized version of the API .txt file.
1440 latestApiModuleName := module.latestApiModuleName(apiScope)
1441 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1442
1443 // Add dependencies on the latest finalized version of the remove API .txt file.
1444 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1445 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1446 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001447 }
1448
Paul Duffindfa131e2020-05-15 20:37:11 +01001449 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001450 // Add dependency to the rule for generating the implementation library.
1451 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1452
Paul Duffindfa131e2020-05-15 20:37:11 +01001453 if module.sharedLibrary() {
1454 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001455 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001456 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001457 }
1458}
Paul Duffine74ac732020-02-06 13:51:46 +00001459
Paul Duffin44f1d842020-06-26 20:17:02 +01001460// Add other dependencies as normal.
1461func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001462 var missingApiModules []string
1463 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1464 if apiScope.unstable {
1465 continue
1466 }
Paul Duffin958806b2022-05-16 13:10:47 +00001467 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001468 missingApiModules = append(missingApiModules, m)
1469 }
Paul Duffin958806b2022-05-16 13:10:47 +00001470 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001471 missingApiModules = append(missingApiModules, m)
1472 }
Paul Duffin958806b2022-05-16 13:10:47 +00001473 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001474 missingApiModules = append(missingApiModules, m)
1475 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001476 }
1477 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1478 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1479 m += "You need to do one of the following:\n"
1480 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1481 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1482 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1483 m += "\n"
1484 m += "The following filegroup modules are missing:\n "
1485 m += strings.Join(missingApiModules, "\n ") + "\n"
1486 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."
1487 ctx.ModuleErrorf(m)
1488 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001489 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001490 // Only add the deps for the library if it is actually going to be built.
1491 module.Library.deps(ctx)
1492 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001493}
1494
Paul Duffin46dc45a2020-05-14 15:39:10 +01001495func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1496 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001497 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001498 return paths, err
1499 }
Colin Cross4acaea92021-12-10 23:05:02 +00001500 if module.requiresRuntimeImplementationLibrary() {
1501 return module.Library.OutputFiles(tag)
1502 }
1503 if tag == "" {
1504 return nil, nil
1505 }
1506 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001507}
1508
Inseob Kimc0907f12019-02-08 21:00:45 +09001509func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001510 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1511 module.CheckMinSdkVersion(ctx)
1512 }
1513
Paul Duffina2ae7e02020-09-11 11:55:00 +01001514 module.generateCommonBuildActions(ctx)
1515
Paul Duffindfa131e2020-05-15 20:37:11 +01001516 // Only build an implementation library if required.
1517 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001518 module.Library.GenerateAndroidBuildActions(ctx)
1519 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001520
Paul Duffinb97b1572021-04-29 21:50:40 +01001521 // Collate the components exported by this module. All scope specific modules are exported but
1522 // the impl and xml component modules are not.
1523 exportedComponents := map[string]struct{}{}
1524
Sundong Ahn57368eb2018-07-06 11:20:23 +09001525 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001526 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001527 // the recorded paths will be returned depending on the link type of the caller.
1528 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001529 tag := ctx.OtherModuleDependencyTag(to)
1530
Paul Duffinc8782502020-04-29 20:45:27 +01001531 // Extract information from any of the scope specific dependencies.
1532 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1533 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001534 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001535
1536 // Extract information from the dependency. The exact information extracted
1537 // is determined by the nature of the dependency which is determined by the tag.
1538 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001539
1540 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001541 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001542 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001543
1544 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001545 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001546 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001547
1548 // Provide additional information for inclusion in an sdk's generated .info file.
1549 additionalSdkInfo := map[string]interface{}{}
1550 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001551 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001552 scopes := map[string]interface{}{}
1553 additionalSdkInfo["scopes"] = scopes
1554 for scope, scopePaths := range module.scopePaths {
1555 scopeInfo := map[string]interface{}{}
1556 scopes[scope.name] = scopeInfo
1557 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1558 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1559 if p := scopePaths.latestApiPath; p.Valid() {
1560 scopeInfo["latest_api"] = p.Path().String()
1561 }
1562 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1563 scopeInfo["latest_removed_api"] = p.Path().String()
1564 }
1565 }
Colin Cross40213022023-12-13 15:19:49 -08001566 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001567}
1568
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001569func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001570 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001571 return nil
1572 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001573 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001574 if module.sharedLibrary() {
1575 entries := &entriesList[0]
1576 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1577 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001578 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001579}
1580
Anton Hansson5fd5d242020-03-27 19:43:19 +00001581// The dist path of the stub artifacts
1582func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001583 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001584}
1585
Paul Duffin12ceb462019-12-24 20:31:31 +00001586// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001587func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001588 scopeProperties := module.scopeToProperties[apiScope]
1589 if scopeProperties.Sdk_version != nil {
1590 return proptools.String(scopeProperties.Sdk_version)
1591 }
1592
Jiyong Parkf1691d22021-03-29 20:11:58 +09001593 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001594 if sdkDep.hasStandardLibs() {
1595 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001596 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001597 } else {
1598 // Otherwise, use no system module.
1599 return "none"
1600 }
1601}
1602
Paul Duffin31310252020-11-20 21:26:20 +00001603func (module *SdkLibrary) distStem() string {
1604 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1605}
1606
Colin Cross986b69a2021-06-01 13:13:40 -07001607// distGroup returns the subdirectory of the dist path of the stub artifacts.
1608func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001609 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001610}
1611
Paul Duffin958806b2022-05-16 13:10:47 +00001612func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1613 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1614}
1615
Paul Duffind1b3a922020-01-22 11:57:20 +00001616func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001617 return ":" + module.latestApiModuleName(apiScope)
1618}
1619
1620func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
1621 return latestPrebuiltApiModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001622}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001623
Paul Duffind1b3a922020-01-22 11:57:20 +00001624func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001625 return ":" + module.latestRemovedApiModuleName(apiScope)
1626}
1627
1628func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
1629 return latestPrebuiltApiModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001630}
1631
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001632func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001633 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1634}
1635
1636func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1637 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001638}
1639
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001640func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1641 _, exists := c.GetApiLibraries()[module.Name()]
1642 return exists
1643}
1644
Jihoon Kang0c705a42023-08-02 06:44:57 +00001645// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1646// api surface that the module contribute to. For example, the public droidstubs and java_library
1647// do not contribute to the public api surface, but contributes to the core platform api surface.
1648// This method returns the full api surface stub lib that
1649// the generated java_api_library should depend on.
1650func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1651 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1652 return val.FullApiSurfaceStubLib
1653 }
1654 return ""
1655}
1656
1657// The listed modules' stubs contents do not match the corresponding txt files,
1658// but require additional api contributions to generate the full stubs.
1659// This method returns the name of the additional api contribution module
1660// for corresponding sdk_library modules.
1661func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1662 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1663 return val.AdditionalApiContribution
1664 }
1665 return ""
1666}
1667
Anton Hansson944e77d2020-08-19 11:40:22 +01001668func childModuleVisibility(childVisibility []string) []string {
1669 if childVisibility == nil {
1670 // No child visibility set. The child will use the visibility of the sdk_library.
1671 return nil
1672 }
1673
1674 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1675 var visibility []string
1676 visibility = append(visibility, "//visibility:override")
1677 visibility = append(visibility, childVisibility...)
1678 return visibility
1679}
1680
Paul Duffin5df79302020-05-16 15:52:12 +01001681// Creates the implementation java library
1682func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001683 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1684
Paul Duffin5df79302020-05-16 15:52:12 +01001685 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001686 Name *string
1687 Visibility []string
1688 Instrument bool
1689 Libs []string
1690 Static_libs []string
1691 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001692 }{
1693 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001694 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001695 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1696 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001697 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1698 // addition of &module.properties below.
1699 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001700 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1701 // addition of &module.properties below.
1702 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1703 // Pass the apex_available settings down so that the impl library can be statically
1704 // embedded within a library that is added to an APEX. Needed for updatable-media.
1705 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001706 }
1707
1708 properties := []interface{}{
1709 &module.properties,
1710 &module.protoProperties,
1711 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001712 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001713 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001714 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001715 &props,
1716 module.sdkComponentPropertiesForChildLibrary(),
1717 }
1718 mctx.CreateModule(LibraryFactory, properties...)
1719}
1720
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001721type libraryProperties struct {
1722 Name *string
1723 Visibility []string
1724 Srcs []string
1725 Installable *bool
1726 Sdk_version *string
1727 System_modules *string
1728 Patch_module *string
1729 Libs []string
1730 Static_libs []string
1731 Compile_dex *bool
1732 Java_version *string
1733 Openjdk9 struct {
1734 Srcs []string
1735 Javacflags []string
1736 }
1737 Dist struct {
1738 Targets []string
1739 Dest *string
1740 Dir *string
1741 Tag *string
1742 }
1743}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001744
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001745func (module *SdkLibrary) stubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
1746 props := libraryProperties{}
Jihoon Kang786df932023-09-07 01:18:31 +00001747 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001748 // sources are generated from the droiddoc
Paul Duffin12ceb462019-12-24 20:31:31 +00001749 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001750 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001751 props.System_modules = module.deviceProperties.System_modules
1752 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001753 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001754 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001755 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001756 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001757 // The stub-annotations library contains special versions of the annotations
1758 // with CLASS retention policy, so that they're kept.
1759 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1760 props.Libs = append(props.Libs, "stub-annotations")
1761 }
Paul Duffina18abc22020-05-16 18:54:24 +01001762 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1763 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001764 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1765 // interop with older developer tools that don't support 1.9.
1766 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001767
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001768 return props
1769}
1770
1771// Creates a static java library that has API stubs
1772func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1773
1774 props := module.stubsLibraryProps(mctx, apiScope)
1775 props.Name = proptools.StringPtr(module.sourceStubsLibraryModuleName(apiScope))
1776 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
1777
1778 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1779}
1780
1781// Create a static java library that compiles the "exportable" stubs
1782func (module *SdkLibrary) createExportableStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1783 props := module.stubsLibraryProps(mctx, apiScope)
1784 props.Name = proptools.StringPtr(module.exportableSourceStubsLibraryModuleName(apiScope))
1785 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope) + "{.exportable}"}
1786
Paul Duffin859fe962020-05-15 10:20:31 +01001787 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001788}
1789
Paul Duffin6d0886e2020-04-07 18:49:53 +01001790// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001791// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001792func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001793 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001794 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001795 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001796 Srcs []string
1797 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001798 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001799 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001800 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001801 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001802 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001803 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001804 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001805 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001806 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001807 Merge_annotations_dirs []string
1808 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001809 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001810 Previous_api *string
Jihoon Kang6592e872023-12-19 01:13:16 +00001811 Aconfig_declarations []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001812 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001813 Current ApiToCheck
1814 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001815
1816 Api_lint struct {
1817 Enabled *bool
1818 New_since *string
1819 Baseline_file *string
1820 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001821 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001822 Aidl struct {
1823 Include_dirs []string
1824 Local_include_dirs []string
1825 }
Paul Duffin040e9062020-11-23 17:41:36 +00001826 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001827 }{}
1828
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001829 // The stubs source processing uses the same compile time classpath when extracting the
1830 // API from the implementation library as it does when compiling it. i.e. the same
1831 // * sdk version
1832 // * system_modules
1833 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001834
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001835 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001836 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001837 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001838 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001839 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001840 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001841 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001842 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001843 // A droiddoc module has only one Libs property and doesn't distinguish between
1844 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001845 props.Libs = module.properties.Libs
1846 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001847 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001848 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001849 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1850 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1851 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001852
Paul Duffine22c2ab2020-05-20 19:35:27 +01001853 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001854 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1855 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
Jihoon Kang6592e872023-12-19 01:13:16 +00001856 props.Aconfig_declarations = module.sdkLibraryProperties.Aconfig_declarations
Sundong Ahn054b19a2018-10-19 13:46:09 +09001857
Paul Duffin6d0886e2020-04-07 18:49:53 +01001858 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001859 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001860 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001861 }
1862 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001863 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001864 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1865 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001866 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00001867 disabledWarnings := []string{"HiddenSuperclass"}
1868 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
1869 disabledWarnings = append(disabledWarnings,
1870 "BroadcastBehavior",
1871 "DeprecationMismatch",
1872 "MissingPermission",
1873 "SdkConstant",
1874 "Todo",
1875 )
Paul Duffin235ffff2019-12-24 10:41:30 +00001876 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001877 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001878
Paul Duffin6877e6d2020-09-25 19:59:14 +01001879 // Output Javadoc comments for public scope.
1880 if apiScope == apiScopePublic {
1881 props.Output_javadoc_comments = proptools.BoolPtr(true)
1882 }
1883
Paul Duffin1fb487d2020-04-07 18:50:10 +01001884 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001885 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001886 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001887 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001888
Paul Duffin15f34ef2020-07-20 18:04:44 +01001889 // List of APIs identified from the provided source files are created. They are later
1890 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1891 // last-released (a.k.a numbered) list of API.
1892 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1893 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1894 apiDir := module.getApiDir()
1895 currentApiFileName = path.Join(apiDir, currentApiFileName)
1896 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001897
Paul Duffin15f34ef2020-07-20 18:04:44 +01001898 // check against the not-yet-release API
1899 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1900 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001901
Paul Duffin958806b2022-05-16 13:10:47 +00001902 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001903 // check against the latest released API
1904 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001905 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001906 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1907 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1908 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001909 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1910 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001911
Paul Duffin15f34ef2020-07-20 18:04:44 +01001912 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1913 // Enable api lint.
1914 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1915 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001916
Paul Duffin15f34ef2020-07-20 18:04:44 +01001917 // If it exists then pass a lint-baseline.txt through to droidstubs.
1918 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1919 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1920 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1921 if err != nil {
1922 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1923 }
1924 if len(paths) == 1 {
1925 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1926 } else if len(paths) != 0 {
1927 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001928 }
1929 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001930 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001931
Paul Duffin15f34ef2020-07-20 18:04:44 +01001932 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001933 // Dist the api txt and removed api txt artifacts for sdk builds.
1934 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1935 for _, p := range []struct {
1936 tag string
1937 pattern string
1938 }{
1939 {tag: ".api.txt", pattern: "%s.txt"},
1940 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1941 } {
1942 props.Dists = append(props.Dists, android.Dist{
1943 Targets: []string{"sdk", "win_sdk"},
1944 Dir: distDir,
1945 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1946 Tag: proptools.StringPtr(p.tag),
1947 })
1948 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001949 }
1950
Spandan Das2cc80ba2023-10-27 17:21:52 +00001951 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001952}
1953
Jihoon Kang0c705a42023-08-02 06:44:57 +00001954func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001955 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00001956 Name *string
1957 Visibility []string
1958 Api_contributions []string
1959 Libs []string
1960 Static_libs []string
1961 Full_api_surface_stub *string
Jihoon Kang4ec24872023-10-05 17:26:09 +00001962 System_modules *string
Jihoon Kang063ec002023-06-28 01:16:23 +00001963 Enable_validation *bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001964 }{}
1965
1966 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00001967 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001968
1969 apiContributions := []string{}
1970
1971 // Api surfaces are not independent of each other, but have subset relationships,
1972 // and so does the api files. To generate from-text stubs for api surfaces other than public,
1973 // all subset api domains' api_contriubtions must be added as well.
1974 scope := apiScope
1975 for scope != nil {
1976 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
1977 scope = scope.extends
1978 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00001979 if apiScope == apiScopePublic {
1980 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
1981 if additionalApiContribution != "" {
1982 apiContributions = append(apiContributions, additionalApiContribution)
1983 }
1984 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001985
1986 props.Api_contributions = apiContributions
1987 props.Libs = module.properties.Libs
1988 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001989 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001990 props.Libs = append(props.Libs, "stub-annotations")
1991 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kange7ee2562023-07-25 05:51:46 +00001992 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
Jihoon Kang0c705a42023-08-02 06:44:57 +00001993 if alternativeFullApiSurfaceStub != "" {
1994 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
1995 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001996
1997 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
1998 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
1999 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00002000 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002001 }
2002
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00002003 // java_sdk_library modules that set sdk_version as none does not depend on other api
2004 // domains. Therefore, java_api_library created from such modules should not depend on
2005 // full_api_surface_stubs but create and compile stubs by the java_api_library module
2006 // itself.
2007 if module.SdkVersion(mctx).Kind == android.SdkNone {
2008 props.Full_api_surface_stub = nil
2009 }
2010
Jihoon Kang4ec24872023-10-05 17:26:09 +00002011 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00002012 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang4ec24872023-10-05 17:26:09 +00002013
Spandan Das2cc80ba2023-10-27 17:21:52 +00002014 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002015}
2016
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002017func (module *SdkLibrary) topLevelStubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
2018 props := libraryProperties{}
2019
Jihoon Kang1147b312023-06-08 23:25:57 +00002020 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
2021 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
2022 props.Sdk_version = proptools.StringPtr(sdkVersion)
2023
Jihoon Kang1147b312023-06-08 23:25:57 +00002024 props.System_modules = module.deviceProperties.System_modules
2025
Jihoon Kang1147b312023-06-08 23:25:57 +00002026 // The imports need to be compiled to dex if the java_sdk_library requests it.
2027 compileDex := module.dexProperties.Compile_dex
2028 if module.stubLibrariesCompiledForDex() {
2029 compileDex = proptools.BoolPtr(true)
2030 }
2031 props.Compile_dex = compileDex
2032
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002033 return props
2034}
2035
2036func (module *SdkLibrary) createTopLevelStubsLibrary(
2037 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
2038
2039 props := module.topLevelStubsLibraryProps(mctx, apiScope)
2040 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
2041
2042 // Add the stub compiling java_library/java_api_library as static lib based on build config
2043 staticLib := module.sourceStubsLibraryModuleName(apiScope)
2044 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
2045 staticLib = module.apiLibraryModuleName(apiScope)
2046 }
2047 props.Static_libs = append(props.Static_libs, staticLib)
2048
2049 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2050}
2051
2052func (module *SdkLibrary) createTopLevelExportableStubsLibrary(
2053 mctx android.DefaultableHookContext, apiScope *apiScope) {
2054
2055 props := module.topLevelStubsLibraryProps(mctx, apiScope)
2056 props.Name = proptools.StringPtr(module.exportableStubsLibraryModuleName(apiScope))
2057
2058 // Dist the class jar artifact for sdk builds.
2059 // "exportable" stubs are copied to dist for sdk builds instead of the "everything" stubs.
2060 if !Bool(module.sdkLibraryProperties.No_dist) {
2061 props.Dist.Targets = []string{"sdk", "win_sdk"}
2062 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
2063 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
2064 props.Dist.Tag = proptools.StringPtr(".jar")
2065 }
2066
2067 staticLib := module.exportableSourceStubsLibraryModuleName(apiScope)
2068 props.Static_libs = append(props.Static_libs, staticLib)
2069
Jihoon Kang1147b312023-06-08 23:25:57 +00002070 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2071}
2072
Paul Duffin958806b2022-05-16 13:10:47 +00002073func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
2074 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
2075}
2076
Paul Duffinea8f8082021-06-24 13:25:57 +01002077// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09002078func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2079 depTag := mctx.OtherModuleDependencyTag(dep)
2080 if depTag == xmlPermissionsFileTag {
2081 return true
2082 }
2083 return module.Library.DepIsInSameApex(mctx, dep)
2084}
2085
Paul Duffinea8f8082021-06-24 13:25:57 +01002086// Implements android.ApexModule
2087func (module *SdkLibrary) UniqueApexVariations() bool {
2088 return module.uniqueApexVariations()
2089}
2090
Jihoon Kang80456fd2023-11-15 19:22:14 +00002091func (module *SdkLibrary) ContributeToApi() bool {
2092 return proptools.BoolDefault(module.sdkLibraryProperties.Contribute_to_android_api, false)
2093}
2094
Jiyong Parkc678ad32018-04-10 13:07:10 +09002095// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01002096func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002097 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002098 var moduleMinApiLevelStr = moduleMinApiLevel.String()
2099 if moduleMinApiLevel == android.NoneApiLevel {
2100 moduleMinApiLevelStr = "current"
2101 }
Jiyong Parke3833882020-02-17 17:28:10 +09002102 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00002103 Name *string
2104 Lib_name *string
2105 Apex_available []string
2106 On_bootclasspath_since *string
2107 On_bootclasspath_before *string
2108 Min_device_sdk *string
2109 Max_device_sdk *string
2110 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002111 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002112 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00002113 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
2114 Lib_name: proptools.StringPtr(module.BaseModuleName()),
2115 Apex_available: module.ApexProperties.Apex_available,
2116 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
2117 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
2118 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
2119 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
2120 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002121 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002122 }
Jiyong Parke3833882020-02-17 17:28:10 +09002123
Jiyong Parke3833882020-02-17 17:28:10 +09002124 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002125}
2126
Jiyong Parkf1691d22021-03-29 20:11:58 +09002127func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002128 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002129 var kind android.SdkKind
2130 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002131 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002132 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002133 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002134 // We don't have prebuilt SDK for the specific sdkVersion.
2135 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002136 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002137 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002138 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002139
2140 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002141 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002142 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002143 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002144 if ctx.Config().AllowMissingDependencies() {
2145 return android.Paths{android.PathForSource(ctx, jar)}
2146 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002147 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002148 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002149 return nil
2150 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002151 return android.Paths{jarPath.Path()}
2152}
2153
Colin Crossaede88c2020-08-11 12:17:01 -07002154// 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 +01002155//
2156// If either this or the other module are on the platform then this will return
2157// false.
Colin Cross56a83212020-09-15 18:30:11 -07002158func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
Colin Crossff694a82023-12-13 15:54:49 -08002159 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross313aa542023-12-13 13:47:44 -08002160 otherApexInfo, _ := android.OtherModuleProvider(ctx, other, android.ApexInfoProvider)
Jiyong Parkab50b072021-05-12 17:13:56 +09002161 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002162}
2163
Jiyong Parkf1691d22021-03-29 20:11:58 +09002164func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002165 // If the client doesn't set sdk_version, but if this library prefers stubs over
2166 // the impl library, let's provide the widest API surface possible. To do so,
2167 // force override sdk_version to module_current so that the closest possible API
2168 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002169 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002170 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002171 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002172
Paul Duffindaaa3322020-05-26 18:13:57 +01002173 // Only provide access to the implementation library if it is actually built.
2174 if module.requiresRuntimeImplementationLibrary() {
2175 // Check any special cases for java_sdk_library.
2176 //
2177 // Only allow access to the implementation library in the following condition:
2178 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002179 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002180 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01002181 if headerJars {
2182 return module.HeaderJars()
2183 } else {
2184 return module.ImplementationJars()
2185 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002186 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002187 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002188
Paul Duffin23970f42020-05-20 14:20:02 +01002189 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002190}
2191
Sundong Ahn241cd372018-07-13 16:16:44 +09002192// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002193func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002194 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
2195}
2196
2197// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002198func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002199 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09002200}
2201
Colin Cross571cccf2019-02-04 11:22:08 -08002202var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2203
Jiyong Park82484c02018-04-23 21:41:26 +09002204func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002205 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002206 return &[]string{}
2207 }).(*[]string)
2208}
2209
Paul Duffin749f98f2019-12-30 17:23:46 +00002210func (module *SdkLibrary) getApiDir() string {
2211 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2212}
2213
Jiyong Parkc678ad32018-04-10 13:07:10 +09002214// For a java_sdk_library module, create internal modules for stubs, docs,
2215// runtime libs and xml file. If requested, the stubs and docs are created twice
2216// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002217func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2218 // If the module has been disabled then don't create any child modules.
2219 if !module.Enabled() {
2220 return
2221 }
2222
Paul Duffina18abc22020-05-16 18:54:24 +01002223 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002224 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002225 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002226 }
2227
Paul Duffin37e0b772019-12-30 17:20:10 +00002228 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002229 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002230 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002231 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002232 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002233
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002234 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002235
Paul Duffin3375e352020-04-28 10:44:03 +01002236 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002237
Paul Duffin749f98f2019-12-30 17:23:46 +00002238 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002239 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002240 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002241 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002242 p := android.ExistentPathForSource(mctx, path)
2243 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002244 if mctx.Config().AllowMissingDependencies() {
2245 mctx.AddMissingDependencies([]string{path})
2246 } else {
2247 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2248 missingCurrentApi = true
2249 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002250 }
2251 }
2252 }
2253
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002254 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002255 script := "build/soong/scripts/gen-java-current-api-files.sh"
2256 p := android.ExistentPathForSource(mctx, script)
2257
2258 if !p.Valid() {
2259 panic(fmt.Sprintf("script file %s doesn't exist", script))
2260 }
2261
2262 mctx.ModuleErrorf("One or more current api files are missing. "+
2263 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002264 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002265 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002266 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002267 return
2268 }
2269
Paul Duffin3375e352020-04-28 10:44:03 +01002270 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002271 // Use the stubs source name for legacy reasons.
2272 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002273
Paul Duffind1b3a922020-01-22 11:57:20 +00002274 module.createStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002275 module.createExportableStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002276
Jihoon Kang0c705a42023-08-02 06:44:57 +00002277 alternativeFullApiSurfaceStubLib := ""
2278 if scope == apiScopePublic {
2279 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
2280 }
2281 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
Jihoon Kang1147b312023-06-08 23:25:57 +00002282 if contributesToApiSurface {
Jihoon Kang0c705a42023-08-02 06:44:57 +00002283 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002284 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002285
2286 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002287 module.createTopLevelExportableStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09002288 }
2289
Paul Duffindfa131e2020-05-15 20:37:11 +01002290 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002291 // Create child module to create an implementation library.
2292 //
2293 // This temporarily creates a second implementation library that can be explicitly
2294 // referenced.
2295 //
2296 // TODO(b/156618935) - update comment once only one implementation library is created.
2297 module.createImplLibrary(mctx)
2298
Paul Duffindfa131e2020-05-15 20:37:11 +01002299 // Only create an XML permissions file that declares the library as being usable
2300 // as a shared library if required.
2301 if module.sharedLibrary() {
2302 module.createXmlFile(mctx)
2303 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002304
2305 // record java_sdk_library modules so that they are exported to make
2306 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2307 javaSdkLibrariesLock.Lock()
2308 defer javaSdkLibrariesLock.Unlock()
2309 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2310 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002311
Paul Duffin77590a82022-04-28 14:13:30 +00002312 // 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 +01002313 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002314 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002315}
2316
2317func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002318 module.addHostAndDeviceProperties()
2319 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002320
Paul Duffin71b33cc2021-06-23 11:39:47 +01002321 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002322
Paul Duffina18abc22020-05-16 18:54:24 +01002323 module.properties.Installable = proptools.BoolPtr(true)
2324 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002325}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002326
Paul Duffindfa131e2020-05-15 20:37:11 +01002327func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2328 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2329}
2330
Jiyong Park932cdfe2020-05-28 00:19:53 +09002331func (module *SdkLibrary) defaultsToStubs() bool {
2332 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2333}
2334
Paul Duffin1b1e8062020-05-08 13:44:43 +01002335// Defines how to name the individual component modules the sdk library creates.
2336type sdkLibraryComponentNamingScheme interface {
2337 stubsLibraryModuleName(scope *apiScope, baseName string) string
2338
2339 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002340
2341 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002342
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002343 sourceStubsLibraryModuleName(scope *apiScope, baseName string) string
2344
2345 exportableStubsLibraryModuleName(scope *apiScope, baseName string) string
2346
2347 exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002348}
2349
2350type defaultNamingScheme struct {
2351}
2352
2353func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2354 return scope.stubsLibraryModuleName(baseName)
2355}
2356
2357func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2358 return scope.stubsSourceModuleName(baseName)
2359}
2360
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002361func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2362 return scope.apiLibraryModuleName(baseName)
2363}
2364
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002365func (s *defaultNamingScheme) sourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
Jihoon Kang1147b312023-06-08 23:25:57 +00002366 return scope.sourceStubLibraryModuleName(baseName)
2367}
2368
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002369func (s *defaultNamingScheme) exportableStubsLibraryModuleName(scope *apiScope, baseName string) string {
2370 return scope.exportableStubsLibraryModuleName(baseName)
2371}
2372
2373func (s *defaultNamingScheme) exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
2374 return scope.exportableSourceStubsLibraryModuleName(baseName)
2375}
2376
Paul Duffin1b1e8062020-05-08 13:44:43 +01002377var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2378
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002379func hasStubsLibrarySuffix(name string, apiScope *apiScope) bool {
2380 return strings.HasSuffix(name, apiScope.stubsLibraryModuleNameSuffix()) ||
2381 strings.HasSuffix(name, apiScope.exportableStubsLibraryModuleNameSuffix())
2382}
2383
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002384func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002385 name = strings.TrimSuffix(name, ".from-source")
2386
Anton Hansson2d0c1942020-05-25 12:20:51 +01002387 // This suffix-based approach is fragile and could potentially mis-trigger.
2388 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002389 if hasStubsLibrarySuffix(name, apiScopePublic) {
Anton Hansson08f476b2021-04-07 15:32:19 +01002390 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2391 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2392 return false, javaPlatform
2393 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002394 return true, javaSdk
2395 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002396 if hasStubsLibrarySuffix(name, apiScopeSystem) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002397 return true, javaSystem
2398 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002399 if hasStubsLibrarySuffix(name, apiScopeModuleLib) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002400 return true, javaModule
2401 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002402 if hasStubsLibrarySuffix(name, apiScopeTest) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002403 return true, javaSystem
2404 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002405 if hasStubsLibrarySuffix(name, apiScopeSystemServer) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002406 return true, javaSystemServer
2407 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002408 return false, javaPlatform
2409}
2410
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002411// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2412// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2413// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2414// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2415// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002416func SdkLibraryFactory() android.Module {
2417 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002418
2419 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002420 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002421
Inseob Kimc0907f12019-02-08 21:00:45 +09002422 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002423 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002424 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002425
2426 // Initialize the map from scope to scope specific properties.
2427 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2428 for _, scope := range allApiScopes {
2429 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2430 }
2431 module.scopeToProperties = scopeToProperties
2432
Paul Duffin4911a892020-04-29 23:35:13 +01002433 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002434 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002435 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2436 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2437
Paul Duffin1b1e8062020-05-08 13:44:43 +01002438 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002439 // If no implementation is required then it cannot be used as a shared library
2440 // either.
2441 if !module.requiresRuntimeImplementationLibrary() {
2442 // If shared_library has been explicitly set to true then it is incompatible
2443 // with api_only: true.
2444 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2445 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2446 }
2447 // Set shared_library: false.
2448 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2449 }
2450
Paul Duffin1b1e8062020-05-08 13:44:43 +01002451 if module.initCommonAfterDefaultsApplied(ctx) {
2452 module.CreateInternalModules(ctx)
2453 }
2454 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002455 return module
2456}
Colin Cross79c7c262019-04-17 11:11:46 -07002457
2458//
2459// SDK library prebuilts
2460//
2461
Paul Duffin56d44902020-01-31 13:36:25 +00002462// Properties associated with each api scope.
2463type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002464 Jars []string `android:"path"`
2465
2466 Sdk_version *string
2467
Colin Cross79c7c262019-04-17 11:11:46 -07002468 // List of shared java libs that this module has dependencies to
2469 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002470
Paul Duffinc8782502020-04-29 20:45:27 +01002471 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002472 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002473
2474 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002475 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002476
2477 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002478 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002479
2480 // Annotation zip
2481 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002482}
2483
Paul Duffin56d44902020-01-31 13:36:25 +00002484type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002485 // List of shared java libs, common to all scopes, that this module has
2486 // dependencies to
2487 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002488
2489 // If set to true, compile dex files for the stubs. Defaults to false.
2490 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002491
2492 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002493 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00002494}
2495
Paul Duffineedc5d52020-06-12 17:46:39 +01002496type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002497 android.ModuleBase
2498 android.DefaultableModuleBase
2499 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002500 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002501
Paul Duffin37856732021-02-26 14:24:15 +00002502 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002503 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002504
Colin Cross79c7c262019-04-17 11:11:46 -07002505 properties sdkLibraryImportProperties
2506
Paul Duffin46a26a82020-04-07 19:27:04 +01002507 // Map from api scope to the scope specific property structure.
2508 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2509
Paul Duffin56d44902020-01-31 13:36:25 +00002510 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002511
2512 // The reference to the implementation library created by the source module.
2513 // Is nil if the source module does not exist.
2514 implLibraryModule *Library
2515
2516 // The reference to the xml permissions module created by the source module.
2517 // Is nil if the source module does not exist.
2518 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002519
Jeongik Chad5fe8782021-07-08 01:13:11 +09002520 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00002521 dexJarFile OptionalDexJarPath
2522 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09002523
2524 // Expected install file path of the source module(sdk_library)
2525 // or dex implementation jar obtained from the prebuilt_apex, if any.
2526 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002527}
2528
Paul Duffineedc5d52020-06-12 17:46:39 +01002529var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002530
Paul Duffin46a26a82020-04-07 19:27:04 +01002531// The type of a structure that contains a field of type sdkLibraryScopeProperties
2532// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002533//
2534// struct {
2535// Public sdkLibraryScopeProperties
2536// System sdkLibraryScopeProperties
2537// ...
2538// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002539var allScopeStructType = createAllScopePropertiesStructType()
2540
2541// Dynamically create a structure type for each apiscope in allApiScopes.
2542func createAllScopePropertiesStructType() reflect.Type {
2543 var fields []reflect.StructField
2544 for _, apiScope := range allApiScopes {
2545 field := reflect.StructField{
2546 Name: apiScope.fieldName,
2547 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2548 }
2549 fields = append(fields, field)
2550 }
2551
2552 return reflect.StructOf(fields)
2553}
2554
2555// Create an instance of the scope specific structure type and return a map
2556// from apiscope to a pointer to each scope specific field.
2557func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2558 allScopePropertiesPtr := reflect.New(allScopeStructType)
2559 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2560 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2561
2562 for _, apiScope := range allApiScopes {
2563 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2564 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2565 }
2566
2567 return allScopePropertiesPtr.Interface(), scopeProperties
2568}
2569
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002570// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002571func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002572 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002573
Paul Duffin46a26a82020-04-07 19:27:04 +01002574 allScopeProperties, scopeToProperties := createPropertiesInstance()
2575 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002576 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002577
Paul Duffinc3091c82020-05-08 14:16:20 +01002578 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002579 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002580
Paul Duffin0bdcb272020-02-06 15:24:57 +00002581 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002582 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002583 InitJavaModule(module, android.HostAndDeviceSupported)
2584
Paul Duffin1b1e8062020-05-08 13:44:43 +01002585 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2586 if module.initCommonAfterDefaultsApplied(mctx) {
2587 module.createInternalModules(mctx)
2588 }
2589 })
Colin Cross79c7c262019-04-17 11:11:46 -07002590 return module
2591}
2592
Paul Duffin630b11e2021-07-15 13:35:26 +01002593var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2594
2595func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2596 return module.properties.Permitted_packages
2597}
2598
Paul Duffineedc5d52020-06-12 17:46:39 +01002599func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002600 return &module.prebuilt
2601}
2602
Paul Duffineedc5d52020-06-12 17:46:39 +01002603func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002604 return module.prebuilt.Name(module.ModuleBase.Name())
2605}
2606
Paul Duffineedc5d52020-06-12 17:46:39 +01002607func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002608
Paul Duffin50061512020-01-21 16:31:05 +00002609 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002610 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002611 module.prebuilt.ForcePrefer()
2612 }
2613
Paul Duffin46a26a82020-04-07 19:27:04 +01002614 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002615 if len(scopeProperties.Jars) == 0 {
2616 continue
2617 }
2618
Paul Duffinbbb546b2020-04-09 00:07:11 +01002619 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002620
Paul Duffin0f8faff2020-05-20 16:18:00 +01002621 if len(scopeProperties.Stub_srcs) > 0 {
2622 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2623 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002624
2625 if scopeProperties.Current_api != nil {
2626 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2627 }
Paul Duffin56d44902020-01-31 13:36:25 +00002628 }
Colin Cross79c7c262019-04-17 11:11:46 -07002629
2630 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2631 javaSdkLibrariesLock.Lock()
2632 defer javaSdkLibrariesLock.Unlock()
2633 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2634}
2635
Paul Duffineedc5d52020-06-12 17:46:39 +01002636func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002637 // Creates a java import for the jar with ".stubs" suffix
2638 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002639 Name *string
2640 Sdk_version *string
2641 Libs []string
2642 Jars []string
Paul Duffin1267d872021-04-16 17:21:36 +01002643 Compile_dex *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002644
2645 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002646 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002647 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002648 props.Sdk_version = scopeProperties.Sdk_version
2649 // Prepend any of the libs from the legacy public properties to the libs for each of the
2650 // scopes to avoid having to duplicate them in each scope.
2651 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2652 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002653
Paul Duffin38b57852020-05-13 16:08:09 +01002654 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002655 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002656
Paul Duffin1267d872021-04-16 17:21:36 +01002657 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002658 compileDex := module.properties.Compile_dex
2659 if module.stubLibrariesCompiledForDex() {
2660 compileDex = proptools.BoolPtr(true)
2661 }
2662 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002663
Paul Duffin859fe962020-05-15 10:20:31 +01002664 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002665}
2666
Paul Duffineedc5d52020-06-12 17:46:39 +01002667func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002668 props := struct {
Paul Duffinbf4de042022-09-27 12:41:52 +01002669 Name *string
2670 Srcs []string
2671
2672 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002673 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002674 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002675 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002676
2677 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002678 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2679
Spandan Das2cc80ba2023-10-27 17:21:52 +00002680 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002681}
2682
Jihoon Kang71c86832023-09-13 01:01:53 +00002683func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2684 api_file := scopeProperties.Current_api
2685 api_surface := &apiScope.name
2686
2687 props := struct {
2688 Name *string
2689 Api_surface *string
2690 Api_file *string
2691 Visibility []string
2692 }{}
2693
2694 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
2695 props.Api_surface = api_surface
2696 props.Api_file = api_file
2697 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2698
Spandan Das2cc80ba2023-10-27 17:21:52 +00002699 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002700}
2701
Paul Duffin44f1d842020-06-26 20:17:02 +01002702// Add the dependencies on the child module in the component deps mutator so that it
2703// creates references to the prebuilt and not the source modules.
2704func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002705 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002706 if len(scopeProperties.Jars) == 0 {
2707 continue
2708 }
2709
2710 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002711 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002712
2713 if len(scopeProperties.Stub_srcs) > 0 {
2714 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002715 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002716 }
Paul Duffin56d44902020-01-31 13:36:25 +00002717 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002718}
2719
2720// Add other dependencies as normal.
2721func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002722
2723 implName := module.implLibraryModuleName()
2724 if ctx.OtherModuleExists(implName) {
2725 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2726
2727 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2728 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2729 // Add dependency to the rule for generating the xml permissions file
2730 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2731 }
2732 }
Colin Cross79c7c262019-04-17 11:11:46 -07002733}
2734
Jiyong Park45bf82e2020-12-15 22:29:02 +09002735var _ android.ApexModule = (*SdkLibraryImport)(nil)
2736
2737// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002738func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2739 depTag := mctx.OtherModuleDependencyTag(dep)
2740 if depTag == xmlPermissionsFileTag {
2741 return true
2742 }
2743
2744 // None of the other dependencies of the java_sdk_library_import are in the same apex
2745 // as the one that references this module.
2746 return false
2747}
2748
Jiyong Park45bf82e2020-12-15 22:29:02 +09002749// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002750func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2751 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002752 // we don't check prebuilt modules for sdk_version
2753 return nil
2754}
2755
Paul Duffinea8f8082021-06-24 13:25:57 +01002756// Implements android.ApexModule
2757func (module *SdkLibraryImport) UniqueApexVariations() bool {
2758 return module.uniqueApexVariations()
2759}
2760
Paul Duffin09817d62022-04-28 17:45:11 +01002761// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002762func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2763 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002764}
2765
2766var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2767
Paul Duffineedc5d52020-06-12 17:46:39 +01002768func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002769 paths, err := module.commonOutputFiles(tag)
2770 if paths != nil || err != nil {
2771 return paths, err
2772 }
2773 if module.implLibraryModule != nil {
2774 return module.implLibraryModule.OutputFiles(tag)
2775 } else {
2776 return nil, nil
2777 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002778}
2779
Paul Duffineedc5d52020-06-12 17:46:39 +01002780func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002781 module.generateCommonBuildActions(ctx)
2782
Jeongik Chad5fe8782021-07-08 01:13:11 +09002783 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2784 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2785
Paul Duffin0f8faff2020-05-20 16:18:00 +01002786 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002787 ctx.VisitDirectDeps(func(to android.Module) {
2788 tag := ctx.OtherModuleDependencyTag(to)
2789
Paul Duffin0f8faff2020-05-20 16:18:00 +01002790 // Extract information from any of the scope specific dependencies.
2791 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2792 apiScope := scopeTag.apiScope
2793 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2794
2795 // Extract information from the dependency. The exact information extracted
2796 // is determined by the nature of the dependency which is determined by the tag.
2797 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002798 } else if tag == implLibraryTag {
2799 if implLibrary, ok := to.(*Library); ok {
2800 module.implLibraryModule = implLibrary
2801 } else {
2802 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2803 }
2804 } else if tag == xmlPermissionsFileTag {
2805 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2806 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2807 } else {
2808 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2809 }
Colin Cross79c7c262019-04-17 11:11:46 -07002810 }
2811 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002812
2813 // Populate the scope paths with information from the properties.
2814 for apiScope, scopeProperties := range module.scopeProperties {
2815 if len(scopeProperties.Jars) == 0 {
2816 continue
2817 }
2818
2819 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002820 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002821 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2822 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2823 }
Paul Duffin39853512021-02-26 11:09:39 +00002824
2825 if ctx.Device() {
2826 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2827 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08002828 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00002829 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002830 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00002831 di, err := android.FindDeapexerProviderForModule(ctx)
2832 if err != nil {
2833 // An error was found, possibly due to multiple apexes in the tree that export this library
2834 // Defer the error till a client tries to call DexJarBuildPath
2835 module.dexJarFileErr = err
2836 return
Martin Stjernholm44825602021-09-17 01:44:12 +01002837 }
Spandan Das5be63332023-12-13 00:06:32 +00002838 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(module.BaseModuleName())
Jiakai Zhang81e46812023-02-08 21:56:07 +08002839 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002840 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2841 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002842 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002843 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002844 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002845 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002846
Jiakai Zhang204356f2021-09-09 08:12:46 +00002847 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2848 module.dexpreopter.isSDKLibrary = true
2849 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002850
2851 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2852 module.dexpreopter.inputProfilePathOnHost = profilePath
2853 }
2854
2855 // Dexpreopting.
Jiakai Zhang204356f2021-09-09 08:12:46 +00002856 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002857 } else {
2858 // This should never happen as a variant for a prebuilt_apex is only created if the
2859 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002860 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002861 }
2862 }
2863 }
Colin Cross79c7c262019-04-17 11:11:46 -07002864}
2865
Jiyong Parkf1691d22021-03-29 20:11:58 +09002866func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002867
2868 // For consistency with SdkLibrary make the implementation jar available to libraries that
2869 // are within the same APEX.
2870 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002871 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002872 if headerJars {
2873 return implLibraryModule.HeaderJars()
2874 } else {
2875 return implLibraryModule.ImplementationJars()
2876 }
2877 }
2878
Paul Duffin23970f42020-05-20 14:20:02 +01002879 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002880}
2881
Colin Cross79c7c262019-04-17 11:11:46 -07002882// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002883func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002884 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002885 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002886}
2887
2888// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002889func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002890 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002891 return module.sdkJars(ctx, sdkVersion, false)
2892}
2893
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002894// to satisfy UsesLibraryDependency interface
Spandan Das59a4a2b2024-01-09 21:35:56 +00002895func (module *SdkLibraryImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002896 // The dex implementation jar extracted from the .apex file should be used in preference to the
2897 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00002898 if module.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002899 ctx.ModuleErrorf(module.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00002900 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002901 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002902 return module.dexJarFile
2903 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002904 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002905 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002906 } else {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002907 return module.implLibraryModule.DexJarBuildPath(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +01002908 }
2909}
2910
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002911// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002912func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002913 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002914}
2915
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002916// to satisfy UsesLibraryDependency interface
2917func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2918 return nil
2919}
2920
Paul Duffineedc5d52020-06-12 17:46:39 +01002921// to satisfy apex.javaDependency interface
2922func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2923 if module.implLibraryModule == nil {
2924 return nil
2925 } else {
2926 return module.implLibraryModule.JacocoReportClassesFile()
2927 }
2928}
2929
2930// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002931func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2932 if module.implLibraryModule == nil {
2933 return LintDepSets{}
2934 } else {
2935 return module.implLibraryModule.LintDepSets()
2936 }
2937}
2938
Spandan Das17854f52022-01-14 21:19:14 +00002939func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002940 if module.implLibraryModule == nil {
2941 return false
2942 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002943 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002944 }
2945}
2946
Spandan Das17854f52022-01-14 21:19:14 +00002947func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002948 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002949 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002950 }
2951}
2952
Colin Cross08dca382020-07-21 20:31:17 -07002953// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002954func (module *SdkLibraryImport) Stem() string {
2955 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002956}
Jiyong Parke3833882020-02-17 17:28:10 +09002957
Paul Duffin44b481b2020-06-17 16:59:43 +01002958var _ ApexDependency = (*SdkLibraryImport)(nil)
2959
2960// to satisfy java.ApexDependency interface
2961func (module *SdkLibraryImport) HeaderJars() android.Paths {
2962 if module.implLibraryModule == nil {
2963 return nil
2964 } else {
2965 return module.implLibraryModule.HeaderJars()
2966 }
2967}
2968
2969// to satisfy java.ApexDependency interface
2970func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2971 if module.implLibraryModule == nil {
2972 return nil
2973 } else {
2974 return module.implLibraryModule.ImplementationAndResourcesJars()
2975 }
2976}
2977
Jiakai Zhang204356f2021-09-09 08:12:46 +00002978// to satisfy java.DexpreopterInterface interface
2979func (module *SdkLibraryImport) IsInstallable() bool {
2980 return true
2981}
2982
Paul Duffinfef55002021-06-17 14:56:05 +01002983var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2984
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002985func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002986 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08002987 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01002988}
2989
Jiyong Parke3833882020-02-17 17:28:10 +09002990// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09002991type sdkLibraryXml struct {
2992 android.ModuleBase
2993 android.DefaultableModuleBase
2994 android.ApexModuleBase
2995
2996 properties sdkLibraryXmlProperties
2997
2998 outputFilePath android.OutputPath
2999 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07003000
3001 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09003002}
3003
3004type sdkLibraryXmlProperties struct {
3005 // canonical name of the lib
3006 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003007
3008 // Signals that this shared library is part of the bootclasspath starting
3009 // on the version indicated in this attribute.
3010 //
3011 // This will make platforms at this level and above to ignore
3012 // <uses-library> tags with this library name because the library is already
3013 // available
3014 On_bootclasspath_since *string
3015
3016 // Signals that this shared library was part of the bootclasspath before
3017 // (but not including) the version indicated in this attribute.
3018 //
3019 // The system will automatically add a <uses-library> tag with this library to
3020 // apps that target any SDK less than the version indicated in this attribute.
3021 On_bootclasspath_before *string
3022
3023 // Indicates that PackageManager should ignore this shared library if the
3024 // platform is below the version indicated in this attribute.
3025 //
3026 // This means that the device won't recognise this library as installed.
3027 Min_device_sdk *string
3028
3029 // Indicates that PackageManager should ignore this shared library if the
3030 // platform is above the version indicated in this attribute.
3031 //
3032 // This means that the device won't recognise this library as installed.
3033 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00003034
3035 // The SdkLibrary's min api level as a string
3036 //
3037 // This value comes from the ApiLevel of the MinSdkVersion property.
3038 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00003039
3040 // Uses-libs dependencies that the shared library requires to work correctly.
3041 //
3042 // This will add dependency="foo:bar" to the <library> section.
3043 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09003044}
3045
3046// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
3047// Not to be used directly by users. java_sdk_library internally uses this.
3048func sdkLibraryXmlFactory() android.Module {
3049 module := &sdkLibraryXml{}
3050
3051 module.AddProperties(&module.properties)
3052
3053 android.InitApexModule(module)
3054 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
3055
3056 return module
3057}
3058
Colin Crossaede88c2020-08-11 12:17:01 -07003059func (module *sdkLibraryXml) UniqueApexVariations() bool {
3060 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
3061 // mounted APEX, which contains the name of the APEX.
3062 return true
3063}
3064
Jiyong Parke3833882020-02-17 17:28:10 +09003065// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09003066func (module *sdkLibraryXml) BaseDir() string {
3067 return "etc"
3068}
3069
3070// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09003071func (module *sdkLibraryXml) SubDir() string {
3072 return "permissions"
3073}
3074
3075// from android.PrebuiltEtcModule
3076func (module *sdkLibraryXml) OutputFile() android.OutputPath {
3077 return module.outputFilePath
3078}
3079
3080// from android.ApexModule
3081func (module *sdkLibraryXml) AvailableFor(what string) bool {
3082 return true
3083}
3084
3085func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
3086 // do nothing
3087}
3088
Jiyong Park45bf82e2020-12-15 22:29:02 +09003089var _ android.ApexModule = (*sdkLibraryXml)(nil)
3090
3091// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003092func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3093 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003094 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
3095 return nil
3096}
3097
Jiyong Parke3833882020-02-17 17:28:10 +09003098// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07003099func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09003100 implName := proptools.String(module.properties.Lib_name)
Colin Crossff694a82023-12-13 15:54:49 -08003101 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07003102 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09003103 // In most cases, this works fine. But when apex_name is set or override_apex is used
3104 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07003105 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09003106 }
3107 partition := "system"
3108 if module.SocSpecific() {
3109 partition = "vendor"
3110 } else if module.DeviceSpecific() {
3111 partition = "odm"
3112 } else if module.ProductSpecific() {
3113 partition = "product"
3114 } else if module.SystemExtSpecific() {
3115 partition = "system_ext"
3116 }
3117 return "/" + partition + "/framework/" + implName + ".jar"
3118}
3119
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003120func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
3121 if value == nil {
3122 return ""
3123 }
3124 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
3125 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003126 // attributes in bp files have underscores but in the xml have dashes.
3127 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003128 return ""
3129 }
Pedro Loureirob638c622021-12-22 15:28:05 +00003130 if apiLevel.IsCurrent() {
3131 // passing "current" would always mean a future release, never the current (or the current in
3132 // progress) which means some conditions would never be triggered.
3133 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3134 `"current" is not an allowed value for this attribute`)
3135 return ""
3136 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003137 // "safeValue" is safe because it translates finalized codenames to a string
3138 // with their SDK int.
3139 safeValue := apiLevel.String()
3140 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003141}
3142
3143// formats an attribute for the xml permissions file if the value is not null
3144// returns empty string otherwise
3145func formattedOptionalAttribute(attrName string, value *string) string {
3146 if value == nil {
3147 return ""
3148 }
3149 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
3150}
3151
Jamie Garsidee570ace2023-11-27 12:07:36 +00003152func formattedDependenciesAttribute(dependencies []string) string {
3153 if dependencies == nil {
3154 return ""
3155 }
3156 return fmt.Sprintf(` dependency=\"%s\"\n`, strings.Join(dependencies, ":"))
3157}
3158
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003159func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3160 libName := proptools.String(module.properties.Lib_name)
3161 libNameAttr := formattedOptionalAttribute("name", &libName)
3162 filePath := module.implPath(ctx)
3163 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003164 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3165 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3166 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3167 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003168 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003169 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3170 // 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 +00003171 var libraryTag string
3172 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003173 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00003174 } else {
3175 libraryTag = ` <library\n`
3176 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003177
3178 return strings.Join([]string{
3179 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
3180 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
3181 `\n`,
3182 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
3183 ` you may not use this file except in compliance with the License.\n`,
3184 ` You may obtain a copy of the License at\n`,
3185 `\n`,
3186 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
3187 `\n`,
3188 ` Unless required by applicable law or agreed to in writing, software\n`,
3189 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
3190 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
3191 ` See the License for the specific language governing permissions and\n`,
3192 ` limitations under the License.\n`,
3193 `-->\n`,
3194 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00003195 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003196 libNameAttr,
3197 filePathAttr,
3198 implicitFromAttr,
3199 implicitUntilAttr,
3200 minSdkAttr,
3201 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003202 dependenciesAttr,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003203 ` />\n`,
3204 `</permissions>\n`}, "")
3205}
3206
Jiyong Parke3833882020-02-17 17:28:10 +09003207func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossff694a82023-12-13 15:54:49 -08003208 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
3209 module.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07003210
Jiyong Parke3833882020-02-17 17:28:10 +09003211 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003212 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003213 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003214
3215 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08003216 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003217 rule.Command().
3218 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
3219 Output(module.outputFilePath)
3220
Colin Crossf1a035e2020-11-16 17:32:30 -08003221 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09003222
3223 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
3224}
3225
3226func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003227 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003228 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003229 Disabled: true,
3230 }}
3231 }
3232
satayev8f088b02021-12-06 11:40:46 +00003233 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003234 Class: "ETC",
3235 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3236 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003237 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003238 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003239 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003240 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3241 },
3242 },
3243 }}
3244}
Paul Duffindd46f712020-02-10 13:37:10 +00003245
Pedro Loureiroc3621422021-09-28 15:40:23 +00003246func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3247 module.validateAtLeastTAttributes(ctx)
3248 module.validateMinAndMaxDeviceSdk(ctx)
3249 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3250 module.validateOnBootclasspathBeforeRequirements(ctx)
3251}
3252
3253func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3254 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3255 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3256 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3257 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3258 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3259}
3260
3261func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3262 if attr != nil {
3263 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3264 // we will inform the user of invalid inputs when we try to write the
3265 // permissions xml file so we don't need to do it here
3266 if t.GreaterThan(level) {
3267 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3268 }
3269 }
3270 }
3271}
3272
3273func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3274 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3275 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3276 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3277 if minErr == nil && maxErr == nil {
3278 // we will inform the user of invalid inputs when we try to write the
3279 // permissions xml file so we don't need to do it here
3280 if min.GreaterThan(max) {
3281 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3282 }
3283 }
3284 }
3285}
3286
3287func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3288 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3289 if module.properties.Min_device_sdk != nil {
3290 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3291 if err == nil {
3292 if moduleMinApi.GreaterThan(api) {
3293 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3294 }
3295 }
3296 }
3297 if module.properties.Max_device_sdk != nil {
3298 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3299 if err == nil {
3300 if moduleMinApi.GreaterThan(api) {
3301 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3302 }
3303 }
3304 }
3305}
3306
3307func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3308 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3309 if module.properties.On_bootclasspath_before != nil {
3310 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3311 // if we use the attribute, then we need to do this validation
3312 if moduleMinApi.LessThan(t) {
3313 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3314 if module.properties.Min_device_sdk == nil {
3315 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")
3316 }
3317 }
3318 }
3319}
3320
Paul Duffindd46f712020-02-10 13:37:10 +00003321type sdkLibrarySdkMemberType struct {
3322 android.SdkMemberTypeBase
3323}
3324
Paul Duffin296701e2021-07-14 10:29:36 +01003325func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3326 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003327}
3328
3329func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3330 _, ok := module.(*SdkLibrary)
3331 return ok
3332}
3333
3334func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3335 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3336}
3337
3338func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3339 return &sdkLibrarySdkMemberProperties{}
3340}
3341
Paul Duffin976b0e52021-04-27 23:20:26 +01003342var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3343 android.SdkMemberTypeBase{
3344 PropertyName: "java_sdk_libs",
3345 SupportsSdk: true,
3346 },
3347}
3348
Paul Duffindd46f712020-02-10 13:37:10 +00003349type sdkLibrarySdkMemberProperties struct {
3350 android.SdkMemberPropertiesBase
3351
Paul Duffine8409952022-09-22 16:24:46 +01003352 // Stem name for files in the sdk snapshot.
3353 //
3354 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3355 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3356 //
3357 // This property is marked as keep so that it will be kept in all instances of this struct, will
3358 // not be cleared but will be copied to common structs. That is needed because this field is used
3359 // to construct many file names for other parts of this struct and so it needs to be present in
3360 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3361 // be unavailable for generating file names if there were other properties that were still set.
3362 Stem string `sdk:"keep"`
3363
Paul Duffindd46f712020-02-10 13:37:10 +00003364 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003365 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003366
Paul Duffin3d1248c2020-04-09 00:10:17 +01003367 // The Java stubs source files.
3368 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003369
3370 // The naming scheme.
3371 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003372
3373 // True if the java_sdk_library_import is for a shared library, false
3374 // otherwise.
3375 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003376
Paul Duffin1267d872021-04-16 17:21:36 +01003377 // True if the stub imports should produce dex jars.
3378 Compile_dex *bool
3379
Paul Duffina2ae7e02020-09-11 11:55:00 +01003380 // The paths to the doctag files to add to the prebuilt.
3381 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003382
3383 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003384
3385 // Signals that this shared library is part of the bootclasspath starting
3386 // on the version indicated in this attribute.
3387 //
3388 // This will make platforms at this level and above to ignore
3389 // <uses-library> tags with this library name because the library is already
3390 // available
3391 On_bootclasspath_since *string
3392
3393 // Signals that this shared library was part of the bootclasspath before
3394 // (but not including) the version indicated in this attribute.
3395 //
3396 // The system will automatically add a <uses-library> tag with this library to
3397 // apps that target any SDK less than the version indicated in this attribute.
3398 On_bootclasspath_before *string
3399
3400 // Indicates that PackageManager should ignore this shared library if the
3401 // platform is below the version indicated in this attribute.
3402 //
3403 // This means that the device won't recognise this library as installed.
3404 Min_device_sdk *string
3405
3406 // Indicates that PackageManager should ignore this shared library if the
3407 // platform is above the version indicated in this attribute.
3408 //
3409 // This means that the device won't recognise this library as installed.
3410 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003411
3412 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003413}
3414
3415type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003416 Jars android.Paths
3417 StubsSrcJar android.Path
3418 CurrentApiFile android.Path
3419 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003420 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003421 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003422}
3423
3424func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3425 sdk := variant.(*SdkLibrary)
3426
Paul Duffine8409952022-09-22 16:24:46 +01003427 // Copy the stem name for files in the sdk snapshot.
3428 s.Stem = sdk.distStem()
3429
Paul Duffin106a3a42022-01-27 16:39:06 +00003430 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003431 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003432 paths := sdk.findScopePaths(apiScope)
3433 if paths == nil {
3434 continue
3435 }
3436
Paul Duffindd46f712020-02-10 13:37:10 +00003437 jars := paths.stubsImplPath
3438 if len(jars) > 0 {
3439 properties := scopeProperties{}
3440 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003441 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003442 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003443 if paths.currentApiFilePath.Valid() {
3444 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3445 }
3446 if paths.removedApiFilePath.Valid() {
3447 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3448 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003449 // The annotations zip is only available for modules that set annotations_enabled: true.
3450 if paths.annotationsZip.Valid() {
3451 properties.AnnotationsZip = paths.annotationsZip.Path()
3452 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003453 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003454 }
3455 }
3456
Paul Duffindfa131e2020-05-15 20:37:11 +01003457 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003458 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003459 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003460 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003461 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003462 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3463 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3464 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3465 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003466
3467 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3468 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3469 }
Paul Duffindd46f712020-02-10 13:37:10 +00003470}
3471
3472func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003473 if s.Naming_scheme != nil {
3474 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3475 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003476 if s.Shared_library != nil {
3477 propertySet.AddProperty("shared_library", *s.Shared_library)
3478 }
Paul Duffin1267d872021-04-16 17:21:36 +01003479 if s.Compile_dex != nil {
3480 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3481 }
Paul Duffin869de142021-07-15 14:14:41 +01003482 if len(s.Permitted_packages) > 0 {
3483 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3484 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003485 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3486 if s.DexPreoptProfileGuided != nil {
3487 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3488 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003489
Paul Duffine8409952022-09-22 16:24:46 +01003490 stem := s.Stem
3491
Paul Duffindd46f712020-02-10 13:37:10 +00003492 for _, apiScope := range allApiScopes {
3493 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003494 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003495
Paul Duffin958806b2022-05-16 13:10:47 +00003496 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003497
Paul Duffindd46f712020-02-10 13:37:10 +00003498 var jars []string
3499 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003500 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003501 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3502 jars = append(jars, dest)
3503 }
3504 scopeSet.AddProperty("jars", jars)
3505
Paul Duffin22628d52021-05-12 23:13:22 +01003506 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3507 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003508 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003509 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3510 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3511 } else {
3512 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3513 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003514 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003515 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3516 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3517 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003518
Paul Duffin1fd005d2020-04-09 01:08:11 +01003519 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003520 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003521 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3522 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3523 }
3524
3525 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003526 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003527 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003528 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3529 }
3530
Anton Hanssond78eb762021-09-21 15:25:12 +01003531 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003532 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003533 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3534 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3535 }
3536
Paul Duffindd46f712020-02-10 13:37:10 +00003537 if properties.SdkVersion != "" {
3538 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3539 }
3540 }
3541 }
3542
Paul Duffina2ae7e02020-09-11 11:55:00 +01003543 if len(s.Doctag_paths) > 0 {
3544 dests := []string{}
3545 for _, p := range s.Doctag_paths {
3546 dest := filepath.Join("doctags", p.Rel())
3547 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3548 dests = append(dests, dest)
3549 }
3550 propertySet.AddProperty("doctag_files", dests)
3551 }
Paul Duffindd46f712020-02-10 13:37:10 +00003552}