blob: 89da19a1973f6210794690338da57b74e4b90679 [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010021 "reflect"
Paul Duffin46dc45a2020-05-14 15:39:10 +010022 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090023 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090024 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090025 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090026
Paul Duffind1b3a922020-01-22 11:57:20 +000027 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090028 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010029
30 "android/soong/android"
Zi Wangb2179e32023-01-31 15:53:30 -080031 "android/soong/bazel"
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
Paul Duffind1b3a922020-01-22 11:57:20 +0000108 // The tag to use to depend on the stubs library module.
109 stubsTag scopeDependencyTag
110
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100111 // The tag to use to depend on the stubs source module (if separate from the API module).
112 stubsSourceTag scopeDependencyTag
113
114 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
115 apiFileTag scopeDependencyTag
116
Paul Duffinc8782502020-04-29 20:45:27 +0100117 // The tag to use to depend on the stubs source and API module.
118 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000119
Paul Duffin958806b2022-05-16 13:10:47 +0000120 // The tag to use to depend on the module that provides the latest version of the API .txt file.
121 latestApiModuleTag scopeDependencyTag
122
123 // The tag to use to depend on the module that provides the latest version of the API removed.txt
124 // file.
125 latestRemovedApiModuleTag scopeDependencyTag
126
Paul Duffind1b3a922020-01-22 11:57:20 +0000127 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
128 apiFilePrefix string
129
Paul Duffind0b9fca2022-09-30 18:11:41 +0100130 // The scope specific suffix to add to the sdk library module name to construct a scope specific
Paul Duffind1b3a922020-01-22 11:57:20 +0000131 // module name.
132 moduleSuffix string
133
Paul Duffind1b3a922020-01-22 11:57:20 +0000134 // SDK version that the stubs library is built against. Note that this is always
135 // *current. Older stubs library built with a numbered SDK version is created from
136 // the prebuilt jar.
137 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100138
Paul Duffin15f34ef2020-07-20 18:04:44 +0100139 // The annotation that identifies this API level, empty for the public API scope.
140 annotation string
141
Paul Duffin1fb487d2020-04-07 18:50:10 +0100142 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100143 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100144 // This is not used directly but is used to construct the droidstubsArgs.
145 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100146
Paul Duffin15f34ef2020-07-20 18:04:44 +0100147 // The args that must be passed to droidstubs to generate the API and stubs source
148 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100149 //
150 // The API only includes the additional members that this scope adds over the scope
151 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100152 //
153 // The stubs source must include the definitions of everything that is in this
154 // api scope and all the scopes that this one extends.
155 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100156
Anton Hansson6478ac12020-05-02 11:19:36 +0100157 // Whether the api scope can be treated as unstable, and should skip compat checks.
158 unstable bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000159
160 // Represents the SDK kind of this scope.
161 kind android.SdkKind
Paul Duffind1b3a922020-01-22 11:57:20 +0000162}
163
164// Initialize a scope, creating and adding appropriate dependency tags
165func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100166 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100167 scopeByName[name] = scope
168 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100169 scope.propertyName = strings.ReplaceAll(name, "-", "_")
170 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000171 scope.stubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100172 name: name + "-stubs",
173 apiScope: scope,
174 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000175 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100176 scope.stubsSourceTag = scopeDependencyTag{
177 name: name + "-stubs-source",
178 apiScope: scope,
179 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
180 }
181 scope.apiFileTag = scopeDependencyTag{
182 name: name + "-api",
183 apiScope: scope,
184 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
185 }
Paul Duffinc8782502020-04-29 20:45:27 +0100186 scope.stubsSourceAndApiTag = scopeDependencyTag{
187 name: name + "-stubs-source-and-api",
188 apiScope: scope,
189 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000190 }
Paul Duffin958806b2022-05-16 13:10:47 +0000191 scope.latestApiModuleTag = scopeDependencyTag{
192 name: name + "-latest-api",
193 apiScope: scope,
194 depInfoExtractor: (*scopePaths).extractLatestApiPath,
195 }
196 scope.latestRemovedApiModuleTag = scopeDependencyTag{
197 name: name + "-latest-removed-api",
198 apiScope: scope,
199 depInfoExtractor: (*scopePaths).extractLatestRemovedApiPath,
200 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100201
202 // To get the args needed to generate the stubs source append all the args from
203 // this scope and all the scopes it extends as each set of args adds additional
204 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100205 var scopeSpecificArgs []string
206 if scope.annotation != "" {
207 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100208 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100209 for s := scope; s != nil; s = s.extends {
210 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100211
Paul Duffin15f34ef2020-07-20 18:04:44 +0100212 // Ensure that the generated stubs includes all the API elements from the API scope
213 // that this scope extends.
214 if s != scope && s.annotation != "" {
215 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
216 }
217 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100218
Paul Duffind0b9fca2022-09-30 18:11:41 +0100219 // By default, a library that can access a scope can also access the scope it extends.
220 if scope.canAccess == nil {
221 scope.canAccess = scope.extends
222 }
223
Paul Duffin15f34ef2020-07-20 18:04:44 +0100224 // Escape any special characters in the arguments. This is needed because droidstubs
225 // passes these directly to the shell command.
226 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100227
Paul Duffind1b3a922020-01-22 11:57:20 +0000228 return scope
229}
230
Anton Hansson08f476b2021-04-07 15:32:19 +0100231func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
232 return ".stubs" + scope.moduleSuffix
233}
234
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000235func (scope *apiScope) apiLibraryModuleName(baseName string) string {
236 return scope.stubsLibraryModuleName(baseName) + ".from-text"
237}
238
Paul Duffinc3091c82020-05-08 14:16:20 +0100239func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100240 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000241}
242
Paul Duffinc8782502020-04-29 20:45:27 +0100243func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100244 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000245}
246
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100247func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100248 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100249}
250
Paul Duffin3375e352020-04-28 10:44:03 +0100251func (scope *apiScope) String() string {
252 return scope.name
253}
254
Paul Duffin958806b2022-05-16 13:10:47 +0000255// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
256// be stored.
257func (scope *apiScope) snapshotRelativeDir() string {
258 return filepath.Join("sdk_library", scope.name)
259}
260
261// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
262// library.
263func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
264 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
265}
266
267// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
268// named library.
269func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
270 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
271}
272
Paul Duffind1b3a922020-01-22 11:57:20 +0000273type apiScopes []*apiScope
274
275func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
276 var list []string
277 for _, scope := range scopes {
278 list = append(list, accessor(scope))
279 }
280 return list
281}
282
Jiyong Parkc678ad32018-04-10 13:07:10 +0900283var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100284 scopeByName = make(map[string]*apiScope)
285 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000286 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100287 name: "public",
288
289 // Public scope is enabled by default for both legacy and non-legacy modes.
290 legacyEnabledStatus: func(module *SdkLibrary) bool {
291 return true
292 },
293 defaultEnabledStatus: true,
294
295 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
296 return &module.sdkLibraryProperties.Public
297 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000298 sdkVersion: "current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000299 kind: android.SdkPublic,
Paul Duffind1b3a922020-01-22 11:57:20 +0000300 })
301 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100302 name: "system",
303 extends: apiScopePublic,
304 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
305 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
306 return &module.sdkLibraryProperties.System
307 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100308 apiFilePrefix: "system-",
309 moduleSuffix: ".system",
310 sdkVersion: "system_current",
311 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000312 kind: android.SdkSystem,
Paul Duffind1b3a922020-01-22 11:57:20 +0000313 })
314 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100315 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100316 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100317 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
318 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
319 return &module.sdkLibraryProperties.Test
320 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100321 apiFilePrefix: "test-",
322 moduleSuffix: ".test",
323 sdkVersion: "test_current",
324 annotation: "android.annotation.TestApi",
325 unstable: true,
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000326 kind: android.SdkTest,
Paul Duffind1b3a922020-01-22 11:57:20 +0000327 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100328 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100329 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100330 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100331 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100332 //
333 // Enabling this would break existing usages.
334 legacyEnabledStatus: func(module *SdkLibrary) bool {
335 return false
336 },
337 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
338 return &module.sdkLibraryProperties.Module_lib
339 },
340 apiFilePrefix: "module-lib-",
341 moduleSuffix: ".module_lib",
342 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100343 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000344 kind: android.SdkModule,
Paul Duffin8f265b92020-04-28 14:13:56 +0100345 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100346 apiScopeSystemServer = initApiScope(&apiScope{
347 name: "system-server",
348 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100349
350 // The system-server scope can access the module-lib scope.
351 //
352 // A module that provides a system-server API is appended to the standard bootclasspath that is
353 // used by the system server. So, it should be able to access module-lib APIs provided by
354 // libraries on the bootclasspath.
355 canAccess: apiScopeModuleLib,
356
Paul Duffin0c5bae52020-06-02 13:00:08 +0100357 // The system-server scope is disabled by default in legacy mode.
358 //
359 // Enabling this would break existing usages.
360 legacyEnabledStatus: func(module *SdkLibrary) bool {
361 return false
362 },
363 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
364 return &module.sdkLibraryProperties.System_server
365 },
366 apiFilePrefix: "system-server-",
367 moduleSuffix: ".system_server",
368 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100369 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
370 extraArgs: []string{
371 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100372 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100373 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100374 },
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000375 kind: android.SdkSystemServer,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100376 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000377 allApiScopes = apiScopes{
378 apiScopePublic,
379 apiScopeSystem,
380 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100381 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100382 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000383 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900384)
385
Jiyong Park82484c02018-04-23 21:41:26 +0900386var (
387 javaSdkLibrariesLock sync.Mutex
388)
389
Jiyong Parkc678ad32018-04-10 13:07:10 +0900390// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900391// 1) disallowing linking to the runtime shared lib
392// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900393
394func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000395 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900396
Jiyong Park82484c02018-04-23 21:41:26 +0900397 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
398 javaSdkLibraries := javaSdkLibraries(ctx.Config())
399 sort.Strings(*javaSdkLibraries)
400 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
401 })
Paul Duffindd46f712020-02-10 13:37:10 +0000402
403 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100404 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900405}
406
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000407func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
408 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
409 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
410}
411
Paul Duffin3375e352020-04-28 10:44:03 +0100412// Properties associated with each api scope.
413type ApiScopeProperties struct {
414 // Indicates whether the api surface is generated.
415 //
416 // If this is set for any scope then all scopes must explicitly specify if they
417 // are enabled. This is to prevent new usages from depending on legacy behavior.
418 //
419 // Otherwise, if this is not set for any scope then the default behavior is
420 // scope specific so please refer to the scope specific property documentation.
421 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100422
423 // The sdk_version to use for building the stubs.
424 //
425 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000426 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100427 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000428 // will be none. This is used for java_sdk_library instances that are used
429 // to create stubs that contribute to the core_current sdk version.
430 // 2) Otherwise, it is assumed that this library extends but does not
431 // contribute directly to a specific sdk_version and so this uses the
432 // sdk_version appropriate for the api scope. e.g. public will use
433 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100434 //
435 // This does not affect the sdk_version used for either generating the stubs source
436 // or the API file. They both have to use the same sdk_version as is used for
437 // compiling the implementation library.
438 Sdk_version *string
Paul Duffin3375e352020-04-28 10:44:03 +0100439}
440
Jiyong Parkc678ad32018-04-10 13:07:10 +0900441type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100442 // List of source files that are needed to compile the API, but are not part of runtime library.
443 Api_srcs []string `android:"arch_variant"`
444
Paul Duffin5df79302020-05-16 15:52:12 +0100445 // Visibility for impl library module. If not specified then defaults to the
446 // visibility property.
447 Impl_library_visibility []string
448
Paul Duffin4911a892020-04-29 23:35:13 +0100449 // Visibility for stubs library modules. If not specified then defaults to the
450 // visibility property.
451 Stubs_library_visibility []string
452
453 // Visibility for stubs source modules. If not specified then defaults to the
454 // visibility property.
455 Stubs_source_visibility []string
456
Anton Hansson7f66efa2020-10-08 14:47:23 +0100457 // List of Java libraries that will be in the classpath when building the implementation lib
458 Impl_only_libs []string `android:"arch_variant"`
459
Paul Duffin77590a82022-04-28 14:13:30 +0000460 // List of Java libraries that will included in the implementation lib.
461 Impl_only_static_libs []string `android:"arch_variant"`
462
Sundong Ahnf043cf62018-06-25 16:04:37 +0900463 // List of Java libraries that will be in the classpath when building stubs
464 Stub_only_libs []string `android:"arch_variant"`
465
Anton Hanssondae54cd2021-04-21 16:30:10 +0100466 // List of Java libraries that will included in stub libraries
467 Stub_only_static_libs []string `android:"arch_variant"`
468
Paul Duffin7a586d32019-12-30 17:09:34 +0000469 // list of package names that will be documented and publicized as API.
470 // This allows the API to be restricted to a subset of the source files provided.
471 // If this is unspecified then all the source files will be treated as being part
472 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900473 Api_packages []string
474
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900475 // list of package names that must be hidden from the API
476 Hidden_api_packages []string
477
Paul Duffin749f98f2019-12-30 17:23:46 +0000478 // the relative path to the directory containing the api specification files.
479 // Defaults to "api".
480 Api_dir *string
481
Paul Duffindfa131e2020-05-15 20:37:11 +0100482 // Determines whether a runtime implementation library is built; defaults to false.
483 //
484 // 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 +0200485 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000486 Api_only *bool
487
Paul Duffin11512472019-02-11 15:55:17 +0000488 // local files that are used within user customized droiddoc options.
489 Droiddoc_option_files []string
490
Spandan Das93e95992021-07-29 18:26:39 +0000491 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000492 // Available variables for substitution:
493 //
494 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900495 Droiddoc_options []string
496
Paul Duffine22c2ab2020-05-20 19:35:27 +0100497 // is set to true, Metalava will allow framework SDK to contain annotations.
498 Annotations_enabled *bool
499
Sundong Ahn054b19a2018-10-19 13:46:09 +0900500 // a list of top-level directories containing files to merge qualifier annotations
501 // (i.e. those intended to be included in the stubs written) from.
502 Merge_annotations_dirs []string
503
504 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
505 Merge_inclusion_annotations_dirs []string
506
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000507 // If set to true then don't create dist rules.
508 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900509
Paul Duffin31310252020-11-20 21:26:20 +0000510 // The stem for the artifacts that are copied to the dist, if not specified
511 // then defaults to the base module name.
512 //
513 // For each scope the following artifacts are copied to the apistubs/<scope>
514 // directory in the dist.
515 // * stubs impl jar -> <dist-stem>.jar
516 // * API specification file -> api/<dist-stem>.txt
517 // * Removed API specification file -> api/<dist-stem>-removed.txt
518 //
519 // Also used to construct the name of the filegroup (created by prebuilt_apis)
520 // that references the latest released API and remove API specification files.
521 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
522 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800523 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000524 Dist_stem *string
525
Colin Cross986b69a2021-06-01 13:13:40 -0700526 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700527 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700528 // in the public Android SDK.
529 Dist_group *string
530
Anton Hanssondff2c782020-12-21 17:10:01 +0000531 // A compatibility mode that allows historical API-tracking files to not exist.
532 // Do not use.
533 Unsafe_ignore_missing_latest_api bool
534
Paul Duffin3375e352020-04-28 10:44:03 +0100535 // indicates whether system and test apis should be generated.
536 Generate_system_and_test_apis bool `blueprint:"mutated"`
537
538 // The properties specific to the public api scope
539 //
540 // Unless explicitly specified by using public.enabled the public api scope is
541 // enabled by default in both legacy and non-legacy mode.
542 Public ApiScopeProperties
543
544 // The properties specific to the system api scope
545 //
546 // In legacy mode the system api scope is enabled by default when sdk_version
547 // is set to something other than "none".
548 //
549 // In non-legacy mode the system api scope is disabled by default.
550 System ApiScopeProperties
551
552 // The properties specific to the test api scope
553 //
554 // In legacy mode the test api scope is enabled by default when sdk_version
555 // is set to something other than "none".
556 //
557 // In non-legacy mode the test api scope is disabled by default.
558 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000559
Paul Duffin0c5bae52020-06-02 13:00:08 +0100560 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100561 //
Zi Wangb2179e32023-01-31 15:53:30 -0800562 // Unless explicitly specified by using module_lib.enabled the module_lib api
563 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100564 Module_lib ApiScopeProperties
565
Paul Duffin0c5bae52020-06-02 13:00:08 +0100566 // The properties specific to the system-server api scope
567 //
Zi Wangb2179e32023-01-31 15:53:30 -0800568 // Unless explicitly specified by using system_server.enabled the
569 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100570 System_server ApiScopeProperties
571
Jiyong Park932cdfe2020-05-28 00:19:53 +0900572 // Determines if the stubs are preferred over the implementation library
573 // for linking, even when the client doesn't specify sdk_version. When this
574 // is set to true, such clients are provided with the widest API surface that
575 // this lib provides. Note however that this option doesn't affect the clients
576 // that are in the same APEX as this library. In that case, the clients are
577 // always linked with the implementation library. Default is false.
578 Default_to_stubs *bool
579
Paul Duffin160fe412020-05-10 19:32:20 +0100580 // Properties related to api linting.
581 Api_lint struct {
582 // Enable api linting.
583 Enabled *bool
584 }
585
Jiyong Parkc678ad32018-04-10 13:07:10 +0900586 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100587 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900588}
589
Paul Duffin0f8faff2020-05-20 16:18:00 +0100590// Paths to outputs from java_sdk_library and java_sdk_library_import.
591//
592// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
593// OptionalPaths are always set by java_sdk_library but may not be set by
594// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000595type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100596 // The path (represented as Paths for convenience when returning) to the stubs header jar.
597 //
598 // That is the jar that is created by turbine.
599 stubsHeaderPath android.Paths
600
601 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
602 //
603 // This is not the implementation jar, it still only contains stubs.
604 stubsImplPath android.Paths
605
Paul Duffin1267d872021-04-16 17:21:36 +0100606 // The dex jar for the stubs.
607 //
608 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100609 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100610
Paul Duffin0f8faff2020-05-20 16:18:00 +0100611 // The API specification file, e.g. system_current.txt.
612 currentApiFilePath android.OptionalPath
613
614 // The specification of API elements removed since the last release.
615 removedApiFilePath android.OptionalPath
616
617 // The stubs source jar.
618 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100619
620 // Extracted annotations.
621 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000622
623 // The path to the latest API file.
624 latestApiPath android.OptionalPath
625
626 // The path to the latest removed API file.
627 latestRemovedApiPath android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000628}
629
Colin Crossdcf71b22021-02-01 13:59:03 -0800630func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
631 if ctx.OtherModuleHasProvider(dep, JavaInfoProvider) {
632 lib := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
633 paths.stubsHeaderPath = lib.HeaderJars
634 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100635
636 libDep := dep.(UsesLibraryDependency)
637 paths.stubsDexJarPath = libDep.DexJarBuildPath()
Paul Duffinc8782502020-04-29 20:45:27 +0100638 return nil
639 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800640 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100641 }
642}
643
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100644func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
645 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
646 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100647 return nil
648 } else {
649 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
650 }
651}
652
Paul Duffin0f8faff2020-05-20 16:18:00 +0100653func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
654 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
655 action(apiStubsProvider)
656 return nil
657 } else {
658 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
659 }
660}
661
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100662func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hanssond78eb762021-09-21 15:25:12 +0100663 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100664 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
665 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100666}
667
Colin Crossdcf71b22021-02-01 13:59:03 -0800668func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100669 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
670 paths.extractApiInfoFromApiStubsProvider(provider)
671 })
672}
673
Paul Duffin0f8faff2020-05-20 16:18:00 +0100674func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
675 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100676}
677
Colin Crossdcf71b22021-02-01 13:59:03 -0800678func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100679 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100680 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
681 })
682}
683
Colin Crossdcf71b22021-02-01 13:59:03 -0800684func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100685 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
686 paths.extractApiInfoFromApiStubsProvider(provider)
687 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
688 })
689}
690
Paul Duffin958806b2022-05-16 13:10:47 +0000691func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
692 var paths android.Paths
693 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
694 paths = sourceFileProducer.Srcs()
695 } else {
696 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
697 }
698 if len(paths) != 1 {
699 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
700 }
701 return android.OptionalPathForPath(paths[0]), nil
702}
703
704func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
705 outputPath, err := extractSingleOptionalOutputPath(dep)
706 paths.latestApiPath = outputPath
707 return err
708}
709
710func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
711 outputPath, err := extractSingleOptionalOutputPath(dep)
712 paths.latestRemovedApiPath = outputPath
713 return err
714}
715
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100716type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100717 // The naming scheme to use for the components that this module creates.
718 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100719 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100720 //
721 // This is a temporary mechanism to simplify conversion from separate modules for each
722 // component that follow a different naming pattern to the default one.
723 //
724 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100725 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100726
727 // Specifies whether this module can be used as an Android shared library; defaults
728 // to true.
729 //
730 // An Android shared library is one that can be referenced in a <uses-library> element
731 // in an AndroidManifest.xml.
732 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100733
734 // Files containing information about supported java doc tags.
735 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000736
737 // Signals that this shared library is part of the bootclasspath starting
738 // on the version indicated in this attribute.
739 //
740 // This will make platforms at this level and above to ignore
741 // <uses-library> tags with this library name because the library is already
742 // available
743 On_bootclasspath_since *string
744
745 // Signals that this shared library was part of the bootclasspath before
746 // (but not including) the version indicated in this attribute.
747 //
748 // The system will automatically add a <uses-library> tag with this library to
749 // apps that target any SDK less than the version indicated in this attribute.
750 On_bootclasspath_before *string
751
752 // Indicates that PackageManager should ignore this shared library if the
753 // platform is below the version indicated in this attribute.
754 //
755 // This means that the device won't recognise this library as installed.
756 Min_device_sdk *string
757
758 // Indicates that PackageManager should ignore this shared library if the
759 // platform is above the version indicated in this attribute.
760 //
761 // This means that the device won't recognise this library as installed.
762 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100763}
764
Paul Duffin71b33cc2021-06-23 11:39:47 +0100765// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
766// embeds the commonToSdkLibraryAndImport struct.
767type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000768 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100769
770 BaseModuleName() string
771}
772
Paul Duffin56d44902020-01-31 13:36:25 +0000773// Common code between sdk library and sdk library import
774type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100775 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100776
Paul Duffin56d44902020-01-31 13:36:25 +0000777 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100778
779 namingScheme sdkLibraryComponentNamingScheme
780
Paul Duffindfa131e2020-05-15 20:37:11 +0100781 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100782
Paul Duffina2ae7e02020-09-11 11:55:00 +0100783 // Paths to commonSdkLibraryProperties.Doctag_files
784 doctagPaths android.Paths
785
Paul Duffin859fe962020-05-15 10:20:31 +0100786 // Functionality related to this being used as a component of a java_sdk_library.
787 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000788}
789
Paul Duffin71b33cc2021-06-23 11:39:47 +0100790func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
791 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100792
Paul Duffin71b33cc2021-06-23 11:39:47 +0100793 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100794
795 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100796 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100797}
798
799func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100800 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100801 switch schemeProperty {
802 case "default":
803 c.namingScheme = &defaultNamingScheme{}
804 default:
805 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
806 return false
807 }
808
Paul Duffin3f0290e2021-06-30 18:25:36 +0100809 namePtr := proptools.StringPtr(c.module.BaseModuleName())
810 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
811
Paul Duffindfa131e2020-05-15 20:37:11 +0100812 // Only track this sdk library if this can be used as a shared library.
813 if c.sharedLibrary() {
814 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100815 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100816 }
Paul Duffin859fe962020-05-15 10:20:31 +0100817
Paul Duffin1b1e8062020-05-08 13:44:43 +0100818 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100819}
820
Paul Duffinea8f8082021-06-24 13:25:57 +0100821// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
822// method.
823func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
824 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
825 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
826 // the APEX and so it needs a unique variation per APEX.
827 return c.sharedLibrary()
828}
829
Paul Duffina2ae7e02020-09-11 11:55:00 +0100830func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
831 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
832}
833
Paul Duffineedc5d52020-06-12 17:46:39 +0100834// Module name of the runtime implementation library
835func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100836 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100837}
838
839// Module name of the XML file for the lib
840func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100841 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100842}
843
Paul Duffinc3091c82020-05-08 14:16:20 +0100844// Name of the java_library module that compiles the stubs source.
845func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100846 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000847 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100848}
849
850// Name of the droidstubs module that generates the stubs source and may also
851// generate/check the API.
852func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100853 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000854 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100855}
856
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000857// Name of the java_api_library module that generates the from-text stubs source
858// and compiles to a jar file.
859func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
860 baseName := c.module.BaseModuleName()
861 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
862}
863
Paul Duffin46dc45a2020-05-14 15:39:10 +0100864// The component names for different outputs of the java_sdk_library.
865//
866// They are similar to the names used for the child modules it creates
867const (
868 stubsSourceComponentName = "stubs.source"
869
870 apiTxtComponentName = "api.txt"
871
872 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +0100873
874 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +0100875)
876
877// A regular expression to match tags that reference a specific stubs component.
878//
879// It will only match if given a valid scope and a valid component. It is verfy strict
880// to ensure it does not accidentally match a similar looking tag that should be processed
881// by the embedded Library.
882var tagSplitter = func() *regexp.Regexp {
883 // Given a list of literal string items returns a regular expression that will
884 // match any one of the items.
885 choice := func(items ...string) string {
886 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
887 }
888
889 // Regular expression to match one of the scopes.
890 scopesRegexp := choice(allScopeNames...)
891
892 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +0100893 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100894
895 // Regular expression to match any combination of one scope and one component.
896 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
897}()
898
899// For OutputFileProducer interface
900//
Anton Hanssond78eb762021-09-21 15:25:12 +0100901// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100902func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
903 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
904 scopeName := groups[1]
905 component := groups[2]
906
907 if scope, ok := scopeByName[scopeName]; ok {
908 paths := c.findScopePaths(scope)
909 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100910 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100911 }
912
913 switch component {
914 case stubsSourceComponentName:
915 if paths.stubsSrcJar.Valid() {
916 return android.Paths{paths.stubsSrcJar.Path()}, nil
917 }
918
919 case apiTxtComponentName:
920 if paths.currentApiFilePath.Valid() {
921 return android.Paths{paths.currentApiFilePath.Path()}, nil
922 }
923
924 case removedApiTxtComponentName:
925 if paths.removedApiFilePath.Valid() {
926 return android.Paths{paths.removedApiFilePath.Path()}, nil
927 }
Anton Hanssond78eb762021-09-21 15:25:12 +0100928
929 case annotationsComponentName:
930 if paths.annotationsZip.Valid() {
931 return android.Paths{paths.annotationsZip.Path()}, nil
932 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100933 }
934
935 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
936 } else {
937 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
938 }
939
940 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100941 switch tag {
942 case ".doctags":
943 if c.doctagPaths != nil {
944 return c.doctagPaths, nil
945 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100946 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +0100947 }
948 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100949 return nil, nil
950 }
951}
952
Paul Duffin803a9562020-05-20 11:52:25 +0100953func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000954 if c.scopePaths == nil {
955 c.scopePaths = make(map[*apiScope]*scopePaths)
956 }
957 paths := c.scopePaths[scope]
958 if paths == nil {
959 paths = &scopePaths{}
960 c.scopePaths[scope] = paths
961 }
962
963 return paths
964}
965
Paul Duffin803a9562020-05-20 11:52:25 +0100966func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
967 if c.scopePaths == nil {
968 return nil
969 }
970
971 return c.scopePaths[scope]
972}
973
974// If this does not support the requested api scope then find the closest available
975// scope it does support. Returns nil if no such scope is available.
976func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +0100977 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +0100978 if paths := c.findScopePaths(s); paths != nil {
979 return paths
980 }
981 }
982
983 // This should never happen outside tests as public should be the base scope for every
984 // scope and is enabled by default.
985 return nil
986}
987
Jiyong Parkf1691d22021-03-29 20:11:58 +0900988func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100989
990 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +0900991 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100992 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +0100993 }
994
Paul Duffin1267d872021-04-16 17:21:36 +0100995 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
996 if paths == nil {
997 return nil
998 }
999
1000 return paths.stubsHeaderPath
1001}
1002
1003// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1004//
1005// If the module does not support the specific kind then it will return the *scopePaths for the
1006// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1007// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1008func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001009 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001010
Paul Duffin803a9562020-05-20 11:52:25 +01001011 paths := c.findClosestScopePath(apiScope)
1012 if paths == nil {
1013 var scopes []string
1014 for _, s := range allApiScopes {
1015 if c.findScopePaths(s) != nil {
1016 scopes = append(scopes, s.name)
1017 }
1018 }
Paul Duffin71b33cc2021-06-23 11:39:47 +01001019 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.module.BaseModuleName(), scopes)
Paul Duffin803a9562020-05-20 11:52:25 +01001020 return nil
1021 }
1022
Paul Duffin1267d872021-04-16 17:21:36 +01001023 return paths
1024}
1025
Paul Duffin32cf58a2021-05-18 16:32:50 +01001026// sdkKindToApiScope maps from android.SdkKind to apiScope.
1027func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1028 var apiScope *apiScope
1029 switch kind {
1030 case android.SdkSystem:
1031 apiScope = apiScopeSystem
1032 case android.SdkModule:
1033 apiScope = apiScopeModuleLib
1034 case android.SdkTest:
1035 apiScope = apiScopeTest
1036 case android.SdkSystemServer:
1037 apiScope = apiScopeSystemServer
1038 default:
1039 apiScope = apiScopePublic
1040 }
1041 return apiScope
1042}
1043
Paul Duffin1267d872021-04-16 17:21:36 +01001044// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001045func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001046 paths := c.selectScopePaths(ctx, kind)
1047 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001048 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001049 }
1050
1051 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001052}
1053
Paul Duffin32cf58a2021-05-18 16:32:50 +01001054// to satisfy SdkLibraryDependency interface
1055func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1056 apiScope := sdkKindToApiScope(kind)
1057 paths := c.findScopePaths(apiScope)
1058 if paths == nil {
1059 return android.OptionalPath{}
1060 }
1061
1062 return paths.removedApiFilePath
1063}
1064
Paul Duffin859fe962020-05-15 10:20:31 +01001065func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1066 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001067 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001068 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001069 }{}
1070
Paul Duffin3f0290e2021-06-30 18:25:36 +01001071 namePtr := proptools.StringPtr(c.module.BaseModuleName())
1072 componentProps.SdkLibraryName = namePtr
1073
Paul Duffindfa131e2020-05-15 20:37:11 +01001074 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001075 // Mark the stubs library as being components of this java_sdk_library so that
1076 // any app that includes code which depends (directly or indirectly) on the stubs
1077 // library will have the appropriate <uses-library> invocation inserted into its
1078 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001079 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001080 }
1081
1082 return componentProps
1083}
1084
Paul Duffindfa131e2020-05-15 20:37:11 +01001085func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1086 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1087}
1088
Paul Duffinf4600f62021-05-13 22:34:45 +01001089// Check if the stub libraries should be compiled for dex
1090func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1091 // Always compile the dex file files for the stub libraries if they will be used on the
1092 // bootclasspath.
1093 return !c.sharedLibrary()
1094}
1095
Paul Duffin859fe962020-05-15 10:20:31 +01001096// Properties related to the use of a module as an component of a java_sdk_library.
1097type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001098 // The name of the java_sdk_library/_import module.
1099 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001100
1101 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1102 // in the AndroidManifest.xml of any Android app that includes code that references
1103 // this module. If not set then no java_sdk_library/_import is tracked.
1104 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1105}
1106
1107// Structure to be embedded in a module struct that needs to support the
1108// SdkLibraryComponentDependency interface.
1109type EmbeddableSdkLibraryComponent struct {
1110 sdkLibraryComponentProperties SdkLibraryComponentProperties
1111}
1112
Paul Duffin71b33cc2021-06-23 11:39:47 +01001113func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1114 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001115}
1116
1117// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001118func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1119 return e.sdkLibraryComponentProperties.SdkLibraryName
1120}
1121
1122// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001123func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001124 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1125 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1126 // run-time library and the corresponding module that provides the implementation. This name is
1127 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1128 // in dexpreopt).
1129 //
1130 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1131 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001132 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1133}
1134
Paul Duffin859fe962020-05-15 10:20:31 +01001135// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1136// (including the java_sdk_library) itself.
1137type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001138 UsesLibraryDependency
1139
Paul Duffin3f0290e2021-06-30 18:25:36 +01001140 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1141 SdkLibraryName() *string
1142
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001143 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1144 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001145}
1146
1147// Make sure that all the module types that are components of java_sdk_library/_import
1148// and which can be referenced (directly or indirectly) from an android app implement
1149// the SdkLibraryComponentDependency interface.
1150var _ SdkLibraryComponentDependency = (*Library)(nil)
1151var _ SdkLibraryComponentDependency = (*Import)(nil)
1152var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001153var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001154
Paul Duffin32cf58a2021-05-18 16:32:50 +01001155// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001156type SdkLibraryDependency interface {
1157 SdkLibraryComponentDependency
1158
1159 // Get the header jars appropriate for the supplied sdk_version.
1160 //
1161 // These are turbine generated jars so they only change if the externals of the
1162 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001163 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001164
1165 // Get the implementation jars appropriate for the supplied sdk version.
1166 //
1167 // These are either the implementation jar for the whole sdk library or the implementation
1168 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1169 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001170 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001171
1172 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
1173 // tool which processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001174 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001175
Paul Duffin32cf58a2021-05-18 16:32:50 +01001176 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1177 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1178
Paul Duffinf4600f62021-05-13 22:34:45 +01001179 // sharedLibrary returns true if this can be used as a shared library.
1180 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001181}
1182
Inseob Kimc0907f12019-02-08 21:00:45 +09001183type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001184 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001185
Zi Wangb2179e32023-01-31 15:53:30 -08001186 android.BazelModuleBase
1187
Sundong Ahn054b19a2018-10-19 13:46:09 +09001188 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001189
Paul Duffin3375e352020-04-28 10:44:03 +01001190 // Map from api scope to the scope specific property structure.
1191 scopeToProperties map[*apiScope]*ApiScopeProperties
1192
Paul Duffin56d44902020-01-31 13:36:25 +00001193 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001194}
1195
Inseob Kimc0907f12019-02-08 21:00:45 +09001196var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001197
Paul Duffin3375e352020-04-28 10:44:03 +01001198func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1199 return module.sdkLibraryProperties.Generate_system_and_test_apis
1200}
1201
1202func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1203 // Check to see if any scopes have been explicitly enabled. If any have then all
1204 // must be.
1205 anyScopesExplicitlyEnabled := false
1206 for _, scope := range allApiScopes {
1207 scopeProperties := module.scopeToProperties[scope]
1208 if scopeProperties.Enabled != nil {
1209 anyScopesExplicitlyEnabled = true
1210 break
1211 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001212 }
Paul Duffin3375e352020-04-28 10:44:03 +01001213
1214 var generatedScopes apiScopes
1215 enabledScopes := make(map[*apiScope]struct{})
1216 for _, scope := range allApiScopes {
1217 scopeProperties := module.scopeToProperties[scope]
1218 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1219 // This is to ensure that any new usages of this module type do not rely on legacy
1220 // behaviour.
1221 defaultEnabledStatus := false
1222 if anyScopesExplicitlyEnabled {
1223 defaultEnabledStatus = scope.defaultEnabledStatus
1224 } else {
1225 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1226 }
1227 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1228 if enabled {
1229 enabledScopes[scope] = struct{}{}
1230 generatedScopes = append(generatedScopes, scope)
1231 }
1232 }
1233
1234 // Now check to make sure that any scope that is extended by an enabled scope is also
1235 // enabled.
1236 for _, scope := range allApiScopes {
1237 if _, ok := enabledScopes[scope]; ok {
1238 extends := scope.extends
1239 if extends != nil {
1240 if _, ok := enabledScopes[extends]; !ok {
1241 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1242 }
1243 }
1244 }
1245 }
1246
1247 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001248}
1249
satayev758968a2021-12-06 11:42:40 +00001250var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1251
satayev8f088b02021-12-06 11:40:46 +00001252func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001253 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001254 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1255 isExternal := !module.depIsInSameApex(ctx, child)
1256 if am, ok := child.(android.ApexModule); ok {
1257 if !do(ctx, parent, am, isExternal) {
1258 return false
1259 }
1260 }
1261 return !isExternal
1262 })
1263 })
1264}
1265
Paul Duffineedc5d52020-06-12 17:46:39 +01001266type sdkLibraryComponentTag struct {
1267 blueprint.BaseDependencyTag
1268 name string
1269}
1270
1271// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1272func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1273
1274var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001275
Jiyong Parke3833882020-02-17 17:28:10 +09001276func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001277 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001278 return dt == xmlPermissionsFileTag
1279 }
1280 return false
1281}
1282
Paul Duffineedc5d52020-06-12 17:46:39 +01001283var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001284
Paul Duffin44f1d842020-06-26 20:17:02 +01001285// Add the dependencies on the child modules in the component deps mutator.
1286func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001287 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001288 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001289 stubModuleName := module.stubsLibraryModuleName(apiScope)
1290 // Use JavaApiLibraryName function to be redirected to stubs generated from .txt if applicable
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001291 if module.contributesToApiSurface(ctx.Config()) {
1292 stubModuleName = android.JavaApiLibraryName(ctx.Config(), stubModuleName)
1293 }
Spandan Das877f39d2023-03-29 16:19:51 +00001294 ctx.AddVariationDependencies(nil, apiScope.stubsTag, stubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001295
Paul Duffin15f34ef2020-07-20 18:04:44 +01001296 // Add a dependency on the stubs source in order to access both stubs source and api information.
1297 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001298
1299 if module.compareAgainstLatestApi(apiScope) {
1300 // Add dependencies on the latest finalized version of the API .txt file.
1301 latestApiModuleName := module.latestApiModuleName(apiScope)
1302 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1303
1304 // Add dependencies on the latest finalized version of the remove API .txt file.
1305 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1306 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1307 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001308 }
1309
Paul Duffindfa131e2020-05-15 20:37:11 +01001310 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001311 // Add dependency to the rule for generating the implementation library.
1312 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1313
Paul Duffindfa131e2020-05-15 20:37:11 +01001314 if module.sharedLibrary() {
1315 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001316 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001317 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001318 }
1319}
Paul Duffine74ac732020-02-06 13:51:46 +00001320
Paul Duffin44f1d842020-06-26 20:17:02 +01001321// Add other dependencies as normal.
1322func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001323 var missingApiModules []string
1324 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1325 if apiScope.unstable {
1326 continue
1327 }
Paul Duffin958806b2022-05-16 13:10:47 +00001328 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001329 missingApiModules = append(missingApiModules, m)
1330 }
Paul Duffin958806b2022-05-16 13:10:47 +00001331 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001332 missingApiModules = append(missingApiModules, m)
1333 }
Paul Duffin958806b2022-05-16 13:10:47 +00001334 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001335 missingApiModules = append(missingApiModules, m)
1336 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001337 }
1338 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1339 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1340 m += "You need to do one of the following:\n"
1341 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1342 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1343 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1344 m += "\n"
1345 m += "The following filegroup modules are missing:\n "
1346 m += strings.Join(missingApiModules, "\n ") + "\n"
1347 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."
1348 ctx.ModuleErrorf(m)
1349 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001350 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001351 // Only add the deps for the library if it is actually going to be built.
1352 module.Library.deps(ctx)
1353 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001354}
1355
Paul Duffin46dc45a2020-05-14 15:39:10 +01001356func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1357 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001358 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001359 return paths, err
1360 }
Colin Cross4acaea92021-12-10 23:05:02 +00001361 if module.requiresRuntimeImplementationLibrary() {
1362 return module.Library.OutputFiles(tag)
1363 }
1364 if tag == "" {
1365 return nil, nil
1366 }
1367 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001368}
1369
Inseob Kimc0907f12019-02-08 21:00:45 +09001370func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001371 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1372 module.CheckMinSdkVersion(ctx)
1373 }
1374
Paul Duffina2ae7e02020-09-11 11:55:00 +01001375 module.generateCommonBuildActions(ctx)
1376
Paul Duffindfa131e2020-05-15 20:37:11 +01001377 // Only build an implementation library if required.
1378 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001379 module.Library.GenerateAndroidBuildActions(ctx)
1380 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001381
Paul Duffinb97b1572021-04-29 21:50:40 +01001382 // Collate the components exported by this module. All scope specific modules are exported but
1383 // the impl and xml component modules are not.
1384 exportedComponents := map[string]struct{}{}
1385
Sundong Ahn57368eb2018-07-06 11:20:23 +09001386 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001387 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001388 // the recorded paths will be returned depending on the link type of the caller.
1389 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001390 tag := ctx.OtherModuleDependencyTag(to)
1391
Paul Duffinc8782502020-04-29 20:45:27 +01001392 // Extract information from any of the scope specific dependencies.
1393 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1394 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001395 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001396
1397 // Extract information from the dependency. The exact information extracted
1398 // is determined by the nature of the dependency which is determined by the tag.
1399 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001400
1401 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001402 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001403 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001404
1405 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001406 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Paul Duffinb97b1572021-04-29 21:50:40 +01001407 ctx.SetProvider(android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001408
1409 // Provide additional information for inclusion in an sdk's generated .info file.
1410 additionalSdkInfo := map[string]interface{}{}
1411 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001412 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001413 scopes := map[string]interface{}{}
1414 additionalSdkInfo["scopes"] = scopes
1415 for scope, scopePaths := range module.scopePaths {
1416 scopeInfo := map[string]interface{}{}
1417 scopes[scope.name] = scopeInfo
1418 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1419 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1420 if p := scopePaths.latestApiPath; p.Valid() {
1421 scopeInfo["latest_api"] = p.Path().String()
1422 }
1423 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1424 scopeInfo["latest_removed_api"] = p.Path().String()
1425 }
1426 }
1427 ctx.SetProvider(android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001428}
1429
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001430func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001431 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001432 return nil
1433 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001434 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001435 if module.sharedLibrary() {
1436 entries := &entriesList[0]
1437 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1438 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001439 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001440}
1441
Anton Hansson5fd5d242020-03-27 19:43:19 +00001442// The dist path of the stub artifacts
1443func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001444 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001445}
1446
Paul Duffin12ceb462019-12-24 20:31:31 +00001447// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001448func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001449 scopeProperties := module.scopeToProperties[apiScope]
1450 if scopeProperties.Sdk_version != nil {
1451 return proptools.String(scopeProperties.Sdk_version)
1452 }
1453
Jiyong Parkf1691d22021-03-29 20:11:58 +09001454 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001455 if sdkDep.hasStandardLibs() {
1456 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001457 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001458 } else {
1459 // Otherwise, use no system module.
1460 return "none"
1461 }
1462}
1463
Paul Duffin31310252020-11-20 21:26:20 +00001464func (module *SdkLibrary) distStem() string {
1465 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1466}
1467
Colin Cross986b69a2021-06-01 13:13:40 -07001468// distGroup returns the subdirectory of the dist path of the stub artifacts.
1469func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001470 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001471}
1472
Paul Duffin958806b2022-05-16 13:10:47 +00001473func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1474 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1475}
1476
Paul Duffind1b3a922020-01-22 11:57:20 +00001477func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001478 return ":" + module.latestApiModuleName(apiScope)
1479}
1480
1481func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
1482 return latestPrebuiltApiModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001483}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001484
Paul Duffind1b3a922020-01-22 11:57:20 +00001485func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001486 return ":" + module.latestRemovedApiModuleName(apiScope)
1487}
1488
1489func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
1490 return latestPrebuiltApiModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001491}
1492
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001493func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001494 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1495}
1496
1497func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1498 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001499}
1500
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001501func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1502 _, exists := c.GetApiLibraries()[module.Name()]
1503 return exists
1504}
1505
Anton Hansson944e77d2020-08-19 11:40:22 +01001506func childModuleVisibility(childVisibility []string) []string {
1507 if childVisibility == nil {
1508 // No child visibility set. The child will use the visibility of the sdk_library.
1509 return nil
1510 }
1511
1512 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1513 var visibility []string
1514 visibility = append(visibility, "//visibility:override")
1515 visibility = append(visibility, childVisibility...)
1516 return visibility
1517}
1518
Paul Duffin5df79302020-05-16 15:52:12 +01001519// Creates the implementation java library
1520func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001521 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1522
Paul Duffin5df79302020-05-16 15:52:12 +01001523 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001524 Name *string
1525 Visibility []string
1526 Instrument bool
1527 Libs []string
1528 Static_libs []string
1529 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001530 }{
1531 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001532 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001533 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1534 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001535 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1536 // addition of &module.properties below.
1537 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001538 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1539 // addition of &module.properties below.
1540 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1541 // Pass the apex_available settings down so that the impl library can be statically
1542 // embedded within a library that is added to an APEX. Needed for updatable-media.
1543 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001544 }
1545
1546 properties := []interface{}{
1547 &module.properties,
1548 &module.protoProperties,
1549 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001550 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001551 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001552 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001553 &props,
1554 module.sdkComponentPropertiesForChildLibrary(),
1555 }
1556 mctx.CreateModule(LibraryFactory, properties...)
1557}
1558
Jiyong Parkc678ad32018-04-10 13:07:10 +09001559// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001560func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001561 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001562 Name *string
1563 Visibility []string
1564 Srcs []string
1565 Installable *bool
1566 Sdk_version *string
1567 System_modules *string
1568 Patch_module *string
1569 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001570 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001571 Compile_dex *bool
1572 Java_version *string
1573 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001574 Srcs []string
1575 Javacflags []string
1576 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001577 Dist struct {
1578 Targets []string
1579 Dest *string
1580 Dir *string
1581 Tag *string
1582 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001583 }{}
1584
Paul Duffinc3091c82020-05-08 14:16:20 +01001585 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Anton Hansson944e77d2020-08-19 11:40:22 +01001586 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001587 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001588 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001589 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001590 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001591 props.System_modules = module.deviceProperties.System_modules
1592 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001593 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001594 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Anton Hanssondae54cd2021-04-21 16:30:10 +01001595 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001596 // The stub-annotations library contains special versions of the annotations
1597 // with CLASS retention policy, so that they're kept.
1598 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1599 props.Libs = append(props.Libs, "stub-annotations")
1600 }
Paul Duffina18abc22020-05-16 18:54:24 +01001601 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1602 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001603 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1604 // interop with older developer tools that don't support 1.9.
1605 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001606
1607 // The imports need to be compiled to dex if the java_sdk_library requests it.
1608 compileDex := module.dexProperties.Compile_dex
1609 if module.stubLibrariesCompiledForDex() {
1610 compileDex = proptools.BoolPtr(true)
Sundong Ahndd567f92018-07-31 17:19:11 +09001611 }
Paul Duffinf4600f62021-05-13 22:34:45 +01001612 props.Compile_dex = compileDex
Jiyong Parkc678ad32018-04-10 13:07:10 +09001613
Anton Hansson5fd5d242020-03-27 19:43:19 +00001614 // Dist the class jar artifact for sdk builds.
1615 if !Bool(module.sdkLibraryProperties.No_dist) {
1616 props.Dist.Targets = []string{"sdk", "win_sdk"}
Paul Duffin31310252020-11-20 21:26:20 +00001617 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
Anton Hansson5fd5d242020-03-27 19:43:19 +00001618 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1619 props.Dist.Tag = proptools.StringPtr(".jar")
1620 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001621
Paul Duffin859fe962020-05-15 10:20:31 +01001622 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001623}
1624
Paul Duffin6d0886e2020-04-07 18:49:53 +01001625// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001626// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001627func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001628 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001629 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001630 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001631 Srcs []string
1632 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001633 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001634 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001635 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001636 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001637 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001638 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001639 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001640 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001641 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001642 Merge_annotations_dirs []string
1643 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001644 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001645 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001646 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001647 Current ApiToCheck
1648 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001649
1650 Api_lint struct {
1651 Enabled *bool
1652 New_since *string
1653 Baseline_file *string
1654 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001655 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001656 Aidl struct {
1657 Include_dirs []string
1658 Local_include_dirs []string
1659 }
Paul Duffin040e9062020-11-23 17:41:36 +00001660 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001661 }{}
1662
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001663 // The stubs source processing uses the same compile time classpath when extracting the
1664 // API from the implementation library as it does when compiling it. i.e. the same
1665 // * sdk version
1666 // * system_modules
1667 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001668
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001669 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001670 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001671 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001672 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001673 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001674 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001675 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001676 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001677 // A droiddoc module has only one Libs property and doesn't distinguish between
1678 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001679 props.Libs = module.properties.Libs
1680 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001681 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001682 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1683 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1684 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001685
Paul Duffine22c2ab2020-05-20 19:35:27 +01001686 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001687 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1688 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1689
Paul Duffin6d0886e2020-04-07 18:49:53 +01001690 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001691 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001692 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001693 }
1694 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001695 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001696 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1697 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001698 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001699 disabledWarnings := []string{
Paul Duffin235ffff2019-12-24 10:41:30 +00001700 "BroadcastBehavior",
Paul Duffin235ffff2019-12-24 10:41:30 +00001701 "DeprecationMismatch",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001702 "HiddenSuperclass",
Paul Duffin235ffff2019-12-24 10:41:30 +00001703 "HiddenTypeParameter",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001704 "MissingPermission",
1705 "SdkConstant",
Paul Duffin235ffff2019-12-24 10:41:30 +00001706 "Todo",
1707 "Typo",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001708 "UnavailableSymbol",
Paul Duffin235ffff2019-12-24 10:41:30 +00001709 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001710 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001711
Paul Duffin6877e6d2020-09-25 19:59:14 +01001712 // Output Javadoc comments for public scope.
1713 if apiScope == apiScopePublic {
1714 props.Output_javadoc_comments = proptools.BoolPtr(true)
1715 }
1716
Paul Duffin1fb487d2020-04-07 18:50:10 +01001717 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001718 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001719 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001720 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001721
Paul Duffin15f34ef2020-07-20 18:04:44 +01001722 // List of APIs identified from the provided source files are created. They are later
1723 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1724 // last-released (a.k.a numbered) list of API.
1725 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1726 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1727 apiDir := module.getApiDir()
1728 currentApiFileName = path.Join(apiDir, currentApiFileName)
1729 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001730
Paul Duffin15f34ef2020-07-20 18:04:44 +01001731 // check against the not-yet-release API
1732 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1733 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001734
Paul Duffin958806b2022-05-16 13:10:47 +00001735 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001736 // check against the latest released API
1737 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001738 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001739 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1740 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1741 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001742 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1743 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001744
Paul Duffin15f34ef2020-07-20 18:04:44 +01001745 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1746 // Enable api lint.
1747 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1748 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001749
Paul Duffin15f34ef2020-07-20 18:04:44 +01001750 // If it exists then pass a lint-baseline.txt through to droidstubs.
1751 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1752 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1753 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1754 if err != nil {
1755 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1756 }
1757 if len(paths) == 1 {
1758 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1759 } else if len(paths) != 0 {
1760 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001761 }
1762 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001763 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001764
Paul Duffin15f34ef2020-07-20 18:04:44 +01001765 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001766 // Dist the api txt and removed api txt artifacts for sdk builds.
1767 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1768 for _, p := range []struct {
1769 tag string
1770 pattern string
1771 }{
1772 {tag: ".api.txt", pattern: "%s.txt"},
1773 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1774 } {
1775 props.Dists = append(props.Dists, android.Dist{
1776 Targets: []string{"sdk", "win_sdk"},
1777 Dir: distDir,
1778 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1779 Tag: proptools.StringPtr(p.tag),
1780 })
1781 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001782 }
1783
Jihoon Kangd48abd52023-02-02 22:32:31 +00001784 mctx.CreateModule(DroidstubsFactory, &props).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001785}
1786
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001787func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1788 props := struct {
1789 Name *string
1790 Visibility []string
1791 Api_contributions []string
1792 Libs []string
1793 Static_libs []string
1794 Dep_api_srcs *string
1795 }{}
1796
1797 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
1798 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
1799
1800 apiContributions := []string{}
1801
1802 // Api surfaces are not independent of each other, but have subset relationships,
1803 // and so does the api files. To generate from-text stubs for api surfaces other than public,
1804 // all subset api domains' api_contriubtions must be added as well.
1805 scope := apiScope
1806 for scope != nil {
1807 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
1808 scope = scope.extends
1809 }
1810
1811 props.Api_contributions = apiContributions
1812 props.Libs = module.properties.Libs
1813 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
1814 props.Libs = append(props.Libs, "stub-annotations")
1815 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
1816 props.Dep_api_srcs = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + ".from-text")
1817
1818 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
1819 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
1820 if apiScope.kind == android.SdkModule {
1821 props.Dep_api_srcs = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
1822 }
1823
1824 mctx.CreateModule(ApiLibraryFactory, &props)
1825}
1826
Paul Duffin958806b2022-05-16 13:10:47 +00001827func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
1828 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
1829}
1830
Paul Duffinea8f8082021-06-24 13:25:57 +01001831// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09001832func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1833 depTag := mctx.OtherModuleDependencyTag(dep)
1834 if depTag == xmlPermissionsFileTag {
1835 return true
1836 }
1837 return module.Library.DepIsInSameApex(mctx, dep)
1838}
1839
Paul Duffinea8f8082021-06-24 13:25:57 +01001840// Implements android.ApexModule
1841func (module *SdkLibrary) UniqueApexVariations() bool {
1842 return module.uniqueApexVariations()
1843}
1844
Jiyong Parkc678ad32018-04-10 13:07:10 +09001845// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001846func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001847 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00001848 var moduleMinApiLevelStr = moduleMinApiLevel.String()
1849 if moduleMinApiLevel == android.NoneApiLevel {
1850 moduleMinApiLevelStr = "current"
1851 }
Jiyong Parke3833882020-02-17 17:28:10 +09001852 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001853 Name *string
1854 Lib_name *string
1855 Apex_available []string
1856 On_bootclasspath_since *string
1857 On_bootclasspath_before *string
1858 Min_device_sdk *string
1859 Max_device_sdk *string
1860 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09001861 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00001862 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
1863 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1864 Apex_available: module.ApexProperties.Apex_available,
1865 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
1866 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
1867 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
1868 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
1869 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001870 }
Jiyong Parke3833882020-02-17 17:28:10 +09001871
Jiyong Parke3833882020-02-17 17:28:10 +09001872 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001873}
1874
Jiyong Parkf1691d22021-03-29 20:11:58 +09001875func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09001876 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001877 var kind android.SdkKind
1878 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09001879 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001880 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001881 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001882 // We don't have prebuilt SDK for the specific sdkVersion.
1883 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09001884 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001885 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001886 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001887
1888 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001889 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001890 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001891 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001892 if ctx.Config().AllowMissingDependencies() {
1893 return android.Paths{android.PathForSource(ctx, jar)}
1894 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001895 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001896 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001897 return nil
1898 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001899 return android.Paths{jarPath.Path()}
1900}
1901
Colin Crossaede88c2020-08-11 12:17:01 -07001902// 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 +01001903//
1904// If either this or the other module are on the platform then this will return
1905// false.
Colin Cross56a83212020-09-15 18:30:11 -07001906func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
1907 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1908 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09001909 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01001910}
1911
Jiyong Parkf1691d22021-03-29 20:11:58 +09001912func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001913 // If the client doesn't set sdk_version, but if this library prefers stubs over
1914 // the impl library, let's provide the widest API surface possible. To do so,
1915 // force override sdk_version to module_current so that the closest possible API
1916 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09001917 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09001918 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09001919 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001920
Paul Duffindaaa3322020-05-26 18:13:57 +01001921 // Only provide access to the implementation library if it is actually built.
1922 if module.requiresRuntimeImplementationLibrary() {
1923 // Check any special cases for java_sdk_library.
1924 //
1925 // Only allow access to the implementation library in the following condition:
1926 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001927 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001928 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001929 if headerJars {
1930 return module.HeaderJars()
1931 } else {
1932 return module.ImplementationJars()
1933 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001934 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001935 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001936
Paul Duffin23970f42020-05-20 14:20:02 +01001937 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001938}
1939
Sundong Ahn241cd372018-07-13 16:16:44 +09001940// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001941func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001942 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1943}
1944
1945// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001946func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001947 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001948}
1949
Colin Cross571cccf2019-02-04 11:22:08 -08001950var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1951
Jiyong Park82484c02018-04-23 21:41:26 +09001952func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001953 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001954 return &[]string{}
1955 }).(*[]string)
1956}
1957
Paul Duffin749f98f2019-12-30 17:23:46 +00001958func (module *SdkLibrary) getApiDir() string {
1959 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1960}
1961
Jiyong Parkc678ad32018-04-10 13:07:10 +09001962// For a java_sdk_library module, create internal modules for stubs, docs,
1963// runtime libs and xml file. If requested, the stubs and docs are created twice
1964// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001965func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1966 // If the module has been disabled then don't create any child modules.
1967 if !module.Enabled() {
1968 return
1969 }
1970
Paul Duffina18abc22020-05-16 18:54:24 +01001971 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001972 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001973 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001974 }
1975
Paul Duffin37e0b772019-12-30 17:20:10 +00001976 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001977 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001978 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00001979 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001980 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001981
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001982 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09001983
Paul Duffin3375e352020-04-28 10:44:03 +01001984 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001985
Paul Duffin749f98f2019-12-30 17:23:46 +00001986 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001987 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001988 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001989 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001990 p := android.ExistentPathForSource(mctx, path)
1991 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07001992 if mctx.Config().AllowMissingDependencies() {
1993 mctx.AddMissingDependencies([]string{path})
1994 } else {
1995 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1996 missingCurrentApi = true
1997 }
Inseob Kim8098faa2019-03-18 10:19:51 +09001998 }
1999 }
2000 }
2001
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002002 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002003 script := "build/soong/scripts/gen-java-current-api-files.sh"
2004 p := android.ExistentPathForSource(mctx, script)
2005
2006 if !p.Valid() {
2007 panic(fmt.Sprintf("script file %s doesn't exist", script))
2008 }
2009
2010 mctx.ModuleErrorf("One or more current api files are missing. "+
2011 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002012 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002013 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002014 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002015 return
2016 }
2017
Paul Duffin3375e352020-04-28 10:44:03 +01002018 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002019 // Use the stubs source name for legacy reasons.
2020 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002021
Paul Duffind1b3a922020-01-22 11:57:20 +00002022 module.createStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002023
2024 if module.contributesToApiSurface(mctx.Config()) {
2025 module.createApiLibrary(mctx, scope)
2026 }
Inseob Kimc0907f12019-02-08 21:00:45 +09002027 }
2028
Paul Duffindfa131e2020-05-15 20:37:11 +01002029 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002030 // Create child module to create an implementation library.
2031 //
2032 // This temporarily creates a second implementation library that can be explicitly
2033 // referenced.
2034 //
2035 // TODO(b/156618935) - update comment once only one implementation library is created.
2036 module.createImplLibrary(mctx)
2037
Paul Duffindfa131e2020-05-15 20:37:11 +01002038 // Only create an XML permissions file that declares the library as being usable
2039 // as a shared library if required.
2040 if module.sharedLibrary() {
2041 module.createXmlFile(mctx)
2042 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002043
2044 // record java_sdk_library modules so that they are exported to make
2045 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2046 javaSdkLibrariesLock.Lock()
2047 defer javaSdkLibrariesLock.Unlock()
2048 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2049 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002050
Paul Duffin77590a82022-04-28 14:13:30 +00002051 // 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 +01002052 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002053 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002054}
2055
2056func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002057 module.addHostAndDeviceProperties()
2058 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002059
Paul Duffin71b33cc2021-06-23 11:39:47 +01002060 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002061
Paul Duffina18abc22020-05-16 18:54:24 +01002062 module.properties.Installable = proptools.BoolPtr(true)
2063 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002064}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002065
Paul Duffindfa131e2020-05-15 20:37:11 +01002066func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2067 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2068}
2069
Jiyong Park932cdfe2020-05-28 00:19:53 +09002070func (module *SdkLibrary) defaultsToStubs() bool {
2071 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2072}
2073
Paul Duffin1b1e8062020-05-08 13:44:43 +01002074// Defines how to name the individual component modules the sdk library creates.
2075type sdkLibraryComponentNamingScheme interface {
2076 stubsLibraryModuleName(scope *apiScope, baseName string) string
2077
2078 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002079
2080 apiLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002081}
2082
2083type defaultNamingScheme struct {
2084}
2085
2086func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2087 return scope.stubsLibraryModuleName(baseName)
2088}
2089
2090func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2091 return scope.stubsSourceModuleName(baseName)
2092}
2093
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002094func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2095 return scope.apiLibraryModuleName(baseName)
2096}
2097
Paul Duffin1b1e8062020-05-08 13:44:43 +01002098var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2099
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002100func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002101 // This suffix-based approach is fragile and could potentially mis-trigger.
2102 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01002103 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
2104 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2105 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2106 return false, javaPlatform
2107 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002108 return true, javaSdk
2109 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002110 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002111 return true, javaSystem
2112 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002113 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002114 return true, javaModule
2115 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002116 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002117 return true, javaSystem
2118 }
2119 return false, javaPlatform
2120}
2121
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002122// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2123// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2124// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2125// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2126// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002127func SdkLibraryFactory() android.Module {
2128 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002129
2130 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002131 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002132
Inseob Kimc0907f12019-02-08 21:00:45 +09002133 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002134 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002135 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002136
2137 // Initialize the map from scope to scope specific properties.
2138 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2139 for _, scope := range allApiScopes {
2140 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2141 }
2142 module.scopeToProperties = scopeToProperties
2143
Paul Duffin4911a892020-04-29 23:35:13 +01002144 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002145 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002146 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2147 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2148
Paul Duffin1b1e8062020-05-08 13:44:43 +01002149 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002150 // If no implementation is required then it cannot be used as a shared library
2151 // either.
2152 if !module.requiresRuntimeImplementationLibrary() {
2153 // If shared_library has been explicitly set to true then it is incompatible
2154 // with api_only: true.
2155 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2156 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2157 }
2158 // Set shared_library: false.
2159 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2160 }
2161
Paul Duffin1b1e8062020-05-08 13:44:43 +01002162 if module.initCommonAfterDefaultsApplied(ctx) {
2163 module.CreateInternalModules(ctx)
2164 }
2165 })
Zi Wangb2179e32023-01-31 15:53:30 -08002166 android.InitBazelModule(module)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002167 return module
2168}
Colin Cross79c7c262019-04-17 11:11:46 -07002169
Zi Wangb2179e32023-01-31 15:53:30 -08002170type bazelSdkLibraryAttributes struct {
2171 Public bazel.StringAttribute
2172 System bazel.StringAttribute
2173 Test bazel.StringAttribute
2174 Module_lib bazel.StringAttribute
2175 System_server bazel.StringAttribute
2176}
2177
2178// java_sdk_library bp2build converter
2179func (module *SdkLibrary) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
2180 if ctx.ModuleType() != "java_sdk_library" {
2181 return
2182 }
2183
2184 nameToAttr := make(map[string]bazel.StringAttribute)
2185
2186 for _, scope := range module.getGeneratedApiScopes(ctx) {
2187 apiSurfaceFile := path.Join(module.getApiDir(), scope.apiFilePrefix+"current.txt")
2188 var scopeStringAttribute bazel.StringAttribute
2189 scopeStringAttribute.SetValue(apiSurfaceFile)
2190 nameToAttr[scope.name] = scopeStringAttribute
2191 }
2192
2193 attrs := bazelSdkLibraryAttributes{
2194 Public: nameToAttr["public"],
2195 System: nameToAttr["system"],
2196 Test: nameToAttr["test"],
2197 Module_lib: nameToAttr["module-lib"],
2198 System_server: nameToAttr["system-server"],
2199 }
2200 props := bazel.BazelTargetModuleProperties{
2201 Rule_class: "java_sdk_library",
2202 Bzl_load_location: "//build/bazel/rules/java:sdk_library.bzl",
2203 }
2204
2205 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs)
2206}
2207
Colin Cross79c7c262019-04-17 11:11:46 -07002208//
2209// SDK library prebuilts
2210//
2211
Paul Duffin56d44902020-01-31 13:36:25 +00002212// Properties associated with each api scope.
2213type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002214 Jars []string `android:"path"`
2215
2216 Sdk_version *string
2217
Colin Cross79c7c262019-04-17 11:11:46 -07002218 // List of shared java libs that this module has dependencies to
2219 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002220
Paul Duffinc8782502020-04-29 20:45:27 +01002221 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002222 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002223
2224 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002225 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002226
2227 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002228 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002229
2230 // Annotation zip
2231 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002232}
2233
Paul Duffin56d44902020-01-31 13:36:25 +00002234type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002235 // List of shared java libs, common to all scopes, that this module has
2236 // dependencies to
2237 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002238
2239 // If set to true, compile dex files for the stubs. Defaults to false.
2240 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002241
2242 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002243 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00002244}
2245
Paul Duffineedc5d52020-06-12 17:46:39 +01002246type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002247 android.ModuleBase
2248 android.DefaultableModuleBase
2249 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002250 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002251
Paul Duffin37856732021-02-26 14:24:15 +00002252 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002253 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002254
Colin Cross79c7c262019-04-17 11:11:46 -07002255 properties sdkLibraryImportProperties
2256
Paul Duffin46a26a82020-04-07 19:27:04 +01002257 // Map from api scope to the scope specific property structure.
2258 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2259
Paul Duffin56d44902020-01-31 13:36:25 +00002260 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002261
2262 // The reference to the implementation library created by the source module.
2263 // Is nil if the source module does not exist.
2264 implLibraryModule *Library
2265
2266 // The reference to the xml permissions module created by the source module.
2267 // Is nil if the source module does not exist.
2268 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002269
Jeongik Chad5fe8782021-07-08 01:13:11 +09002270 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002271 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09002272
2273 // Expected install file path of the source module(sdk_library)
2274 // or dex implementation jar obtained from the prebuilt_apex, if any.
2275 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002276}
2277
Paul Duffineedc5d52020-06-12 17:46:39 +01002278var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002279
Paul Duffin46a26a82020-04-07 19:27:04 +01002280// The type of a structure that contains a field of type sdkLibraryScopeProperties
2281// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002282//
2283// struct {
2284// Public sdkLibraryScopeProperties
2285// System sdkLibraryScopeProperties
2286// ...
2287// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002288var allScopeStructType = createAllScopePropertiesStructType()
2289
2290// Dynamically create a structure type for each apiscope in allApiScopes.
2291func createAllScopePropertiesStructType() reflect.Type {
2292 var fields []reflect.StructField
2293 for _, apiScope := range allApiScopes {
2294 field := reflect.StructField{
2295 Name: apiScope.fieldName,
2296 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2297 }
2298 fields = append(fields, field)
2299 }
2300
2301 return reflect.StructOf(fields)
2302}
2303
2304// Create an instance of the scope specific structure type and return a map
2305// from apiscope to a pointer to each scope specific field.
2306func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2307 allScopePropertiesPtr := reflect.New(allScopeStructType)
2308 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2309 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2310
2311 for _, apiScope := range allApiScopes {
2312 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2313 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2314 }
2315
2316 return allScopePropertiesPtr.Interface(), scopeProperties
2317}
2318
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002319// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002320func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002321 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002322
Paul Duffin46a26a82020-04-07 19:27:04 +01002323 allScopeProperties, scopeToProperties := createPropertiesInstance()
2324 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002325 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002326
Paul Duffinc3091c82020-05-08 14:16:20 +01002327 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002328 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002329
Paul Duffin0bdcb272020-02-06 15:24:57 +00002330 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002331 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002332 InitJavaModule(module, android.HostAndDeviceSupported)
2333
Paul Duffin1b1e8062020-05-08 13:44:43 +01002334 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2335 if module.initCommonAfterDefaultsApplied(mctx) {
2336 module.createInternalModules(mctx)
2337 }
2338 })
Colin Cross79c7c262019-04-17 11:11:46 -07002339 return module
2340}
2341
Paul Duffin630b11e2021-07-15 13:35:26 +01002342var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2343
2344func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2345 return module.properties.Permitted_packages
2346}
2347
Paul Duffineedc5d52020-06-12 17:46:39 +01002348func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002349 return &module.prebuilt
2350}
2351
Paul Duffineedc5d52020-06-12 17:46:39 +01002352func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002353 return module.prebuilt.Name(module.ModuleBase.Name())
2354}
2355
Paul Duffineedc5d52020-06-12 17:46:39 +01002356func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002357
Paul Duffin50061512020-01-21 16:31:05 +00002358 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002359 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002360 module.prebuilt.ForcePrefer()
2361 }
2362
Paul Duffin46a26a82020-04-07 19:27:04 +01002363 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002364 if len(scopeProperties.Jars) == 0 {
2365 continue
2366 }
2367
Paul Duffinbbb546b2020-04-09 00:07:11 +01002368 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002369
Paul Duffin0f8faff2020-05-20 16:18:00 +01002370 if len(scopeProperties.Stub_srcs) > 0 {
2371 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2372 }
Paul Duffin56d44902020-01-31 13:36:25 +00002373 }
Colin Cross79c7c262019-04-17 11:11:46 -07002374
2375 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2376 javaSdkLibrariesLock.Lock()
2377 defer javaSdkLibrariesLock.Unlock()
2378 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2379}
2380
Paul Duffineedc5d52020-06-12 17:46:39 +01002381func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002382 // Creates a java import for the jar with ".stubs" suffix
2383 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002384 Name *string
2385 Sdk_version *string
2386 Libs []string
2387 Jars []string
Paul Duffin1267d872021-04-16 17:21:36 +01002388 Compile_dex *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002389
2390 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002391 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002392 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002393 props.Sdk_version = scopeProperties.Sdk_version
2394 // Prepend any of the libs from the legacy public properties to the libs for each of the
2395 // scopes to avoid having to duplicate them in each scope.
2396 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2397 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002398
Paul Duffin38b57852020-05-13 16:08:09 +01002399 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002400 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002401
Paul Duffin1267d872021-04-16 17:21:36 +01002402 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002403 compileDex := module.properties.Compile_dex
2404 if module.stubLibrariesCompiledForDex() {
2405 compileDex = proptools.BoolPtr(true)
2406 }
2407 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002408
Paul Duffin859fe962020-05-15 10:20:31 +01002409 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002410}
2411
Paul Duffineedc5d52020-06-12 17:46:39 +01002412func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002413 props := struct {
Paul Duffinbf4de042022-09-27 12:41:52 +01002414 Name *string
2415 Srcs []string
2416
2417 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002418 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002419 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002420 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002421
2422 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002423 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2424
2425 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002426}
2427
Paul Duffin44f1d842020-06-26 20:17:02 +01002428// Add the dependencies on the child module in the component deps mutator so that it
2429// creates references to the prebuilt and not the source modules.
2430func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002431 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002432 if len(scopeProperties.Jars) == 0 {
2433 continue
2434 }
2435
2436 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002437 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002438
2439 if len(scopeProperties.Stub_srcs) > 0 {
2440 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002441 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002442 }
Paul Duffin56d44902020-01-31 13:36:25 +00002443 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002444}
2445
2446// Add other dependencies as normal.
2447func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002448
2449 implName := module.implLibraryModuleName()
2450 if ctx.OtherModuleExists(implName) {
2451 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2452
2453 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2454 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2455 // Add dependency to the rule for generating the xml permissions file
2456 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2457 }
2458 }
Colin Cross79c7c262019-04-17 11:11:46 -07002459}
2460
Jiakai Zhang204356f2021-09-09 08:12:46 +00002461func (module *SdkLibraryImport) AndroidMkEntries() []android.AndroidMkEntries {
2462 // For an SDK library imported from a prebuilt APEX, we don't need a Make module for itself, as we
2463 // don't need to install it. However, we need to add its dexpreopt outputs as sub-modules, if it
2464 // is preopted.
2465 dexpreoptEntries := module.dexpreopter.AndroidMkEntriesForApex()
2466 return append(dexpreoptEntries, android.AndroidMkEntries{Disabled: true})
2467}
2468
Jiyong Park45bf82e2020-12-15 22:29:02 +09002469var _ android.ApexModule = (*SdkLibraryImport)(nil)
2470
2471// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002472func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2473 depTag := mctx.OtherModuleDependencyTag(dep)
2474 if depTag == xmlPermissionsFileTag {
2475 return true
2476 }
2477
2478 // None of the other dependencies of the java_sdk_library_import are in the same apex
2479 // as the one that references this module.
2480 return false
2481}
2482
Jiyong Park45bf82e2020-12-15 22:29:02 +09002483// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002484func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2485 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002486 // we don't check prebuilt modules for sdk_version
2487 return nil
2488}
2489
Paul Duffinea8f8082021-06-24 13:25:57 +01002490// Implements android.ApexModule
2491func (module *SdkLibraryImport) UniqueApexVariations() bool {
2492 return module.uniqueApexVariations()
2493}
2494
Paul Duffin09817d62022-04-28 17:45:11 +01002495// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002496func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2497 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002498}
2499
2500var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2501
Paul Duffineedc5d52020-06-12 17:46:39 +01002502func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002503 paths, err := module.commonOutputFiles(tag)
2504 if paths != nil || err != nil {
2505 return paths, err
2506 }
2507 if module.implLibraryModule != nil {
2508 return module.implLibraryModule.OutputFiles(tag)
2509 } else {
2510 return nil, nil
2511 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002512}
2513
Paul Duffineedc5d52020-06-12 17:46:39 +01002514func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002515 module.generateCommonBuildActions(ctx)
2516
Jeongik Chad5fe8782021-07-08 01:13:11 +09002517 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2518 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2519
Paul Duffin0f8faff2020-05-20 16:18:00 +01002520 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002521 ctx.VisitDirectDeps(func(to android.Module) {
2522 tag := ctx.OtherModuleDependencyTag(to)
2523
Paul Duffin0f8faff2020-05-20 16:18:00 +01002524 // Extract information from any of the scope specific dependencies.
2525 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2526 apiScope := scopeTag.apiScope
2527 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2528
2529 // Extract information from the dependency. The exact information extracted
2530 // is determined by the nature of the dependency which is determined by the tag.
2531 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002532 } else if tag == implLibraryTag {
2533 if implLibrary, ok := to.(*Library); ok {
2534 module.implLibraryModule = implLibrary
2535 } else {
2536 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2537 }
2538 } else if tag == xmlPermissionsFileTag {
2539 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2540 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2541 } else {
2542 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2543 }
Colin Cross79c7c262019-04-17 11:11:46 -07002544 }
2545 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002546
2547 // Populate the scope paths with information from the properties.
2548 for apiScope, scopeProperties := range module.scopeProperties {
2549 if len(scopeProperties.Jars) == 0 {
2550 continue
2551 }
2552
2553 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002554 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002555 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2556 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2557 }
Paul Duffin39853512021-02-26 11:09:39 +00002558
2559 if ctx.Device() {
2560 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2561 // obtained from the associated deapexer module.
2562 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2563 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002564 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01002565 di := android.FindDeapexerProviderForModule(ctx)
2566 if di == nil {
2567 return // An error has been reported by FindDeapexerProviderForModule.
2568 }
Jiakai Zhang81e46812023-02-08 21:56:07 +08002569 dexJarFileApexRootRelative := apexRootRelativePathToJavaLib(module.BaseModuleName())
2570 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002571 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2572 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002573 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002574 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002575 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002576 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002577
Jiakai Zhang204356f2021-09-09 08:12:46 +00002578 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2579 module.dexpreopter.isSDKLibrary = true
2580 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002581
2582 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2583 module.dexpreopter.inputProfilePathOnHost = profilePath
2584 }
2585
2586 // Dexpreopting.
Jiakai Zhang204356f2021-09-09 08:12:46 +00002587 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002588 } else {
2589 // This should never happen as a variant for a prebuilt_apex is only created if the
2590 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002591 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002592 }
2593 }
2594 }
Colin Cross79c7c262019-04-17 11:11:46 -07002595}
2596
Jiyong Parkf1691d22021-03-29 20:11:58 +09002597func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002598
2599 // For consistency with SdkLibrary make the implementation jar available to libraries that
2600 // are within the same APEX.
2601 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002602 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002603 if headerJars {
2604 return implLibraryModule.HeaderJars()
2605 } else {
2606 return implLibraryModule.ImplementationJars()
2607 }
2608 }
2609
Paul Duffin23970f42020-05-20 14:20:02 +01002610 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002611}
2612
Colin Cross79c7c262019-04-17 11:11:46 -07002613// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002614func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002615 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002616 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002617}
2618
2619// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002620func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002621 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002622 return module.sdkJars(ctx, sdkVersion, false)
2623}
2624
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002625// to satisfy UsesLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002626func (module *SdkLibraryImport) DexJarBuildPath() OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002627 // The dex implementation jar extracted from the .apex file should be used in preference to the
2628 // source.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002629 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002630 return module.dexJarFile
2631 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002632 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002633 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002634 } else {
2635 return module.implLibraryModule.DexJarBuildPath()
2636 }
2637}
2638
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002639// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002640func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002641 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002642}
2643
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002644// to satisfy UsesLibraryDependency interface
2645func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2646 return nil
2647}
2648
Paul Duffineedc5d52020-06-12 17:46:39 +01002649// to satisfy apex.javaDependency interface
2650func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2651 if module.implLibraryModule == nil {
2652 return nil
2653 } else {
2654 return module.implLibraryModule.JacocoReportClassesFile()
2655 }
2656}
2657
2658// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002659func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2660 if module.implLibraryModule == nil {
2661 return LintDepSets{}
2662 } else {
2663 return module.implLibraryModule.LintDepSets()
2664 }
2665}
2666
Spandan Das17854f52022-01-14 21:19:14 +00002667func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002668 if module.implLibraryModule == nil {
2669 return false
2670 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002671 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002672 }
2673}
2674
Spandan Das17854f52022-01-14 21:19:14 +00002675func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002676 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002677 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002678 }
2679}
2680
Colin Cross08dca382020-07-21 20:31:17 -07002681// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002682func (module *SdkLibraryImport) Stem() string {
2683 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002684}
Jiyong Parke3833882020-02-17 17:28:10 +09002685
Paul Duffin44b481b2020-06-17 16:59:43 +01002686var _ ApexDependency = (*SdkLibraryImport)(nil)
2687
2688// to satisfy java.ApexDependency interface
2689func (module *SdkLibraryImport) HeaderJars() android.Paths {
2690 if module.implLibraryModule == nil {
2691 return nil
2692 } else {
2693 return module.implLibraryModule.HeaderJars()
2694 }
2695}
2696
2697// to satisfy java.ApexDependency interface
2698func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2699 if module.implLibraryModule == nil {
2700 return nil
2701 } else {
2702 return module.implLibraryModule.ImplementationAndResourcesJars()
2703 }
2704}
2705
Jiakai Zhang204356f2021-09-09 08:12:46 +00002706// to satisfy java.DexpreopterInterface interface
2707func (module *SdkLibraryImport) IsInstallable() bool {
2708 return true
2709}
2710
Paul Duffinfef55002021-06-17 14:56:05 +01002711var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2712
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002713func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002714 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08002715 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01002716}
2717
Jiyong Parke3833882020-02-17 17:28:10 +09002718// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09002719type sdkLibraryXml struct {
2720 android.ModuleBase
2721 android.DefaultableModuleBase
2722 android.ApexModuleBase
2723
2724 properties sdkLibraryXmlProperties
2725
2726 outputFilePath android.OutputPath
2727 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002728
2729 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002730}
2731
2732type sdkLibraryXmlProperties struct {
2733 // canonical name of the lib
2734 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002735
2736 // Signals that this shared library is part of the bootclasspath starting
2737 // on the version indicated in this attribute.
2738 //
2739 // This will make platforms at this level and above to ignore
2740 // <uses-library> tags with this library name because the library is already
2741 // available
2742 On_bootclasspath_since *string
2743
2744 // Signals that this shared library was part of the bootclasspath before
2745 // (but not including) the version indicated in this attribute.
2746 //
2747 // The system will automatically add a <uses-library> tag with this library to
2748 // apps that target any SDK less than the version indicated in this attribute.
2749 On_bootclasspath_before *string
2750
2751 // Indicates that PackageManager should ignore this shared library if the
2752 // platform is below the version indicated in this attribute.
2753 //
2754 // This means that the device won't recognise this library as installed.
2755 Min_device_sdk *string
2756
2757 // Indicates that PackageManager should ignore this shared library if the
2758 // platform is above the version indicated in this attribute.
2759 //
2760 // This means that the device won't recognise this library as installed.
2761 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00002762
2763 // The SdkLibrary's min api level as a string
2764 //
2765 // This value comes from the ApiLevel of the MinSdkVersion property.
2766 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09002767}
2768
2769// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2770// Not to be used directly by users. java_sdk_library internally uses this.
2771func sdkLibraryXmlFactory() android.Module {
2772 module := &sdkLibraryXml{}
2773
2774 module.AddProperties(&module.properties)
2775
2776 android.InitApexModule(module)
2777 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2778
2779 return module
2780}
2781
Colin Crossaede88c2020-08-11 12:17:01 -07002782func (module *sdkLibraryXml) UniqueApexVariations() bool {
2783 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2784 // mounted APEX, which contains the name of the APEX.
2785 return true
2786}
2787
Jiyong Parke3833882020-02-17 17:28:10 +09002788// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002789func (module *sdkLibraryXml) BaseDir() string {
2790 return "etc"
2791}
2792
2793// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002794func (module *sdkLibraryXml) SubDir() string {
2795 return "permissions"
2796}
2797
2798// from android.PrebuiltEtcModule
2799func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2800 return module.outputFilePath
2801}
2802
2803// from android.ApexModule
2804func (module *sdkLibraryXml) AvailableFor(what string) bool {
2805 return true
2806}
2807
2808func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2809 // do nothing
2810}
2811
Jiyong Park45bf82e2020-12-15 22:29:02 +09002812var _ android.ApexModule = (*sdkLibraryXml)(nil)
2813
2814// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002815func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2816 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002817 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2818 return nil
2819}
2820
Jiyong Parke3833882020-02-17 17:28:10 +09002821// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002822func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002823 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002824 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002825 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002826 // In most cases, this works fine. But when apex_name is set or override_apex is used
2827 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002828 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002829 }
2830 partition := "system"
2831 if module.SocSpecific() {
2832 partition = "vendor"
2833 } else if module.DeviceSpecific() {
2834 partition = "odm"
2835 } else if module.ProductSpecific() {
2836 partition = "product"
2837 } else if module.SystemExtSpecific() {
2838 partition = "system_ext"
2839 }
2840 return "/" + partition + "/framework/" + implName + ".jar"
2841}
2842
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002843func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
2844 if value == nil {
2845 return ""
2846 }
2847 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
2848 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002849 // attributes in bp files have underscores but in the xml have dashes.
2850 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002851 return ""
2852 }
Pedro Loureirob638c622021-12-22 15:28:05 +00002853 if apiLevel.IsCurrent() {
2854 // passing "current" would always mean a future release, never the current (or the current in
2855 // progress) which means some conditions would never be triggered.
2856 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
2857 `"current" is not an allowed value for this attribute`)
2858 return ""
2859 }
Pedro Loureiro48991222022-06-17 20:01:21 +00002860 // "safeValue" is safe because it translates finalized codenames to a string
2861 // with their SDK int.
2862 safeValue := apiLevel.String()
2863 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002864}
2865
2866// formats an attribute for the xml permissions file if the value is not null
2867// returns empty string otherwise
2868func formattedOptionalAttribute(attrName string, value *string) string {
2869 if value == nil {
2870 return ""
2871 }
2872 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
2873}
2874
2875func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
2876 libName := proptools.String(module.properties.Lib_name)
2877 libNameAttr := formattedOptionalAttribute("name", &libName)
2878 filePath := module.implPath(ctx)
2879 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002880 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
2881 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
2882 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
2883 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002884 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
2885 // 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 +00002886 var libraryTag string
2887 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002888 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00002889 } else {
2890 libraryTag = ` <library\n`
2891 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002892
2893 return strings.Join([]string{
2894 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
2895 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
2896 `\n`,
2897 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
2898 ` you may not use this file except in compliance with the License.\n`,
2899 ` You may obtain a copy of the License at\n`,
2900 `\n`,
2901 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
2902 `\n`,
2903 ` Unless required by applicable law or agreed to in writing, software\n`,
2904 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
2905 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
2906 ` See the License for the specific language governing permissions and\n`,
2907 ` limitations under the License.\n`,
2908 `-->\n`,
2909 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00002910 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002911 libNameAttr,
2912 filePathAttr,
2913 implicitFromAttr,
2914 implicitUntilAttr,
2915 minSdkAttr,
2916 maxSdkAttr,
2917 ` />\n`,
2918 `</permissions>\n`}, "")
2919}
2920
Jiyong Parke3833882020-02-17 17:28:10 +09002921func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07002922 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
2923
Jiyong Parke3833882020-02-17 17:28:10 +09002924 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002925 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002926 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002927
2928 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08002929 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002930 rule.Command().
2931 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2932 Output(module.outputFilePath)
2933
Colin Crossf1a035e2020-11-16 17:32:30 -08002934 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09002935
2936 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2937}
2938
2939func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07002940 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00002941 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09002942 Disabled: true,
2943 }}
2944 }
2945
satayev8f088b02021-12-06 11:40:46 +00002946 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09002947 Class: "ETC",
2948 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2949 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07002950 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09002951 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08002952 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09002953 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2954 },
2955 },
2956 }}
2957}
Paul Duffindd46f712020-02-10 13:37:10 +00002958
Pedro Loureiroc3621422021-09-28 15:40:23 +00002959func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
2960 module.validateAtLeastTAttributes(ctx)
2961 module.validateMinAndMaxDeviceSdk(ctx)
2962 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
2963 module.validateOnBootclasspathBeforeRequirements(ctx)
2964}
2965
2966func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
2967 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
2968 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
2969 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
2970 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
2971 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
2972}
2973
2974func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
2975 if attr != nil {
2976 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
2977 // we will inform the user of invalid inputs when we try to write the
2978 // permissions xml file so we don't need to do it here
2979 if t.GreaterThan(level) {
2980 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
2981 }
2982 }
2983 }
2984}
2985
2986func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
2987 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
2988 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
2989 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
2990 if minErr == nil && maxErr == nil {
2991 // we will inform the user of invalid inputs when we try to write the
2992 // permissions xml file so we don't need to do it here
2993 if min.GreaterThan(max) {
2994 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
2995 }
2996 }
2997 }
2998}
2999
3000func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3001 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3002 if module.properties.Min_device_sdk != nil {
3003 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3004 if err == nil {
3005 if moduleMinApi.GreaterThan(api) {
3006 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3007 }
3008 }
3009 }
3010 if module.properties.Max_device_sdk != nil {
3011 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3012 if err == nil {
3013 if moduleMinApi.GreaterThan(api) {
3014 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3015 }
3016 }
3017 }
3018}
3019
3020func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3021 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3022 if module.properties.On_bootclasspath_before != nil {
3023 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3024 // if we use the attribute, then we need to do this validation
3025 if moduleMinApi.LessThan(t) {
3026 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3027 if module.properties.Min_device_sdk == nil {
3028 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")
3029 }
3030 }
3031 }
3032}
3033
Paul Duffindd46f712020-02-10 13:37:10 +00003034type sdkLibrarySdkMemberType struct {
3035 android.SdkMemberTypeBase
3036}
3037
Paul Duffin296701e2021-07-14 10:29:36 +01003038func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3039 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003040}
3041
3042func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3043 _, ok := module.(*SdkLibrary)
3044 return ok
3045}
3046
3047func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3048 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3049}
3050
3051func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3052 return &sdkLibrarySdkMemberProperties{}
3053}
3054
Paul Duffin976b0e52021-04-27 23:20:26 +01003055var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3056 android.SdkMemberTypeBase{
3057 PropertyName: "java_sdk_libs",
3058 SupportsSdk: true,
3059 },
3060}
3061
Paul Duffindd46f712020-02-10 13:37:10 +00003062type sdkLibrarySdkMemberProperties struct {
3063 android.SdkMemberPropertiesBase
3064
Paul Duffine8409952022-09-22 16:24:46 +01003065 // Stem name for files in the sdk snapshot.
3066 //
3067 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3068 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3069 //
3070 // This property is marked as keep so that it will be kept in all instances of this struct, will
3071 // not be cleared but will be copied to common structs. That is needed because this field is used
3072 // to construct many file names for other parts of this struct and so it needs to be present in
3073 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3074 // be unavailable for generating file names if there were other properties that were still set.
3075 Stem string `sdk:"keep"`
3076
Paul Duffindd46f712020-02-10 13:37:10 +00003077 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003078 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003079
Paul Duffin3d1248c2020-04-09 00:10:17 +01003080 // The Java stubs source files.
3081 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003082
3083 // The naming scheme.
3084 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003085
3086 // True if the java_sdk_library_import is for a shared library, false
3087 // otherwise.
3088 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003089
Paul Duffin1267d872021-04-16 17:21:36 +01003090 // True if the stub imports should produce dex jars.
3091 Compile_dex *bool
3092
Paul Duffina2ae7e02020-09-11 11:55:00 +01003093 // The paths to the doctag files to add to the prebuilt.
3094 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003095
3096 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003097
3098 // Signals that this shared library is part of the bootclasspath starting
3099 // on the version indicated in this attribute.
3100 //
3101 // This will make platforms at this level and above to ignore
3102 // <uses-library> tags with this library name because the library is already
3103 // available
3104 On_bootclasspath_since *string
3105
3106 // Signals that this shared library was part of the bootclasspath before
3107 // (but not including) the version indicated in this attribute.
3108 //
3109 // The system will automatically add a <uses-library> tag with this library to
3110 // apps that target any SDK less than the version indicated in this attribute.
3111 On_bootclasspath_before *string
3112
3113 // Indicates that PackageManager should ignore this shared library if the
3114 // platform is below the version indicated in this attribute.
3115 //
3116 // This means that the device won't recognise this library as installed.
3117 Min_device_sdk *string
3118
3119 // Indicates that PackageManager should ignore this shared library if the
3120 // platform is above the version indicated in this attribute.
3121 //
3122 // This means that the device won't recognise this library as installed.
3123 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003124
3125 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003126}
3127
3128type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003129 Jars android.Paths
3130 StubsSrcJar android.Path
3131 CurrentApiFile android.Path
3132 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003133 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003134 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003135}
3136
3137func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3138 sdk := variant.(*SdkLibrary)
3139
Paul Duffine8409952022-09-22 16:24:46 +01003140 // Copy the stem name for files in the sdk snapshot.
3141 s.Stem = sdk.distStem()
3142
Paul Duffin106a3a42022-01-27 16:39:06 +00003143 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003144 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003145 paths := sdk.findScopePaths(apiScope)
3146 if paths == nil {
3147 continue
3148 }
3149
Paul Duffindd46f712020-02-10 13:37:10 +00003150 jars := paths.stubsImplPath
3151 if len(jars) > 0 {
3152 properties := scopeProperties{}
3153 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003154 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003155 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003156 if paths.currentApiFilePath.Valid() {
3157 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3158 }
3159 if paths.removedApiFilePath.Valid() {
3160 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3161 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003162 // The annotations zip is only available for modules that set annotations_enabled: true.
3163 if paths.annotationsZip.Valid() {
3164 properties.AnnotationsZip = paths.annotationsZip.Path()
3165 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003166 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003167 }
3168 }
3169
Paul Duffindfa131e2020-05-15 20:37:11 +01003170 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003171 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003172 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003173 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003174 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003175 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3176 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3177 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3178 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003179
3180 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3181 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3182 }
Paul Duffindd46f712020-02-10 13:37:10 +00003183}
3184
3185func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003186 if s.Naming_scheme != nil {
3187 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3188 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003189 if s.Shared_library != nil {
3190 propertySet.AddProperty("shared_library", *s.Shared_library)
3191 }
Paul Duffin1267d872021-04-16 17:21:36 +01003192 if s.Compile_dex != nil {
3193 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3194 }
Paul Duffin869de142021-07-15 14:14:41 +01003195 if len(s.Permitted_packages) > 0 {
3196 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3197 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003198 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3199 if s.DexPreoptProfileGuided != nil {
3200 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3201 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003202
Paul Duffine8409952022-09-22 16:24:46 +01003203 stem := s.Stem
3204
Paul Duffindd46f712020-02-10 13:37:10 +00003205 for _, apiScope := range allApiScopes {
3206 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003207 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003208
Paul Duffin958806b2022-05-16 13:10:47 +00003209 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003210
Paul Duffindd46f712020-02-10 13:37:10 +00003211 var jars []string
3212 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003213 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003214 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3215 jars = append(jars, dest)
3216 }
3217 scopeSet.AddProperty("jars", jars)
3218
Paul Duffin22628d52021-05-12 23:13:22 +01003219 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3220 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003221 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003222 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3223 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3224 } else {
3225 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3226 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003227 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003228 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3229 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3230 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003231
Paul Duffin1fd005d2020-04-09 01:08:11 +01003232 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003233 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003234 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3235 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3236 }
3237
3238 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003239 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003240 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003241 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3242 }
3243
Anton Hanssond78eb762021-09-21 15:25:12 +01003244 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003245 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003246 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3247 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3248 }
3249
Paul Duffindd46f712020-02-10 13:37:10 +00003250 if properties.SdkVersion != "" {
3251 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3252 }
3253 }
3254 }
3255
Paul Duffina2ae7e02020-09-11 11:55:00 +01003256 if len(s.Doctag_paths) > 0 {
3257 dests := []string{}
3258 for _, p := range s.Doctag_paths {
3259 dest := filepath.Join("doctags", p.Rel())
3260 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3261 dests = append(dests, dest)
3262 }
3263 propertySet.AddProperty("doctag_files", dests)
3264 }
Paul Duffindd46f712020-02-10 13:37:10 +00003265}