blob: d2fbfd953a60c428bce552a9babd5ee1e72c5e9e [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) {
1234 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx).ApiLevel, func(c android.ModuleContext, do android.PayloadDepsCallback) {
1235 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
Paul Duffinc3091c82020-05-08 14:16:20 +01001270 ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
Paul Duffind1b3a922020-01-22 11:57:20 +00001271
Paul Duffin15f34ef2020-07-20 18:04:44 +01001272 // Add a dependency on the stubs source in order to access both stubs source and api information.
1273 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001274
1275 if module.compareAgainstLatestApi(apiScope) {
1276 // Add dependencies on the latest finalized version of the API .txt file.
1277 latestApiModuleName := module.latestApiModuleName(apiScope)
1278 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1279
1280 // Add dependencies on the latest finalized version of the remove API .txt file.
1281 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1282 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1283 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001284 }
1285
Paul Duffindfa131e2020-05-15 20:37:11 +01001286 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001287 // Add dependency to the rule for generating the implementation library.
1288 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1289
Paul Duffindfa131e2020-05-15 20:37:11 +01001290 if module.sharedLibrary() {
1291 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001292 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001293 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001294 }
1295}
Paul Duffine74ac732020-02-06 13:51:46 +00001296
Paul Duffin44f1d842020-06-26 20:17:02 +01001297// Add other dependencies as normal.
1298func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001299 var missingApiModules []string
1300 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1301 if apiScope.unstable {
1302 continue
1303 }
Paul Duffin958806b2022-05-16 13:10:47 +00001304 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001305 missingApiModules = append(missingApiModules, m)
1306 }
Paul Duffin958806b2022-05-16 13:10:47 +00001307 if m := module.latestRemovedApiModuleName(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.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001311 missingApiModules = append(missingApiModules, m)
1312 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001313 }
1314 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1315 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1316 m += "You need to do one of the following:\n"
1317 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1318 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1319 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1320 m += "\n"
1321 m += "The following filegroup modules are missing:\n "
1322 m += strings.Join(missingApiModules, "\n ") + "\n"
1323 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."
1324 ctx.ModuleErrorf(m)
1325 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001326 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001327 // Only add the deps for the library if it is actually going to be built.
1328 module.Library.deps(ctx)
1329 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001330}
1331
Paul Duffin46dc45a2020-05-14 15:39:10 +01001332func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1333 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001334 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001335 return paths, err
1336 }
Colin Cross4acaea92021-12-10 23:05:02 +00001337 if module.requiresRuntimeImplementationLibrary() {
1338 return module.Library.OutputFiles(tag)
1339 }
1340 if tag == "" {
1341 return nil, nil
1342 }
1343 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001344}
1345
Inseob Kimc0907f12019-02-08 21:00:45 +09001346func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001347 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1348 module.CheckMinSdkVersion(ctx)
1349 }
1350
Paul Duffina2ae7e02020-09-11 11:55:00 +01001351 module.generateCommonBuildActions(ctx)
1352
Paul Duffindfa131e2020-05-15 20:37:11 +01001353 // Only build an implementation library if required.
1354 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001355 module.Library.GenerateAndroidBuildActions(ctx)
1356 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001357
Paul Duffinb97b1572021-04-29 21:50:40 +01001358 // Collate the components exported by this module. All scope specific modules are exported but
1359 // the impl and xml component modules are not.
1360 exportedComponents := map[string]struct{}{}
1361
Sundong Ahn57368eb2018-07-06 11:20:23 +09001362 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001363 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001364 // the recorded paths will be returned depending on the link type of the caller.
1365 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001366 tag := ctx.OtherModuleDependencyTag(to)
1367
Paul Duffinc8782502020-04-29 20:45:27 +01001368 // Extract information from any of the scope specific dependencies.
1369 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1370 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001371 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001372
1373 // Extract information from the dependency. The exact information extracted
1374 // is determined by the nature of the dependency which is determined by the tag.
1375 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001376
1377 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001378 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001379 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001380
1381 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001382 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Paul Duffinb97b1572021-04-29 21:50:40 +01001383 ctx.SetProvider(android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001384
1385 // Provide additional information for inclusion in an sdk's generated .info file.
1386 additionalSdkInfo := map[string]interface{}{}
1387 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001388 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001389 scopes := map[string]interface{}{}
1390 additionalSdkInfo["scopes"] = scopes
1391 for scope, scopePaths := range module.scopePaths {
1392 scopeInfo := map[string]interface{}{}
1393 scopes[scope.name] = scopeInfo
1394 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1395 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1396 if p := scopePaths.latestApiPath; p.Valid() {
1397 scopeInfo["latest_api"] = p.Path().String()
1398 }
1399 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1400 scopeInfo["latest_removed_api"] = p.Path().String()
1401 }
1402 }
1403 ctx.SetProvider(android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001404}
1405
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001406func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001407 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001408 return nil
1409 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001410 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001411 if module.sharedLibrary() {
1412 entries := &entriesList[0]
1413 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1414 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001415 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001416}
1417
Anton Hansson5fd5d242020-03-27 19:43:19 +00001418// The dist path of the stub artifacts
1419func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001420 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001421}
1422
Paul Duffin12ceb462019-12-24 20:31:31 +00001423// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001424func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001425 scopeProperties := module.scopeToProperties[apiScope]
1426 if scopeProperties.Sdk_version != nil {
1427 return proptools.String(scopeProperties.Sdk_version)
1428 }
1429
Jiyong Parkf1691d22021-03-29 20:11:58 +09001430 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001431 if sdkDep.hasStandardLibs() {
1432 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001433 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001434 } else {
1435 // Otherwise, use no system module.
1436 return "none"
1437 }
1438}
1439
Paul Duffin31310252020-11-20 21:26:20 +00001440func (module *SdkLibrary) distStem() string {
1441 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1442}
1443
Colin Cross986b69a2021-06-01 13:13:40 -07001444// distGroup returns the subdirectory of the dist path of the stub artifacts.
1445func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001446 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001447}
1448
Paul Duffin958806b2022-05-16 13:10:47 +00001449func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1450 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1451}
1452
Paul Duffind1b3a922020-01-22 11:57:20 +00001453func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001454 return ":" + module.latestApiModuleName(apiScope)
1455}
1456
1457func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
1458 return latestPrebuiltApiModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001459}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001460
Paul Duffind1b3a922020-01-22 11:57:20 +00001461func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001462 return ":" + module.latestRemovedApiModuleName(apiScope)
1463}
1464
1465func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
1466 return latestPrebuiltApiModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001467}
1468
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001469func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001470 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1471}
1472
1473func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1474 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001475}
1476
Anton Hansson944e77d2020-08-19 11:40:22 +01001477func childModuleVisibility(childVisibility []string) []string {
1478 if childVisibility == nil {
1479 // No child visibility set. The child will use the visibility of the sdk_library.
1480 return nil
1481 }
1482
1483 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1484 var visibility []string
1485 visibility = append(visibility, "//visibility:override")
1486 visibility = append(visibility, childVisibility...)
1487 return visibility
1488}
1489
Paul Duffin5df79302020-05-16 15:52:12 +01001490// Creates the implementation java library
1491func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001492 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1493
Paul Duffin5df79302020-05-16 15:52:12 +01001494 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001495 Name *string
1496 Visibility []string
1497 Instrument bool
1498 Libs []string
1499 Static_libs []string
1500 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001501 }{
1502 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001503 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001504 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1505 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001506 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1507 // addition of &module.properties below.
1508 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001509 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1510 // addition of &module.properties below.
1511 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1512 // Pass the apex_available settings down so that the impl library can be statically
1513 // embedded within a library that is added to an APEX. Needed for updatable-media.
1514 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001515 }
1516
1517 properties := []interface{}{
1518 &module.properties,
1519 &module.protoProperties,
1520 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001521 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001522 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001523 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001524 &props,
1525 module.sdkComponentPropertiesForChildLibrary(),
1526 }
1527 mctx.CreateModule(LibraryFactory, properties...)
1528}
1529
Jiyong Parkc678ad32018-04-10 13:07:10 +09001530// Creates a static java library that has API stubs
Paul Duffinf0229202020-04-29 16:47:28 +01001531func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001532 props := struct {
Dan Willemsen9f435972020-05-28 15:28:00 -07001533 Name *string
1534 Visibility []string
1535 Srcs []string
1536 Installable *bool
1537 Sdk_version *string
1538 System_modules *string
1539 Patch_module *string
1540 Libs []string
Anton Hanssondae54cd2021-04-21 16:30:10 +01001541 Static_libs []string
Dan Willemsen9f435972020-05-28 15:28:00 -07001542 Compile_dex *bool
1543 Java_version *string
1544 Openjdk9 struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001545 Srcs []string
1546 Javacflags []string
1547 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001548 Dist struct {
1549 Targets []string
1550 Dest *string
1551 Dir *string
1552 Tag *string
1553 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001554 }{}
1555
Paul Duffinc3091c82020-05-08 14:16:20 +01001556 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Anton Hansson944e77d2020-08-19 11:40:22 +01001557 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001558 // sources are generated from the droiddoc
Paul Duffinc3091c82020-05-08 14:16:20 +01001559 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
Paul Duffin12ceb462019-12-24 20:31:31 +00001560 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001561 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001562 props.System_modules = module.deviceProperties.System_modules
1563 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001564 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001565 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Anton Hanssondae54cd2021-04-21 16:30:10 +01001566 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001567 // The stub-annotations library contains special versions of the annotations
1568 // with CLASS retention policy, so that they're kept.
1569 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1570 props.Libs = append(props.Libs, "stub-annotations")
1571 }
Paul Duffina18abc22020-05-16 18:54:24 +01001572 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1573 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001574 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1575 // interop with older developer tools that don't support 1.9.
1576 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001577
1578 // The imports need to be compiled to dex if the java_sdk_library requests it.
1579 compileDex := module.dexProperties.Compile_dex
1580 if module.stubLibrariesCompiledForDex() {
1581 compileDex = proptools.BoolPtr(true)
Sundong Ahndd567f92018-07-31 17:19:11 +09001582 }
Paul Duffinf4600f62021-05-13 22:34:45 +01001583 props.Compile_dex = compileDex
Jiyong Parkc678ad32018-04-10 13:07:10 +09001584
Anton Hansson5fd5d242020-03-27 19:43:19 +00001585 // Dist the class jar artifact for sdk builds.
1586 if !Bool(module.sdkLibraryProperties.No_dist) {
1587 props.Dist.Targets = []string{"sdk", "win_sdk"}
Paul Duffin31310252020-11-20 21:26:20 +00001588 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
Anton Hansson5fd5d242020-03-27 19:43:19 +00001589 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1590 props.Dist.Tag = proptools.StringPtr(".jar")
1591 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001592
Paul Duffin859fe962020-05-15 10:20:31 +01001593 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001594}
1595
Paul Duffin6d0886e2020-04-07 18:49:53 +01001596// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001597// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001598func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001599 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001600 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001601 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001602 Srcs []string
1603 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001604 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001605 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001606 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001607 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001608 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001609 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001610 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001611 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001612 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001613 Merge_annotations_dirs []string
1614 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001615 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001616 Previous_api *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001617 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001618 Current ApiToCheck
1619 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001620
1621 Api_lint struct {
1622 Enabled *bool
1623 New_since *string
1624 Baseline_file *string
1625 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001626 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001627 Aidl struct {
1628 Include_dirs []string
1629 Local_include_dirs []string
1630 }
Paul Duffin040e9062020-11-23 17:41:36 +00001631 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001632 }{}
1633
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001634 // The stubs source processing uses the same compile time classpath when extracting the
1635 // API from the implementation library as it does when compiling it. i.e. the same
1636 // * sdk version
1637 // * system_modules
1638 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001639
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001640 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001641 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001642 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001643 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001644 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001645 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001646 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001647 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001648 // A droiddoc module has only one Libs property and doesn't distinguish between
1649 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001650 props.Libs = module.properties.Libs
1651 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001652 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001653 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1654 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1655 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001656
Paul Duffine22c2ab2020-05-20 19:35:27 +01001657 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001658 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1659 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1660
Paul Duffin6d0886e2020-04-07 18:49:53 +01001661 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001662 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001663 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001664 }
1665 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001666 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001667 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1668 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001669 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Paul Duffin235ffff2019-12-24 10:41:30 +00001670 disabledWarnings := []string{
Paul Duffin235ffff2019-12-24 10:41:30 +00001671 "BroadcastBehavior",
Paul Duffin235ffff2019-12-24 10:41:30 +00001672 "DeprecationMismatch",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001673 "HiddenSuperclass",
Paul Duffin235ffff2019-12-24 10:41:30 +00001674 "HiddenTypeParameter",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001675 "MissingPermission",
1676 "SdkConstant",
Paul Duffin235ffff2019-12-24 10:41:30 +00001677 "Todo",
1678 "Typo",
Anton Hansson3c0779a2022-02-18 19:24:30 +00001679 "UnavailableSymbol",
Paul Duffin235ffff2019-12-24 10:41:30 +00001680 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001681 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001682
Paul Duffin6877e6d2020-09-25 19:59:14 +01001683 // Output Javadoc comments for public scope.
1684 if apiScope == apiScopePublic {
1685 props.Output_javadoc_comments = proptools.BoolPtr(true)
1686 }
1687
Paul Duffin1fb487d2020-04-07 18:50:10 +01001688 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001689 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001690 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001691 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001692
Paul Duffin15f34ef2020-07-20 18:04:44 +01001693 // List of APIs identified from the provided source files are created. They are later
1694 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1695 // last-released (a.k.a numbered) list of API.
1696 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1697 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1698 apiDir := module.getApiDir()
1699 currentApiFileName = path.Join(apiDir, currentApiFileName)
1700 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001701
Paul Duffin15f34ef2020-07-20 18:04:44 +01001702 // check against the not-yet-release API
1703 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1704 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001705
Paul Duffin958806b2022-05-16 13:10:47 +00001706 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001707 // check against the latest released API
1708 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001709 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001710 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1711 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1712 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001713 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1714 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001715
Paul Duffin15f34ef2020-07-20 18:04:44 +01001716 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1717 // Enable api lint.
1718 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1719 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001720
Paul Duffin15f34ef2020-07-20 18:04:44 +01001721 // If it exists then pass a lint-baseline.txt through to droidstubs.
1722 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1723 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1724 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1725 if err != nil {
1726 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1727 }
1728 if len(paths) == 1 {
1729 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1730 } else if len(paths) != 0 {
1731 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001732 }
1733 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001734 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001735
Paul Duffin15f34ef2020-07-20 18:04:44 +01001736 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001737 // Dist the api txt and removed api txt artifacts for sdk builds.
1738 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1739 for _, p := range []struct {
1740 tag string
1741 pattern string
1742 }{
1743 {tag: ".api.txt", pattern: "%s.txt"},
1744 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1745 } {
1746 props.Dists = append(props.Dists, android.Dist{
1747 Targets: []string{"sdk", "win_sdk"},
1748 Dir: distDir,
1749 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1750 Tag: proptools.StringPtr(p.tag),
1751 })
1752 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001753 }
1754
Jihoon Kangd48abd52023-02-02 22:32:31 +00001755 mctx.CreateModule(DroidstubsFactory, &props).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001756}
1757
Paul Duffin958806b2022-05-16 13:10:47 +00001758func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
1759 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
1760}
1761
Paul Duffinea8f8082021-06-24 13:25:57 +01001762// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09001763func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1764 depTag := mctx.OtherModuleDependencyTag(dep)
1765 if depTag == xmlPermissionsFileTag {
1766 return true
1767 }
1768 return module.Library.DepIsInSameApex(mctx, dep)
1769}
1770
Paul Duffinea8f8082021-06-24 13:25:57 +01001771// Implements android.ApexModule
1772func (module *SdkLibrary) UniqueApexVariations() bool {
1773 return module.uniqueApexVariations()
1774}
1775
Jiyong Parkc678ad32018-04-10 13:07:10 +09001776// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01001777func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001778 moduleMinApiLevel := module.Library.MinSdkVersion(mctx).ApiLevel
1779 var moduleMinApiLevelStr = moduleMinApiLevel.String()
1780 if moduleMinApiLevel == android.NoneApiLevel {
1781 moduleMinApiLevelStr = "current"
1782 }
Jiyong Parke3833882020-02-17 17:28:10 +09001783 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00001784 Name *string
1785 Lib_name *string
1786 Apex_available []string
1787 On_bootclasspath_since *string
1788 On_bootclasspath_before *string
1789 Min_device_sdk *string
1790 Max_device_sdk *string
1791 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09001792 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00001793 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
1794 Lib_name: proptools.StringPtr(module.BaseModuleName()),
1795 Apex_available: module.ApexProperties.Apex_available,
1796 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
1797 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
1798 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
1799 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
1800 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001801 }
Jiyong Parke3833882020-02-17 17:28:10 +09001802
Jiyong Parke3833882020-02-17 17:28:10 +09001803 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001804}
1805
Jiyong Parkf1691d22021-03-29 20:11:58 +09001806func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09001807 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001808 var kind android.SdkKind
1809 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09001810 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001811 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09001812 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09001813 // We don't have prebuilt SDK for the specific sdkVersion.
1814 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09001815 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09001816 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09001817 }
Jiyong Park6a927c42020-01-21 02:03:43 +09001818
1819 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00001820 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09001821 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09001822 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08001823 if ctx.Config().AllowMissingDependencies() {
1824 return android.Paths{android.PathForSource(ctx, jar)}
1825 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001826 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08001827 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09001828 return nil
1829 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001830 return android.Paths{jarPath.Path()}
1831}
1832
Colin Crossaede88c2020-08-11 12:17:01 -07001833// 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 +01001834//
1835// If either this or the other module are on the platform then this will return
1836// false.
Colin Cross56a83212020-09-15 18:30:11 -07001837func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
1838 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1839 otherApexInfo := ctx.OtherModuleProvider(other, android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkab50b072021-05-12 17:13:56 +09001840 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01001841}
1842
Jiyong Parkf1691d22021-03-29 20:11:58 +09001843func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001844 // If the client doesn't set sdk_version, but if this library prefers stubs over
1845 // the impl library, let's provide the widest API surface possible. To do so,
1846 // force override sdk_version to module_current so that the closest possible API
1847 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09001848 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09001849 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09001850 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001851
Paul Duffindaaa3322020-05-26 18:13:57 +01001852 // Only provide access to the implementation library if it is actually built.
1853 if module.requiresRuntimeImplementationLibrary() {
1854 // Check any special cases for java_sdk_library.
1855 //
1856 // Only allow access to the implementation library in the following condition:
1857 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01001858 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001859 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001860 if headerJars {
1861 return module.HeaderJars()
1862 } else {
1863 return module.ImplementationJars()
1864 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001865 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001866 }
Paul Duffinb05d4292020-05-20 12:19:10 +01001867
Paul Duffin23970f42020-05-20 14:20:02 +01001868 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001869}
1870
Sundong Ahn241cd372018-07-13 16:16:44 +09001871// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001872func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001873 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1874}
1875
1876// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09001877func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00001878 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09001879}
1880
Colin Cross571cccf2019-02-04 11:22:08 -08001881var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1882
Jiyong Park82484c02018-04-23 21:41:26 +09001883func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001884 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09001885 return &[]string{}
1886 }).(*[]string)
1887}
1888
Paul Duffin749f98f2019-12-30 17:23:46 +00001889func (module *SdkLibrary) getApiDir() string {
1890 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1891}
1892
Jiyong Parkc678ad32018-04-10 13:07:10 +09001893// For a java_sdk_library module, create internal modules for stubs, docs,
1894// runtime libs and xml file. If requested, the stubs and docs are created twice
1895// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01001896func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1897 // If the module has been disabled then don't create any child modules.
1898 if !module.Enabled() {
1899 return
1900 }
1901
Paul Duffina18abc22020-05-16 18:54:24 +01001902 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09001903 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09001904 return
Inseob Kimc0907f12019-02-08 21:00:45 +09001905 }
1906
Paul Duffin37e0b772019-12-30 17:20:10 +00001907 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001908 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001909 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00001910 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01001911 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00001912
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001913 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09001914
Paul Duffin3375e352020-04-28 10:44:03 +01001915 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00001916
Paul Duffin749f98f2019-12-30 17:23:46 +00001917 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01001918 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09001919 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00001920 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09001921 p := android.ExistentPathForSource(mctx, path)
1922 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07001923 if mctx.Config().AllowMissingDependencies() {
1924 mctx.AddMissingDependencies([]string{path})
1925 } else {
1926 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1927 missingCurrentApi = true
1928 }
Inseob Kim8098faa2019-03-18 10:19:51 +09001929 }
1930 }
1931 }
1932
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001933 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09001934 script := "build/soong/scripts/gen-java-current-api-files.sh"
1935 p := android.ExistentPathForSource(mctx, script)
1936
1937 if !p.Valid() {
1938 panic(fmt.Sprintf("script file %s doesn't exist", script))
1939 }
1940
1941 mctx.ModuleErrorf("One or more current api files are missing. "+
1942 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00001943 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00001944 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01001945 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09001946 return
1947 }
1948
Paul Duffin3375e352020-04-28 10:44:03 +01001949 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001950 // Use the stubs source name for legacy reasons.
1951 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001952
Paul Duffind1b3a922020-01-22 11:57:20 +00001953 module.createStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09001954 }
1955
Paul Duffindfa131e2020-05-15 20:37:11 +01001956 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001957 // Create child module to create an implementation library.
1958 //
1959 // This temporarily creates a second implementation library that can be explicitly
1960 // referenced.
1961 //
1962 // TODO(b/156618935) - update comment once only one implementation library is created.
1963 module.createImplLibrary(mctx)
1964
Paul Duffindfa131e2020-05-15 20:37:11 +01001965 // Only create an XML permissions file that declares the library as being usable
1966 // as a shared library if required.
1967 if module.sharedLibrary() {
1968 module.createXmlFile(mctx)
1969 }
Paul Duffin43db9be2019-12-30 17:35:49 +00001970
1971 // record java_sdk_library modules so that they are exported to make
1972 javaSdkLibraries := javaSdkLibraries(mctx.Config())
1973 javaSdkLibrariesLock.Lock()
1974 defer javaSdkLibrariesLock.Unlock()
1975 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1976 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01001977
Paul Duffin77590a82022-04-28 14:13:30 +00001978 // 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 +01001979 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00001980 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09001981}
1982
1983func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07001984 module.addHostAndDeviceProperties()
1985 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001986
Paul Duffin71b33cc2021-06-23 11:39:47 +01001987 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001988
Paul Duffina18abc22020-05-16 18:54:24 +01001989 module.properties.Installable = proptools.BoolPtr(true)
1990 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09001991}
Sundong Ahn054b19a2018-10-19 13:46:09 +09001992
Paul Duffindfa131e2020-05-15 20:37:11 +01001993func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1994 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1995}
1996
Jiyong Park932cdfe2020-05-28 00:19:53 +09001997func (module *SdkLibrary) defaultsToStubs() bool {
1998 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1999}
2000
Paul Duffin1b1e8062020-05-08 13:44:43 +01002001// Defines how to name the individual component modules the sdk library creates.
2002type sdkLibraryComponentNamingScheme interface {
2003 stubsLibraryModuleName(scope *apiScope, baseName string) string
2004
2005 stubsSourceModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002006}
2007
2008type defaultNamingScheme struct {
2009}
2010
2011func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2012 return scope.stubsLibraryModuleName(baseName)
2013}
2014
2015func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2016 return scope.stubsSourceModuleName(baseName)
2017}
2018
Paul Duffin1b1e8062020-05-08 13:44:43 +01002019var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2020
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002021func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002022 // This suffix-based approach is fragile and could potentially mis-trigger.
2023 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Anton Hansson08f476b2021-04-07 15:32:19 +01002024 if strings.HasSuffix(name, apiScopePublic.stubsLibraryModuleNameSuffix()) {
2025 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2026 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2027 return false, javaPlatform
2028 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002029 return true, javaSdk
2030 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002031 if strings.HasSuffix(name, apiScopeSystem.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002032 return true, javaSystem
2033 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002034 if strings.HasSuffix(name, apiScopeModuleLib.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002035 return true, javaModule
2036 }
Anton Hansson08f476b2021-04-07 15:32:19 +01002037 if strings.HasSuffix(name, apiScopeTest.stubsLibraryModuleNameSuffix()) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002038 return true, javaSystem
2039 }
2040 return false, javaPlatform
2041}
2042
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002043// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2044// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2045// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2046// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2047// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002048func SdkLibraryFactory() android.Module {
2049 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002050
2051 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002052 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002053
Inseob Kimc0907f12019-02-08 21:00:45 +09002054 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002055 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002056 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002057
2058 // Initialize the map from scope to scope specific properties.
2059 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2060 for _, scope := range allApiScopes {
2061 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2062 }
2063 module.scopeToProperties = scopeToProperties
2064
Paul Duffin4911a892020-04-29 23:35:13 +01002065 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002066 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002067 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2068 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2069
Paul Duffin1b1e8062020-05-08 13:44:43 +01002070 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002071 // If no implementation is required then it cannot be used as a shared library
2072 // either.
2073 if !module.requiresRuntimeImplementationLibrary() {
2074 // If shared_library has been explicitly set to true then it is incompatible
2075 // with api_only: true.
2076 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2077 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2078 }
2079 // Set shared_library: false.
2080 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2081 }
2082
Paul Duffin1b1e8062020-05-08 13:44:43 +01002083 if module.initCommonAfterDefaultsApplied(ctx) {
2084 module.CreateInternalModules(ctx)
2085 }
2086 })
Zi Wangb2179e32023-01-31 15:53:30 -08002087 android.InitBazelModule(module)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002088 return module
2089}
Colin Cross79c7c262019-04-17 11:11:46 -07002090
Zi Wangb2179e32023-01-31 15:53:30 -08002091type bazelSdkLibraryAttributes struct {
2092 Public bazel.StringAttribute
2093 System bazel.StringAttribute
2094 Test bazel.StringAttribute
2095 Module_lib bazel.StringAttribute
2096 System_server bazel.StringAttribute
2097}
2098
2099// java_sdk_library bp2build converter
2100func (module *SdkLibrary) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
2101 if ctx.ModuleType() != "java_sdk_library" {
2102 return
2103 }
2104
2105 nameToAttr := make(map[string]bazel.StringAttribute)
2106
2107 for _, scope := range module.getGeneratedApiScopes(ctx) {
2108 apiSurfaceFile := path.Join(module.getApiDir(), scope.apiFilePrefix+"current.txt")
2109 var scopeStringAttribute bazel.StringAttribute
2110 scopeStringAttribute.SetValue(apiSurfaceFile)
2111 nameToAttr[scope.name] = scopeStringAttribute
2112 }
2113
2114 attrs := bazelSdkLibraryAttributes{
2115 Public: nameToAttr["public"],
2116 System: nameToAttr["system"],
2117 Test: nameToAttr["test"],
2118 Module_lib: nameToAttr["module-lib"],
2119 System_server: nameToAttr["system-server"],
2120 }
2121 props := bazel.BazelTargetModuleProperties{
2122 Rule_class: "java_sdk_library",
2123 Bzl_load_location: "//build/bazel/rules/java:sdk_library.bzl",
2124 }
2125
2126 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs)
2127}
2128
Colin Cross79c7c262019-04-17 11:11:46 -07002129//
2130// SDK library prebuilts
2131//
2132
Paul Duffin56d44902020-01-31 13:36:25 +00002133// Properties associated with each api scope.
2134type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002135 Jars []string `android:"path"`
2136
2137 Sdk_version *string
2138
Colin Cross79c7c262019-04-17 11:11:46 -07002139 // List of shared java libs that this module has dependencies to
2140 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002141
Paul Duffinc8782502020-04-29 20:45:27 +01002142 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002143 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002144
2145 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002146 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002147
2148 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002149 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002150
2151 // Annotation zip
2152 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002153}
2154
Paul Duffin56d44902020-01-31 13:36:25 +00002155type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002156 // List of shared java libs, common to all scopes, that this module has
2157 // dependencies to
2158 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002159
2160 // If set to true, compile dex files for the stubs. Defaults to false.
2161 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002162
2163 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002164 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00002165}
2166
Paul Duffineedc5d52020-06-12 17:46:39 +01002167type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002168 android.ModuleBase
2169 android.DefaultableModuleBase
2170 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002171 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002172
Paul Duffin37856732021-02-26 14:24:15 +00002173 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002174 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002175
Colin Cross79c7c262019-04-17 11:11:46 -07002176 properties sdkLibraryImportProperties
2177
Paul Duffin46a26a82020-04-07 19:27:04 +01002178 // Map from api scope to the scope specific property structure.
2179 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2180
Paul Duffin56d44902020-01-31 13:36:25 +00002181 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002182
2183 // The reference to the implementation library created by the source module.
2184 // Is nil if the source module does not exist.
2185 implLibraryModule *Library
2186
2187 // The reference to the xml permissions module created by the source module.
2188 // Is nil if the source module does not exist.
2189 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002190
Jeongik Chad5fe8782021-07-08 01:13:11 +09002191 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002192 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09002193
2194 // Expected install file path of the source module(sdk_library)
2195 // or dex implementation jar obtained from the prebuilt_apex, if any.
2196 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002197}
2198
Paul Duffineedc5d52020-06-12 17:46:39 +01002199var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002200
Paul Duffin46a26a82020-04-07 19:27:04 +01002201// The type of a structure that contains a field of type sdkLibraryScopeProperties
2202// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002203//
2204// struct {
2205// Public sdkLibraryScopeProperties
2206// System sdkLibraryScopeProperties
2207// ...
2208// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002209var allScopeStructType = createAllScopePropertiesStructType()
2210
2211// Dynamically create a structure type for each apiscope in allApiScopes.
2212func createAllScopePropertiesStructType() reflect.Type {
2213 var fields []reflect.StructField
2214 for _, apiScope := range allApiScopes {
2215 field := reflect.StructField{
2216 Name: apiScope.fieldName,
2217 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2218 }
2219 fields = append(fields, field)
2220 }
2221
2222 return reflect.StructOf(fields)
2223}
2224
2225// Create an instance of the scope specific structure type and return a map
2226// from apiscope to a pointer to each scope specific field.
2227func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2228 allScopePropertiesPtr := reflect.New(allScopeStructType)
2229 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2230 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2231
2232 for _, apiScope := range allApiScopes {
2233 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2234 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2235 }
2236
2237 return allScopePropertiesPtr.Interface(), scopeProperties
2238}
2239
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002240// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002241func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002242 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002243
Paul Duffin46a26a82020-04-07 19:27:04 +01002244 allScopeProperties, scopeToProperties := createPropertiesInstance()
2245 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002246 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002247
Paul Duffinc3091c82020-05-08 14:16:20 +01002248 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002249 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002250
Paul Duffin0bdcb272020-02-06 15:24:57 +00002251 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002252 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002253 InitJavaModule(module, android.HostAndDeviceSupported)
2254
Paul Duffin1b1e8062020-05-08 13:44:43 +01002255 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2256 if module.initCommonAfterDefaultsApplied(mctx) {
2257 module.createInternalModules(mctx)
2258 }
2259 })
Colin Cross79c7c262019-04-17 11:11:46 -07002260 return module
2261}
2262
Paul Duffin630b11e2021-07-15 13:35:26 +01002263var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2264
2265func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2266 return module.properties.Permitted_packages
2267}
2268
Paul Duffineedc5d52020-06-12 17:46:39 +01002269func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002270 return &module.prebuilt
2271}
2272
Paul Duffineedc5d52020-06-12 17:46:39 +01002273func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002274 return module.prebuilt.Name(module.ModuleBase.Name())
2275}
2276
Paul Duffineedc5d52020-06-12 17:46:39 +01002277func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002278
Paul Duffin50061512020-01-21 16:31:05 +00002279 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002280 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002281 module.prebuilt.ForcePrefer()
2282 }
2283
Paul Duffin46a26a82020-04-07 19:27:04 +01002284 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002285 if len(scopeProperties.Jars) == 0 {
2286 continue
2287 }
2288
Paul Duffinbbb546b2020-04-09 00:07:11 +01002289 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002290
Paul Duffin0f8faff2020-05-20 16:18:00 +01002291 if len(scopeProperties.Stub_srcs) > 0 {
2292 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2293 }
Paul Duffin56d44902020-01-31 13:36:25 +00002294 }
Colin Cross79c7c262019-04-17 11:11:46 -07002295
2296 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2297 javaSdkLibrariesLock.Lock()
2298 defer javaSdkLibrariesLock.Unlock()
2299 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2300}
2301
Paul Duffineedc5d52020-06-12 17:46:39 +01002302func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002303 // Creates a java import for the jar with ".stubs" suffix
2304 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002305 Name *string
2306 Sdk_version *string
2307 Libs []string
2308 Jars []string
Paul Duffin1267d872021-04-16 17:21:36 +01002309 Compile_dex *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002310
2311 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002312 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002313 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002314 props.Sdk_version = scopeProperties.Sdk_version
2315 // Prepend any of the libs from the legacy public properties to the libs for each of the
2316 // scopes to avoid having to duplicate them in each scope.
2317 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2318 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002319
Paul Duffin38b57852020-05-13 16:08:09 +01002320 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002321 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002322
Paul Duffin1267d872021-04-16 17:21:36 +01002323 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002324 compileDex := module.properties.Compile_dex
2325 if module.stubLibrariesCompiledForDex() {
2326 compileDex = proptools.BoolPtr(true)
2327 }
2328 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002329
Paul Duffin859fe962020-05-15 10:20:31 +01002330 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002331}
2332
Paul Duffineedc5d52020-06-12 17:46:39 +01002333func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002334 props := struct {
Paul Duffinbf4de042022-09-27 12:41:52 +01002335 Name *string
2336 Srcs []string
2337
2338 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002339 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002340 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002341 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002342
2343 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002344 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2345
2346 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002347}
2348
Paul Duffin44f1d842020-06-26 20:17:02 +01002349// Add the dependencies on the child module in the component deps mutator so that it
2350// creates references to the prebuilt and not the source modules.
2351func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002352 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002353 if len(scopeProperties.Jars) == 0 {
2354 continue
2355 }
2356
2357 // Add dependencies to the prebuilt stubs library
Paul Duffin864116c2021-04-02 10:24:13 +01002358 ctx.AddVariationDependencies(nil, apiScope.stubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002359
2360 if len(scopeProperties.Stub_srcs) > 0 {
2361 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002362 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002363 }
Paul Duffin56d44902020-01-31 13:36:25 +00002364 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002365}
2366
2367// Add other dependencies as normal.
2368func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002369
2370 implName := module.implLibraryModuleName()
2371 if ctx.OtherModuleExists(implName) {
2372 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2373
2374 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2375 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2376 // Add dependency to the rule for generating the xml permissions file
2377 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2378 }
2379 }
Colin Cross79c7c262019-04-17 11:11:46 -07002380}
2381
Jiakai Zhang204356f2021-09-09 08:12:46 +00002382func (module *SdkLibraryImport) AndroidMkEntries() []android.AndroidMkEntries {
2383 // For an SDK library imported from a prebuilt APEX, we don't need a Make module for itself, as we
2384 // don't need to install it. However, we need to add its dexpreopt outputs as sub-modules, if it
2385 // is preopted.
2386 dexpreoptEntries := module.dexpreopter.AndroidMkEntriesForApex()
2387 return append(dexpreoptEntries, android.AndroidMkEntries{Disabled: true})
2388}
2389
Jiyong Park45bf82e2020-12-15 22:29:02 +09002390var _ android.ApexModule = (*SdkLibraryImport)(nil)
2391
2392// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002393func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2394 depTag := mctx.OtherModuleDependencyTag(dep)
2395 if depTag == xmlPermissionsFileTag {
2396 return true
2397 }
2398
2399 // None of the other dependencies of the java_sdk_library_import are in the same apex
2400 // as the one that references this module.
2401 return false
2402}
2403
Jiyong Park45bf82e2020-12-15 22:29:02 +09002404// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002405func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2406 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002407 // we don't check prebuilt modules for sdk_version
2408 return nil
2409}
2410
Paul Duffinea8f8082021-06-24 13:25:57 +01002411// Implements android.ApexModule
2412func (module *SdkLibraryImport) UniqueApexVariations() bool {
2413 return module.uniqueApexVariations()
2414}
2415
Paul Duffin09817d62022-04-28 17:45:11 +01002416// MinSdkVersion - Implements hiddenAPIModule
2417func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
2418 return android.SdkSpecNone
2419}
2420
2421var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2422
Paul Duffineedc5d52020-06-12 17:46:39 +01002423func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002424 paths, err := module.commonOutputFiles(tag)
2425 if paths != nil || err != nil {
2426 return paths, err
2427 }
2428 if module.implLibraryModule != nil {
2429 return module.implLibraryModule.OutputFiles(tag)
2430 } else {
2431 return nil, nil
2432 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002433}
2434
Paul Duffineedc5d52020-06-12 17:46:39 +01002435func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002436 module.generateCommonBuildActions(ctx)
2437
Jeongik Chad5fe8782021-07-08 01:13:11 +09002438 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2439 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2440
Paul Duffin0f8faff2020-05-20 16:18:00 +01002441 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002442 ctx.VisitDirectDeps(func(to android.Module) {
2443 tag := ctx.OtherModuleDependencyTag(to)
2444
Paul Duffin0f8faff2020-05-20 16:18:00 +01002445 // Extract information from any of the scope specific dependencies.
2446 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2447 apiScope := scopeTag.apiScope
2448 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2449
2450 // Extract information from the dependency. The exact information extracted
2451 // is determined by the nature of the dependency which is determined by the tag.
2452 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002453 } else if tag == implLibraryTag {
2454 if implLibrary, ok := to.(*Library); ok {
2455 module.implLibraryModule = implLibrary
2456 } else {
2457 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2458 }
2459 } else if tag == xmlPermissionsFileTag {
2460 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2461 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2462 } else {
2463 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2464 }
Colin Cross79c7c262019-04-17 11:11:46 -07002465 }
2466 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002467
2468 // Populate the scope paths with information from the properties.
2469 for apiScope, scopeProperties := range module.scopeProperties {
2470 if len(scopeProperties.Jars) == 0 {
2471 continue
2472 }
2473
2474 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002475 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002476 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2477 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2478 }
Paul Duffin39853512021-02-26 11:09:39 +00002479
2480 if ctx.Device() {
2481 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2482 // obtained from the associated deapexer module.
2483 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2484 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002485 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01002486 di := android.FindDeapexerProviderForModule(ctx)
2487 if di == nil {
2488 return // An error has been reported by FindDeapexerProviderForModule.
2489 }
Jiakai Zhang81e46812023-02-08 21:56:07 +08002490 dexJarFileApexRootRelative := apexRootRelativePathToJavaLib(module.BaseModuleName())
2491 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002492 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2493 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002494 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002495 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002496 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002497 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002498
Jiakai Zhang204356f2021-09-09 08:12:46 +00002499 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2500 module.dexpreopter.isSDKLibrary = true
2501 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002502
2503 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2504 module.dexpreopter.inputProfilePathOnHost = profilePath
2505 }
2506
2507 // Dexpreopting.
Jiakai Zhang204356f2021-09-09 08:12:46 +00002508 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002509 } else {
2510 // This should never happen as a variant for a prebuilt_apex is only created if the
2511 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002512 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002513 }
2514 }
2515 }
Colin Cross79c7c262019-04-17 11:11:46 -07002516}
2517
Jiyong Parkf1691d22021-03-29 20:11:58 +09002518func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002519
2520 // For consistency with SdkLibrary make the implementation jar available to libraries that
2521 // are within the same APEX.
2522 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002523 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002524 if headerJars {
2525 return implLibraryModule.HeaderJars()
2526 } else {
2527 return implLibraryModule.ImplementationJars()
2528 }
2529 }
2530
Paul Duffin23970f42020-05-20 14:20:02 +01002531 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002532}
2533
Colin Cross79c7c262019-04-17 11:11:46 -07002534// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002535func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002536 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002537 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002538}
2539
2540// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002541func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002542 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002543 return module.sdkJars(ctx, sdkVersion, false)
2544}
2545
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002546// to satisfy UsesLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002547func (module *SdkLibraryImport) DexJarBuildPath() OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002548 // The dex implementation jar extracted from the .apex file should be used in preference to the
2549 // source.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002550 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002551 return module.dexJarFile
2552 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002553 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002554 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002555 } else {
2556 return module.implLibraryModule.DexJarBuildPath()
2557 }
2558}
2559
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002560// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002561func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002562 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002563}
2564
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002565// to satisfy UsesLibraryDependency interface
2566func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2567 return nil
2568}
2569
Paul Duffineedc5d52020-06-12 17:46:39 +01002570// to satisfy apex.javaDependency interface
2571func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2572 if module.implLibraryModule == nil {
2573 return nil
2574 } else {
2575 return module.implLibraryModule.JacocoReportClassesFile()
2576 }
2577}
2578
2579// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002580func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2581 if module.implLibraryModule == nil {
2582 return LintDepSets{}
2583 } else {
2584 return module.implLibraryModule.LintDepSets()
2585 }
2586}
2587
Spandan Das17854f52022-01-14 21:19:14 +00002588func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002589 if module.implLibraryModule == nil {
2590 return false
2591 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002592 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002593 }
2594}
2595
Spandan Das17854f52022-01-14 21:19:14 +00002596func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002597 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002598 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002599 }
2600}
2601
Colin Cross08dca382020-07-21 20:31:17 -07002602// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002603func (module *SdkLibraryImport) Stem() string {
2604 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002605}
Jiyong Parke3833882020-02-17 17:28:10 +09002606
Paul Duffin44b481b2020-06-17 16:59:43 +01002607var _ ApexDependency = (*SdkLibraryImport)(nil)
2608
2609// to satisfy java.ApexDependency interface
2610func (module *SdkLibraryImport) HeaderJars() android.Paths {
2611 if module.implLibraryModule == nil {
2612 return nil
2613 } else {
2614 return module.implLibraryModule.HeaderJars()
2615 }
2616}
2617
2618// to satisfy java.ApexDependency interface
2619func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2620 if module.implLibraryModule == nil {
2621 return nil
2622 } else {
2623 return module.implLibraryModule.ImplementationAndResourcesJars()
2624 }
2625}
2626
Jiakai Zhang204356f2021-09-09 08:12:46 +00002627// to satisfy java.DexpreopterInterface interface
2628func (module *SdkLibraryImport) IsInstallable() bool {
2629 return true
2630}
2631
Paul Duffinfef55002021-06-17 14:56:05 +01002632var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2633
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002634func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002635 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08002636 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01002637}
2638
Jiyong Parke3833882020-02-17 17:28:10 +09002639// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09002640type sdkLibraryXml struct {
2641 android.ModuleBase
2642 android.DefaultableModuleBase
2643 android.ApexModuleBase
2644
2645 properties sdkLibraryXmlProperties
2646
2647 outputFilePath android.OutputPath
2648 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002649
2650 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002651}
2652
2653type sdkLibraryXmlProperties struct {
2654 // canonical name of the lib
2655 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002656
2657 // Signals that this shared library is part of the bootclasspath starting
2658 // on the version indicated in this attribute.
2659 //
2660 // This will make platforms at this level and above to ignore
2661 // <uses-library> tags with this library name because the library is already
2662 // available
2663 On_bootclasspath_since *string
2664
2665 // Signals that this shared library was part of the bootclasspath before
2666 // (but not including) the version indicated in this attribute.
2667 //
2668 // The system will automatically add a <uses-library> tag with this library to
2669 // apps that target any SDK less than the version indicated in this attribute.
2670 On_bootclasspath_before *string
2671
2672 // Indicates that PackageManager should ignore this shared library if the
2673 // platform is below the version indicated in this attribute.
2674 //
2675 // This means that the device won't recognise this library as installed.
2676 Min_device_sdk *string
2677
2678 // Indicates that PackageManager should ignore this shared library if the
2679 // platform is above the version indicated in this attribute.
2680 //
2681 // This means that the device won't recognise this library as installed.
2682 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00002683
2684 // The SdkLibrary's min api level as a string
2685 //
2686 // This value comes from the ApiLevel of the MinSdkVersion property.
2687 Sdk_library_min_api_level *string
Jiyong Parke3833882020-02-17 17:28:10 +09002688}
2689
2690// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2691// Not to be used directly by users. java_sdk_library internally uses this.
2692func sdkLibraryXmlFactory() android.Module {
2693 module := &sdkLibraryXml{}
2694
2695 module.AddProperties(&module.properties)
2696
2697 android.InitApexModule(module)
2698 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2699
2700 return module
2701}
2702
Colin Crossaede88c2020-08-11 12:17:01 -07002703func (module *sdkLibraryXml) UniqueApexVariations() bool {
2704 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
2705 // mounted APEX, which contains the name of the APEX.
2706 return true
2707}
2708
Jiyong Parke3833882020-02-17 17:28:10 +09002709// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09002710func (module *sdkLibraryXml) BaseDir() string {
2711 return "etc"
2712}
2713
2714// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09002715func (module *sdkLibraryXml) SubDir() string {
2716 return "permissions"
2717}
2718
2719// from android.PrebuiltEtcModule
2720func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2721 return module.outputFilePath
2722}
2723
2724// from android.ApexModule
2725func (module *sdkLibraryXml) AvailableFor(what string) bool {
2726 return true
2727}
2728
2729func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2730 // do nothing
2731}
2732
Jiyong Park45bf82e2020-12-15 22:29:02 +09002733var _ android.ApexModule = (*sdkLibraryXml)(nil)
2734
2735// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002736func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2737 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002738 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
2739 return nil
2740}
2741
Jiyong Parke3833882020-02-17 17:28:10 +09002742// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07002743func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09002744 implName := proptools.String(module.properties.Lib_name)
Colin Cross56a83212020-09-15 18:30:11 -07002745 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07002746 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09002747 // In most cases, this works fine. But when apex_name is set or override_apex is used
2748 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07002749 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09002750 }
2751 partition := "system"
2752 if module.SocSpecific() {
2753 partition = "vendor"
2754 } else if module.DeviceSpecific() {
2755 partition = "odm"
2756 } else if module.ProductSpecific() {
2757 partition = "product"
2758 } else if module.SystemExtSpecific() {
2759 partition = "system_ext"
2760 }
2761 return "/" + partition + "/framework/" + implName + ".jar"
2762}
2763
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002764func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
2765 if value == nil {
2766 return ""
2767 }
2768 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
2769 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002770 // attributes in bp files have underscores but in the xml have dashes.
2771 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002772 return ""
2773 }
Pedro Loureirob638c622021-12-22 15:28:05 +00002774 if apiLevel.IsCurrent() {
2775 // passing "current" would always mean a future release, never the current (or the current in
2776 // progress) which means some conditions would never be triggered.
2777 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
2778 `"current" is not an allowed value for this attribute`)
2779 return ""
2780 }
Pedro Loureiro48991222022-06-17 20:01:21 +00002781 // "safeValue" is safe because it translates finalized codenames to a string
2782 // with their SDK int.
2783 safeValue := apiLevel.String()
2784 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002785}
2786
2787// formats an attribute for the xml permissions file if the value is not null
2788// returns empty string otherwise
2789func formattedOptionalAttribute(attrName string, value *string) string {
2790 if value == nil {
2791 return ""
2792 }
2793 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
2794}
2795
2796func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
2797 libName := proptools.String(module.properties.Lib_name)
2798 libNameAttr := formattedOptionalAttribute("name", &libName)
2799 filePath := module.implPath(ctx)
2800 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00002801 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
2802 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
2803 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
2804 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002805 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
2806 // 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 +00002807 var libraryTag string
2808 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00002809 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00002810 } else {
2811 libraryTag = ` <library\n`
2812 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002813
2814 return strings.Join([]string{
2815 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
2816 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
2817 `\n`,
2818 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
2819 ` you may not use this file except in compliance with the License.\n`,
2820 ` You may obtain a copy of the License at\n`,
2821 `\n`,
2822 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
2823 `\n`,
2824 ` Unless required by applicable law or agreed to in writing, software\n`,
2825 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
2826 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
2827 ` See the License for the specific language governing permissions and\n`,
2828 ` limitations under the License.\n`,
2829 `-->\n`,
2830 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00002831 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002832 libNameAttr,
2833 filePathAttr,
2834 implicitFromAttr,
2835 implicitUntilAttr,
2836 minSdkAttr,
2837 maxSdkAttr,
2838 ` />\n`,
2839 `</permissions>\n`}, "")
2840}
2841
Jiyong Parke3833882020-02-17 17:28:10 +09002842func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross56a83212020-09-15 18:30:11 -07002843 module.hideApexVariantFromMake = !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
2844
Jiyong Parke3833882020-02-17 17:28:10 +09002845 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002846 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002847 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002848
2849 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08002850 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09002851 rule.Command().
2852 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2853 Output(module.outputFilePath)
2854
Colin Crossf1a035e2020-11-16 17:32:30 -08002855 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09002856
2857 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2858}
2859
2860func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07002861 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00002862 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09002863 Disabled: true,
2864 }}
2865 }
2866
satayev8f088b02021-12-06 11:40:46 +00002867 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09002868 Class: "ETC",
2869 OutputFile: android.OptionalPathForPath(module.outputFilePath),
2870 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07002871 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09002872 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08002873 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09002874 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2875 },
2876 },
2877 }}
2878}
Paul Duffindd46f712020-02-10 13:37:10 +00002879
Pedro Loureiroc3621422021-09-28 15:40:23 +00002880func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
2881 module.validateAtLeastTAttributes(ctx)
2882 module.validateMinAndMaxDeviceSdk(ctx)
2883 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
2884 module.validateOnBootclasspathBeforeRequirements(ctx)
2885}
2886
2887func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
2888 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
2889 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
2890 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
2891 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
2892 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
2893}
2894
2895func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
2896 if attr != nil {
2897 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
2898 // we will inform the user of invalid inputs when we try to write the
2899 // permissions xml file so we don't need to do it here
2900 if t.GreaterThan(level) {
2901 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
2902 }
2903 }
2904 }
2905}
2906
2907func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
2908 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
2909 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
2910 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
2911 if minErr == nil && maxErr == nil {
2912 // we will inform the user of invalid inputs when we try to write the
2913 // permissions xml file so we don't need to do it here
2914 if min.GreaterThan(max) {
2915 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
2916 }
2917 }
2918 }
2919}
2920
2921func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
2922 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
2923 if module.properties.Min_device_sdk != nil {
2924 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
2925 if err == nil {
2926 if moduleMinApi.GreaterThan(api) {
2927 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
2928 }
2929 }
2930 }
2931 if module.properties.Max_device_sdk != nil {
2932 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
2933 if err == nil {
2934 if moduleMinApi.GreaterThan(api) {
2935 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
2936 }
2937 }
2938 }
2939}
2940
2941func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
2942 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
2943 if module.properties.On_bootclasspath_before != nil {
2944 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
2945 // if we use the attribute, then we need to do this validation
2946 if moduleMinApi.LessThan(t) {
2947 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
2948 if module.properties.Min_device_sdk == nil {
2949 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")
2950 }
2951 }
2952 }
2953}
2954
Paul Duffindd46f712020-02-10 13:37:10 +00002955type sdkLibrarySdkMemberType struct {
2956 android.SdkMemberTypeBase
2957}
2958
Paul Duffin296701e2021-07-14 10:29:36 +01002959func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
2960 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00002961}
2962
2963func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2964 _, ok := module.(*SdkLibrary)
2965 return ok
2966}
2967
2968func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2969 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2970}
2971
2972func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2973 return &sdkLibrarySdkMemberProperties{}
2974}
2975
Paul Duffin976b0e52021-04-27 23:20:26 +01002976var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
2977 android.SdkMemberTypeBase{
2978 PropertyName: "java_sdk_libs",
2979 SupportsSdk: true,
2980 },
2981}
2982
Paul Duffindd46f712020-02-10 13:37:10 +00002983type sdkLibrarySdkMemberProperties struct {
2984 android.SdkMemberPropertiesBase
2985
Paul Duffine8409952022-09-22 16:24:46 +01002986 // Stem name for files in the sdk snapshot.
2987 //
2988 // This is used to construct the path names of various sdk library files in the sdk snapshot to
2989 // make sure that they match the finalized versions of those files in prebuilts/sdk.
2990 //
2991 // This property is marked as keep so that it will be kept in all instances of this struct, will
2992 // not be cleared but will be copied to common structs. That is needed because this field is used
2993 // to construct many file names for other parts of this struct and so it needs to be present in
2994 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
2995 // be unavailable for generating file names if there were other properties that were still set.
2996 Stem string `sdk:"keep"`
2997
Paul Duffindd46f712020-02-10 13:37:10 +00002998 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00002999 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003000
Paul Duffin3d1248c2020-04-09 00:10:17 +01003001 // The Java stubs source files.
3002 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003003
3004 // The naming scheme.
3005 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003006
3007 // True if the java_sdk_library_import is for a shared library, false
3008 // otherwise.
3009 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003010
Paul Duffin1267d872021-04-16 17:21:36 +01003011 // True if the stub imports should produce dex jars.
3012 Compile_dex *bool
3013
Paul Duffina2ae7e02020-09-11 11:55:00 +01003014 // The paths to the doctag files to add to the prebuilt.
3015 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003016
3017 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003018
3019 // Signals that this shared library is part of the bootclasspath starting
3020 // on the version indicated in this attribute.
3021 //
3022 // This will make platforms at this level and above to ignore
3023 // <uses-library> tags with this library name because the library is already
3024 // available
3025 On_bootclasspath_since *string
3026
3027 // Signals that this shared library was part of the bootclasspath before
3028 // (but not including) the version indicated in this attribute.
3029 //
3030 // The system will automatically add a <uses-library> tag with this library to
3031 // apps that target any SDK less than the version indicated in this attribute.
3032 On_bootclasspath_before *string
3033
3034 // Indicates that PackageManager should ignore this shared library if the
3035 // platform is below the version indicated in this attribute.
3036 //
3037 // This means that the device won't recognise this library as installed.
3038 Min_device_sdk *string
3039
3040 // Indicates that PackageManager should ignore this shared library if the
3041 // platform is above the version indicated in this attribute.
3042 //
3043 // This means that the device won't recognise this library as installed.
3044 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003045
3046 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003047}
3048
3049type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003050 Jars android.Paths
3051 StubsSrcJar android.Path
3052 CurrentApiFile android.Path
3053 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003054 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003055 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003056}
3057
3058func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3059 sdk := variant.(*SdkLibrary)
3060
Paul Duffine8409952022-09-22 16:24:46 +01003061 // Copy the stem name for files in the sdk snapshot.
3062 s.Stem = sdk.distStem()
3063
Paul Duffin106a3a42022-01-27 16:39:06 +00003064 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003065 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003066 paths := sdk.findScopePaths(apiScope)
3067 if paths == nil {
3068 continue
3069 }
3070
Paul Duffindd46f712020-02-10 13:37:10 +00003071 jars := paths.stubsImplPath
3072 if len(jars) > 0 {
3073 properties := scopeProperties{}
3074 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003075 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003076 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003077 if paths.currentApiFilePath.Valid() {
3078 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3079 }
3080 if paths.removedApiFilePath.Valid() {
3081 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3082 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003083 // The annotations zip is only available for modules that set annotations_enabled: true.
3084 if paths.annotationsZip.Valid() {
3085 properties.AnnotationsZip = paths.annotationsZip.Path()
3086 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003087 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003088 }
3089 }
3090
Paul Duffindfa131e2020-05-15 20:37:11 +01003091 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003092 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003093 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003094 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003095 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003096 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3097 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3098 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3099 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003100
3101 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3102 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3103 }
Paul Duffindd46f712020-02-10 13:37:10 +00003104}
3105
3106func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003107 if s.Naming_scheme != nil {
3108 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3109 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003110 if s.Shared_library != nil {
3111 propertySet.AddProperty("shared_library", *s.Shared_library)
3112 }
Paul Duffin1267d872021-04-16 17:21:36 +01003113 if s.Compile_dex != nil {
3114 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3115 }
Paul Duffin869de142021-07-15 14:14:41 +01003116 if len(s.Permitted_packages) > 0 {
3117 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3118 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003119 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3120 if s.DexPreoptProfileGuided != nil {
3121 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3122 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003123
Paul Duffine8409952022-09-22 16:24:46 +01003124 stem := s.Stem
3125
Paul Duffindd46f712020-02-10 13:37:10 +00003126 for _, apiScope := range allApiScopes {
3127 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003128 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003129
Paul Duffin958806b2022-05-16 13:10:47 +00003130 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003131
Paul Duffindd46f712020-02-10 13:37:10 +00003132 var jars []string
3133 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003134 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003135 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3136 jars = append(jars, dest)
3137 }
3138 scopeSet.AddProperty("jars", jars)
3139
Paul Duffin22628d52021-05-12 23:13:22 +01003140 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3141 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003142 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003143 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3144 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3145 } else {
3146 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3147 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003148 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003149 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3150 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3151 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003152
Paul Duffin1fd005d2020-04-09 01:08:11 +01003153 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003154 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003155 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3156 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3157 }
3158
3159 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003160 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003161 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003162 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3163 }
3164
Anton Hanssond78eb762021-09-21 15:25:12 +01003165 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003166 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003167 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3168 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3169 }
3170
Paul Duffindd46f712020-02-10 13:37:10 +00003171 if properties.SdkVersion != "" {
3172 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3173 }
3174 }
3175 }
3176
Paul Duffina2ae7e02020-09-11 11:55:00 +01003177 if len(s.Doctag_paths) > 0 {
3178 dests := []string{}
3179 for _, p := range s.Doctag_paths {
3180 dest := filepath.Join("doctags", p.Rel())
3181 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3182 dests = append(dests, dest)
3183 }
3184 propertySet.AddProperty("doctag_files", dests)
3185 }
Paul Duffindd46f712020-02-10 13:37:10 +00003186}