blob: fbde04276ce89d4d67f60cf2ee6211b5472cd986 [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jihoon Kangee113282024-01-23 00:16:41 +000018 "errors"
Jiyong Parkc678ad32018-04-10 13:07:10 +090019 "fmt"
20 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090021 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010022 "reflect"
Paul Duffin46dc45a2020-05-14 15:39:10 +010023 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090024 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090025 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090026 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090027
Paul Duffind1b3a922020-01-22 11:57:20 +000028 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090029 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010030
31 "android/soong/android"
Ulya Trafimovichdbf31662020-12-17 12:07:54 +000032 "android/soong/dexpreopt"
Jiyong Parkc678ad32018-04-10 13:07:10 +090033)
34
Jooyung Han58f26ab2019-12-18 15:34:32 +090035const (
Pedro Loureiro9956e5e2021-09-07 17:21:59 +000036 sdkXmlFileSuffix = ".xml"
Jiyong Parkc678ad32018-04-10 13:07:10 +090037)
38
Paul Duffind1b3a922020-01-22 11:57:20 +000039// A tag to associated a dependency with a specific api scope.
40type scopeDependencyTag struct {
41 blueprint.BaseDependencyTag
42 name string
43 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010044
45 // Function for extracting appropriate path information from the dependency.
Colin Crossdcf71b22021-02-01 13:59:03 -080046 depInfoExtractor func(paths *scopePaths, ctx android.ModuleContext, dep android.Module) error
Paul Duffinc8782502020-04-29 20:45:27 +010047}
48
49// Extract tag specific information from the dependency.
50func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
Colin Crossdcf71b22021-02-01 13:59:03 -080051 err := tag.depInfoExtractor(paths, ctx, dep)
Paul Duffinc8782502020-04-29 20:45:27 +010052 if err != nil {
53 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
54 }
Paul Duffind1b3a922020-01-22 11:57:20 +000055}
56
Paul Duffin80342d72020-06-26 22:08:43 +010057var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
58
59func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
60 return false
61}
62
Paul Duffind1b3a922020-01-22 11:57:20 +000063// Provides information about an api scope, e.g. public, system, test.
64type apiScope struct {
65 // The name of the api scope, e.g. public, system, test
66 name string
67
Paul Duffin97b53b82020-05-05 14:40:52 +010068 // The api scope that this scope extends.
Paul Duffind0b9fca2022-09-30 18:11:41 +010069 //
70 // This organizes the scopes into an extension hierarchy.
71 //
72 // If set this means that the API provided by this scope includes the API provided by the scope
73 // set in this field.
Paul Duffin97b53b82020-05-05 14:40:52 +010074 extends *apiScope
75
Paul Duffind0b9fca2022-09-30 18:11:41 +010076 // The next api scope that a library that uses this scope can access.
77 //
78 // This organizes the scopes into an access hierarchy.
79 //
80 // If set this means that a library that can access this API can also access the API provided by
81 // the scope set in this field.
82 //
83 // A module that sets sdk_version: "<scope>_current" should have access to the <scope> API of
84 // every java_sdk_library that it depends on. If the library does not provide an API for <scope>
85 // then it will traverse up this access hierarchy to find an API that it does provide.
86 //
87 // If this is not set then it defaults to the scope set in extends.
88 canAccess *apiScope
89
Paul Duffin3375e352020-04-28 10:44:03 +010090 // The legacy enabled status for a specific scope can be dependent on other
91 // properties that have been specified on the library so it is provided by
92 // a function that can determine the status by examining those properties.
93 legacyEnabledStatus func(module *SdkLibrary) bool
94
95 // The default enabled status for non-legacy behavior, which is triggered by
96 // explicitly enabling at least one api scope.
97 defaultEnabledStatus bool
98
99 // Gets a pointer to the scope specific properties.
100 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
101
Paul Duffin46a26a82020-04-07 19:27:04 +0100102 // The name of the field in the dynamically created structure.
103 fieldName string
104
Paul Duffin6b836ba2020-05-13 19:19:49 +0100105 // The name of the property in the java_sdk_library_import
106 propertyName string
107
Jihoon Kangb7431552024-01-22 19:40:08 +0000108 // The tag to use to depend on the prebuilt stubs library module
109 prebuiltStubsTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000110
Jihoon Kangbd093452023-12-26 19:08:01 +0000111 // The tag to use to depend on the everything stubs library module.
112 everythingStubsTag scopeDependencyTag
113
114 // The tag to use to depend on the exportable stubs library module.
115 exportableStubsTag scopeDependencyTag
116
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100117 // The tag to use to depend on the stubs source module (if separate from the API module).
118 stubsSourceTag scopeDependencyTag
119
120 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
121 apiFileTag scopeDependencyTag
122
Paul Duffinc8782502020-04-29 20:45:27 +0100123 // The tag to use to depend on the stubs source and API module.
124 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000125
Paul Duffin958806b2022-05-16 13:10:47 +0000126 // The tag to use to depend on the module that provides the latest version of the API .txt file.
127 latestApiModuleTag scopeDependencyTag
128
129 // The tag to use to depend on the module that provides the latest version of the API removed.txt
130 // file.
131 latestRemovedApiModuleTag scopeDependencyTag
132
Paul Duffind1b3a922020-01-22 11:57:20 +0000133 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
134 apiFilePrefix string
135
Paul Duffind0b9fca2022-09-30 18:11:41 +0100136 // The scope specific suffix to add to the sdk library module name to construct a scope specific
Paul Duffind1b3a922020-01-22 11:57:20 +0000137 // module name.
138 moduleSuffix string
139
Paul Duffind1b3a922020-01-22 11:57:20 +0000140 // SDK version that the stubs library is built against. Note that this is always
141 // *current. Older stubs library built with a numbered SDK version is created from
142 // the prebuilt jar.
143 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100144
Paul Duffin15f34ef2020-07-20 18:04:44 +0100145 // The annotation that identifies this API level, empty for the public API scope.
146 annotation string
147
Paul Duffin1fb487d2020-04-07 18:50:10 +0100148 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100149 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100150 // This is not used directly but is used to construct the droidstubsArgs.
151 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100152
Paul Duffin15f34ef2020-07-20 18:04:44 +0100153 // The args that must be passed to droidstubs to generate the API and stubs source
154 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100155 //
156 // The API only includes the additional members that this scope adds over the scope
157 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100158 //
159 // The stubs source must include the definitions of everything that is in this
160 // api scope and all the scopes that this one extends.
161 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100162
Anton Hansson6478ac12020-05-02 11:19:36 +0100163 // Whether the api scope can be treated as unstable, and should skip compat checks.
164 unstable bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000165
166 // Represents the SDK kind of this scope.
167 kind android.SdkKind
Paul Duffind1b3a922020-01-22 11:57:20 +0000168}
169
170// Initialize a scope, creating and adding appropriate dependency tags
171func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100172 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100173 scopeByName[name] = scope
174 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100175 scope.propertyName = strings.ReplaceAll(name, "-", "_")
176 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Jihoon Kangb7431552024-01-22 19:40:08 +0000177 scope.prebuiltStubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100178 name: name + "-stubs",
179 apiScope: scope,
180 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000181 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000182 scope.everythingStubsTag = scopeDependencyTag{
183 name: name + "-stubs-everything",
184 apiScope: scope,
185 depInfoExtractor: (*scopePaths).extractEverythingStubsLibraryInfoFromDependency,
186 }
187 scope.exportableStubsTag = scopeDependencyTag{
188 name: name + "-stubs-exportable",
189 apiScope: scope,
190 depInfoExtractor: (*scopePaths).extractExportableStubsLibraryInfoFromDependency,
191 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100192 scope.stubsSourceTag = scopeDependencyTag{
193 name: name + "-stubs-source",
194 apiScope: scope,
195 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
196 }
197 scope.apiFileTag = scopeDependencyTag{
198 name: name + "-api",
199 apiScope: scope,
200 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
201 }
Paul Duffinc8782502020-04-29 20:45:27 +0100202 scope.stubsSourceAndApiTag = scopeDependencyTag{
203 name: name + "-stubs-source-and-api",
204 apiScope: scope,
205 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000206 }
Paul Duffin958806b2022-05-16 13:10:47 +0000207 scope.latestApiModuleTag = scopeDependencyTag{
208 name: name + "-latest-api",
209 apiScope: scope,
210 depInfoExtractor: (*scopePaths).extractLatestApiPath,
211 }
212 scope.latestRemovedApiModuleTag = scopeDependencyTag{
213 name: name + "-latest-removed-api",
214 apiScope: scope,
215 depInfoExtractor: (*scopePaths).extractLatestRemovedApiPath,
216 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100217
218 // To get the args needed to generate the stubs source append all the args from
219 // this scope and all the scopes it extends as each set of args adds additional
220 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100221 var scopeSpecificArgs []string
222 if scope.annotation != "" {
223 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100224 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100225 for s := scope; s != nil; s = s.extends {
226 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100227
Paul Duffin15f34ef2020-07-20 18:04:44 +0100228 // Ensure that the generated stubs includes all the API elements from the API scope
229 // that this scope extends.
230 if s != scope && s.annotation != "" {
231 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
232 }
233 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100234
Paul Duffind0b9fca2022-09-30 18:11:41 +0100235 // By default, a library that can access a scope can also access the scope it extends.
236 if scope.canAccess == nil {
237 scope.canAccess = scope.extends
238 }
239
Paul Duffin15f34ef2020-07-20 18:04:44 +0100240 // Escape any special characters in the arguments. This is needed because droidstubs
241 // passes these directly to the shell command.
242 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100243
Paul Duffind1b3a922020-01-22 11:57:20 +0000244 return scope
245}
246
Anton Hansson08f476b2021-04-07 15:32:19 +0100247func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
248 return ".stubs" + scope.moduleSuffix
249}
250
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000251func (scope *apiScope) exportableStubsLibraryModuleNameSuffix() string {
252 return ".stubs.exportable" + scope.moduleSuffix
253}
254
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000255func (scope *apiScope) apiLibraryModuleName(baseName string) string {
256 return scope.stubsLibraryModuleName(baseName) + ".from-text"
257}
258
Jihoon Kang1147b312023-06-08 23:25:57 +0000259func (scope *apiScope) sourceStubLibraryModuleName(baseName string) string {
260 return scope.stubsLibraryModuleName(baseName) + ".from-source"
261}
262
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000263func (scope *apiScope) exportableSourceStubsLibraryModuleName(baseName string) string {
264 return scope.exportableStubsLibraryModuleName(baseName) + ".from-source"
265}
266
Paul Duffinc3091c82020-05-08 14:16:20 +0100267func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100268 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000269}
270
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000271func (scope *apiScope) exportableStubsLibraryModuleName(baseName string) string {
272 return baseName + scope.exportableStubsLibraryModuleNameSuffix()
273}
274
Paul Duffinc8782502020-04-29 20:45:27 +0100275func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100276 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000277}
278
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100279func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100280 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100281}
282
Paul Duffin3375e352020-04-28 10:44:03 +0100283func (scope *apiScope) String() string {
284 return scope.name
285}
286
Paul Duffin958806b2022-05-16 13:10:47 +0000287// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
288// be stored.
289func (scope *apiScope) snapshotRelativeDir() string {
290 return filepath.Join("sdk_library", scope.name)
291}
292
293// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
294// library.
295func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
296 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
297}
298
299// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
300// named library.
301func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
302 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
303}
304
Paul Duffind1b3a922020-01-22 11:57:20 +0000305type apiScopes []*apiScope
306
307func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
308 var list []string
309 for _, scope := range scopes {
310 list = append(list, accessor(scope))
311 }
312 return list
313}
314
Jihoon Kanga96a7b12023-09-20 23:43:32 +0000315// Method that maps the apiScopes properties to the index of each apiScopes elements.
316// apiScopes property to be used as the key can be specified with the input accessor.
317// Only a string property of apiScope can be used as the key of the map.
318func (scopes apiScopes) MapToIndex(accessor func(*apiScope) string) map[string]int {
319 ret := make(map[string]int)
320 for i, scope := range scopes {
321 ret[accessor(scope)] = i
322 }
323 return ret
324}
325
Jiyong Parkc678ad32018-04-10 13:07:10 +0900326var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100327 scopeByName = make(map[string]*apiScope)
328 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000329 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100330 name: "public",
331
332 // Public scope is enabled by default for both legacy and non-legacy modes.
333 legacyEnabledStatus: func(module *SdkLibrary) bool {
334 return true
335 },
336 defaultEnabledStatus: true,
337
338 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
339 return &module.sdkLibraryProperties.Public
340 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000341 sdkVersion: "current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000342 kind: android.SdkPublic,
Paul Duffind1b3a922020-01-22 11:57:20 +0000343 })
344 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100345 name: "system",
346 extends: apiScopePublic,
347 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
348 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
349 return &module.sdkLibraryProperties.System
350 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100351 apiFilePrefix: "system-",
352 moduleSuffix: ".system",
353 sdkVersion: "system_current",
354 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000355 kind: android.SdkSystem,
Paul Duffind1b3a922020-01-22 11:57:20 +0000356 })
357 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100358 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100359 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100360 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
361 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
362 return &module.sdkLibraryProperties.Test
363 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100364 apiFilePrefix: "test-",
365 moduleSuffix: ".test",
366 sdkVersion: "test_current",
367 annotation: "android.annotation.TestApi",
368 unstable: true,
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000369 kind: android.SdkTest,
Paul Duffind1b3a922020-01-22 11:57:20 +0000370 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100371 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100372 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100373 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100374 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100375 //
376 // Enabling this would break existing usages.
377 legacyEnabledStatus: func(module *SdkLibrary) bool {
378 return false
379 },
380 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
381 return &module.sdkLibraryProperties.Module_lib
382 },
383 apiFilePrefix: "module-lib-",
384 moduleSuffix: ".module_lib",
385 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100386 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000387 kind: android.SdkModule,
Paul Duffin8f265b92020-04-28 14:13:56 +0100388 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100389 apiScopeSystemServer = initApiScope(&apiScope{
390 name: "system-server",
391 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100392
393 // The system-server scope can access the module-lib scope.
394 //
395 // A module that provides a system-server API is appended to the standard bootclasspath that is
396 // used by the system server. So, it should be able to access module-lib APIs provided by
397 // libraries on the bootclasspath.
398 canAccess: apiScopeModuleLib,
399
Paul Duffin0c5bae52020-06-02 13:00:08 +0100400 // The system-server scope is disabled by default in legacy mode.
401 //
402 // Enabling this would break existing usages.
403 legacyEnabledStatus: func(module *SdkLibrary) bool {
404 return false
405 },
406 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
407 return &module.sdkLibraryProperties.System_server
408 },
409 apiFilePrefix: "system-server-",
410 moduleSuffix: ".system_server",
411 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100412 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
413 extraArgs: []string{
414 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100415 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100416 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100417 },
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000418 kind: android.SdkSystemServer,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100419 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000420 allApiScopes = apiScopes{
421 apiScopePublic,
422 apiScopeSystem,
423 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100424 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100425 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000426 }
Jihoon Kang0c705a42023-08-02 06:44:57 +0000427 apiLibraryAdditionalProperties = map[string]struct {
428 FullApiSurfaceStubLib string
429 AdditionalApiContribution string
430 }{
431 "legacy.i18n.module.platform.api": {
432 FullApiSurfaceStubLib: "legacy.core.platform.api.stubs",
433 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
434 },
435 "stable.i18n.module.platform.api": {
436 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
437 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
438 },
439 "conscrypt.module.platform.api": {
440 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
441 AdditionalApiContribution: "conscrypt.module.public.api.stubs.source.api.contribution",
442 },
443 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900444)
445
Jiyong Park82484c02018-04-23 21:41:26 +0900446var (
447 javaSdkLibrariesLock sync.Mutex
448)
449
Jiyong Parkc678ad32018-04-10 13:07:10 +0900450// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900451// 1) disallowing linking to the runtime shared lib
452// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900453
454func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000455 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900456
Jiyong Park82484c02018-04-23 21:41:26 +0900457 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
458 javaSdkLibraries := javaSdkLibraries(ctx.Config())
459 sort.Strings(*javaSdkLibraries)
460 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
461 })
Paul Duffindd46f712020-02-10 13:37:10 +0000462
463 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100464 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900465}
466
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000467func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
468 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
469 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
470}
471
Paul Duffin3375e352020-04-28 10:44:03 +0100472// Properties associated with each api scope.
473type ApiScopeProperties struct {
474 // Indicates whether the api surface is generated.
475 //
476 // If this is set for any scope then all scopes must explicitly specify if they
477 // are enabled. This is to prevent new usages from depending on legacy behavior.
478 //
479 // Otherwise, if this is not set for any scope then the default behavior is
480 // scope specific so please refer to the scope specific property documentation.
481 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100482
483 // The sdk_version to use for building the stubs.
484 //
485 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000486 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100487 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000488 // will be none. This is used for java_sdk_library instances that are used
489 // to create stubs that contribute to the core_current sdk version.
490 // 2) Otherwise, it is assumed that this library extends but does not
491 // contribute directly to a specific sdk_version and so this uses the
492 // sdk_version appropriate for the api scope. e.g. public will use
493 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100494 //
495 // This does not affect the sdk_version used for either generating the stubs source
496 // or the API file. They both have to use the same sdk_version as is used for
497 // compiling the implementation library.
498 Sdk_version *string
Mark White9421c4c2023-08-10 00:07:03 +0000499
500 // Extra libs used when compiling stubs for this scope.
501 Libs []string
Paul Duffin3375e352020-04-28 10:44:03 +0100502}
503
Jiyong Parkc678ad32018-04-10 13:07:10 +0900504type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100505 // List of source files that are needed to compile the API, but are not part of runtime library.
506 Api_srcs []string `android:"arch_variant"`
507
Paul Duffin5df79302020-05-16 15:52:12 +0100508 // Visibility for impl library module. If not specified then defaults to the
509 // visibility property.
510 Impl_library_visibility []string
511
Paul Duffin4911a892020-04-29 23:35:13 +0100512 // Visibility for stubs library modules. If not specified then defaults to the
513 // visibility property.
514 Stubs_library_visibility []string
515
516 // Visibility for stubs source modules. If not specified then defaults to the
517 // visibility property.
518 Stubs_source_visibility []string
519
Anton Hansson7f66efa2020-10-08 14:47:23 +0100520 // List of Java libraries that will be in the classpath when building the implementation lib
521 Impl_only_libs []string `android:"arch_variant"`
522
Paul Duffin77590a82022-04-28 14:13:30 +0000523 // List of Java libraries that will included in the implementation lib.
524 Impl_only_static_libs []string `android:"arch_variant"`
525
Sundong Ahnf043cf62018-06-25 16:04:37 +0900526 // List of Java libraries that will be in the classpath when building stubs
527 Stub_only_libs []string `android:"arch_variant"`
528
Anton Hanssondae54cd2021-04-21 16:30:10 +0100529 // List of Java libraries that will included in stub libraries
530 Stub_only_static_libs []string `android:"arch_variant"`
531
Paul Duffin7a586d32019-12-30 17:09:34 +0000532 // list of package names that will be documented and publicized as API.
533 // This allows the API to be restricted to a subset of the source files provided.
534 // If this is unspecified then all the source files will be treated as being part
535 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900536 Api_packages []string
537
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900538 // list of package names that must be hidden from the API
539 Hidden_api_packages []string
540
Paul Duffin749f98f2019-12-30 17:23:46 +0000541 // the relative path to the directory containing the api specification files.
542 // Defaults to "api".
543 Api_dir *string
544
Paul Duffindfa131e2020-05-15 20:37:11 +0100545 // Determines whether a runtime implementation library is built; defaults to false.
546 //
547 // If true then it also prevents the module from being used as a shared module, i.e.
MÃ¥rten Kongstad81d90952022-05-25 16:27:11 +0200548 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000549 Api_only *bool
550
Paul Duffin11512472019-02-11 15:55:17 +0000551 // local files that are used within user customized droiddoc options.
552 Droiddoc_option_files []string
553
Spandan Das93e95992021-07-29 18:26:39 +0000554 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000555 // Available variables for substitution:
556 //
557 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900558 Droiddoc_options []string
559
Paul Duffine22c2ab2020-05-20 19:35:27 +0100560 // is set to true, Metalava will allow framework SDK to contain annotations.
561 Annotations_enabled *bool
562
Sundong Ahn054b19a2018-10-19 13:46:09 +0900563 // a list of top-level directories containing files to merge qualifier annotations
564 // (i.e. those intended to be included in the stubs written) from.
565 Merge_annotations_dirs []string
566
567 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
568 Merge_inclusion_annotations_dirs []string
569
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000570 // If set to true then don't create dist rules.
571 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900572
Paul Duffin31310252020-11-20 21:26:20 +0000573 // The stem for the artifacts that are copied to the dist, if not specified
574 // then defaults to the base module name.
575 //
576 // For each scope the following artifacts are copied to the apistubs/<scope>
577 // directory in the dist.
578 // * stubs impl jar -> <dist-stem>.jar
579 // * API specification file -> api/<dist-stem>.txt
580 // * Removed API specification file -> api/<dist-stem>-removed.txt
581 //
582 // Also used to construct the name of the filegroup (created by prebuilt_apis)
583 // that references the latest released API and remove API specification files.
584 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
585 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800586 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000587 Dist_stem *string
588
Colin Cross986b69a2021-06-01 13:13:40 -0700589 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700590 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700591 // in the public Android SDK.
592 Dist_group *string
593
Anton Hanssondff2c782020-12-21 17:10:01 +0000594 // A compatibility mode that allows historical API-tracking files to not exist.
595 // Do not use.
596 Unsafe_ignore_missing_latest_api bool
597
Paul Duffin3375e352020-04-28 10:44:03 +0100598 // indicates whether system and test apis should be generated.
599 Generate_system_and_test_apis bool `blueprint:"mutated"`
600
601 // The properties specific to the public api scope
602 //
603 // Unless explicitly specified by using public.enabled the public api scope is
604 // enabled by default in both legacy and non-legacy mode.
605 Public ApiScopeProperties
606
607 // The properties specific to the system api scope
608 //
609 // In legacy mode the system api scope is enabled by default when sdk_version
610 // is set to something other than "none".
611 //
612 // In non-legacy mode the system api scope is disabled by default.
613 System ApiScopeProperties
614
615 // The properties specific to the test api scope
616 //
617 // In legacy mode the test api scope is enabled by default when sdk_version
618 // is set to something other than "none".
619 //
620 // In non-legacy mode the test api scope is disabled by default.
621 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000622
Paul Duffin0c5bae52020-06-02 13:00:08 +0100623 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100624 //
Zi Wangb2179e32023-01-31 15:53:30 -0800625 // Unless explicitly specified by using module_lib.enabled the module_lib api
626 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100627 Module_lib ApiScopeProperties
628
Paul Duffin0c5bae52020-06-02 13:00:08 +0100629 // The properties specific to the system-server api scope
630 //
Zi Wangb2179e32023-01-31 15:53:30 -0800631 // Unless explicitly specified by using system_server.enabled the
632 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100633 System_server ApiScopeProperties
634
Jiyong Park932cdfe2020-05-28 00:19:53 +0900635 // Determines if the stubs are preferred over the implementation library
636 // for linking, even when the client doesn't specify sdk_version. When this
637 // is set to true, such clients are provided with the widest API surface that
638 // this lib provides. Note however that this option doesn't affect the clients
639 // that are in the same APEX as this library. In that case, the clients are
640 // always linked with the implementation library. Default is false.
641 Default_to_stubs *bool
642
Paul Duffin160fe412020-05-10 19:32:20 +0100643 // Properties related to api linting.
644 Api_lint struct {
645 // Enable api linting.
646 Enabled *bool
Anton Hanssonfd1c0d22023-11-02 15:18:09 +0000647
648 // If API lint is enabled, this flag controls whether a set of legitimate lint errors
649 // are turned off. The default is true.
650 Legacy_errors_allowed *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100651 }
652
Jihoon Kang80456fd2023-11-15 19:22:14 +0000653 // Determines if the module contributes to any api surfaces.
654 // This property should be set to true only if the module is listed under
655 // frameworks-base-api.bootclasspath in frameworks/base/api/Android.bp.
656 // Otherwise, this property should be set to false.
657 // Defaults to false.
658 Contribute_to_android_api *bool
659
Jihoon Kang6592e872023-12-19 01:13:16 +0000660 // a list of aconfig_declarations module names that the stubs generated in this module
661 // depend on.
662 Aconfig_declarations []string
663
Jiyong Parkc678ad32018-04-10 13:07:10 +0900664 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100665 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900666}
667
Paul Duffin0f8faff2020-05-20 16:18:00 +0100668// Paths to outputs from java_sdk_library and java_sdk_library_import.
669//
670// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
671// OptionalPaths are always set by java_sdk_library but may not be set by
672// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000673type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100674 // The path (represented as Paths for convenience when returning) to the stubs header jar.
675 //
676 // That is the jar that is created by turbine.
677 stubsHeaderPath android.Paths
678
679 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
680 //
681 // This is not the implementation jar, it still only contains stubs.
682 stubsImplPath android.Paths
683
Paul Duffin1267d872021-04-16 17:21:36 +0100684 // The dex jar for the stubs.
685 //
686 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100687 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100688
Jihoon Kangbd093452023-12-26 19:08:01 +0000689 // The exportable dex jar for the stubs.
690 // This is not the implementation jar, it still only contains stubs.
691 // Includes unflagged apis and flagged apis enabled by release configurations.
692 exportableStubsDexJarPath OptionalDexJarPath
693
Paul Duffin0f8faff2020-05-20 16:18:00 +0100694 // The API specification file, e.g. system_current.txt.
695 currentApiFilePath android.OptionalPath
696
697 // The specification of API elements removed since the last release.
698 removedApiFilePath android.OptionalPath
699
700 // The stubs source jar.
701 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100702
703 // Extracted annotations.
704 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000705
706 // The path to the latest API file.
707 latestApiPath android.OptionalPath
708
709 // The path to the latest removed API file.
710 latestRemovedApiPath android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000711}
712
Colin Crossdcf71b22021-02-01 13:59:03 -0800713func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Colin Cross313aa542023-12-13 13:47:44 -0800714 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800715 paths.stubsHeaderPath = lib.HeaderJars
716 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100717
718 libDep := dep.(UsesLibraryDependency)
Spandan Das59a4a2b2024-01-09 21:35:56 +0000719 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
Jihoon Kangbd093452023-12-26 19:08:01 +0000720 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
721 return nil
722 } else {
723 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
724 }
725}
726
727func (paths *scopePaths) extractEverythingStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
728 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
729 paths.stubsHeaderPath = lib.HeaderJars
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000730 if !ctx.Config().ReleaseHiddenApiExportableStubs() {
731 paths.stubsImplPath = lib.ImplementationJars
732 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000733
734 libDep := dep.(UsesLibraryDependency)
735 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
736 return nil
737 } else {
738 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
739 }
740}
741
742func (paths *scopePaths) extractExportableStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000743 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
744 if ctx.Config().ReleaseHiddenApiExportableStubs() {
745 paths.stubsImplPath = lib.ImplementationJars
746 }
747
Jihoon Kangbd093452023-12-26 19:08:01 +0000748 libDep := dep.(UsesLibraryDependency)
749 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
Paul Duffinc8782502020-04-29 20:45:27 +0100750 return nil
751 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800752 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100753 }
754}
755
Jihoon Kangee113282024-01-23 00:16:41 +0000756func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider) error) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100757 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000758 err := action(apiStubsProvider)
759 if err != nil {
760 return err
761 }
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000762 return nil
763 } else {
764 return fmt.Errorf("expected module that implements ExportableApiStubsSrcProvider, e.g. droidstubs")
765 }
766}
767
Jihoon Kangee113282024-01-23 00:16:41 +0000768func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider) error) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100769 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000770 err := action(apiStubsProvider)
771 if err != nil {
772 return err
773 }
Paul Duffin0f8faff2020-05-20 16:18:00 +0100774 return nil
775 } else {
776 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
777 }
778}
779
Jihoon Kangee113282024-01-23 00:16:41 +0000780func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider, stubsType StubsType) error {
781 var annotationsZip, currentApiFilePath, removedApiFilePath android.Path
782 annotationsZip, annotationsZipErr := provider.AnnotationsZip(stubsType)
783 currentApiFilePath, currentApiFilePathErr := provider.ApiFilePath(stubsType)
784 removedApiFilePath, removedApiFilePathErr := provider.RemovedApiFilePath(stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100785
Jihoon Kangee113282024-01-23 00:16:41 +0000786 combinedError := errors.Join(annotationsZipErr, currentApiFilePathErr, removedApiFilePathErr)
787
788 if combinedError == nil {
789 paths.annotationsZip = android.OptionalPathForPath(annotationsZip)
790 paths.currentApiFilePath = android.OptionalPathForPath(currentApiFilePath)
791 paths.removedApiFilePath = android.OptionalPathForPath(removedApiFilePath)
792 }
793 return combinedError
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000794}
795
Colin Crossdcf71b22021-02-01 13:59:03 -0800796func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangee113282024-01-23 00:16:41 +0000797 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
798 return paths.extractApiInfoFromApiStubsProvider(provider, Everything)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100799 })
800}
801
Jihoon Kangee113282024-01-23 00:16:41 +0000802func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider, stubsType StubsType) error {
803 stubsSrcJar, err := provider.StubsSrcJar(stubsType)
804 if err == nil {
805 paths.stubsSrcJar = android.OptionalPathForPath(stubsSrcJar)
806 }
807 return err
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000808}
809
Colin Crossdcf71b22021-02-01 13:59:03 -0800810func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangee113282024-01-23 00:16:41 +0000811 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) error {
812 return paths.extractStubsSourceInfoFromApiStubsProviders(provider, Everything)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100813 })
814}
815
Colin Crossdcf71b22021-02-01 13:59:03 -0800816func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000817 if ctx.Config().ReleaseHiddenApiExportableStubs() {
Jihoon Kangee113282024-01-23 00:16:41 +0000818 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
819 extractApiInfoErr := paths.extractApiInfoFromApiStubsProvider(provider, Exportable)
820 extractStubsSourceInfoErr := paths.extractStubsSourceInfoFromApiStubsProviders(provider, Exportable)
821 return errors.Join(extractApiInfoErr, extractStubsSourceInfoErr)
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000822 })
823 }
Jihoon Kangee113282024-01-23 00:16:41 +0000824 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
825 extractApiInfoErr := paths.extractApiInfoFromApiStubsProvider(provider, Everything)
826 extractStubsSourceInfoErr := paths.extractStubsSourceInfoFromApiStubsProviders(provider, Everything)
827 return errors.Join(extractApiInfoErr, extractStubsSourceInfoErr)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100828 })
829}
830
Paul Duffin958806b2022-05-16 13:10:47 +0000831func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
832 var paths android.Paths
833 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
834 paths = sourceFileProducer.Srcs()
835 } else {
836 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
837 }
838 if len(paths) != 1 {
839 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
840 }
841 return android.OptionalPathForPath(paths[0]), nil
842}
843
844func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
845 outputPath, err := extractSingleOptionalOutputPath(dep)
846 paths.latestApiPath = outputPath
847 return err
848}
849
850func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
851 outputPath, err := extractSingleOptionalOutputPath(dep)
852 paths.latestRemovedApiPath = outputPath
853 return err
854}
855
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100856type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100857 // The naming scheme to use for the components that this module creates.
858 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100859 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100860 //
861 // This is a temporary mechanism to simplify conversion from separate modules for each
862 // component that follow a different naming pattern to the default one.
863 //
864 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100865 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100866
867 // Specifies whether this module can be used as an Android shared library; defaults
868 // to true.
869 //
870 // An Android shared library is one that can be referenced in a <uses-library> element
871 // in an AndroidManifest.xml.
872 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100873
874 // Files containing information about supported java doc tags.
875 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000876
877 // Signals that this shared library is part of the bootclasspath starting
878 // on the version indicated in this attribute.
879 //
880 // This will make platforms at this level and above to ignore
881 // <uses-library> tags with this library name because the library is already
882 // available
883 On_bootclasspath_since *string
884
885 // Signals that this shared library was part of the bootclasspath before
886 // (but not including) the version indicated in this attribute.
887 //
888 // The system will automatically add a <uses-library> tag with this library to
889 // apps that target any SDK less than the version indicated in this attribute.
890 On_bootclasspath_before *string
891
892 // Indicates that PackageManager should ignore this shared library if the
893 // platform is below the version indicated in this attribute.
894 //
895 // This means that the device won't recognise this library as installed.
896 Min_device_sdk *string
897
898 // Indicates that PackageManager should ignore this shared library if the
899 // platform is above the version indicated in this attribute.
900 //
901 // This means that the device won't recognise this library as installed.
902 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100903}
904
Paul Duffin71b33cc2021-06-23 11:39:47 +0100905// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
906// embeds the commonToSdkLibraryAndImport struct.
907type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000908 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100909
Spandan Das23956d12024-01-19 00:22:22 +0000910 // Returns the name of the root java_sdk_library that creates the child stub libraries
911 // This is the `name` as it appears in Android.bp, and not the name in Soong's build graph
912 // (with the prebuilt_ prefix)
913 //
914 // e.g. in the following java_sdk_library_import
915 // java_sdk_library_import {
916 // name: "framework-foo.v1",
917 // source_module_name: "framework-foo",
918 // }
919 // the values returned by
920 // 1. Name(): prebuilt_framework-foo.v1 # unique
921 // 2. BaseModuleName(): framework-foo # the source
922 // 3. RootLibraryName: framework-foo.v1 # the undecordated `name` from Android.bp
923 RootLibraryName() string
924}
925
926func (m *SdkLibrary) RootLibraryName() string {
927 return m.BaseModuleName()
928}
929
930func (m *SdkLibraryImport) RootLibraryName() string {
931 // m.BaseModuleName refers to the source of the import
932 // use moduleBase.Name to get the name of the module as it appears in the .bp file
933 return m.ModuleBase.Name()
Paul Duffin71b33cc2021-06-23 11:39:47 +0100934}
935
Paul Duffin56d44902020-01-31 13:36:25 +0000936// Common code between sdk library and sdk library import
937type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100938 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100939
Paul Duffin56d44902020-01-31 13:36:25 +0000940 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100941
942 namingScheme sdkLibraryComponentNamingScheme
943
Paul Duffindfa131e2020-05-15 20:37:11 +0100944 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100945
Paul Duffina2ae7e02020-09-11 11:55:00 +0100946 // Paths to commonSdkLibraryProperties.Doctag_files
947 doctagPaths android.Paths
948
Paul Duffin859fe962020-05-15 10:20:31 +0100949 // Functionality related to this being used as a component of a java_sdk_library.
950 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000951}
952
Paul Duffin71b33cc2021-06-23 11:39:47 +0100953func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
954 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100955
Paul Duffin71b33cc2021-06-23 11:39:47 +0100956 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100957
958 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100959 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100960}
961
962func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100963 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100964 switch schemeProperty {
965 case "default":
966 c.namingScheme = &defaultNamingScheme{}
967 default:
968 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
969 return false
970 }
971
Spandan Das23956d12024-01-19 00:22:22 +0000972 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +0100973 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
974
Paul Duffindfa131e2020-05-15 20:37:11 +0100975 // Only track this sdk library if this can be used as a shared library.
976 if c.sharedLibrary() {
977 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100978 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100979 }
Paul Duffin859fe962020-05-15 10:20:31 +0100980
Paul Duffin1b1e8062020-05-08 13:44:43 +0100981 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100982}
983
Paul Duffinea8f8082021-06-24 13:25:57 +0100984// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
985// method.
986func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
987 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
988 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
989 // the APEX and so it needs a unique variation per APEX.
990 return c.sharedLibrary()
991}
992
Paul Duffina2ae7e02020-09-11 11:55:00 +0100993func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
994 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
995}
996
Paul Duffineedc5d52020-06-12 17:46:39 +0100997// Module name of the runtime implementation library
998func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +0000999 return c.module.RootLibraryName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +01001000}
1001
1002// Module name of the XML file for the lib
1003func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +00001004 return c.module.RootLibraryName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +01001005}
1006
Paul Duffinc3091c82020-05-08 14:16:20 +01001007// Name of the java_library module that compiles the stubs source.
1008func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001009 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001010 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001011}
1012
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001013// Name of the java_library module that compiles the exportable stubs source.
1014func (c *commonToSdkLibraryAndImport) exportableStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001015 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001016 return c.namingScheme.exportableStubsLibraryModuleName(apiScope, baseName)
1017}
1018
Paul Duffinc3091c82020-05-08 14:16:20 +01001019// Name of the droidstubs module that generates the stubs source and may also
1020// generate/check the API.
1021func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001022 baseName := c.module.RootLibraryName()
Paul Duffin21787622022-11-25 12:48:20 +00001023 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +01001024}
1025
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001026// Name of the java_api_library module that generates the from-text stubs source
1027// and compiles to a jar file.
1028func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001029 baseName := c.module.RootLibraryName()
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001030 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
1031}
1032
Jihoon Kang1147b312023-06-08 23:25:57 +00001033// Name of the java_library module that compiles the stubs
1034// generated from source Java files.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001035func (c *commonToSdkLibraryAndImport) sourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001036 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001037 return c.namingScheme.sourceStubsLibraryModuleName(apiScope, baseName)
1038}
1039
1040// Name of the java_library module that compiles the exportable stubs
1041// generated from source Java files.
1042func (c *commonToSdkLibraryAndImport) exportableSourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001043 baseName := c.module.RootLibraryName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001044 return c.namingScheme.exportableSourceStubsLibraryModuleName(apiScope, baseName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001045}
1046
Paul Duffin46dc45a2020-05-14 15:39:10 +01001047// The component names for different outputs of the java_sdk_library.
1048//
1049// They are similar to the names used for the child modules it creates
1050const (
1051 stubsSourceComponentName = "stubs.source"
1052
1053 apiTxtComponentName = "api.txt"
1054
1055 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +01001056
1057 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +01001058)
1059
1060// A regular expression to match tags that reference a specific stubs component.
1061//
1062// It will only match if given a valid scope and a valid component. It is verfy strict
1063// to ensure it does not accidentally match a similar looking tag that should be processed
1064// by the embedded Library.
1065var tagSplitter = func() *regexp.Regexp {
1066 // Given a list of literal string items returns a regular expression that will
1067 // match any one of the items.
1068 choice := func(items ...string) string {
1069 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
1070 }
1071
1072 // Regular expression to match one of the scopes.
1073 scopesRegexp := choice(allScopeNames...)
1074
1075 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +01001076 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001077
1078 // Regular expression to match any combination of one scope and one component.
1079 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
1080}()
1081
1082// For OutputFileProducer interface
1083//
Anton Hanssond78eb762021-09-21 15:25:12 +01001084// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001085func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
1086 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
1087 scopeName := groups[1]
1088 component := groups[2]
1089
1090 if scope, ok := scopeByName[scopeName]; ok {
1091 paths := c.findScopePaths(scope)
1092 if paths == nil {
Spandan Das23956d12024-01-19 00:22:22 +00001093 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.RootLibraryName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001094 }
1095
1096 switch component {
1097 case stubsSourceComponentName:
1098 if paths.stubsSrcJar.Valid() {
1099 return android.Paths{paths.stubsSrcJar.Path()}, nil
1100 }
1101
1102 case apiTxtComponentName:
1103 if paths.currentApiFilePath.Valid() {
1104 return android.Paths{paths.currentApiFilePath.Path()}, nil
1105 }
1106
1107 case removedApiTxtComponentName:
1108 if paths.removedApiFilePath.Valid() {
1109 return android.Paths{paths.removedApiFilePath.Path()}, nil
1110 }
Anton Hanssond78eb762021-09-21 15:25:12 +01001111
1112 case annotationsComponentName:
1113 if paths.annotationsZip.Valid() {
1114 return android.Paths{paths.annotationsZip.Path()}, nil
1115 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001116 }
1117
1118 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
1119 } else {
1120 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
1121 }
1122
1123 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001124 switch tag {
1125 case ".doctags":
1126 if c.doctagPaths != nil {
1127 return c.doctagPaths, nil
1128 } else {
Spandan Das23956d12024-01-19 00:22:22 +00001129 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.RootLibraryName())
Paul Duffina2ae7e02020-09-11 11:55:00 +01001130 }
1131 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001132 return nil, nil
1133 }
1134}
1135
Paul Duffin803a9562020-05-20 11:52:25 +01001136func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001137 if c.scopePaths == nil {
1138 c.scopePaths = make(map[*apiScope]*scopePaths)
1139 }
1140 paths := c.scopePaths[scope]
1141 if paths == nil {
1142 paths = &scopePaths{}
1143 c.scopePaths[scope] = paths
1144 }
1145
1146 return paths
1147}
1148
Paul Duffin803a9562020-05-20 11:52:25 +01001149func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1150 if c.scopePaths == nil {
1151 return nil
1152 }
1153
1154 return c.scopePaths[scope]
1155}
1156
1157// If this does not support the requested api scope then find the closest available
1158// scope it does support. Returns nil if no such scope is available.
1159func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001160 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001161 if paths := c.findScopePaths(s); paths != nil {
1162 return paths
1163 }
1164 }
1165
1166 // This should never happen outside tests as public should be the base scope for every
1167 // scope and is enabled by default.
1168 return nil
1169}
1170
Jiyong Parkf1691d22021-03-29 20:11:58 +09001171func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001172
1173 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001174 if !sdkVersion.ApiLevel.IsPreview() {
Spandan Das23956d12024-01-19 00:22:22 +00001175 return PrebuiltJars(ctx, c.module.RootLibraryName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001176 }
1177
Paul Duffin1267d872021-04-16 17:21:36 +01001178 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1179 if paths == nil {
1180 return nil
1181 }
1182
1183 return paths.stubsHeaderPath
1184}
1185
1186// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1187//
1188// If the module does not support the specific kind then it will return the *scopePaths for the
1189// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1190// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1191func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001192 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001193
Paul Duffin803a9562020-05-20 11:52:25 +01001194 paths := c.findClosestScopePath(apiScope)
1195 if paths == nil {
1196 var scopes []string
1197 for _, s := range allApiScopes {
1198 if c.findScopePaths(s) != nil {
1199 scopes = append(scopes, s.name)
1200 }
1201 }
Spandan Das23956d12024-01-19 00:22:22 +00001202 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.module.RootLibraryName(), scopes)
Paul Duffin803a9562020-05-20 11:52:25 +01001203 return nil
1204 }
1205
Paul Duffin1267d872021-04-16 17:21:36 +01001206 return paths
1207}
1208
Paul Duffin32cf58a2021-05-18 16:32:50 +01001209// sdkKindToApiScope maps from android.SdkKind to apiScope.
1210func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1211 var apiScope *apiScope
1212 switch kind {
1213 case android.SdkSystem:
1214 apiScope = apiScopeSystem
1215 case android.SdkModule:
1216 apiScope = apiScopeModuleLib
1217 case android.SdkTest:
1218 apiScope = apiScopeTest
1219 case android.SdkSystemServer:
1220 apiScope = apiScopeSystemServer
1221 default:
1222 apiScope = apiScopePublic
1223 }
1224 return apiScope
1225}
1226
Paul Duffin1267d872021-04-16 17:21:36 +01001227// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001228func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001229 paths := c.selectScopePaths(ctx, kind)
1230 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001231 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001232 }
1233
1234 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001235}
1236
Paul Duffin32cf58a2021-05-18 16:32:50 +01001237// to satisfy SdkLibraryDependency interface
Jihoon Kangbd093452023-12-26 19:08:01 +00001238func (c *commonToSdkLibraryAndImport) SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
1239 paths := c.selectScopePaths(ctx, kind)
1240 if paths == nil {
1241 return makeUnsetDexJarPath()
1242 }
1243
1244 return paths.exportableStubsDexJarPath
1245}
1246
1247// to satisfy SdkLibraryDependency interface
Paul Duffin32cf58a2021-05-18 16:32:50 +01001248func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1249 apiScope := sdkKindToApiScope(kind)
1250 paths := c.findScopePaths(apiScope)
1251 if paths == nil {
1252 return android.OptionalPath{}
1253 }
1254
1255 return paths.removedApiFilePath
1256}
1257
Paul Duffin859fe962020-05-15 10:20:31 +01001258func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1259 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001260 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001261 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001262 }{}
1263
Spandan Das23956d12024-01-19 00:22:22 +00001264 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +01001265 componentProps.SdkLibraryName = namePtr
1266
Paul Duffindfa131e2020-05-15 20:37:11 +01001267 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001268 // Mark the stubs library as being components of this java_sdk_library so that
1269 // any app that includes code which depends (directly or indirectly) on the stubs
1270 // library will have the appropriate <uses-library> invocation inserted into its
1271 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001272 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001273 }
1274
1275 return componentProps
1276}
1277
Paul Duffindfa131e2020-05-15 20:37:11 +01001278func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1279 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1280}
1281
Paul Duffinf4600f62021-05-13 22:34:45 +01001282// Check if the stub libraries should be compiled for dex
1283func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1284 // Always compile the dex file files for the stub libraries if they will be used on the
1285 // bootclasspath.
1286 return !c.sharedLibrary()
1287}
1288
Paul Duffin859fe962020-05-15 10:20:31 +01001289// Properties related to the use of a module as an component of a java_sdk_library.
1290type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001291 // The name of the java_sdk_library/_import module.
1292 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001293
1294 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1295 // in the AndroidManifest.xml of any Android app that includes code that references
1296 // this module. If not set then no java_sdk_library/_import is tracked.
1297 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1298}
1299
1300// Structure to be embedded in a module struct that needs to support the
1301// SdkLibraryComponentDependency interface.
1302type EmbeddableSdkLibraryComponent struct {
1303 sdkLibraryComponentProperties SdkLibraryComponentProperties
1304}
1305
Paul Duffin71b33cc2021-06-23 11:39:47 +01001306func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1307 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001308}
1309
1310// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001311func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1312 return e.sdkLibraryComponentProperties.SdkLibraryName
1313}
1314
1315// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001316func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001317 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1318 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1319 // run-time library and the corresponding module that provides the implementation. This name is
1320 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1321 // in dexpreopt).
1322 //
1323 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1324 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001325 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1326}
1327
Paul Duffin859fe962020-05-15 10:20:31 +01001328// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1329// (including the java_sdk_library) itself.
1330type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001331 UsesLibraryDependency
1332
Paul Duffin3f0290e2021-06-30 18:25:36 +01001333 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1334 SdkLibraryName() *string
1335
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001336 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1337 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001338}
1339
1340// Make sure that all the module types that are components of java_sdk_library/_import
1341// and which can be referenced (directly or indirectly) from an android app implement
1342// the SdkLibraryComponentDependency interface.
1343var _ SdkLibraryComponentDependency = (*Library)(nil)
1344var _ SdkLibraryComponentDependency = (*Import)(nil)
1345var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001346var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001347
Paul Duffin32cf58a2021-05-18 16:32:50 +01001348// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001349type SdkLibraryDependency interface {
1350 SdkLibraryComponentDependency
1351
1352 // Get the header jars appropriate for the supplied sdk_version.
1353 //
1354 // These are turbine generated jars so they only change if the externals of the
1355 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001356 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001357
1358 // Get the implementation jars appropriate for the supplied sdk version.
1359 //
1360 // These are either the implementation jar for the whole sdk library or the implementation
1361 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1362 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001363 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001364
Jihoon Kangbd093452023-12-26 19:08:01 +00001365 // SdkApiStubDexJar returns the dex jar for the stubs for the prebuilt
1366 // java_sdk_library_import module. It is needed by the hiddenapi processing tool which
1367 // processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001368 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001369
Jihoon Kangbd093452023-12-26 19:08:01 +00001370 // SdkApiExportableStubDexJar returns the exportable dex jar for the stubs for
1371 // java_sdk_library module. It is needed by the hiddenapi processing tool which processes
1372 // dex files.
1373 SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
1374
Paul Duffin32cf58a2021-05-18 16:32:50 +01001375 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1376 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1377
Paul Duffinf4600f62021-05-13 22:34:45 +01001378 // sharedLibrary returns true if this can be used as a shared library.
1379 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001380}
1381
Inseob Kimc0907f12019-02-08 21:00:45 +09001382type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001383 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001384
Sundong Ahn054b19a2018-10-19 13:46:09 +09001385 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001386
Paul Duffin3375e352020-04-28 10:44:03 +01001387 // Map from api scope to the scope specific property structure.
1388 scopeToProperties map[*apiScope]*ApiScopeProperties
1389
Paul Duffin56d44902020-01-31 13:36:25 +00001390 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001391}
1392
Inseob Kimc0907f12019-02-08 21:00:45 +09001393var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001394
Paul Duffin3375e352020-04-28 10:44:03 +01001395func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1396 return module.sdkLibraryProperties.Generate_system_and_test_apis
1397}
1398
1399func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1400 // Check to see if any scopes have been explicitly enabled. If any have then all
1401 // must be.
1402 anyScopesExplicitlyEnabled := false
1403 for _, scope := range allApiScopes {
1404 scopeProperties := module.scopeToProperties[scope]
1405 if scopeProperties.Enabled != nil {
1406 anyScopesExplicitlyEnabled = true
1407 break
1408 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001409 }
Paul Duffin3375e352020-04-28 10:44:03 +01001410
1411 var generatedScopes apiScopes
1412 enabledScopes := make(map[*apiScope]struct{})
1413 for _, scope := range allApiScopes {
1414 scopeProperties := module.scopeToProperties[scope]
1415 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1416 // This is to ensure that any new usages of this module type do not rely on legacy
1417 // behaviour.
1418 defaultEnabledStatus := false
1419 if anyScopesExplicitlyEnabled {
1420 defaultEnabledStatus = scope.defaultEnabledStatus
1421 } else {
1422 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1423 }
1424 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1425 if enabled {
1426 enabledScopes[scope] = struct{}{}
1427 generatedScopes = append(generatedScopes, scope)
1428 }
1429 }
1430
1431 // Now check to make sure that any scope that is extended by an enabled scope is also
1432 // enabled.
1433 for _, scope := range allApiScopes {
1434 if _, ok := enabledScopes[scope]; ok {
1435 extends := scope.extends
1436 if extends != nil {
1437 if _, ok := enabledScopes[extends]; !ok {
1438 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1439 }
1440 }
1441 }
1442 }
1443
1444 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001445}
1446
satayev758968a2021-12-06 11:42:40 +00001447var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1448
satayev8f088b02021-12-06 11:40:46 +00001449func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001450 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001451 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1452 isExternal := !module.depIsInSameApex(ctx, child)
1453 if am, ok := child.(android.ApexModule); ok {
1454 if !do(ctx, parent, am, isExternal) {
1455 return false
1456 }
1457 }
1458 return !isExternal
1459 })
1460 })
1461}
1462
Paul Duffineedc5d52020-06-12 17:46:39 +01001463type sdkLibraryComponentTag struct {
1464 blueprint.BaseDependencyTag
1465 name string
1466}
1467
1468// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1469func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1470
1471var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001472
Jiyong Parke3833882020-02-17 17:28:10 +09001473func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001474 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001475 return dt == xmlPermissionsFileTag
1476 }
1477 return false
1478}
1479
Paul Duffineedc5d52020-06-12 17:46:39 +01001480var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001481
Paul Duffin44f1d842020-06-26 20:17:02 +01001482// Add the dependencies on the child modules in the component deps mutator.
1483func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001484 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001485 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001486 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kangbd093452023-12-26 19:08:01 +00001487 ctx.AddVariationDependencies(nil, apiScope.everythingStubsTag, stubModuleName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001488
Jihoon Kangbd093452023-12-26 19:08:01 +00001489 exportableStubModuleName := module.exportableStubsLibraryModuleName(apiScope)
1490 ctx.AddVariationDependencies(nil, apiScope.exportableStubsTag, exportableStubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001491
Paul Duffin15f34ef2020-07-20 18:04:44 +01001492 // Add a dependency on the stubs source in order to access both stubs source and api information.
1493 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001494
1495 if module.compareAgainstLatestApi(apiScope) {
1496 // Add dependencies on the latest finalized version of the API .txt file.
1497 latestApiModuleName := module.latestApiModuleName(apiScope)
1498 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1499
1500 // Add dependencies on the latest finalized version of the remove API .txt file.
1501 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1502 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1503 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001504 }
1505
Paul Duffindfa131e2020-05-15 20:37:11 +01001506 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001507 // Add dependency to the rule for generating the implementation library.
1508 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1509
Paul Duffindfa131e2020-05-15 20:37:11 +01001510 if module.sharedLibrary() {
1511 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001512 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001513 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001514 }
1515}
Paul Duffine74ac732020-02-06 13:51:46 +00001516
Paul Duffin44f1d842020-06-26 20:17:02 +01001517// Add other dependencies as normal.
1518func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001519 var missingApiModules []string
1520 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1521 if apiScope.unstable {
1522 continue
1523 }
Paul Duffin958806b2022-05-16 13:10:47 +00001524 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001525 missingApiModules = append(missingApiModules, m)
1526 }
Paul Duffin958806b2022-05-16 13:10:47 +00001527 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001528 missingApiModules = append(missingApiModules, m)
1529 }
Paul Duffin958806b2022-05-16 13:10:47 +00001530 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001531 missingApiModules = append(missingApiModules, m)
1532 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001533 }
1534 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1535 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1536 m += "You need to do one of the following:\n"
1537 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1538 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1539 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1540 m += "\n"
1541 m += "The following filegroup modules are missing:\n "
1542 m += strings.Join(missingApiModules, "\n ") + "\n"
1543 m += "Please see the documentation of the prebuilt_apis module type (and a usage example in prebuilts/sdk) for a convenient way to generate these."
1544 ctx.ModuleErrorf(m)
1545 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001546 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001547 // Only add the deps for the library if it is actually going to be built.
1548 module.Library.deps(ctx)
1549 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001550}
1551
Paul Duffin46dc45a2020-05-14 15:39:10 +01001552func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1553 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001554 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001555 return paths, err
1556 }
Colin Cross4acaea92021-12-10 23:05:02 +00001557 if module.requiresRuntimeImplementationLibrary() {
1558 return module.Library.OutputFiles(tag)
1559 }
1560 if tag == "" {
1561 return nil, nil
1562 }
1563 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001564}
1565
Inseob Kimc0907f12019-02-08 21:00:45 +09001566func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001567 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1568 module.CheckMinSdkVersion(ctx)
1569 }
1570
Paul Duffina2ae7e02020-09-11 11:55:00 +01001571 module.generateCommonBuildActions(ctx)
1572
Paul Duffindfa131e2020-05-15 20:37:11 +01001573 // Only build an implementation library if required.
1574 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001575 module.Library.GenerateAndroidBuildActions(ctx)
1576 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001577
Paul Duffinb97b1572021-04-29 21:50:40 +01001578 // Collate the components exported by this module. All scope specific modules are exported but
1579 // the impl and xml component modules are not.
1580 exportedComponents := map[string]struct{}{}
1581
Sundong Ahn57368eb2018-07-06 11:20:23 +09001582 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001583 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001584 // the recorded paths will be returned depending on the link type of the caller.
1585 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001586 tag := ctx.OtherModuleDependencyTag(to)
1587
Paul Duffinc8782502020-04-29 20:45:27 +01001588 // Extract information from any of the scope specific dependencies.
1589 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1590 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001591 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001592
1593 // Extract information from the dependency. The exact information extracted
1594 // is determined by the nature of the dependency which is determined by the tag.
1595 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001596
1597 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001598 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001599 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001600
1601 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001602 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001603 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001604
1605 // Provide additional information for inclusion in an sdk's generated .info file.
1606 additionalSdkInfo := map[string]interface{}{}
1607 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001608 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001609 scopes := map[string]interface{}{}
1610 additionalSdkInfo["scopes"] = scopes
1611 for scope, scopePaths := range module.scopePaths {
1612 scopeInfo := map[string]interface{}{}
1613 scopes[scope.name] = scopeInfo
1614 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1615 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1616 if p := scopePaths.latestApiPath; p.Valid() {
1617 scopeInfo["latest_api"] = p.Path().String()
1618 }
1619 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1620 scopeInfo["latest_removed_api"] = p.Path().String()
1621 }
1622 }
Colin Cross40213022023-12-13 15:19:49 -08001623 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001624}
1625
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001626func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001627 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001628 return nil
1629 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001630 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001631 if module.sharedLibrary() {
1632 entries := &entriesList[0]
1633 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1634 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001635 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001636}
1637
Anton Hansson5fd5d242020-03-27 19:43:19 +00001638// The dist path of the stub artifacts
1639func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001640 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001641}
1642
Paul Duffin12ceb462019-12-24 20:31:31 +00001643// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001644func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001645 scopeProperties := module.scopeToProperties[apiScope]
1646 if scopeProperties.Sdk_version != nil {
1647 return proptools.String(scopeProperties.Sdk_version)
1648 }
1649
Jiyong Parkf1691d22021-03-29 20:11:58 +09001650 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001651 if sdkDep.hasStandardLibs() {
1652 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001653 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001654 } else {
1655 // Otherwise, use no system module.
1656 return "none"
1657 }
1658}
1659
Paul Duffin31310252020-11-20 21:26:20 +00001660func (module *SdkLibrary) distStem() string {
1661 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1662}
1663
Colin Cross986b69a2021-06-01 13:13:40 -07001664// distGroup returns the subdirectory of the dist path of the stub artifacts.
1665func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001666 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001667}
1668
Paul Duffin958806b2022-05-16 13:10:47 +00001669func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1670 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1671}
1672
Paul Duffind1b3a922020-01-22 11:57:20 +00001673func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001674 return ":" + module.latestApiModuleName(apiScope)
1675}
1676
1677func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
1678 return latestPrebuiltApiModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001679}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001680
Paul Duffind1b3a922020-01-22 11:57:20 +00001681func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001682 return ":" + module.latestRemovedApiModuleName(apiScope)
1683}
1684
1685func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
1686 return latestPrebuiltApiModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001687}
1688
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001689func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001690 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1691}
1692
1693func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1694 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001695}
1696
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001697func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1698 _, exists := c.GetApiLibraries()[module.Name()]
1699 return exists
1700}
1701
Jihoon Kang0c705a42023-08-02 06:44:57 +00001702// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1703// api surface that the module contribute to. For example, the public droidstubs and java_library
1704// do not contribute to the public api surface, but contributes to the core platform api surface.
1705// This method returns the full api surface stub lib that
1706// the generated java_api_library should depend on.
1707func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1708 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1709 return val.FullApiSurfaceStubLib
1710 }
1711 return ""
1712}
1713
1714// The listed modules' stubs contents do not match the corresponding txt files,
1715// but require additional api contributions to generate the full stubs.
1716// This method returns the name of the additional api contribution module
1717// for corresponding sdk_library modules.
1718func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1719 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1720 return val.AdditionalApiContribution
1721 }
1722 return ""
1723}
1724
Anton Hansson944e77d2020-08-19 11:40:22 +01001725func childModuleVisibility(childVisibility []string) []string {
1726 if childVisibility == nil {
1727 // No child visibility set. The child will use the visibility of the sdk_library.
1728 return nil
1729 }
1730
1731 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1732 var visibility []string
1733 visibility = append(visibility, "//visibility:override")
1734 visibility = append(visibility, childVisibility...)
1735 return visibility
1736}
1737
Paul Duffin5df79302020-05-16 15:52:12 +01001738// Creates the implementation java library
1739func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001740 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1741
Paul Duffin5df79302020-05-16 15:52:12 +01001742 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001743 Name *string
1744 Visibility []string
1745 Instrument bool
1746 Libs []string
1747 Static_libs []string
1748 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001749 }{
1750 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001751 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001752 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1753 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001754 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1755 // addition of &module.properties below.
1756 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001757 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1758 // addition of &module.properties below.
1759 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1760 // Pass the apex_available settings down so that the impl library can be statically
1761 // embedded within a library that is added to an APEX. Needed for updatable-media.
1762 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001763 }
1764
1765 properties := []interface{}{
1766 &module.properties,
1767 &module.protoProperties,
1768 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001769 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001770 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001771 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001772 &props,
1773 module.sdkComponentPropertiesForChildLibrary(),
1774 }
1775 mctx.CreateModule(LibraryFactory, properties...)
1776}
1777
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001778type libraryProperties struct {
1779 Name *string
1780 Visibility []string
1781 Srcs []string
1782 Installable *bool
1783 Sdk_version *string
1784 System_modules *string
1785 Patch_module *string
1786 Libs []string
1787 Static_libs []string
1788 Compile_dex *bool
1789 Java_version *string
1790 Openjdk9 struct {
1791 Srcs []string
1792 Javacflags []string
1793 }
1794 Dist struct {
1795 Targets []string
1796 Dest *string
1797 Dir *string
1798 Tag *string
1799 }
1800}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001801
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001802func (module *SdkLibrary) stubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
1803 props := libraryProperties{}
Jihoon Kang786df932023-09-07 01:18:31 +00001804 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001805 // sources are generated from the droiddoc
Paul Duffin12ceb462019-12-24 20:31:31 +00001806 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001807 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001808 props.System_modules = module.deviceProperties.System_modules
1809 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001810 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001811 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001812 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001813 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001814 // The stub-annotations library contains special versions of the annotations
1815 // with CLASS retention policy, so that they're kept.
1816 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1817 props.Libs = append(props.Libs, "stub-annotations")
1818 }
Paul Duffina18abc22020-05-16 18:54:24 +01001819 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1820 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001821 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1822 // interop with older developer tools that don't support 1.9.
1823 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001824
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001825 return props
1826}
1827
1828// Creates a static java library that has API stubs
1829func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1830
1831 props := module.stubsLibraryProps(mctx, apiScope)
1832 props.Name = proptools.StringPtr(module.sourceStubsLibraryModuleName(apiScope))
1833 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
1834
1835 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1836}
1837
1838// Create a static java library that compiles the "exportable" stubs
1839func (module *SdkLibrary) createExportableStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1840 props := module.stubsLibraryProps(mctx, apiScope)
1841 props.Name = proptools.StringPtr(module.exportableSourceStubsLibraryModuleName(apiScope))
1842 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope) + "{.exportable}"}
1843
Paul Duffin859fe962020-05-15 10:20:31 +01001844 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001845}
1846
Paul Duffin6d0886e2020-04-07 18:49:53 +01001847// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001848// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001849func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001850 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001851 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001852 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001853 Srcs []string
1854 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001855 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001856 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001857 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001858 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001859 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001860 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001861 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001862 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001863 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001864 Merge_annotations_dirs []string
1865 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001866 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001867 Previous_api *string
Jihoon Kang6592e872023-12-19 01:13:16 +00001868 Aconfig_declarations []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001869 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001870 Current ApiToCheck
1871 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001872
1873 Api_lint struct {
1874 Enabled *bool
1875 New_since *string
1876 Baseline_file *string
1877 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001878 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001879 Aidl struct {
1880 Include_dirs []string
1881 Local_include_dirs []string
1882 }
Paul Duffin040e9062020-11-23 17:41:36 +00001883 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001884 }{}
1885
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001886 // The stubs source processing uses the same compile time classpath when extracting the
1887 // API from the implementation library as it does when compiling it. i.e. the same
1888 // * sdk version
1889 // * system_modules
1890 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001891
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001892 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001893 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001894 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001895 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001896 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001897 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001898 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001899 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001900 // A droiddoc module has only one Libs property and doesn't distinguish between
1901 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001902 props.Libs = module.properties.Libs
1903 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001904 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001905 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001906 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1907 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1908 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001909
Paul Duffine22c2ab2020-05-20 19:35:27 +01001910 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001911 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1912 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
Jihoon Kang6592e872023-12-19 01:13:16 +00001913 props.Aconfig_declarations = module.sdkLibraryProperties.Aconfig_declarations
Sundong Ahn054b19a2018-10-19 13:46:09 +09001914
Paul Duffin6d0886e2020-04-07 18:49:53 +01001915 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001916 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001917 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001918 }
1919 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001920 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001921 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1922 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001923 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00001924 disabledWarnings := []string{"HiddenSuperclass"}
1925 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
1926 disabledWarnings = append(disabledWarnings,
1927 "BroadcastBehavior",
1928 "DeprecationMismatch",
1929 "MissingPermission",
1930 "SdkConstant",
1931 "Todo",
1932 )
Paul Duffin235ffff2019-12-24 10:41:30 +00001933 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001934 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001935
Paul Duffin6877e6d2020-09-25 19:59:14 +01001936 // Output Javadoc comments for public scope.
1937 if apiScope == apiScopePublic {
1938 props.Output_javadoc_comments = proptools.BoolPtr(true)
1939 }
1940
Paul Duffin1fb487d2020-04-07 18:50:10 +01001941 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001942 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001943 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001944 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001945
Paul Duffin15f34ef2020-07-20 18:04:44 +01001946 // List of APIs identified from the provided source files are created. They are later
1947 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1948 // last-released (a.k.a numbered) list of API.
1949 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1950 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1951 apiDir := module.getApiDir()
1952 currentApiFileName = path.Join(apiDir, currentApiFileName)
1953 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001954
Paul Duffin15f34ef2020-07-20 18:04:44 +01001955 // check against the not-yet-release API
1956 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1957 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001958
Paul Duffin958806b2022-05-16 13:10:47 +00001959 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001960 // check against the latest released API
1961 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001962 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001963 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1964 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1965 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001966 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1967 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001968
Paul Duffin15f34ef2020-07-20 18:04:44 +01001969 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1970 // Enable api lint.
1971 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1972 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001973
Paul Duffin15f34ef2020-07-20 18:04:44 +01001974 // If it exists then pass a lint-baseline.txt through to droidstubs.
1975 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1976 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1977 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1978 if err != nil {
1979 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1980 }
1981 if len(paths) == 1 {
1982 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1983 } else if len(paths) != 0 {
1984 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001985 }
1986 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001987 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001988
Paul Duffin15f34ef2020-07-20 18:04:44 +01001989 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001990 // Dist the api txt and removed api txt artifacts for sdk builds.
1991 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1992 for _, p := range []struct {
1993 tag string
1994 pattern string
1995 }{
1996 {tag: ".api.txt", pattern: "%s.txt"},
1997 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1998 } {
1999 props.Dists = append(props.Dists, android.Dist{
2000 Targets: []string{"sdk", "win_sdk"},
2001 Dir: distDir,
2002 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
2003 Tag: proptools.StringPtr(p.tag),
2004 })
2005 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00002006 }
2007
Spandan Das2cc80ba2023-10-27 17:21:52 +00002008 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002009}
2010
Jihoon Kang0c705a42023-08-02 06:44:57 +00002011func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002012 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00002013 Name *string
2014 Visibility []string
2015 Api_contributions []string
2016 Libs []string
2017 Static_libs []string
2018 Full_api_surface_stub *string
Jihoon Kang4ec24872023-10-05 17:26:09 +00002019 System_modules *string
Jihoon Kang063ec002023-06-28 01:16:23 +00002020 Enable_validation *bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002021 }{}
2022
2023 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00002024 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002025
2026 apiContributions := []string{}
2027
2028 // Api surfaces are not independent of each other, but have subset relationships,
2029 // and so does the api files. To generate from-text stubs for api surfaces other than public,
2030 // all subset api domains' api_contriubtions must be added as well.
2031 scope := apiScope
2032 for scope != nil {
2033 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
2034 scope = scope.extends
2035 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00002036 if apiScope == apiScopePublic {
2037 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
2038 if additionalApiContribution != "" {
2039 apiContributions = append(apiContributions, additionalApiContribution)
2040 }
2041 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002042
2043 props.Api_contributions = apiContributions
2044 props.Libs = module.properties.Libs
2045 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00002046 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002047 props.Libs = append(props.Libs, "stub-annotations")
2048 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kange7ee2562023-07-25 05:51:46 +00002049 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
Jihoon Kang0c705a42023-08-02 06:44:57 +00002050 if alternativeFullApiSurfaceStub != "" {
2051 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
2052 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002053
2054 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
2055 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
2056 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00002057 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002058 }
2059
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00002060 // java_sdk_library modules that set sdk_version as none does not depend on other api
2061 // domains. Therefore, java_api_library created from such modules should not depend on
2062 // full_api_surface_stubs but create and compile stubs by the java_api_library module
2063 // itself.
2064 if module.SdkVersion(mctx).Kind == android.SdkNone {
2065 props.Full_api_surface_stub = nil
2066 }
2067
Jihoon Kang4ec24872023-10-05 17:26:09 +00002068 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00002069 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang4ec24872023-10-05 17:26:09 +00002070
Spandan Das2cc80ba2023-10-27 17:21:52 +00002071 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002072}
2073
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002074func (module *SdkLibrary) topLevelStubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
2075 props := libraryProperties{}
2076
Jihoon Kang1147b312023-06-08 23:25:57 +00002077 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
2078 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
2079 props.Sdk_version = proptools.StringPtr(sdkVersion)
2080
Jihoon Kang1147b312023-06-08 23:25:57 +00002081 props.System_modules = module.deviceProperties.System_modules
2082
Jihoon Kang1147b312023-06-08 23:25:57 +00002083 // The imports need to be compiled to dex if the java_sdk_library requests it.
2084 compileDex := module.dexProperties.Compile_dex
2085 if module.stubLibrariesCompiledForDex() {
2086 compileDex = proptools.BoolPtr(true)
2087 }
2088 props.Compile_dex = compileDex
2089
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002090 return props
2091}
2092
2093func (module *SdkLibrary) createTopLevelStubsLibrary(
2094 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
2095
2096 props := module.topLevelStubsLibraryProps(mctx, apiScope)
2097 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
2098
2099 // Add the stub compiling java_library/java_api_library as static lib based on build config
2100 staticLib := module.sourceStubsLibraryModuleName(apiScope)
2101 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
2102 staticLib = module.apiLibraryModuleName(apiScope)
2103 }
2104 props.Static_libs = append(props.Static_libs, staticLib)
2105
2106 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2107}
2108
2109func (module *SdkLibrary) createTopLevelExportableStubsLibrary(
2110 mctx android.DefaultableHookContext, apiScope *apiScope) {
2111
2112 props := module.topLevelStubsLibraryProps(mctx, apiScope)
2113 props.Name = proptools.StringPtr(module.exportableStubsLibraryModuleName(apiScope))
2114
2115 // Dist the class jar artifact for sdk builds.
2116 // "exportable" stubs are copied to dist for sdk builds instead of the "everything" stubs.
2117 if !Bool(module.sdkLibraryProperties.No_dist) {
2118 props.Dist.Targets = []string{"sdk", "win_sdk"}
2119 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
2120 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
2121 props.Dist.Tag = proptools.StringPtr(".jar")
2122 }
2123
2124 staticLib := module.exportableSourceStubsLibraryModuleName(apiScope)
2125 props.Static_libs = append(props.Static_libs, staticLib)
2126
Jihoon Kang1147b312023-06-08 23:25:57 +00002127 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2128}
2129
Paul Duffin958806b2022-05-16 13:10:47 +00002130func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
2131 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
2132}
2133
Paul Duffinea8f8082021-06-24 13:25:57 +01002134// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09002135func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2136 depTag := mctx.OtherModuleDependencyTag(dep)
2137 if depTag == xmlPermissionsFileTag {
2138 return true
2139 }
2140 return module.Library.DepIsInSameApex(mctx, dep)
2141}
2142
Paul Duffinea8f8082021-06-24 13:25:57 +01002143// Implements android.ApexModule
2144func (module *SdkLibrary) UniqueApexVariations() bool {
2145 return module.uniqueApexVariations()
2146}
2147
Jihoon Kang80456fd2023-11-15 19:22:14 +00002148func (module *SdkLibrary) ContributeToApi() bool {
2149 return proptools.BoolDefault(module.sdkLibraryProperties.Contribute_to_android_api, false)
2150}
2151
Jiyong Parkc678ad32018-04-10 13:07:10 +09002152// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01002153func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002154 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002155 var moduleMinApiLevelStr = moduleMinApiLevel.String()
2156 if moduleMinApiLevel == android.NoneApiLevel {
2157 moduleMinApiLevelStr = "current"
2158 }
Jiyong Parke3833882020-02-17 17:28:10 +09002159 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00002160 Name *string
2161 Lib_name *string
2162 Apex_available []string
2163 On_bootclasspath_since *string
2164 On_bootclasspath_before *string
2165 Min_device_sdk *string
2166 Max_device_sdk *string
2167 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002168 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002169 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00002170 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
2171 Lib_name: proptools.StringPtr(module.BaseModuleName()),
2172 Apex_available: module.ApexProperties.Apex_available,
2173 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
2174 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
2175 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
2176 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
2177 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002178 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002179 }
Jiyong Parke3833882020-02-17 17:28:10 +09002180
Jiyong Parke3833882020-02-17 17:28:10 +09002181 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002182}
2183
Jiyong Parkf1691d22021-03-29 20:11:58 +09002184func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002185 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002186 var kind android.SdkKind
2187 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002188 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002189 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002190 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002191 // We don't have prebuilt SDK for the specific sdkVersion.
2192 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002193 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002194 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002195 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002196
2197 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002198 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002199 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002200 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002201 if ctx.Config().AllowMissingDependencies() {
2202 return android.Paths{android.PathForSource(ctx, jar)}
2203 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002204 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002205 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002206 return nil
2207 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002208 return android.Paths{jarPath.Path()}
2209}
2210
Colin Crossaede88c2020-08-11 12:17:01 -07002211// 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 +01002212//
2213// If either this or the other module are on the platform then this will return
2214// false.
Colin Cross56a83212020-09-15 18:30:11 -07002215func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
Colin Crossff694a82023-12-13 15:54:49 -08002216 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross313aa542023-12-13 13:47:44 -08002217 otherApexInfo, _ := android.OtherModuleProvider(ctx, other, android.ApexInfoProvider)
Jiyong Parkab50b072021-05-12 17:13:56 +09002218 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002219}
2220
Jiyong Parkf1691d22021-03-29 20:11:58 +09002221func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002222 // If the client doesn't set sdk_version, but if this library prefers stubs over
2223 // the impl library, let's provide the widest API surface possible. To do so,
2224 // force override sdk_version to module_current so that the closest possible API
2225 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002226 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002227 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002228 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002229
Paul Duffindaaa3322020-05-26 18:13:57 +01002230 // Only provide access to the implementation library if it is actually built.
2231 if module.requiresRuntimeImplementationLibrary() {
2232 // Check any special cases for java_sdk_library.
2233 //
2234 // Only allow access to the implementation library in the following condition:
2235 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002236 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002237 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01002238 if headerJars {
2239 return module.HeaderJars()
2240 } else {
2241 return module.ImplementationJars()
2242 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002243 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002244 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002245
Paul Duffin23970f42020-05-20 14:20:02 +01002246 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002247}
2248
Sundong Ahn241cd372018-07-13 16:16:44 +09002249// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002250func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002251 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
2252}
2253
2254// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002255func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002256 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09002257}
2258
Colin Cross571cccf2019-02-04 11:22:08 -08002259var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2260
Jiyong Park82484c02018-04-23 21:41:26 +09002261func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002262 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002263 return &[]string{}
2264 }).(*[]string)
2265}
2266
Paul Duffin749f98f2019-12-30 17:23:46 +00002267func (module *SdkLibrary) getApiDir() string {
2268 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2269}
2270
Jiyong Parkc678ad32018-04-10 13:07:10 +09002271// For a java_sdk_library module, create internal modules for stubs, docs,
2272// runtime libs and xml file. If requested, the stubs and docs are created twice
2273// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002274func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2275 // If the module has been disabled then don't create any child modules.
2276 if !module.Enabled() {
2277 return
2278 }
2279
Paul Duffina18abc22020-05-16 18:54:24 +01002280 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002281 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002282 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002283 }
2284
Paul Duffin37e0b772019-12-30 17:20:10 +00002285 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002286 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002287 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002288 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002289 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002290
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002291 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002292
Paul Duffin3375e352020-04-28 10:44:03 +01002293 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002294
Paul Duffin749f98f2019-12-30 17:23:46 +00002295 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002296 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002297 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002298 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002299 p := android.ExistentPathForSource(mctx, path)
2300 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002301 if mctx.Config().AllowMissingDependencies() {
2302 mctx.AddMissingDependencies([]string{path})
2303 } else {
2304 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2305 missingCurrentApi = true
2306 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002307 }
2308 }
2309 }
2310
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002311 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002312 script := "build/soong/scripts/gen-java-current-api-files.sh"
2313 p := android.ExistentPathForSource(mctx, script)
2314
2315 if !p.Valid() {
2316 panic(fmt.Sprintf("script file %s doesn't exist", script))
2317 }
2318
2319 mctx.ModuleErrorf("One or more current api files are missing. "+
2320 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002321 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002322 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002323 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002324 return
2325 }
2326
Paul Duffin3375e352020-04-28 10:44:03 +01002327 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002328 // Use the stubs source name for legacy reasons.
2329 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002330
Paul Duffind1b3a922020-01-22 11:57:20 +00002331 module.createStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002332 module.createExportableStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002333
Jihoon Kang0c705a42023-08-02 06:44:57 +00002334 alternativeFullApiSurfaceStubLib := ""
2335 if scope == apiScopePublic {
2336 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
2337 }
2338 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
Jihoon Kang1147b312023-06-08 23:25:57 +00002339 if contributesToApiSurface {
Jihoon Kang0c705a42023-08-02 06:44:57 +00002340 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002341 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002342
2343 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002344 module.createTopLevelExportableStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09002345 }
2346
Paul Duffindfa131e2020-05-15 20:37:11 +01002347 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002348 // Create child module to create an implementation library.
2349 //
2350 // This temporarily creates a second implementation library that can be explicitly
2351 // referenced.
2352 //
2353 // TODO(b/156618935) - update comment once only one implementation library is created.
2354 module.createImplLibrary(mctx)
2355
Paul Duffindfa131e2020-05-15 20:37:11 +01002356 // Only create an XML permissions file that declares the library as being usable
2357 // as a shared library if required.
2358 if module.sharedLibrary() {
2359 module.createXmlFile(mctx)
2360 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002361
2362 // record java_sdk_library modules so that they are exported to make
2363 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2364 javaSdkLibrariesLock.Lock()
2365 defer javaSdkLibrariesLock.Unlock()
2366 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2367 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002368
Paul Duffin77590a82022-04-28 14:13:30 +00002369 // 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 +01002370 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002371 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002372}
2373
2374func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002375 module.addHostAndDeviceProperties()
2376 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002377
Paul Duffin71b33cc2021-06-23 11:39:47 +01002378 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002379
Paul Duffina18abc22020-05-16 18:54:24 +01002380 module.properties.Installable = proptools.BoolPtr(true)
2381 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002382}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002383
Paul Duffindfa131e2020-05-15 20:37:11 +01002384func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2385 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2386}
2387
Jiyong Park932cdfe2020-05-28 00:19:53 +09002388func (module *SdkLibrary) defaultsToStubs() bool {
2389 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2390}
2391
Paul Duffin1b1e8062020-05-08 13:44:43 +01002392// Defines how to name the individual component modules the sdk library creates.
2393type sdkLibraryComponentNamingScheme interface {
2394 stubsLibraryModuleName(scope *apiScope, baseName string) string
2395
2396 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002397
2398 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002399
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002400 sourceStubsLibraryModuleName(scope *apiScope, baseName string) string
2401
2402 exportableStubsLibraryModuleName(scope *apiScope, baseName string) string
2403
2404 exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002405}
2406
2407type defaultNamingScheme struct {
2408}
2409
2410func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2411 return scope.stubsLibraryModuleName(baseName)
2412}
2413
2414func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2415 return scope.stubsSourceModuleName(baseName)
2416}
2417
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002418func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2419 return scope.apiLibraryModuleName(baseName)
2420}
2421
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002422func (s *defaultNamingScheme) sourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
Jihoon Kang1147b312023-06-08 23:25:57 +00002423 return scope.sourceStubLibraryModuleName(baseName)
2424}
2425
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002426func (s *defaultNamingScheme) exportableStubsLibraryModuleName(scope *apiScope, baseName string) string {
2427 return scope.exportableStubsLibraryModuleName(baseName)
2428}
2429
2430func (s *defaultNamingScheme) exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
2431 return scope.exportableSourceStubsLibraryModuleName(baseName)
2432}
2433
Paul Duffin1b1e8062020-05-08 13:44:43 +01002434var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2435
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002436func hasStubsLibrarySuffix(name string, apiScope *apiScope) bool {
2437 return strings.HasSuffix(name, apiScope.stubsLibraryModuleNameSuffix()) ||
2438 strings.HasSuffix(name, apiScope.exportableStubsLibraryModuleNameSuffix())
2439}
2440
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002441func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002442 name = strings.TrimSuffix(name, ".from-source")
2443
Anton Hansson2d0c1942020-05-25 12:20:51 +01002444 // This suffix-based approach is fragile and could potentially mis-trigger.
2445 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002446 if hasStubsLibrarySuffix(name, apiScopePublic) {
Anton Hansson08f476b2021-04-07 15:32:19 +01002447 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2448 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2449 return false, javaPlatform
2450 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002451 return true, javaSdk
2452 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002453 if hasStubsLibrarySuffix(name, apiScopeSystem) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002454 return true, javaSystem
2455 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002456 if hasStubsLibrarySuffix(name, apiScopeModuleLib) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002457 return true, javaModule
2458 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002459 if hasStubsLibrarySuffix(name, apiScopeTest) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002460 return true, javaSystem
2461 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002462 if hasStubsLibrarySuffix(name, apiScopeSystemServer) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002463 return true, javaSystemServer
2464 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002465 return false, javaPlatform
2466}
2467
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002468// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2469// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2470// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2471// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2472// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002473func SdkLibraryFactory() android.Module {
2474 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002475
2476 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002477 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002478
Inseob Kimc0907f12019-02-08 21:00:45 +09002479 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002480 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002481 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002482
2483 // Initialize the map from scope to scope specific properties.
2484 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2485 for _, scope := range allApiScopes {
2486 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2487 }
2488 module.scopeToProperties = scopeToProperties
2489
Paul Duffin4911a892020-04-29 23:35:13 +01002490 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002491 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002492 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2493 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2494
Paul Duffin1b1e8062020-05-08 13:44:43 +01002495 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002496 // If no implementation is required then it cannot be used as a shared library
2497 // either.
2498 if !module.requiresRuntimeImplementationLibrary() {
2499 // If shared_library has been explicitly set to true then it is incompatible
2500 // with api_only: true.
2501 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2502 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2503 }
2504 // Set shared_library: false.
2505 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2506 }
2507
Paul Duffin1b1e8062020-05-08 13:44:43 +01002508 if module.initCommonAfterDefaultsApplied(ctx) {
2509 module.CreateInternalModules(ctx)
2510 }
2511 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002512 return module
2513}
Colin Cross79c7c262019-04-17 11:11:46 -07002514
2515//
2516// SDK library prebuilts
2517//
2518
Paul Duffin56d44902020-01-31 13:36:25 +00002519// Properties associated with each api scope.
2520type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002521 Jars []string `android:"path"`
2522
2523 Sdk_version *string
2524
Colin Cross79c7c262019-04-17 11:11:46 -07002525 // List of shared java libs that this module has dependencies to
2526 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002527
Paul Duffinc8782502020-04-29 20:45:27 +01002528 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002529 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002530
2531 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002532 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002533
2534 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002535 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002536
2537 // Annotation zip
2538 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002539}
2540
Paul Duffin56d44902020-01-31 13:36:25 +00002541type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002542 // List of shared java libs, common to all scopes, that this module has
2543 // dependencies to
2544 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002545
2546 // If set to true, compile dex files for the stubs. Defaults to false.
2547 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002548
2549 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002550 Permitted_packages []string
Spandan Das23956d12024-01-19 00:22:22 +00002551
2552 // Name of the source soong module that gets shadowed by this prebuilt
2553 // If unspecified, follows the naming convention that the source module of
2554 // the prebuilt is Name() without "prebuilt_" prefix
2555 Source_module_name *string
Paul Duffin56d44902020-01-31 13:36:25 +00002556}
2557
Paul Duffineedc5d52020-06-12 17:46:39 +01002558type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002559 android.ModuleBase
2560 android.DefaultableModuleBase
2561 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002562 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002563
Paul Duffin37856732021-02-26 14:24:15 +00002564 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002565 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002566
Colin Cross79c7c262019-04-17 11:11:46 -07002567 properties sdkLibraryImportProperties
2568
Paul Duffin46a26a82020-04-07 19:27:04 +01002569 // Map from api scope to the scope specific property structure.
2570 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2571
Paul Duffin56d44902020-01-31 13:36:25 +00002572 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002573
2574 // The reference to the implementation library created by the source module.
2575 // Is nil if the source module does not exist.
2576 implLibraryModule *Library
2577
2578 // The reference to the xml permissions module created by the source module.
2579 // Is nil if the source module does not exist.
2580 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002581
Jeongik Chad5fe8782021-07-08 01:13:11 +09002582 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00002583 dexJarFile OptionalDexJarPath
2584 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09002585
2586 // Expected install file path of the source module(sdk_library)
2587 // or dex implementation jar obtained from the prebuilt_apex, if any.
2588 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002589}
2590
Paul Duffineedc5d52020-06-12 17:46:39 +01002591var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002592
Paul Duffin46a26a82020-04-07 19:27:04 +01002593// The type of a structure that contains a field of type sdkLibraryScopeProperties
2594// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002595//
2596// struct {
2597// Public sdkLibraryScopeProperties
2598// System sdkLibraryScopeProperties
2599// ...
2600// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002601var allScopeStructType = createAllScopePropertiesStructType()
2602
2603// Dynamically create a structure type for each apiscope in allApiScopes.
2604func createAllScopePropertiesStructType() reflect.Type {
2605 var fields []reflect.StructField
2606 for _, apiScope := range allApiScopes {
2607 field := reflect.StructField{
2608 Name: apiScope.fieldName,
2609 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2610 }
2611 fields = append(fields, field)
2612 }
2613
2614 return reflect.StructOf(fields)
2615}
2616
2617// Create an instance of the scope specific structure type and return a map
2618// from apiscope to a pointer to each scope specific field.
2619func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2620 allScopePropertiesPtr := reflect.New(allScopeStructType)
2621 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2622 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2623
2624 for _, apiScope := range allApiScopes {
2625 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2626 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2627 }
2628
2629 return allScopePropertiesPtr.Interface(), scopeProperties
2630}
2631
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002632// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002633func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002634 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002635
Paul Duffin46a26a82020-04-07 19:27:04 +01002636 allScopeProperties, scopeToProperties := createPropertiesInstance()
2637 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002638 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002639
Paul Duffinc3091c82020-05-08 14:16:20 +01002640 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002641 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002642
Paul Duffin0bdcb272020-02-06 15:24:57 +00002643 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002644 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002645 InitJavaModule(module, android.HostAndDeviceSupported)
2646
Paul Duffin1b1e8062020-05-08 13:44:43 +01002647 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2648 if module.initCommonAfterDefaultsApplied(mctx) {
2649 module.createInternalModules(mctx)
2650 }
2651 })
Colin Cross79c7c262019-04-17 11:11:46 -07002652 return module
2653}
2654
Paul Duffin630b11e2021-07-15 13:35:26 +01002655var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2656
2657func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2658 return module.properties.Permitted_packages
2659}
2660
Paul Duffineedc5d52020-06-12 17:46:39 +01002661func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002662 return &module.prebuilt
2663}
2664
Paul Duffineedc5d52020-06-12 17:46:39 +01002665func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002666 return module.prebuilt.Name(module.ModuleBase.Name())
2667}
2668
Spandan Das23956d12024-01-19 00:22:22 +00002669func (module *SdkLibraryImport) BaseModuleName() string {
2670 return proptools.StringDefault(module.properties.Source_module_name, module.ModuleBase.Name())
2671}
2672
Paul Duffineedc5d52020-06-12 17:46:39 +01002673func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002674
Paul Duffin50061512020-01-21 16:31:05 +00002675 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002676 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002677 module.prebuilt.ForcePrefer()
2678 }
2679
Paul Duffin46a26a82020-04-07 19:27:04 +01002680 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002681 if len(scopeProperties.Jars) == 0 {
2682 continue
2683 }
2684
Paul Duffinbbb546b2020-04-09 00:07:11 +01002685 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002686
Paul Duffin0f8faff2020-05-20 16:18:00 +01002687 if len(scopeProperties.Stub_srcs) > 0 {
2688 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2689 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002690
2691 if scopeProperties.Current_api != nil {
2692 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2693 }
Paul Duffin56d44902020-01-31 13:36:25 +00002694 }
Colin Cross79c7c262019-04-17 11:11:46 -07002695
2696 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2697 javaSdkLibrariesLock.Lock()
2698 defer javaSdkLibrariesLock.Unlock()
2699 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2700}
2701
Paul Duffineedc5d52020-06-12 17:46:39 +01002702func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002703 // Creates a java import for the jar with ".stubs" suffix
2704 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002705 Name *string
2706 Source_module_name *string
2707 Created_by_java_sdk_library_name *string
2708 Sdk_version *string
2709 Libs []string
2710 Jars []string
2711 Compile_dex *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002712
2713 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002714 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002715 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002716 props.Source_module_name = proptools.StringPtr(apiScope.stubsLibraryModuleName(module.BaseModuleName()))
2717 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002718 props.Sdk_version = scopeProperties.Sdk_version
2719 // Prepend any of the libs from the legacy public properties to the libs for each of the
2720 // scopes to avoid having to duplicate them in each scope.
2721 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2722 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002723
Paul Duffin38b57852020-05-13 16:08:09 +01002724 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002725 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002726
Paul Duffin1267d872021-04-16 17:21:36 +01002727 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002728 compileDex := module.properties.Compile_dex
2729 if module.stubLibrariesCompiledForDex() {
2730 compileDex = proptools.BoolPtr(true)
2731 }
2732 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002733
Paul Duffin859fe962020-05-15 10:20:31 +01002734 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002735}
2736
Paul Duffineedc5d52020-06-12 17:46:39 +01002737func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002738 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002739 Name *string
2740 Source_module_name *string
2741 Created_by_java_sdk_library_name *string
2742 Srcs []string
Paul Duffinbf4de042022-09-27 12:41:52 +01002743
2744 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002745 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002746 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002747 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()))
2748 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002749 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002750
2751 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002752 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2753
Spandan Das2cc80ba2023-10-27 17:21:52 +00002754 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002755}
2756
Jihoon Kang71c86832023-09-13 01:01:53 +00002757func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2758 api_file := scopeProperties.Current_api
2759 api_surface := &apiScope.name
2760
2761 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002762 Name *string
2763 Source_module_name *string
2764 Created_by_java_sdk_library_name *string
2765 Api_surface *string
2766 Api_file *string
2767 Visibility []string
Jihoon Kang71c86832023-09-13 01:01:53 +00002768 }{}
2769
2770 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
Spandan Das23956d12024-01-19 00:22:22 +00002771 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()) + ".api.contribution")
2772 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Jihoon Kang71c86832023-09-13 01:01:53 +00002773 props.Api_surface = api_surface
2774 props.Api_file = api_file
2775 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2776
Spandan Das2cc80ba2023-10-27 17:21:52 +00002777 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002778}
2779
Paul Duffin44f1d842020-06-26 20:17:02 +01002780// Add the dependencies on the child module in the component deps mutator so that it
2781// creates references to the prebuilt and not the source modules.
2782func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002783 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002784 if len(scopeProperties.Jars) == 0 {
2785 continue
2786 }
2787
2788 // Add dependencies to the prebuilt stubs library
Jihoon Kangb7431552024-01-22 19:40:08 +00002789 ctx.AddVariationDependencies(nil, apiScope.prebuiltStubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002790
2791 if len(scopeProperties.Stub_srcs) > 0 {
2792 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002793 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002794 }
Paul Duffin56d44902020-01-31 13:36:25 +00002795 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002796}
2797
2798// Add other dependencies as normal.
2799func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002800
2801 implName := module.implLibraryModuleName()
2802 if ctx.OtherModuleExists(implName) {
2803 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2804
2805 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2806 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2807 // Add dependency to the rule for generating the xml permissions file
2808 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2809 }
2810 }
Colin Cross79c7c262019-04-17 11:11:46 -07002811}
2812
Jiyong Park45bf82e2020-12-15 22:29:02 +09002813var _ android.ApexModule = (*SdkLibraryImport)(nil)
2814
2815// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002816func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2817 depTag := mctx.OtherModuleDependencyTag(dep)
2818 if depTag == xmlPermissionsFileTag {
2819 return true
2820 }
2821
2822 // None of the other dependencies of the java_sdk_library_import are in the same apex
2823 // as the one that references this module.
2824 return false
2825}
2826
Jiyong Park45bf82e2020-12-15 22:29:02 +09002827// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002828func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2829 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002830 // we don't check prebuilt modules for sdk_version
2831 return nil
2832}
2833
Paul Duffinea8f8082021-06-24 13:25:57 +01002834// Implements android.ApexModule
2835func (module *SdkLibraryImport) UniqueApexVariations() bool {
2836 return module.uniqueApexVariations()
2837}
2838
Paul Duffin09817d62022-04-28 17:45:11 +01002839// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002840func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2841 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002842}
2843
2844var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2845
Paul Duffineedc5d52020-06-12 17:46:39 +01002846func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002847 paths, err := module.commonOutputFiles(tag)
2848 if paths != nil || err != nil {
2849 return paths, err
2850 }
2851 if module.implLibraryModule != nil {
2852 return module.implLibraryModule.OutputFiles(tag)
2853 } else {
2854 return nil, nil
2855 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002856}
2857
Paul Duffineedc5d52020-06-12 17:46:39 +01002858func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002859 module.generateCommonBuildActions(ctx)
2860
Jeongik Chad5fe8782021-07-08 01:13:11 +09002861 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2862 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2863
Paul Duffin0f8faff2020-05-20 16:18:00 +01002864 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002865 ctx.VisitDirectDeps(func(to android.Module) {
2866 tag := ctx.OtherModuleDependencyTag(to)
2867
Paul Duffin0f8faff2020-05-20 16:18:00 +01002868 // Extract information from any of the scope specific dependencies.
2869 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2870 apiScope := scopeTag.apiScope
2871 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2872
2873 // Extract information from the dependency. The exact information extracted
2874 // is determined by the nature of the dependency which is determined by the tag.
2875 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002876 } else if tag == implLibraryTag {
2877 if implLibrary, ok := to.(*Library); ok {
2878 module.implLibraryModule = implLibrary
2879 } else {
2880 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2881 }
2882 } else if tag == xmlPermissionsFileTag {
2883 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2884 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2885 } else {
2886 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2887 }
Colin Cross79c7c262019-04-17 11:11:46 -07002888 }
2889 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002890
2891 // Populate the scope paths with information from the properties.
2892 for apiScope, scopeProperties := range module.scopeProperties {
2893 if len(scopeProperties.Jars) == 0 {
2894 continue
2895 }
2896
2897 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002898 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002899 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2900 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2901 }
Paul Duffin39853512021-02-26 11:09:39 +00002902
2903 if ctx.Device() {
2904 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2905 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08002906 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00002907 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002908 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00002909 di, err := android.FindDeapexerProviderForModule(ctx)
2910 if err != nil {
2911 // An error was found, possibly due to multiple apexes in the tree that export this library
2912 // Defer the error till a client tries to call DexJarBuildPath
2913 module.dexJarFileErr = err
Spandan Das3a392012024-01-17 18:26:27 +00002914 module.initHiddenAPIError(err)
Spandan Dasfae468e2023-12-12 23:23:53 +00002915 return
Martin Stjernholm44825602021-09-17 01:44:12 +01002916 }
Spandan Das5be63332023-12-13 00:06:32 +00002917 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(module.BaseModuleName())
Jiakai Zhang81e46812023-02-08 21:56:07 +08002918 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002919 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2920 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002921 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002922 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002923 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002924 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002925
Spandan Dase21a8d42024-01-23 23:56:29 +00002926 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), installPath)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002927 module.dexpreopter.isSDKLibrary = true
Spandan Dase21a8d42024-01-23 23:56:29 +00002928 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002929
2930 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2931 module.dexpreopter.inputProfilePathOnHost = profilePath
2932 }
Paul Duffin39853512021-02-26 11:09:39 +00002933 } else {
2934 // This should never happen as a variant for a prebuilt_apex is only created if the
2935 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002936 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002937 }
2938 }
2939 }
Colin Cross79c7c262019-04-17 11:11:46 -07002940}
2941
Jiyong Parkf1691d22021-03-29 20:11:58 +09002942func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002943
2944 // For consistency with SdkLibrary make the implementation jar available to libraries that
2945 // are within the same APEX.
2946 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002947 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002948 if headerJars {
2949 return implLibraryModule.HeaderJars()
2950 } else {
2951 return implLibraryModule.ImplementationJars()
2952 }
2953 }
2954
Paul Duffin23970f42020-05-20 14:20:02 +01002955 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002956}
2957
Colin Cross79c7c262019-04-17 11:11:46 -07002958// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002959func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002960 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002961 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002962}
2963
2964// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002965func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002966 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002967 return module.sdkJars(ctx, sdkVersion, false)
2968}
2969
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002970// to satisfy UsesLibraryDependency interface
Spandan Das59a4a2b2024-01-09 21:35:56 +00002971func (module *SdkLibraryImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002972 // The dex implementation jar extracted from the .apex file should be used in preference to the
2973 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00002974 if module.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002975 ctx.ModuleErrorf(module.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00002976 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002977 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002978 return module.dexJarFile
2979 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002980 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002981 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002982 } else {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002983 return module.implLibraryModule.DexJarBuildPath(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +01002984 }
2985}
2986
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002987// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002988func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002989 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002990}
2991
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002992// to satisfy UsesLibraryDependency interface
2993func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2994 return nil
2995}
2996
Paul Duffineedc5d52020-06-12 17:46:39 +01002997// to satisfy apex.javaDependency interface
2998func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2999 if module.implLibraryModule == nil {
3000 return nil
3001 } else {
3002 return module.implLibraryModule.JacocoReportClassesFile()
3003 }
3004}
3005
3006// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07003007func (module *SdkLibraryImport) LintDepSets() LintDepSets {
3008 if module.implLibraryModule == nil {
3009 return LintDepSets{}
3010 } else {
3011 return module.implLibraryModule.LintDepSets()
3012 }
3013}
3014
Spandan Das17854f52022-01-14 21:19:14 +00003015func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003016 if module.implLibraryModule == nil {
3017 return false
3018 } else {
Spandan Das17854f52022-01-14 21:19:14 +00003019 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003020 }
3021}
3022
Spandan Das17854f52022-01-14 21:19:14 +00003023func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003024 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00003025 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07003026 }
3027}
3028
Colin Cross08dca382020-07-21 20:31:17 -07003029// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01003030func (module *SdkLibraryImport) Stem() string {
3031 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07003032}
Jiyong Parke3833882020-02-17 17:28:10 +09003033
Paul Duffin44b481b2020-06-17 16:59:43 +01003034var _ ApexDependency = (*SdkLibraryImport)(nil)
3035
3036// to satisfy java.ApexDependency interface
3037func (module *SdkLibraryImport) HeaderJars() android.Paths {
3038 if module.implLibraryModule == nil {
3039 return nil
3040 } else {
3041 return module.implLibraryModule.HeaderJars()
3042 }
3043}
3044
3045// to satisfy java.ApexDependency interface
3046func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
3047 if module.implLibraryModule == nil {
3048 return nil
3049 } else {
3050 return module.implLibraryModule.ImplementationAndResourcesJars()
3051 }
3052}
3053
Jiakai Zhang204356f2021-09-09 08:12:46 +00003054// to satisfy java.DexpreopterInterface interface
3055func (module *SdkLibraryImport) IsInstallable() bool {
3056 return true
3057}
3058
Paul Duffinfef55002021-06-17 14:56:05 +01003059var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
3060
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01003061func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01003062 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08003063 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01003064}
3065
Spandan Das2ea84dd2024-01-25 22:12:50 +00003066func (j *SdkLibraryImport) UseProfileGuidedDexpreopt() bool {
3067 return proptools.Bool(j.importDexpreoptProperties.Dex_preopt.Profile_guided)
3068}
3069
Jiyong Parke3833882020-02-17 17:28:10 +09003070// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09003071type sdkLibraryXml struct {
3072 android.ModuleBase
3073 android.DefaultableModuleBase
3074 android.ApexModuleBase
3075
3076 properties sdkLibraryXmlProperties
3077
3078 outputFilePath android.OutputPath
3079 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07003080
3081 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09003082}
3083
3084type sdkLibraryXmlProperties struct {
3085 // canonical name of the lib
3086 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003087
3088 // Signals that this shared library is part of the bootclasspath starting
3089 // on the version indicated in this attribute.
3090 //
3091 // This will make platforms at this level and above to ignore
3092 // <uses-library> tags with this library name because the library is already
3093 // available
3094 On_bootclasspath_since *string
3095
3096 // Signals that this shared library was part of the bootclasspath before
3097 // (but not including) the version indicated in this attribute.
3098 //
3099 // The system will automatically add a <uses-library> tag with this library to
3100 // apps that target any SDK less than the version indicated in this attribute.
3101 On_bootclasspath_before *string
3102
3103 // Indicates that PackageManager should ignore this shared library if the
3104 // platform is below the version indicated in this attribute.
3105 //
3106 // This means that the device won't recognise this library as installed.
3107 Min_device_sdk *string
3108
3109 // Indicates that PackageManager should ignore this shared library if the
3110 // platform is above the version indicated in this attribute.
3111 //
3112 // This means that the device won't recognise this library as installed.
3113 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00003114
3115 // The SdkLibrary's min api level as a string
3116 //
3117 // This value comes from the ApiLevel of the MinSdkVersion property.
3118 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00003119
3120 // Uses-libs dependencies that the shared library requires to work correctly.
3121 //
3122 // This will add dependency="foo:bar" to the <library> section.
3123 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09003124}
3125
3126// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
3127// Not to be used directly by users. java_sdk_library internally uses this.
3128func sdkLibraryXmlFactory() android.Module {
3129 module := &sdkLibraryXml{}
3130
3131 module.AddProperties(&module.properties)
3132
3133 android.InitApexModule(module)
3134 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
3135
3136 return module
3137}
3138
Colin Crossaede88c2020-08-11 12:17:01 -07003139func (module *sdkLibraryXml) UniqueApexVariations() bool {
3140 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
3141 // mounted APEX, which contains the name of the APEX.
3142 return true
3143}
3144
Jiyong Parke3833882020-02-17 17:28:10 +09003145// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09003146func (module *sdkLibraryXml) BaseDir() string {
3147 return "etc"
3148}
3149
3150// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09003151func (module *sdkLibraryXml) SubDir() string {
3152 return "permissions"
3153}
3154
3155// from android.PrebuiltEtcModule
3156func (module *sdkLibraryXml) OutputFile() android.OutputPath {
3157 return module.outputFilePath
3158}
3159
3160// from android.ApexModule
3161func (module *sdkLibraryXml) AvailableFor(what string) bool {
3162 return true
3163}
3164
3165func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
3166 // do nothing
3167}
3168
Jiyong Park45bf82e2020-12-15 22:29:02 +09003169var _ android.ApexModule = (*sdkLibraryXml)(nil)
3170
3171// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003172func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3173 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003174 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
3175 return nil
3176}
3177
Jiyong Parke3833882020-02-17 17:28:10 +09003178// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07003179func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09003180 implName := proptools.String(module.properties.Lib_name)
Colin Crossff694a82023-12-13 15:54:49 -08003181 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07003182 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09003183 // In most cases, this works fine. But when apex_name is set or override_apex is used
3184 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07003185 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09003186 }
3187 partition := "system"
3188 if module.SocSpecific() {
3189 partition = "vendor"
3190 } else if module.DeviceSpecific() {
3191 partition = "odm"
3192 } else if module.ProductSpecific() {
3193 partition = "product"
3194 } else if module.SystemExtSpecific() {
3195 partition = "system_ext"
3196 }
3197 return "/" + partition + "/framework/" + implName + ".jar"
3198}
3199
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003200func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
3201 if value == nil {
3202 return ""
3203 }
3204 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
3205 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003206 // attributes in bp files have underscores but in the xml have dashes.
3207 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003208 return ""
3209 }
Pedro Loureirob638c622021-12-22 15:28:05 +00003210 if apiLevel.IsCurrent() {
3211 // passing "current" would always mean a future release, never the current (or the current in
3212 // progress) which means some conditions would never be triggered.
3213 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3214 `"current" is not an allowed value for this attribute`)
3215 return ""
3216 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003217 // "safeValue" is safe because it translates finalized codenames to a string
3218 // with their SDK int.
3219 safeValue := apiLevel.String()
3220 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003221}
3222
3223// formats an attribute for the xml permissions file if the value is not null
3224// returns empty string otherwise
3225func formattedOptionalAttribute(attrName string, value *string) string {
3226 if value == nil {
3227 return ""
3228 }
3229 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
3230}
3231
Jamie Garsidee570ace2023-11-27 12:07:36 +00003232func formattedDependenciesAttribute(dependencies []string) string {
3233 if dependencies == nil {
3234 return ""
3235 }
3236 return fmt.Sprintf(` dependency=\"%s\"\n`, strings.Join(dependencies, ":"))
3237}
3238
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003239func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3240 libName := proptools.String(module.properties.Lib_name)
3241 libNameAttr := formattedOptionalAttribute("name", &libName)
3242 filePath := module.implPath(ctx)
3243 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003244 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3245 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3246 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3247 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003248 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003249 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3250 // 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 +00003251 var libraryTag string
3252 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003253 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00003254 } else {
3255 libraryTag = ` <library\n`
3256 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003257
3258 return strings.Join([]string{
3259 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
3260 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
3261 `\n`,
3262 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
3263 ` you may not use this file except in compliance with the License.\n`,
3264 ` You may obtain a copy of the License at\n`,
3265 `\n`,
3266 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
3267 `\n`,
3268 ` Unless required by applicable law or agreed to in writing, software\n`,
3269 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
3270 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
3271 ` See the License for the specific language governing permissions and\n`,
3272 ` limitations under the License.\n`,
3273 `-->\n`,
3274 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00003275 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003276 libNameAttr,
3277 filePathAttr,
3278 implicitFromAttr,
3279 implicitUntilAttr,
3280 minSdkAttr,
3281 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003282 dependenciesAttr,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003283 ` />\n`,
3284 `</permissions>\n`}, "")
3285}
3286
Jiyong Parke3833882020-02-17 17:28:10 +09003287func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossff694a82023-12-13 15:54:49 -08003288 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
3289 module.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07003290
Jiyong Parke3833882020-02-17 17:28:10 +09003291 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003292 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003293 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003294
3295 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08003296 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003297 rule.Command().
3298 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
3299 Output(module.outputFilePath)
3300
Colin Crossf1a035e2020-11-16 17:32:30 -08003301 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09003302
3303 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
3304}
3305
3306func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003307 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003308 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003309 Disabled: true,
3310 }}
3311 }
3312
satayev8f088b02021-12-06 11:40:46 +00003313 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003314 Class: "ETC",
3315 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3316 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003317 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003318 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003319 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003320 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3321 },
3322 },
3323 }}
3324}
Paul Duffindd46f712020-02-10 13:37:10 +00003325
Pedro Loureiroc3621422021-09-28 15:40:23 +00003326func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3327 module.validateAtLeastTAttributes(ctx)
3328 module.validateMinAndMaxDeviceSdk(ctx)
3329 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3330 module.validateOnBootclasspathBeforeRequirements(ctx)
3331}
3332
3333func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3334 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3335 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3336 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3337 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3338 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3339}
3340
3341func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3342 if attr != nil {
3343 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3344 // we will inform the user of invalid inputs when we try to write the
3345 // permissions xml file so we don't need to do it here
3346 if t.GreaterThan(level) {
3347 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3348 }
3349 }
3350 }
3351}
3352
3353func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3354 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3355 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3356 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3357 if minErr == nil && maxErr == nil {
3358 // we will inform the user of invalid inputs when we try to write the
3359 // permissions xml file so we don't need to do it here
3360 if min.GreaterThan(max) {
3361 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3362 }
3363 }
3364 }
3365}
3366
3367func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3368 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3369 if module.properties.Min_device_sdk != nil {
3370 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3371 if err == nil {
3372 if moduleMinApi.GreaterThan(api) {
3373 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3374 }
3375 }
3376 }
3377 if module.properties.Max_device_sdk != nil {
3378 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3379 if err == nil {
3380 if moduleMinApi.GreaterThan(api) {
3381 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3382 }
3383 }
3384 }
3385}
3386
3387func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3388 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3389 if module.properties.On_bootclasspath_before != nil {
3390 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3391 // if we use the attribute, then we need to do this validation
3392 if moduleMinApi.LessThan(t) {
3393 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3394 if module.properties.Min_device_sdk == nil {
3395 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")
3396 }
3397 }
3398 }
3399}
3400
Paul Duffindd46f712020-02-10 13:37:10 +00003401type sdkLibrarySdkMemberType struct {
3402 android.SdkMemberTypeBase
3403}
3404
Paul Duffin296701e2021-07-14 10:29:36 +01003405func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3406 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003407}
3408
3409func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3410 _, ok := module.(*SdkLibrary)
3411 return ok
3412}
3413
3414func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3415 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3416}
3417
3418func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3419 return &sdkLibrarySdkMemberProperties{}
3420}
3421
Paul Duffin976b0e52021-04-27 23:20:26 +01003422var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3423 android.SdkMemberTypeBase{
3424 PropertyName: "java_sdk_libs",
3425 SupportsSdk: true,
3426 },
3427}
3428
Paul Duffindd46f712020-02-10 13:37:10 +00003429type sdkLibrarySdkMemberProperties struct {
3430 android.SdkMemberPropertiesBase
3431
Paul Duffine8409952022-09-22 16:24:46 +01003432 // Stem name for files in the sdk snapshot.
3433 //
3434 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3435 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3436 //
3437 // This property is marked as keep so that it will be kept in all instances of this struct, will
3438 // not be cleared but will be copied to common structs. That is needed because this field is used
3439 // to construct many file names for other parts of this struct and so it needs to be present in
3440 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3441 // be unavailable for generating file names if there were other properties that were still set.
3442 Stem string `sdk:"keep"`
3443
Paul Duffindd46f712020-02-10 13:37:10 +00003444 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003445 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003446
Paul Duffin3d1248c2020-04-09 00:10:17 +01003447 // The Java stubs source files.
3448 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003449
3450 // The naming scheme.
3451 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003452
3453 // True if the java_sdk_library_import is for a shared library, false
3454 // otherwise.
3455 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003456
Paul Duffin1267d872021-04-16 17:21:36 +01003457 // True if the stub imports should produce dex jars.
3458 Compile_dex *bool
3459
Paul Duffina2ae7e02020-09-11 11:55:00 +01003460 // The paths to the doctag files to add to the prebuilt.
3461 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003462
3463 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003464
3465 // Signals that this shared library is part of the bootclasspath starting
3466 // on the version indicated in this attribute.
3467 //
3468 // This will make platforms at this level and above to ignore
3469 // <uses-library> tags with this library name because the library is already
3470 // available
3471 On_bootclasspath_since *string
3472
3473 // Signals that this shared library was part of the bootclasspath before
3474 // (but not including) the version indicated in this attribute.
3475 //
3476 // The system will automatically add a <uses-library> tag with this library to
3477 // apps that target any SDK less than the version indicated in this attribute.
3478 On_bootclasspath_before *string
3479
3480 // Indicates that PackageManager should ignore this shared library if the
3481 // platform is below the version indicated in this attribute.
3482 //
3483 // This means that the device won't recognise this library as installed.
3484 Min_device_sdk *string
3485
3486 // Indicates that PackageManager should ignore this shared library if the
3487 // platform is above the version indicated in this attribute.
3488 //
3489 // This means that the device won't recognise this library as installed.
3490 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003491
3492 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003493}
3494
3495type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003496 Jars android.Paths
3497 StubsSrcJar android.Path
3498 CurrentApiFile android.Path
3499 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003500 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003501 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003502}
3503
3504func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3505 sdk := variant.(*SdkLibrary)
3506
Paul Duffine8409952022-09-22 16:24:46 +01003507 // Copy the stem name for files in the sdk snapshot.
3508 s.Stem = sdk.distStem()
3509
Paul Duffin106a3a42022-01-27 16:39:06 +00003510 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003511 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003512 paths := sdk.findScopePaths(apiScope)
3513 if paths == nil {
3514 continue
3515 }
3516
Paul Duffindd46f712020-02-10 13:37:10 +00003517 jars := paths.stubsImplPath
3518 if len(jars) > 0 {
3519 properties := scopeProperties{}
3520 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003521 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003522 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003523 if paths.currentApiFilePath.Valid() {
3524 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3525 }
3526 if paths.removedApiFilePath.Valid() {
3527 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3528 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003529 // The annotations zip is only available for modules that set annotations_enabled: true.
3530 if paths.annotationsZip.Valid() {
3531 properties.AnnotationsZip = paths.annotationsZip.Path()
3532 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003533 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003534 }
3535 }
3536
Paul Duffindfa131e2020-05-15 20:37:11 +01003537 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003538 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003539 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003540 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003541 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003542 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3543 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3544 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3545 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003546
3547 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3548 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3549 }
Paul Duffindd46f712020-02-10 13:37:10 +00003550}
3551
3552func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003553 if s.Naming_scheme != nil {
3554 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3555 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003556 if s.Shared_library != nil {
3557 propertySet.AddProperty("shared_library", *s.Shared_library)
3558 }
Paul Duffin1267d872021-04-16 17:21:36 +01003559 if s.Compile_dex != nil {
3560 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3561 }
Paul Duffin869de142021-07-15 14:14:41 +01003562 if len(s.Permitted_packages) > 0 {
3563 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3564 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003565 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3566 if s.DexPreoptProfileGuided != nil {
3567 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3568 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003569
Paul Duffine8409952022-09-22 16:24:46 +01003570 stem := s.Stem
3571
Paul Duffindd46f712020-02-10 13:37:10 +00003572 for _, apiScope := range allApiScopes {
3573 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003574 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003575
Paul Duffin958806b2022-05-16 13:10:47 +00003576 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003577
Paul Duffindd46f712020-02-10 13:37:10 +00003578 var jars []string
3579 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003580 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003581 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3582 jars = append(jars, dest)
3583 }
3584 scopeSet.AddProperty("jars", jars)
3585
Paul Duffin22628d52021-05-12 23:13:22 +01003586 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3587 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003588 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003589 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3590 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3591 } else {
3592 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3593 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003594 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003595 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3596 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3597 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003598
Paul Duffin1fd005d2020-04-09 01:08:11 +01003599 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003600 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003601 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3602 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3603 }
3604
3605 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003606 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003607 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003608 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3609 }
3610
Anton Hanssond78eb762021-09-21 15:25:12 +01003611 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003612 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003613 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3614 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3615 }
3616
Paul Duffindd46f712020-02-10 13:37:10 +00003617 if properties.SdkVersion != "" {
3618 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3619 }
3620 }
3621 }
3622
Paul Duffina2ae7e02020-09-11 11:55:00 +01003623 if len(s.Doctag_paths) > 0 {
3624 dests := []string{}
3625 for _, p := range s.Doctag_paths {
3626 dest := filepath.Join("doctags", p.Rel())
3627 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3628 dests = append(dests, dest)
3629 }
3630 propertySet.AddProperty("doctag_files", dests)
3631 }
Paul Duffindd46f712020-02-10 13:37:10 +00003632}