blob: 6491bed0ac3dadf2c7311eaecb14cf5fd1765f38 [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
Jihoon Kang1147b312023-06-08 23:25:57 +0000239func (scope *apiScope) sourceStubLibraryModuleName(baseName string) string {
240 return scope.stubsLibraryModuleName(baseName) + ".from-source"
241}
242
Paul Duffinc3091c82020-05-08 14:16:20 +0100243func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100244 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000245}
246
Paul Duffinc8782502020-04-29 20:45:27 +0100247func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100248 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000249}
250
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100251func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100252 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100253}
254
Paul Duffin3375e352020-04-28 10:44:03 +0100255func (scope *apiScope) String() string {
256 return scope.name
257}
258
Paul Duffin958806b2022-05-16 13:10:47 +0000259// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
260// be stored.
261func (scope *apiScope) snapshotRelativeDir() string {
262 return filepath.Join("sdk_library", scope.name)
263}
264
265// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
266// library.
267func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
268 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
269}
270
271// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
272// named library.
273func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
274 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
275}
276
Paul Duffind1b3a922020-01-22 11:57:20 +0000277type apiScopes []*apiScope
278
279func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
280 var list []string
281 for _, scope := range scopes {
282 list = append(list, accessor(scope))
283 }
284 return list
285}
286
Jiyong Parkc678ad32018-04-10 13:07:10 +0900287var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100288 scopeByName = make(map[string]*apiScope)
289 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000290 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100291 name: "public",
292
293 // Public scope is enabled by default for both legacy and non-legacy modes.
294 legacyEnabledStatus: func(module *SdkLibrary) bool {
295 return true
296 },
297 defaultEnabledStatus: true,
298
299 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
300 return &module.sdkLibraryProperties.Public
301 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000302 sdkVersion: "current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000303 kind: android.SdkPublic,
Paul Duffind1b3a922020-01-22 11:57:20 +0000304 })
305 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100306 name: "system",
307 extends: apiScopePublic,
308 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
309 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
310 return &module.sdkLibraryProperties.System
311 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100312 apiFilePrefix: "system-",
313 moduleSuffix: ".system",
314 sdkVersion: "system_current",
315 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000316 kind: android.SdkSystem,
Paul Duffind1b3a922020-01-22 11:57:20 +0000317 })
318 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100319 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100320 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100321 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
322 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
323 return &module.sdkLibraryProperties.Test
324 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100325 apiFilePrefix: "test-",
326 moduleSuffix: ".test",
327 sdkVersion: "test_current",
328 annotation: "android.annotation.TestApi",
329 unstable: true,
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000330 kind: android.SdkTest,
Paul Duffind1b3a922020-01-22 11:57:20 +0000331 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100332 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100333 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100334 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100335 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100336 //
337 // Enabling this would break existing usages.
338 legacyEnabledStatus: func(module *SdkLibrary) bool {
339 return false
340 },
341 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
342 return &module.sdkLibraryProperties.Module_lib
343 },
344 apiFilePrefix: "module-lib-",
345 moduleSuffix: ".module_lib",
346 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100347 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000348 kind: android.SdkModule,
Paul Duffin8f265b92020-04-28 14:13:56 +0100349 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100350 apiScopeSystemServer = initApiScope(&apiScope{
351 name: "system-server",
352 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100353
354 // The system-server scope can access the module-lib scope.
355 //
356 // A module that provides a system-server API is appended to the standard bootclasspath that is
357 // used by the system server. So, it should be able to access module-lib APIs provided by
358 // libraries on the bootclasspath.
359 canAccess: apiScopeModuleLib,
360
Paul Duffin0c5bae52020-06-02 13:00:08 +0100361 // The system-server scope is disabled by default in legacy mode.
362 //
363 // Enabling this would break existing usages.
364 legacyEnabledStatus: func(module *SdkLibrary) bool {
365 return false
366 },
367 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
368 return &module.sdkLibraryProperties.System_server
369 },
370 apiFilePrefix: "system-server-",
371 moduleSuffix: ".system_server",
372 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100373 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
374 extraArgs: []string{
375 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100376 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100377 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100378 },
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000379 kind: android.SdkSystemServer,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100380 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000381 allApiScopes = apiScopes{
382 apiScopePublic,
383 apiScopeSystem,
384 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100385 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100386 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000387 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900388)
389
Jiyong Park82484c02018-04-23 21:41:26 +0900390var (
391 javaSdkLibrariesLock sync.Mutex
392)
393
Jiyong Parkc678ad32018-04-10 13:07:10 +0900394// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900395// 1) disallowing linking to the runtime shared lib
396// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900397
398func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000399 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900400
Jiyong Park82484c02018-04-23 21:41:26 +0900401 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
402 javaSdkLibraries := javaSdkLibraries(ctx.Config())
403 sort.Strings(*javaSdkLibraries)
404 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
405 })
Paul Duffindd46f712020-02-10 13:37:10 +0000406
407 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100408 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900409}
410
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000411func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
412 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
413 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
414}
415
Paul Duffin3375e352020-04-28 10:44:03 +0100416// Properties associated with each api scope.
417type ApiScopeProperties struct {
418 // Indicates whether the api surface is generated.
419 //
420 // If this is set for any scope then all scopes must explicitly specify if they
421 // are enabled. This is to prevent new usages from depending on legacy behavior.
422 //
423 // Otherwise, if this is not set for any scope then the default behavior is
424 // scope specific so please refer to the scope specific property documentation.
425 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100426
427 // The sdk_version to use for building the stubs.
428 //
429 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000430 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100431 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000432 // will be none. This is used for java_sdk_library instances that are used
433 // to create stubs that contribute to the core_current sdk version.
434 // 2) Otherwise, it is assumed that this library extends but does not
435 // contribute directly to a specific sdk_version and so this uses the
436 // sdk_version appropriate for the api scope. e.g. public will use
437 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100438 //
439 // This does not affect the sdk_version used for either generating the stubs source
440 // or the API file. They both have to use the same sdk_version as is used for
441 // compiling the implementation library.
442 Sdk_version *string
Paul Duffin3375e352020-04-28 10:44:03 +0100443}
444
Jiyong Parkc678ad32018-04-10 13:07:10 +0900445type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100446 // List of source files that are needed to compile the API, but are not part of runtime library.
447 Api_srcs []string `android:"arch_variant"`
448
Paul Duffin5df79302020-05-16 15:52:12 +0100449 // Visibility for impl library module. If not specified then defaults to the
450 // visibility property.
451 Impl_library_visibility []string
452
Paul Duffin4911a892020-04-29 23:35:13 +0100453 // Visibility for stubs library modules. If not specified then defaults to the
454 // visibility property.
455 Stubs_library_visibility []string
456
457 // Visibility for stubs source modules. If not specified then defaults to the
458 // visibility property.
459 Stubs_source_visibility []string
460
Anton Hansson7f66efa2020-10-08 14:47:23 +0100461 // List of Java libraries that will be in the classpath when building the implementation lib
462 Impl_only_libs []string `android:"arch_variant"`
463
Paul Duffin77590a82022-04-28 14:13:30 +0000464 // List of Java libraries that will included in the implementation lib.
465 Impl_only_static_libs []string `android:"arch_variant"`
466
Sundong Ahnf043cf62018-06-25 16:04:37 +0900467 // List of Java libraries that will be in the classpath when building stubs
468 Stub_only_libs []string `android:"arch_variant"`
469
Anton Hanssondae54cd2021-04-21 16:30:10 +0100470 // List of Java libraries that will included in stub libraries
471 Stub_only_static_libs []string `android:"arch_variant"`
472
Paul Duffin7a586d32019-12-30 17:09:34 +0000473 // list of package names that will be documented and publicized as API.
474 // This allows the API to be restricted to a subset of the source files provided.
475 // If this is unspecified then all the source files will be treated as being part
476 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900477 Api_packages []string
478
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900479 // list of package names that must be hidden from the API
480 Hidden_api_packages []string
481
Paul Duffin749f98f2019-12-30 17:23:46 +0000482 // the relative path to the directory containing the api specification files.
483 // Defaults to "api".
484 Api_dir *string
485
Paul Duffindfa131e2020-05-15 20:37:11 +0100486 // Determines whether a runtime implementation library is built; defaults to false.
487 //
488 // 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 +0200489 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000490 Api_only *bool
491
Paul Duffin11512472019-02-11 15:55:17 +0000492 // local files that are used within user customized droiddoc options.
493 Droiddoc_option_files []string
494
Spandan Das93e95992021-07-29 18:26:39 +0000495 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000496 // Available variables for substitution:
497 //
498 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900499 Droiddoc_options []string
500
Paul Duffine22c2ab2020-05-20 19:35:27 +0100501 // is set to true, Metalava will allow framework SDK to contain annotations.
502 Annotations_enabled *bool
503
Sundong Ahn054b19a2018-10-19 13:46:09 +0900504 // a list of top-level directories containing files to merge qualifier annotations
505 // (i.e. those intended to be included in the stubs written) from.
506 Merge_annotations_dirs []string
507
508 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
509 Merge_inclusion_annotations_dirs []string
510
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000511 // If set to true then don't create dist rules.
512 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900513
Paul Duffin31310252020-11-20 21:26:20 +0000514 // The stem for the artifacts that are copied to the dist, if not specified
515 // then defaults to the base module name.
516 //
517 // For each scope the following artifacts are copied to the apistubs/<scope>
518 // directory in the dist.
519 // * stubs impl jar -> <dist-stem>.jar
520 // * API specification file -> api/<dist-stem>.txt
521 // * Removed API specification file -> api/<dist-stem>-removed.txt
522 //
523 // Also used to construct the name of the filegroup (created by prebuilt_apis)
524 // that references the latest released API and remove API specification files.
525 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
526 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800527 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000528 Dist_stem *string
529
Colin Cross986b69a2021-06-01 13:13:40 -0700530 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700531 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700532 // in the public Android SDK.
533 Dist_group *string
534
Anton Hanssondff2c782020-12-21 17:10:01 +0000535 // A compatibility mode that allows historical API-tracking files to not exist.
536 // Do not use.
537 Unsafe_ignore_missing_latest_api bool
538
Paul Duffin3375e352020-04-28 10:44:03 +0100539 // indicates whether system and test apis should be generated.
540 Generate_system_and_test_apis bool `blueprint:"mutated"`
541
542 // The properties specific to the public api scope
543 //
544 // Unless explicitly specified by using public.enabled the public api scope is
545 // enabled by default in both legacy and non-legacy mode.
546 Public ApiScopeProperties
547
548 // The properties specific to the system api scope
549 //
550 // In legacy mode the system api scope is enabled by default when sdk_version
551 // is set to something other than "none".
552 //
553 // In non-legacy mode the system api scope is disabled by default.
554 System ApiScopeProperties
555
556 // The properties specific to the test api scope
557 //
558 // In legacy mode the test api scope is enabled by default when sdk_version
559 // is set to something other than "none".
560 //
561 // In non-legacy mode the test api scope is disabled by default.
562 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000563
Paul Duffin0c5bae52020-06-02 13:00:08 +0100564 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100565 //
Zi Wangb2179e32023-01-31 15:53:30 -0800566 // Unless explicitly specified by using module_lib.enabled the module_lib api
567 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100568 Module_lib ApiScopeProperties
569
Paul Duffin0c5bae52020-06-02 13:00:08 +0100570 // The properties specific to the system-server api scope
571 //
Zi Wangb2179e32023-01-31 15:53:30 -0800572 // Unless explicitly specified by using system_server.enabled the
573 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100574 System_server ApiScopeProperties
575
Jiyong Park932cdfe2020-05-28 00:19:53 +0900576 // Determines if the stubs are preferred over the implementation library
577 // for linking, even when the client doesn't specify sdk_version. When this
578 // is set to true, such clients are provided with the widest API surface that
579 // this lib provides. Note however that this option doesn't affect the clients
580 // that are in the same APEX as this library. In that case, the clients are
581 // always linked with the implementation library. Default is false.
582 Default_to_stubs *bool
583
Paul Duffin160fe412020-05-10 19:32:20 +0100584 // Properties related to api linting.
585 Api_lint struct {
586 // Enable api linting.
587 Enabled *bool
588 }
589
Jiyong Parkc678ad32018-04-10 13:07:10 +0900590 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100591 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900592}
593
Paul Duffin0f8faff2020-05-20 16:18:00 +0100594// Paths to outputs from java_sdk_library and java_sdk_library_import.
595//
596// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
597// OptionalPaths are always set by java_sdk_library but may not be set by
598// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000599type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100600 // The path (represented as Paths for convenience when returning) to the stubs header jar.
601 //
602 // That is the jar that is created by turbine.
603 stubsHeaderPath android.Paths
604
605 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
606 //
607 // This is not the implementation jar, it still only contains stubs.
608 stubsImplPath android.Paths
609
Paul Duffin1267d872021-04-16 17:21:36 +0100610 // The dex jar for the stubs.
611 //
612 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100613 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100614
Paul Duffin0f8faff2020-05-20 16:18:00 +0100615 // The API specification file, e.g. system_current.txt.
616 currentApiFilePath android.OptionalPath
617
618 // The specification of API elements removed since the last release.
619 removedApiFilePath android.OptionalPath
620
621 // The stubs source jar.
622 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100623
624 // Extracted annotations.
625 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000626
627 // The path to the latest API file.
628 latestApiPath android.OptionalPath
629
630 // The path to the latest removed API file.
631 latestRemovedApiPath android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000632}
633
Colin Crossdcf71b22021-02-01 13:59:03 -0800634func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
635 if ctx.OtherModuleHasProvider(dep, JavaInfoProvider) {
636 lib := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
637 paths.stubsHeaderPath = lib.HeaderJars
638 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100639
640 libDep := dep.(UsesLibraryDependency)
641 paths.stubsDexJarPath = libDep.DexJarBuildPath()
Paul Duffinc8782502020-04-29 20:45:27 +0100642 return nil
643 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800644 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100645 }
646}
647
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100648func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
649 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
650 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100651 return nil
652 } else {
653 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
654 }
655}
656
Paul Duffin0f8faff2020-05-20 16:18:00 +0100657func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
658 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
659 action(apiStubsProvider)
660 return nil
661 } else {
662 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
663 }
664}
665
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100666func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hanssond78eb762021-09-21 15:25:12 +0100667 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100668 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
669 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100670}
671
Colin Crossdcf71b22021-02-01 13:59:03 -0800672func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100673 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
674 paths.extractApiInfoFromApiStubsProvider(provider)
675 })
676}
677
Paul Duffin0f8faff2020-05-20 16:18:00 +0100678func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
679 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100680}
681
Colin Crossdcf71b22021-02-01 13:59:03 -0800682func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100683 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100684 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
685 })
686}
687
Colin Crossdcf71b22021-02-01 13:59:03 -0800688func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100689 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
690 paths.extractApiInfoFromApiStubsProvider(provider)
691 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
692 })
693}
694
Paul Duffin958806b2022-05-16 13:10:47 +0000695func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
696 var paths android.Paths
697 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
698 paths = sourceFileProducer.Srcs()
699 } else {
700 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
701 }
702 if len(paths) != 1 {
703 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
704 }
705 return android.OptionalPathForPath(paths[0]), nil
706}
707
708func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
709 outputPath, err := extractSingleOptionalOutputPath(dep)
710 paths.latestApiPath = outputPath
711 return err
712}
713
714func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
715 outputPath, err := extractSingleOptionalOutputPath(dep)
716 paths.latestRemovedApiPath = outputPath
717 return err
718}
719
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100720type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100721 // The naming scheme to use for the components that this module creates.
722 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100723 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100724 //
725 // This is a temporary mechanism to simplify conversion from separate modules for each
726 // component that follow a different naming pattern to the default one.
727 //
728 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100729 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100730
731 // Specifies whether this module can be used as an Android shared library; defaults
732 // to true.
733 //
734 // An Android shared library is one that can be referenced in a <uses-library> element
735 // in an AndroidManifest.xml.
736 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100737
738 // Files containing information about supported java doc tags.
739 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000740
741 // Signals that this shared library is part of the bootclasspath starting
742 // on the version indicated in this attribute.
743 //
744 // This will make platforms at this level and above to ignore
745 // <uses-library> tags with this library name because the library is already
746 // available
747 On_bootclasspath_since *string
748
749 // Signals that this shared library was part of the bootclasspath before
750 // (but not including) the version indicated in this attribute.
751 //
752 // The system will automatically add a <uses-library> tag with this library to
753 // apps that target any SDK less than the version indicated in this attribute.
754 On_bootclasspath_before *string
755
756 // Indicates that PackageManager should ignore this shared library if the
757 // platform is below the version indicated in this attribute.
758 //
759 // This means that the device won't recognise this library as installed.
760 Min_device_sdk *string
761
762 // Indicates that PackageManager should ignore this shared library if the
763 // platform is above the version indicated in this attribute.
764 //
765 // This means that the device won't recognise this library as installed.
766 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100767}
768
Paul Duffin71b33cc2021-06-23 11:39:47 +0100769// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
770// embeds the commonToSdkLibraryAndImport struct.
771type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000772 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100773
774 BaseModuleName() string
775}
776
Paul Duffin56d44902020-01-31 13:36:25 +0000777// Common code between sdk library and sdk library import
778type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100779 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100780
Paul Duffin56d44902020-01-31 13:36:25 +0000781 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100782
783 namingScheme sdkLibraryComponentNamingScheme
784
Paul Duffindfa131e2020-05-15 20:37:11 +0100785 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100786
Paul Duffina2ae7e02020-09-11 11:55:00 +0100787 // Paths to commonSdkLibraryProperties.Doctag_files
788 doctagPaths android.Paths
789
Paul Duffin859fe962020-05-15 10:20:31 +0100790 // Functionality related to this being used as a component of a java_sdk_library.
791 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000792}
793
Paul Duffin71b33cc2021-06-23 11:39:47 +0100794func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
795 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100796
Paul Duffin71b33cc2021-06-23 11:39:47 +0100797 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100798
799 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100800 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100801}
802
803func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100804 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100805 switch schemeProperty {
806 case "default":
807 c.namingScheme = &defaultNamingScheme{}
808 default:
809 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
810 return false
811 }
812
Paul Duffin3f0290e2021-06-30 18:25:36 +0100813 namePtr := proptools.StringPtr(c.module.BaseModuleName())
814 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
815
Paul Duffindfa131e2020-05-15 20:37:11 +0100816 // Only track this sdk library if this can be used as a shared library.
817 if c.sharedLibrary() {
818 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100819 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100820 }
Paul Duffin859fe962020-05-15 10:20:31 +0100821
Paul Duffin1b1e8062020-05-08 13:44:43 +0100822 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100823}
824
Paul Duffinea8f8082021-06-24 13:25:57 +0100825// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
826// method.
827func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
828 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
829 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
830 // the APEX and so it needs a unique variation per APEX.
831 return c.sharedLibrary()
832}
833
Paul Duffina2ae7e02020-09-11 11:55:00 +0100834func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
835 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
836}
837
Paul Duffineedc5d52020-06-12 17:46:39 +0100838// Module name of the runtime implementation library
839func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100840 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100841}
842
843// Module name of the XML file for the lib
844func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100845 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100846}
847
Paul Duffinc3091c82020-05-08 14:16:20 +0100848// Name of the java_library module that compiles the stubs source.
849func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100850 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000851 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100852}
853
854// Name of the droidstubs module that generates the stubs source and may also
855// generate/check the API.
856func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100857 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000858 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100859}
860
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000861// Name of the java_api_library module that generates the from-text stubs source
862// and compiles to a jar file.
863func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
864 baseName := c.module.BaseModuleName()
865 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
866}
867
Jihoon Kang1147b312023-06-08 23:25:57 +0000868// Name of the java_library module that compiles the stubs
869// generated from source Java files.
870func (c *commonToSdkLibraryAndImport) sourceStubLibraryModuleName(apiScope *apiScope) string {
871 baseName := c.module.BaseModuleName()
872 return c.namingScheme.sourceStubLibraryModuleName(apiScope, baseName)
873}
874
Paul Duffin46dc45a2020-05-14 15:39:10 +0100875// The component names for different outputs of the java_sdk_library.
876//
877// They are similar to the names used for the child modules it creates
878const (
879 stubsSourceComponentName = "stubs.source"
880
881 apiTxtComponentName = "api.txt"
882
883 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +0100884
885 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +0100886)
887
888// A regular expression to match tags that reference a specific stubs component.
889//
890// It will only match if given a valid scope and a valid component. It is verfy strict
891// to ensure it does not accidentally match a similar looking tag that should be processed
892// by the embedded Library.
893var tagSplitter = func() *regexp.Regexp {
894 // Given a list of literal string items returns a regular expression that will
895 // match any one of the items.
896 choice := func(items ...string) string {
897 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
898 }
899
900 // Regular expression to match one of the scopes.
901 scopesRegexp := choice(allScopeNames...)
902
903 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +0100904 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100905
906 // Regular expression to match any combination of one scope and one component.
907 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
908}()
909
910// For OutputFileProducer interface
911//
Anton Hanssond78eb762021-09-21 15:25:12 +0100912// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100913func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
914 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
915 scopeName := groups[1]
916 component := groups[2]
917
918 if scope, ok := scopeByName[scopeName]; ok {
919 paths := c.findScopePaths(scope)
920 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100921 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100922 }
923
924 switch component {
925 case stubsSourceComponentName:
926 if paths.stubsSrcJar.Valid() {
927 return android.Paths{paths.stubsSrcJar.Path()}, nil
928 }
929
930 case apiTxtComponentName:
931 if paths.currentApiFilePath.Valid() {
932 return android.Paths{paths.currentApiFilePath.Path()}, nil
933 }
934
935 case removedApiTxtComponentName:
936 if paths.removedApiFilePath.Valid() {
937 return android.Paths{paths.removedApiFilePath.Path()}, nil
938 }
Anton Hanssond78eb762021-09-21 15:25:12 +0100939
940 case annotationsComponentName:
941 if paths.annotationsZip.Valid() {
942 return android.Paths{paths.annotationsZip.Path()}, nil
943 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100944 }
945
946 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
947 } else {
948 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
949 }
950
951 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100952 switch tag {
953 case ".doctags":
954 if c.doctagPaths != nil {
955 return c.doctagPaths, nil
956 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100957 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +0100958 }
959 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100960 return nil, nil
961 }
962}
963
Paul Duffin803a9562020-05-20 11:52:25 +0100964func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000965 if c.scopePaths == nil {
966 c.scopePaths = make(map[*apiScope]*scopePaths)
967 }
968 paths := c.scopePaths[scope]
969 if paths == nil {
970 paths = &scopePaths{}
971 c.scopePaths[scope] = paths
972 }
973
974 return paths
975}
976
Paul Duffin803a9562020-05-20 11:52:25 +0100977func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
978 if c.scopePaths == nil {
979 return nil
980 }
981
982 return c.scopePaths[scope]
983}
984
985// If this does not support the requested api scope then find the closest available
986// scope it does support. Returns nil if no such scope is available.
987func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +0100988 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +0100989 if paths := c.findScopePaths(s); paths != nil {
990 return paths
991 }
992 }
993
994 // This should never happen outside tests as public should be the base scope for every
995 // scope and is enabled by default.
996 return nil
997}
998
Jiyong Parkf1691d22021-03-29 20:11:58 +0900999func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001000
1001 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001002 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001003 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001004 }
1005
Paul Duffin1267d872021-04-16 17:21:36 +01001006 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1007 if paths == nil {
1008 return nil
1009 }
1010
1011 return paths.stubsHeaderPath
1012}
1013
1014// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1015//
1016// If the module does not support the specific kind then it will return the *scopePaths for the
1017// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1018// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1019func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001020 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001021
Paul Duffin803a9562020-05-20 11:52:25 +01001022 paths := c.findClosestScopePath(apiScope)
1023 if paths == nil {
1024 var scopes []string
1025 for _, s := range allApiScopes {
1026 if c.findScopePaths(s) != nil {
1027 scopes = append(scopes, s.name)
1028 }
1029 }
Paul Duffin71b33cc2021-06-23 11:39:47 +01001030 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 +01001031 return nil
1032 }
1033
Paul Duffin1267d872021-04-16 17:21:36 +01001034 return paths
1035}
1036
Paul Duffin32cf58a2021-05-18 16:32:50 +01001037// sdkKindToApiScope maps from android.SdkKind to apiScope.
1038func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1039 var apiScope *apiScope
1040 switch kind {
1041 case android.SdkSystem:
1042 apiScope = apiScopeSystem
1043 case android.SdkModule:
1044 apiScope = apiScopeModuleLib
1045 case android.SdkTest:
1046 apiScope = apiScopeTest
1047 case android.SdkSystemServer:
1048 apiScope = apiScopeSystemServer
1049 default:
1050 apiScope = apiScopePublic
1051 }
1052 return apiScope
1053}
1054
Paul Duffin1267d872021-04-16 17:21:36 +01001055// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001056func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001057 paths := c.selectScopePaths(ctx, kind)
1058 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001059 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001060 }
1061
1062 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001063}
1064
Paul Duffin32cf58a2021-05-18 16:32:50 +01001065// to satisfy SdkLibraryDependency interface
1066func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1067 apiScope := sdkKindToApiScope(kind)
1068 paths := c.findScopePaths(apiScope)
1069 if paths == nil {
1070 return android.OptionalPath{}
1071 }
1072
1073 return paths.removedApiFilePath
1074}
1075
Paul Duffin859fe962020-05-15 10:20:31 +01001076func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1077 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001078 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001079 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001080 }{}
1081
Paul Duffin3f0290e2021-06-30 18:25:36 +01001082 namePtr := proptools.StringPtr(c.module.BaseModuleName())
1083 componentProps.SdkLibraryName = namePtr
1084
Paul Duffindfa131e2020-05-15 20:37:11 +01001085 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001086 // Mark the stubs library as being components of this java_sdk_library so that
1087 // any app that includes code which depends (directly or indirectly) on the stubs
1088 // library will have the appropriate <uses-library> invocation inserted into its
1089 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001090 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001091 }
1092
1093 return componentProps
1094}
1095
Paul Duffindfa131e2020-05-15 20:37:11 +01001096func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1097 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1098}
1099
Paul Duffinf4600f62021-05-13 22:34:45 +01001100// Check if the stub libraries should be compiled for dex
1101func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1102 // Always compile the dex file files for the stub libraries if they will be used on the
1103 // bootclasspath.
1104 return !c.sharedLibrary()
1105}
1106
Paul Duffin859fe962020-05-15 10:20:31 +01001107// Properties related to the use of a module as an component of a java_sdk_library.
1108type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001109 // The name of the java_sdk_library/_import module.
1110 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001111
1112 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1113 // in the AndroidManifest.xml of any Android app that includes code that references
1114 // this module. If not set then no java_sdk_library/_import is tracked.
1115 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1116}
1117
1118// Structure to be embedded in a module struct that needs to support the
1119// SdkLibraryComponentDependency interface.
1120type EmbeddableSdkLibraryComponent struct {
1121 sdkLibraryComponentProperties SdkLibraryComponentProperties
1122}
1123
Paul Duffin71b33cc2021-06-23 11:39:47 +01001124func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1125 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001126}
1127
1128// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001129func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1130 return e.sdkLibraryComponentProperties.SdkLibraryName
1131}
1132
1133// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001134func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001135 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1136 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1137 // run-time library and the corresponding module that provides the implementation. This name is
1138 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1139 // in dexpreopt).
1140 //
1141 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1142 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001143 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1144}
1145
Paul Duffin859fe962020-05-15 10:20:31 +01001146// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1147// (including the java_sdk_library) itself.
1148type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001149 UsesLibraryDependency
1150
Paul Duffin3f0290e2021-06-30 18:25:36 +01001151 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1152 SdkLibraryName() *string
1153
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001154 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1155 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001156}
1157
1158// Make sure that all the module types that are components of java_sdk_library/_import
1159// and which can be referenced (directly or indirectly) from an android app implement
1160// the SdkLibraryComponentDependency interface.
1161var _ SdkLibraryComponentDependency = (*Library)(nil)
1162var _ SdkLibraryComponentDependency = (*Import)(nil)
1163var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001164var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001165
Paul Duffin32cf58a2021-05-18 16:32:50 +01001166// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001167type SdkLibraryDependency interface {
1168 SdkLibraryComponentDependency
1169
1170 // Get the header jars appropriate for the supplied sdk_version.
1171 //
1172 // These are turbine generated jars so they only change if the externals of the
1173 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001174 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001175
1176 // Get the implementation jars appropriate for the supplied sdk version.
1177 //
1178 // These are either the implementation jar for the whole sdk library or the implementation
1179 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1180 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001181 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001182
1183 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
1184 // tool which processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001185 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001186
Paul Duffin32cf58a2021-05-18 16:32:50 +01001187 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1188 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1189
Paul Duffinf4600f62021-05-13 22:34:45 +01001190 // sharedLibrary returns true if this can be used as a shared library.
1191 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001192}
1193
Inseob Kimc0907f12019-02-08 21:00:45 +09001194type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001195 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001196
Zi Wangb2179e32023-01-31 15:53:30 -08001197 android.BazelModuleBase
1198
Sundong Ahn054b19a2018-10-19 13:46:09 +09001199 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001200
Paul Duffin3375e352020-04-28 10:44:03 +01001201 // Map from api scope to the scope specific property structure.
1202 scopeToProperties map[*apiScope]*ApiScopeProperties
1203
Paul Duffin56d44902020-01-31 13:36:25 +00001204 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001205}
1206
Inseob Kimc0907f12019-02-08 21:00:45 +09001207var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001208
Paul Duffin3375e352020-04-28 10:44:03 +01001209func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1210 return module.sdkLibraryProperties.Generate_system_and_test_apis
1211}
1212
1213func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1214 // Check to see if any scopes have been explicitly enabled. If any have then all
1215 // must be.
1216 anyScopesExplicitlyEnabled := false
1217 for _, scope := range allApiScopes {
1218 scopeProperties := module.scopeToProperties[scope]
1219 if scopeProperties.Enabled != nil {
1220 anyScopesExplicitlyEnabled = true
1221 break
1222 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001223 }
Paul Duffin3375e352020-04-28 10:44:03 +01001224
1225 var generatedScopes apiScopes
1226 enabledScopes := make(map[*apiScope]struct{})
1227 for _, scope := range allApiScopes {
1228 scopeProperties := module.scopeToProperties[scope]
1229 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1230 // This is to ensure that any new usages of this module type do not rely on legacy
1231 // behaviour.
1232 defaultEnabledStatus := false
1233 if anyScopesExplicitlyEnabled {
1234 defaultEnabledStatus = scope.defaultEnabledStatus
1235 } else {
1236 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1237 }
1238 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1239 if enabled {
1240 enabledScopes[scope] = struct{}{}
1241 generatedScopes = append(generatedScopes, scope)
1242 }
1243 }
1244
1245 // Now check to make sure that any scope that is extended by an enabled scope is also
1246 // enabled.
1247 for _, scope := range allApiScopes {
1248 if _, ok := enabledScopes[scope]; ok {
1249 extends := scope.extends
1250 if extends != nil {
1251 if _, ok := enabledScopes[extends]; !ok {
1252 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1253 }
1254 }
1255 }
1256 }
1257
1258 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001259}
1260
satayev758968a2021-12-06 11:42:40 +00001261var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1262
satayev8f088b02021-12-06 11:40:46 +00001263func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001264 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001265 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1266 isExternal := !module.depIsInSameApex(ctx, child)
1267 if am, ok := child.(android.ApexModule); ok {
1268 if !do(ctx, parent, am, isExternal) {
1269 return false
1270 }
1271 }
1272 return !isExternal
1273 })
1274 })
1275}
1276
Paul Duffineedc5d52020-06-12 17:46:39 +01001277type sdkLibraryComponentTag struct {
1278 blueprint.BaseDependencyTag
1279 name string
1280}
1281
1282// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1283func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1284
1285var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001286
Jiyong Parke3833882020-02-17 17:28:10 +09001287func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001288 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001289 return dt == xmlPermissionsFileTag
1290 }
1291 return false
1292}
1293
Paul Duffineedc5d52020-06-12 17:46:39 +01001294var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001295
Paul Duffin44f1d842020-06-26 20:17:02 +01001296// Add the dependencies on the child modules in the component deps mutator.
1297func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001298 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001299 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001300 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kang1147b312023-06-08 23:25:57 +00001301
Spandan Das877f39d2023-03-29 16:19:51 +00001302 ctx.AddVariationDependencies(nil, apiScope.stubsTag, stubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001303
Paul Duffin15f34ef2020-07-20 18:04:44 +01001304 // Add a dependency on the stubs source in order to access both stubs source and api information.
1305 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001306
1307 if module.compareAgainstLatestApi(apiScope) {
1308 // Add dependencies on the latest finalized version of the API .txt file.
1309 latestApiModuleName := module.latestApiModuleName(apiScope)
1310 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1311
1312 // Add dependencies on the latest finalized version of the remove API .txt file.
1313 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1314 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1315 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001316 }
1317
Paul Duffindfa131e2020-05-15 20:37:11 +01001318 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001319 // Add dependency to the rule for generating the implementation library.
1320 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1321
Paul Duffindfa131e2020-05-15 20:37:11 +01001322 if module.sharedLibrary() {
1323 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001324 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001325 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001326 }
1327}
Paul Duffine74ac732020-02-06 13:51:46 +00001328
Paul Duffin44f1d842020-06-26 20:17:02 +01001329// Add other dependencies as normal.
1330func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001331 var missingApiModules []string
1332 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1333 if apiScope.unstable {
1334 continue
1335 }
Paul Duffin958806b2022-05-16 13:10:47 +00001336 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001337 missingApiModules = append(missingApiModules, m)
1338 }
Paul Duffin958806b2022-05-16 13:10:47 +00001339 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001340 missingApiModules = append(missingApiModules, m)
1341 }
Paul Duffin958806b2022-05-16 13:10:47 +00001342 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001343 missingApiModules = append(missingApiModules, m)
1344 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001345 }
1346 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1347 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1348 m += "You need to do one of the following:\n"
1349 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1350 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1351 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1352 m += "\n"
1353 m += "The following filegroup modules are missing:\n "
1354 m += strings.Join(missingApiModules, "\n ") + "\n"
1355 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."
1356 ctx.ModuleErrorf(m)
1357 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001358 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001359 // Only add the deps for the library if it is actually going to be built.
1360 module.Library.deps(ctx)
1361 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001362}
1363
Paul Duffin46dc45a2020-05-14 15:39:10 +01001364func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1365 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001366 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001367 return paths, err
1368 }
Colin Cross4acaea92021-12-10 23:05:02 +00001369 if module.requiresRuntimeImplementationLibrary() {
1370 return module.Library.OutputFiles(tag)
1371 }
1372 if tag == "" {
1373 return nil, nil
1374 }
1375 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001376}
1377
Inseob Kimc0907f12019-02-08 21:00:45 +09001378func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001379 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1380 module.CheckMinSdkVersion(ctx)
1381 }
1382
Paul Duffina2ae7e02020-09-11 11:55:00 +01001383 module.generateCommonBuildActions(ctx)
1384
Paul Duffindfa131e2020-05-15 20:37:11 +01001385 // Only build an implementation library if required.
1386 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001387 module.Library.GenerateAndroidBuildActions(ctx)
1388 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001389
Paul Duffinb97b1572021-04-29 21:50:40 +01001390 // Collate the components exported by this module. All scope specific modules are exported but
1391 // the impl and xml component modules are not.
1392 exportedComponents := map[string]struct{}{}
1393
Sundong Ahn57368eb2018-07-06 11:20:23 +09001394 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001395 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001396 // the recorded paths will be returned depending on the link type of the caller.
1397 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001398 tag := ctx.OtherModuleDependencyTag(to)
1399
Paul Duffinc8782502020-04-29 20:45:27 +01001400 // Extract information from any of the scope specific dependencies.
1401 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1402 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001403 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001404
1405 // Extract information from the dependency. The exact information extracted
1406 // is determined by the nature of the dependency which is determined by the tag.
1407 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001408
1409 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001410 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001411 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001412
1413 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001414 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Paul Duffinb97b1572021-04-29 21:50:40 +01001415 ctx.SetProvider(android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001416
1417 // Provide additional information for inclusion in an sdk's generated .info file.
1418 additionalSdkInfo := map[string]interface{}{}
1419 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001420 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001421 scopes := map[string]interface{}{}
1422 additionalSdkInfo["scopes"] = scopes
1423 for scope, scopePaths := range module.scopePaths {
1424 scopeInfo := map[string]interface{}{}
1425 scopes[scope.name] = scopeInfo
1426 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1427 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1428 if p := scopePaths.latestApiPath; p.Valid() {
1429 scopeInfo["latest_api"] = p.Path().String()
1430 }
1431 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1432 scopeInfo["latest_removed_api"] = p.Path().String()
1433 }
1434 }
1435 ctx.SetProvider(android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001436}
1437
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001438func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001439 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001440 return nil
1441 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001442 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001443 if module.sharedLibrary() {
1444 entries := &entriesList[0]
1445 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1446 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001447 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001448}
1449
Anton Hansson5fd5d242020-03-27 19:43:19 +00001450// The dist path of the stub artifacts
1451func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001452 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001453}
1454
Paul Duffin12ceb462019-12-24 20:31:31 +00001455// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001456func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001457 scopeProperties := module.scopeToProperties[apiScope]
1458 if scopeProperties.Sdk_version != nil {
1459 return proptools.String(scopeProperties.Sdk_version)
1460 }
1461
Jiyong Parkf1691d22021-03-29 20:11:58 +09001462 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001463 if sdkDep.hasStandardLibs() {
1464 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001465 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001466 } else {
1467 // Otherwise, use no system module.
1468 return "none"
1469 }
1470}
1471
Paul Duffin31310252020-11-20 21:26:20 +00001472func (module *SdkLibrary) distStem() string {
1473 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1474}
1475
Colin Cross986b69a2021-06-01 13:13:40 -07001476// distGroup returns the subdirectory of the dist path of the stub artifacts.
1477func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001478 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001479}
1480
Paul Duffin958806b2022-05-16 13:10:47 +00001481func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1482 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1483}
1484
Paul Duffind1b3a922020-01-22 11:57:20 +00001485func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001486 return ":" + module.latestApiModuleName(apiScope)
1487}
1488
1489func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
1490 return latestPrebuiltApiModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001491}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001492
Paul Duffind1b3a922020-01-22 11:57:20 +00001493func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001494 return ":" + module.latestRemovedApiModuleName(apiScope)
1495}
1496
1497func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
1498 return latestPrebuiltApiModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001499}
1500
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001501func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001502 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1503}
1504
1505func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1506 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001507}
1508
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001509func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1510 _, exists := c.GetApiLibraries()[module.Name()]
1511 return exists
1512}
1513
Anton Hansson944e77d2020-08-19 11:40:22 +01001514func childModuleVisibility(childVisibility []string) []string {
1515 if childVisibility == nil {
1516 // No child visibility set. The child will use the visibility of the sdk_library.
1517 return nil
1518 }
1519
1520 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1521 var visibility []string
1522 visibility = append(visibility, "//visibility:override")
1523 visibility = append(visibility, childVisibility...)
1524 return visibility
1525}
1526
Paul Duffin5df79302020-05-16 15:52:12 +01001527// Creates the implementation java library
1528func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001529 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1530
Paul Duffin5df79302020-05-16 15:52:12 +01001531 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001532 Name *string
1533 Visibility []string
1534 Instrument bool
1535 Libs []string
1536 Static_libs []string
1537 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001538 }{
1539 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001540 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001541 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1542 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001543 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1544 // addition of &module.properties below.
1545 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001546 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1547 // addition of &module.properties below.
1548 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1549 // Pass the apex_available settings down so that the impl library can be statically
1550 // embedded within a library that is added to an APEX. Needed for updatable-media.
1551 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001552 }
1553
1554 properties := []interface{}{
1555 &module.properties,
1556 &module.protoProperties,
1557 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001558 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001559 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001560 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001561 &props,
1562 module.sdkComponentPropertiesForChildLibrary(),
1563 }
1564 mctx.CreateModule(LibraryFactory, properties...)
1565}
1566
Jiyong Parkc678ad32018-04-10 13:07:10 +09001567// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001568func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001569 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001570 Name *string
1571 Visibility []string
1572 Srcs []string
1573 Installable *bool
1574 Sdk_version *string
1575 System_modules *string
1576 Patch_module *string
1577 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001578 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001579 Compile_dex *bool
1580 Java_version *string
1581 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001582 Srcs []string
1583 Javacflags []string
1584 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001585 Dist struct {
1586 Targets []string
1587 Dest *string
1588 Dir *string
1589 Tag *string
1590 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001591 }{}
1592
Jihoon Kang1147b312023-06-08 23:25:57 +00001593 props.Name = proptools.StringPtr(module.sourceStubLibraryModuleName(apiScope))
Anton Hansson944e77d2020-08-19 11:40:22 +01001594 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001595 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001596 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001597 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001598 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001599 props.System_modules = module.deviceProperties.System_modules
1600 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001601 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001602 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Anton Hanssondae54cd2021-04-21 16:30:10 +01001603 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001604 // The stub-annotations library contains special versions of the annotations
1605 // with CLASS retention policy, so that they're kept.
1606 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1607 props.Libs = append(props.Libs, "stub-annotations")
1608 }
Paul Duffina18abc22020-05-16 18:54:24 +01001609 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1610 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001611 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1612 // interop with older developer tools that don't support 1.9.
1613 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001614
Paul Duffin859fe962020-05-15 10:20:31 +01001615 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001616}
1617
Paul Duffin6d0886e2020-04-07 18:49:53 +01001618// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001619// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001620func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001621 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001622 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001623 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001624 Srcs []string
1625 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001626 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001627 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001628 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001629 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001630 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001631 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001632 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001633 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001634 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001635 Merge_annotations_dirs []string
1636 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001637 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001638 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001639 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001640 Current ApiToCheck
1641 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001642
1643 Api_lint struct {
1644 Enabled *bool
1645 New_since *string
1646 Baseline_file *string
1647 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001648 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001649 Aidl struct {
1650 Include_dirs []string
1651 Local_include_dirs []string
1652 }
Paul Duffin040e9062020-11-23 17:41:36 +00001653 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001654 }{}
1655
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001656 // The stubs source processing uses the same compile time classpath when extracting the
1657 // API from the implementation library as it does when compiling it. i.e. the same
1658 // * sdk version
1659 // * system_modules
1660 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001661
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001662 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001663 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001664 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001665 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001666 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001667 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001668 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001669 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001670 // A droiddoc module has only one Libs property and doesn't distinguish between
1671 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001672 props.Libs = module.properties.Libs
1673 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001674 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001675 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1676 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1677 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001678
Paul Duffine22c2ab2020-05-20 19:35:27 +01001679 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001680 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1681 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1682
Paul Duffin6d0886e2020-04-07 18:49:53 +01001683 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001684 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001685 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001686 }
1687 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001688 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001689 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1690 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001691 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001692 disabledWarnings := []string{
Paul Duffin235ffff2019-12-24 10:41:30 +00001693 "BroadcastBehavior",
Paul Duffin235ffff2019-12-24 10:41:30 +00001694 "DeprecationMismatch",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001695 "HiddenSuperclass",
Paul Duffin235ffff2019-12-24 10:41:30 +00001696 "HiddenTypeParameter",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001697 "MissingPermission",
1698 "SdkConstant",
Paul Duffin235ffff2019-12-24 10:41:30 +00001699 "Todo",
1700 "Typo",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001701 "UnavailableSymbol",
Paul Duffin235ffff2019-12-24 10:41:30 +00001702 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001703 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001704
Paul Duffin6877e6d2020-09-25 19:59:14 +01001705 // Output Javadoc comments for public scope.
1706 if apiScope == apiScopePublic {
1707 props.Output_javadoc_comments = proptools.BoolPtr(true)
1708 }
1709
Paul Duffin1fb487d2020-04-07 18:50:10 +01001710 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001711 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001712 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001713 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001714
Paul Duffin15f34ef2020-07-20 18:04:44 +01001715 // List of APIs identified from the provided source files are created. They are later
1716 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1717 // last-released (a.k.a numbered) list of API.
1718 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1719 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1720 apiDir := module.getApiDir()
1721 currentApiFileName = path.Join(apiDir, currentApiFileName)
1722 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001723
Paul Duffin15f34ef2020-07-20 18:04:44 +01001724 // check against the not-yet-release API
1725 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1726 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001727
Paul Duffin958806b2022-05-16 13:10:47 +00001728 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001729 // check against the latest released API
1730 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001731 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001732 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1733 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1734 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001735 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1736 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001737
Paul Duffin15f34ef2020-07-20 18:04:44 +01001738 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1739 // Enable api lint.
1740 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1741 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001742
Paul Duffin15f34ef2020-07-20 18:04:44 +01001743 // If it exists then pass a lint-baseline.txt through to droidstubs.
1744 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1745 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1746 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1747 if err != nil {
1748 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1749 }
1750 if len(paths) == 1 {
1751 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1752 } else if len(paths) != 0 {
1753 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001754 }
1755 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001756 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001757
Paul Duffin15f34ef2020-07-20 18:04:44 +01001758 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001759 // Dist the api txt and removed api txt artifacts for sdk builds.
1760 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1761 for _, p := range []struct {
1762 tag string
1763 pattern string
1764 }{
1765 {tag: ".api.txt", pattern: "%s.txt"},
1766 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1767 } {
1768 props.Dists = append(props.Dists, android.Dist{
1769 Targets: []string{"sdk", "win_sdk"},
1770 Dir: distDir,
1771 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1772 Tag: proptools.StringPtr(p.tag),
1773 })
1774 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001775 }
1776
Jihoon Kangd48abd52023-02-02 22:32:31 +00001777 mctx.CreateModule(DroidstubsFactory, &props).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001778}
1779
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001780func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1781 props := struct {
1782 Name *string
1783 Visibility []string
1784 Api_contributions []string
1785 Libs []string
1786 Static_libs []string
1787 Dep_api_srcs *string
1788 }{}
1789
1790 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
1791 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
1792
1793 apiContributions := []string{}
1794
1795 // Api surfaces are not independent of each other, but have subset relationships,
1796 // and so does the api files. To generate from-text stubs for api surfaces other than public,
1797 // all subset api domains' api_contriubtions must be added as well.
1798 scope := apiScope
1799 for scope != nil {
1800 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
1801 scope = scope.extends
1802 }
1803
1804 props.Api_contributions = apiContributions
1805 props.Libs = module.properties.Libs
1806 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
1807 props.Libs = append(props.Libs, "stub-annotations")
1808 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
1809 props.Dep_api_srcs = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + ".from-text")
1810
1811 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
1812 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
1813 if apiScope.kind == android.SdkModule {
1814 props.Dep_api_srcs = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
1815 }
1816
1817 mctx.CreateModule(ApiLibraryFactory, &props)
1818}
1819
Jihoon Kang1147b312023-06-08 23:25:57 +00001820func (module *SdkLibrary) createTopLevelStubsLibrary(
1821 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
1822 props := struct {
1823 Name *string
1824 Visibility []string
1825 Sdk_version *string
1826 Static_libs []string
1827 System_modules *string
1828 Dist struct {
1829 Targets []string
1830 Dest *string
1831 Dir *string
1832 Tag *string
1833 }
1834 Compile_dex *bool
1835 }{}
1836 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
1837 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
1838 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
1839 props.Sdk_version = proptools.StringPtr(sdkVersion)
1840
1841 // Add the stub compiling java_library/java_api_library as static lib based on build config
1842 staticLib := module.sourceStubLibraryModuleName(apiScope)
1843 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
1844 staticLib = module.apiLibraryModuleName(apiScope)
1845 }
1846 props.Static_libs = append(props.Static_libs, staticLib)
1847 props.System_modules = module.deviceProperties.System_modules
1848
1849 // Dist the class jar artifact for sdk builds.
1850 if !Bool(module.sdkLibraryProperties.No_dist) {
1851 props.Dist.Targets = []string{"sdk", "win_sdk"}
1852 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
1853 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1854 props.Dist.Tag = proptools.StringPtr(".jar")
1855 }
1856
1857 // The imports need to be compiled to dex if the java_sdk_library requests it.
1858 compileDex := module.dexProperties.Compile_dex
1859 if module.stubLibrariesCompiledForDex() {
1860 compileDex = proptools.BoolPtr(true)
1861 }
1862 props.Compile_dex = compileDex
1863
1864 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1865}
1866
Paul Duffin958806b2022-05-16 13:10:47 +00001867func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
1868 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
1869}
1870
Paul Duffinea8f8082021-06-24 13:25:57 +01001871// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09001872func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1873 depTag := mctx.OtherModuleDependencyTag(dep)
1874 if depTag == xmlPermissionsFileTag {
1875 return true
1876 }
1877 return module.Library.DepIsInSameApex(mctx, dep)
1878}
1879
Paul Duffinea8f8082021-06-24 13:25:57 +01001880// Implements android.ApexModule
1881func (module *SdkLibrary) UniqueApexVariations() bool {
1882 return module.uniqueApexVariations()
1883}
1884
Jiyong Parkc678ad32018-04-10 13:07:10 +09001885// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001886func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001887 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00001888 var moduleMinApiLevelStr = moduleMinApiLevel.String()
1889 if moduleMinApiLevel == android.NoneApiLevel {
1890 moduleMinApiLevelStr = "current"
1891 }
Jiyong Parke3833882020-02-17 17:28:10 +09001892 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001893 Name *string
1894 Lib_name *string
1895 Apex_available []string
1896 On_bootclasspath_since *string
1897 On_bootclasspath_before *string
1898 Min_device_sdk *string
1899 Max_device_sdk *string
1900 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09001901 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00001902 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
1903 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1904 Apex_available: module.ApexProperties.Apex_available,
1905 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
1906 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
1907 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
1908 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
1909 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001910 }
Jiyong Parke3833882020-02-17 17:28:10 +09001911
Jiyong Parke3833882020-02-17 17:28:10 +09001912 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001913}
1914
Jiyong Parkf1691d22021-03-29 20:11:58 +09001915func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09001916 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001917 var kind android.SdkKind
1918 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09001919 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001920 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001921 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001922 // We don't have prebuilt SDK for the specific sdkVersion.
1923 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09001924 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001925 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001926 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001927
1928 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001929 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001930 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001931 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001932 if ctx.Config().AllowMissingDependencies() {
1933 return android.Paths{android.PathForSource(ctx, jar)}
1934 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001935 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001936 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001937 return nil
1938 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001939 return android.Paths{jarPath.Path()}
1940}
1941
Colin Crossaede88c2020-08-11 12:17:01 -07001942// 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 +01001943//
1944// If either this or the other module are on the platform then this will return
1945// false.
Colin Cross56a83212020-09-15 18:30:11 -07001946func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
1947 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1948 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09001949 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01001950}
1951
Jiyong Parkf1691d22021-03-29 20:11:58 +09001952func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001953 // If the client doesn't set sdk_version, but if this library prefers stubs over
1954 // the impl library, let's provide the widest API surface possible. To do so,
1955 // force override sdk_version to module_current so that the closest possible API
1956 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09001957 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09001958 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09001959 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001960
Paul Duffindaaa3322020-05-26 18:13:57 +01001961 // Only provide access to the implementation library if it is actually built.
1962 if module.requiresRuntimeImplementationLibrary() {
1963 // Check any special cases for java_sdk_library.
1964 //
1965 // Only allow access to the implementation library in the following condition:
1966 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001967 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001968 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001969 if headerJars {
1970 return module.HeaderJars()
1971 } else {
1972 return module.ImplementationJars()
1973 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001974 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001975 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001976
Paul Duffin23970f42020-05-20 14:20:02 +01001977 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001978}
1979
Sundong Ahn241cd372018-07-13 16:16:44 +09001980// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001981func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001982 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1983}
1984
1985// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001986func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001987 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001988}
1989
Colin Cross571cccf2019-02-04 11:22:08 -08001990var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1991
Jiyong Park82484c02018-04-23 21:41:26 +09001992func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001993 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001994 return &[]string{}
1995 }).(*[]string)
1996}
1997
Paul Duffin749f98f2019-12-30 17:23:46 +00001998func (module *SdkLibrary) getApiDir() string {
1999 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2000}
2001
Jiyong Parkc678ad32018-04-10 13:07:10 +09002002// For a java_sdk_library module, create internal modules for stubs, docs,
2003// runtime libs and xml file. If requested, the stubs and docs are created twice
2004// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002005func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2006 // If the module has been disabled then don't create any child modules.
2007 if !module.Enabled() {
2008 return
2009 }
2010
Paul Duffina18abc22020-05-16 18:54:24 +01002011 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002012 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002013 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002014 }
2015
Paul Duffin37e0b772019-12-30 17:20:10 +00002016 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002017 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002018 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002019 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002020 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002021
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002022 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002023
Paul Duffin3375e352020-04-28 10:44:03 +01002024 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002025
Paul Duffin749f98f2019-12-30 17:23:46 +00002026 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002027 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002028 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002029 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002030 p := android.ExistentPathForSource(mctx, path)
2031 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002032 if mctx.Config().AllowMissingDependencies() {
2033 mctx.AddMissingDependencies([]string{path})
2034 } else {
2035 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2036 missingCurrentApi = true
2037 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002038 }
2039 }
2040 }
2041
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002042 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002043 script := "build/soong/scripts/gen-java-current-api-files.sh"
2044 p := android.ExistentPathForSource(mctx, script)
2045
2046 if !p.Valid() {
2047 panic(fmt.Sprintf("script file %s doesn't exist", script))
2048 }
2049
2050 mctx.ModuleErrorf("One or more current api files are missing. "+
2051 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002052 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002053 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002054 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002055 return
2056 }
2057
Paul Duffin3375e352020-04-28 10:44:03 +01002058 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002059 // Use the stubs source name for legacy reasons.
2060 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002061
Paul Duffind1b3a922020-01-22 11:57:20 +00002062 module.createStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002063
Jihoon Kang1147b312023-06-08 23:25:57 +00002064 contributesToApiSurface := module.contributesToApiSurface(mctx.Config())
2065 if contributesToApiSurface {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002066 module.createApiLibrary(mctx, scope)
2067 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002068
2069 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Inseob Kimc0907f12019-02-08 21:00:45 +09002070 }
2071
Paul Duffindfa131e2020-05-15 20:37:11 +01002072 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002073 // Create child module to create an implementation library.
2074 //
2075 // This temporarily creates a second implementation library that can be explicitly
2076 // referenced.
2077 //
2078 // TODO(b/156618935) - update comment once only one implementation library is created.
2079 module.createImplLibrary(mctx)
2080
Paul Duffindfa131e2020-05-15 20:37:11 +01002081 // Only create an XML permissions file that declares the library as being usable
2082 // as a shared library if required.
2083 if module.sharedLibrary() {
2084 module.createXmlFile(mctx)
2085 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002086
2087 // record java_sdk_library modules so that they are exported to make
2088 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2089 javaSdkLibrariesLock.Lock()
2090 defer javaSdkLibrariesLock.Unlock()
2091 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2092 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002093
Paul Duffin77590a82022-04-28 14:13:30 +00002094 // 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 +01002095 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002096 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002097}
2098
2099func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002100 module.addHostAndDeviceProperties()
2101 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002102
Paul Duffin71b33cc2021-06-23 11:39:47 +01002103 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002104
Paul Duffina18abc22020-05-16 18:54:24 +01002105 module.properties.Installable = proptools.BoolPtr(true)
2106 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002107}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002108
Paul Duffindfa131e2020-05-15 20:37:11 +01002109func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2110 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2111}
2112
Jiyong Park932cdfe2020-05-28 00:19:53 +09002113func (module *SdkLibrary) defaultsToStubs() bool {
2114 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2115}
2116
Paul Duffin1b1e8062020-05-08 13:44:43 +01002117// Defines how to name the individual component modules the sdk library creates.
2118type sdkLibraryComponentNamingScheme interface {
2119 stubsLibraryModuleName(scope *apiScope, baseName string) string
2120
2121 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002122
2123 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002124
2125 sourceStubLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002126}
2127
2128type defaultNamingScheme struct {
2129}
2130
2131func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2132 return scope.stubsLibraryModuleName(baseName)
2133}
2134
2135func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2136 return scope.stubsSourceModuleName(baseName)
2137}
2138
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002139func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2140 return scope.apiLibraryModuleName(baseName)
2141}
2142
Jihoon Kang1147b312023-06-08 23:25:57 +00002143func (s *defaultNamingScheme) sourceStubLibraryModuleName(scope *apiScope, baseName string) string {
2144 return scope.sourceStubLibraryModuleName(baseName)
2145}
2146
Paul Duffin1b1e8062020-05-08 13:44:43 +01002147var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2148
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002149func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002150 name = strings.TrimSuffix(name, ".from-source")
2151
Anton Hansson2d0c1942020-05-25 12:20:51 +01002152 // This suffix-based approach is fragile and could potentially mis-trigger.
2153 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01002154 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
2155 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2156 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2157 return false, javaPlatform
2158 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002159 return true, javaSdk
2160 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002161 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002162 return true, javaSystem
2163 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002164 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002165 return true, javaModule
2166 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002167 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002168 return true, javaSystem
2169 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002170 if strings.HasSuffix(name, apiScopeSystemServer.stubsLibraryModuleNameSuffix()) {
2171 return true, javaSystemServer
2172 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002173 return false, javaPlatform
2174}
2175
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002176// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2177// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2178// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2179// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2180// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002181func SdkLibraryFactory() android.Module {
2182 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002183
2184 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002185 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002186
Inseob Kimc0907f12019-02-08 21:00:45 +09002187 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002188 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002189 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002190
2191 // Initialize the map from scope to scope specific properties.
2192 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2193 for _, scope := range allApiScopes {
2194 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2195 }
2196 module.scopeToProperties = scopeToProperties
2197
Paul Duffin4911a892020-04-29 23:35:13 +01002198 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002199 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002200 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2201 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2202
Paul Duffin1b1e8062020-05-08 13:44:43 +01002203 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002204 // If no implementation is required then it cannot be used as a shared library
2205 // either.
2206 if !module.requiresRuntimeImplementationLibrary() {
2207 // If shared_library has been explicitly set to true then it is incompatible
2208 // with api_only: true.
2209 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2210 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2211 }
2212 // Set shared_library: false.
2213 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2214 }
2215
Paul Duffin1b1e8062020-05-08 13:44:43 +01002216 if module.initCommonAfterDefaultsApplied(ctx) {
2217 module.CreateInternalModules(ctx)
2218 }
2219 })
Zi Wangb2179e32023-01-31 15:53:30 -08002220 android.InitBazelModule(module)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002221 return module
2222}
Colin Cross79c7c262019-04-17 11:11:46 -07002223
Zi Wangb2179e32023-01-31 15:53:30 -08002224type bazelSdkLibraryAttributes struct {
2225 Public bazel.StringAttribute
2226 System bazel.StringAttribute
2227 Test bazel.StringAttribute
2228 Module_lib bazel.StringAttribute
2229 System_server bazel.StringAttribute
2230}
2231
2232// java_sdk_library bp2build converter
2233func (module *SdkLibrary) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
2234 if ctx.ModuleType() != "java_sdk_library" {
2235 return
2236 }
2237
2238 nameToAttr := make(map[string]bazel.StringAttribute)
2239
2240 for _, scope := range module.getGeneratedApiScopes(ctx) {
2241 apiSurfaceFile := path.Join(module.getApiDir(), scope.apiFilePrefix+"current.txt")
2242 var scopeStringAttribute bazel.StringAttribute
2243 scopeStringAttribute.SetValue(apiSurfaceFile)
2244 nameToAttr[scope.name] = scopeStringAttribute
2245 }
2246
2247 attrs := bazelSdkLibraryAttributes{
2248 Public: nameToAttr["public"],
2249 System: nameToAttr["system"],
2250 Test: nameToAttr["test"],
2251 Module_lib: nameToAttr["module-lib"],
2252 System_server: nameToAttr["system-server"],
2253 }
2254 props := bazel.BazelTargetModuleProperties{
2255 Rule_class: "java_sdk_library",
2256 Bzl_load_location: "//build/bazel/rules/java:sdk_library.bzl",
2257 }
2258
2259 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs)
2260}
2261
Colin Cross79c7c262019-04-17 11:11:46 -07002262//
2263// SDK library prebuilts
2264//
2265
Paul Duffin56d44902020-01-31 13:36:25 +00002266// Properties associated with each api scope.
2267type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002268 Jars []string `android:"path"`
2269
2270 Sdk_version *string
2271
Colin Cross79c7c262019-04-17 11:11:46 -07002272 // List of shared java libs that this module has dependencies to
2273 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002274
Paul Duffinc8782502020-04-29 20:45:27 +01002275 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002276 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002277
2278 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002279 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002280
2281 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002282 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002283
2284 // Annotation zip
2285 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002286}
2287
Paul Duffin56d44902020-01-31 13:36:25 +00002288type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002289 // List of shared java libs, common to all scopes, that this module has
2290 // dependencies to
2291 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002292
2293 // If set to true, compile dex files for the stubs. Defaults to false.
2294 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002295
2296 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002297 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00002298}
2299
Paul Duffineedc5d52020-06-12 17:46:39 +01002300type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002301 android.ModuleBase
2302 android.DefaultableModuleBase
2303 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002304 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002305
Paul Duffin37856732021-02-26 14:24:15 +00002306 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002307 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002308
Colin Cross79c7c262019-04-17 11:11:46 -07002309 properties sdkLibraryImportProperties
2310
Paul Duffin46a26a82020-04-07 19:27:04 +01002311 // Map from api scope to the scope specific property structure.
2312 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2313
Paul Duffin56d44902020-01-31 13:36:25 +00002314 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002315
2316 // The reference to the implementation library created by the source module.
2317 // Is nil if the source module does not exist.
2318 implLibraryModule *Library
2319
2320 // The reference to the xml permissions module created by the source module.
2321 // Is nil if the source module does not exist.
2322 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002323
Jeongik Chad5fe8782021-07-08 01:13:11 +09002324 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002325 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09002326
2327 // Expected install file path of the source module(sdk_library)
2328 // or dex implementation jar obtained from the prebuilt_apex, if any.
2329 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002330}
2331
Paul Duffineedc5d52020-06-12 17:46:39 +01002332var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002333
Paul Duffin46a26a82020-04-07 19:27:04 +01002334// The type of a structure that contains a field of type sdkLibraryScopeProperties
2335// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002336//
2337// struct {
2338// Public sdkLibraryScopeProperties
2339// System sdkLibraryScopeProperties
2340// ...
2341// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002342var allScopeStructType = createAllScopePropertiesStructType()
2343
2344// Dynamically create a structure type for each apiscope in allApiScopes.
2345func createAllScopePropertiesStructType() reflect.Type {
2346 var fields []reflect.StructField
2347 for _, apiScope := range allApiScopes {
2348 field := reflect.StructField{
2349 Name: apiScope.fieldName,
2350 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2351 }
2352 fields = append(fields, field)
2353 }
2354
2355 return reflect.StructOf(fields)
2356}
2357
2358// Create an instance of the scope specific structure type and return a map
2359// from apiscope to a pointer to each scope specific field.
2360func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2361 allScopePropertiesPtr := reflect.New(allScopeStructType)
2362 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2363 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2364
2365 for _, apiScope := range allApiScopes {
2366 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2367 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2368 }
2369
2370 return allScopePropertiesPtr.Interface(), scopeProperties
2371}
2372
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002373// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002374func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002375 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002376
Paul Duffin46a26a82020-04-07 19:27:04 +01002377 allScopeProperties, scopeToProperties := createPropertiesInstance()
2378 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002379 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002380
Paul Duffinc3091c82020-05-08 14:16:20 +01002381 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002382 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002383
Paul Duffin0bdcb272020-02-06 15:24:57 +00002384 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002385 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002386 InitJavaModule(module, android.HostAndDeviceSupported)
2387
Paul Duffin1b1e8062020-05-08 13:44:43 +01002388 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2389 if module.initCommonAfterDefaultsApplied(mctx) {
2390 module.createInternalModules(mctx)
2391 }
2392 })
Colin Cross79c7c262019-04-17 11:11:46 -07002393 return module
2394}
2395
Paul Duffin630b11e2021-07-15 13:35:26 +01002396var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2397
2398func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2399 return module.properties.Permitted_packages
2400}
2401
Paul Duffineedc5d52020-06-12 17:46:39 +01002402func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002403 return &module.prebuilt
2404}
2405
Paul Duffineedc5d52020-06-12 17:46:39 +01002406func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002407 return module.prebuilt.Name(module.ModuleBase.Name())
2408}
2409
Paul Duffineedc5d52020-06-12 17:46:39 +01002410func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002411
Paul Duffin50061512020-01-21 16:31:05 +00002412 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002413 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002414 module.prebuilt.ForcePrefer()
2415 }
2416
Paul Duffin46a26a82020-04-07 19:27:04 +01002417 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002418 if len(scopeProperties.Jars) == 0 {
2419 continue
2420 }
2421
Paul Duffinbbb546b2020-04-09 00:07:11 +01002422 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002423
Paul Duffin0f8faff2020-05-20 16:18:00 +01002424 if len(scopeProperties.Stub_srcs) > 0 {
2425 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2426 }
Paul Duffin56d44902020-01-31 13:36:25 +00002427 }
Colin Cross79c7c262019-04-17 11:11:46 -07002428
2429 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2430 javaSdkLibrariesLock.Lock()
2431 defer javaSdkLibrariesLock.Unlock()
2432 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2433}
2434
Paul Duffineedc5d52020-06-12 17:46:39 +01002435func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002436 // Creates a java import for the jar with ".stubs" suffix
2437 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002438 Name *string
2439 Sdk_version *string
2440 Libs []string
2441 Jars []string
Paul Duffin1267d872021-04-16 17:21:36 +01002442 Compile_dex *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002443
2444 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002445 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002446 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002447 props.Sdk_version = scopeProperties.Sdk_version
2448 // Prepend any of the libs from the legacy public properties to the libs for each of the
2449 // scopes to avoid having to duplicate them in each scope.
2450 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2451 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002452
Paul Duffin38b57852020-05-13 16:08:09 +01002453 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002454 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002455
Paul Duffin1267d872021-04-16 17:21:36 +01002456 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002457 compileDex := module.properties.Compile_dex
2458 if module.stubLibrariesCompiledForDex() {
2459 compileDex = proptools.BoolPtr(true)
2460 }
2461 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002462
Paul Duffin859fe962020-05-15 10:20:31 +01002463 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002464}
2465
Paul Duffineedc5d52020-06-12 17:46:39 +01002466func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002467 props := struct {
Paul Duffinbf4de042022-09-27 12:41:52 +01002468 Name *string
2469 Srcs []string
2470
2471 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002472 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002473 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002474 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002475
2476 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002477 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2478
2479 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002480}
2481
Paul Duffin44f1d842020-06-26 20:17:02 +01002482// Add the dependencies on the child module in the component deps mutator so that it
2483// creates references to the prebuilt and not the source modules.
2484func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002485 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002486 if len(scopeProperties.Jars) == 0 {
2487 continue
2488 }
2489
2490 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002491 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002492
2493 if len(scopeProperties.Stub_srcs) > 0 {
2494 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002495 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002496 }
Paul Duffin56d44902020-01-31 13:36:25 +00002497 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002498}
2499
2500// Add other dependencies as normal.
2501func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002502
2503 implName := module.implLibraryModuleName()
2504 if ctx.OtherModuleExists(implName) {
2505 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2506
2507 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2508 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2509 // Add dependency to the rule for generating the xml permissions file
2510 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2511 }
2512 }
Colin Cross79c7c262019-04-17 11:11:46 -07002513}
2514
Jiakai Zhang204356f2021-09-09 08:12:46 +00002515func (module *SdkLibraryImport) AndroidMkEntries() []android.AndroidMkEntries {
2516 // For an SDK library imported from a prebuilt APEX, we don't need a Make module for itself, as we
2517 // don't need to install it. However, we need to add its dexpreopt outputs as sub-modules, if it
2518 // is preopted.
2519 dexpreoptEntries := module.dexpreopter.AndroidMkEntriesForApex()
2520 return append(dexpreoptEntries, android.AndroidMkEntries{Disabled: true})
2521}
2522
Jiyong Park45bf82e2020-12-15 22:29:02 +09002523var _ android.ApexModule = (*SdkLibraryImport)(nil)
2524
2525// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002526func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2527 depTag := mctx.OtherModuleDependencyTag(dep)
2528 if depTag == xmlPermissionsFileTag {
2529 return true
2530 }
2531
2532 // None of the other dependencies of the java_sdk_library_import are in the same apex
2533 // as the one that references this module.
2534 return false
2535}
2536
Jiyong Park45bf82e2020-12-15 22:29:02 +09002537// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002538func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2539 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002540 // we don't check prebuilt modules for sdk_version
2541 return nil
2542}
2543
Paul Duffinea8f8082021-06-24 13:25:57 +01002544// Implements android.ApexModule
2545func (module *SdkLibraryImport) UniqueApexVariations() bool {
2546 return module.uniqueApexVariations()
2547}
2548
Paul Duffin09817d62022-04-28 17:45:11 +01002549// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002550func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2551 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002552}
2553
2554var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2555
Paul Duffineedc5d52020-06-12 17:46:39 +01002556func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002557 paths, err := module.commonOutputFiles(tag)
2558 if paths != nil || err != nil {
2559 return paths, err
2560 }
2561 if module.implLibraryModule != nil {
2562 return module.implLibraryModule.OutputFiles(tag)
2563 } else {
2564 return nil, nil
2565 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002566}
2567
Paul Duffineedc5d52020-06-12 17:46:39 +01002568func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002569 module.generateCommonBuildActions(ctx)
2570
Jeongik Chad5fe8782021-07-08 01:13:11 +09002571 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2572 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2573
Paul Duffin0f8faff2020-05-20 16:18:00 +01002574 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002575 ctx.VisitDirectDeps(func(to android.Module) {
2576 tag := ctx.OtherModuleDependencyTag(to)
2577
Paul Duffin0f8faff2020-05-20 16:18:00 +01002578 // Extract information from any of the scope specific dependencies.
2579 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2580 apiScope := scopeTag.apiScope
2581 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2582
2583 // Extract information from the dependency. The exact information extracted
2584 // is determined by the nature of the dependency which is determined by the tag.
2585 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002586 } else if tag == implLibraryTag {
2587 if implLibrary, ok := to.(*Library); ok {
2588 module.implLibraryModule = implLibrary
2589 } else {
2590 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2591 }
2592 } else if tag == xmlPermissionsFileTag {
2593 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2594 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2595 } else {
2596 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2597 }
Colin Cross79c7c262019-04-17 11:11:46 -07002598 }
2599 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002600
2601 // Populate the scope paths with information from the properties.
2602 for apiScope, scopeProperties := range module.scopeProperties {
2603 if len(scopeProperties.Jars) == 0 {
2604 continue
2605 }
2606
2607 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002608 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002609 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2610 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2611 }
Paul Duffin39853512021-02-26 11:09:39 +00002612
2613 if ctx.Device() {
2614 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2615 // obtained from the associated deapexer module.
2616 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2617 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002618 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01002619 di := android.FindDeapexerProviderForModule(ctx)
2620 if di == nil {
2621 return // An error has been reported by FindDeapexerProviderForModule.
2622 }
Jiakai Zhang81e46812023-02-08 21:56:07 +08002623 dexJarFileApexRootRelative := apexRootRelativePathToJavaLib(module.BaseModuleName())
2624 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002625 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2626 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002627 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002628 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002629 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002630 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002631
Jiakai Zhang204356f2021-09-09 08:12:46 +00002632 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2633 module.dexpreopter.isSDKLibrary = true
2634 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002635
2636 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2637 module.dexpreopter.inputProfilePathOnHost = profilePath
2638 }
2639
2640 // Dexpreopting.
Jiakai Zhang204356f2021-09-09 08:12:46 +00002641 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002642 } else {
2643 // This should never happen as a variant for a prebuilt_apex is only created if the
2644 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002645 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002646 }
2647 }
2648 }
Colin Cross79c7c262019-04-17 11:11:46 -07002649}
2650
Jiyong Parkf1691d22021-03-29 20:11:58 +09002651func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002652
2653 // For consistency with SdkLibrary make the implementation jar available to libraries that
2654 // are within the same APEX.
2655 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002656 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002657 if headerJars {
2658 return implLibraryModule.HeaderJars()
2659 } else {
2660 return implLibraryModule.ImplementationJars()
2661 }
2662 }
2663
Paul Duffin23970f42020-05-20 14:20:02 +01002664 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002665}
2666
Colin Cross79c7c262019-04-17 11:11:46 -07002667// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002668func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002669 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002670 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002671}
2672
2673// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002674func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002675 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002676 return module.sdkJars(ctx, sdkVersion, false)
2677}
2678
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002679// to satisfy UsesLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002680func (module *SdkLibraryImport) DexJarBuildPath() OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002681 // The dex implementation jar extracted from the .apex file should be used in preference to the
2682 // source.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002683 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002684 return module.dexJarFile
2685 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002686 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002687 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002688 } else {
2689 return module.implLibraryModule.DexJarBuildPath()
2690 }
2691}
2692
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002693// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002694func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002695 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002696}
2697
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002698// to satisfy UsesLibraryDependency interface
2699func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2700 return nil
2701}
2702
Paul Duffineedc5d52020-06-12 17:46:39 +01002703// to satisfy apex.javaDependency interface
2704func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2705 if module.implLibraryModule == nil {
2706 return nil
2707 } else {
2708 return module.implLibraryModule.JacocoReportClassesFile()
2709 }
2710}
2711
2712// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002713func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2714 if module.implLibraryModule == nil {
2715 return LintDepSets{}
2716 } else {
2717 return module.implLibraryModule.LintDepSets()
2718 }
2719}
2720
Spandan Das17854f52022-01-14 21:19:14 +00002721func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002722 if module.implLibraryModule == nil {
2723 return false
2724 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002725 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002726 }
2727}
2728
Spandan Das17854f52022-01-14 21:19:14 +00002729func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002730 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002731 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002732 }
2733}
2734
Colin Cross08dca382020-07-21 20:31:17 -07002735// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002736func (module *SdkLibraryImport) Stem() string {
2737 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002738}
Jiyong Parke3833882020-02-17 17:28:10 +09002739
Paul Duffin44b481b2020-06-17 16:59:43 +01002740var _ ApexDependency = (*SdkLibraryImport)(nil)
2741
2742// to satisfy java.ApexDependency interface
2743func (module *SdkLibraryImport) HeaderJars() android.Paths {
2744 if module.implLibraryModule == nil {
2745 return nil
2746 } else {
2747 return module.implLibraryModule.HeaderJars()
2748 }
2749}
2750
2751// to satisfy java.ApexDependency interface
2752func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2753 if module.implLibraryModule == nil {
2754 return nil
2755 } else {
2756 return module.implLibraryModule.ImplementationAndResourcesJars()
2757 }
2758}
2759
Jiakai Zhang204356f2021-09-09 08:12:46 +00002760// to satisfy java.DexpreopterInterface interface
2761func (module *SdkLibraryImport) IsInstallable() bool {
2762 return true
2763}
2764
Paul Duffinfef55002021-06-17 14:56:05 +01002765var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2766
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002767func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002768 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08002769 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01002770}
2771
Jiyong Parke3833882020-02-17 17:28:10 +09002772// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09002773type sdkLibraryXml struct {
2774 android.ModuleBase
2775 android.DefaultableModuleBase
2776 android.ApexModuleBase
2777
2778 properties sdkLibraryXmlProperties
2779
2780 outputFilePath android.OutputPath
2781 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002782
2783 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002784}
2785
2786type sdkLibraryXmlProperties struct {
2787 // canonical name of the lib
2788 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002789
2790 // Signals that this shared library is part of the bootclasspath starting
2791 // on the version indicated in this attribute.
2792 //
2793 // This will make platforms at this level and above to ignore
2794 // <uses-library> tags with this library name because the library is already
2795 // available
2796 On_bootclasspath_since *string
2797
2798 // Signals that this shared library was part of the bootclasspath before
2799 // (but not including) the version indicated in this attribute.
2800 //
2801 // The system will automatically add a <uses-library> tag with this library to
2802 // apps that target any SDK less than the version indicated in this attribute.
2803 On_bootclasspath_before *string
2804
2805 // Indicates that PackageManager should ignore this shared library if the
2806 // platform is below the version indicated in this attribute.
2807 //
2808 // This means that the device won't recognise this library as installed.
2809 Min_device_sdk *string
2810
2811 // Indicates that PackageManager should ignore this shared library if the
2812 // platform is above the version indicated in this attribute.
2813 //
2814 // This means that the device won't recognise this library as installed.
2815 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00002816
2817 // The SdkLibrary's min api level as a string
2818 //
2819 // This value comes from the ApiLevel of the MinSdkVersion property.
2820 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09002821}
2822
2823// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2824// Not to be used directly by users. java_sdk_library internally uses this.
2825func sdkLibraryXmlFactory() android.Module {
2826 module := &sdkLibraryXml{}
2827
2828 module.AddProperties(&module.properties)
2829
2830 android.InitApexModule(module)
2831 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2832
2833 return module
2834}
2835
Colin Crossaede88c2020-08-11 12:17:01 -07002836func (module *sdkLibraryXml) UniqueApexVariations() bool {
2837 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2838 // mounted APEX, which contains the name of the APEX.
2839 return true
2840}
2841
Jiyong Parke3833882020-02-17 17:28:10 +09002842// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002843func (module *sdkLibraryXml) BaseDir() string {
2844 return "etc"
2845}
2846
2847// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002848func (module *sdkLibraryXml) SubDir() string {
2849 return "permissions"
2850}
2851
2852// from android.PrebuiltEtcModule
2853func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2854 return module.outputFilePath
2855}
2856
2857// from android.ApexModule
2858func (module *sdkLibraryXml) AvailableFor(what string) bool {
2859 return true
2860}
2861
2862func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2863 // do nothing
2864}
2865
Jiyong Park45bf82e2020-12-15 22:29:02 +09002866var _ android.ApexModule = (*sdkLibraryXml)(nil)
2867
2868// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002869func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2870 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002871 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2872 return nil
2873}
2874
Jiyong Parke3833882020-02-17 17:28:10 +09002875// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002876func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002877 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002878 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002879 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002880 // In most cases, this works fine. But when apex_name is set or override_apex is used
2881 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002882 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002883 }
2884 partition := "system"
2885 if module.SocSpecific() {
2886 partition = "vendor"
2887 } else if module.DeviceSpecific() {
2888 partition = "odm"
2889 } else if module.ProductSpecific() {
2890 partition = "product"
2891 } else if module.SystemExtSpecific() {
2892 partition = "system_ext"
2893 }
2894 return "/" + partition + "/framework/" + implName + ".jar"
2895}
2896
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002897func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
2898 if value == nil {
2899 return ""
2900 }
2901 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
2902 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002903 // attributes in bp files have underscores but in the xml have dashes.
2904 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002905 return ""
2906 }
Pedro Loureirob638c622021-12-22 15:28:05 +00002907 if apiLevel.IsCurrent() {
2908 // passing "current" would always mean a future release, never the current (or the current in
2909 // progress) which means some conditions would never be triggered.
2910 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
2911 `"current" is not an allowed value for this attribute`)
2912 return ""
2913 }
Pedro Loureiro48991222022-06-17 20:01:21 +00002914 // "safeValue" is safe because it translates finalized codenames to a string
2915 // with their SDK int.
2916 safeValue := apiLevel.String()
2917 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002918}
2919
2920// formats an attribute for the xml permissions file if the value is not null
2921// returns empty string otherwise
2922func formattedOptionalAttribute(attrName string, value *string) string {
2923 if value == nil {
2924 return ""
2925 }
2926 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
2927}
2928
2929func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
2930 libName := proptools.String(module.properties.Lib_name)
2931 libNameAttr := formattedOptionalAttribute("name", &libName)
2932 filePath := module.implPath(ctx)
2933 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002934 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
2935 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
2936 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
2937 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002938 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
2939 // 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 +00002940 var libraryTag string
2941 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002942 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00002943 } else {
2944 libraryTag = ` <library\n`
2945 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002946
2947 return strings.Join([]string{
2948 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
2949 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
2950 `\n`,
2951 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
2952 ` you may not use this file except in compliance with the License.\n`,
2953 ` You may obtain a copy of the License at\n`,
2954 `\n`,
2955 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
2956 `\n`,
2957 ` Unless required by applicable law or agreed to in writing, software\n`,
2958 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
2959 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
2960 ` See the License for the specific language governing permissions and\n`,
2961 ` limitations under the License.\n`,
2962 `-->\n`,
2963 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00002964 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002965 libNameAttr,
2966 filePathAttr,
2967 implicitFromAttr,
2968 implicitUntilAttr,
2969 minSdkAttr,
2970 maxSdkAttr,
2971 ` />\n`,
2972 `</permissions>\n`}, "")
2973}
2974
Jiyong Parke3833882020-02-17 17:28:10 +09002975func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07002976 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
2977
Jiyong Parke3833882020-02-17 17:28:10 +09002978 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002979 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002980 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002981
2982 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08002983 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002984 rule.Command().
2985 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2986 Output(module.outputFilePath)
2987
Colin Crossf1a035e2020-11-16 17:32:30 -08002988 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09002989
2990 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2991}
2992
2993func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07002994 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00002995 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09002996 Disabled: true,
2997 }}
2998 }
2999
satayev8f088b02021-12-06 11:40:46 +00003000 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003001 Class: "ETC",
3002 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3003 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003004 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003005 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003006 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003007 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3008 },
3009 },
3010 }}
3011}
Paul Duffindd46f712020-02-10 13:37:10 +00003012
Pedro Loureiroc3621422021-09-28 15:40:23 +00003013func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3014 module.validateAtLeastTAttributes(ctx)
3015 module.validateMinAndMaxDeviceSdk(ctx)
3016 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3017 module.validateOnBootclasspathBeforeRequirements(ctx)
3018}
3019
3020func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3021 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3022 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3023 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3024 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3025 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3026}
3027
3028func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3029 if attr != nil {
3030 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3031 // we will inform the user of invalid inputs when we try to write the
3032 // permissions xml file so we don't need to do it here
3033 if t.GreaterThan(level) {
3034 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3035 }
3036 }
3037 }
3038}
3039
3040func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3041 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3042 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3043 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3044 if minErr == nil && maxErr == nil {
3045 // we will inform the user of invalid inputs when we try to write the
3046 // permissions xml file so we don't need to do it here
3047 if min.GreaterThan(max) {
3048 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3049 }
3050 }
3051 }
3052}
3053
3054func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3055 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3056 if module.properties.Min_device_sdk != nil {
3057 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3058 if err == nil {
3059 if moduleMinApi.GreaterThan(api) {
3060 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3061 }
3062 }
3063 }
3064 if module.properties.Max_device_sdk != nil {
3065 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3066 if err == nil {
3067 if moduleMinApi.GreaterThan(api) {
3068 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3069 }
3070 }
3071 }
3072}
3073
3074func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3075 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3076 if module.properties.On_bootclasspath_before != nil {
3077 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3078 // if we use the attribute, then we need to do this validation
3079 if moduleMinApi.LessThan(t) {
3080 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3081 if module.properties.Min_device_sdk == nil {
3082 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")
3083 }
3084 }
3085 }
3086}
3087
Paul Duffindd46f712020-02-10 13:37:10 +00003088type sdkLibrarySdkMemberType struct {
3089 android.SdkMemberTypeBase
3090}
3091
Paul Duffin296701e2021-07-14 10:29:36 +01003092func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3093 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003094}
3095
3096func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3097 _, ok := module.(*SdkLibrary)
3098 return ok
3099}
3100
3101func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3102 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3103}
3104
3105func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3106 return &sdkLibrarySdkMemberProperties{}
3107}
3108
Paul Duffin976b0e52021-04-27 23:20:26 +01003109var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3110 android.SdkMemberTypeBase{
3111 PropertyName: "java_sdk_libs",
3112 SupportsSdk: true,
3113 },
3114}
3115
Paul Duffindd46f712020-02-10 13:37:10 +00003116type sdkLibrarySdkMemberProperties struct {
3117 android.SdkMemberPropertiesBase
3118
Paul Duffine8409952022-09-22 16:24:46 +01003119 // Stem name for files in the sdk snapshot.
3120 //
3121 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3122 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3123 //
3124 // This property is marked as keep so that it will be kept in all instances of this struct, will
3125 // not be cleared but will be copied to common structs. That is needed because this field is used
3126 // to construct many file names for other parts of this struct and so it needs to be present in
3127 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3128 // be unavailable for generating file names if there were other properties that were still set.
3129 Stem string `sdk:"keep"`
3130
Paul Duffindd46f712020-02-10 13:37:10 +00003131 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003132 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003133
Paul Duffin3d1248c2020-04-09 00:10:17 +01003134 // The Java stubs source files.
3135 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003136
3137 // The naming scheme.
3138 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003139
3140 // True if the java_sdk_library_import is for a shared library, false
3141 // otherwise.
3142 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003143
Paul Duffin1267d872021-04-16 17:21:36 +01003144 // True if the stub imports should produce dex jars.
3145 Compile_dex *bool
3146
Paul Duffina2ae7e02020-09-11 11:55:00 +01003147 // The paths to the doctag files to add to the prebuilt.
3148 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003149
3150 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003151
3152 // Signals that this shared library is part of the bootclasspath starting
3153 // on the version indicated in this attribute.
3154 //
3155 // This will make platforms at this level and above to ignore
3156 // <uses-library> tags with this library name because the library is already
3157 // available
3158 On_bootclasspath_since *string
3159
3160 // Signals that this shared library was part of the bootclasspath before
3161 // (but not including) the version indicated in this attribute.
3162 //
3163 // The system will automatically add a <uses-library> tag with this library to
3164 // apps that target any SDK less than the version indicated in this attribute.
3165 On_bootclasspath_before *string
3166
3167 // Indicates that PackageManager should ignore this shared library if the
3168 // platform is below the version indicated in this attribute.
3169 //
3170 // This means that the device won't recognise this library as installed.
3171 Min_device_sdk *string
3172
3173 // Indicates that PackageManager should ignore this shared library if the
3174 // platform is above the version indicated in this attribute.
3175 //
3176 // This means that the device won't recognise this library as installed.
3177 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003178
3179 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003180}
3181
3182type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003183 Jars android.Paths
3184 StubsSrcJar android.Path
3185 CurrentApiFile android.Path
3186 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003187 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003188 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003189}
3190
3191func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3192 sdk := variant.(*SdkLibrary)
3193
Paul Duffine8409952022-09-22 16:24:46 +01003194 // Copy the stem name for files in the sdk snapshot.
3195 s.Stem = sdk.distStem()
3196
Paul Duffin106a3a42022-01-27 16:39:06 +00003197 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003198 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003199 paths := sdk.findScopePaths(apiScope)
3200 if paths == nil {
3201 continue
3202 }
3203
Paul Duffindd46f712020-02-10 13:37:10 +00003204 jars := paths.stubsImplPath
3205 if len(jars) > 0 {
3206 properties := scopeProperties{}
3207 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003208 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003209 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003210 if paths.currentApiFilePath.Valid() {
3211 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3212 }
3213 if paths.removedApiFilePath.Valid() {
3214 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3215 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003216 // The annotations zip is only available for modules that set annotations_enabled: true.
3217 if paths.annotationsZip.Valid() {
3218 properties.AnnotationsZip = paths.annotationsZip.Path()
3219 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003220 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003221 }
3222 }
3223
Paul Duffindfa131e2020-05-15 20:37:11 +01003224 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003225 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003226 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003227 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003228 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003229 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3230 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3231 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3232 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003233
3234 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3235 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3236 }
Paul Duffindd46f712020-02-10 13:37:10 +00003237}
3238
3239func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003240 if s.Naming_scheme != nil {
3241 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3242 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003243 if s.Shared_library != nil {
3244 propertySet.AddProperty("shared_library", *s.Shared_library)
3245 }
Paul Duffin1267d872021-04-16 17:21:36 +01003246 if s.Compile_dex != nil {
3247 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3248 }
Paul Duffin869de142021-07-15 14:14:41 +01003249 if len(s.Permitted_packages) > 0 {
3250 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3251 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003252 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3253 if s.DexPreoptProfileGuided != nil {
3254 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3255 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003256
Paul Duffine8409952022-09-22 16:24:46 +01003257 stem := s.Stem
3258
Paul Duffindd46f712020-02-10 13:37:10 +00003259 for _, apiScope := range allApiScopes {
3260 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003261 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003262
Paul Duffin958806b2022-05-16 13:10:47 +00003263 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003264
Paul Duffindd46f712020-02-10 13:37:10 +00003265 var jars []string
3266 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003267 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003268 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3269 jars = append(jars, dest)
3270 }
3271 scopeSet.AddProperty("jars", jars)
3272
Paul Duffin22628d52021-05-12 23:13:22 +01003273 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3274 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003275 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003276 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3277 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3278 } else {
3279 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3280 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003281 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003282 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3283 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3284 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003285
Paul Duffin1fd005d2020-04-09 01:08:11 +01003286 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003287 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003288 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3289 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3290 }
3291
3292 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003293 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003294 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003295 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3296 }
3297
Anton Hanssond78eb762021-09-21 15:25:12 +01003298 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003299 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003300 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3301 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3302 }
3303
Paul Duffindd46f712020-02-10 13:37:10 +00003304 if properties.SdkVersion != "" {
3305 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3306 }
3307 }
3308 }
3309
Paul Duffina2ae7e02020-09-11 11:55:00 +01003310 if len(s.Doctag_paths) > 0 {
3311 dests := []string{}
3312 for _, p := range s.Doctag_paths {
3313 dest := filepath.Join("doctags", p.Rel())
3314 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3315 dests = append(dests, dest)
3316 }
3317 propertySet.AddProperty("doctag_files", dests)
3318 }
Paul Duffindd46f712020-02-10 13:37:10 +00003319}