blob: 103f1ace72f1e7022f195a31f86119dd42575fd5 [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
Paul Duffind1b3a922020-01-22 11:57:20 +0000159}
160
161// Initialize a scope, creating and adding appropriate dependency tags
162func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100163 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100164 scopeByName[name] = scope
165 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100166 scope.propertyName = strings.ReplaceAll(name, "-", "_")
167 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Paul Duffind1b3a922020-01-22 11:57:20 +0000168 scope.stubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100169 name: name + "-stubs",
170 apiScope: scope,
171 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000172 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100173 scope.stubsSourceTag = scopeDependencyTag{
174 name: name + "-stubs-source",
175 apiScope: scope,
176 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
177 }
178 scope.apiFileTag = scopeDependencyTag{
179 name: name + "-api",
180 apiScope: scope,
181 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
182 }
Paul Duffinc8782502020-04-29 20:45:27 +0100183 scope.stubsSourceAndApiTag = scopeDependencyTag{
184 name: name + "-stubs-source-and-api",
185 apiScope: scope,
186 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000187 }
Paul Duffin958806b2022-05-16 13:10:47 +0000188 scope.latestApiModuleTag = scopeDependencyTag{
189 name: name + "-latest-api",
190 apiScope: scope,
191 depInfoExtractor: (*scopePaths).extractLatestApiPath,
192 }
193 scope.latestRemovedApiModuleTag = scopeDependencyTag{
194 name: name + "-latest-removed-api",
195 apiScope: scope,
196 depInfoExtractor: (*scopePaths).extractLatestRemovedApiPath,
197 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100198
199 // To get the args needed to generate the stubs source append all the args from
200 // this scope and all the scopes it extends as each set of args adds additional
201 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100202 var scopeSpecificArgs []string
203 if scope.annotation != "" {
204 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100205 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100206 for s := scope; s != nil; s = s.extends {
207 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100208
Paul Duffin15f34ef2020-07-20 18:04:44 +0100209 // Ensure that the generated stubs includes all the API elements from the API scope
210 // that this scope extends.
211 if s != scope && s.annotation != "" {
212 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
213 }
214 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100215
Paul Duffind0b9fca2022-09-30 18:11:41 +0100216 // By default, a library that can access a scope can also access the scope it extends.
217 if scope.canAccess == nil {
218 scope.canAccess = scope.extends
219 }
220
Paul Duffin15f34ef2020-07-20 18:04:44 +0100221 // Escape any special characters in the arguments. This is needed because droidstubs
222 // passes these directly to the shell command.
223 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100224
Paul Duffind1b3a922020-01-22 11:57:20 +0000225 return scope
226}
227
Anton Hansson08f476b2021-04-07 15:32:19 +0100228func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
229 return ".stubs" + scope.moduleSuffix
230}
231
Paul Duffinc3091c82020-05-08 14:16:20 +0100232func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100233 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000234}
235
Paul Duffinc8782502020-04-29 20:45:27 +0100236func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100237 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000238}
239
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100240func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100241 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100242}
243
Paul Duffin3375e352020-04-28 10:44:03 +0100244func (scope *apiScope) String() string {
245 return scope.name
246}
247
Paul Duffin958806b2022-05-16 13:10:47 +0000248// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
249// be stored.
250func (scope *apiScope) snapshotRelativeDir() string {
251 return filepath.Join("sdk_library", scope.name)
252}
253
254// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
255// library.
256func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
257 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
258}
259
260// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
261// named library.
262func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
263 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
264}
265
Paul Duffind1b3a922020-01-22 11:57:20 +0000266type apiScopes []*apiScope
267
268func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
269 var list []string
270 for _, scope := range scopes {
271 list = append(list, accessor(scope))
272 }
273 return list
274}
275
Jiyong Parkc678ad32018-04-10 13:07:10 +0900276var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100277 scopeByName = make(map[string]*apiScope)
278 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000279 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100280 name: "public",
281
282 // Public scope is enabled by default for both legacy and non-legacy modes.
283 legacyEnabledStatus: func(module *SdkLibrary) bool {
284 return true
285 },
286 defaultEnabledStatus: true,
287
288 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
289 return &module.sdkLibraryProperties.Public
290 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000291 sdkVersion: "current",
292 })
293 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100294 name: "system",
295 extends: apiScopePublic,
296 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
297 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
298 return &module.sdkLibraryProperties.System
299 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100300 apiFilePrefix: "system-",
301 moduleSuffix: ".system",
302 sdkVersion: "system_current",
303 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Paul Duffind1b3a922020-01-22 11:57:20 +0000304 })
305 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100306 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100307 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100308 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
309 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
310 return &module.sdkLibraryProperties.Test
311 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100312 apiFilePrefix: "test-",
313 moduleSuffix: ".test",
314 sdkVersion: "test_current",
315 annotation: "android.annotation.TestApi",
316 unstable: true,
Paul Duffind1b3a922020-01-22 11:57:20 +0000317 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100318 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100319 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100320 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100321 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100322 //
323 // Enabling this would break existing usages.
324 legacyEnabledStatus: func(module *SdkLibrary) bool {
325 return false
326 },
327 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
328 return &module.sdkLibraryProperties.Module_lib
329 },
330 apiFilePrefix: "module-lib-",
331 moduleSuffix: ".module_lib",
332 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100333 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Paul Duffin8f265b92020-04-28 14:13:56 +0100334 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100335 apiScopeSystemServer = initApiScope(&apiScope{
336 name: "system-server",
337 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100338
339 // The system-server scope can access the module-lib scope.
340 //
341 // A module that provides a system-server API is appended to the standard bootclasspath that is
342 // used by the system server. So, it should be able to access module-lib APIs provided by
343 // libraries on the bootclasspath.
344 canAccess: apiScopeModuleLib,
345
Paul Duffin0c5bae52020-06-02 13:00:08 +0100346 // The system-server scope is disabled by default in legacy mode.
347 //
348 // Enabling this would break existing usages.
349 legacyEnabledStatus: func(module *SdkLibrary) bool {
350 return false
351 },
352 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
353 return &module.sdkLibraryProperties.System_server
354 },
355 apiFilePrefix: "system-server-",
356 moduleSuffix: ".system_server",
357 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100358 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
359 extraArgs: []string{
360 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100361 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100362 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100363 },
364 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000365 allApiScopes = apiScopes{
366 apiScopePublic,
367 apiScopeSystem,
368 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100369 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100370 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000371 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900372)
373
Jiyong Park82484c02018-04-23 21:41:26 +0900374var (
375 javaSdkLibrariesLock sync.Mutex
376)
377
Jiyong Parkc678ad32018-04-10 13:07:10 +0900378// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900379// 1) disallowing linking to the runtime shared lib
380// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900381
382func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000383 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900384
Jiyong Park82484c02018-04-23 21:41:26 +0900385 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
386 javaSdkLibraries := javaSdkLibraries(ctx.Config())
387 sort.Strings(*javaSdkLibraries)
388 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
389 })
Paul Duffindd46f712020-02-10 13:37:10 +0000390
391 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100392 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900393}
394
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000395func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
396 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
397 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
398}
399
Paul Duffin3375e352020-04-28 10:44:03 +0100400// Properties associated with each api scope.
401type ApiScopeProperties struct {
402 // Indicates whether the api surface is generated.
403 //
404 // If this is set for any scope then all scopes must explicitly specify if they
405 // are enabled. This is to prevent new usages from depending on legacy behavior.
406 //
407 // Otherwise, if this is not set for any scope then the default behavior is
408 // scope specific so please refer to the scope specific property documentation.
409 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100410
411 // The sdk_version to use for building the stubs.
412 //
413 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000414 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100415 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000416 // will be none. This is used for java_sdk_library instances that are used
417 // to create stubs that contribute to the core_current sdk version.
418 // 2) Otherwise, it is assumed that this library extends but does not
419 // contribute directly to a specific sdk_version and so this uses the
420 // sdk_version appropriate for the api scope. e.g. public will use
421 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100422 //
423 // This does not affect the sdk_version used for either generating the stubs source
424 // or the API file. They both have to use the same sdk_version as is used for
425 // compiling the implementation library.
426 Sdk_version *string
Paul Duffin3375e352020-04-28 10:44:03 +0100427}
428
Jiyong Parkc678ad32018-04-10 13:07:10 +0900429type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100430 // List of source files that are needed to compile the API, but are not part of runtime library.
431 Api_srcs []string `android:"arch_variant"`
432
Paul Duffin5df79302020-05-16 15:52:12 +0100433 // Visibility for impl library module. If not specified then defaults to the
434 // visibility property.
435 Impl_library_visibility []string
436
Paul Duffin4911a892020-04-29 23:35:13 +0100437 // Visibility for stubs library modules. If not specified then defaults to the
438 // visibility property.
439 Stubs_library_visibility []string
440
441 // Visibility for stubs source modules. If not specified then defaults to the
442 // visibility property.
443 Stubs_source_visibility []string
444
Anton Hansson7f66efa2020-10-08 14:47:23 +0100445 // List of Java libraries that will be in the classpath when building the implementation lib
446 Impl_only_libs []string `android:"arch_variant"`
447
Paul Duffin77590a82022-04-28 14:13:30 +0000448 // List of Java libraries that will included in the implementation lib.
449 Impl_only_static_libs []string `android:"arch_variant"`
450
Sundong Ahnf043cf62018-06-25 16:04:37 +0900451 // List of Java libraries that will be in the classpath when building stubs
452 Stub_only_libs []string `android:"arch_variant"`
453
Anton Hanssondae54cd2021-04-21 16:30:10 +0100454 // List of Java libraries that will included in stub libraries
455 Stub_only_static_libs []string `android:"arch_variant"`
456
Paul Duffin7a586d32019-12-30 17:09:34 +0000457 // list of package names that will be documented and publicized as API.
458 // This allows the API to be restricted to a subset of the source files provided.
459 // If this is unspecified then all the source files will be treated as being part
460 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900461 Api_packages []string
462
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900463 // list of package names that must be hidden from the API
464 Hidden_api_packages []string
465
Paul Duffin749f98f2019-12-30 17:23:46 +0000466 // the relative path to the directory containing the api specification files.
467 // Defaults to "api".
468 Api_dir *string
469
Paul Duffindfa131e2020-05-15 20:37:11 +0100470 // Determines whether a runtime implementation library is built; defaults to false.
471 //
472 // 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 +0200473 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000474 Api_only *bool
475
Paul Duffin11512472019-02-11 15:55:17 +0000476 // local files that are used within user customized droiddoc options.
477 Droiddoc_option_files []string
478
Spandan Das93e95992021-07-29 18:26:39 +0000479 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000480 // Available variables for substitution:
481 //
482 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900483 Droiddoc_options []string
484
Paul Duffine22c2ab2020-05-20 19:35:27 +0100485 // is set to true, Metalava will allow framework SDK to contain annotations.
486 Annotations_enabled *bool
487
Sundong Ahn054b19a2018-10-19 13:46:09 +0900488 // a list of top-level directories containing files to merge qualifier annotations
489 // (i.e. those intended to be included in the stubs written) from.
490 Merge_annotations_dirs []string
491
492 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
493 Merge_inclusion_annotations_dirs []string
494
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000495 // If set to true then don't create dist rules.
496 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900497
Paul Duffin31310252020-11-20 21:26:20 +0000498 // The stem for the artifacts that are copied to the dist, if not specified
499 // then defaults to the base module name.
500 //
501 // For each scope the following artifacts are copied to the apistubs/<scope>
502 // directory in the dist.
503 // * stubs impl jar -> <dist-stem>.jar
504 // * API specification file -> api/<dist-stem>.txt
505 // * Removed API specification file -> api/<dist-stem>-removed.txt
506 //
507 // Also used to construct the name of the filegroup (created by prebuilt_apis)
508 // that references the latest released API and remove API specification files.
509 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
510 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800511 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000512 Dist_stem *string
513
Colin Cross986b69a2021-06-01 13:13:40 -0700514 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700515 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700516 // in the public Android SDK.
517 Dist_group *string
518
Anton Hanssondff2c782020-12-21 17:10:01 +0000519 // A compatibility mode that allows historical API-tracking files to not exist.
520 // Do not use.
521 Unsafe_ignore_missing_latest_api bool
522
Paul Duffin3375e352020-04-28 10:44:03 +0100523 // indicates whether system and test apis should be generated.
524 Generate_system_and_test_apis bool `blueprint:"mutated"`
525
526 // The properties specific to the public api scope
527 //
528 // Unless explicitly specified by using public.enabled the public api scope is
529 // enabled by default in both legacy and non-legacy mode.
530 Public ApiScopeProperties
531
532 // The properties specific to the system api scope
533 //
534 // In legacy mode the system api scope is enabled by default when sdk_version
535 // is set to something other than "none".
536 //
537 // In non-legacy mode the system api scope is disabled by default.
538 System ApiScopeProperties
539
540 // The properties specific to the test api scope
541 //
542 // In legacy mode the test api scope is enabled by default when sdk_version
543 // is set to something other than "none".
544 //
545 // In non-legacy mode the test api scope is disabled by default.
546 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000547
Paul Duffin0c5bae52020-06-02 13:00:08 +0100548 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100549 //
Zi Wangb2179e32023-01-31 15:53:30 -0800550 // Unless explicitly specified by using module_lib.enabled the module_lib api
551 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100552 Module_lib ApiScopeProperties
553
Paul Duffin0c5bae52020-06-02 13:00:08 +0100554 // The properties specific to the system-server api scope
555 //
Zi Wangb2179e32023-01-31 15:53:30 -0800556 // Unless explicitly specified by using system_server.enabled the
557 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100558 System_server ApiScopeProperties
559
Jiyong Park932cdfe2020-05-28 00:19:53 +0900560 // Determines if the stubs are preferred over the implementation library
561 // for linking, even when the client doesn't specify sdk_version. When this
562 // is set to true, such clients are provided with the widest API surface that
563 // this lib provides. Note however that this option doesn't affect the clients
564 // that are in the same APEX as this library. In that case, the clients are
565 // always linked with the implementation library. Default is false.
566 Default_to_stubs *bool
567
Paul Duffin160fe412020-05-10 19:32:20 +0100568 // Properties related to api linting.
569 Api_lint struct {
570 // Enable api linting.
571 Enabled *bool
572 }
573
Jiyong Parkc678ad32018-04-10 13:07:10 +0900574 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100575 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900576}
577
Paul Duffin0f8faff2020-05-20 16:18:00 +0100578// Paths to outputs from java_sdk_library and java_sdk_library_import.
579//
580// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
581// OptionalPaths are always set by java_sdk_library but may not be set by
582// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000583type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100584 // The path (represented as Paths for convenience when returning) to the stubs header jar.
585 //
586 // That is the jar that is created by turbine.
587 stubsHeaderPath android.Paths
588
589 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
590 //
591 // This is not the implementation jar, it still only contains stubs.
592 stubsImplPath android.Paths
593
Paul Duffin1267d872021-04-16 17:21:36 +0100594 // The dex jar for the stubs.
595 //
596 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100597 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100598
Paul Duffin0f8faff2020-05-20 16:18:00 +0100599 // The API specification file, e.g. system_current.txt.
600 currentApiFilePath android.OptionalPath
601
602 // The specification of API elements removed since the last release.
603 removedApiFilePath android.OptionalPath
604
605 // The stubs source jar.
606 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100607
608 // Extracted annotations.
609 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000610
611 // The path to the latest API file.
612 latestApiPath android.OptionalPath
613
614 // The path to the latest removed API file.
615 latestRemovedApiPath android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000616}
617
Colin Crossdcf71b22021-02-01 13:59:03 -0800618func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
619 if ctx.OtherModuleHasProvider(dep, JavaInfoProvider) {
620 lib := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
621 paths.stubsHeaderPath = lib.HeaderJars
622 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100623
624 libDep := dep.(UsesLibraryDependency)
625 paths.stubsDexJarPath = libDep.DexJarBuildPath()
Paul Duffinc8782502020-04-29 20:45:27 +0100626 return nil
627 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800628 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100629 }
630}
631
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100632func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
633 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
634 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100635 return nil
636 } else {
637 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
638 }
639}
640
Paul Duffin0f8faff2020-05-20 16:18:00 +0100641func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
642 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
643 action(apiStubsProvider)
644 return nil
645 } else {
646 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
647 }
648}
649
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100650func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hanssond78eb762021-09-21 15:25:12 +0100651 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100652 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
653 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100654}
655
Colin Crossdcf71b22021-02-01 13:59:03 -0800656func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100657 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
658 paths.extractApiInfoFromApiStubsProvider(provider)
659 })
660}
661
Paul Duffin0f8faff2020-05-20 16:18:00 +0100662func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
663 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100664}
665
Colin Crossdcf71b22021-02-01 13:59:03 -0800666func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100667 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100668 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
669 })
670}
671
Colin Crossdcf71b22021-02-01 13:59:03 -0800672func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(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 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
676 })
677}
678
Paul Duffin958806b2022-05-16 13:10:47 +0000679func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
680 var paths android.Paths
681 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
682 paths = sourceFileProducer.Srcs()
683 } else {
684 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
685 }
686 if len(paths) != 1 {
687 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
688 }
689 return android.OptionalPathForPath(paths[0]), nil
690}
691
692func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
693 outputPath, err := extractSingleOptionalOutputPath(dep)
694 paths.latestApiPath = outputPath
695 return err
696}
697
698func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
699 outputPath, err := extractSingleOptionalOutputPath(dep)
700 paths.latestRemovedApiPath = outputPath
701 return err
702}
703
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100704type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100705 // The naming scheme to use for the components that this module creates.
706 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100707 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100708 //
709 // This is a temporary mechanism to simplify conversion from separate modules for each
710 // component that follow a different naming pattern to the default one.
711 //
712 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100713 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100714
715 // Specifies whether this module can be used as an Android shared library; defaults
716 // to true.
717 //
718 // An Android shared library is one that can be referenced in a <uses-library> element
719 // in an AndroidManifest.xml.
720 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100721
722 // Files containing information about supported java doc tags.
723 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000724
725 // Signals that this shared library is part of the bootclasspath starting
726 // on the version indicated in this attribute.
727 //
728 // This will make platforms at this level and above to ignore
729 // <uses-library> tags with this library name because the library is already
730 // available
731 On_bootclasspath_since *string
732
733 // Signals that this shared library was part of the bootclasspath before
734 // (but not including) the version indicated in this attribute.
735 //
736 // The system will automatically add a <uses-library> tag with this library to
737 // apps that target any SDK less than the version indicated in this attribute.
738 On_bootclasspath_before *string
739
740 // Indicates that PackageManager should ignore this shared library if the
741 // platform is below the version indicated in this attribute.
742 //
743 // This means that the device won't recognise this library as installed.
744 Min_device_sdk *string
745
746 // Indicates that PackageManager should ignore this shared library if the
747 // platform is above the version indicated in this attribute.
748 //
749 // This means that the device won't recognise this library as installed.
750 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100751}
752
Paul Duffin71b33cc2021-06-23 11:39:47 +0100753// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
754// embeds the commonToSdkLibraryAndImport struct.
755type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000756 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100757
758 BaseModuleName() string
759}
760
Paul Duffin56d44902020-01-31 13:36:25 +0000761// Common code between sdk library and sdk library import
762type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100763 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100764
Paul Duffin56d44902020-01-31 13:36:25 +0000765 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100766
767 namingScheme sdkLibraryComponentNamingScheme
768
Paul Duffindfa131e2020-05-15 20:37:11 +0100769 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100770
Paul Duffina2ae7e02020-09-11 11:55:00 +0100771 // Paths to commonSdkLibraryProperties.Doctag_files
772 doctagPaths android.Paths
773
Paul Duffin859fe962020-05-15 10:20:31 +0100774 // Functionality related to this being used as a component of a java_sdk_library.
775 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000776}
777
Paul Duffin71b33cc2021-06-23 11:39:47 +0100778func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
779 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100780
Paul Duffin71b33cc2021-06-23 11:39:47 +0100781 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100782
783 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100784 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100785}
786
787func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100788 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100789 switch schemeProperty {
790 case "default":
791 c.namingScheme = &defaultNamingScheme{}
792 default:
793 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
794 return false
795 }
796
Paul Duffin3f0290e2021-06-30 18:25:36 +0100797 namePtr := proptools.StringPtr(c.module.BaseModuleName())
798 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
799
Paul Duffindfa131e2020-05-15 20:37:11 +0100800 // Only track this sdk library if this can be used as a shared library.
801 if c.sharedLibrary() {
802 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100803 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100804 }
Paul Duffin859fe962020-05-15 10:20:31 +0100805
Paul Duffin1b1e8062020-05-08 13:44:43 +0100806 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100807}
808
Paul Duffinea8f8082021-06-24 13:25:57 +0100809// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
810// method.
811func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
812 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
813 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
814 // the APEX and so it needs a unique variation per APEX.
815 return c.sharedLibrary()
816}
817
Paul Duffina2ae7e02020-09-11 11:55:00 +0100818func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
819 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
820}
821
Paul Duffineedc5d52020-06-12 17:46:39 +0100822// Module name of the runtime implementation library
823func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100824 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100825}
826
827// Module name of the XML file for the lib
828func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100829 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100830}
831
Paul Duffinc3091c82020-05-08 14:16:20 +0100832// Name of the java_library module that compiles the stubs source.
833func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100834 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000835 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100836}
837
838// Name of the droidstubs module that generates the stubs source and may also
839// generate/check the API.
840func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100841 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000842 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100843}
844
Paul Duffin46dc45a2020-05-14 15:39:10 +0100845// The component names for different outputs of the java_sdk_library.
846//
847// They are similar to the names used for the child modules it creates
848const (
849 stubsSourceComponentName = "stubs.source"
850
851 apiTxtComponentName = "api.txt"
852
853 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +0100854
855 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +0100856)
857
858// A regular expression to match tags that reference a specific stubs component.
859//
860// It will only match if given a valid scope and a valid component. It is verfy strict
861// to ensure it does not accidentally match a similar looking tag that should be processed
862// by the embedded Library.
863var tagSplitter = func() *regexp.Regexp {
864 // Given a list of literal string items returns a regular expression that will
865 // match any one of the items.
866 choice := func(items ...string) string {
867 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
868 }
869
870 // Regular expression to match one of the scopes.
871 scopesRegexp := choice(allScopeNames...)
872
873 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +0100874 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100875
876 // Regular expression to match any combination of one scope and one component.
877 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
878}()
879
880// For OutputFileProducer interface
881//
Anton Hanssond78eb762021-09-21 15:25:12 +0100882// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100883func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
884 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
885 scopeName := groups[1]
886 component := groups[2]
887
888 if scope, ok := scopeByName[scopeName]; ok {
889 paths := c.findScopePaths(scope)
890 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100891 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +0100892 }
893
894 switch component {
895 case stubsSourceComponentName:
896 if paths.stubsSrcJar.Valid() {
897 return android.Paths{paths.stubsSrcJar.Path()}, nil
898 }
899
900 case apiTxtComponentName:
901 if paths.currentApiFilePath.Valid() {
902 return android.Paths{paths.currentApiFilePath.Path()}, nil
903 }
904
905 case removedApiTxtComponentName:
906 if paths.removedApiFilePath.Valid() {
907 return android.Paths{paths.removedApiFilePath.Path()}, nil
908 }
Anton Hanssond78eb762021-09-21 15:25:12 +0100909
910 case annotationsComponentName:
911 if paths.annotationsZip.Valid() {
912 return android.Paths{paths.annotationsZip.Path()}, nil
913 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100914 }
915
916 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
917 } else {
918 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
919 }
920
921 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +0100922 switch tag {
923 case ".doctags":
924 if c.doctagPaths != nil {
925 return c.doctagPaths, nil
926 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100927 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +0100928 }
929 }
Paul Duffin46dc45a2020-05-14 15:39:10 +0100930 return nil, nil
931 }
932}
933
Paul Duffin803a9562020-05-20 11:52:25 +0100934func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +0000935 if c.scopePaths == nil {
936 c.scopePaths = make(map[*apiScope]*scopePaths)
937 }
938 paths := c.scopePaths[scope]
939 if paths == nil {
940 paths = &scopePaths{}
941 c.scopePaths[scope] = paths
942 }
943
944 return paths
945}
946
Paul Duffin803a9562020-05-20 11:52:25 +0100947func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
948 if c.scopePaths == nil {
949 return nil
950 }
951
952 return c.scopePaths[scope]
953}
954
955// If this does not support the requested api scope then find the closest available
956// scope it does support. Returns nil if no such scope is available.
957func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +0100958 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +0100959 if paths := c.findScopePaths(s); paths != nil {
960 return paths
961 }
962 }
963
964 // This should never happen outside tests as public should be the base scope for every
965 // scope and is enabled by default.
966 return nil
967}
968
Jiyong Parkf1691d22021-03-29 20:11:58 +0900969func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +0100970
971 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +0900972 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100973 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +0100974 }
975
Paul Duffin1267d872021-04-16 17:21:36 +0100976 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
977 if paths == nil {
978 return nil
979 }
980
981 return paths.stubsHeaderPath
982}
983
984// selectScopePaths returns the *scopePaths appropriate for the specific kind.
985//
986// If the module does not support the specific kind then it will return the *scopePaths for the
987// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
988// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
989func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +0100990 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +0100991
Paul Duffin803a9562020-05-20 11:52:25 +0100992 paths := c.findClosestScopePath(apiScope)
993 if paths == nil {
994 var scopes []string
995 for _, s := range allApiScopes {
996 if c.findScopePaths(s) != nil {
997 scopes = append(scopes, s.name)
998 }
999 }
Paul Duffin71b33cc2021-06-23 11:39:47 +01001000 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 +01001001 return nil
1002 }
1003
Paul Duffin1267d872021-04-16 17:21:36 +01001004 return paths
1005}
1006
Paul Duffin32cf58a2021-05-18 16:32:50 +01001007// sdkKindToApiScope maps from android.SdkKind to apiScope.
1008func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1009 var apiScope *apiScope
1010 switch kind {
1011 case android.SdkSystem:
1012 apiScope = apiScopeSystem
1013 case android.SdkModule:
1014 apiScope = apiScopeModuleLib
1015 case android.SdkTest:
1016 apiScope = apiScopeTest
1017 case android.SdkSystemServer:
1018 apiScope = apiScopeSystemServer
1019 default:
1020 apiScope = apiScopePublic
1021 }
1022 return apiScope
1023}
1024
Paul Duffin1267d872021-04-16 17:21:36 +01001025// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001026func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001027 paths := c.selectScopePaths(ctx, kind)
1028 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001029 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001030 }
1031
1032 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001033}
1034
Paul Duffin32cf58a2021-05-18 16:32:50 +01001035// to satisfy SdkLibraryDependency interface
1036func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1037 apiScope := sdkKindToApiScope(kind)
1038 paths := c.findScopePaths(apiScope)
1039 if paths == nil {
1040 return android.OptionalPath{}
1041 }
1042
1043 return paths.removedApiFilePath
1044}
1045
Paul Duffin859fe962020-05-15 10:20:31 +01001046func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1047 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001048 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001049 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001050 }{}
1051
Paul Duffin3f0290e2021-06-30 18:25:36 +01001052 namePtr := proptools.StringPtr(c.module.BaseModuleName())
1053 componentProps.SdkLibraryName = namePtr
1054
Paul Duffindfa131e2020-05-15 20:37:11 +01001055 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001056 // Mark the stubs library as being components of this java_sdk_library so that
1057 // any app that includes code which depends (directly or indirectly) on the stubs
1058 // library will have the appropriate <uses-library> invocation inserted into its
1059 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001060 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001061 }
1062
1063 return componentProps
1064}
1065
Paul Duffindfa131e2020-05-15 20:37:11 +01001066func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1067 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1068}
1069
Paul Duffinf4600f62021-05-13 22:34:45 +01001070// Check if the stub libraries should be compiled for dex
1071func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1072 // Always compile the dex file files for the stub libraries if they will be used on the
1073 // bootclasspath.
1074 return !c.sharedLibrary()
1075}
1076
Paul Duffin859fe962020-05-15 10:20:31 +01001077// Properties related to the use of a module as an component of a java_sdk_library.
1078type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001079 // The name of the java_sdk_library/_import module.
1080 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001081
1082 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1083 // in the AndroidManifest.xml of any Android app that includes code that references
1084 // this module. If not set then no java_sdk_library/_import is tracked.
1085 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1086}
1087
1088// Structure to be embedded in a module struct that needs to support the
1089// SdkLibraryComponentDependency interface.
1090type EmbeddableSdkLibraryComponent struct {
1091 sdkLibraryComponentProperties SdkLibraryComponentProperties
1092}
1093
Paul Duffin71b33cc2021-06-23 11:39:47 +01001094func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1095 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001096}
1097
1098// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001099func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1100 return e.sdkLibraryComponentProperties.SdkLibraryName
1101}
1102
1103// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001104func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001105 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1106 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1107 // run-time library and the corresponding module that provides the implementation. This name is
1108 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1109 // in dexpreopt).
1110 //
1111 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1112 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001113 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1114}
1115
Paul Duffin859fe962020-05-15 10:20:31 +01001116// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1117// (including the java_sdk_library) itself.
1118type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001119 UsesLibraryDependency
1120
Paul Duffin3f0290e2021-06-30 18:25:36 +01001121 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1122 SdkLibraryName() *string
1123
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001124 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1125 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001126}
1127
1128// Make sure that all the module types that are components of java_sdk_library/_import
1129// and which can be referenced (directly or indirectly) from an android app implement
1130// the SdkLibraryComponentDependency interface.
1131var _ SdkLibraryComponentDependency = (*Library)(nil)
1132var _ SdkLibraryComponentDependency = (*Import)(nil)
1133var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001134var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001135
Paul Duffin32cf58a2021-05-18 16:32:50 +01001136// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001137type SdkLibraryDependency interface {
1138 SdkLibraryComponentDependency
1139
1140 // Get the header jars appropriate for the supplied sdk_version.
1141 //
1142 // These are turbine generated jars so they only change if the externals of the
1143 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001144 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001145
1146 // Get the implementation jars appropriate for the supplied sdk version.
1147 //
1148 // These are either the implementation jar for the whole sdk library or the implementation
1149 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1150 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001151 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001152
1153 // SdkApiStubDexJar returns the dex jar for the stubs. It is needed by the hiddenapi processing
1154 // tool which processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001155 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001156
Paul Duffin32cf58a2021-05-18 16:32:50 +01001157 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1158 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1159
Paul Duffinf4600f62021-05-13 22:34:45 +01001160 // sharedLibrary returns true if this can be used as a shared library.
1161 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001162}
1163
Inseob Kimc0907f12019-02-08 21:00:45 +09001164type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001165 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001166
Zi Wangb2179e32023-01-31 15:53:30 -08001167 android.BazelModuleBase
1168
Sundong Ahn054b19a2018-10-19 13:46:09 +09001169 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001170
Paul Duffin3375e352020-04-28 10:44:03 +01001171 // Map from api scope to the scope specific property structure.
1172 scopeToProperties map[*apiScope]*ApiScopeProperties
1173
Paul Duffin56d44902020-01-31 13:36:25 +00001174 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001175}
1176
Inseob Kimc0907f12019-02-08 21:00:45 +09001177var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001178
Paul Duffin3375e352020-04-28 10:44:03 +01001179func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1180 return module.sdkLibraryProperties.Generate_system_and_test_apis
1181}
1182
1183func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1184 // Check to see if any scopes have been explicitly enabled. If any have then all
1185 // must be.
1186 anyScopesExplicitlyEnabled := false
1187 for _, scope := range allApiScopes {
1188 scopeProperties := module.scopeToProperties[scope]
1189 if scopeProperties.Enabled != nil {
1190 anyScopesExplicitlyEnabled = true
1191 break
1192 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001193 }
Paul Duffin3375e352020-04-28 10:44:03 +01001194
1195 var generatedScopes apiScopes
1196 enabledScopes := make(map[*apiScope]struct{})
1197 for _, scope := range allApiScopes {
1198 scopeProperties := module.scopeToProperties[scope]
1199 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1200 // This is to ensure that any new usages of this module type do not rely on legacy
1201 // behaviour.
1202 defaultEnabledStatus := false
1203 if anyScopesExplicitlyEnabled {
1204 defaultEnabledStatus = scope.defaultEnabledStatus
1205 } else {
1206 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1207 }
1208 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1209 if enabled {
1210 enabledScopes[scope] = struct{}{}
1211 generatedScopes = append(generatedScopes, scope)
1212 }
1213 }
1214
1215 // Now check to make sure that any scope that is extended by an enabled scope is also
1216 // enabled.
1217 for _, scope := range allApiScopes {
1218 if _, ok := enabledScopes[scope]; ok {
1219 extends := scope.extends
1220 if extends != nil {
1221 if _, ok := enabledScopes[extends]; !ok {
1222 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1223 }
1224 }
1225 }
1226 }
1227
1228 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001229}
1230
satayev758968a2021-12-06 11:42:40 +00001231var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1232
satayev8f088b02021-12-06 11:40:46 +00001233func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001234 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001235 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1236 isExternal := !module.depIsInSameApex(ctx, child)
1237 if am, ok := child.(android.ApexModule); ok {
1238 if !do(ctx, parent, am, isExternal) {
1239 return false
1240 }
1241 }
1242 return !isExternal
1243 })
1244 })
1245}
1246
Paul Duffineedc5d52020-06-12 17:46:39 +01001247type sdkLibraryComponentTag struct {
1248 blueprint.BaseDependencyTag
1249 name string
1250}
1251
1252// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1253func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1254
1255var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001256
Jiyong Parke3833882020-02-17 17:28:10 +09001257func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001258 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001259 return dt == xmlPermissionsFileTag
1260 }
1261 return false
1262}
1263
Paul Duffineedc5d52020-06-12 17:46:39 +01001264var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001265
Paul Duffin44f1d842020-06-26 20:17:02 +01001266// Add the dependencies on the child modules in the component deps mutator.
1267func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001268 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001269 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001270 stubModuleName := module.stubsLibraryModuleName(apiScope)
1271 // Use JavaApiLibraryName function to be redirected to stubs generated from .txt if applicable
1272 stubModuleName = android.JavaApiLibraryName(ctx.Config(), stubModuleName)
1273 ctx.AddVariationDependencies(nil, apiScope.stubsTag, stubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001274
Paul Duffin15f34ef2020-07-20 18:04:44 +01001275 // Add a dependency on the stubs source in order to access both stubs source and api information.
1276 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001277
1278 if module.compareAgainstLatestApi(apiScope) {
1279 // Add dependencies on the latest finalized version of the API .txt file.
1280 latestApiModuleName := module.latestApiModuleName(apiScope)
1281 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1282
1283 // Add dependencies on the latest finalized version of the remove API .txt file.
1284 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1285 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1286 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001287 }
1288
Paul Duffindfa131e2020-05-15 20:37:11 +01001289 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001290 // Add dependency to the rule for generating the implementation library.
1291 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1292
Paul Duffindfa131e2020-05-15 20:37:11 +01001293 if module.sharedLibrary() {
1294 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001295 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001296 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001297 }
1298}
Paul Duffine74ac732020-02-06 13:51:46 +00001299
Paul Duffin44f1d842020-06-26 20:17:02 +01001300// Add other dependencies as normal.
1301func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001302 var missingApiModules []string
1303 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1304 if apiScope.unstable {
1305 continue
1306 }
Paul Duffin958806b2022-05-16 13:10:47 +00001307 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001308 missingApiModules = append(missingApiModules, m)
1309 }
Paul Duffin958806b2022-05-16 13:10:47 +00001310 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001311 missingApiModules = append(missingApiModules, m)
1312 }
Paul Duffin958806b2022-05-16 13:10:47 +00001313 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001314 missingApiModules = append(missingApiModules, m)
1315 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001316 }
1317 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1318 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1319 m += "You need to do one of the following:\n"
1320 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1321 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1322 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1323 m += "\n"
1324 m += "The following filegroup modules are missing:\n "
1325 m += strings.Join(missingApiModules, "\n ") + "\n"
1326 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."
1327 ctx.ModuleErrorf(m)
1328 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001329 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001330 // Only add the deps for the library if it is actually going to be built.
1331 module.Library.deps(ctx)
1332 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001333}
1334
Paul Duffin46dc45a2020-05-14 15:39:10 +01001335func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1336 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001337 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001338 return paths, err
1339 }
Colin Cross4acaea92021-12-10 23:05:02 +00001340 if module.requiresRuntimeImplementationLibrary() {
1341 return module.Library.OutputFiles(tag)
1342 }
1343 if tag == "" {
1344 return nil, nil
1345 }
1346 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001347}
1348
Inseob Kimc0907f12019-02-08 21:00:45 +09001349func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001350 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1351 module.CheckMinSdkVersion(ctx)
1352 }
1353
Paul Duffina2ae7e02020-09-11 11:55:00 +01001354 module.generateCommonBuildActions(ctx)
1355
Paul Duffindfa131e2020-05-15 20:37:11 +01001356 // Only build an implementation library if required.
1357 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001358 module.Library.GenerateAndroidBuildActions(ctx)
1359 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001360
Paul Duffinb97b1572021-04-29 21:50:40 +01001361 // Collate the components exported by this module. All scope specific modules are exported but
1362 // the impl and xml component modules are not.
1363 exportedComponents := map[string]struct{}{}
1364
Sundong Ahn57368eb2018-07-06 11:20:23 +09001365 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001366 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001367 // the recorded paths will be returned depending on the link type of the caller.
1368 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001369 tag := ctx.OtherModuleDependencyTag(to)
1370
Paul Duffinc8782502020-04-29 20:45:27 +01001371 // Extract information from any of the scope specific dependencies.
1372 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1373 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001374 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001375
1376 // Extract information from the dependency. The exact information extracted
1377 // is determined by the nature of the dependency which is determined by the tag.
1378 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001379
1380 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001381 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001382 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001383
1384 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001385 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Paul Duffinb97b1572021-04-29 21:50:40 +01001386 ctx.SetProvider(android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001387
1388 // Provide additional information for inclusion in an sdk's generated .info file.
1389 additionalSdkInfo := map[string]interface{}{}
1390 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001391 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001392 scopes := map[string]interface{}{}
1393 additionalSdkInfo["scopes"] = scopes
1394 for scope, scopePaths := range module.scopePaths {
1395 scopeInfo := map[string]interface{}{}
1396 scopes[scope.name] = scopeInfo
1397 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1398 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1399 if p := scopePaths.latestApiPath; p.Valid() {
1400 scopeInfo["latest_api"] = p.Path().String()
1401 }
1402 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1403 scopeInfo["latest_removed_api"] = p.Path().String()
1404 }
1405 }
1406 ctx.SetProvider(android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001407}
1408
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001409func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001410 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001411 return nil
1412 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001413 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001414 if module.sharedLibrary() {
1415 entries := &entriesList[0]
1416 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1417 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001418 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001419}
1420
Anton Hansson5fd5d242020-03-27 19:43:19 +00001421// The dist path of the stub artifacts
1422func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001423 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001424}
1425
Paul Duffin12ceb462019-12-24 20:31:31 +00001426// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001427func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001428 scopeProperties := module.scopeToProperties[apiScope]
1429 if scopeProperties.Sdk_version != nil {
1430 return proptools.String(scopeProperties.Sdk_version)
1431 }
1432
Jiyong Parkf1691d22021-03-29 20:11:58 +09001433 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001434 if sdkDep.hasStandardLibs() {
1435 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001436 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001437 } else {
1438 // Otherwise, use no system module.
1439 return "none"
1440 }
1441}
1442
Paul Duffin31310252020-11-20 21:26:20 +00001443func (module *SdkLibrary) distStem() string {
1444 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1445}
1446
Colin Cross986b69a2021-06-01 13:13:40 -07001447// distGroup returns the subdirectory of the dist path of the stub artifacts.
1448func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001449 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001450}
1451
Paul Duffin958806b2022-05-16 13:10:47 +00001452func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1453 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1454}
1455
Paul Duffind1b3a922020-01-22 11:57:20 +00001456func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001457 return ":" + module.latestApiModuleName(apiScope)
1458}
1459
1460func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
1461 return latestPrebuiltApiModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001462}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001463
Paul Duffind1b3a922020-01-22 11:57:20 +00001464func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001465 return ":" + module.latestRemovedApiModuleName(apiScope)
1466}
1467
1468func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
1469 return latestPrebuiltApiModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001470}
1471
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001472func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001473 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1474}
1475
1476func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1477 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001478}
1479
Anton Hansson944e77d2020-08-19 11:40:22 +01001480func childModuleVisibility(childVisibility []string) []string {
1481 if childVisibility == nil {
1482 // No child visibility set. The child will use the visibility of the sdk_library.
1483 return nil
1484 }
1485
1486 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1487 var visibility []string
1488 visibility = append(visibility, "//visibility:override")
1489 visibility = append(visibility, childVisibility...)
1490 return visibility
1491}
1492
Paul Duffin5df79302020-05-16 15:52:12 +01001493// Creates the implementation java library
1494func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001495 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1496
Paul Duffin5df79302020-05-16 15:52:12 +01001497 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001498 Name *string
1499 Visibility []string
1500 Instrument bool
1501 Libs []string
1502 Static_libs []string
1503 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001504 }{
1505 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001506 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001507 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1508 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001509 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1510 // addition of &module.properties below.
1511 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001512 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1513 // addition of &module.properties below.
1514 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1515 // Pass the apex_available settings down so that the impl library can be statically
1516 // embedded within a library that is added to an APEX. Needed for updatable-media.
1517 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001518 }
1519
1520 properties := []interface{}{
1521 &module.properties,
1522 &module.protoProperties,
1523 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001524 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001525 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001526 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001527 &props,
1528 module.sdkComponentPropertiesForChildLibrary(),
1529 }
1530 mctx.CreateModule(LibraryFactory, properties...)
1531}
1532
Jiyong Parkc678ad32018-04-10 13:07:10 +09001533// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001534func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001535 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001536 Name *string
1537 Visibility []string
1538 Srcs []string
1539 Installable *bool
1540 Sdk_version *string
1541 System_modules *string
1542 Patch_module *string
1543 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001544 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001545 Compile_dex *bool
1546 Java_version *string
1547 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001548 Srcs []string
1549 Javacflags []string
1550 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001551 Dist struct {
1552 Targets []string
1553 Dest *string
1554 Dir *string
1555 Tag *string
1556 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001557 }{}
1558
Paul Duffinc3091c82020-05-08 14:16:20 +01001559 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Anton Hansson944e77d2020-08-19 11:40:22 +01001560 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001561 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001562 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001563 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001564 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001565 props.System_modules = module.deviceProperties.System_modules
1566 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001567 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001568 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Anton Hanssondae54cd2021-04-21 16:30:10 +01001569 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001570 // The stub-annotations library contains special versions of the annotations
1571 // with CLASS retention policy, so that they're kept.
1572 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1573 props.Libs = append(props.Libs, "stub-annotations")
1574 }
Paul Duffina18abc22020-05-16 18:54:24 +01001575 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1576 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001577 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1578 // interop with older developer tools that don't support 1.9.
1579 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001580
1581 // The imports need to be compiled to dex if the java_sdk_library requests it.
1582 compileDex := module.dexProperties.Compile_dex
1583 if module.stubLibrariesCompiledForDex() {
1584 compileDex = proptools.BoolPtr(true)
Sundong Ahndd567f92018-07-31 17:19:11 +09001585 }
Paul Duffinf4600f62021-05-13 22:34:45 +01001586 props.Compile_dex = compileDex
Jiyong Parkc678ad32018-04-10 13:07:10 +09001587
Anton Hansson5fd5d242020-03-27 19:43:19 +00001588 // Dist the class jar artifact for sdk builds.
1589 if !Bool(module.sdkLibraryProperties.No_dist) {
1590 props.Dist.Targets = []string{"sdk", "win_sdk"}
Paul Duffin31310252020-11-20 21:26:20 +00001591 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
Anton Hansson5fd5d242020-03-27 19:43:19 +00001592 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1593 props.Dist.Tag = proptools.StringPtr(".jar")
1594 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001595
Paul Duffin859fe962020-05-15 10:20:31 +01001596 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001597}
1598
Paul Duffin6d0886e2020-04-07 18:49:53 +01001599// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001600// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001601func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001602 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001603 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001604 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001605 Srcs []string
1606 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001607 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001608 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001609 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001610 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001611 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001612 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001613 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001614 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001615 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001616 Merge_annotations_dirs []string
1617 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001618 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001619 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001620 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001621 Current ApiToCheck
1622 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001623
1624 Api_lint struct {
1625 Enabled *bool
1626 New_since *string
1627 Baseline_file *string
1628 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001629 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001630 Aidl struct {
1631 Include_dirs []string
1632 Local_include_dirs []string
1633 }
Paul Duffin040e9062020-11-23 17:41:36 +00001634 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001635 }{}
1636
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001637 // The stubs source processing uses the same compile time classpath when extracting the
1638 // API from the implementation library as it does when compiling it. i.e. the same
1639 // * sdk version
1640 // * system_modules
1641 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001642
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001643 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001644 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001645 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001646 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001647 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001648 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001649 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001650 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001651 // A droiddoc module has only one Libs property and doesn't distinguish between
1652 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001653 props.Libs = module.properties.Libs
1654 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001655 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001656 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1657 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1658 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001659
Paul Duffine22c2ab2020-05-20 19:35:27 +01001660 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001661 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1662 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1663
Paul Duffin6d0886e2020-04-07 18:49:53 +01001664 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001665 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001666 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001667 }
1668 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001669 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001670 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1671 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001672 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001673 disabledWarnings := []string{
Paul Duffin235ffff2019-12-24 10:41:30 +00001674 "BroadcastBehavior",
Paul Duffin235ffff2019-12-24 10:41:30 +00001675 "DeprecationMismatch",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001676 "HiddenSuperclass",
Paul Duffin235ffff2019-12-24 10:41:30 +00001677 "HiddenTypeParameter",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001678 "MissingPermission",
1679 "SdkConstant",
Paul Duffin235ffff2019-12-24 10:41:30 +00001680 "Todo",
1681 "Typo",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001682 "UnavailableSymbol",
Paul Duffin235ffff2019-12-24 10:41:30 +00001683 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001684 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001685
Paul Duffin6877e6d2020-09-25 19:59:14 +01001686 // Output Javadoc comments for public scope.
1687 if apiScope == apiScopePublic {
1688 props.Output_javadoc_comments = proptools.BoolPtr(true)
1689 }
1690
Paul Duffin1fb487d2020-04-07 18:50:10 +01001691 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001692 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001693 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001694 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001695
Paul Duffin15f34ef2020-07-20 18:04:44 +01001696 // List of APIs identified from the provided source files are created. They are later
1697 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1698 // last-released (a.k.a numbered) list of API.
1699 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1700 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1701 apiDir := module.getApiDir()
1702 currentApiFileName = path.Join(apiDir, currentApiFileName)
1703 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001704
Paul Duffin15f34ef2020-07-20 18:04:44 +01001705 // check against the not-yet-release API
1706 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1707 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001708
Paul Duffin958806b2022-05-16 13:10:47 +00001709 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001710 // check against the latest released API
1711 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001712 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001713 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1714 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1715 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001716 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1717 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001718
Paul Duffin15f34ef2020-07-20 18:04:44 +01001719 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1720 // Enable api lint.
1721 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1722 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001723
Paul Duffin15f34ef2020-07-20 18:04:44 +01001724 // If it exists then pass a lint-baseline.txt through to droidstubs.
1725 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1726 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1727 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1728 if err != nil {
1729 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1730 }
1731 if len(paths) == 1 {
1732 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1733 } else if len(paths) != 0 {
1734 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001735 }
1736 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001737 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001738
Paul Duffin15f34ef2020-07-20 18:04:44 +01001739 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001740 // Dist the api txt and removed api txt artifacts for sdk builds.
1741 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1742 for _, p := range []struct {
1743 tag string
1744 pattern string
1745 }{
1746 {tag: ".api.txt", pattern: "%s.txt"},
1747 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1748 } {
1749 props.Dists = append(props.Dists, android.Dist{
1750 Targets: []string{"sdk", "win_sdk"},
1751 Dir: distDir,
1752 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1753 Tag: proptools.StringPtr(p.tag),
1754 })
1755 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001756 }
1757
Jihoon Kangd48abd52023-02-02 22:32:31 +00001758 mctx.CreateModule(DroidstubsFactory, &props).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001759}
1760
Paul Duffin958806b2022-05-16 13:10:47 +00001761func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
1762 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
1763}
1764
Paul Duffinea8f8082021-06-24 13:25:57 +01001765// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09001766func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1767 depTag := mctx.OtherModuleDependencyTag(dep)
1768 if depTag == xmlPermissionsFileTag {
1769 return true
1770 }
1771 return module.Library.DepIsInSameApex(mctx, dep)
1772}
1773
Paul Duffinea8f8082021-06-24 13:25:57 +01001774// Implements android.ApexModule
1775func (module *SdkLibrary) UniqueApexVariations() bool {
1776 return module.uniqueApexVariations()
1777}
1778
Jiyong Parkc678ad32018-04-10 13:07:10 +09001779// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001780func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001781 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00001782 var moduleMinApiLevelStr = moduleMinApiLevel.String()
1783 if moduleMinApiLevel == android.NoneApiLevel {
1784 moduleMinApiLevelStr = "current"
1785 }
Jiyong Parke3833882020-02-17 17:28:10 +09001786 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001787 Name *string
1788 Lib_name *string
1789 Apex_available []string
1790 On_bootclasspath_since *string
1791 On_bootclasspath_before *string
1792 Min_device_sdk *string
1793 Max_device_sdk *string
1794 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09001795 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00001796 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
1797 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1798 Apex_available: module.ApexProperties.Apex_available,
1799 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
1800 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
1801 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
1802 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
1803 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001804 }
Jiyong Parke3833882020-02-17 17:28:10 +09001805
Jiyong Parke3833882020-02-17 17:28:10 +09001806 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001807}
1808
Jiyong Parkf1691d22021-03-29 20:11:58 +09001809func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09001810 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001811 var kind android.SdkKind
1812 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09001813 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001814 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001815 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001816 // We don't have prebuilt SDK for the specific sdkVersion.
1817 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09001818 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001819 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001820 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001821
1822 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001823 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001824 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001825 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001826 if ctx.Config().AllowMissingDependencies() {
1827 return android.Paths{android.PathForSource(ctx, jar)}
1828 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001829 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001830 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001831 return nil
1832 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001833 return android.Paths{jarPath.Path()}
1834}
1835
Colin Crossaede88c2020-08-11 12:17:01 -07001836// 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 +01001837//
1838// If either this or the other module are on the platform then this will return
1839// false.
Colin Cross56a83212020-09-15 18:30:11 -07001840func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
1841 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1842 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09001843 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01001844}
1845
Jiyong Parkf1691d22021-03-29 20:11:58 +09001846func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001847 // If the client doesn't set sdk_version, but if this library prefers stubs over
1848 // the impl library, let's provide the widest API surface possible. To do so,
1849 // force override sdk_version to module_current so that the closest possible API
1850 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09001851 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09001852 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09001853 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001854
Paul Duffindaaa3322020-05-26 18:13:57 +01001855 // Only provide access to the implementation library if it is actually built.
1856 if module.requiresRuntimeImplementationLibrary() {
1857 // Check any special cases for java_sdk_library.
1858 //
1859 // Only allow access to the implementation library in the following condition:
1860 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001861 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001862 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001863 if headerJars {
1864 return module.HeaderJars()
1865 } else {
1866 return module.ImplementationJars()
1867 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001868 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001869 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001870
Paul Duffin23970f42020-05-20 14:20:02 +01001871 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001872}
1873
Sundong Ahn241cd372018-07-13 16:16:44 +09001874// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001875func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001876 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1877}
1878
1879// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001880func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001881 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001882}
1883
Colin Cross571cccf2019-02-04 11:22:08 -08001884var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1885
Jiyong Park82484c02018-04-23 21:41:26 +09001886func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001887 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001888 return &[]string{}
1889 }).(*[]string)
1890}
1891
Paul Duffin749f98f2019-12-30 17:23:46 +00001892func (module *SdkLibrary) getApiDir() string {
1893 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1894}
1895
Jiyong Parkc678ad32018-04-10 13:07:10 +09001896// For a java_sdk_library module, create internal modules for stubs, docs,
1897// runtime libs and xml file. If requested, the stubs and docs are created twice
1898// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001899func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1900 // If the module has been disabled then don't create any child modules.
1901 if !module.Enabled() {
1902 return
1903 }
1904
Paul Duffina18abc22020-05-16 18:54:24 +01001905 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001906 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001907 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001908 }
1909
Paul Duffin37e0b772019-12-30 17:20:10 +00001910 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001911 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001912 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00001913 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001914 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001915
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001916 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09001917
Paul Duffin3375e352020-04-28 10:44:03 +01001918 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001919
Paul Duffin749f98f2019-12-30 17:23:46 +00001920 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001921 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001922 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001923 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001924 p := android.ExistentPathForSource(mctx, path)
1925 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07001926 if mctx.Config().AllowMissingDependencies() {
1927 mctx.AddMissingDependencies([]string{path})
1928 } else {
1929 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1930 missingCurrentApi = true
1931 }
Inseob Kim8098faa2019-03-18 10:19:51 +09001932 }
1933 }
1934 }
1935
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001936 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09001937 script := "build/soong/scripts/gen-java-current-api-files.sh"
1938 p := android.ExistentPathForSource(mctx, script)
1939
1940 if !p.Valid() {
1941 panic(fmt.Sprintf("script file %s doesn't exist", script))
1942 }
1943
1944 mctx.ModuleErrorf("One or more current api files are missing. "+
1945 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001946 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001947 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001948 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001949 return
1950 }
1951
Paul Duffin3375e352020-04-28 10:44:03 +01001952 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001953 // Use the stubs source name for legacy reasons.
1954 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001955
Paul Duffind1b3a922020-01-22 11:57:20 +00001956 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001957 }
1958
Paul Duffindfa131e2020-05-15 20:37:11 +01001959 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001960 // Create child module to create an implementation library.
1961 //
1962 // This temporarily creates a second implementation library that can be explicitly
1963 // referenced.
1964 //
1965 // TODO(b/156618935) - update comment once only one implementation library is created.
1966 module.createImplLibrary(mctx)
1967
Paul Duffindfa131e2020-05-15 20:37:11 +01001968 // Only create an XML permissions file that declares the library as being usable
1969 // as a shared library if required.
1970 if module.sharedLibrary() {
1971 module.createXmlFile(mctx)
1972 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001973
1974 // record java_sdk_library modules so that they are exported to make
1975 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1976 javaSdkLibrariesLock.Lock()
1977 defer javaSdkLibrariesLock.Unlock()
1978 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1979 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01001980
Paul Duffin77590a82022-04-28 14:13:30 +00001981 // 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 +01001982 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00001983 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09001984}
1985
1986func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07001987 module.addHostAndDeviceProperties()
1988 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001989
Paul Duffin71b33cc2021-06-23 11:39:47 +01001990 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001991
Paul Duffina18abc22020-05-16 18:54:24 +01001992 module.properties.Installable = proptools.BoolPtr(true)
1993 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001994}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001995
Paul Duffindfa131e2020-05-15 20:37:11 +01001996func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1997 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1998}
1999
Jiyong Park932cdfe2020-05-28 00:19:53 +09002000func (module *SdkLibrary) defaultsToStubs() bool {
2001 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2002}
2003
Paul Duffin1b1e8062020-05-08 13:44:43 +01002004// Defines how to name the individual component modules the sdk library creates.
2005type sdkLibraryComponentNamingScheme interface {
2006 stubsLibraryModuleName(scope *apiScope, baseName string) string
2007
2008 stubsSourceModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002009}
2010
2011type defaultNamingScheme struct {
2012}
2013
2014func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2015 return scope.stubsLibraryModuleName(baseName)
2016}
2017
2018func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2019 return scope.stubsSourceModuleName(baseName)
2020}
2021
Paul Duffin1b1e8062020-05-08 13:44:43 +01002022var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2023
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002024func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002025 // This suffix-based approach is fragile and could potentially mis-trigger.
2026 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01002027 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
2028 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2029 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2030 return false, javaPlatform
2031 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002032 return true, javaSdk
2033 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002034 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002035 return true, javaSystem
2036 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002037 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002038 return true, javaModule
2039 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002040 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002041 return true, javaSystem
2042 }
2043 return false, javaPlatform
2044}
2045
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002046// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2047// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2048// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2049// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2050// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002051func SdkLibraryFactory() android.Module {
2052 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002053
2054 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002055 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002056
Inseob Kimc0907f12019-02-08 21:00:45 +09002057 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002058 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002059 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002060
2061 // Initialize the map from scope to scope specific properties.
2062 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2063 for _, scope := range allApiScopes {
2064 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2065 }
2066 module.scopeToProperties = scopeToProperties
2067
Paul Duffin4911a892020-04-29 23:35:13 +01002068 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002069 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002070 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2071 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2072
Paul Duffin1b1e8062020-05-08 13:44:43 +01002073 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002074 // If no implementation is required then it cannot be used as a shared library
2075 // either.
2076 if !module.requiresRuntimeImplementationLibrary() {
2077 // If shared_library has been explicitly set to true then it is incompatible
2078 // with api_only: true.
2079 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2080 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2081 }
2082 // Set shared_library: false.
2083 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2084 }
2085
Paul Duffin1b1e8062020-05-08 13:44:43 +01002086 if module.initCommonAfterDefaultsApplied(ctx) {
2087 module.CreateInternalModules(ctx)
2088 }
2089 })
Zi Wangb2179e32023-01-31 15:53:30 -08002090 android.InitBazelModule(module)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002091 return module
2092}
Colin Cross79c7c262019-04-17 11:11:46 -07002093
Zi Wangb2179e32023-01-31 15:53:30 -08002094type bazelSdkLibraryAttributes struct {
2095 Public bazel.StringAttribute
2096 System bazel.StringAttribute
2097 Test bazel.StringAttribute
2098 Module_lib bazel.StringAttribute
2099 System_server bazel.StringAttribute
2100}
2101
2102// java_sdk_library bp2build converter
2103func (module *SdkLibrary) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
2104 if ctx.ModuleType() != "java_sdk_library" {
2105 return
2106 }
2107
2108 nameToAttr := make(map[string]bazel.StringAttribute)
2109
2110 for _, scope := range module.getGeneratedApiScopes(ctx) {
2111 apiSurfaceFile := path.Join(module.getApiDir(), scope.apiFilePrefix+"current.txt")
2112 var scopeStringAttribute bazel.StringAttribute
2113 scopeStringAttribute.SetValue(apiSurfaceFile)
2114 nameToAttr[scope.name] = scopeStringAttribute
2115 }
2116
2117 attrs := bazelSdkLibraryAttributes{
2118 Public: nameToAttr["public"],
2119 System: nameToAttr["system"],
2120 Test: nameToAttr["test"],
2121 Module_lib: nameToAttr["module-lib"],
2122 System_server: nameToAttr["system-server"],
2123 }
2124 props := bazel.BazelTargetModuleProperties{
2125 Rule_class: "java_sdk_library",
2126 Bzl_load_location: "//build/bazel/rules/java:sdk_library.bzl",
2127 }
2128
2129 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs)
2130}
2131
Colin Cross79c7c262019-04-17 11:11:46 -07002132//
2133// SDK library prebuilts
2134//
2135
Paul Duffin56d44902020-01-31 13:36:25 +00002136// Properties associated with each api scope.
2137type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002138 Jars []string `android:"path"`
2139
2140 Sdk_version *string
2141
Colin Cross79c7c262019-04-17 11:11:46 -07002142 // List of shared java libs that this module has dependencies to
2143 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002144
Paul Duffinc8782502020-04-29 20:45:27 +01002145 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002146 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002147
2148 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002149 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002150
2151 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002152 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002153
2154 // Annotation zip
2155 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002156}
2157
Paul Duffin56d44902020-01-31 13:36:25 +00002158type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002159 // List of shared java libs, common to all scopes, that this module has
2160 // dependencies to
2161 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002162
2163 // If set to true, compile dex files for the stubs. Defaults to false.
2164 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002165
2166 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002167 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00002168}
2169
Paul Duffineedc5d52020-06-12 17:46:39 +01002170type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002171 android.ModuleBase
2172 android.DefaultableModuleBase
2173 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002174 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002175
Paul Duffin37856732021-02-26 14:24:15 +00002176 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002177 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002178
Colin Cross79c7c262019-04-17 11:11:46 -07002179 properties sdkLibraryImportProperties
2180
Paul Duffin46a26a82020-04-07 19:27:04 +01002181 // Map from api scope to the scope specific property structure.
2182 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2183
Paul Duffin56d44902020-01-31 13:36:25 +00002184 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002185
2186 // The reference to the implementation library created by the source module.
2187 // Is nil if the source module does not exist.
2188 implLibraryModule *Library
2189
2190 // The reference to the xml permissions module created by the source module.
2191 // Is nil if the source module does not exist.
2192 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002193
Jeongik Chad5fe8782021-07-08 01:13:11 +09002194 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002195 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09002196
2197 // Expected install file path of the source module(sdk_library)
2198 // or dex implementation jar obtained from the prebuilt_apex, if any.
2199 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002200}
2201
Paul Duffineedc5d52020-06-12 17:46:39 +01002202var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002203
Paul Duffin46a26a82020-04-07 19:27:04 +01002204// The type of a structure that contains a field of type sdkLibraryScopeProperties
2205// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002206//
2207// struct {
2208// Public sdkLibraryScopeProperties
2209// System sdkLibraryScopeProperties
2210// ...
2211// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002212var allScopeStructType = createAllScopePropertiesStructType()
2213
2214// Dynamically create a structure type for each apiscope in allApiScopes.
2215func createAllScopePropertiesStructType() reflect.Type {
2216 var fields []reflect.StructField
2217 for _, apiScope := range allApiScopes {
2218 field := reflect.StructField{
2219 Name: apiScope.fieldName,
2220 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2221 }
2222 fields = append(fields, field)
2223 }
2224
2225 return reflect.StructOf(fields)
2226}
2227
2228// Create an instance of the scope specific structure type and return a map
2229// from apiscope to a pointer to each scope specific field.
2230func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2231 allScopePropertiesPtr := reflect.New(allScopeStructType)
2232 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2233 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2234
2235 for _, apiScope := range allApiScopes {
2236 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2237 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2238 }
2239
2240 return allScopePropertiesPtr.Interface(), scopeProperties
2241}
2242
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002243// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002244func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002245 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002246
Paul Duffin46a26a82020-04-07 19:27:04 +01002247 allScopeProperties, scopeToProperties := createPropertiesInstance()
2248 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002249 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002250
Paul Duffinc3091c82020-05-08 14:16:20 +01002251 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002252 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002253
Paul Duffin0bdcb272020-02-06 15:24:57 +00002254 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002255 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002256 InitJavaModule(module, android.HostAndDeviceSupported)
2257
Paul Duffin1b1e8062020-05-08 13:44:43 +01002258 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2259 if module.initCommonAfterDefaultsApplied(mctx) {
2260 module.createInternalModules(mctx)
2261 }
2262 })
Colin Cross79c7c262019-04-17 11:11:46 -07002263 return module
2264}
2265
Paul Duffin630b11e2021-07-15 13:35:26 +01002266var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2267
2268func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2269 return module.properties.Permitted_packages
2270}
2271
Paul Duffineedc5d52020-06-12 17:46:39 +01002272func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002273 return &module.prebuilt
2274}
2275
Paul Duffineedc5d52020-06-12 17:46:39 +01002276func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002277 return module.prebuilt.Name(module.ModuleBase.Name())
2278}
2279
Paul Duffineedc5d52020-06-12 17:46:39 +01002280func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002281
Paul Duffin50061512020-01-21 16:31:05 +00002282 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002283 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002284 module.prebuilt.ForcePrefer()
2285 }
2286
Paul Duffin46a26a82020-04-07 19:27:04 +01002287 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002288 if len(scopeProperties.Jars) == 0 {
2289 continue
2290 }
2291
Paul Duffinbbb546b2020-04-09 00:07:11 +01002292 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002293
Paul Duffin0f8faff2020-05-20 16:18:00 +01002294 if len(scopeProperties.Stub_srcs) > 0 {
2295 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2296 }
Paul Duffin56d44902020-01-31 13:36:25 +00002297 }
Colin Cross79c7c262019-04-17 11:11:46 -07002298
2299 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2300 javaSdkLibrariesLock.Lock()
2301 defer javaSdkLibrariesLock.Unlock()
2302 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2303}
2304
Paul Duffineedc5d52020-06-12 17:46:39 +01002305func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002306 // Creates a java import for the jar with ".stubs" suffix
2307 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002308 Name *string
2309 Sdk_version *string
2310 Libs []string
2311 Jars []string
Paul Duffin1267d872021-04-16 17:21:36 +01002312 Compile_dex *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002313
2314 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002315 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002316 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002317 props.Sdk_version = scopeProperties.Sdk_version
2318 // Prepend any of the libs from the legacy public properties to the libs for each of the
2319 // scopes to avoid having to duplicate them in each scope.
2320 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2321 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002322
Paul Duffin38b57852020-05-13 16:08:09 +01002323 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002324 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002325
Paul Duffin1267d872021-04-16 17:21:36 +01002326 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002327 compileDex := module.properties.Compile_dex
2328 if module.stubLibrariesCompiledForDex() {
2329 compileDex = proptools.BoolPtr(true)
2330 }
2331 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002332
Paul Duffin859fe962020-05-15 10:20:31 +01002333 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002334}
2335
Paul Duffineedc5d52020-06-12 17:46:39 +01002336func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002337 props := struct {
Paul Duffinbf4de042022-09-27 12:41:52 +01002338 Name *string
2339 Srcs []string
2340
2341 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002342 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002343 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002344 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002345
2346 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002347 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2348
2349 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002350}
2351
Paul Duffin44f1d842020-06-26 20:17:02 +01002352// Add the dependencies on the child module in the component deps mutator so that it
2353// creates references to the prebuilt and not the source modules.
2354func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002355 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002356 if len(scopeProperties.Jars) == 0 {
2357 continue
2358 }
2359
2360 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002361 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002362
2363 if len(scopeProperties.Stub_srcs) > 0 {
2364 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002365 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002366 }
Paul Duffin56d44902020-01-31 13:36:25 +00002367 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002368}
2369
2370// Add other dependencies as normal.
2371func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002372
2373 implName := module.implLibraryModuleName()
2374 if ctx.OtherModuleExists(implName) {
2375 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2376
2377 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2378 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2379 // Add dependency to the rule for generating the xml permissions file
2380 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2381 }
2382 }
Colin Cross79c7c262019-04-17 11:11:46 -07002383}
2384
Jiakai Zhang204356f2021-09-09 08:12:46 +00002385func (module *SdkLibraryImport) AndroidMkEntries() []android.AndroidMkEntries {
2386 // For an SDK library imported from a prebuilt APEX, we don't need a Make module for itself, as we
2387 // don't need to install it. However, we need to add its dexpreopt outputs as sub-modules, if it
2388 // is preopted.
2389 dexpreoptEntries := module.dexpreopter.AndroidMkEntriesForApex()
2390 return append(dexpreoptEntries, android.AndroidMkEntries{Disabled: true})
2391}
2392
Jiyong Park45bf82e2020-12-15 22:29:02 +09002393var _ android.ApexModule = (*SdkLibraryImport)(nil)
2394
2395// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002396func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2397 depTag := mctx.OtherModuleDependencyTag(dep)
2398 if depTag == xmlPermissionsFileTag {
2399 return true
2400 }
2401
2402 // None of the other dependencies of the java_sdk_library_import are in the same apex
2403 // as the one that references this module.
2404 return false
2405}
2406
Jiyong Park45bf82e2020-12-15 22:29:02 +09002407// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002408func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2409 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002410 // we don't check prebuilt modules for sdk_version
2411 return nil
2412}
2413
Paul Duffinea8f8082021-06-24 13:25:57 +01002414// Implements android.ApexModule
2415func (module *SdkLibraryImport) UniqueApexVariations() bool {
2416 return module.uniqueApexVariations()
2417}
2418
Paul Duffin09817d62022-04-28 17:45:11 +01002419// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002420func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2421 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002422}
2423
2424var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2425
Paul Duffineedc5d52020-06-12 17:46:39 +01002426func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002427 paths, err := module.commonOutputFiles(tag)
2428 if paths != nil || err != nil {
2429 return paths, err
2430 }
2431 if module.implLibraryModule != nil {
2432 return module.implLibraryModule.OutputFiles(tag)
2433 } else {
2434 return nil, nil
2435 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002436}
2437
Paul Duffineedc5d52020-06-12 17:46:39 +01002438func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002439 module.generateCommonBuildActions(ctx)
2440
Jeongik Chad5fe8782021-07-08 01:13:11 +09002441 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2442 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2443
Paul Duffin0f8faff2020-05-20 16:18:00 +01002444 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002445 ctx.VisitDirectDeps(func(to android.Module) {
2446 tag := ctx.OtherModuleDependencyTag(to)
2447
Paul Duffin0f8faff2020-05-20 16:18:00 +01002448 // Extract information from any of the scope specific dependencies.
2449 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2450 apiScope := scopeTag.apiScope
2451 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2452
2453 // Extract information from the dependency. The exact information extracted
2454 // is determined by the nature of the dependency which is determined by the tag.
2455 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002456 } else if tag == implLibraryTag {
2457 if implLibrary, ok := to.(*Library); ok {
2458 module.implLibraryModule = implLibrary
2459 } else {
2460 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2461 }
2462 } else if tag == xmlPermissionsFileTag {
2463 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2464 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2465 } else {
2466 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2467 }
Colin Cross79c7c262019-04-17 11:11:46 -07002468 }
2469 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002470
2471 // Populate the scope paths with information from the properties.
2472 for apiScope, scopeProperties := range module.scopeProperties {
2473 if len(scopeProperties.Jars) == 0 {
2474 continue
2475 }
2476
2477 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002478 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002479 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2480 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2481 }
Paul Duffin39853512021-02-26 11:09:39 +00002482
2483 if ctx.Device() {
2484 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2485 // obtained from the associated deapexer module.
2486 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2487 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002488 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01002489 di := android.FindDeapexerProviderForModule(ctx)
2490 if di == nil {
2491 return // An error has been reported by FindDeapexerProviderForModule.
2492 }
Jiakai Zhang81e46812023-02-08 21:56:07 +08002493 dexJarFileApexRootRelative := apexRootRelativePathToJavaLib(module.BaseModuleName())
2494 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002495 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2496 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002497 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002498 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002499 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002500 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002501
Jiakai Zhang204356f2021-09-09 08:12:46 +00002502 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2503 module.dexpreopter.isSDKLibrary = true
2504 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002505
2506 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2507 module.dexpreopter.inputProfilePathOnHost = profilePath
2508 }
2509
2510 // Dexpreopting.
Jiakai Zhang204356f2021-09-09 08:12:46 +00002511 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002512 } else {
2513 // This should never happen as a variant for a prebuilt_apex is only created if the
2514 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002515 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002516 }
2517 }
2518 }
Colin Cross79c7c262019-04-17 11:11:46 -07002519}
2520
Jiyong Parkf1691d22021-03-29 20:11:58 +09002521func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002522
2523 // For consistency with SdkLibrary make the implementation jar available to libraries that
2524 // are within the same APEX.
2525 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002526 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002527 if headerJars {
2528 return implLibraryModule.HeaderJars()
2529 } else {
2530 return implLibraryModule.ImplementationJars()
2531 }
2532 }
2533
Paul Duffin23970f42020-05-20 14:20:02 +01002534 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002535}
2536
Colin Cross79c7c262019-04-17 11:11:46 -07002537// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002538func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002539 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002540 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002541}
2542
2543// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002544func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002545 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002546 return module.sdkJars(ctx, sdkVersion, false)
2547}
2548
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002549// to satisfy UsesLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002550func (module *SdkLibraryImport) DexJarBuildPath() OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002551 // The dex implementation jar extracted from the .apex file should be used in preference to the
2552 // source.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002553 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002554 return module.dexJarFile
2555 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002556 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002557 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002558 } else {
2559 return module.implLibraryModule.DexJarBuildPath()
2560 }
2561}
2562
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002563// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002564func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002565 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002566}
2567
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002568// to satisfy UsesLibraryDependency interface
2569func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2570 return nil
2571}
2572
Paul Duffineedc5d52020-06-12 17:46:39 +01002573// to satisfy apex.javaDependency interface
2574func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2575 if module.implLibraryModule == nil {
2576 return nil
2577 } else {
2578 return module.implLibraryModule.JacocoReportClassesFile()
2579 }
2580}
2581
2582// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002583func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2584 if module.implLibraryModule == nil {
2585 return LintDepSets{}
2586 } else {
2587 return module.implLibraryModule.LintDepSets()
2588 }
2589}
2590
Spandan Das17854f52022-01-14 21:19:14 +00002591func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002592 if module.implLibraryModule == nil {
2593 return false
2594 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002595 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002596 }
2597}
2598
Spandan Das17854f52022-01-14 21:19:14 +00002599func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002600 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002601 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002602 }
2603}
2604
Colin Cross08dca382020-07-21 20:31:17 -07002605// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002606func (module *SdkLibraryImport) Stem() string {
2607 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002608}
Jiyong Parke3833882020-02-17 17:28:10 +09002609
Paul Duffin44b481b2020-06-17 16:59:43 +01002610var _ ApexDependency = (*SdkLibraryImport)(nil)
2611
2612// to satisfy java.ApexDependency interface
2613func (module *SdkLibraryImport) HeaderJars() android.Paths {
2614 if module.implLibraryModule == nil {
2615 return nil
2616 } else {
2617 return module.implLibraryModule.HeaderJars()
2618 }
2619}
2620
2621// to satisfy java.ApexDependency interface
2622func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2623 if module.implLibraryModule == nil {
2624 return nil
2625 } else {
2626 return module.implLibraryModule.ImplementationAndResourcesJars()
2627 }
2628}
2629
Jiakai Zhang204356f2021-09-09 08:12:46 +00002630// to satisfy java.DexpreopterInterface interface
2631func (module *SdkLibraryImport) IsInstallable() bool {
2632 return true
2633}
2634
Paul Duffinfef55002021-06-17 14:56:05 +01002635var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2636
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002637func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002638 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08002639 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01002640}
2641
Jiyong Parke3833882020-02-17 17:28:10 +09002642// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09002643type sdkLibraryXml struct {
2644 android.ModuleBase
2645 android.DefaultableModuleBase
2646 android.ApexModuleBase
2647
2648 properties sdkLibraryXmlProperties
2649
2650 outputFilePath android.OutputPath
2651 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002652
2653 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002654}
2655
2656type sdkLibraryXmlProperties struct {
2657 // canonical name of the lib
2658 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002659
2660 // Signals that this shared library is part of the bootclasspath starting
2661 // on the version indicated in this attribute.
2662 //
2663 // This will make platforms at this level and above to ignore
2664 // <uses-library> tags with this library name because the library is already
2665 // available
2666 On_bootclasspath_since *string
2667
2668 // Signals that this shared library was part of the bootclasspath before
2669 // (but not including) the version indicated in this attribute.
2670 //
2671 // The system will automatically add a <uses-library> tag with this library to
2672 // apps that target any SDK less than the version indicated in this attribute.
2673 On_bootclasspath_before *string
2674
2675 // Indicates that PackageManager should ignore this shared library if the
2676 // platform is below the version indicated in this attribute.
2677 //
2678 // This means that the device won't recognise this library as installed.
2679 Min_device_sdk *string
2680
2681 // Indicates that PackageManager should ignore this shared library if the
2682 // platform is above the version indicated in this attribute.
2683 //
2684 // This means that the device won't recognise this library as installed.
2685 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00002686
2687 // The SdkLibrary's min api level as a string
2688 //
2689 // This value comes from the ApiLevel of the MinSdkVersion property.
2690 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09002691}
2692
2693// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2694// Not to be used directly by users. java_sdk_library internally uses this.
2695func sdkLibraryXmlFactory() android.Module {
2696 module := &sdkLibraryXml{}
2697
2698 module.AddProperties(&module.properties)
2699
2700 android.InitApexModule(module)
2701 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2702
2703 return module
2704}
2705
Colin Crossaede88c2020-08-11 12:17:01 -07002706func (module *sdkLibraryXml) UniqueApexVariations() bool {
2707 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2708 // mounted APEX, which contains the name of the APEX.
2709 return true
2710}
2711
Jiyong Parke3833882020-02-17 17:28:10 +09002712// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002713func (module *sdkLibraryXml) BaseDir() string {
2714 return "etc"
2715}
2716
2717// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002718func (module *sdkLibraryXml) SubDir() string {
2719 return "permissions"
2720}
2721
2722// from android.PrebuiltEtcModule
2723func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2724 return module.outputFilePath
2725}
2726
2727// from android.ApexModule
2728func (module *sdkLibraryXml) AvailableFor(what string) bool {
2729 return true
2730}
2731
2732func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2733 // do nothing
2734}
2735
Jiyong Park45bf82e2020-12-15 22:29:02 +09002736var _ android.ApexModule = (*sdkLibraryXml)(nil)
2737
2738// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002739func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2740 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002741 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2742 return nil
2743}
2744
Jiyong Parke3833882020-02-17 17:28:10 +09002745// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002746func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002747 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002748 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002749 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002750 // In most cases, this works fine. But when apex_name is set or override_apex is used
2751 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002752 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002753 }
2754 partition := "system"
2755 if module.SocSpecific() {
2756 partition = "vendor"
2757 } else if module.DeviceSpecific() {
2758 partition = "odm"
2759 } else if module.ProductSpecific() {
2760 partition = "product"
2761 } else if module.SystemExtSpecific() {
2762 partition = "system_ext"
2763 }
2764 return "/" + partition + "/framework/" + implName + ".jar"
2765}
2766
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002767func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
2768 if value == nil {
2769 return ""
2770 }
2771 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
2772 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002773 // attributes in bp files have underscores but in the xml have dashes.
2774 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002775 return ""
2776 }
Pedro Loureirob638c622021-12-22 15:28:05 +00002777 if apiLevel.IsCurrent() {
2778 // passing "current" would always mean a future release, never the current (or the current in
2779 // progress) which means some conditions would never be triggered.
2780 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
2781 `"current" is not an allowed value for this attribute`)
2782 return ""
2783 }
Pedro Loureiro48991222022-06-17 20:01:21 +00002784 // "safeValue" is safe because it translates finalized codenames to a string
2785 // with their SDK int.
2786 safeValue := apiLevel.String()
2787 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002788}
2789
2790// formats an attribute for the xml permissions file if the value is not null
2791// returns empty string otherwise
2792func formattedOptionalAttribute(attrName string, value *string) string {
2793 if value == nil {
2794 return ""
2795 }
2796 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
2797}
2798
2799func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
2800 libName := proptools.String(module.properties.Lib_name)
2801 libNameAttr := formattedOptionalAttribute("name", &libName)
2802 filePath := module.implPath(ctx)
2803 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002804 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
2805 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
2806 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
2807 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002808 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
2809 // 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 +00002810 var libraryTag string
2811 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002812 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00002813 } else {
2814 libraryTag = ` <library\n`
2815 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002816
2817 return strings.Join([]string{
2818 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
2819 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
2820 `\n`,
2821 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
2822 ` you may not use this file except in compliance with the License.\n`,
2823 ` You may obtain a copy of the License at\n`,
2824 `\n`,
2825 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
2826 `\n`,
2827 ` Unless required by applicable law or agreed to in writing, software\n`,
2828 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
2829 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
2830 ` See the License for the specific language governing permissions and\n`,
2831 ` limitations under the License.\n`,
2832 `-->\n`,
2833 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00002834 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002835 libNameAttr,
2836 filePathAttr,
2837 implicitFromAttr,
2838 implicitUntilAttr,
2839 minSdkAttr,
2840 maxSdkAttr,
2841 ` />\n`,
2842 `</permissions>\n`}, "")
2843}
2844
Jiyong Parke3833882020-02-17 17:28:10 +09002845func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07002846 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
2847
Jiyong Parke3833882020-02-17 17:28:10 +09002848 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002849 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002850 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002851
2852 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08002853 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002854 rule.Command().
2855 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2856 Output(module.outputFilePath)
2857
Colin Crossf1a035e2020-11-16 17:32:30 -08002858 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09002859
2860 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2861}
2862
2863func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07002864 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00002865 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09002866 Disabled: true,
2867 }}
2868 }
2869
satayev8f088b02021-12-06 11:40:46 +00002870 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09002871 Class: "ETC",
2872 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2873 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07002874 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09002875 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08002876 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09002877 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2878 },
2879 },
2880 }}
2881}
Paul Duffindd46f712020-02-10 13:37:10 +00002882
Pedro Loureiroc3621422021-09-28 15:40:23 +00002883func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
2884 module.validateAtLeastTAttributes(ctx)
2885 module.validateMinAndMaxDeviceSdk(ctx)
2886 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
2887 module.validateOnBootclasspathBeforeRequirements(ctx)
2888}
2889
2890func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
2891 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
2892 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
2893 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
2894 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
2895 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
2896}
2897
2898func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
2899 if attr != nil {
2900 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
2901 // we will inform the user of invalid inputs when we try to write the
2902 // permissions xml file so we don't need to do it here
2903 if t.GreaterThan(level) {
2904 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
2905 }
2906 }
2907 }
2908}
2909
2910func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
2911 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
2912 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
2913 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
2914 if minErr == nil && maxErr == nil {
2915 // we will inform the user of invalid inputs when we try to write the
2916 // permissions xml file so we don't need to do it here
2917 if min.GreaterThan(max) {
2918 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
2919 }
2920 }
2921 }
2922}
2923
2924func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
2925 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
2926 if module.properties.Min_device_sdk != nil {
2927 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
2928 if err == nil {
2929 if moduleMinApi.GreaterThan(api) {
2930 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
2931 }
2932 }
2933 }
2934 if module.properties.Max_device_sdk != nil {
2935 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
2936 if err == nil {
2937 if moduleMinApi.GreaterThan(api) {
2938 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
2939 }
2940 }
2941 }
2942}
2943
2944func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
2945 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
2946 if module.properties.On_bootclasspath_before != nil {
2947 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
2948 // if we use the attribute, then we need to do this validation
2949 if moduleMinApi.LessThan(t) {
2950 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
2951 if module.properties.Min_device_sdk == nil {
2952 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")
2953 }
2954 }
2955 }
2956}
2957
Paul Duffindd46f712020-02-10 13:37:10 +00002958type sdkLibrarySdkMemberType struct {
2959 android.SdkMemberTypeBase
2960}
2961
Paul Duffin296701e2021-07-14 10:29:36 +01002962func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
2963 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00002964}
2965
2966func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2967 _, ok := module.(*SdkLibrary)
2968 return ok
2969}
2970
2971func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2972 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2973}
2974
2975func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2976 return &sdkLibrarySdkMemberProperties{}
2977}
2978
Paul Duffin976b0e52021-04-27 23:20:26 +01002979var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
2980 android.SdkMemberTypeBase{
2981 PropertyName: "java_sdk_libs",
2982 SupportsSdk: true,
2983 },
2984}
2985
Paul Duffindd46f712020-02-10 13:37:10 +00002986type sdkLibrarySdkMemberProperties struct {
2987 android.SdkMemberPropertiesBase
2988
Paul Duffine8409952022-09-22 16:24:46 +01002989 // Stem name for files in the sdk snapshot.
2990 //
2991 // This is used to construct the path names of various sdk library files in the sdk snapshot to
2992 // make sure that they match the finalized versions of those files in prebuilts/sdk.
2993 //
2994 // This property is marked as keep so that it will be kept in all instances of this struct, will
2995 // not be cleared but will be copied to common structs. That is needed because this field is used
2996 // to construct many file names for other parts of this struct and so it needs to be present in
2997 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
2998 // be unavailable for generating file names if there were other properties that were still set.
2999 Stem string `sdk:"keep"`
3000
Paul Duffindd46f712020-02-10 13:37:10 +00003001 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003002 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003003
Paul Duffin3d1248c2020-04-09 00:10:17 +01003004 // The Java stubs source files.
3005 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003006
3007 // The naming scheme.
3008 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003009
3010 // True if the java_sdk_library_import is for a shared library, false
3011 // otherwise.
3012 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003013
Paul Duffin1267d872021-04-16 17:21:36 +01003014 // True if the stub imports should produce dex jars.
3015 Compile_dex *bool
3016
Paul Duffina2ae7e02020-09-11 11:55:00 +01003017 // The paths to the doctag files to add to the prebuilt.
3018 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003019
3020 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003021
3022 // Signals that this shared library is part of the bootclasspath starting
3023 // on the version indicated in this attribute.
3024 //
3025 // This will make platforms at this level and above to ignore
3026 // <uses-library> tags with this library name because the library is already
3027 // available
3028 On_bootclasspath_since *string
3029
3030 // Signals that this shared library was part of the bootclasspath before
3031 // (but not including) the version indicated in this attribute.
3032 //
3033 // The system will automatically add a <uses-library> tag with this library to
3034 // apps that target any SDK less than the version indicated in this attribute.
3035 On_bootclasspath_before *string
3036
3037 // Indicates that PackageManager should ignore this shared library if the
3038 // platform is below the version indicated in this attribute.
3039 //
3040 // This means that the device won't recognise this library as installed.
3041 Min_device_sdk *string
3042
3043 // Indicates that PackageManager should ignore this shared library if the
3044 // platform is above the version indicated in this attribute.
3045 //
3046 // This means that the device won't recognise this library as installed.
3047 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003048
3049 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003050}
3051
3052type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003053 Jars android.Paths
3054 StubsSrcJar android.Path
3055 CurrentApiFile android.Path
3056 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003057 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003058 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003059}
3060
3061func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3062 sdk := variant.(*SdkLibrary)
3063
Paul Duffine8409952022-09-22 16:24:46 +01003064 // Copy the stem name for files in the sdk snapshot.
3065 s.Stem = sdk.distStem()
3066
Paul Duffin106a3a42022-01-27 16:39:06 +00003067 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003068 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003069 paths := sdk.findScopePaths(apiScope)
3070 if paths == nil {
3071 continue
3072 }
3073
Paul Duffindd46f712020-02-10 13:37:10 +00003074 jars := paths.stubsImplPath
3075 if len(jars) > 0 {
3076 properties := scopeProperties{}
3077 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003078 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003079 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003080 if paths.currentApiFilePath.Valid() {
3081 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3082 }
3083 if paths.removedApiFilePath.Valid() {
3084 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3085 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003086 // The annotations zip is only available for modules that set annotations_enabled: true.
3087 if paths.annotationsZip.Valid() {
3088 properties.AnnotationsZip = paths.annotationsZip.Path()
3089 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003090 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003091 }
3092 }
3093
Paul Duffindfa131e2020-05-15 20:37:11 +01003094 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003095 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003096 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003097 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003098 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003099 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3100 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3101 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3102 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003103
3104 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3105 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3106 }
Paul Duffindd46f712020-02-10 13:37:10 +00003107}
3108
3109func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003110 if s.Naming_scheme != nil {
3111 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3112 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003113 if s.Shared_library != nil {
3114 propertySet.AddProperty("shared_library", *s.Shared_library)
3115 }
Paul Duffin1267d872021-04-16 17:21:36 +01003116 if s.Compile_dex != nil {
3117 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3118 }
Paul Duffin869de142021-07-15 14:14:41 +01003119 if len(s.Permitted_packages) > 0 {
3120 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3121 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003122 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3123 if s.DexPreoptProfileGuided != nil {
3124 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3125 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003126
Paul Duffine8409952022-09-22 16:24:46 +01003127 stem := s.Stem
3128
Paul Duffindd46f712020-02-10 13:37:10 +00003129 for _, apiScope := range allApiScopes {
3130 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003131 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003132
Paul Duffin958806b2022-05-16 13:10:47 +00003133 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003134
Paul Duffindd46f712020-02-10 13:37:10 +00003135 var jars []string
3136 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003137 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003138 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3139 jars = append(jars, dest)
3140 }
3141 scopeSet.AddProperty("jars", jars)
3142
Paul Duffin22628d52021-05-12 23:13:22 +01003143 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3144 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003145 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003146 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3147 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3148 } else {
3149 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3150 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003151 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003152 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3153 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3154 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003155
Paul Duffin1fd005d2020-04-09 01:08:11 +01003156 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003157 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003158 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3159 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3160 }
3161
3162 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003163 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003164 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003165 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3166 }
3167
Anton Hanssond78eb762021-09-21 15:25:12 +01003168 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003169 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003170 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3171 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3172 }
3173
Paul Duffindd46f712020-02-10 13:37:10 +00003174 if properties.SdkVersion != "" {
3175 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3176 }
3177 }
3178 }
3179
Paul Duffina2ae7e02020-09-11 11:55:00 +01003180 if len(s.Doctag_paths) > 0 {
3181 dests := []string{}
3182 for _, p := range s.Doctag_paths {
3183 dest := filepath.Join("doctags", p.Rel())
3184 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3185 dests = append(dests, dest)
3186 }
3187 propertySet.AddProperty("doctag_files", dests)
3188 }
Paul Duffindd46f712020-02-10 13:37:10 +00003189}