blob: d532aaa00b289e4d557eb59b7602cf664c20c436 [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jihoon Kangee113282024-01-23 00:16:41 +000018 "errors"
Jiyong Parkc678ad32018-04-10 13:07:10 +090019 "fmt"
20 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090021 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010022 "reflect"
Paul Duffin46dc45a2020-05-14 15:39:10 +010023 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090024 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090025 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090026 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090027
Paul Duffind1b3a922020-01-22 11:57:20 +000028 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090029 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010030
31 "android/soong/android"
Ulya Trafimovichdbf31662020-12-17 12:07:54 +000032 "android/soong/dexpreopt"
Jiyong Parkc678ad32018-04-10 13:07:10 +090033)
34
Jooyung Han58f26ab2019-12-18 15:34:32 +090035const (
Pedro Loureiro9956e5e2021-09-07 17:21:59 +000036 sdkXmlFileSuffix = ".xml"
Jiyong Parkc678ad32018-04-10 13:07:10 +090037)
38
Paul Duffind1b3a922020-01-22 11:57:20 +000039// A tag to associated a dependency with a specific api scope.
40type scopeDependencyTag struct {
41 blueprint.BaseDependencyTag
42 name string
43 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010044
45 // Function for extracting appropriate path information from the dependency.
Colin Crossdcf71b22021-02-01 13:59:03 -080046 depInfoExtractor func(paths *scopePaths, ctx android.ModuleContext, dep android.Module) error
Paul Duffinc8782502020-04-29 20:45:27 +010047}
48
49// Extract tag specific information from the dependency.
50func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
Colin Crossdcf71b22021-02-01 13:59:03 -080051 err := tag.depInfoExtractor(paths, ctx, dep)
Paul Duffinc8782502020-04-29 20:45:27 +010052 if err != nil {
53 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
54 }
Paul Duffind1b3a922020-01-22 11:57:20 +000055}
56
Paul Duffin80342d72020-06-26 22:08:43 +010057var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
58
59func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
60 return false
61}
62
Paul Duffind1b3a922020-01-22 11:57:20 +000063// Provides information about an api scope, e.g. public, system, test.
64type apiScope struct {
65 // The name of the api scope, e.g. public, system, test
66 name string
67
Paul Duffin97b53b82020-05-05 14:40:52 +010068 // The api scope that this scope extends.
Paul Duffind0b9fca2022-09-30 18:11:41 +010069 //
70 // This organizes the scopes into an extension hierarchy.
71 //
72 // If set this means that the API provided by this scope includes the API provided by the scope
73 // set in this field.
Paul Duffin97b53b82020-05-05 14:40:52 +010074 extends *apiScope
75
Paul Duffind0b9fca2022-09-30 18:11:41 +010076 // The next api scope that a library that uses this scope can access.
77 //
78 // This organizes the scopes into an access hierarchy.
79 //
80 // If set this means that a library that can access this API can also access the API provided by
81 // the scope set in this field.
82 //
83 // A module that sets sdk_version: "<scope>_current" should have access to the <scope> API of
84 // every java_sdk_library that it depends on. If the library does not provide an API for <scope>
85 // then it will traverse up this access hierarchy to find an API that it does provide.
86 //
87 // If this is not set then it defaults to the scope set in extends.
88 canAccess *apiScope
89
Paul Duffin3375e352020-04-28 10:44:03 +010090 // The legacy enabled status for a specific scope can be dependent on other
91 // properties that have been specified on the library so it is provided by
92 // a function that can determine the status by examining those properties.
93 legacyEnabledStatus func(module *SdkLibrary) bool
94
95 // The default enabled status for non-legacy behavior, which is triggered by
96 // explicitly enabling at least one api scope.
97 defaultEnabledStatus bool
98
99 // Gets a pointer to the scope specific properties.
100 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
101
Paul Duffin46a26a82020-04-07 19:27:04 +0100102 // The name of the field in the dynamically created structure.
103 fieldName string
104
Paul Duffin6b836ba2020-05-13 19:19:49 +0100105 // The name of the property in the java_sdk_library_import
106 propertyName string
107
Jihoon Kangb7431552024-01-22 19:40:08 +0000108 // The tag to use to depend on the prebuilt stubs library module
109 prebuiltStubsTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000110
Jihoon Kangbd093452023-12-26 19:08:01 +0000111 // The tag to use to depend on the everything stubs library module.
112 everythingStubsTag scopeDependencyTag
113
114 // The tag to use to depend on the exportable stubs library module.
115 exportableStubsTag scopeDependencyTag
116
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100117 // The tag to use to depend on the stubs source module (if separate from the API module).
118 stubsSourceTag scopeDependencyTag
119
120 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
121 apiFileTag scopeDependencyTag
122
Paul Duffinc8782502020-04-29 20:45:27 +0100123 // The tag to use to depend on the stubs source and API module.
124 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000125
Paul Duffin958806b2022-05-16 13:10:47 +0000126 // The tag to use to depend on the module that provides the latest version of the API .txt file.
127 latestApiModuleTag scopeDependencyTag
128
129 // The tag to use to depend on the module that provides the latest version of the API removed.txt
130 // file.
131 latestRemovedApiModuleTag scopeDependencyTag
132
Paul Duffind1b3a922020-01-22 11:57:20 +0000133 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
134 apiFilePrefix string
135
Paul Duffind0b9fca2022-09-30 18:11:41 +0100136 // The scope specific suffix to add to the sdk library module name to construct a scope specific
Paul Duffind1b3a922020-01-22 11:57:20 +0000137 // module name.
138 moduleSuffix string
139
Paul Duffind1b3a922020-01-22 11:57:20 +0000140 // SDK version that the stubs library is built against. Note that this is always
141 // *current. Older stubs library built with a numbered SDK version is created from
142 // the prebuilt jar.
143 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100144
Paul Duffin15f34ef2020-07-20 18:04:44 +0100145 // The annotation that identifies this API level, empty for the public API scope.
146 annotation string
147
Paul Duffin1fb487d2020-04-07 18:50:10 +0100148 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100149 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100150 // This is not used directly but is used to construct the droidstubsArgs.
151 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100152
Paul Duffin15f34ef2020-07-20 18:04:44 +0100153 // The args that must be passed to droidstubs to generate the API and stubs source
154 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100155 //
156 // The API only includes the additional members that this scope adds over the scope
157 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100158 //
159 // The stubs source must include the definitions of everything that is in this
160 // api scope and all the scopes that this one extends.
161 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100162
Anton Hansson6478ac12020-05-02 11:19:36 +0100163 // Whether the api scope can be treated as unstable, and should skip compat checks.
164 unstable bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000165
166 // Represents the SDK kind of this scope.
167 kind android.SdkKind
Paul Duffind1b3a922020-01-22 11:57:20 +0000168}
169
170// Initialize a scope, creating and adding appropriate dependency tags
171func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100172 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100173 scopeByName[name] = scope
174 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100175 scope.propertyName = strings.ReplaceAll(name, "-", "_")
176 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Jihoon Kangb7431552024-01-22 19:40:08 +0000177 scope.prebuiltStubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100178 name: name + "-stubs",
179 apiScope: scope,
180 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000181 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000182 scope.everythingStubsTag = scopeDependencyTag{
183 name: name + "-stubs-everything",
184 apiScope: scope,
185 depInfoExtractor: (*scopePaths).extractEverythingStubsLibraryInfoFromDependency,
186 }
187 scope.exportableStubsTag = scopeDependencyTag{
188 name: name + "-stubs-exportable",
189 apiScope: scope,
190 depInfoExtractor: (*scopePaths).extractExportableStubsLibraryInfoFromDependency,
191 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100192 scope.stubsSourceTag = scopeDependencyTag{
193 name: name + "-stubs-source",
194 apiScope: scope,
195 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
196 }
197 scope.apiFileTag = scopeDependencyTag{
198 name: name + "-api",
199 apiScope: scope,
200 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
201 }
Paul Duffinc8782502020-04-29 20:45:27 +0100202 scope.stubsSourceAndApiTag = scopeDependencyTag{
203 name: name + "-stubs-source-and-api",
204 apiScope: scope,
205 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000206 }
Paul Duffin958806b2022-05-16 13:10:47 +0000207 scope.latestApiModuleTag = scopeDependencyTag{
208 name: name + "-latest-api",
209 apiScope: scope,
210 depInfoExtractor: (*scopePaths).extractLatestApiPath,
211 }
212 scope.latestRemovedApiModuleTag = scopeDependencyTag{
213 name: name + "-latest-removed-api",
214 apiScope: scope,
215 depInfoExtractor: (*scopePaths).extractLatestRemovedApiPath,
216 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100217
218 // To get the args needed to generate the stubs source append all the args from
219 // this scope and all the scopes it extends as each set of args adds additional
220 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100221 var scopeSpecificArgs []string
222 if scope.annotation != "" {
223 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100224 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100225 for s := scope; s != nil; s = s.extends {
226 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100227
Paul Duffin15f34ef2020-07-20 18:04:44 +0100228 // Ensure that the generated stubs includes all the API elements from the API scope
229 // that this scope extends.
230 if s != scope && s.annotation != "" {
231 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
232 }
233 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100234
Paul Duffind0b9fca2022-09-30 18:11:41 +0100235 // By default, a library that can access a scope can also access the scope it extends.
236 if scope.canAccess == nil {
237 scope.canAccess = scope.extends
238 }
239
Paul Duffin15f34ef2020-07-20 18:04:44 +0100240 // Escape any special characters in the arguments. This is needed because droidstubs
241 // passes these directly to the shell command.
242 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100243
Paul Duffind1b3a922020-01-22 11:57:20 +0000244 return scope
245}
246
Anton Hansson08f476b2021-04-07 15:32:19 +0100247func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
248 return ".stubs" + scope.moduleSuffix
249}
250
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000251func (scope *apiScope) exportableStubsLibraryModuleNameSuffix() string {
252 return ".stubs.exportable" + scope.moduleSuffix
253}
254
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000255func (scope *apiScope) apiLibraryModuleName(baseName string) string {
256 return scope.stubsLibraryModuleName(baseName) + ".from-text"
257}
258
Jihoon Kang1147b312023-06-08 23:25:57 +0000259func (scope *apiScope) sourceStubLibraryModuleName(baseName string) string {
260 return scope.stubsLibraryModuleName(baseName) + ".from-source"
261}
262
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000263func (scope *apiScope) exportableSourceStubsLibraryModuleName(baseName string) string {
264 return scope.exportableStubsLibraryModuleName(baseName) + ".from-source"
265}
266
Paul Duffinc3091c82020-05-08 14:16:20 +0100267func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100268 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000269}
270
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000271func (scope *apiScope) exportableStubsLibraryModuleName(baseName string) string {
272 return baseName + scope.exportableStubsLibraryModuleNameSuffix()
273}
274
Paul Duffinc8782502020-04-29 20:45:27 +0100275func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100276 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000277}
278
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100279func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100280 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100281}
282
Paul Duffin3375e352020-04-28 10:44:03 +0100283func (scope *apiScope) String() string {
284 return scope.name
285}
286
Paul Duffin958806b2022-05-16 13:10:47 +0000287// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
288// be stored.
289func (scope *apiScope) snapshotRelativeDir() string {
290 return filepath.Join("sdk_library", scope.name)
291}
292
293// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
294// library.
295func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
296 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
297}
298
299// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
300// named library.
301func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
302 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
303}
304
Paul Duffind1b3a922020-01-22 11:57:20 +0000305type apiScopes []*apiScope
306
307func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
308 var list []string
309 for _, scope := range scopes {
310 list = append(list, accessor(scope))
311 }
312 return list
313}
314
Jihoon Kanga96a7b12023-09-20 23:43:32 +0000315// Method that maps the apiScopes properties to the index of each apiScopes elements.
316// apiScopes property to be used as the key can be specified with the input accessor.
317// Only a string property of apiScope can be used as the key of the map.
318func (scopes apiScopes) MapToIndex(accessor func(*apiScope) string) map[string]int {
319 ret := make(map[string]int)
320 for i, scope := range scopes {
321 ret[accessor(scope)] = i
322 }
323 return ret
324}
325
Jiyong Parkc678ad32018-04-10 13:07:10 +0900326var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100327 scopeByName = make(map[string]*apiScope)
328 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000329 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100330 name: "public",
331
332 // Public scope is enabled by default for both legacy and non-legacy modes.
333 legacyEnabledStatus: func(module *SdkLibrary) bool {
334 return true
335 },
336 defaultEnabledStatus: true,
337
338 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
339 return &module.sdkLibraryProperties.Public
340 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000341 sdkVersion: "current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000342 kind: android.SdkPublic,
Paul Duffind1b3a922020-01-22 11:57:20 +0000343 })
344 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100345 name: "system",
346 extends: apiScopePublic,
347 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
348 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
349 return &module.sdkLibraryProperties.System
350 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100351 apiFilePrefix: "system-",
352 moduleSuffix: ".system",
353 sdkVersion: "system_current",
354 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000355 kind: android.SdkSystem,
Paul Duffind1b3a922020-01-22 11:57:20 +0000356 })
357 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100358 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100359 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100360 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
361 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
362 return &module.sdkLibraryProperties.Test
363 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100364 apiFilePrefix: "test-",
365 moduleSuffix: ".test",
366 sdkVersion: "test_current",
367 annotation: "android.annotation.TestApi",
368 unstable: true,
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000369 kind: android.SdkTest,
Paul Duffind1b3a922020-01-22 11:57:20 +0000370 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100371 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100372 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100373 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100374 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100375 //
376 // Enabling this would break existing usages.
377 legacyEnabledStatus: func(module *SdkLibrary) bool {
378 return false
379 },
380 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
381 return &module.sdkLibraryProperties.Module_lib
382 },
383 apiFilePrefix: "module-lib-",
384 moduleSuffix: ".module_lib",
385 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100386 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000387 kind: android.SdkModule,
Paul Duffin8f265b92020-04-28 14:13:56 +0100388 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100389 apiScopeSystemServer = initApiScope(&apiScope{
390 name: "system-server",
391 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100392
393 // The system-server scope can access the module-lib scope.
394 //
395 // A module that provides a system-server API is appended to the standard bootclasspath that is
396 // used by the system server. So, it should be able to access module-lib APIs provided by
397 // libraries on the bootclasspath.
398 canAccess: apiScopeModuleLib,
399
Paul Duffin0c5bae52020-06-02 13:00:08 +0100400 // The system-server scope is disabled by default in legacy mode.
401 //
402 // Enabling this would break existing usages.
403 legacyEnabledStatus: func(module *SdkLibrary) bool {
404 return false
405 },
406 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
407 return &module.sdkLibraryProperties.System_server
408 },
409 apiFilePrefix: "system-server-",
410 moduleSuffix: ".system_server",
411 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100412 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
413 extraArgs: []string{
414 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100415 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100416 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100417 },
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000418 kind: android.SdkSystemServer,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100419 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000420 allApiScopes = apiScopes{
421 apiScopePublic,
422 apiScopeSystem,
423 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100424 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100425 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000426 }
Jihoon Kang0c705a42023-08-02 06:44:57 +0000427 apiLibraryAdditionalProperties = map[string]struct {
428 FullApiSurfaceStubLib string
429 AdditionalApiContribution string
430 }{
431 "legacy.i18n.module.platform.api": {
432 FullApiSurfaceStubLib: "legacy.core.platform.api.stubs",
433 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
434 },
435 "stable.i18n.module.platform.api": {
436 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
437 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
438 },
439 "conscrypt.module.platform.api": {
440 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
441 AdditionalApiContribution: "conscrypt.module.public.api.stubs.source.api.contribution",
442 },
443 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900444)
445
Jiyong Park82484c02018-04-23 21:41:26 +0900446var (
447 javaSdkLibrariesLock sync.Mutex
448)
449
Jiyong Parkc678ad32018-04-10 13:07:10 +0900450// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900451// 1) disallowing linking to the runtime shared lib
452// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900453
454func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000455 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900456
Jiyong Park82484c02018-04-23 21:41:26 +0900457 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
458 javaSdkLibraries := javaSdkLibraries(ctx.Config())
459 sort.Strings(*javaSdkLibraries)
460 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
461 })
Paul Duffindd46f712020-02-10 13:37:10 +0000462
463 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100464 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900465}
466
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000467func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
468 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
469 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
470}
471
Paul Duffin3375e352020-04-28 10:44:03 +0100472// Properties associated with each api scope.
473type ApiScopeProperties struct {
474 // Indicates whether the api surface is generated.
475 //
476 // If this is set for any scope then all scopes must explicitly specify if they
477 // are enabled. This is to prevent new usages from depending on legacy behavior.
478 //
479 // Otherwise, if this is not set for any scope then the default behavior is
480 // scope specific so please refer to the scope specific property documentation.
481 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100482
483 // The sdk_version to use for building the stubs.
484 //
485 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000486 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100487 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000488 // will be none. This is used for java_sdk_library instances that are used
489 // to create stubs that contribute to the core_current sdk version.
490 // 2) Otherwise, it is assumed that this library extends but does not
491 // contribute directly to a specific sdk_version and so this uses the
492 // sdk_version appropriate for the api scope. e.g. public will use
493 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100494 //
495 // This does not affect the sdk_version used for either generating the stubs source
496 // or the API file. They both have to use the same sdk_version as is used for
497 // compiling the implementation library.
498 Sdk_version *string
Mark White9421c4c2023-08-10 00:07:03 +0000499
500 // Extra libs used when compiling stubs for this scope.
501 Libs []string
Paul Duffin3375e352020-04-28 10:44:03 +0100502}
503
Jiyong Parkc678ad32018-04-10 13:07:10 +0900504type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100505 // List of source files that are needed to compile the API, but are not part of runtime library.
506 Api_srcs []string `android:"arch_variant"`
507
Paul Duffin5df79302020-05-16 15:52:12 +0100508 // Visibility for impl library module. If not specified then defaults to the
509 // visibility property.
510 Impl_library_visibility []string
511
Paul Duffin4911a892020-04-29 23:35:13 +0100512 // Visibility for stubs library modules. If not specified then defaults to the
513 // visibility property.
514 Stubs_library_visibility []string
515
516 // Visibility for stubs source modules. If not specified then defaults to the
517 // visibility property.
518 Stubs_source_visibility []string
519
Anton Hansson7f66efa2020-10-08 14:47:23 +0100520 // List of Java libraries that will be in the classpath when building the implementation lib
521 Impl_only_libs []string `android:"arch_variant"`
522
Paul Duffin77590a82022-04-28 14:13:30 +0000523 // List of Java libraries that will included in the implementation lib.
524 Impl_only_static_libs []string `android:"arch_variant"`
525
Sundong Ahnf043cf62018-06-25 16:04:37 +0900526 // List of Java libraries that will be in the classpath when building stubs
527 Stub_only_libs []string `android:"arch_variant"`
528
Anton Hanssondae54cd2021-04-21 16:30:10 +0100529 // List of Java libraries that will included in stub libraries
530 Stub_only_static_libs []string `android:"arch_variant"`
531
Paul Duffin7a586d32019-12-30 17:09:34 +0000532 // list of package names that will be documented and publicized as API.
533 // This allows the API to be restricted to a subset of the source files provided.
534 // If this is unspecified then all the source files will be treated as being part
535 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900536 Api_packages []string
537
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900538 // list of package names that must be hidden from the API
539 Hidden_api_packages []string
540
Paul Duffin749f98f2019-12-30 17:23:46 +0000541 // the relative path to the directory containing the api specification files.
542 // Defaults to "api".
543 Api_dir *string
544
Paul Duffindfa131e2020-05-15 20:37:11 +0100545 // Determines whether a runtime implementation library is built; defaults to false.
546 //
547 // If true then it also prevents the module from being used as a shared module, i.e.
MÃ¥rten Kongstad81d90952022-05-25 16:27:11 +0200548 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000549 Api_only *bool
550
Paul Duffin11512472019-02-11 15:55:17 +0000551 // local files that are used within user customized droiddoc options.
552 Droiddoc_option_files []string
553
Spandan Das93e95992021-07-29 18:26:39 +0000554 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000555 // Available variables for substitution:
556 //
557 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900558 Droiddoc_options []string
559
Paul Duffine22c2ab2020-05-20 19:35:27 +0100560 // is set to true, Metalava will allow framework SDK to contain annotations.
561 Annotations_enabled *bool
562
Sundong Ahn054b19a2018-10-19 13:46:09 +0900563 // a list of top-level directories containing files to merge qualifier annotations
564 // (i.e. those intended to be included in the stubs written) from.
565 Merge_annotations_dirs []string
566
567 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
568 Merge_inclusion_annotations_dirs []string
569
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000570 // If set to true then don't create dist rules.
571 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900572
Paul Duffin31310252020-11-20 21:26:20 +0000573 // The stem for the artifacts that are copied to the dist, if not specified
574 // then defaults to the base module name.
575 //
576 // For each scope the following artifacts are copied to the apistubs/<scope>
577 // directory in the dist.
578 // * stubs impl jar -> <dist-stem>.jar
579 // * API specification file -> api/<dist-stem>.txt
580 // * Removed API specification file -> api/<dist-stem>-removed.txt
581 //
582 // Also used to construct the name of the filegroup (created by prebuilt_apis)
583 // that references the latest released API and remove API specification files.
584 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
585 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800586 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000587 Dist_stem *string
588
Colin Cross986b69a2021-06-01 13:13:40 -0700589 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700590 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700591 // in the public Android SDK.
592 Dist_group *string
593
Anton Hanssondff2c782020-12-21 17:10:01 +0000594 // A compatibility mode that allows historical API-tracking files to not exist.
595 // Do not use.
596 Unsafe_ignore_missing_latest_api bool
597
Paul Duffin3375e352020-04-28 10:44:03 +0100598 // indicates whether system and test apis should be generated.
599 Generate_system_and_test_apis bool `blueprint:"mutated"`
600
601 // The properties specific to the public api scope
602 //
603 // Unless explicitly specified by using public.enabled the public api scope is
604 // enabled by default in both legacy and non-legacy mode.
605 Public ApiScopeProperties
606
607 // The properties specific to the system api scope
608 //
609 // In legacy mode the system api scope is enabled by default when sdk_version
610 // is set to something other than "none".
611 //
612 // In non-legacy mode the system api scope is disabled by default.
613 System ApiScopeProperties
614
615 // The properties specific to the test api scope
616 //
617 // In legacy mode the test api scope is enabled by default when sdk_version
618 // is set to something other than "none".
619 //
620 // In non-legacy mode the test api scope is disabled by default.
621 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000622
Paul Duffin0c5bae52020-06-02 13:00:08 +0100623 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100624 //
Zi Wangb2179e32023-01-31 15:53:30 -0800625 // Unless explicitly specified by using module_lib.enabled the module_lib api
626 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100627 Module_lib ApiScopeProperties
628
Paul Duffin0c5bae52020-06-02 13:00:08 +0100629 // The properties specific to the system-server api scope
630 //
Zi Wangb2179e32023-01-31 15:53:30 -0800631 // Unless explicitly specified by using system_server.enabled the
632 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100633 System_server ApiScopeProperties
634
Jiyong Park932cdfe2020-05-28 00:19:53 +0900635 // Determines if the stubs are preferred over the implementation library
636 // for linking, even when the client doesn't specify sdk_version. When this
637 // is set to true, such clients are provided with the widest API surface that
638 // this lib provides. Note however that this option doesn't affect the clients
639 // that are in the same APEX as this library. In that case, the clients are
640 // always linked with the implementation library. Default is false.
641 Default_to_stubs *bool
642
Paul Duffin160fe412020-05-10 19:32:20 +0100643 // Properties related to api linting.
644 Api_lint struct {
645 // Enable api linting.
646 Enabled *bool
Anton Hanssonfd1c0d22023-11-02 15:18:09 +0000647
648 // If API lint is enabled, this flag controls whether a set of legitimate lint errors
649 // are turned off. The default is true.
650 Legacy_errors_allowed *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100651 }
652
Jihoon Kang80456fd2023-11-15 19:22:14 +0000653 // Determines if the module contributes to any api surfaces.
654 // This property should be set to true only if the module is listed under
655 // frameworks-base-api.bootclasspath in frameworks/base/api/Android.bp.
656 // Otherwise, this property should be set to false.
657 // Defaults to false.
658 Contribute_to_android_api *bool
659
Jihoon Kang6592e872023-12-19 01:13:16 +0000660 // a list of aconfig_declarations module names that the stubs generated in this module
661 // depend on.
662 Aconfig_declarations []string
663
Jiyong Parkc678ad32018-04-10 13:07:10 +0900664 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100665 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900666}
667
Paul Duffin0f8faff2020-05-20 16:18:00 +0100668// Paths to outputs from java_sdk_library and java_sdk_library_import.
669//
670// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
671// OptionalPaths are always set by java_sdk_library but may not be set by
672// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000673type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100674 // The path (represented as Paths for convenience when returning) to the stubs header jar.
675 //
676 // That is the jar that is created by turbine.
677 stubsHeaderPath android.Paths
678
679 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
680 //
681 // This is not the implementation jar, it still only contains stubs.
682 stubsImplPath android.Paths
683
Paul Duffin1267d872021-04-16 17:21:36 +0100684 // The dex jar for the stubs.
685 //
686 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100687 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100688
Jihoon Kangbd093452023-12-26 19:08:01 +0000689 // The exportable dex jar for the stubs.
690 // This is not the implementation jar, it still only contains stubs.
691 // Includes unflagged apis and flagged apis enabled by release configurations.
692 exportableStubsDexJarPath OptionalDexJarPath
693
Paul Duffin0f8faff2020-05-20 16:18:00 +0100694 // The API specification file, e.g. system_current.txt.
695 currentApiFilePath android.OptionalPath
696
697 // The specification of API elements removed since the last release.
698 removedApiFilePath android.OptionalPath
699
700 // The stubs source jar.
701 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100702
703 // Extracted annotations.
704 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000705
706 // The path to the latest API file.
707 latestApiPath android.OptionalPath
708
709 // The path to the latest removed API file.
710 latestRemovedApiPath android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000711}
712
Colin Crossdcf71b22021-02-01 13:59:03 -0800713func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Colin Cross313aa542023-12-13 13:47:44 -0800714 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800715 paths.stubsHeaderPath = lib.HeaderJars
716 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100717
718 libDep := dep.(UsesLibraryDependency)
Spandan Das59a4a2b2024-01-09 21:35:56 +0000719 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
Jihoon Kangbd093452023-12-26 19:08:01 +0000720 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
721 return nil
722 } else {
723 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
724 }
725}
726
727func (paths *scopePaths) extractEverythingStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
728 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
729 paths.stubsHeaderPath = lib.HeaderJars
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000730 if !ctx.Config().ReleaseHiddenApiExportableStubs() {
731 paths.stubsImplPath = lib.ImplementationJars
732 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000733
734 libDep := dep.(UsesLibraryDependency)
735 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
736 return nil
737 } else {
738 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
739 }
740}
741
742func (paths *scopePaths) extractExportableStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000743 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
744 if ctx.Config().ReleaseHiddenApiExportableStubs() {
745 paths.stubsImplPath = lib.ImplementationJars
746 }
747
Jihoon Kangbd093452023-12-26 19:08:01 +0000748 libDep := dep.(UsesLibraryDependency)
749 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
Paul Duffinc8782502020-04-29 20:45:27 +0100750 return nil
751 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800752 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100753 }
754}
755
Jihoon Kangee113282024-01-23 00:16:41 +0000756func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider) error) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100757 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000758 err := action(apiStubsProvider)
759 if err != nil {
760 return err
761 }
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000762 return nil
763 } else {
764 return fmt.Errorf("expected module that implements ExportableApiStubsSrcProvider, e.g. droidstubs")
765 }
766}
767
Jihoon Kangee113282024-01-23 00:16:41 +0000768func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider) error) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100769 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000770 err := action(apiStubsProvider)
771 if err != nil {
772 return err
773 }
Paul Duffin0f8faff2020-05-20 16:18:00 +0100774 return nil
775 } else {
776 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
777 }
778}
779
Jihoon Kangee113282024-01-23 00:16:41 +0000780func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider, stubsType StubsType) error {
781 var annotationsZip, currentApiFilePath, removedApiFilePath android.Path
782 annotationsZip, annotationsZipErr := provider.AnnotationsZip(stubsType)
783 currentApiFilePath, currentApiFilePathErr := provider.ApiFilePath(stubsType)
784 removedApiFilePath, removedApiFilePathErr := provider.RemovedApiFilePath(stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100785
Jihoon Kangee113282024-01-23 00:16:41 +0000786 combinedError := errors.Join(annotationsZipErr, currentApiFilePathErr, removedApiFilePathErr)
787
788 if combinedError == nil {
789 paths.annotationsZip = android.OptionalPathForPath(annotationsZip)
790 paths.currentApiFilePath = android.OptionalPathForPath(currentApiFilePath)
791 paths.removedApiFilePath = android.OptionalPathForPath(removedApiFilePath)
792 }
793 return combinedError
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000794}
795
Colin Crossdcf71b22021-02-01 13:59:03 -0800796func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangee113282024-01-23 00:16:41 +0000797 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
798 return paths.extractApiInfoFromApiStubsProvider(provider, Everything)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100799 })
800}
801
Jihoon Kangee113282024-01-23 00:16:41 +0000802func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider, stubsType StubsType) error {
803 stubsSrcJar, err := provider.StubsSrcJar(stubsType)
804 if err == nil {
805 paths.stubsSrcJar = android.OptionalPathForPath(stubsSrcJar)
806 }
807 return err
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000808}
809
Colin Crossdcf71b22021-02-01 13:59:03 -0800810func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangee113282024-01-23 00:16:41 +0000811 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) error {
812 return paths.extractStubsSourceInfoFromApiStubsProviders(provider, Everything)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100813 })
814}
815
Colin Crossdcf71b22021-02-01 13:59:03 -0800816func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000817 if ctx.Config().ReleaseHiddenApiExportableStubs() {
Jihoon Kangee113282024-01-23 00:16:41 +0000818 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
819 extractApiInfoErr := paths.extractApiInfoFromApiStubsProvider(provider, Exportable)
820 extractStubsSourceInfoErr := paths.extractStubsSourceInfoFromApiStubsProviders(provider, Exportable)
821 return errors.Join(extractApiInfoErr, extractStubsSourceInfoErr)
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000822 })
823 }
Jihoon Kangee113282024-01-23 00:16:41 +0000824 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
825 extractApiInfoErr := paths.extractApiInfoFromApiStubsProvider(provider, Everything)
826 extractStubsSourceInfoErr := paths.extractStubsSourceInfoFromApiStubsProviders(provider, Everything)
827 return errors.Join(extractApiInfoErr, extractStubsSourceInfoErr)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100828 })
829}
830
Paul Duffin958806b2022-05-16 13:10:47 +0000831func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
832 var paths android.Paths
833 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
834 paths = sourceFileProducer.Srcs()
835 } else {
836 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
837 }
838 if len(paths) != 1 {
839 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
840 }
841 return android.OptionalPathForPath(paths[0]), nil
842}
843
844func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
845 outputPath, err := extractSingleOptionalOutputPath(dep)
846 paths.latestApiPath = outputPath
847 return err
848}
849
850func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
851 outputPath, err := extractSingleOptionalOutputPath(dep)
852 paths.latestRemovedApiPath = outputPath
853 return err
854}
855
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100856type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100857 // The naming scheme to use for the components that this module creates.
858 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100859 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100860 //
861 // This is a temporary mechanism to simplify conversion from separate modules for each
862 // component that follow a different naming pattern to the default one.
863 //
864 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100865 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100866
867 // Specifies whether this module can be used as an Android shared library; defaults
868 // to true.
869 //
870 // An Android shared library is one that can be referenced in a <uses-library> element
871 // in an AndroidManifest.xml.
872 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100873
874 // Files containing information about supported java doc tags.
875 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000876
877 // Signals that this shared library is part of the bootclasspath starting
878 // on the version indicated in this attribute.
879 //
880 // This will make platforms at this level and above to ignore
881 // <uses-library> tags with this library name because the library is already
882 // available
883 On_bootclasspath_since *string
884
885 // Signals that this shared library was part of the bootclasspath before
886 // (but not including) the version indicated in this attribute.
887 //
888 // The system will automatically add a <uses-library> tag with this library to
889 // apps that target any SDK less than the version indicated in this attribute.
890 On_bootclasspath_before *string
891
892 // Indicates that PackageManager should ignore this shared library if the
893 // platform is below the version indicated in this attribute.
894 //
895 // This means that the device won't recognise this library as installed.
896 Min_device_sdk *string
897
898 // Indicates that PackageManager should ignore this shared library if the
899 // platform is above the version indicated in this attribute.
900 //
901 // This means that the device won't recognise this library as installed.
902 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100903}
904
Paul Duffin71b33cc2021-06-23 11:39:47 +0100905// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
906// embeds the commonToSdkLibraryAndImport struct.
907type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000908 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100909
Spandan Das23956d12024-01-19 00:22:22 +0000910 // Returns the name of the root java_sdk_library that creates the child stub libraries
911 // This is the `name` as it appears in Android.bp, and not the name in Soong's build graph
912 // (with the prebuilt_ prefix)
913 //
914 // e.g. in the following java_sdk_library_import
915 // java_sdk_library_import {
916 // name: "framework-foo.v1",
917 // source_module_name: "framework-foo",
918 // }
919 // the values returned by
920 // 1. Name(): prebuilt_framework-foo.v1 # unique
921 // 2. BaseModuleName(): framework-foo # the source
922 // 3. RootLibraryName: framework-foo.v1 # the undecordated `name` from Android.bp
923 RootLibraryName() string
924}
925
926func (m *SdkLibrary) RootLibraryName() string {
927 return m.BaseModuleName()
928}
929
930func (m *SdkLibraryImport) RootLibraryName() string {
931 // m.BaseModuleName refers to the source of the import
932 // use moduleBase.Name to get the name of the module as it appears in the .bp file
933 return m.ModuleBase.Name()
Paul Duffin71b33cc2021-06-23 11:39:47 +0100934}
935
Paul Duffin56d44902020-01-31 13:36:25 +0000936// Common code between sdk library and sdk library import
937type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100938 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100939
Paul Duffin56d44902020-01-31 13:36:25 +0000940 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100941
942 namingScheme sdkLibraryComponentNamingScheme
943
Paul Duffindfa131e2020-05-15 20:37:11 +0100944 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100945
Paul Duffina2ae7e02020-09-11 11:55:00 +0100946 // Paths to commonSdkLibraryProperties.Doctag_files
947 doctagPaths android.Paths
948
Paul Duffin859fe962020-05-15 10:20:31 +0100949 // Functionality related to this being used as a component of a java_sdk_library.
950 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000951}
952
Paul Duffin71b33cc2021-06-23 11:39:47 +0100953func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
954 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100955
Paul Duffin71b33cc2021-06-23 11:39:47 +0100956 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100957
958 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100959 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100960}
961
962func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100963 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100964 switch schemeProperty {
965 case "default":
966 c.namingScheme = &defaultNamingScheme{}
967 default:
968 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
969 return false
970 }
971
Spandan Das23956d12024-01-19 00:22:22 +0000972 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +0100973 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
974
Paul Duffindfa131e2020-05-15 20:37:11 +0100975 // Only track this sdk library if this can be used as a shared library.
976 if c.sharedLibrary() {
977 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100978 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100979 }
Paul Duffin859fe962020-05-15 10:20:31 +0100980
Paul Duffin1b1e8062020-05-08 13:44:43 +0100981 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100982}
983
Paul Duffinea8f8082021-06-24 13:25:57 +0100984// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
985// method.
986func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
987 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
988 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
989 // the APEX and so it needs a unique variation per APEX.
990 return c.sharedLibrary()
991}
992
Paul Duffina2ae7e02020-09-11 11:55:00 +0100993func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
994 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
995}
996
Paul Duffineedc5d52020-06-12 17:46:39 +0100997// Module name of the runtime implementation library
998func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +0000999 return c.module.RootLibraryName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +01001000}
1001
1002// Module name of the XML file for the lib
1003func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +00001004 return c.module.RootLibraryName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +01001005}
1006
Paul Duffinc3091c82020-05-08 14:16:20 +01001007// Name of the java_library module that compiles the stubs source.
1008func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001009 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001010 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001011}
1012
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001013// Name of the java_library module that compiles the exportable stubs source.
1014func (c *commonToSdkLibraryAndImport) exportableStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001015 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001016 return c.namingScheme.exportableStubsLibraryModuleName(apiScope, baseName)
1017}
1018
Paul Duffinc3091c82020-05-08 14:16:20 +01001019// Name of the droidstubs module that generates the stubs source and may also
1020// generate/check the API.
1021func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001022 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001023 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001024}
1025
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001026// Name of the java_api_library module that generates the from-text stubs source
1027// and compiles to a jar file.
1028func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001029 baseName := c.module.RootLibraryName()
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001030 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
1031}
1032
Jihoon Kang1147b312023-06-08 23:25:57 +00001033// Name of the java_library module that compiles the stubs
1034// generated from source Java files.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001035func (c *commonToSdkLibraryAndImport) sourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001036 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001037 return c.namingScheme.sourceStubsLibraryModuleName(apiScope, baseName)
1038}
1039
1040// Name of the java_library module that compiles the exportable stubs
1041// generated from source Java files.
1042func (c *commonToSdkLibraryAndImport) exportableSourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001043 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001044 return c.namingScheme.exportableSourceStubsLibraryModuleName(apiScope, baseName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001045}
1046
Paul Duffin46dc45a2020-05-14 15:39:10 +01001047// The component names for different outputs of the java_sdk_library.
1048//
1049// They are similar to the names used for the child modules it creates
1050const (
1051 stubsSourceComponentName = "stubs.source"
1052
1053 apiTxtComponentName = "api.txt"
1054
1055 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +01001056
1057 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +01001058)
1059
1060// A regular expression to match tags that reference a specific stubs component.
1061//
1062// It will only match if given a valid scope and a valid component. It is verfy strict
1063// to ensure it does not accidentally match a similar looking tag that should be processed
1064// by the embedded Library.
1065var tagSplitter = func() *regexp.Regexp {
1066 // Given a list of literal string items returns a regular expression that will
1067 // match any one of the items.
1068 choice := func(items ...string) string {
1069 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
1070 }
1071
1072 // Regular expression to match one of the scopes.
1073 scopesRegexp := choice(allScopeNames...)
1074
1075 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +01001076 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001077
1078 // Regular expression to match any combination of one scope and one component.
1079 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
1080}()
1081
1082// For OutputFileProducer interface
1083//
Anton Hanssond78eb762021-09-21 15:25:12 +01001084// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001085func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
1086 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
1087 scopeName := groups[1]
1088 component := groups[2]
1089
1090 if scope, ok := scopeByName[scopeName]; ok {
1091 paths := c.findScopePaths(scope)
1092 if paths == nil {
Spandan Das23956d12024-01-19 00:22:22 +00001093 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.RootLibraryName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001094 }
1095
1096 switch component {
1097 case stubsSourceComponentName:
1098 if paths.stubsSrcJar.Valid() {
1099 return android.Paths{paths.stubsSrcJar.Path()}, nil
1100 }
1101
1102 case apiTxtComponentName:
1103 if paths.currentApiFilePath.Valid() {
1104 return android.Paths{paths.currentApiFilePath.Path()}, nil
1105 }
1106
1107 case removedApiTxtComponentName:
1108 if paths.removedApiFilePath.Valid() {
1109 return android.Paths{paths.removedApiFilePath.Path()}, nil
1110 }
Anton Hanssond78eb762021-09-21 15:25:12 +01001111
1112 case annotationsComponentName:
1113 if paths.annotationsZip.Valid() {
1114 return android.Paths{paths.annotationsZip.Path()}, nil
1115 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001116 }
1117
1118 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
1119 } else {
1120 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
1121 }
1122
1123 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001124 switch tag {
1125 case ".doctags":
1126 if c.doctagPaths != nil {
1127 return c.doctagPaths, nil
1128 } else {
Spandan Das23956d12024-01-19 00:22:22 +00001129 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.RootLibraryName())
Paul Duffina2ae7e02020-09-11 11:55:00 +01001130 }
1131 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001132 return nil, nil
1133 }
1134}
1135
Paul Duffin803a9562020-05-20 11:52:25 +01001136func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001137 if c.scopePaths == nil {
1138 c.scopePaths = make(map[*apiScope]*scopePaths)
1139 }
1140 paths := c.scopePaths[scope]
1141 if paths == nil {
1142 paths = &scopePaths{}
1143 c.scopePaths[scope] = paths
1144 }
1145
1146 return paths
1147}
1148
Paul Duffin803a9562020-05-20 11:52:25 +01001149func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1150 if c.scopePaths == nil {
1151 return nil
1152 }
1153
1154 return c.scopePaths[scope]
1155}
1156
1157// If this does not support the requested api scope then find the closest available
1158// scope it does support. Returns nil if no such scope is available.
1159func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001160 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001161 if paths := c.findScopePaths(s); paths != nil {
1162 return paths
1163 }
1164 }
1165
1166 // This should never happen outside tests as public should be the base scope for every
1167 // scope and is enabled by default.
1168 return nil
1169}
1170
Jiyong Parkf1691d22021-03-29 20:11:58 +09001171func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001172
1173 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001174 if !sdkVersion.ApiLevel.IsPreview() {
Spandan Das23956d12024-01-19 00:22:22 +00001175 return PrebuiltJars(ctx, c.module.RootLibraryName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001176 }
1177
Paul Duffin1267d872021-04-16 17:21:36 +01001178 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1179 if paths == nil {
1180 return nil
1181 }
1182
1183 return paths.stubsHeaderPath
1184}
1185
1186// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1187//
1188// If the module does not support the specific kind then it will return the *scopePaths for the
1189// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1190// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1191func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001192 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001193
Paul Duffin803a9562020-05-20 11:52:25 +01001194 paths := c.findClosestScopePath(apiScope)
1195 if paths == nil {
1196 var scopes []string
1197 for _, s := range allApiScopes {
1198 if c.findScopePaths(s) != nil {
1199 scopes = append(scopes, s.name)
1200 }
1201 }
Spandan Das23956d12024-01-19 00:22:22 +00001202 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.module.RootLibraryName(), scopes)
Paul Duffin803a9562020-05-20 11:52:25 +01001203 return nil
1204 }
1205
Paul Duffin1267d872021-04-16 17:21:36 +01001206 return paths
1207}
1208
Paul Duffin32cf58a2021-05-18 16:32:50 +01001209// sdkKindToApiScope maps from android.SdkKind to apiScope.
1210func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1211 var apiScope *apiScope
1212 switch kind {
1213 case android.SdkSystem:
1214 apiScope = apiScopeSystem
1215 case android.SdkModule:
1216 apiScope = apiScopeModuleLib
1217 case android.SdkTest:
1218 apiScope = apiScopeTest
1219 case android.SdkSystemServer:
1220 apiScope = apiScopeSystemServer
1221 default:
1222 apiScope = apiScopePublic
1223 }
1224 return apiScope
1225}
1226
Paul Duffin1267d872021-04-16 17:21:36 +01001227// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001228func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001229 paths := c.selectScopePaths(ctx, kind)
1230 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001231 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001232 }
1233
1234 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001235}
1236
Paul Duffin32cf58a2021-05-18 16:32:50 +01001237// to satisfy SdkLibraryDependency interface
Jihoon Kangbd093452023-12-26 19:08:01 +00001238func (c *commonToSdkLibraryAndImport) SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
1239 paths := c.selectScopePaths(ctx, kind)
1240 if paths == nil {
1241 return makeUnsetDexJarPath()
1242 }
1243
1244 return paths.exportableStubsDexJarPath
1245}
1246
1247// to satisfy SdkLibraryDependency interface
Paul Duffin32cf58a2021-05-18 16:32:50 +01001248func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1249 apiScope := sdkKindToApiScope(kind)
1250 paths := c.findScopePaths(apiScope)
1251 if paths == nil {
1252 return android.OptionalPath{}
1253 }
1254
1255 return paths.removedApiFilePath
1256}
1257
Paul Duffin859fe962020-05-15 10:20:31 +01001258func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1259 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001260 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001261 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001262 }{}
1263
Spandan Das23956d12024-01-19 00:22:22 +00001264 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +01001265 componentProps.SdkLibraryName = namePtr
1266
Paul Duffindfa131e2020-05-15 20:37:11 +01001267 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001268 // Mark the stubs library as being components of this java_sdk_library so that
1269 // any app that includes code which depends (directly or indirectly) on the stubs
1270 // library will have the appropriate <uses-library> invocation inserted into its
1271 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001272 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001273 }
1274
1275 return componentProps
1276}
1277
Paul Duffindfa131e2020-05-15 20:37:11 +01001278func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1279 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1280}
1281
Paul Duffinf4600f62021-05-13 22:34:45 +01001282// Check if the stub libraries should be compiled for dex
1283func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1284 // Always compile the dex file files for the stub libraries if they will be used on the
1285 // bootclasspath.
1286 return !c.sharedLibrary()
1287}
1288
Paul Duffin859fe962020-05-15 10:20:31 +01001289// Properties related to the use of a module as an component of a java_sdk_library.
1290type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001291 // The name of the java_sdk_library/_import module.
1292 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001293
1294 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1295 // in the AndroidManifest.xml of any Android app that includes code that references
1296 // this module. If not set then no java_sdk_library/_import is tracked.
1297 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1298}
1299
1300// Structure to be embedded in a module struct that needs to support the
1301// SdkLibraryComponentDependency interface.
1302type EmbeddableSdkLibraryComponent struct {
1303 sdkLibraryComponentProperties SdkLibraryComponentProperties
1304}
1305
Paul Duffin71b33cc2021-06-23 11:39:47 +01001306func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1307 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001308}
1309
1310// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001311func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1312 return e.sdkLibraryComponentProperties.SdkLibraryName
1313}
1314
1315// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001316func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001317 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1318 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1319 // run-time library and the corresponding module that provides the implementation. This name is
1320 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1321 // in dexpreopt).
1322 //
1323 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1324 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001325 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1326}
1327
Paul Duffin859fe962020-05-15 10:20:31 +01001328// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1329// (including the java_sdk_library) itself.
1330type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001331 UsesLibraryDependency
1332
Paul Duffin3f0290e2021-06-30 18:25:36 +01001333 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1334 SdkLibraryName() *string
1335
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001336 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1337 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001338}
1339
1340// Make sure that all the module types that are components of java_sdk_library/_import
1341// and which can be referenced (directly or indirectly) from an android app implement
1342// the SdkLibraryComponentDependency interface.
1343var _ SdkLibraryComponentDependency = (*Library)(nil)
1344var _ SdkLibraryComponentDependency = (*Import)(nil)
1345var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001346var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001347
Paul Duffin32cf58a2021-05-18 16:32:50 +01001348// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001349type SdkLibraryDependency interface {
1350 SdkLibraryComponentDependency
1351
1352 // Get the header jars appropriate for the supplied sdk_version.
1353 //
1354 // These are turbine generated jars so they only change if the externals of the
1355 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001356 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001357
1358 // Get the implementation jars appropriate for the supplied sdk version.
1359 //
1360 // These are either the implementation jar for the whole sdk library or the implementation
1361 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1362 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001363 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001364
Jihoon Kangbd093452023-12-26 19:08:01 +00001365 // SdkApiStubDexJar returns the dex jar for the stubs for the prebuilt
1366 // java_sdk_library_import module. It is needed by the hiddenapi processing tool which
1367 // processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001368 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001369
Jihoon Kangbd093452023-12-26 19:08:01 +00001370 // SdkApiExportableStubDexJar returns the exportable dex jar for the stubs for
1371 // java_sdk_library module. It is needed by the hiddenapi processing tool which processes
1372 // dex files.
1373 SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
1374
Paul Duffin32cf58a2021-05-18 16:32:50 +01001375 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1376 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1377
Paul Duffinf4600f62021-05-13 22:34:45 +01001378 // sharedLibrary returns true if this can be used as a shared library.
1379 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001380}
1381
Inseob Kimc0907f12019-02-08 21:00:45 +09001382type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001383 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001384
Sundong Ahn054b19a2018-10-19 13:46:09 +09001385 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001386
Paul Duffin3375e352020-04-28 10:44:03 +01001387 // Map from api scope to the scope specific property structure.
1388 scopeToProperties map[*apiScope]*ApiScopeProperties
1389
Paul Duffin56d44902020-01-31 13:36:25 +00001390 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001391}
1392
Inseob Kimc0907f12019-02-08 21:00:45 +09001393var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001394
Paul Duffin3375e352020-04-28 10:44:03 +01001395func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1396 return module.sdkLibraryProperties.Generate_system_and_test_apis
1397}
1398
1399func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1400 // Check to see if any scopes have been explicitly enabled. If any have then all
1401 // must be.
1402 anyScopesExplicitlyEnabled := false
1403 for _, scope := range allApiScopes {
1404 scopeProperties := module.scopeToProperties[scope]
1405 if scopeProperties.Enabled != nil {
1406 anyScopesExplicitlyEnabled = true
1407 break
1408 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001409 }
Paul Duffin3375e352020-04-28 10:44:03 +01001410
1411 var generatedScopes apiScopes
1412 enabledScopes := make(map[*apiScope]struct{})
1413 for _, scope := range allApiScopes {
1414 scopeProperties := module.scopeToProperties[scope]
1415 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1416 // This is to ensure that any new usages of this module type do not rely on legacy
1417 // behaviour.
1418 defaultEnabledStatus := false
1419 if anyScopesExplicitlyEnabled {
1420 defaultEnabledStatus = scope.defaultEnabledStatus
1421 } else {
1422 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1423 }
1424 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1425 if enabled {
1426 enabledScopes[scope] = struct{}{}
1427 generatedScopes = append(generatedScopes, scope)
1428 }
1429 }
1430
1431 // Now check to make sure that any scope that is extended by an enabled scope is also
1432 // enabled.
1433 for _, scope := range allApiScopes {
1434 if _, ok := enabledScopes[scope]; ok {
1435 extends := scope.extends
1436 if extends != nil {
1437 if _, ok := enabledScopes[extends]; !ok {
1438 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1439 }
1440 }
1441 }
1442 }
1443
1444 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001445}
1446
satayev758968a2021-12-06 11:42:40 +00001447var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1448
satayev8f088b02021-12-06 11:40:46 +00001449func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001450 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001451 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1452 isExternal := !module.depIsInSameApex(ctx, child)
1453 if am, ok := child.(android.ApexModule); ok {
1454 if !do(ctx, parent, am, isExternal) {
1455 return false
1456 }
1457 }
1458 return !isExternal
1459 })
1460 })
1461}
1462
Paul Duffineedc5d52020-06-12 17:46:39 +01001463type sdkLibraryComponentTag struct {
1464 blueprint.BaseDependencyTag
1465 name string
1466}
1467
1468// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1469func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1470
1471var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001472
Jiyong Parke3833882020-02-17 17:28:10 +09001473func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001474 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001475 return dt == xmlPermissionsFileTag
1476 }
1477 return false
1478}
1479
Paul Duffineedc5d52020-06-12 17:46:39 +01001480var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001481
Paul Duffin44f1d842020-06-26 20:17:02 +01001482// Add the dependencies on the child modules in the component deps mutator.
1483func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001484 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001485 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001486 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kangbd093452023-12-26 19:08:01 +00001487 ctx.AddVariationDependencies(nil, apiScope.everythingStubsTag, stubModuleName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001488
Jihoon Kangbd093452023-12-26 19:08:01 +00001489 exportableStubModuleName := module.exportableStubsLibraryModuleName(apiScope)
1490 ctx.AddVariationDependencies(nil, apiScope.exportableStubsTag, exportableStubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001491
Paul Duffin15f34ef2020-07-20 18:04:44 +01001492 // Add a dependency on the stubs source in order to access both stubs source and api information.
1493 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001494
1495 if module.compareAgainstLatestApi(apiScope) {
1496 // Add dependencies on the latest finalized version of the API .txt file.
1497 latestApiModuleName := module.latestApiModuleName(apiScope)
1498 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1499
1500 // Add dependencies on the latest finalized version of the remove API .txt file.
1501 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1502 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1503 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001504 }
1505
Paul Duffindfa131e2020-05-15 20:37:11 +01001506 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001507 // Add dependency to the rule for generating the implementation library.
1508 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1509
Paul Duffindfa131e2020-05-15 20:37:11 +01001510 if module.sharedLibrary() {
1511 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001512 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001513 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001514 }
1515}
Paul Duffine74ac732020-02-06 13:51:46 +00001516
Paul Duffin44f1d842020-06-26 20:17:02 +01001517// Add other dependencies as normal.
1518func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001519 var missingApiModules []string
1520 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1521 if apiScope.unstable {
1522 continue
1523 }
Paul Duffin958806b2022-05-16 13:10:47 +00001524 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001525 missingApiModules = append(missingApiModules, m)
1526 }
Paul Duffin958806b2022-05-16 13:10:47 +00001527 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001528 missingApiModules = append(missingApiModules, m)
1529 }
Paul Duffin958806b2022-05-16 13:10:47 +00001530 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001531 missingApiModules = append(missingApiModules, m)
1532 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001533 }
1534 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1535 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1536 m += "You need to do one of the following:\n"
1537 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1538 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1539 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1540 m += "\n"
1541 m += "The following filegroup modules are missing:\n "
1542 m += strings.Join(missingApiModules, "\n ") + "\n"
1543 m += "Please see the documentation of the prebuilt_apis module type (and a usage example in prebuilts/sdk) for a convenient way to generate these."
1544 ctx.ModuleErrorf(m)
1545 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001546 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001547 // Only add the deps for the library if it is actually going to be built.
1548 module.Library.deps(ctx)
1549 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001550}
1551
Paul Duffin46dc45a2020-05-14 15:39:10 +01001552func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1553 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001554 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001555 return paths, err
1556 }
Colin Cross4acaea92021-12-10 23:05:02 +00001557 if module.requiresRuntimeImplementationLibrary() {
1558 return module.Library.OutputFiles(tag)
1559 }
1560 if tag == "" {
1561 return nil, nil
1562 }
1563 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001564}
1565
Inseob Kimc0907f12019-02-08 21:00:45 +09001566func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001567 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1568 module.CheckMinSdkVersion(ctx)
1569 }
1570
Paul Duffina2ae7e02020-09-11 11:55:00 +01001571 module.generateCommonBuildActions(ctx)
1572
Paul Duffindfa131e2020-05-15 20:37:11 +01001573 // Only build an implementation library if required.
1574 if module.requiresRuntimeImplementationLibrary() {
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001575 // stubsLinkType must be set before calling Library.GenerateAndroidBuildActions
1576 module.Library.stubsLinkType = Unknown
Paul Duffin43db9be2019-12-30 17:35:49 +00001577 module.Library.GenerateAndroidBuildActions(ctx)
1578 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001579
Paul Duffinb97b1572021-04-29 21:50:40 +01001580 // Collate the components exported by this module. All scope specific modules are exported but
1581 // the impl and xml component modules are not.
1582 exportedComponents := map[string]struct{}{}
1583
Sundong Ahn57368eb2018-07-06 11:20:23 +09001584 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001585 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001586 // the recorded paths will be returned depending on the link type of the caller.
1587 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001588 tag := ctx.OtherModuleDependencyTag(to)
1589
Paul Duffinc8782502020-04-29 20:45:27 +01001590 // Extract information from any of the scope specific dependencies.
1591 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1592 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001593 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001594
1595 // Extract information from the dependency. The exact information extracted
1596 // is determined by the nature of the dependency which is determined by the tag.
1597 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001598
1599 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001600 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001601 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001602
1603 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001604 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001605 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001606
1607 // Provide additional information for inclusion in an sdk's generated .info file.
1608 additionalSdkInfo := map[string]interface{}{}
1609 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001610 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001611 scopes := map[string]interface{}{}
1612 additionalSdkInfo["scopes"] = scopes
1613 for scope, scopePaths := range module.scopePaths {
1614 scopeInfo := map[string]interface{}{}
1615 scopes[scope.name] = scopeInfo
1616 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1617 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1618 if p := scopePaths.latestApiPath; p.Valid() {
1619 scopeInfo["latest_api"] = p.Path().String()
1620 }
1621 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1622 scopeInfo["latest_removed_api"] = p.Path().String()
1623 }
1624 }
Colin Cross40213022023-12-13 15:19:49 -08001625 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001626}
1627
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001628func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001629 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001630 return nil
1631 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001632 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001633 if module.sharedLibrary() {
1634 entries := &entriesList[0]
1635 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1636 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001637 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001638}
1639
Anton Hansson5fd5d242020-03-27 19:43:19 +00001640// The dist path of the stub artifacts
1641func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001642 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001643}
1644
Paul Duffin12ceb462019-12-24 20:31:31 +00001645// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001646func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001647 scopeProperties := module.scopeToProperties[apiScope]
1648 if scopeProperties.Sdk_version != nil {
1649 return proptools.String(scopeProperties.Sdk_version)
1650 }
1651
Jiyong Parkf1691d22021-03-29 20:11:58 +09001652 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001653 if sdkDep.hasStandardLibs() {
1654 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001655 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001656 } else {
1657 // Otherwise, use no system module.
1658 return "none"
1659 }
1660}
1661
Paul Duffin31310252020-11-20 21:26:20 +00001662func (module *SdkLibrary) distStem() string {
1663 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1664}
1665
Colin Cross986b69a2021-06-01 13:13:40 -07001666// distGroup returns the subdirectory of the dist path of the stub artifacts.
1667func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001668 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001669}
1670
Paul Duffin958806b2022-05-16 13:10:47 +00001671func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1672 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1673}
1674
Paul Duffind1b3a922020-01-22 11:57:20 +00001675func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001676 return ":" + module.latestApiModuleName(apiScope)
1677}
1678
1679func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
1680 return latestPrebuiltApiModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001681}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001682
Paul Duffind1b3a922020-01-22 11:57:20 +00001683func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001684 return ":" + module.latestRemovedApiModuleName(apiScope)
1685}
1686
1687func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
1688 return latestPrebuiltApiModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001689}
1690
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001691func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001692 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1693}
1694
1695func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1696 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001697}
1698
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001699func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1700 _, exists := c.GetApiLibraries()[module.Name()]
1701 return exists
1702}
1703
Jihoon Kang0c705a42023-08-02 06:44:57 +00001704// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1705// api surface that the module contribute to. For example, the public droidstubs and java_library
1706// do not contribute to the public api surface, but contributes to the core platform api surface.
1707// This method returns the full api surface stub lib that
1708// the generated java_api_library should depend on.
1709func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1710 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1711 return val.FullApiSurfaceStubLib
1712 }
1713 return ""
1714}
1715
1716// The listed modules' stubs contents do not match the corresponding txt files,
1717// but require additional api contributions to generate the full stubs.
1718// This method returns the name of the additional api contribution module
1719// for corresponding sdk_library modules.
1720func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1721 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1722 return val.AdditionalApiContribution
1723 }
1724 return ""
1725}
1726
Anton Hansson944e77d2020-08-19 11:40:22 +01001727func childModuleVisibility(childVisibility []string) []string {
1728 if childVisibility == nil {
1729 // No child visibility set. The child will use the visibility of the sdk_library.
1730 return nil
1731 }
1732
1733 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1734 var visibility []string
1735 visibility = append(visibility, "//visibility:override")
1736 visibility = append(visibility, childVisibility...)
1737 return visibility
1738}
1739
Paul Duffin5df79302020-05-16 15:52:12 +01001740// Creates the implementation java library
1741func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001742 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1743
Paul Duffin5df79302020-05-16 15:52:12 +01001744 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001745 Name *string
1746 Visibility []string
1747 Instrument bool
1748 Libs []string
1749 Static_libs []string
1750 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001751 }{
1752 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001753 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001754 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1755 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001756 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1757 // addition of &module.properties below.
1758 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001759 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1760 // addition of &module.properties below.
1761 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1762 // Pass the apex_available settings down so that the impl library can be statically
1763 // embedded within a library that is added to an APEX. Needed for updatable-media.
1764 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001765 }
1766
1767 properties := []interface{}{
1768 &module.properties,
1769 &module.protoProperties,
1770 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001771 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001772 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001773 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001774 &props,
1775 module.sdkComponentPropertiesForChildLibrary(),
1776 }
1777 mctx.CreateModule(LibraryFactory, properties...)
1778}
1779
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001780type libraryProperties struct {
1781 Name *string
1782 Visibility []string
1783 Srcs []string
1784 Installable *bool
1785 Sdk_version *string
1786 System_modules *string
1787 Patch_module *string
1788 Libs []string
1789 Static_libs []string
1790 Compile_dex *bool
1791 Java_version *string
1792 Openjdk9 struct {
1793 Srcs []string
1794 Javacflags []string
1795 }
1796 Dist struct {
1797 Targets []string
1798 Dest *string
1799 Dir *string
1800 Tag *string
1801 }
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001802 Is_stubs_module *bool
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001803}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001804
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001805func (module *SdkLibrary) stubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
1806 props := libraryProperties{}
Jihoon Kang786df932023-09-07 01:18:31 +00001807 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001808 // sources are generated from the droiddoc
Paul Duffin12ceb462019-12-24 20:31:31 +00001809 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001810 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001811 props.System_modules = module.deviceProperties.System_modules
1812 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001813 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001814 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001815 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001816 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001817 // The stub-annotations library contains special versions of the annotations
1818 // with CLASS retention policy, so that they're kept.
1819 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1820 props.Libs = append(props.Libs, "stub-annotations")
1821 }
Paul Duffina18abc22020-05-16 18:54:24 +01001822 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1823 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001824 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1825 // interop with older developer tools that don't support 1.9.
1826 props.Java_version = proptools.StringPtr("1.8")
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001827 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffinf4600f62021-05-13 22:34:45 +01001828
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001829 return props
1830}
1831
1832// Creates a static java library that has API stubs
1833func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1834
1835 props := module.stubsLibraryProps(mctx, apiScope)
1836 props.Name = proptools.StringPtr(module.sourceStubsLibraryModuleName(apiScope))
1837 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
1838
1839 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1840}
1841
1842// Create a static java library that compiles the "exportable" stubs
1843func (module *SdkLibrary) createExportableStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1844 props := module.stubsLibraryProps(mctx, apiScope)
1845 props.Name = proptools.StringPtr(module.exportableSourceStubsLibraryModuleName(apiScope))
1846 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope) + "{.exportable}"}
1847
Paul Duffin859fe962020-05-15 10:20:31 +01001848 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001849}
1850
Paul Duffin6d0886e2020-04-07 18:49:53 +01001851// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001852// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001853func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001854 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001855 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001856 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001857 Srcs []string
1858 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001859 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001860 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001861 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001862 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001863 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001864 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001865 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001866 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001867 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001868 Merge_annotations_dirs []string
1869 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001870 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001871 Previous_api *string
Jihoon Kang6592e872023-12-19 01:13:16 +00001872 Aconfig_declarations []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001873 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001874 Current ApiToCheck
1875 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001876
1877 Api_lint struct {
1878 Enabled *bool
1879 New_since *string
1880 Baseline_file *string
1881 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001882 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001883 Aidl struct {
1884 Include_dirs []string
1885 Local_include_dirs []string
1886 }
Paul Duffin040e9062020-11-23 17:41:36 +00001887 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001888 }{}
1889
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001890 // The stubs source processing uses the same compile time classpath when extracting the
1891 // API from the implementation library as it does when compiling it. i.e. the same
1892 // * sdk version
1893 // * system_modules
1894 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001895
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001896 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001897 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001898 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001899 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001900 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001901 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001902 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001903 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001904 // A droiddoc module has only one Libs property and doesn't distinguish between
1905 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001906 props.Libs = module.properties.Libs
1907 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001908 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001909 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001910 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1911 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1912 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001913
Paul Duffine22c2ab2020-05-20 19:35:27 +01001914 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001915 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1916 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
Jihoon Kang6592e872023-12-19 01:13:16 +00001917 props.Aconfig_declarations = module.sdkLibraryProperties.Aconfig_declarations
Sundong Ahn054b19a2018-10-19 13:46:09 +09001918
Paul Duffin6d0886e2020-04-07 18:49:53 +01001919 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001920 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001921 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001922 }
1923 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001924 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001925 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1926 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001927 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00001928 disabledWarnings := []string{"HiddenSuperclass"}
1929 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
1930 disabledWarnings = append(disabledWarnings,
1931 "BroadcastBehavior",
1932 "DeprecationMismatch",
1933 "MissingPermission",
1934 "SdkConstant",
1935 "Todo",
1936 )
Paul Duffin235ffff2019-12-24 10:41:30 +00001937 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001938 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001939
Paul Duffin6877e6d2020-09-25 19:59:14 +01001940 // Output Javadoc comments for public scope.
1941 if apiScope == apiScopePublic {
1942 props.Output_javadoc_comments = proptools.BoolPtr(true)
1943 }
1944
Paul Duffin1fb487d2020-04-07 18:50:10 +01001945 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001946 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001947 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001948 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001949
Paul Duffin15f34ef2020-07-20 18:04:44 +01001950 // List of APIs identified from the provided source files are created. They are later
1951 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1952 // last-released (a.k.a numbered) list of API.
1953 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1954 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1955 apiDir := module.getApiDir()
1956 currentApiFileName = path.Join(apiDir, currentApiFileName)
1957 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001958
Paul Duffin15f34ef2020-07-20 18:04:44 +01001959 // check against the not-yet-release API
1960 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1961 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001962
Paul Duffin958806b2022-05-16 13:10:47 +00001963 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001964 // check against the latest released API
1965 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001966 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001967 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1968 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1969 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001970 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1971 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001972
Paul Duffin15f34ef2020-07-20 18:04:44 +01001973 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1974 // Enable api lint.
1975 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1976 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001977
Paul Duffin15f34ef2020-07-20 18:04:44 +01001978 // If it exists then pass a lint-baseline.txt through to droidstubs.
1979 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1980 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1981 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1982 if err != nil {
1983 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1984 }
1985 if len(paths) == 1 {
1986 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1987 } else if len(paths) != 0 {
1988 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001989 }
1990 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001991 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001992
Paul Duffin15f34ef2020-07-20 18:04:44 +01001993 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001994 // Dist the api txt and removed api txt artifacts for sdk builds.
1995 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1996 for _, p := range []struct {
1997 tag string
1998 pattern string
1999 }{
Jihoon Kangd1799f62024-02-20 23:01:38 +00002000 // "exportable" api files are copied to the dist directory instead of the
2001 // "everything" api files.
2002 {tag: ".exportable.api.txt", pattern: "%s.txt"},
2003 {tag: ".exportable.removed-api.txt", pattern: "%s-removed.txt"},
Paul Duffin040e9062020-11-23 17:41:36 +00002004 } {
2005 props.Dists = append(props.Dists, android.Dist{
2006 Targets: []string{"sdk", "win_sdk"},
2007 Dir: distDir,
2008 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
2009 Tag: proptools.StringPtr(p.tag),
2010 })
2011 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00002012 }
2013
Spandan Das2cc80ba2023-10-27 17:21:52 +00002014 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002015}
2016
Jihoon Kang0c705a42023-08-02 06:44:57 +00002017func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002018 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00002019 Name *string
2020 Visibility []string
2021 Api_contributions []string
2022 Libs []string
2023 Static_libs []string
2024 Full_api_surface_stub *string
Jihoon Kang4ec24872023-10-05 17:26:09 +00002025 System_modules *string
Jihoon Kang063ec002023-06-28 01:16:23 +00002026 Enable_validation *bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002027 }{}
2028
2029 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00002030 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002031
2032 apiContributions := []string{}
2033
2034 // Api surfaces are not independent of each other, but have subset relationships,
2035 // and so does the api files. To generate from-text stubs for api surfaces other than public,
2036 // all subset api domains' api_contriubtions must be added as well.
2037 scope := apiScope
2038 for scope != nil {
2039 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
2040 scope = scope.extends
2041 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00002042 if apiScope == apiScopePublic {
2043 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
2044 if additionalApiContribution != "" {
2045 apiContributions = append(apiContributions, additionalApiContribution)
2046 }
2047 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002048
2049 props.Api_contributions = apiContributions
2050 props.Libs = module.properties.Libs
2051 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00002052 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002053 props.Libs = append(props.Libs, "stub-annotations")
2054 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kange7ee2562023-07-25 05:51:46 +00002055 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
Jihoon Kang0c705a42023-08-02 06:44:57 +00002056 if alternativeFullApiSurfaceStub != "" {
2057 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
2058 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002059
2060 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
2061 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
2062 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00002063 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002064 }
2065
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00002066 // java_sdk_library modules that set sdk_version as none does not depend on other api
2067 // domains. Therefore, java_api_library created from such modules should not depend on
2068 // full_api_surface_stubs but create and compile stubs by the java_api_library module
2069 // itself.
2070 if module.SdkVersion(mctx).Kind == android.SdkNone {
2071 props.Full_api_surface_stub = nil
2072 }
2073
Jihoon Kang4ec24872023-10-05 17:26:09 +00002074 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00002075 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang4ec24872023-10-05 17:26:09 +00002076
Spandan Das2cc80ba2023-10-27 17:21:52 +00002077 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002078}
2079
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002080func (module *SdkLibrary) topLevelStubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
2081 props := libraryProperties{}
2082
Jihoon Kang1147b312023-06-08 23:25:57 +00002083 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
2084 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
2085 props.Sdk_version = proptools.StringPtr(sdkVersion)
2086
Jihoon Kang1147b312023-06-08 23:25:57 +00002087 props.System_modules = module.deviceProperties.System_modules
2088
Jihoon Kang1147b312023-06-08 23:25:57 +00002089 // The imports need to be compiled to dex if the java_sdk_library requests it.
2090 compileDex := module.dexProperties.Compile_dex
2091 if module.stubLibrariesCompiledForDex() {
2092 compileDex = proptools.BoolPtr(true)
2093 }
2094 props.Compile_dex = compileDex
2095
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002096 return props
2097}
2098
2099func (module *SdkLibrary) createTopLevelStubsLibrary(
2100 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
2101
2102 props := module.topLevelStubsLibraryProps(mctx, apiScope)
2103 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
2104
2105 // Add the stub compiling java_library/java_api_library as static lib based on build config
2106 staticLib := module.sourceStubsLibraryModuleName(apiScope)
2107 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
2108 staticLib = module.apiLibraryModuleName(apiScope)
2109 }
2110 props.Static_libs = append(props.Static_libs, staticLib)
2111
2112 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2113}
2114
2115func (module *SdkLibrary) createTopLevelExportableStubsLibrary(
2116 mctx android.DefaultableHookContext, apiScope *apiScope) {
2117
2118 props := module.topLevelStubsLibraryProps(mctx, apiScope)
2119 props.Name = proptools.StringPtr(module.exportableStubsLibraryModuleName(apiScope))
2120
2121 // Dist the class jar artifact for sdk builds.
2122 // "exportable" stubs are copied to dist for sdk builds instead of the "everything" stubs.
2123 if !Bool(module.sdkLibraryProperties.No_dist) {
2124 props.Dist.Targets = []string{"sdk", "win_sdk"}
2125 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
2126 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
2127 props.Dist.Tag = proptools.StringPtr(".jar")
2128 }
2129
2130 staticLib := module.exportableSourceStubsLibraryModuleName(apiScope)
2131 props.Static_libs = append(props.Static_libs, staticLib)
2132
Jihoon Kang1147b312023-06-08 23:25:57 +00002133 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2134}
2135
Paul Duffin958806b2022-05-16 13:10:47 +00002136func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
2137 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
2138}
2139
Paul Duffinea8f8082021-06-24 13:25:57 +01002140// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09002141func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2142 depTag := mctx.OtherModuleDependencyTag(dep)
2143 if depTag == xmlPermissionsFileTag {
2144 return true
2145 }
2146 return module.Library.DepIsInSameApex(mctx, dep)
2147}
2148
Paul Duffinea8f8082021-06-24 13:25:57 +01002149// Implements android.ApexModule
2150func (module *SdkLibrary) UniqueApexVariations() bool {
2151 return module.uniqueApexVariations()
2152}
2153
Jihoon Kang80456fd2023-11-15 19:22:14 +00002154func (module *SdkLibrary) ContributeToApi() bool {
2155 return proptools.BoolDefault(module.sdkLibraryProperties.Contribute_to_android_api, false)
2156}
2157
Jiyong Parkc678ad32018-04-10 13:07:10 +09002158// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01002159func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002160 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002161 var moduleMinApiLevelStr = moduleMinApiLevel.String()
2162 if moduleMinApiLevel == android.NoneApiLevel {
2163 moduleMinApiLevelStr = "current"
2164 }
Jiyong Parke3833882020-02-17 17:28:10 +09002165 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00002166 Name *string
2167 Lib_name *string
2168 Apex_available []string
2169 On_bootclasspath_since *string
2170 On_bootclasspath_before *string
2171 Min_device_sdk *string
2172 Max_device_sdk *string
2173 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002174 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002175 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00002176 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
2177 Lib_name: proptools.StringPtr(module.BaseModuleName()),
2178 Apex_available: module.ApexProperties.Apex_available,
2179 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
2180 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
2181 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
2182 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
2183 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002184 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002185 }
Jiyong Parke3833882020-02-17 17:28:10 +09002186
Jiyong Parke3833882020-02-17 17:28:10 +09002187 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002188}
2189
Jiyong Parkf1691d22021-03-29 20:11:58 +09002190func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002191 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002192 var kind android.SdkKind
2193 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002194 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002195 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002196 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002197 // We don't have prebuilt SDK for the specific sdkVersion.
2198 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002199 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002200 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002201 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002202
2203 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002204 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002205 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002206 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002207 if ctx.Config().AllowMissingDependencies() {
2208 return android.Paths{android.PathForSource(ctx, jar)}
2209 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002210 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002211 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002212 return nil
2213 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002214 return android.Paths{jarPath.Path()}
2215}
2216
Colin Crossaede88c2020-08-11 12:17:01 -07002217// 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 +01002218//
2219// If either this or the other module are on the platform then this will return
2220// false.
Colin Cross56a83212020-09-15 18:30:11 -07002221func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
Colin Crossff694a82023-12-13 15:54:49 -08002222 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross313aa542023-12-13 13:47:44 -08002223 otherApexInfo, _ := android.OtherModuleProvider(ctx, other, android.ApexInfoProvider)
Jiyong Parkab50b072021-05-12 17:13:56 +09002224 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002225}
2226
Jiyong Parkf1691d22021-03-29 20:11:58 +09002227func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002228 // If the client doesn't set sdk_version, but if this library prefers stubs over
2229 // the impl library, let's provide the widest API surface possible. To do so,
2230 // force override sdk_version to module_current so that the closest possible API
2231 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002232 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002233 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002234 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002235
Paul Duffindaaa3322020-05-26 18:13:57 +01002236 // Only provide access to the implementation library if it is actually built.
2237 if module.requiresRuntimeImplementationLibrary() {
2238 // Check any special cases for java_sdk_library.
2239 //
2240 // Only allow access to the implementation library in the following condition:
2241 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002242 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002243 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01002244 if headerJars {
2245 return module.HeaderJars()
2246 } else {
2247 return module.ImplementationJars()
2248 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002249 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002250 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002251
Paul Duffin23970f42020-05-20 14:20:02 +01002252 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002253}
2254
Sundong Ahn241cd372018-07-13 16:16:44 +09002255// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002256func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002257 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
2258}
2259
2260// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002261func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002262 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09002263}
2264
Colin Cross571cccf2019-02-04 11:22:08 -08002265var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2266
Jiyong Park82484c02018-04-23 21:41:26 +09002267func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002268 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002269 return &[]string{}
2270 }).(*[]string)
2271}
2272
Paul Duffin749f98f2019-12-30 17:23:46 +00002273func (module *SdkLibrary) getApiDir() string {
2274 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2275}
2276
Jiyong Parkc678ad32018-04-10 13:07:10 +09002277// For a java_sdk_library module, create internal modules for stubs, docs,
2278// runtime libs and xml file. If requested, the stubs and docs are created twice
2279// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002280func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2281 // If the module has been disabled then don't create any child modules.
2282 if !module.Enabled() {
2283 return
2284 }
2285
Paul Duffina18abc22020-05-16 18:54:24 +01002286 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002287 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002288 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002289 }
2290
Paul Duffin37e0b772019-12-30 17:20:10 +00002291 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002292 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002293 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002294 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002295 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002296
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002297 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002298
Paul Duffin3375e352020-04-28 10:44:03 +01002299 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002300
Paul Duffin749f98f2019-12-30 17:23:46 +00002301 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002302 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002303 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002304 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002305 p := android.ExistentPathForSource(mctx, path)
2306 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002307 if mctx.Config().AllowMissingDependencies() {
2308 mctx.AddMissingDependencies([]string{path})
2309 } else {
2310 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2311 missingCurrentApi = true
2312 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002313 }
2314 }
2315 }
2316
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002317 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002318 script := "build/soong/scripts/gen-java-current-api-files.sh"
2319 p := android.ExistentPathForSource(mctx, script)
2320
2321 if !p.Valid() {
2322 panic(fmt.Sprintf("script file %s doesn't exist", script))
2323 }
2324
2325 mctx.ModuleErrorf("One or more current api files are missing. "+
2326 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002327 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002328 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002329 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002330 return
2331 }
2332
Paul Duffin3375e352020-04-28 10:44:03 +01002333 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002334 // Use the stubs source name for legacy reasons.
2335 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002336
Paul Duffind1b3a922020-01-22 11:57:20 +00002337 module.createStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002338 module.createExportableStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002339
Jihoon Kang0c705a42023-08-02 06:44:57 +00002340 alternativeFullApiSurfaceStubLib := ""
2341 if scope == apiScopePublic {
2342 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
2343 }
2344 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
Jihoon Kang1147b312023-06-08 23:25:57 +00002345 if contributesToApiSurface {
Jihoon Kang0c705a42023-08-02 06:44:57 +00002346 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002347 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002348
2349 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002350 module.createTopLevelExportableStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09002351 }
2352
Paul Duffindfa131e2020-05-15 20:37:11 +01002353 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002354 // Create child module to create an implementation library.
2355 //
2356 // This temporarily creates a second implementation library that can be explicitly
2357 // referenced.
2358 //
2359 // TODO(b/156618935) - update comment once only one implementation library is created.
2360 module.createImplLibrary(mctx)
2361
Paul Duffindfa131e2020-05-15 20:37:11 +01002362 // Only create an XML permissions file that declares the library as being usable
2363 // as a shared library if required.
2364 if module.sharedLibrary() {
2365 module.createXmlFile(mctx)
2366 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002367
2368 // record java_sdk_library modules so that they are exported to make
2369 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2370 javaSdkLibrariesLock.Lock()
2371 defer javaSdkLibrariesLock.Unlock()
2372 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2373 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002374
Paul Duffin77590a82022-04-28 14:13:30 +00002375 // 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 +01002376 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002377 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002378}
2379
2380func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002381 module.addHostAndDeviceProperties()
2382 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002383
Paul Duffin71b33cc2021-06-23 11:39:47 +01002384 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002385
Paul Duffina18abc22020-05-16 18:54:24 +01002386 module.properties.Installable = proptools.BoolPtr(true)
2387 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002388}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002389
Paul Duffindfa131e2020-05-15 20:37:11 +01002390func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2391 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2392}
2393
Jiyong Park932cdfe2020-05-28 00:19:53 +09002394func (module *SdkLibrary) defaultsToStubs() bool {
2395 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2396}
2397
Paul Duffin1b1e8062020-05-08 13:44:43 +01002398// Defines how to name the individual component modules the sdk library creates.
2399type sdkLibraryComponentNamingScheme interface {
2400 stubsLibraryModuleName(scope *apiScope, baseName string) string
2401
2402 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002403
2404 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002405
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002406 sourceStubsLibraryModuleName(scope *apiScope, baseName string) string
2407
2408 exportableStubsLibraryModuleName(scope *apiScope, baseName string) string
2409
2410 exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002411}
2412
2413type defaultNamingScheme struct {
2414}
2415
2416func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2417 return scope.stubsLibraryModuleName(baseName)
2418}
2419
2420func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2421 return scope.stubsSourceModuleName(baseName)
2422}
2423
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002424func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2425 return scope.apiLibraryModuleName(baseName)
2426}
2427
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002428func (s *defaultNamingScheme) sourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
Jihoon Kang1147b312023-06-08 23:25:57 +00002429 return scope.sourceStubLibraryModuleName(baseName)
2430}
2431
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002432func (s *defaultNamingScheme) exportableStubsLibraryModuleName(scope *apiScope, baseName string) string {
2433 return scope.exportableStubsLibraryModuleName(baseName)
2434}
2435
2436func (s *defaultNamingScheme) exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
2437 return scope.exportableSourceStubsLibraryModuleName(baseName)
2438}
2439
Paul Duffin1b1e8062020-05-08 13:44:43 +01002440var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2441
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002442func hasStubsLibrarySuffix(name string, apiScope *apiScope) bool {
2443 return strings.HasSuffix(name, apiScope.stubsLibraryModuleNameSuffix()) ||
2444 strings.HasSuffix(name, apiScope.exportableStubsLibraryModuleNameSuffix())
2445}
2446
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002447func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002448 name = strings.TrimSuffix(name, ".from-source")
2449
Anton Hansson2d0c1942020-05-25 12:20:51 +01002450 // This suffix-based approach is fragile and could potentially mis-trigger.
2451 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002452 if hasStubsLibrarySuffix(name, apiScopePublic) {
Anton Hansson08f476b2021-04-07 15:32:19 +01002453 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2454 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2455 return false, javaPlatform
2456 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002457 return true, javaSdk
2458 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002459 if hasStubsLibrarySuffix(name, apiScopeSystem) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002460 return true, javaSystem
2461 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002462 if hasStubsLibrarySuffix(name, apiScopeModuleLib) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002463 return true, javaModule
2464 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002465 if hasStubsLibrarySuffix(name, apiScopeTest) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002466 return true, javaSystem
2467 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002468 if hasStubsLibrarySuffix(name, apiScopeSystemServer) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002469 return true, javaSystemServer
2470 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002471 return false, javaPlatform
2472}
2473
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002474// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2475// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2476// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2477// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2478// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002479func SdkLibraryFactory() android.Module {
2480 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002481
2482 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002483 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002484
Inseob Kimc0907f12019-02-08 21:00:45 +09002485 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002486 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002487 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002488
2489 // Initialize the map from scope to scope specific properties.
2490 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2491 for _, scope := range allApiScopes {
2492 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2493 }
2494 module.scopeToProperties = scopeToProperties
2495
Paul Duffin4911a892020-04-29 23:35:13 +01002496 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002497 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002498 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2499 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2500
Paul Duffin1b1e8062020-05-08 13:44:43 +01002501 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002502 // If no implementation is required then it cannot be used as a shared library
2503 // either.
2504 if !module.requiresRuntimeImplementationLibrary() {
2505 // If shared_library has been explicitly set to true then it is incompatible
2506 // with api_only: true.
2507 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2508 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2509 }
2510 // Set shared_library: false.
2511 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2512 }
2513
Paul Duffin1b1e8062020-05-08 13:44:43 +01002514 if module.initCommonAfterDefaultsApplied(ctx) {
2515 module.CreateInternalModules(ctx)
2516 }
2517 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002518 return module
2519}
Colin Cross79c7c262019-04-17 11:11:46 -07002520
2521//
2522// SDK library prebuilts
2523//
2524
Paul Duffin56d44902020-01-31 13:36:25 +00002525// Properties associated with each api scope.
2526type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002527 Jars []string `android:"path"`
2528
2529 Sdk_version *string
2530
Colin Cross79c7c262019-04-17 11:11:46 -07002531 // List of shared java libs that this module has dependencies to
2532 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002533
Paul Duffinc8782502020-04-29 20:45:27 +01002534 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002535 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002536
2537 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002538 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002539
2540 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002541 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002542
2543 // Annotation zip
2544 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002545}
2546
Paul Duffin56d44902020-01-31 13:36:25 +00002547type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002548 // List of shared java libs, common to all scopes, that this module has
2549 // dependencies to
2550 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002551
2552 // If set to true, compile dex files for the stubs. Defaults to false.
2553 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002554
2555 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002556 Permitted_packages []string
Spandan Das23956d12024-01-19 00:22:22 +00002557
2558 // Name of the source soong module that gets shadowed by this prebuilt
2559 // If unspecified, follows the naming convention that the source module of
2560 // the prebuilt is Name() without "prebuilt_" prefix
2561 Source_module_name *string
Paul Duffin56d44902020-01-31 13:36:25 +00002562}
2563
Paul Duffineedc5d52020-06-12 17:46:39 +01002564type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002565 android.ModuleBase
2566 android.DefaultableModuleBase
2567 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002568 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002569
Paul Duffin37856732021-02-26 14:24:15 +00002570 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002571 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002572
Colin Cross79c7c262019-04-17 11:11:46 -07002573 properties sdkLibraryImportProperties
2574
Paul Duffin46a26a82020-04-07 19:27:04 +01002575 // Map from api scope to the scope specific property structure.
2576 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2577
Paul Duffin56d44902020-01-31 13:36:25 +00002578 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002579
2580 // The reference to the implementation library created by the source module.
2581 // Is nil if the source module does not exist.
2582 implLibraryModule *Library
2583
2584 // The reference to the xml permissions module created by the source module.
2585 // Is nil if the source module does not exist.
2586 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002587
Jeongik Chad5fe8782021-07-08 01:13:11 +09002588 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00002589 dexJarFile OptionalDexJarPath
2590 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09002591
2592 // Expected install file path of the source module(sdk_library)
2593 // or dex implementation jar obtained from the prebuilt_apex, if any.
2594 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002595}
2596
Paul Duffineedc5d52020-06-12 17:46:39 +01002597var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002598
Paul Duffin46a26a82020-04-07 19:27:04 +01002599// The type of a structure that contains a field of type sdkLibraryScopeProperties
2600// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002601//
2602// struct {
2603// Public sdkLibraryScopeProperties
2604// System sdkLibraryScopeProperties
2605// ...
2606// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002607var allScopeStructType = createAllScopePropertiesStructType()
2608
2609// Dynamically create a structure type for each apiscope in allApiScopes.
2610func createAllScopePropertiesStructType() reflect.Type {
2611 var fields []reflect.StructField
2612 for _, apiScope := range allApiScopes {
2613 field := reflect.StructField{
2614 Name: apiScope.fieldName,
2615 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2616 }
2617 fields = append(fields, field)
2618 }
2619
2620 return reflect.StructOf(fields)
2621}
2622
2623// Create an instance of the scope specific structure type and return a map
2624// from apiscope to a pointer to each scope specific field.
2625func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2626 allScopePropertiesPtr := reflect.New(allScopeStructType)
2627 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2628 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2629
2630 for _, apiScope := range allApiScopes {
2631 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2632 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2633 }
2634
2635 return allScopePropertiesPtr.Interface(), scopeProperties
2636}
2637
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002638// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002639func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002640 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002641
Paul Duffin46a26a82020-04-07 19:27:04 +01002642 allScopeProperties, scopeToProperties := createPropertiesInstance()
2643 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002644 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002645
Paul Duffinc3091c82020-05-08 14:16:20 +01002646 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002647 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002648
Paul Duffin0bdcb272020-02-06 15:24:57 +00002649 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002650 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002651 InitJavaModule(module, android.HostAndDeviceSupported)
2652
Paul Duffin1b1e8062020-05-08 13:44:43 +01002653 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2654 if module.initCommonAfterDefaultsApplied(mctx) {
2655 module.createInternalModules(mctx)
2656 }
2657 })
Colin Cross79c7c262019-04-17 11:11:46 -07002658 return module
2659}
2660
Paul Duffin630b11e2021-07-15 13:35:26 +01002661var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2662
2663func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2664 return module.properties.Permitted_packages
2665}
2666
Paul Duffineedc5d52020-06-12 17:46:39 +01002667func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002668 return &module.prebuilt
2669}
2670
Paul Duffineedc5d52020-06-12 17:46:39 +01002671func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002672 return module.prebuilt.Name(module.ModuleBase.Name())
2673}
2674
Spandan Das23956d12024-01-19 00:22:22 +00002675func (module *SdkLibraryImport) BaseModuleName() string {
2676 return proptools.StringDefault(module.properties.Source_module_name, module.ModuleBase.Name())
2677}
2678
Paul Duffineedc5d52020-06-12 17:46:39 +01002679func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002680
Paul Duffin50061512020-01-21 16:31:05 +00002681 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002682 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002683 module.prebuilt.ForcePrefer()
2684 }
2685
Paul Duffin46a26a82020-04-07 19:27:04 +01002686 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002687 if len(scopeProperties.Jars) == 0 {
2688 continue
2689 }
2690
Paul Duffinbbb546b2020-04-09 00:07:11 +01002691 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002692
Paul Duffin0f8faff2020-05-20 16:18:00 +01002693 if len(scopeProperties.Stub_srcs) > 0 {
2694 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2695 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002696
2697 if scopeProperties.Current_api != nil {
2698 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2699 }
Paul Duffin56d44902020-01-31 13:36:25 +00002700 }
Colin Cross79c7c262019-04-17 11:11:46 -07002701
2702 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2703 javaSdkLibrariesLock.Lock()
2704 defer javaSdkLibrariesLock.Unlock()
2705 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2706}
2707
Paul Duffineedc5d52020-06-12 17:46:39 +01002708func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002709 // Creates a java import for the jar with ".stubs" suffix
2710 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002711 Name *string
2712 Source_module_name *string
2713 Created_by_java_sdk_library_name *string
2714 Sdk_version *string
2715 Libs []string
2716 Jars []string
2717 Compile_dex *bool
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002718 Is_stubs_module *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002719
2720 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002721 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002722 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002723 props.Source_module_name = proptools.StringPtr(apiScope.stubsLibraryModuleName(module.BaseModuleName()))
2724 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002725 props.Sdk_version = scopeProperties.Sdk_version
2726 // Prepend any of the libs from the legacy public properties to the libs for each of the
2727 // scopes to avoid having to duplicate them in each scope.
2728 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2729 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002730
Paul Duffin38b57852020-05-13 16:08:09 +01002731 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002732 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002733
Paul Duffin1267d872021-04-16 17:21:36 +01002734 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002735 compileDex := module.properties.Compile_dex
2736 if module.stubLibrariesCompiledForDex() {
2737 compileDex = proptools.BoolPtr(true)
2738 }
2739 props.Compile_dex = compileDex
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002740 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffin1267d872021-04-16 17:21:36 +01002741
Paul Duffin859fe962020-05-15 10:20:31 +01002742 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002743}
2744
Paul Duffineedc5d52020-06-12 17:46:39 +01002745func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002746 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002747 Name *string
2748 Source_module_name *string
2749 Created_by_java_sdk_library_name *string
2750 Srcs []string
Paul Duffinbf4de042022-09-27 12:41:52 +01002751
2752 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002753 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002754 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002755 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()))
2756 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002757 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002758
2759 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002760 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2761
Spandan Das2cc80ba2023-10-27 17:21:52 +00002762 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002763}
2764
Jihoon Kang71c86832023-09-13 01:01:53 +00002765func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2766 api_file := scopeProperties.Current_api
2767 api_surface := &apiScope.name
2768
2769 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002770 Name *string
2771 Source_module_name *string
2772 Created_by_java_sdk_library_name *string
2773 Api_surface *string
2774 Api_file *string
2775 Visibility []string
Jihoon Kang71c86832023-09-13 01:01:53 +00002776 }{}
2777
2778 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
Spandan Das23956d12024-01-19 00:22:22 +00002779 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()) + ".api.contribution")
2780 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Jihoon Kang71c86832023-09-13 01:01:53 +00002781 props.Api_surface = api_surface
2782 props.Api_file = api_file
2783 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2784
Spandan Das2cc80ba2023-10-27 17:21:52 +00002785 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002786}
2787
Paul Duffin44f1d842020-06-26 20:17:02 +01002788// Add the dependencies on the child module in the component deps mutator so that it
2789// creates references to the prebuilt and not the source modules.
2790func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002791 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002792 if len(scopeProperties.Jars) == 0 {
2793 continue
2794 }
2795
2796 // Add dependencies to the prebuilt stubs library
Jihoon Kangb7431552024-01-22 19:40:08 +00002797 ctx.AddVariationDependencies(nil, apiScope.prebuiltStubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002798
2799 if len(scopeProperties.Stub_srcs) > 0 {
2800 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002801 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002802 }
Paul Duffin56d44902020-01-31 13:36:25 +00002803 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002804}
2805
2806// Add other dependencies as normal.
2807func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002808
2809 implName := module.implLibraryModuleName()
2810 if ctx.OtherModuleExists(implName) {
2811 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2812
2813 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2814 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2815 // Add dependency to the rule for generating the xml permissions file
2816 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2817 }
2818 }
Colin Cross79c7c262019-04-17 11:11:46 -07002819}
2820
Jiyong Park45bf82e2020-12-15 22:29:02 +09002821var _ android.ApexModule = (*SdkLibraryImport)(nil)
2822
2823// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002824func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2825 depTag := mctx.OtherModuleDependencyTag(dep)
2826 if depTag == xmlPermissionsFileTag {
2827 return true
2828 }
2829
2830 // None of the other dependencies of the java_sdk_library_import are in the same apex
2831 // as the one that references this module.
2832 return false
2833}
2834
Jiyong Park45bf82e2020-12-15 22:29:02 +09002835// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002836func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2837 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002838 // we don't check prebuilt modules for sdk_version
2839 return nil
2840}
2841
Paul Duffinea8f8082021-06-24 13:25:57 +01002842// Implements android.ApexModule
2843func (module *SdkLibraryImport) UniqueApexVariations() bool {
2844 return module.uniqueApexVariations()
2845}
2846
Paul Duffin09817d62022-04-28 17:45:11 +01002847// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002848func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2849 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002850}
2851
2852var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2853
Paul Duffineedc5d52020-06-12 17:46:39 +01002854func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002855 paths, err := module.commonOutputFiles(tag)
2856 if paths != nil || err != nil {
2857 return paths, err
2858 }
2859 if module.implLibraryModule != nil {
2860 return module.implLibraryModule.OutputFiles(tag)
2861 } else {
2862 return nil, nil
2863 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002864}
2865
Paul Duffineedc5d52020-06-12 17:46:39 +01002866func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002867 module.generateCommonBuildActions(ctx)
2868
Jeongik Chad5fe8782021-07-08 01:13:11 +09002869 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2870 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2871
Paul Duffin0f8faff2020-05-20 16:18:00 +01002872 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002873 ctx.VisitDirectDeps(func(to android.Module) {
2874 tag := ctx.OtherModuleDependencyTag(to)
2875
Paul Duffin0f8faff2020-05-20 16:18:00 +01002876 // Extract information from any of the scope specific dependencies.
2877 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2878 apiScope := scopeTag.apiScope
2879 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2880
2881 // Extract information from the dependency. The exact information extracted
2882 // is determined by the nature of the dependency which is determined by the tag.
2883 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002884 } else if tag == implLibraryTag {
2885 if implLibrary, ok := to.(*Library); ok {
2886 module.implLibraryModule = implLibrary
2887 } else {
2888 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2889 }
2890 } else if tag == xmlPermissionsFileTag {
2891 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2892 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2893 } else {
2894 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2895 }
Colin Cross79c7c262019-04-17 11:11:46 -07002896 }
2897 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002898
2899 // Populate the scope paths with information from the properties.
2900 for apiScope, scopeProperties := range module.scopeProperties {
2901 if len(scopeProperties.Jars) == 0 {
2902 continue
2903 }
2904
2905 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002906 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002907 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2908 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2909 }
Paul Duffin39853512021-02-26 11:09:39 +00002910
2911 if ctx.Device() {
2912 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2913 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08002914 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00002915 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002916 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00002917 di, err := android.FindDeapexerProviderForModule(ctx)
2918 if err != nil {
2919 // An error was found, possibly due to multiple apexes in the tree that export this library
2920 // Defer the error till a client tries to call DexJarBuildPath
2921 module.dexJarFileErr = err
Spandan Das3a392012024-01-17 18:26:27 +00002922 module.initHiddenAPIError(err)
Spandan Dasfae468e2023-12-12 23:23:53 +00002923 return
Martin Stjernholm44825602021-09-17 01:44:12 +01002924 }
Spandan Das5be63332023-12-13 00:06:32 +00002925 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(module.BaseModuleName())
Jiakai Zhang81e46812023-02-08 21:56:07 +08002926 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002927 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2928 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002929 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002930 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002931 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002932 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002933
Spandan Dase21a8d42024-01-23 23:56:29 +00002934 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), installPath)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002935 module.dexpreopter.isSDKLibrary = true
Spandan Dase21a8d42024-01-23 23:56:29 +00002936 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002937
2938 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2939 module.dexpreopter.inputProfilePathOnHost = profilePath
2940 }
Paul Duffin39853512021-02-26 11:09:39 +00002941 } else {
2942 // This should never happen as a variant for a prebuilt_apex is only created if the
2943 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002944 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002945 }
2946 }
2947 }
Colin Cross79c7c262019-04-17 11:11:46 -07002948}
2949
Jiyong Parkf1691d22021-03-29 20:11:58 +09002950func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002951
2952 // For consistency with SdkLibrary make the implementation jar available to libraries that
2953 // are within the same APEX.
2954 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002955 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002956 if headerJars {
2957 return implLibraryModule.HeaderJars()
2958 } else {
2959 return implLibraryModule.ImplementationJars()
2960 }
2961 }
2962
Paul Duffin23970f42020-05-20 14:20:02 +01002963 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002964}
2965
Colin Cross79c7c262019-04-17 11:11:46 -07002966// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002967func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002968 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002969 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002970}
2971
2972// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002973func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002974 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002975 return module.sdkJars(ctx, sdkVersion, false)
2976}
2977
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002978// to satisfy UsesLibraryDependency interface
Spandan Das59a4a2b2024-01-09 21:35:56 +00002979func (module *SdkLibraryImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002980 // The dex implementation jar extracted from the .apex file should be used in preference to the
2981 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00002982 if module.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002983 ctx.ModuleErrorf(module.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00002984 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002985 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002986 return module.dexJarFile
2987 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002988 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002989 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002990 } else {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002991 return module.implLibraryModule.DexJarBuildPath(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +01002992 }
2993}
2994
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002995// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002996func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002997 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002998}
2999
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00003000// to satisfy UsesLibraryDependency interface
3001func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
3002 return nil
3003}
3004
Paul Duffineedc5d52020-06-12 17:46:39 +01003005// to satisfy apex.javaDependency interface
3006func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
3007 if module.implLibraryModule == nil {
3008 return nil
3009 } else {
3010 return module.implLibraryModule.JacocoReportClassesFile()
3011 }
3012}
3013
3014// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07003015func (module *SdkLibraryImport) LintDepSets() LintDepSets {
3016 if module.implLibraryModule == nil {
3017 return LintDepSets{}
3018 } else {
3019 return module.implLibraryModule.LintDepSets()
3020 }
3021}
3022
Spandan Das17854f52022-01-14 21:19:14 +00003023func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003024 if module.implLibraryModule == nil {
3025 return false
3026 } else {
Spandan Das17854f52022-01-14 21:19:14 +00003027 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003028 }
3029}
3030
Spandan Das17854f52022-01-14 21:19:14 +00003031func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003032 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00003033 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003034 }
3035}
3036
Colin Cross08dca382020-07-21 20:31:17 -07003037// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01003038func (module *SdkLibraryImport) Stem() string {
3039 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07003040}
Jiyong Parke3833882020-02-17 17:28:10 +09003041
Paul Duffin44b481b2020-06-17 16:59:43 +01003042var _ ApexDependency = (*SdkLibraryImport)(nil)
3043
3044// to satisfy java.ApexDependency interface
3045func (module *SdkLibraryImport) HeaderJars() android.Paths {
3046 if module.implLibraryModule == nil {
3047 return nil
3048 } else {
3049 return module.implLibraryModule.HeaderJars()
3050 }
3051}
3052
3053// to satisfy java.ApexDependency interface
3054func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
3055 if module.implLibraryModule == nil {
3056 return nil
3057 } else {
3058 return module.implLibraryModule.ImplementationAndResourcesJars()
3059 }
3060}
3061
Jiakai Zhang204356f2021-09-09 08:12:46 +00003062// to satisfy java.DexpreopterInterface interface
3063func (module *SdkLibraryImport) IsInstallable() bool {
3064 return true
3065}
3066
Paul Duffinfef55002021-06-17 14:56:05 +01003067var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
3068
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01003069func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01003070 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08003071 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01003072}
3073
Spandan Das2ea84dd2024-01-25 22:12:50 +00003074func (j *SdkLibraryImport) UseProfileGuidedDexpreopt() bool {
3075 return proptools.Bool(j.importDexpreoptProperties.Dex_preopt.Profile_guided)
3076}
3077
Jiyong Parke3833882020-02-17 17:28:10 +09003078// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09003079type sdkLibraryXml struct {
3080 android.ModuleBase
3081 android.DefaultableModuleBase
3082 android.ApexModuleBase
3083
3084 properties sdkLibraryXmlProperties
3085
3086 outputFilePath android.OutputPath
3087 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07003088
3089 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09003090}
3091
3092type sdkLibraryXmlProperties struct {
3093 // canonical name of the lib
3094 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003095
3096 // Signals that this shared library is part of the bootclasspath starting
3097 // on the version indicated in this attribute.
3098 //
3099 // This will make platforms at this level and above to ignore
3100 // <uses-library> tags with this library name because the library is already
3101 // available
3102 On_bootclasspath_since *string
3103
3104 // Signals that this shared library was part of the bootclasspath before
3105 // (but not including) the version indicated in this attribute.
3106 //
3107 // The system will automatically add a <uses-library> tag with this library to
3108 // apps that target any SDK less than the version indicated in this attribute.
3109 On_bootclasspath_before *string
3110
3111 // Indicates that PackageManager should ignore this shared library if the
3112 // platform is below the version indicated in this attribute.
3113 //
3114 // This means that the device won't recognise this library as installed.
3115 Min_device_sdk *string
3116
3117 // Indicates that PackageManager should ignore this shared library if the
3118 // platform is above the version indicated in this attribute.
3119 //
3120 // This means that the device won't recognise this library as installed.
3121 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00003122
3123 // The SdkLibrary's min api level as a string
3124 //
3125 // This value comes from the ApiLevel of the MinSdkVersion property.
3126 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00003127
3128 // Uses-libs dependencies that the shared library requires to work correctly.
3129 //
3130 // This will add dependency="foo:bar" to the <library> section.
3131 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09003132}
3133
3134// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
3135// Not to be used directly by users. java_sdk_library internally uses this.
3136func sdkLibraryXmlFactory() android.Module {
3137 module := &sdkLibraryXml{}
3138
3139 module.AddProperties(&module.properties)
3140
3141 android.InitApexModule(module)
3142 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
3143
3144 return module
3145}
3146
Colin Crossaede88c2020-08-11 12:17:01 -07003147func (module *sdkLibraryXml) UniqueApexVariations() bool {
3148 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
3149 // mounted APEX, which contains the name of the APEX.
3150 return true
3151}
3152
Jiyong Parke3833882020-02-17 17:28:10 +09003153// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09003154func (module *sdkLibraryXml) BaseDir() string {
3155 return "etc"
3156}
3157
3158// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09003159func (module *sdkLibraryXml) SubDir() string {
3160 return "permissions"
3161}
3162
3163// from android.PrebuiltEtcModule
3164func (module *sdkLibraryXml) OutputFile() android.OutputPath {
3165 return module.outputFilePath
3166}
3167
3168// from android.ApexModule
3169func (module *sdkLibraryXml) AvailableFor(what string) bool {
3170 return true
3171}
3172
3173func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
3174 // do nothing
3175}
3176
Jiyong Park45bf82e2020-12-15 22:29:02 +09003177var _ android.ApexModule = (*sdkLibraryXml)(nil)
3178
3179// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003180func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3181 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003182 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
3183 return nil
3184}
3185
Jiyong Parke3833882020-02-17 17:28:10 +09003186// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07003187func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09003188 implName := proptools.String(module.properties.Lib_name)
Colin Crossff694a82023-12-13 15:54:49 -08003189 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07003190 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09003191 // In most cases, this works fine. But when apex_name is set or override_apex is used
3192 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07003193 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09003194 }
3195 partition := "system"
3196 if module.SocSpecific() {
3197 partition = "vendor"
3198 } else if module.DeviceSpecific() {
3199 partition = "odm"
3200 } else if module.ProductSpecific() {
3201 partition = "product"
3202 } else if module.SystemExtSpecific() {
3203 partition = "system_ext"
3204 }
3205 return "/" + partition + "/framework/" + implName + ".jar"
3206}
3207
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003208func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
3209 if value == nil {
3210 return ""
3211 }
3212 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
3213 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003214 // attributes in bp files have underscores but in the xml have dashes.
3215 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003216 return ""
3217 }
Pedro Loureirob638c622021-12-22 15:28:05 +00003218 if apiLevel.IsCurrent() {
3219 // passing "current" would always mean a future release, never the current (or the current in
3220 // progress) which means some conditions would never be triggered.
3221 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3222 `"current" is not an allowed value for this attribute`)
3223 return ""
3224 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003225 // "safeValue" is safe because it translates finalized codenames to a string
3226 // with their SDK int.
3227 safeValue := apiLevel.String()
3228 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003229}
3230
3231// formats an attribute for the xml permissions file if the value is not null
3232// returns empty string otherwise
3233func formattedOptionalAttribute(attrName string, value *string) string {
3234 if value == nil {
3235 return ""
3236 }
3237 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
3238}
3239
Jamie Garsidee570ace2023-11-27 12:07:36 +00003240func formattedDependenciesAttribute(dependencies []string) string {
3241 if dependencies == nil {
3242 return ""
3243 }
3244 return fmt.Sprintf(` dependency=\"%s\"\n`, strings.Join(dependencies, ":"))
3245}
3246
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003247func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3248 libName := proptools.String(module.properties.Lib_name)
3249 libNameAttr := formattedOptionalAttribute("name", &libName)
3250 filePath := module.implPath(ctx)
3251 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003252 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3253 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3254 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3255 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003256 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003257 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3258 // 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 +00003259 var libraryTag string
3260 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003261 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00003262 } else {
3263 libraryTag = ` <library\n`
3264 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003265
3266 return strings.Join([]string{
3267 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
3268 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
3269 `\n`,
3270 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
3271 ` you may not use this file except in compliance with the License.\n`,
3272 ` You may obtain a copy of the License at\n`,
3273 `\n`,
3274 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
3275 `\n`,
3276 ` Unless required by applicable law or agreed to in writing, software\n`,
3277 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
3278 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
3279 ` See the License for the specific language governing permissions and\n`,
3280 ` limitations under the License.\n`,
3281 `-->\n`,
3282 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00003283 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003284 libNameAttr,
3285 filePathAttr,
3286 implicitFromAttr,
3287 implicitUntilAttr,
3288 minSdkAttr,
3289 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003290 dependenciesAttr,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003291 ` />\n`,
3292 `</permissions>\n`}, "")
3293}
3294
Jiyong Parke3833882020-02-17 17:28:10 +09003295func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossff694a82023-12-13 15:54:49 -08003296 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
3297 module.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07003298
Jiyong Parke3833882020-02-17 17:28:10 +09003299 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003300 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003301 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003302
3303 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08003304 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003305 rule.Command().
3306 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
3307 Output(module.outputFilePath)
3308
Colin Crossf1a035e2020-11-16 17:32:30 -08003309 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09003310
3311 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
3312}
3313
3314func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003315 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003316 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003317 Disabled: true,
3318 }}
3319 }
3320
satayev8f088b02021-12-06 11:40:46 +00003321 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003322 Class: "ETC",
3323 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3324 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003325 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003326 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003327 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003328 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3329 },
3330 },
3331 }}
3332}
Paul Duffindd46f712020-02-10 13:37:10 +00003333
Pedro Loureiroc3621422021-09-28 15:40:23 +00003334func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3335 module.validateAtLeastTAttributes(ctx)
3336 module.validateMinAndMaxDeviceSdk(ctx)
3337 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3338 module.validateOnBootclasspathBeforeRequirements(ctx)
3339}
3340
3341func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3342 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3343 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3344 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3345 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3346 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3347}
3348
3349func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3350 if attr != nil {
3351 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3352 // we will inform the user of invalid inputs when we try to write the
3353 // permissions xml file so we don't need to do it here
3354 if t.GreaterThan(level) {
3355 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3356 }
3357 }
3358 }
3359}
3360
3361func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3362 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3363 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3364 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3365 if minErr == nil && maxErr == nil {
3366 // we will inform the user of invalid inputs when we try to write the
3367 // permissions xml file so we don't need to do it here
3368 if min.GreaterThan(max) {
3369 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3370 }
3371 }
3372 }
3373}
3374
3375func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3376 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3377 if module.properties.Min_device_sdk != nil {
3378 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3379 if err == nil {
3380 if moduleMinApi.GreaterThan(api) {
3381 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3382 }
3383 }
3384 }
3385 if module.properties.Max_device_sdk != nil {
3386 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3387 if err == nil {
3388 if moduleMinApi.GreaterThan(api) {
3389 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3390 }
3391 }
3392 }
3393}
3394
3395func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3396 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3397 if module.properties.On_bootclasspath_before != nil {
3398 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3399 // if we use the attribute, then we need to do this validation
3400 if moduleMinApi.LessThan(t) {
3401 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3402 if module.properties.Min_device_sdk == nil {
3403 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")
3404 }
3405 }
3406 }
3407}
3408
Paul Duffindd46f712020-02-10 13:37:10 +00003409type sdkLibrarySdkMemberType struct {
3410 android.SdkMemberTypeBase
3411}
3412
Paul Duffin296701e2021-07-14 10:29:36 +01003413func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3414 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003415}
3416
3417func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3418 _, ok := module.(*SdkLibrary)
3419 return ok
3420}
3421
3422func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3423 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3424}
3425
3426func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3427 return &sdkLibrarySdkMemberProperties{}
3428}
3429
Paul Duffin976b0e52021-04-27 23:20:26 +01003430var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3431 android.SdkMemberTypeBase{
3432 PropertyName: "java_sdk_libs",
3433 SupportsSdk: true,
3434 },
3435}
3436
Paul Duffindd46f712020-02-10 13:37:10 +00003437type sdkLibrarySdkMemberProperties struct {
3438 android.SdkMemberPropertiesBase
3439
Paul Duffine8409952022-09-22 16:24:46 +01003440 // Stem name for files in the sdk snapshot.
3441 //
3442 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3443 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3444 //
3445 // This property is marked as keep so that it will be kept in all instances of this struct, will
3446 // not be cleared but will be copied to common structs. That is needed because this field is used
3447 // to construct many file names for other parts of this struct and so it needs to be present in
3448 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3449 // be unavailable for generating file names if there were other properties that were still set.
3450 Stem string `sdk:"keep"`
3451
Paul Duffindd46f712020-02-10 13:37:10 +00003452 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003453 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003454
Paul Duffin3d1248c2020-04-09 00:10:17 +01003455 // The Java stubs source files.
3456 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003457
3458 // The naming scheme.
3459 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003460
3461 // True if the java_sdk_library_import is for a shared library, false
3462 // otherwise.
3463 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003464
Paul Duffin1267d872021-04-16 17:21:36 +01003465 // True if the stub imports should produce dex jars.
3466 Compile_dex *bool
3467
Paul Duffina2ae7e02020-09-11 11:55:00 +01003468 // The paths to the doctag files to add to the prebuilt.
3469 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003470
3471 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003472
3473 // Signals that this shared library is part of the bootclasspath starting
3474 // on the version indicated in this attribute.
3475 //
3476 // This will make platforms at this level and above to ignore
3477 // <uses-library> tags with this library name because the library is already
3478 // available
3479 On_bootclasspath_since *string
3480
3481 // Signals that this shared library was part of the bootclasspath before
3482 // (but not including) the version indicated in this attribute.
3483 //
3484 // The system will automatically add a <uses-library> tag with this library to
3485 // apps that target any SDK less than the version indicated in this attribute.
3486 On_bootclasspath_before *string
3487
3488 // Indicates that PackageManager should ignore this shared library if the
3489 // platform is below the version indicated in this attribute.
3490 //
3491 // This means that the device won't recognise this library as installed.
3492 Min_device_sdk *string
3493
3494 // Indicates that PackageManager should ignore this shared library if the
3495 // platform is above the version indicated in this attribute.
3496 //
3497 // This means that the device won't recognise this library as installed.
3498 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003499
3500 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003501}
3502
3503type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003504 Jars android.Paths
3505 StubsSrcJar android.Path
3506 CurrentApiFile android.Path
3507 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003508 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003509 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003510}
3511
3512func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3513 sdk := variant.(*SdkLibrary)
3514
Paul Duffine8409952022-09-22 16:24:46 +01003515 // Copy the stem name for files in the sdk snapshot.
3516 s.Stem = sdk.distStem()
3517
Paul Duffin106a3a42022-01-27 16:39:06 +00003518 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003519 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003520 paths := sdk.findScopePaths(apiScope)
3521 if paths == nil {
3522 continue
3523 }
3524
Paul Duffindd46f712020-02-10 13:37:10 +00003525 jars := paths.stubsImplPath
3526 if len(jars) > 0 {
3527 properties := scopeProperties{}
3528 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003529 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003530 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003531 if paths.currentApiFilePath.Valid() {
3532 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3533 }
3534 if paths.removedApiFilePath.Valid() {
3535 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3536 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003537 // The annotations zip is only available for modules that set annotations_enabled: true.
3538 if paths.annotationsZip.Valid() {
3539 properties.AnnotationsZip = paths.annotationsZip.Path()
3540 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003541 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003542 }
3543 }
3544
Paul Duffindfa131e2020-05-15 20:37:11 +01003545 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003546 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003547 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003548 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003549 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003550 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3551 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3552 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3553 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003554
3555 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3556 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3557 }
Paul Duffindd46f712020-02-10 13:37:10 +00003558}
3559
3560func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003561 if s.Naming_scheme != nil {
3562 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3563 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003564 if s.Shared_library != nil {
3565 propertySet.AddProperty("shared_library", *s.Shared_library)
3566 }
Paul Duffin1267d872021-04-16 17:21:36 +01003567 if s.Compile_dex != nil {
3568 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3569 }
Paul Duffin869de142021-07-15 14:14:41 +01003570 if len(s.Permitted_packages) > 0 {
3571 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3572 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003573 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3574 if s.DexPreoptProfileGuided != nil {
3575 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3576 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003577
Paul Duffine8409952022-09-22 16:24:46 +01003578 stem := s.Stem
3579
Paul Duffindd46f712020-02-10 13:37:10 +00003580 for _, apiScope := range allApiScopes {
3581 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003582 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003583
Paul Duffin958806b2022-05-16 13:10:47 +00003584 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003585
Paul Duffindd46f712020-02-10 13:37:10 +00003586 var jars []string
3587 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003588 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003589 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3590 jars = append(jars, dest)
3591 }
3592 scopeSet.AddProperty("jars", jars)
3593
Paul Duffin22628d52021-05-12 23:13:22 +01003594 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3595 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003596 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003597 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3598 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3599 } else {
3600 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3601 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003602 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003603 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3604 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3605 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003606
Paul Duffin1fd005d2020-04-09 01:08:11 +01003607 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003608 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003609 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3610 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3611 }
3612
3613 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003614 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003615 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003616 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3617 }
3618
Anton Hanssond78eb762021-09-21 15:25:12 +01003619 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003620 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003621 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3622 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3623 }
3624
Paul Duffindd46f712020-02-10 13:37:10 +00003625 if properties.SdkVersion != "" {
3626 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3627 }
3628 }
3629 }
3630
Paul Duffina2ae7e02020-09-11 11:55:00 +01003631 if len(s.Doctag_paths) > 0 {
3632 dests := []string{}
3633 for _, p := range s.Doctag_paths {
3634 dest := filepath.Join("doctags", p.Rel())
3635 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3636 dests = append(dests, dest)
3637 }
3638 propertySet.AddProperty("doctag_files", dests)
3639 }
Paul Duffindd46f712020-02-10 13:37:10 +00003640}