blob: 29da28d4316aaf34269231e48f88839fc0fc671b [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jiyong Parkc678ad32018-04-10 13:07:10 +090018 "fmt"
19 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090020 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010021 "reflect"
Paul Duffin46dc45a2020-05-14 15:39:10 +010022 "regexp"
Jiyong Park82484c02018-04-23 21:41:26 +090023 "sort"
Jiyong Parkc678ad32018-04-10 13:07:10 +090024 "strings"
Jiyong Park82484c02018-04-23 21:41:26 +090025 "sync"
Jiyong Parkc678ad32018-04-10 13:07:10 +090026
Paul Duffind1b3a922020-01-22 11:57:20 +000027 "github.com/google/blueprint"
Jiyong Parkc678ad32018-04-10 13:07:10 +090028 "github.com/google/blueprint/proptools"
Paul Duffin46a26a82020-04-07 19:27:04 +010029
30 "android/soong/android"
Ulya Trafimovichdbf31662020-12-17 12:07:54 +000031 "android/soong/dexpreopt"
Jiyong Parkc678ad32018-04-10 13:07:10 +090032)
33
Jooyung Han58f26ab2019-12-18 15:34:32 +090034const (
Pedro Loureiro9956e5e2021-09-07 17:21:59 +000035 sdkXmlFileSuffix = ".xml"
Jiyong Parkc678ad32018-04-10 13:07:10 +090036)
37
Paul Duffind1b3a922020-01-22 11:57:20 +000038// A tag to associated a dependency with a specific api scope.
39type scopeDependencyTag struct {
40 blueprint.BaseDependencyTag
41 name string
42 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010043
44 // Function for extracting appropriate path information from the dependency.
Colin Crossdcf71b22021-02-01 13:59:03 -080045 depInfoExtractor func(paths *scopePaths, ctx android.ModuleContext, dep android.Module) error
Paul Duffinc8782502020-04-29 20:45:27 +010046}
47
48// Extract tag specific information from the dependency.
49func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
Colin Crossdcf71b22021-02-01 13:59:03 -080050 err := tag.depInfoExtractor(paths, ctx, dep)
Paul Duffinc8782502020-04-29 20:45:27 +010051 if err != nil {
52 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
53 }
Paul Duffind1b3a922020-01-22 11:57:20 +000054}
55
Paul Duffin80342d72020-06-26 22:08:43 +010056var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
57
58func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
59 return false
60}
61
Paul Duffind1b3a922020-01-22 11:57:20 +000062// Provides information about an api scope, e.g. public, system, test.
63type apiScope struct {
64 // The name of the api scope, e.g. public, system, test
65 name string
66
Paul Duffin97b53b82020-05-05 14:40:52 +010067 // The api scope that this scope extends.
Paul Duffind0b9fca2022-09-30 18:11:41 +010068 //
69 // This organizes the scopes into an extension hierarchy.
70 //
71 // If set this means that the API provided by this scope includes the API provided by the scope
72 // set in this field.
Paul Duffin97b53b82020-05-05 14:40:52 +010073 extends *apiScope
74
Paul Duffind0b9fca2022-09-30 18:11:41 +010075 // The next api scope that a library that uses this scope can access.
76 //
77 // This organizes the scopes into an access hierarchy.
78 //
79 // If set this means that a library that can access this API can also access the API provided by
80 // the scope set in this field.
81 //
82 // A module that sets sdk_version: "<scope>_current" should have access to the <scope> API of
83 // every java_sdk_library that it depends on. If the library does not provide an API for <scope>
84 // then it will traverse up this access hierarchy to find an API that it does provide.
85 //
86 // If this is not set then it defaults to the scope set in extends.
87 canAccess *apiScope
88
Paul Duffin3375e352020-04-28 10:44:03 +010089 // The legacy enabled status for a specific scope can be dependent on other
90 // properties that have been specified on the library so it is provided by
91 // a function that can determine the status by examining those properties.
92 legacyEnabledStatus func(module *SdkLibrary) bool
93
94 // The default enabled status for non-legacy behavior, which is triggered by
95 // explicitly enabling at least one api scope.
96 defaultEnabledStatus bool
97
98 // Gets a pointer to the scope specific properties.
99 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
100
Paul Duffin46a26a82020-04-07 19:27:04 +0100101 // The name of the field in the dynamically created structure.
102 fieldName string
103
Paul Duffin6b836ba2020-05-13 19:19:49 +0100104 // The name of the property in the java_sdk_library_import
105 propertyName string
106
Jihoon Kangb7431552024-01-22 19:40:08 +0000107 // The tag to use to depend on the prebuilt stubs library module
108 prebuiltStubsTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000109
Jihoon Kangbd093452023-12-26 19:08:01 +0000110 // The tag to use to depend on the everything stubs library module.
111 everythingStubsTag scopeDependencyTag
112
113 // The tag to use to depend on the exportable stubs library module.
114 exportableStubsTag scopeDependencyTag
115
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100116 // The tag to use to depend on the stubs source module (if separate from the API module).
117 stubsSourceTag scopeDependencyTag
118
119 // The tag to use to depend on the API file generating module (if separate from the stubs source module).
120 apiFileTag scopeDependencyTag
121
Paul Duffinc8782502020-04-29 20:45:27 +0100122 // The tag to use to depend on the stubs source and API module.
123 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000124
Paul Duffin958806b2022-05-16 13:10:47 +0000125 // The tag to use to depend on the module that provides the latest version of the API .txt file.
126 latestApiModuleTag scopeDependencyTag
127
128 // The tag to use to depend on the module that provides the latest version of the API removed.txt
129 // file.
130 latestRemovedApiModuleTag scopeDependencyTag
131
Paul Duffind1b3a922020-01-22 11:57:20 +0000132 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
133 apiFilePrefix string
134
Paul Duffind0b9fca2022-09-30 18:11:41 +0100135 // The scope specific suffix to add to the sdk library module name to construct a scope specific
Paul Duffind1b3a922020-01-22 11:57:20 +0000136 // module name.
137 moduleSuffix string
138
Paul Duffind1b3a922020-01-22 11:57:20 +0000139 // SDK version that the stubs library is built against. Note that this is always
140 // *current. Older stubs library built with a numbered SDK version is created from
141 // the prebuilt jar.
142 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100143
Paul Duffin15f34ef2020-07-20 18:04:44 +0100144 // The annotation that identifies this API level, empty for the public API scope.
145 annotation string
146
Paul Duffin1fb487d2020-04-07 18:50:10 +0100147 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100148 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100149 // This is not used directly but is used to construct the droidstubsArgs.
150 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100151
Paul Duffin15f34ef2020-07-20 18:04:44 +0100152 // The args that must be passed to droidstubs to generate the API and stubs source
153 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100154 //
155 // The API only includes the additional members that this scope adds over the scope
156 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100157 //
158 // The stubs source must include the definitions of everything that is in this
159 // api scope and all the scopes that this one extends.
160 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100161
Anton Hansson6478ac12020-05-02 11:19:36 +0100162 // Whether the api scope can be treated as unstable, and should skip compat checks.
163 unstable bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000164
165 // Represents the SDK kind of this scope.
166 kind android.SdkKind
Paul Duffind1b3a922020-01-22 11:57:20 +0000167}
168
169// Initialize a scope, creating and adding appropriate dependency tags
170func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100171 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100172 scopeByName[name] = scope
173 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100174 scope.propertyName = strings.ReplaceAll(name, "-", "_")
175 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Jihoon Kangb7431552024-01-22 19:40:08 +0000176 scope.prebuiltStubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100177 name: name + "-stubs",
178 apiScope: scope,
179 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000180 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000181 scope.everythingStubsTag = scopeDependencyTag{
182 name: name + "-stubs-everything",
183 apiScope: scope,
184 depInfoExtractor: (*scopePaths).extractEverythingStubsLibraryInfoFromDependency,
185 }
186 scope.exportableStubsTag = scopeDependencyTag{
187 name: name + "-stubs-exportable",
188 apiScope: scope,
189 depInfoExtractor: (*scopePaths).extractExportableStubsLibraryInfoFromDependency,
190 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100191 scope.stubsSourceTag = scopeDependencyTag{
192 name: name + "-stubs-source",
193 apiScope: scope,
194 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
195 }
196 scope.apiFileTag = scopeDependencyTag{
197 name: name + "-api",
198 apiScope: scope,
199 depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
200 }
Paul Duffinc8782502020-04-29 20:45:27 +0100201 scope.stubsSourceAndApiTag = scopeDependencyTag{
202 name: name + "-stubs-source-and-api",
203 apiScope: scope,
204 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000205 }
Paul Duffin958806b2022-05-16 13:10:47 +0000206 scope.latestApiModuleTag = scopeDependencyTag{
207 name: name + "-latest-api",
208 apiScope: scope,
209 depInfoExtractor: (*scopePaths).extractLatestApiPath,
210 }
211 scope.latestRemovedApiModuleTag = scopeDependencyTag{
212 name: name + "-latest-removed-api",
213 apiScope: scope,
214 depInfoExtractor: (*scopePaths).extractLatestRemovedApiPath,
215 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100216
217 // To get the args needed to generate the stubs source append all the args from
218 // this scope and all the scopes it extends as each set of args adds additional
219 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100220 var scopeSpecificArgs []string
221 if scope.annotation != "" {
222 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100223 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100224 for s := scope; s != nil; s = s.extends {
225 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100226
Paul Duffin15f34ef2020-07-20 18:04:44 +0100227 // Ensure that the generated stubs includes all the API elements from the API scope
228 // that this scope extends.
229 if s != scope && s.annotation != "" {
230 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
231 }
232 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100233
Paul Duffind0b9fca2022-09-30 18:11:41 +0100234 // By default, a library that can access a scope can also access the scope it extends.
235 if scope.canAccess == nil {
236 scope.canAccess = scope.extends
237 }
238
Paul Duffin15f34ef2020-07-20 18:04:44 +0100239 // Escape any special characters in the arguments. This is needed because droidstubs
240 // passes these directly to the shell command.
241 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100242
Paul Duffind1b3a922020-01-22 11:57:20 +0000243 return scope
244}
245
Anton Hansson08f476b2021-04-07 15:32:19 +0100246func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
247 return ".stubs" + scope.moduleSuffix
248}
249
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000250func (scope *apiScope) exportableStubsLibraryModuleNameSuffix() string {
251 return ".stubs.exportable" + scope.moduleSuffix
252}
253
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000254func (scope *apiScope) apiLibraryModuleName(baseName string) string {
255 return scope.stubsLibraryModuleName(baseName) + ".from-text"
256}
257
Jihoon Kang1147b312023-06-08 23:25:57 +0000258func (scope *apiScope) sourceStubLibraryModuleName(baseName string) string {
259 return scope.stubsLibraryModuleName(baseName) + ".from-source"
260}
261
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000262func (scope *apiScope) exportableSourceStubsLibraryModuleName(baseName string) string {
263 return scope.exportableStubsLibraryModuleName(baseName) + ".from-source"
264}
265
Paul Duffinc3091c82020-05-08 14:16:20 +0100266func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100267 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000268}
269
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000270func (scope *apiScope) exportableStubsLibraryModuleName(baseName string) string {
271 return baseName + scope.exportableStubsLibraryModuleNameSuffix()
272}
273
Paul Duffinc8782502020-04-29 20:45:27 +0100274func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100275 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000276}
277
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100278func (scope *apiScope) apiModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100279 return baseName + ".api" + scope.moduleSuffix
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100280}
281
Paul Duffin3375e352020-04-28 10:44:03 +0100282func (scope *apiScope) String() string {
283 return scope.name
284}
285
Paul Duffin958806b2022-05-16 13:10:47 +0000286// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
287// be stored.
288func (scope *apiScope) snapshotRelativeDir() string {
289 return filepath.Join("sdk_library", scope.name)
290}
291
292// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
293// library.
294func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
295 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
296}
297
298// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
299// named library.
300func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
301 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
302}
303
Paul Duffind1b3a922020-01-22 11:57:20 +0000304type apiScopes []*apiScope
305
306func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
307 var list []string
308 for _, scope := range scopes {
309 list = append(list, accessor(scope))
310 }
311 return list
312}
313
Jihoon Kanga96a7b12023-09-20 23:43:32 +0000314// Method that maps the apiScopes properties to the index of each apiScopes elements.
315// apiScopes property to be used as the key can be specified with the input accessor.
316// Only a string property of apiScope can be used as the key of the map.
317func (scopes apiScopes) MapToIndex(accessor func(*apiScope) string) map[string]int {
318 ret := make(map[string]int)
319 for i, scope := range scopes {
320 ret[accessor(scope)] = i
321 }
322 return ret
323}
324
Jiyong Parkc678ad32018-04-10 13:07:10 +0900325var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100326 scopeByName = make(map[string]*apiScope)
327 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000328 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100329 name: "public",
330
331 // Public scope is enabled by default for both legacy and non-legacy modes.
332 legacyEnabledStatus: func(module *SdkLibrary) bool {
333 return true
334 },
335 defaultEnabledStatus: true,
336
337 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
338 return &module.sdkLibraryProperties.Public
339 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000340 sdkVersion: "current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000341 kind: android.SdkPublic,
Paul Duffind1b3a922020-01-22 11:57:20 +0000342 })
343 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100344 name: "system",
345 extends: apiScopePublic,
346 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
347 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
348 return &module.sdkLibraryProperties.System
349 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100350 apiFilePrefix: "system-",
351 moduleSuffix: ".system",
352 sdkVersion: "system_current",
353 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000354 kind: android.SdkSystem,
Paul Duffind1b3a922020-01-22 11:57:20 +0000355 })
356 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100357 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100358 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100359 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
360 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
361 return &module.sdkLibraryProperties.Test
362 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100363 apiFilePrefix: "test-",
364 moduleSuffix: ".test",
365 sdkVersion: "test_current",
366 annotation: "android.annotation.TestApi",
367 unstable: true,
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000368 kind: android.SdkTest,
Paul Duffind1b3a922020-01-22 11:57:20 +0000369 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100370 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100371 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100372 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100373 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100374 //
375 // Enabling this would break existing usages.
376 legacyEnabledStatus: func(module *SdkLibrary) bool {
377 return false
378 },
379 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
380 return &module.sdkLibraryProperties.Module_lib
381 },
382 apiFilePrefix: "module-lib-",
383 moduleSuffix: ".module_lib",
384 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100385 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000386 kind: android.SdkModule,
Paul Duffin8f265b92020-04-28 14:13:56 +0100387 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100388 apiScopeSystemServer = initApiScope(&apiScope{
389 name: "system-server",
390 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100391
392 // The system-server scope can access the module-lib scope.
393 //
394 // A module that provides a system-server API is appended to the standard bootclasspath that is
395 // used by the system server. So, it should be able to access module-lib APIs provided by
396 // libraries on the bootclasspath.
397 canAccess: apiScopeModuleLib,
398
Paul Duffin0c5bae52020-06-02 13:00:08 +0100399 // The system-server scope is disabled by default in legacy mode.
400 //
401 // Enabling this would break existing usages.
402 legacyEnabledStatus: func(module *SdkLibrary) bool {
403 return false
404 },
405 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
406 return &module.sdkLibraryProperties.System_server
407 },
408 apiFilePrefix: "system-server-",
409 moduleSuffix: ".system_server",
410 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100411 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
412 extraArgs: []string{
413 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100414 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100415 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100416 },
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000417 kind: android.SdkSystemServer,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100418 })
Paul Duffind1b3a922020-01-22 11:57:20 +0000419 allApiScopes = apiScopes{
420 apiScopePublic,
421 apiScopeSystem,
422 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100423 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100424 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000425 }
Jihoon Kang0c705a42023-08-02 06:44:57 +0000426 apiLibraryAdditionalProperties = map[string]struct {
427 FullApiSurfaceStubLib string
428 AdditionalApiContribution string
429 }{
430 "legacy.i18n.module.platform.api": {
431 FullApiSurfaceStubLib: "legacy.core.platform.api.stubs",
432 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
433 },
434 "stable.i18n.module.platform.api": {
435 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
436 AdditionalApiContribution: "i18n.module.public.api.stubs.source.api.contribution",
437 },
438 "conscrypt.module.platform.api": {
439 FullApiSurfaceStubLib: "stable.core.platform.api.stubs",
440 AdditionalApiContribution: "conscrypt.module.public.api.stubs.source.api.contribution",
441 },
442 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900443)
444
Jiyong Park82484c02018-04-23 21:41:26 +0900445var (
446 javaSdkLibrariesLock sync.Mutex
447)
448
Jiyong Parkc678ad32018-04-10 13:07:10 +0900449// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900450// 1) disallowing linking to the runtime shared lib
451// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900452
453func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000454 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900455
Jiyong Park82484c02018-04-23 21:41:26 +0900456 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
457 javaSdkLibraries := javaSdkLibraries(ctx.Config())
458 sort.Strings(*javaSdkLibraries)
459 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
460 })
Paul Duffindd46f712020-02-10 13:37:10 +0000461
462 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100463 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900464}
465
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000466func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
467 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
468 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
469}
470
Paul Duffin3375e352020-04-28 10:44:03 +0100471// Properties associated with each api scope.
472type ApiScopeProperties struct {
473 // Indicates whether the api surface is generated.
474 //
475 // If this is set for any scope then all scopes must explicitly specify if they
476 // are enabled. This is to prevent new usages from depending on legacy behavior.
477 //
478 // Otherwise, if this is not set for any scope then the default behavior is
479 // scope specific so please refer to the scope specific property documentation.
480 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100481
482 // The sdk_version to use for building the stubs.
483 //
484 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000485 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100486 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000487 // will be none. This is used for java_sdk_library instances that are used
488 // to create stubs that contribute to the core_current sdk version.
489 // 2) Otherwise, it is assumed that this library extends but does not
490 // contribute directly to a specific sdk_version and so this uses the
491 // sdk_version appropriate for the api scope. e.g. public will use
492 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100493 //
494 // This does not affect the sdk_version used for either generating the stubs source
495 // or the API file. They both have to use the same sdk_version as is used for
496 // compiling the implementation library.
497 Sdk_version *string
Mark White9421c4c2023-08-10 00:07:03 +0000498
499 // Extra libs used when compiling stubs for this scope.
500 Libs []string
Paul Duffin3375e352020-04-28 10:44:03 +0100501}
502
Jiyong Parkc678ad32018-04-10 13:07:10 +0900503type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100504 // List of source files that are needed to compile the API, but are not part of runtime library.
505 Api_srcs []string `android:"arch_variant"`
506
Paul Duffin5df79302020-05-16 15:52:12 +0100507 // Visibility for impl library module. If not specified then defaults to the
508 // visibility property.
509 Impl_library_visibility []string
510
Paul Duffin4911a892020-04-29 23:35:13 +0100511 // Visibility for stubs library modules. If not specified then defaults to the
512 // visibility property.
513 Stubs_library_visibility []string
514
515 // Visibility for stubs source modules. If not specified then defaults to the
516 // visibility property.
517 Stubs_source_visibility []string
518
Anton Hansson7f66efa2020-10-08 14:47:23 +0100519 // List of Java libraries that will be in the classpath when building the implementation lib
520 Impl_only_libs []string `android:"arch_variant"`
521
Paul Duffin77590a82022-04-28 14:13:30 +0000522 // List of Java libraries that will included in the implementation lib.
523 Impl_only_static_libs []string `android:"arch_variant"`
524
Sundong Ahnf043cf62018-06-25 16:04:37 +0900525 // List of Java libraries that will be in the classpath when building stubs
526 Stub_only_libs []string `android:"arch_variant"`
527
Anton Hanssondae54cd2021-04-21 16:30:10 +0100528 // List of Java libraries that will included in stub libraries
529 Stub_only_static_libs []string `android:"arch_variant"`
530
Paul Duffin7a586d32019-12-30 17:09:34 +0000531 // list of package names that will be documented and publicized as API.
532 // This allows the API to be restricted to a subset of the source files provided.
533 // If this is unspecified then all the source files will be treated as being part
534 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900535 Api_packages []string
536
Jiyong Park5a2c9d72018-05-01 22:25:41 +0900537 // list of package names that must be hidden from the API
538 Hidden_api_packages []string
539
Paul Duffin749f98f2019-12-30 17:23:46 +0000540 // the relative path to the directory containing the api specification files.
541 // Defaults to "api".
542 Api_dir *string
543
Paul Duffindfa131e2020-05-15 20:37:11 +0100544 // Determines whether a runtime implementation library is built; defaults to false.
545 //
546 // 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 +0200547 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000548 Api_only *bool
549
Paul Duffin11512472019-02-11 15:55:17 +0000550 // local files that are used within user customized droiddoc options.
551 Droiddoc_option_files []string
552
Spandan Das93e95992021-07-29 18:26:39 +0000553 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000554 // Available variables for substitution:
555 //
556 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900557 Droiddoc_options []string
558
Paul Duffine22c2ab2020-05-20 19:35:27 +0100559 // is set to true, Metalava will allow framework SDK to contain annotations.
560 Annotations_enabled *bool
561
Sundong Ahn054b19a2018-10-19 13:46:09 +0900562 // a list of top-level directories containing files to merge qualifier annotations
563 // (i.e. those intended to be included in the stubs written) from.
564 Merge_annotations_dirs []string
565
566 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
567 Merge_inclusion_annotations_dirs []string
568
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000569 // If set to true then don't create dist rules.
570 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900571
Paul Duffin31310252020-11-20 21:26:20 +0000572 // The stem for the artifacts that are copied to the dist, if not specified
573 // then defaults to the base module name.
574 //
575 // For each scope the following artifacts are copied to the apistubs/<scope>
576 // directory in the dist.
577 // * stubs impl jar -> <dist-stem>.jar
578 // * API specification file -> api/<dist-stem>.txt
579 // * Removed API specification file -> api/<dist-stem>-removed.txt
580 //
581 // Also used to construct the name of the filegroup (created by prebuilt_apis)
582 // that references the latest released API and remove API specification files.
583 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
584 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800585 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000586 Dist_stem *string
587
Colin Cross986b69a2021-06-01 13:13:40 -0700588 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700589 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700590 // in the public Android SDK.
591 Dist_group *string
592
Anton Hanssondff2c782020-12-21 17:10:01 +0000593 // A compatibility mode that allows historical API-tracking files to not exist.
594 // Do not use.
595 Unsafe_ignore_missing_latest_api bool
596
Paul Duffin3375e352020-04-28 10:44:03 +0100597 // indicates whether system and test apis should be generated.
598 Generate_system_and_test_apis bool `blueprint:"mutated"`
599
600 // The properties specific to the public api scope
601 //
602 // Unless explicitly specified by using public.enabled the public api scope is
603 // enabled by default in both legacy and non-legacy mode.
604 Public ApiScopeProperties
605
606 // The properties specific to the system api scope
607 //
608 // In legacy mode the system api scope is enabled by default when sdk_version
609 // is set to something other than "none".
610 //
611 // In non-legacy mode the system api scope is disabled by default.
612 System ApiScopeProperties
613
614 // The properties specific to the test api scope
615 //
616 // In legacy mode the test api scope is enabled by default when sdk_version
617 // is set to something other than "none".
618 //
619 // In non-legacy mode the test api scope is disabled by default.
620 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000621
Paul Duffin0c5bae52020-06-02 13:00:08 +0100622 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100623 //
Zi Wangb2179e32023-01-31 15:53:30 -0800624 // Unless explicitly specified by using module_lib.enabled the module_lib api
625 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100626 Module_lib ApiScopeProperties
627
Paul Duffin0c5bae52020-06-02 13:00:08 +0100628 // The properties specific to the system-server api scope
629 //
Zi Wangb2179e32023-01-31 15:53:30 -0800630 // Unless explicitly specified by using system_server.enabled the
631 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100632 System_server ApiScopeProperties
633
Jiyong Park932cdfe2020-05-28 00:19:53 +0900634 // Determines if the stubs are preferred over the implementation library
635 // for linking, even when the client doesn't specify sdk_version. When this
636 // is set to true, such clients are provided with the widest API surface that
637 // this lib provides. Note however that this option doesn't affect the clients
638 // that are in the same APEX as this library. In that case, the clients are
639 // always linked with the implementation library. Default is false.
640 Default_to_stubs *bool
641
Paul Duffin160fe412020-05-10 19:32:20 +0100642 // Properties related to api linting.
643 Api_lint struct {
644 // Enable api linting.
645 Enabled *bool
Anton Hanssonfd1c0d22023-11-02 15:18:09 +0000646
647 // If API lint is enabled, this flag controls whether a set of legitimate lint errors
648 // are turned off. The default is true.
649 Legacy_errors_allowed *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100650 }
651
Jihoon Kang80456fd2023-11-15 19:22:14 +0000652 // Determines if the module contributes to any api surfaces.
653 // This property should be set to true only if the module is listed under
654 // frameworks-base-api.bootclasspath in frameworks/base/api/Android.bp.
655 // Otherwise, this property should be set to false.
656 // Defaults to false.
657 Contribute_to_android_api *bool
658
Jihoon Kang6592e872023-12-19 01:13:16 +0000659 // a list of aconfig_declarations module names that the stubs generated in this module
660 // depend on.
661 Aconfig_declarations []string
662
Jiyong Parkc678ad32018-04-10 13:07:10 +0900663 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100664 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900665}
666
Paul Duffin0f8faff2020-05-20 16:18:00 +0100667// Paths to outputs from java_sdk_library and java_sdk_library_import.
668//
669// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
670// OptionalPaths are always set by java_sdk_library but may not be set by
671// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000672type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100673 // The path (represented as Paths for convenience when returning) to the stubs header jar.
674 //
675 // That is the jar that is created by turbine.
676 stubsHeaderPath android.Paths
677
678 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
679 //
680 // This is not the implementation jar, it still only contains stubs.
681 stubsImplPath android.Paths
682
Paul Duffin1267d872021-04-16 17:21:36 +0100683 // The dex jar for the stubs.
684 //
685 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100686 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100687
Jihoon Kangbd093452023-12-26 19:08:01 +0000688 // The exportable dex jar for the stubs.
689 // This is not the implementation jar, it still only contains stubs.
690 // Includes unflagged apis and flagged apis enabled by release configurations.
691 exportableStubsDexJarPath OptionalDexJarPath
692
Paul Duffin0f8faff2020-05-20 16:18:00 +0100693 // The API specification file, e.g. system_current.txt.
694 currentApiFilePath android.OptionalPath
695
696 // The specification of API elements removed since the last release.
697 removedApiFilePath android.OptionalPath
698
699 // The stubs source jar.
700 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100701
702 // Extracted annotations.
703 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000704
705 // The path to the latest API file.
706 latestApiPath android.OptionalPath
707
708 // The path to the latest removed API file.
709 latestRemovedApiPath android.OptionalPath
Paul Duffind1b3a922020-01-22 11:57:20 +0000710}
711
Colin Crossdcf71b22021-02-01 13:59:03 -0800712func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Colin Cross313aa542023-12-13 13:47:44 -0800713 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800714 paths.stubsHeaderPath = lib.HeaderJars
715 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100716
717 libDep := dep.(UsesLibraryDependency)
Spandan Das59a4a2b2024-01-09 21:35:56 +0000718 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
Jihoon Kangbd093452023-12-26 19:08:01 +0000719 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
720 return nil
721 } else {
722 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
723 }
724}
725
726func (paths *scopePaths) extractEverythingStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
727 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
728 paths.stubsHeaderPath = lib.HeaderJars
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000729 if !ctx.Config().ReleaseHiddenApiExportableStubs() {
730 paths.stubsImplPath = lib.ImplementationJars
731 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000732
733 libDep := dep.(UsesLibraryDependency)
734 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
735 return nil
736 } else {
737 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
738 }
739}
740
741func (paths *scopePaths) extractExportableStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000742 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
743 if ctx.Config().ReleaseHiddenApiExportableStubs() {
744 paths.stubsImplPath = lib.ImplementationJars
745 }
746
Jihoon Kangbd093452023-12-26 19:08:01 +0000747 libDep := dep.(UsesLibraryDependency)
748 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
Paul Duffinc8782502020-04-29 20:45:27 +0100749 return nil
750 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800751 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100752 }
753}
754
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100755func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
756 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
757 action(apiStubsProvider)
Paul Duffinc8782502020-04-29 20:45:27 +0100758 return nil
759 } else {
760 return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
761 }
762}
763
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000764func (paths *scopePaths) treatDepAsExportableApiStubsProvider(dep android.Module, action func(provider ExportableApiStubsProvider)) error {
765 if exportableApiStubsProvider, ok := dep.(ExportableApiStubsProvider); ok {
766 action(exportableApiStubsProvider)
767 return nil
768 } else {
769 return fmt.Errorf("expected module that implements ExportableApiStubsSrcProvider, e.g. droidstubs")
770 }
771}
772
Paul Duffin0f8faff2020-05-20 16:18:00 +0100773func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
774 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
775 action(apiStubsProvider)
776 return nil
777 } else {
778 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
779 }
780}
781
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100782func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
Anton Hanssond78eb762021-09-21 15:25:12 +0100783 paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
Paul Duffin0f8faff2020-05-20 16:18:00 +0100784 paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
785 paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100786}
787
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000788func (paths *scopePaths) extractApiInfoFromExportableApiStubsProvider(provider ExportableApiStubsProvider) {
789 paths.annotationsZip = android.OptionalPathForPath(provider.ExportableAnnotationsZip())
790 paths.currentApiFilePath = android.OptionalPathForPath(provider.ExportableApiFilePath())
791 paths.removedApiFilePath = android.OptionalPathForPath(provider.ExportableRemovedApiFilePath())
792}
793
Colin Crossdcf71b22021-02-01 13:59:03 -0800794func (paths *scopePaths) extractApiInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100795 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
796 paths.extractApiInfoFromApiStubsProvider(provider)
797 })
798}
799
Paul Duffin0f8faff2020-05-20 16:18:00 +0100800func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
801 paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100802}
803
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000804func (paths *scopePaths) extractStubsSourceInfoFromExportableApiStubsProviders(provider ExportableApiStubsSrcProvider) {
805 paths.stubsSrcJar = android.OptionalPathForPath(provider.ExportableStubsSrcJar())
806}
807
Colin Crossdcf71b22021-02-01 13:59:03 -0800808func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100809 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100810 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
811 })
812}
813
Colin Crossdcf71b22021-02-01 13:59:03 -0800814func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000815 if ctx.Config().ReleaseHiddenApiExportableStubs() {
816 return paths.treatDepAsExportableApiStubsProvider(dep, func(provider ExportableApiStubsProvider) {
817 paths.extractApiInfoFromExportableApiStubsProvider(provider)
818 paths.extractStubsSourceInfoFromExportableApiStubsProviders(provider)
819 })
820 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100821 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
822 paths.extractApiInfoFromApiStubsProvider(provider)
823 paths.extractStubsSourceInfoFromApiStubsProviders(provider)
824 })
825}
826
Paul Duffin958806b2022-05-16 13:10:47 +0000827func extractSingleOptionalOutputPath(dep android.Module) (android.OptionalPath, error) {
828 var paths android.Paths
829 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
830 paths = sourceFileProducer.Srcs()
831 } else {
832 return android.OptionalPath{}, fmt.Errorf("module %q does not produce source files", dep)
833 }
834 if len(paths) != 1 {
835 return android.OptionalPath{}, fmt.Errorf("expected one path from %q, got %q", dep, paths)
836 }
837 return android.OptionalPathForPath(paths[0]), nil
838}
839
840func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
841 outputPath, err := extractSingleOptionalOutputPath(dep)
842 paths.latestApiPath = outputPath
843 return err
844}
845
846func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
847 outputPath, err := extractSingleOptionalOutputPath(dep)
848 paths.latestRemovedApiPath = outputPath
849 return err
850}
851
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100852type commonToSdkLibraryAndImportProperties struct {
Paul Duffin1b1e8062020-05-08 13:44:43 +0100853 // The naming scheme to use for the components that this module creates.
854 //
Paul Duffinee9ad5d2020-09-11 13:04:05 +0100855 // If not specified then it defaults to "default".
Paul Duffin1b1e8062020-05-08 13:44:43 +0100856 //
857 // This is a temporary mechanism to simplify conversion from separate modules for each
858 // component that follow a different naming pattern to the default one.
859 //
860 // TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100861 Naming_scheme *string
Paul Duffindfa131e2020-05-15 20:37:11 +0100862
863 // Specifies whether this module can be used as an Android shared library; defaults
864 // to true.
865 //
866 // An Android shared library is one that can be referenced in a <uses-library> element
867 // in an AndroidManifest.xml.
868 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100869
870 // Files containing information about supported java doc tags.
871 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000872
873 // Signals that this shared library is part of the bootclasspath starting
874 // on the version indicated in this attribute.
875 //
876 // This will make platforms at this level and above to ignore
877 // <uses-library> tags with this library name because the library is already
878 // available
879 On_bootclasspath_since *string
880
881 // Signals that this shared library was part of the bootclasspath before
882 // (but not including) the version indicated in this attribute.
883 //
884 // The system will automatically add a <uses-library> tag with this library to
885 // apps that target any SDK less than the version indicated in this attribute.
886 On_bootclasspath_before *string
887
888 // Indicates that PackageManager should ignore this shared library if the
889 // platform is below the version indicated in this attribute.
890 //
891 // This means that the device won't recognise this library as installed.
892 Min_device_sdk *string
893
894 // Indicates that PackageManager should ignore this shared library if the
895 // platform is above the version indicated in this attribute.
896 //
897 // This means that the device won't recognise this library as installed.
898 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100899}
900
Paul Duffin71b33cc2021-06-23 11:39:47 +0100901// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
902// embeds the commonToSdkLibraryAndImport struct.
903type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000904 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100905
906 BaseModuleName() string
907}
908
Paul Duffin56d44902020-01-31 13:36:25 +0000909// Common code between sdk library and sdk library import
910type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100911 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100912
Paul Duffin56d44902020-01-31 13:36:25 +0000913 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100914
915 namingScheme sdkLibraryComponentNamingScheme
916
Paul Duffindfa131e2020-05-15 20:37:11 +0100917 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100918
Paul Duffina2ae7e02020-09-11 11:55:00 +0100919 // Paths to commonSdkLibraryProperties.Doctag_files
920 doctagPaths android.Paths
921
Paul Duffin859fe962020-05-15 10:20:31 +0100922 // Functionality related to this being used as a component of a java_sdk_library.
923 EmbeddableSdkLibraryComponent
Paul Duffin56d44902020-01-31 13:36:25 +0000924}
925
Paul Duffin71b33cc2021-06-23 11:39:47 +0100926func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
927 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100928
Paul Duffin71b33cc2021-06-23 11:39:47 +0100929 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100930
931 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100932 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100933}
934
935func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Paul Duffindfa131e2020-05-15 20:37:11 +0100936 schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
Paul Duffin1b1e8062020-05-08 13:44:43 +0100937 switch schemeProperty {
938 case "default":
939 c.namingScheme = &defaultNamingScheme{}
940 default:
941 ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
942 return false
943 }
944
Paul Duffin3f0290e2021-06-30 18:25:36 +0100945 namePtr := proptools.StringPtr(c.module.BaseModuleName())
946 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
947
Paul Duffindfa131e2020-05-15 20:37:11 +0100948 // Only track this sdk library if this can be used as a shared library.
949 if c.sharedLibrary() {
950 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100951 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100952 }
Paul Duffin859fe962020-05-15 10:20:31 +0100953
Paul Duffin1b1e8062020-05-08 13:44:43 +0100954 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100955}
956
Paul Duffinea8f8082021-06-24 13:25:57 +0100957// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
958// method.
959func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
960 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
961 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
962 // the APEX and so it needs a unique variation per APEX.
963 return c.sharedLibrary()
964}
965
Paul Duffina2ae7e02020-09-11 11:55:00 +0100966func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
967 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
968}
969
Paul Duffineedc5d52020-06-12 17:46:39 +0100970// Module name of the runtime implementation library
971func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100972 return c.module.BaseModuleName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100973}
974
975// Module name of the XML file for the lib
976func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100977 return c.module.BaseModuleName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100978}
979
Paul Duffinc3091c82020-05-08 14:16:20 +0100980// Name of the java_library module that compiles the stubs source.
981func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100982 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000983 return c.namingScheme.stubsLibraryModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100984}
985
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000986// Name of the java_library module that compiles the exportable stubs source.
987func (c *commonToSdkLibraryAndImport) exportableStubsLibraryModuleName(apiScope *apiScope) string {
988 baseName := c.module.BaseModuleName()
989 return c.namingScheme.exportableStubsLibraryModuleName(apiScope, baseName)
990}
991
Paul Duffinc3091c82020-05-08 14:16:20 +0100992// Name of the droidstubs module that generates the stubs source and may also
993// generate/check the API.
994func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Paul Duffinb97b1572021-04-29 21:50:40 +0100995 baseName := c.module.BaseModuleName()
Paul Duffin21787622022-11-25 12:48:20 +0000996 return c.namingScheme.stubsSourceModuleName(apiScope, baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100997}
998
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000999// Name of the java_api_library module that generates the from-text stubs source
1000// and compiles to a jar file.
1001func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
1002 baseName := c.module.BaseModuleName()
1003 return c.namingScheme.apiLibraryModuleName(apiScope, baseName)
1004}
1005
Jihoon Kang1147b312023-06-08 23:25:57 +00001006// Name of the java_library module that compiles the stubs
1007// generated from source Java files.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001008func (c *commonToSdkLibraryAndImport) sourceStubsLibraryModuleName(apiScope *apiScope) string {
Jihoon Kang1147b312023-06-08 23:25:57 +00001009 baseName := c.module.BaseModuleName()
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001010 return c.namingScheme.sourceStubsLibraryModuleName(apiScope, baseName)
1011}
1012
1013// Name of the java_library module that compiles the exportable stubs
1014// generated from source Java files.
1015func (c *commonToSdkLibraryAndImport) exportableSourceStubsLibraryModuleName(apiScope *apiScope) string {
1016 baseName := c.module.BaseModuleName()
1017 return c.namingScheme.exportableSourceStubsLibraryModuleName(apiScope, baseName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001018}
1019
Paul Duffin46dc45a2020-05-14 15:39:10 +01001020// The component names for different outputs of the java_sdk_library.
1021//
1022// They are similar to the names used for the child modules it creates
1023const (
1024 stubsSourceComponentName = "stubs.source"
1025
1026 apiTxtComponentName = "api.txt"
1027
1028 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +01001029
1030 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +01001031)
1032
1033// A regular expression to match tags that reference a specific stubs component.
1034//
1035// It will only match if given a valid scope and a valid component. It is verfy strict
1036// to ensure it does not accidentally match a similar looking tag that should be processed
1037// by the embedded Library.
1038var tagSplitter = func() *regexp.Regexp {
1039 // Given a list of literal string items returns a regular expression that will
1040 // match any one of the items.
1041 choice := func(items ...string) string {
1042 return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
1043 }
1044
1045 // Regular expression to match one of the scopes.
1046 scopesRegexp := choice(allScopeNames...)
1047
1048 // Regular expression to match one of the components.
Anton Hanssond78eb762021-09-21 15:25:12 +01001049 componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001050
1051 // Regular expression to match any combination of one scope and one component.
1052 return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
1053}()
1054
1055// For OutputFileProducer interface
1056//
Anton Hanssond78eb762021-09-21 15:25:12 +01001057// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001058func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
1059 if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
1060 scopeName := groups[1]
1061 component := groups[2]
1062
1063 if scope, ok := scopeByName[scopeName]; ok {
1064 paths := c.findScopePaths(scope)
1065 if paths == nil {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001066 return nil, fmt.Errorf("%q does not provide api scope %s", c.module.BaseModuleName(), scopeName)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001067 }
1068
1069 switch component {
1070 case stubsSourceComponentName:
1071 if paths.stubsSrcJar.Valid() {
1072 return android.Paths{paths.stubsSrcJar.Path()}, nil
1073 }
1074
1075 case apiTxtComponentName:
1076 if paths.currentApiFilePath.Valid() {
1077 return android.Paths{paths.currentApiFilePath.Path()}, nil
1078 }
1079
1080 case removedApiTxtComponentName:
1081 if paths.removedApiFilePath.Valid() {
1082 return android.Paths{paths.removedApiFilePath.Path()}, nil
1083 }
Anton Hanssond78eb762021-09-21 15:25:12 +01001084
1085 case annotationsComponentName:
1086 if paths.annotationsZip.Valid() {
1087 return android.Paths{paths.annotationsZip.Path()}, nil
1088 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001089 }
1090
1091 return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
1092 } else {
1093 return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
1094 }
1095
1096 } else {
Paul Duffina2ae7e02020-09-11 11:55:00 +01001097 switch tag {
1098 case ".doctags":
1099 if c.doctagPaths != nil {
1100 return c.doctagPaths, nil
1101 } else {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001102 return nil, fmt.Errorf("no doctag_files specified on %s", c.module.BaseModuleName())
Paul Duffina2ae7e02020-09-11 11:55:00 +01001103 }
1104 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001105 return nil, nil
1106 }
1107}
1108
Paul Duffin803a9562020-05-20 11:52:25 +01001109func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001110 if c.scopePaths == nil {
1111 c.scopePaths = make(map[*apiScope]*scopePaths)
1112 }
1113 paths := c.scopePaths[scope]
1114 if paths == nil {
1115 paths = &scopePaths{}
1116 c.scopePaths[scope] = paths
1117 }
1118
1119 return paths
1120}
1121
Paul Duffin803a9562020-05-20 11:52:25 +01001122func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1123 if c.scopePaths == nil {
1124 return nil
1125 }
1126
1127 return c.scopePaths[scope]
1128}
1129
1130// If this does not support the requested api scope then find the closest available
1131// scope it does support. Returns nil if no such scope is available.
1132func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001133 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001134 if paths := c.findScopePaths(s); paths != nil {
1135 return paths
1136 }
1137 }
1138
1139 // This should never happen outside tests as public should be the base scope for every
1140 // scope and is enabled by default.
1141 return nil
1142}
1143
Jiyong Parkf1691d22021-03-29 20:11:58 +09001144func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffinb05d4292020-05-20 12:19:10 +01001145
1146 // If a specific numeric version has been requested then use prebuilt versions of the sdk.
Jiyong Park54105c42021-03-31 18:17:53 +09001147 if !sdkVersion.ApiLevel.IsPreview() {
Paul Duffin71b33cc2021-06-23 11:39:47 +01001148 return PrebuiltJars(ctx, c.module.BaseModuleName(), sdkVersion)
Paul Duffinb05d4292020-05-20 12:19:10 +01001149 }
1150
Paul Duffin1267d872021-04-16 17:21:36 +01001151 paths := c.selectScopePaths(ctx, sdkVersion.Kind)
1152 if paths == nil {
1153 return nil
1154 }
1155
1156 return paths.stubsHeaderPath
1157}
1158
1159// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1160//
1161// If the module does not support the specific kind then it will return the *scopePaths for the
1162// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1163// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1164func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001165 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001166
Paul Duffin803a9562020-05-20 11:52:25 +01001167 paths := c.findClosestScopePath(apiScope)
1168 if paths == nil {
1169 var scopes []string
1170 for _, s := range allApiScopes {
1171 if c.findScopePaths(s) != nil {
1172 scopes = append(scopes, s.name)
1173 }
1174 }
Paul Duffin71b33cc2021-06-23 11:39:47 +01001175 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.module.BaseModuleName(), scopes)
Paul Duffin803a9562020-05-20 11:52:25 +01001176 return nil
1177 }
1178
Paul Duffin1267d872021-04-16 17:21:36 +01001179 return paths
1180}
1181
Paul Duffin32cf58a2021-05-18 16:32:50 +01001182// sdkKindToApiScope maps from android.SdkKind to apiScope.
1183func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1184 var apiScope *apiScope
1185 switch kind {
1186 case android.SdkSystem:
1187 apiScope = apiScopeSystem
1188 case android.SdkModule:
1189 apiScope = apiScopeModuleLib
1190 case android.SdkTest:
1191 apiScope = apiScopeTest
1192 case android.SdkSystemServer:
1193 apiScope = apiScopeSystemServer
1194 default:
1195 apiScope = apiScopePublic
1196 }
1197 return apiScope
1198}
1199
Paul Duffin1267d872021-04-16 17:21:36 +01001200// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001201func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001202 paths := c.selectScopePaths(ctx, kind)
1203 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001204 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001205 }
1206
1207 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001208}
1209
Paul Duffin32cf58a2021-05-18 16:32:50 +01001210// to satisfy SdkLibraryDependency interface
Jihoon Kangbd093452023-12-26 19:08:01 +00001211func (c *commonToSdkLibraryAndImport) SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
1212 paths := c.selectScopePaths(ctx, kind)
1213 if paths == nil {
1214 return makeUnsetDexJarPath()
1215 }
1216
1217 return paths.exportableStubsDexJarPath
1218}
1219
1220// to satisfy SdkLibraryDependency interface
Paul Duffin32cf58a2021-05-18 16:32:50 +01001221func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1222 apiScope := sdkKindToApiScope(kind)
1223 paths := c.findScopePaths(apiScope)
1224 if paths == nil {
1225 return android.OptionalPath{}
1226 }
1227
1228 return paths.removedApiFilePath
1229}
1230
Paul Duffin859fe962020-05-15 10:20:31 +01001231func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1232 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001233 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001234 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001235 }{}
1236
Paul Duffin3f0290e2021-06-30 18:25:36 +01001237 namePtr := proptools.StringPtr(c.module.BaseModuleName())
1238 componentProps.SdkLibraryName = namePtr
1239
Paul Duffindfa131e2020-05-15 20:37:11 +01001240 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001241 // Mark the stubs library as being components of this java_sdk_library so that
1242 // any app that includes code which depends (directly or indirectly) on the stubs
1243 // library will have the appropriate <uses-library> invocation inserted into its
1244 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001245 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001246 }
1247
1248 return componentProps
1249}
1250
Paul Duffindfa131e2020-05-15 20:37:11 +01001251func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1252 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1253}
1254
Paul Duffinf4600f62021-05-13 22:34:45 +01001255// Check if the stub libraries should be compiled for dex
1256func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1257 // Always compile the dex file files for the stub libraries if they will be used on the
1258 // bootclasspath.
1259 return !c.sharedLibrary()
1260}
1261
Paul Duffin859fe962020-05-15 10:20:31 +01001262// Properties related to the use of a module as an component of a java_sdk_library.
1263type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001264 // The name of the java_sdk_library/_import module.
1265 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001266
1267 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1268 // in the AndroidManifest.xml of any Android app that includes code that references
1269 // this module. If not set then no java_sdk_library/_import is tracked.
1270 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1271}
1272
1273// Structure to be embedded in a module struct that needs to support the
1274// SdkLibraryComponentDependency interface.
1275type EmbeddableSdkLibraryComponent struct {
1276 sdkLibraryComponentProperties SdkLibraryComponentProperties
1277}
1278
Paul Duffin71b33cc2021-06-23 11:39:47 +01001279func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1280 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001281}
1282
1283// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001284func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1285 return e.sdkLibraryComponentProperties.SdkLibraryName
1286}
1287
1288// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001289func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001290 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1291 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1292 // run-time library and the corresponding module that provides the implementation. This name is
1293 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1294 // in dexpreopt).
1295 //
1296 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1297 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001298 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1299}
1300
Paul Duffin859fe962020-05-15 10:20:31 +01001301// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1302// (including the java_sdk_library) itself.
1303type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001304 UsesLibraryDependency
1305
Paul Duffin3f0290e2021-06-30 18:25:36 +01001306 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1307 SdkLibraryName() *string
1308
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001309 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1310 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001311}
1312
1313// Make sure that all the module types that are components of java_sdk_library/_import
1314// and which can be referenced (directly or indirectly) from an android app implement
1315// the SdkLibraryComponentDependency interface.
1316var _ SdkLibraryComponentDependency = (*Library)(nil)
1317var _ SdkLibraryComponentDependency = (*Import)(nil)
1318var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001319var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001320
Paul Duffin32cf58a2021-05-18 16:32:50 +01001321// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001322type SdkLibraryDependency interface {
1323 SdkLibraryComponentDependency
1324
1325 // Get the header jars appropriate for the supplied sdk_version.
1326 //
1327 // These are turbine generated jars so they only change if the externals of the
1328 // class changes but it does not contain and implementation or JavaDoc.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001329 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin859fe962020-05-15 10:20:31 +01001330
1331 // Get the implementation jars appropriate for the supplied sdk version.
1332 //
1333 // These are either the implementation jar for the whole sdk library or the implementation
1334 // jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
1335 // they are identical to the corresponding header jars.
Jiyong Parkf1691d22021-03-29 20:11:58 +09001336 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths
Paul Duffin1267d872021-04-16 17:21:36 +01001337
Jihoon Kangbd093452023-12-26 19:08:01 +00001338 // SdkApiStubDexJar returns the dex jar for the stubs for the prebuilt
1339 // java_sdk_library_import module. It is needed by the hiddenapi processing tool which
1340 // processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001341 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001342
Jihoon Kangbd093452023-12-26 19:08:01 +00001343 // SdkApiExportableStubDexJar returns the exportable dex jar for the stubs for
1344 // java_sdk_library module. It is needed by the hiddenapi processing tool which processes
1345 // dex files.
1346 SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
1347
Paul Duffin32cf58a2021-05-18 16:32:50 +01001348 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1349 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1350
Paul Duffinf4600f62021-05-13 22:34:45 +01001351 // sharedLibrary returns true if this can be used as a shared library.
1352 sharedLibrary() bool
Paul Duffin859fe962020-05-15 10:20:31 +01001353}
1354
Inseob Kimc0907f12019-02-08 21:00:45 +09001355type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001356 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001357
Sundong Ahn054b19a2018-10-19 13:46:09 +09001358 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001359
Paul Duffin3375e352020-04-28 10:44:03 +01001360 // Map from api scope to the scope specific property structure.
1361 scopeToProperties map[*apiScope]*ApiScopeProperties
1362
Paul Duffin56d44902020-01-31 13:36:25 +00001363 commonToSdkLibraryAndImport
Jiyong Parkc678ad32018-04-10 13:07:10 +09001364}
1365
Inseob Kimc0907f12019-02-08 21:00:45 +09001366var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001367
Paul Duffin3375e352020-04-28 10:44:03 +01001368func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1369 return module.sdkLibraryProperties.Generate_system_and_test_apis
1370}
1371
1372func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1373 // Check to see if any scopes have been explicitly enabled. If any have then all
1374 // must be.
1375 anyScopesExplicitlyEnabled := false
1376 for _, scope := range allApiScopes {
1377 scopeProperties := module.scopeToProperties[scope]
1378 if scopeProperties.Enabled != nil {
1379 anyScopesExplicitlyEnabled = true
1380 break
1381 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001382 }
Paul Duffin3375e352020-04-28 10:44:03 +01001383
1384 var generatedScopes apiScopes
1385 enabledScopes := make(map[*apiScope]struct{})
1386 for _, scope := range allApiScopes {
1387 scopeProperties := module.scopeToProperties[scope]
1388 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1389 // This is to ensure that any new usages of this module type do not rely on legacy
1390 // behaviour.
1391 defaultEnabledStatus := false
1392 if anyScopesExplicitlyEnabled {
1393 defaultEnabledStatus = scope.defaultEnabledStatus
1394 } else {
1395 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1396 }
1397 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1398 if enabled {
1399 enabledScopes[scope] = struct{}{}
1400 generatedScopes = append(generatedScopes, scope)
1401 }
1402 }
1403
1404 // Now check to make sure that any scope that is extended by an enabled scope is also
1405 // enabled.
1406 for _, scope := range allApiScopes {
1407 if _, ok := enabledScopes[scope]; ok {
1408 extends := scope.extends
1409 if extends != nil {
1410 if _, ok := enabledScopes[extends]; !ok {
1411 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1412 }
1413 }
1414 }
1415 }
1416
1417 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001418}
1419
satayev758968a2021-12-06 11:42:40 +00001420var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1421
satayev8f088b02021-12-06 11:40:46 +00001422func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001423 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001424 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1425 isExternal := !module.depIsInSameApex(ctx, child)
1426 if am, ok := child.(android.ApexModule); ok {
1427 if !do(ctx, parent, am, isExternal) {
1428 return false
1429 }
1430 }
1431 return !isExternal
1432 })
1433 })
1434}
1435
Paul Duffineedc5d52020-06-12 17:46:39 +01001436type sdkLibraryComponentTag struct {
1437 blueprint.BaseDependencyTag
1438 name string
1439}
1440
1441// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1442func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1443
1444var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001445
Jiyong Parke3833882020-02-17 17:28:10 +09001446func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001447 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001448 return dt == xmlPermissionsFileTag
1449 }
1450 return false
1451}
1452
Paul Duffineedc5d52020-06-12 17:46:39 +01001453var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001454
Paul Duffin44f1d842020-06-26 20:17:02 +01001455// Add the dependencies on the child modules in the component deps mutator.
1456func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001457 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001458 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001459 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kangbd093452023-12-26 19:08:01 +00001460 ctx.AddVariationDependencies(nil, apiScope.everythingStubsTag, stubModuleName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001461
Jihoon Kangbd093452023-12-26 19:08:01 +00001462 exportableStubModuleName := module.exportableStubsLibraryModuleName(apiScope)
1463 ctx.AddVariationDependencies(nil, apiScope.exportableStubsTag, exportableStubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001464
Paul Duffin15f34ef2020-07-20 18:04:44 +01001465 // Add a dependency on the stubs source in order to access both stubs source and api information.
1466 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001467
1468 if module.compareAgainstLatestApi(apiScope) {
1469 // Add dependencies on the latest finalized version of the API .txt file.
1470 latestApiModuleName := module.latestApiModuleName(apiScope)
1471 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1472
1473 // Add dependencies on the latest finalized version of the remove API .txt file.
1474 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1475 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1476 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001477 }
1478
Paul Duffindfa131e2020-05-15 20:37:11 +01001479 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001480 // Add dependency to the rule for generating the implementation library.
1481 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1482
Paul Duffindfa131e2020-05-15 20:37:11 +01001483 if module.sharedLibrary() {
1484 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001485 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001486 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001487 }
1488}
Paul Duffine74ac732020-02-06 13:51:46 +00001489
Paul Duffin44f1d842020-06-26 20:17:02 +01001490// Add other dependencies as normal.
1491func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001492 var missingApiModules []string
1493 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1494 if apiScope.unstable {
1495 continue
1496 }
Paul Duffin958806b2022-05-16 13:10:47 +00001497 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001498 missingApiModules = append(missingApiModules, m)
1499 }
Paul Duffin958806b2022-05-16 13:10:47 +00001500 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001501 missingApiModules = append(missingApiModules, m)
1502 }
Paul Duffin958806b2022-05-16 13:10:47 +00001503 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001504 missingApiModules = append(missingApiModules, m)
1505 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001506 }
1507 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1508 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1509 m += "You need to do one of the following:\n"
1510 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1511 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1512 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1513 m += "\n"
1514 m += "The following filegroup modules are missing:\n "
1515 m += strings.Join(missingApiModules, "\n ") + "\n"
1516 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."
1517 ctx.ModuleErrorf(m)
1518 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001519 if module.requiresRuntimeImplementationLibrary() {
Paul Duffindfa131e2020-05-15 20:37:11 +01001520 // Only add the deps for the library if it is actually going to be built.
1521 module.Library.deps(ctx)
1522 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001523}
1524
Paul Duffin46dc45a2020-05-14 15:39:10 +01001525func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1526 paths, err := module.commonOutputFiles(tag)
Colin Cross4acaea92021-12-10 23:05:02 +00001527 if paths != nil || err != nil {
Paul Duffin46dc45a2020-05-14 15:39:10 +01001528 return paths, err
1529 }
Colin Cross4acaea92021-12-10 23:05:02 +00001530 if module.requiresRuntimeImplementationLibrary() {
1531 return module.Library.OutputFiles(tag)
1532 }
1533 if tag == "" {
1534 return nil, nil
1535 }
1536 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001537}
1538
Inseob Kimc0907f12019-02-08 21:00:45 +09001539func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
satayev8f088b02021-12-06 11:40:46 +00001540 if proptools.String(module.deviceProperties.Min_sdk_version) != "" {
1541 module.CheckMinSdkVersion(ctx)
1542 }
1543
Paul Duffina2ae7e02020-09-11 11:55:00 +01001544 module.generateCommonBuildActions(ctx)
1545
Paul Duffindfa131e2020-05-15 20:37:11 +01001546 // Only build an implementation library if required.
1547 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001548 module.Library.GenerateAndroidBuildActions(ctx)
1549 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001550
Paul Duffinb97b1572021-04-29 21:50:40 +01001551 // Collate the components exported by this module. All scope specific modules are exported but
1552 // the impl and xml component modules are not.
1553 exportedComponents := map[string]struct{}{}
1554
Sundong Ahn57368eb2018-07-06 11:20:23 +09001555 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001556 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001557 // the recorded paths will be returned depending on the link type of the caller.
1558 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001559 tag := ctx.OtherModuleDependencyTag(to)
1560
Paul Duffinc8782502020-04-29 20:45:27 +01001561 // Extract information from any of the scope specific dependencies.
1562 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1563 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001564 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001565
1566 // Extract information from the dependency. The exact information extracted
1567 // is determined by the nature of the dependency which is determined by the tag.
1568 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001569
1570 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Sundong Ahn20e998b2018-07-24 11:19:26 +09001571 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001572 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001573
1574 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001575 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001576 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001577
1578 // Provide additional information for inclusion in an sdk's generated .info file.
1579 additionalSdkInfo := map[string]interface{}{}
1580 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001581 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001582 scopes := map[string]interface{}{}
1583 additionalSdkInfo["scopes"] = scopes
1584 for scope, scopePaths := range module.scopePaths {
1585 scopeInfo := map[string]interface{}{}
1586 scopes[scope.name] = scopeInfo
1587 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1588 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
1589 if p := scopePaths.latestApiPath; p.Valid() {
1590 scopeInfo["latest_api"] = p.Path().String()
1591 }
1592 if p := scopePaths.latestRemovedApiPath; p.Valid() {
1593 scopeInfo["latest_removed_api"] = p.Path().String()
1594 }
1595 }
Colin Cross40213022023-12-13 15:19:49 -08001596 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
Jiyong Parkc678ad32018-04-10 13:07:10 +09001597}
1598
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001599func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001600 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001601 return nil
1602 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001603 entriesList := module.Library.AndroidMkEntries()
Yo Chiang07d75072020-06-05 17:43:19 +08001604 if module.sharedLibrary() {
1605 entries := &entriesList[0]
1606 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1607 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001608 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001609}
1610
Anton Hansson5fd5d242020-03-27 19:43:19 +00001611// The dist path of the stub artifacts
1612func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001613 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001614}
1615
Paul Duffin12ceb462019-12-24 20:31:31 +00001616// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001617func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001618 scopeProperties := module.scopeToProperties[apiScope]
1619 if scopeProperties.Sdk_version != nil {
1620 return proptools.String(scopeProperties.Sdk_version)
1621 }
1622
Jiyong Parkf1691d22021-03-29 20:11:58 +09001623 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001624 if sdkDep.hasStandardLibs() {
1625 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001626 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001627 } else {
1628 // Otherwise, use no system module.
1629 return "none"
1630 }
1631}
1632
Paul Duffin31310252020-11-20 21:26:20 +00001633func (module *SdkLibrary) distStem() string {
1634 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1635}
1636
Colin Cross986b69a2021-06-01 13:13:40 -07001637// distGroup returns the subdirectory of the dist path of the stub artifacts.
1638func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001639 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001640}
1641
Paul Duffin958806b2022-05-16 13:10:47 +00001642func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1643 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1644}
1645
Paul Duffind1b3a922020-01-22 11:57:20 +00001646func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001647 return ":" + module.latestApiModuleName(apiScope)
1648}
1649
1650func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
1651 return latestPrebuiltApiModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001652}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001653
Paul Duffind1b3a922020-01-22 11:57:20 +00001654func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001655 return ":" + module.latestRemovedApiModuleName(apiScope)
1656}
1657
1658func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
1659 return latestPrebuiltApiModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001660}
1661
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001662func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001663 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1664}
1665
1666func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1667 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001668}
1669
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001670func (module *SdkLibrary) contributesToApiSurface(c android.Config) bool {
1671 _, exists := c.GetApiLibraries()[module.Name()]
1672 return exists
1673}
1674
Jihoon Kang0c705a42023-08-02 06:44:57 +00001675// The listed modules are the special java_sdk_libraries where apiScope.kind do not match the
1676// api surface that the module contribute to. For example, the public droidstubs and java_library
1677// do not contribute to the public api surface, but contributes to the core platform api surface.
1678// This method returns the full api surface stub lib that
1679// the generated java_api_library should depend on.
1680func (module *SdkLibrary) alternativeFullApiSurfaceStubLib() string {
1681 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1682 return val.FullApiSurfaceStubLib
1683 }
1684 return ""
1685}
1686
1687// The listed modules' stubs contents do not match the corresponding txt files,
1688// but require additional api contributions to generate the full stubs.
1689// This method returns the name of the additional api contribution module
1690// for corresponding sdk_library modules.
1691func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1692 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
1693 return val.AdditionalApiContribution
1694 }
1695 return ""
1696}
1697
Anton Hansson944e77d2020-08-19 11:40:22 +01001698func childModuleVisibility(childVisibility []string) []string {
1699 if childVisibility == nil {
1700 // No child visibility set. The child will use the visibility of the sdk_library.
1701 return nil
1702 }
1703
1704 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1705 var visibility []string
1706 visibility = append(visibility, "//visibility:override")
1707 visibility = append(visibility, childVisibility...)
1708 return visibility
1709}
1710
Paul Duffin5df79302020-05-16 15:52:12 +01001711// Creates the implementation java library
1712func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001713 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1714
Paul Duffin5df79302020-05-16 15:52:12 +01001715 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001716 Name *string
1717 Visibility []string
1718 Instrument bool
1719 Libs []string
1720 Static_libs []string
1721 Apex_available []string
Paul Duffin5df79302020-05-16 15:52:12 +01001722 }{
1723 Name: proptools.StringPtr(module.implLibraryModuleName()),
Anton Hansson944e77d2020-08-19 11:40:22 +01001724 Visibility: visibility,
Paul Duffin296cf332020-06-18 21:09:55 +01001725 // Set the instrument property to ensure it is instrumented when instrumentation is required.
1726 Instrument: true,
Anton Hansson7f66efa2020-10-08 14:47:23 +01001727 // Set the impl_only libs. Note that the module's "Libs" get appended as well, via the
1728 // addition of &module.properties below.
1729 Libs: module.sdkLibraryProperties.Impl_only_libs,
Paul Duffin77590a82022-04-28 14:13:30 +00001730 // Set the impl_only static libs. Note that the module's "static_libs" get appended as well, via the
1731 // addition of &module.properties below.
1732 Static_libs: module.sdkLibraryProperties.Impl_only_static_libs,
1733 // Pass the apex_available settings down so that the impl library can be statically
1734 // embedded within a library that is added to an APEX. Needed for updatable-media.
1735 Apex_available: module.ApexAvailable(),
Paul Duffin5df79302020-05-16 15:52:12 +01001736 }
1737
1738 properties := []interface{}{
1739 &module.properties,
1740 &module.protoProperties,
1741 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001742 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001743 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001744 &module.linter.properties,
Paul Duffin5df79302020-05-16 15:52:12 +01001745 &props,
1746 module.sdkComponentPropertiesForChildLibrary(),
1747 }
1748 mctx.CreateModule(LibraryFactory, properties...)
1749}
1750
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001751type libraryProperties struct {
1752 Name *string
1753 Visibility []string
1754 Srcs []string
1755 Installable *bool
1756 Sdk_version *string
1757 System_modules *string
1758 Patch_module *string
1759 Libs []string
1760 Static_libs []string
1761 Compile_dex *bool
1762 Java_version *string
1763 Openjdk9 struct {
1764 Srcs []string
1765 Javacflags []string
1766 }
1767 Dist struct {
1768 Targets []string
1769 Dest *string
1770 Dir *string
1771 Tag *string
1772 }
1773}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001774
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001775func (module *SdkLibrary) stubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
1776 props := libraryProperties{}
Jihoon Kang786df932023-09-07 01:18:31 +00001777 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001778 // sources are generated from the droiddoc
Paul Duffin12ceb462019-12-24 20:31:31 +00001779 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001780 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001781 props.System_modules = module.deviceProperties.System_modules
1782 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001783 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001784 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001785 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001786 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001787 // The stub-annotations library contains special versions of the annotations
1788 // with CLASS retention policy, so that they're kept.
1789 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1790 props.Libs = append(props.Libs, "stub-annotations")
1791 }
Paul Duffina18abc22020-05-16 18:54:24 +01001792 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1793 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001794 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1795 // interop with older developer tools that don't support 1.9.
1796 props.Java_version = proptools.StringPtr("1.8")
Paul Duffinf4600f62021-05-13 22:34:45 +01001797
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001798 return props
1799}
1800
1801// Creates a static java library that has API stubs
1802func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1803
1804 props := module.stubsLibraryProps(mctx, apiScope)
1805 props.Name = proptools.StringPtr(module.sourceStubsLibraryModuleName(apiScope))
1806 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
1807
1808 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1809}
1810
1811// Create a static java library that compiles the "exportable" stubs
1812func (module *SdkLibrary) createExportableStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1813 props := module.stubsLibraryProps(mctx, apiScope)
1814 props.Name = proptools.StringPtr(module.exportableSourceStubsLibraryModuleName(apiScope))
1815 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope) + "{.exportable}"}
1816
Paul Duffin859fe962020-05-15 10:20:31 +01001817 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001818}
1819
Paul Duffin6d0886e2020-04-07 18:49:53 +01001820// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001821// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001822func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001823 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001824 Name *string
Paul Duffin4911a892020-04-29 23:35:13 +01001825 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001826 Srcs []string
1827 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001828 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001829 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001830 System_modules *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001831 Libs []string
Paul Duffin6877e6d2020-09-25 19:59:14 +01001832 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001833 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001834 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001835 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001836 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001837 Merge_annotations_dirs []string
1838 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001839 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001840 Previous_api *string
Jihoon Kang6592e872023-12-19 01:13:16 +00001841 Aconfig_declarations []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001842 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001843 Current ApiToCheck
1844 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001845
1846 Api_lint struct {
1847 Enabled *bool
1848 New_since *string
1849 Baseline_file *string
1850 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001851 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001852 Aidl struct {
1853 Include_dirs []string
1854 Local_include_dirs []string
1855 }
Paul Duffin040e9062020-11-23 17:41:36 +00001856 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001857 }{}
1858
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001859 // The stubs source processing uses the same compile time classpath when extracting the
1860 // API from the implementation library as it does when compiling it. i.e. the same
1861 // * sdk version
1862 // * system_modules
1863 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001864
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001865 props.Name = proptools.StringPtr(name)
Anton Hansson944e77d2020-08-19 11:40:22 +01001866 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001867 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001868 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001869 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001870 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001871 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001872 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001873 // A droiddoc module has only one Libs property and doesn't distinguish between
1874 // shared libs and static libs. So we need to add both of these libs to Libs property.
Paul Duffina18abc22020-05-16 18:54:24 +01001875 props.Libs = module.properties.Libs
1876 props.Libs = append(props.Libs, module.properties.Static_libs...)
Nikita Ioffed732da72022-11-21 12:38:25 +00001877 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00001878 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001879 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1880 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1881 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001882
Paul Duffine22c2ab2020-05-20 19:35:27 +01001883 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001884 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1885 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
Jihoon Kang6592e872023-12-19 01:13:16 +00001886 props.Aconfig_declarations = module.sdkLibraryProperties.Aconfig_declarations
Sundong Ahn054b19a2018-10-19 13:46:09 +09001887
Paul Duffin6d0886e2020-04-07 18:49:53 +01001888 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001889 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001890 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001891 }
1892 if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001893 droidstubsArgs = append(droidstubsArgs,
Paul Duffin235ffff2019-12-24 10:41:30 +00001894 android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1895 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001896 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00001897 disabledWarnings := []string{"HiddenSuperclass"}
1898 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
1899 disabledWarnings = append(disabledWarnings,
1900 "BroadcastBehavior",
1901 "DeprecationMismatch",
1902 "MissingPermission",
1903 "SdkConstant",
1904 "Todo",
1905 )
Paul Duffin235ffff2019-12-24 10:41:30 +00001906 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001907 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001908
Paul Duffin6877e6d2020-09-25 19:59:14 +01001909 // Output Javadoc comments for public scope.
1910 if apiScope == apiScopePublic {
1911 props.Output_javadoc_comments = proptools.BoolPtr(true)
1912 }
1913
Paul Duffin1fb487d2020-04-07 18:50:10 +01001914 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001915 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001916 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001917 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001918
Paul Duffin15f34ef2020-07-20 18:04:44 +01001919 // List of APIs identified from the provided source files are created. They are later
1920 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1921 // last-released (a.k.a numbered) list of API.
1922 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1923 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1924 apiDir := module.getApiDir()
1925 currentApiFileName = path.Join(apiDir, currentApiFileName)
1926 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001927
Paul Duffin15f34ef2020-07-20 18:04:44 +01001928 // check against the not-yet-release API
1929 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1930 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001931
Paul Duffin958806b2022-05-16 13:10:47 +00001932 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001933 // check against the latest released API
1934 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001935 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001936 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1937 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1938 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001939 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1940 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001941
Paul Duffin15f34ef2020-07-20 18:04:44 +01001942 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1943 // Enable api lint.
1944 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1945 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001946
Paul Duffin15f34ef2020-07-20 18:04:44 +01001947 // If it exists then pass a lint-baseline.txt through to droidstubs.
1948 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1949 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1950 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1951 if err != nil {
1952 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1953 }
1954 if len(paths) == 1 {
1955 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1956 } else if len(paths) != 0 {
1957 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001958 }
1959 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001960 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001961
Paul Duffin15f34ef2020-07-20 18:04:44 +01001962 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001963 // Dist the api txt and removed api txt artifacts for sdk builds.
1964 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1965 for _, p := range []struct {
1966 tag string
1967 pattern string
1968 }{
1969 {tag: ".api.txt", pattern: "%s.txt"},
1970 {tag: ".removed-api.txt", pattern: "%s-removed.txt"},
1971 } {
1972 props.Dists = append(props.Dists, android.Dist{
1973 Targets: []string{"sdk", "win_sdk"},
1974 Dir: distDir,
1975 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
1976 Tag: proptools.StringPtr(p.tag),
1977 })
1978 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00001979 }
1980
Spandan Das2cc80ba2023-10-27 17:21:52 +00001981 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001982}
1983
Jihoon Kang0c705a42023-08-02 06:44:57 +00001984func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope, alternativeFullApiSurfaceStub string) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001985 props := struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00001986 Name *string
1987 Visibility []string
1988 Api_contributions []string
1989 Libs []string
1990 Static_libs []string
1991 Full_api_surface_stub *string
Jihoon Kang4ec24872023-10-05 17:26:09 +00001992 System_modules *string
Jihoon Kang063ec002023-06-28 01:16:23 +00001993 Enable_validation *bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001994 }{}
1995
1996 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Jihoon Kang786df932023-09-07 01:18:31 +00001997 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001998
1999 apiContributions := []string{}
2000
2001 // Api surfaces are not independent of each other, but have subset relationships,
2002 // and so does the api files. To generate from-text stubs for api surfaces other than public,
2003 // all subset api domains' api_contriubtions must be added as well.
2004 scope := apiScope
2005 for scope != nil {
2006 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
2007 scope = scope.extends
2008 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00002009 if apiScope == apiScopePublic {
2010 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
2011 if additionalApiContribution != "" {
2012 apiContributions = append(apiContributions, additionalApiContribution)
2013 }
2014 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002015
2016 props.Api_contributions = apiContributions
2017 props.Libs = module.properties.Libs
2018 props.Libs = append(props.Libs, module.sdkLibraryProperties.Stub_only_libs...)
Mark White9421c4c2023-08-10 00:07:03 +00002019 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002020 props.Libs = append(props.Libs, "stub-annotations")
2021 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kange7ee2562023-07-25 05:51:46 +00002022 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName())
Jihoon Kang0c705a42023-08-02 06:44:57 +00002023 if alternativeFullApiSurfaceStub != "" {
2024 props.Full_api_surface_stub = proptools.StringPtr(alternativeFullApiSurfaceStub)
2025 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002026
2027 // android_module_lib_stubs_current.from-text only comprises api contributions from art, conscrypt and i18n.
2028 // Thus, replace with android_module_lib_stubs_current_full.from-text, which comprises every api domains.
2029 if apiScope.kind == android.SdkModule {
Jihoon Kangca198c22023-06-22 23:13:51 +00002030 props.Full_api_surface_stub = proptools.StringPtr(apiScope.kind.DefaultJavaLibraryName() + "_full.from-text")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002031 }
2032
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00002033 // java_sdk_library modules that set sdk_version as none does not depend on other api
2034 // domains. Therefore, java_api_library created from such modules should not depend on
2035 // full_api_surface_stubs but create and compile stubs by the java_api_library module
2036 // itself.
2037 if module.SdkVersion(mctx).Kind == android.SdkNone {
2038 props.Full_api_surface_stub = nil
2039 }
2040
Jihoon Kang4ec24872023-10-05 17:26:09 +00002041 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00002042 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang4ec24872023-10-05 17:26:09 +00002043
Spandan Das2cc80ba2023-10-27 17:21:52 +00002044 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002045}
2046
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002047func (module *SdkLibrary) topLevelStubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
2048 props := libraryProperties{}
2049
Jihoon Kang1147b312023-06-08 23:25:57 +00002050 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
2051 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
2052 props.Sdk_version = proptools.StringPtr(sdkVersion)
2053
Jihoon Kang1147b312023-06-08 23:25:57 +00002054 props.System_modules = module.deviceProperties.System_modules
2055
Jihoon Kang1147b312023-06-08 23:25:57 +00002056 // The imports need to be compiled to dex if the java_sdk_library requests it.
2057 compileDex := module.dexProperties.Compile_dex
2058 if module.stubLibrariesCompiledForDex() {
2059 compileDex = proptools.BoolPtr(true)
2060 }
2061 props.Compile_dex = compileDex
2062
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002063 return props
2064}
2065
2066func (module *SdkLibrary) createTopLevelStubsLibrary(
2067 mctx android.DefaultableHookContext, apiScope *apiScope, contributesToApiSurface bool) {
2068
2069 props := module.topLevelStubsLibraryProps(mctx, apiScope)
2070 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
2071
2072 // Add the stub compiling java_library/java_api_library as static lib based on build config
2073 staticLib := module.sourceStubsLibraryModuleName(apiScope)
2074 if mctx.Config().BuildFromTextStub() && contributesToApiSurface {
2075 staticLib = module.apiLibraryModuleName(apiScope)
2076 }
2077 props.Static_libs = append(props.Static_libs, staticLib)
2078
2079 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2080}
2081
2082func (module *SdkLibrary) createTopLevelExportableStubsLibrary(
2083 mctx android.DefaultableHookContext, apiScope *apiScope) {
2084
2085 props := module.topLevelStubsLibraryProps(mctx, apiScope)
2086 props.Name = proptools.StringPtr(module.exportableStubsLibraryModuleName(apiScope))
2087
2088 // Dist the class jar artifact for sdk builds.
2089 // "exportable" stubs are copied to dist for sdk builds instead of the "everything" stubs.
2090 if !Bool(module.sdkLibraryProperties.No_dist) {
2091 props.Dist.Targets = []string{"sdk", "win_sdk"}
2092 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
2093 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
2094 props.Dist.Tag = proptools.StringPtr(".jar")
2095 }
2096
2097 staticLib := module.exportableSourceStubsLibraryModuleName(apiScope)
2098 props.Static_libs = append(props.Static_libs, staticLib)
2099
Jihoon Kang1147b312023-06-08 23:25:57 +00002100 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2101}
2102
Paul Duffin958806b2022-05-16 13:10:47 +00002103func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
2104 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
2105}
2106
Paul Duffinea8f8082021-06-24 13:25:57 +01002107// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09002108func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2109 depTag := mctx.OtherModuleDependencyTag(dep)
2110 if depTag == xmlPermissionsFileTag {
2111 return true
2112 }
2113 return module.Library.DepIsInSameApex(mctx, dep)
2114}
2115
Paul Duffinea8f8082021-06-24 13:25:57 +01002116// Implements android.ApexModule
2117func (module *SdkLibrary) UniqueApexVariations() bool {
2118 return module.uniqueApexVariations()
2119}
2120
Jihoon Kang80456fd2023-11-15 19:22:14 +00002121func (module *SdkLibrary) ContributeToApi() bool {
2122 return proptools.BoolDefault(module.sdkLibraryProperties.Contribute_to_android_api, false)
2123}
2124
Jiyong Parkc678ad32018-04-10 13:07:10 +09002125// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01002126func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002127 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002128 var moduleMinApiLevelStr = moduleMinApiLevel.String()
2129 if moduleMinApiLevel == android.NoneApiLevel {
2130 moduleMinApiLevelStr = "current"
2131 }
Jiyong Parke3833882020-02-17 17:28:10 +09002132 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00002133 Name *string
2134 Lib_name *string
2135 Apex_available []string
2136 On_bootclasspath_since *string
2137 On_bootclasspath_before *string
2138 Min_device_sdk *string
2139 Max_device_sdk *string
2140 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002141 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002142 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00002143 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
2144 Lib_name: proptools.StringPtr(module.BaseModuleName()),
2145 Apex_available: module.ApexProperties.Apex_available,
2146 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
2147 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
2148 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
2149 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
2150 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002151 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002152 }
Jiyong Parke3833882020-02-17 17:28:10 +09002153
Jiyong Parke3833882020-02-17 17:28:10 +09002154 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002155}
2156
Jiyong Parkf1691d22021-03-29 20:11:58 +09002157func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s android.SdkSpec) android.Paths {
Jiyong Park54105c42021-03-31 18:17:53 +09002158 var ver android.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002159 var kind android.SdkKind
2160 if s.UsePrebuilt(ctx) {
Jiyong Park54105c42021-03-31 18:17:53 +09002161 ver = s.ApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002162 kind = s.Kind
Jiyong Parkc678ad32018-04-10 13:07:10 +09002163 } else {
Jiyong Park6a927c42020-01-21 02:03:43 +09002164 // We don't have prebuilt SDK for the specific sdkVersion.
2165 // Instead of breaking the build, fallback to use "system_current"
Jiyong Park54105c42021-03-31 18:17:53 +09002166 ver = android.FutureApiLevel
Jiyong Parkf1691d22021-03-29 20:11:58 +09002167 kind = android.SdkSystem
Sundong Ahn054b19a2018-10-19 13:46:09 +09002168 }
Jiyong Park6a927c42020-01-21 02:03:43 +09002169
2170 dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
Paul Duffin50061512020-01-21 16:31:05 +00002171 jar := filepath.Join(dir, baseName+".jar")
Sundong Ahn054b19a2018-10-19 13:46:09 +09002172 jarPath := android.ExistentPathForSource(ctx, jar)
Sundong Ahnae418ac2019-02-28 15:01:28 +09002173 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -08002174 if ctx.Config().AllowMissingDependencies() {
2175 return android.Paths{android.PathForSource(ctx, jar)}
2176 } else {
Jiyong Parkf1691d22021-03-29 20:11:58 +09002177 ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.Raw, jar)
Colin Cross07c88562020-01-07 09:34:44 -08002178 }
Sundong Ahnae418ac2019-02-28 15:01:28 +09002179 return nil
2180 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002181 return android.Paths{jarPath.Path()}
2182}
2183
Colin Crossaede88c2020-08-11 12:17:01 -07002184// 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 +01002185//
2186// If either this or the other module are on the platform then this will return
2187// false.
Colin Cross56a83212020-09-15 18:30:11 -07002188func withinSameApexesAs(ctx android.BaseModuleContext, other android.Module) bool {
Colin Crossff694a82023-12-13 15:54:49 -08002189 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Colin Cross313aa542023-12-13 13:47:44 -08002190 otherApexInfo, _ := android.OtherModuleProvider(ctx, other, android.ApexInfoProvider)
Jiyong Parkab50b072021-05-12 17:13:56 +09002191 return len(otherApexInfo.InApexVariants) > 0 && reflect.DeepEqual(apexInfo.InApexVariants, otherApexInfo.InApexVariants)
Paul Duffin9b879592020-05-26 13:21:35 +01002192}
2193
Jiyong Parkf1691d22021-03-29 20:11:58 +09002194func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Jiyong Park932cdfe2020-05-28 00:19:53 +09002195 // If the client doesn't set sdk_version, but if this library prefers stubs over
2196 // the impl library, let's provide the widest API surface possible. To do so,
2197 // force override sdk_version to module_current so that the closest possible API
2198 // surface could be found in selectHeaderJarsForSdkVersion
Jiyong Parkf1691d22021-03-29 20:11:58 +09002199 if module.defaultsToStubs() && !sdkVersion.Specified() {
Jiyong Park92315372021-04-02 08:45:46 +09002200 sdkVersion = android.SdkSpecFrom(ctx, "module_current")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002201 }
Paul Duffind1b3a922020-01-22 11:57:20 +00002202
Paul Duffindaaa3322020-05-26 18:13:57 +01002203 // Only provide access to the implementation library if it is actually built.
2204 if module.requiresRuntimeImplementationLibrary() {
2205 // Check any special cases for java_sdk_library.
2206 //
2207 // Only allow access to the implementation library in the following condition:
2208 // * No sdk_version specified on the referencing module.
Paul Duffin9b879592020-05-26 13:21:35 +01002209 // * The referencing module is in the same apex as this.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002210 if sdkVersion.Kind == android.SdkPrivate || withinSameApexesAs(ctx, module) {
Paul Duffindaaa3322020-05-26 18:13:57 +01002211 if headerJars {
2212 return module.HeaderJars()
2213 } else {
2214 return module.ImplementationJars()
2215 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09002216 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09002217 }
Paul Duffinb05d4292020-05-20 12:19:10 +01002218
Paul Duffin23970f42020-05-20 14:20:02 +01002219 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002220}
2221
Sundong Ahn241cd372018-07-13 16:16:44 +09002222// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002223func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002224 return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
2225}
2226
2227// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002228func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Paul Duffind1b3a922020-01-22 11:57:20 +00002229 return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
Sundong Ahn241cd372018-07-13 16:16:44 +09002230}
2231
Colin Cross571cccf2019-02-04 11:22:08 -08002232var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2233
Jiyong Park82484c02018-04-23 21:41:26 +09002234func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002235 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002236 return &[]string{}
2237 }).(*[]string)
2238}
2239
Paul Duffin749f98f2019-12-30 17:23:46 +00002240func (module *SdkLibrary) getApiDir() string {
2241 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2242}
2243
Jiyong Parkc678ad32018-04-10 13:07:10 +09002244// For a java_sdk_library module, create internal modules for stubs, docs,
2245// runtime libs and xml file. If requested, the stubs and docs are created twice
2246// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002247func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
2248 // If the module has been disabled then don't create any child modules.
2249 if !module.Enabled() {
2250 return
2251 }
2252
Paul Duffina18abc22020-05-16 18:54:24 +01002253 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002254 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002255 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002256 }
2257
Paul Duffin37e0b772019-12-30 17:20:10 +00002258 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002259 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002260 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002261 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002262 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002263
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002264 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002265
Paul Duffin3375e352020-04-28 10:44:03 +01002266 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002267
Paul Duffin749f98f2019-12-30 17:23:46 +00002268 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002269 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002270 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002271 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002272 p := android.ExistentPathForSource(mctx, path)
2273 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002274 if mctx.Config().AllowMissingDependencies() {
2275 mctx.AddMissingDependencies([]string{path})
2276 } else {
2277 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2278 missingCurrentApi = true
2279 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002280 }
2281 }
2282 }
2283
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002284 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002285 script := "build/soong/scripts/gen-java-current-api-files.sh"
2286 p := android.ExistentPathForSource(mctx, script)
2287
2288 if !p.Valid() {
2289 panic(fmt.Sprintf("script file %s doesn't exist", script))
2290 }
2291
2292 mctx.ModuleErrorf("One or more current api files are missing. "+
2293 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002294 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002295 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002296 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002297 return
2298 }
2299
Paul Duffin3375e352020-04-28 10:44:03 +01002300 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002301 // Use the stubs source name for legacy reasons.
2302 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002303
Paul Duffind1b3a922020-01-22 11:57:20 +00002304 module.createStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002305 module.createExportableStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002306
Jihoon Kang0c705a42023-08-02 06:44:57 +00002307 alternativeFullApiSurfaceStubLib := ""
2308 if scope == apiScopePublic {
2309 alternativeFullApiSurfaceStubLib = module.alternativeFullApiSurfaceStubLib()
2310 }
2311 contributesToApiSurface := module.contributesToApiSurface(mctx.Config()) || alternativeFullApiSurfaceStubLib != ""
Jihoon Kang1147b312023-06-08 23:25:57 +00002312 if contributesToApiSurface {
Jihoon Kang0c705a42023-08-02 06:44:57 +00002313 module.createApiLibrary(mctx, scope, alternativeFullApiSurfaceStubLib)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002314 }
Jihoon Kang1147b312023-06-08 23:25:57 +00002315
2316 module.createTopLevelStubsLibrary(mctx, scope, contributesToApiSurface)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002317 module.createTopLevelExportableStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09002318 }
2319
Paul Duffindfa131e2020-05-15 20:37:11 +01002320 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002321 // Create child module to create an implementation library.
2322 //
2323 // This temporarily creates a second implementation library that can be explicitly
2324 // referenced.
2325 //
2326 // TODO(b/156618935) - update comment once only one implementation library is created.
2327 module.createImplLibrary(mctx)
2328
Paul Duffindfa131e2020-05-15 20:37:11 +01002329 // Only create an XML permissions file that declares the library as being usable
2330 // as a shared library if required.
2331 if module.sharedLibrary() {
2332 module.createXmlFile(mctx)
2333 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002334
2335 // record java_sdk_library modules so that they are exported to make
2336 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2337 javaSdkLibrariesLock.Lock()
2338 defer javaSdkLibrariesLock.Unlock()
2339 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2340 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002341
Paul Duffin77590a82022-04-28 14:13:30 +00002342 // 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 +01002343 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Paul Duffin77590a82022-04-28 14:13:30 +00002344 module.properties.Static_libs = append(module.properties.Static_libs, module.sdkLibraryProperties.Impl_only_static_libs...)
Inseob Kimc0907f12019-02-08 21:00:45 +09002345}
2346
2347func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002348 module.addHostAndDeviceProperties()
2349 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002350
Paul Duffin71b33cc2021-06-23 11:39:47 +01002351 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002352
Paul Duffina18abc22020-05-16 18:54:24 +01002353 module.properties.Installable = proptools.BoolPtr(true)
2354 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002355}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002356
Paul Duffindfa131e2020-05-15 20:37:11 +01002357func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2358 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2359}
2360
Jiyong Park932cdfe2020-05-28 00:19:53 +09002361func (module *SdkLibrary) defaultsToStubs() bool {
2362 return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
2363}
2364
Paul Duffin1b1e8062020-05-08 13:44:43 +01002365// Defines how to name the individual component modules the sdk library creates.
2366type sdkLibraryComponentNamingScheme interface {
2367 stubsLibraryModuleName(scope *apiScope, baseName string) string
2368
2369 stubsSourceModuleName(scope *apiScope, baseName string) string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002370
2371 apiLibraryModuleName(scope *apiScope, baseName string) string
Jihoon Kang1147b312023-06-08 23:25:57 +00002372
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002373 sourceStubsLibraryModuleName(scope *apiScope, baseName string) string
2374
2375 exportableStubsLibraryModuleName(scope *apiScope, baseName string) string
2376
2377 exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string
Paul Duffin1b1e8062020-05-08 13:44:43 +01002378}
2379
2380type defaultNamingScheme struct {
2381}
2382
2383func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
2384 return scope.stubsLibraryModuleName(baseName)
2385}
2386
2387func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
2388 return scope.stubsSourceModuleName(baseName)
2389}
2390
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002391func (s *defaultNamingScheme) apiLibraryModuleName(scope *apiScope, baseName string) string {
2392 return scope.apiLibraryModuleName(baseName)
2393}
2394
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002395func (s *defaultNamingScheme) sourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
Jihoon Kang1147b312023-06-08 23:25:57 +00002396 return scope.sourceStubLibraryModuleName(baseName)
2397}
2398
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002399func (s *defaultNamingScheme) exportableStubsLibraryModuleName(scope *apiScope, baseName string) string {
2400 return scope.exportableStubsLibraryModuleName(baseName)
2401}
2402
2403func (s *defaultNamingScheme) exportableSourceStubsLibraryModuleName(scope *apiScope, baseName string) string {
2404 return scope.exportableSourceStubsLibraryModuleName(baseName)
2405}
2406
Paul Duffin1b1e8062020-05-08 13:44:43 +01002407var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
2408
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002409func hasStubsLibrarySuffix(name string, apiScope *apiScope) bool {
2410 return strings.HasSuffix(name, apiScope.stubsLibraryModuleNameSuffix()) ||
2411 strings.HasSuffix(name, apiScope.exportableStubsLibraryModuleNameSuffix())
2412}
2413
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08002414func moduleStubLinkType(name string) (stub bool, ret sdkLinkType) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002415 name = strings.TrimSuffix(name, ".from-source")
2416
Anton Hansson2d0c1942020-05-25 12:20:51 +01002417 // This suffix-based approach is fragile and could potentially mis-trigger.
2418 // TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002419 if hasStubsLibrarySuffix(name, apiScopePublic) {
Anton Hansson08f476b2021-04-07 15:32:19 +01002420 if name == "hwbinder.stubs" || name == "libcore_private.stubs" {
2421 // Due to a previous bug, these modules were not considered stubs, so we retain that.
2422 return false, javaPlatform
2423 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002424 return true, javaSdk
2425 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002426 if hasStubsLibrarySuffix(name, apiScopeSystem) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002427 return true, javaSystem
2428 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002429 if hasStubsLibrarySuffix(name, apiScopeModuleLib) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002430 return true, javaModule
2431 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002432 if hasStubsLibrarySuffix(name, apiScopeTest) {
Anton Hansson2d0c1942020-05-25 12:20:51 +01002433 return true, javaSystem
2434 }
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002435 if hasStubsLibrarySuffix(name, apiScopeSystemServer) {
Jihoon Kang1147b312023-06-08 23:25:57 +00002436 return true, javaSystemServer
2437 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002438 return false, javaPlatform
2439}
2440
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002441// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2442// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2443// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2444// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2445// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002446func SdkLibraryFactory() android.Module {
2447 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002448
2449 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002450 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002451
Inseob Kimc0907f12019-02-08 21:00:45 +09002452 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002453 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002454 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002455
2456 // Initialize the map from scope to scope specific properties.
2457 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
2458 for _, scope := range allApiScopes {
2459 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2460 }
2461 module.scopeToProperties = scopeToProperties
2462
Paul Duffin4911a892020-04-29 23:35:13 +01002463 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002464 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002465 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2466 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2467
Paul Duffin1b1e8062020-05-08 13:44:43 +01002468 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002469 // If no implementation is required then it cannot be used as a shared library
2470 // either.
2471 if !module.requiresRuntimeImplementationLibrary() {
2472 // If shared_library has been explicitly set to true then it is incompatible
2473 // with api_only: true.
2474 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2475 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2476 }
2477 // Set shared_library: false.
2478 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2479 }
2480
Paul Duffin1b1e8062020-05-08 13:44:43 +01002481 if module.initCommonAfterDefaultsApplied(ctx) {
2482 module.CreateInternalModules(ctx)
2483 }
2484 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002485 return module
2486}
Colin Cross79c7c262019-04-17 11:11:46 -07002487
2488//
2489// SDK library prebuilts
2490//
2491
Paul Duffin56d44902020-01-31 13:36:25 +00002492// Properties associated with each api scope.
2493type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002494 Jars []string `android:"path"`
2495
2496 Sdk_version *string
2497
Colin Cross79c7c262019-04-17 11:11:46 -07002498 // List of shared java libs that this module has dependencies to
2499 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002500
Paul Duffinc8782502020-04-29 20:45:27 +01002501 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002502 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002503
2504 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002505 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002506
2507 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002508 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002509
2510 // Annotation zip
2511 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002512}
2513
Paul Duffin56d44902020-01-31 13:36:25 +00002514type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002515 // List of shared java libs, common to all scopes, that this module has
2516 // dependencies to
2517 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002518
2519 // If set to true, compile dex files for the stubs. Defaults to false.
2520 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002521
2522 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002523 Permitted_packages []string
Paul Duffin56d44902020-01-31 13:36:25 +00002524}
2525
Paul Duffineedc5d52020-06-12 17:46:39 +01002526type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002527 android.ModuleBase
2528 android.DefaultableModuleBase
2529 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002530 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002531
Paul Duffin37856732021-02-26 14:24:15 +00002532 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002533 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002534
Colin Cross79c7c262019-04-17 11:11:46 -07002535 properties sdkLibraryImportProperties
2536
Paul Duffin46a26a82020-04-07 19:27:04 +01002537 // Map from api scope to the scope specific property structure.
2538 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2539
Paul Duffin56d44902020-01-31 13:36:25 +00002540 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002541
2542 // The reference to the implementation library created by the source module.
2543 // Is nil if the source module does not exist.
2544 implLibraryModule *Library
2545
2546 // The reference to the xml permissions module created by the source module.
2547 // Is nil if the source module does not exist.
2548 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002549
Jeongik Chad5fe8782021-07-08 01:13:11 +09002550 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00002551 dexJarFile OptionalDexJarPath
2552 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09002553
2554 // Expected install file path of the source module(sdk_library)
2555 // or dex implementation jar obtained from the prebuilt_apex, if any.
2556 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002557}
2558
Paul Duffineedc5d52020-06-12 17:46:39 +01002559var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002560
Paul Duffin46a26a82020-04-07 19:27:04 +01002561// The type of a structure that contains a field of type sdkLibraryScopeProperties
2562// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002563//
2564// struct {
2565// Public sdkLibraryScopeProperties
2566// System sdkLibraryScopeProperties
2567// ...
2568// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002569var allScopeStructType = createAllScopePropertiesStructType()
2570
2571// Dynamically create a structure type for each apiscope in allApiScopes.
2572func createAllScopePropertiesStructType() reflect.Type {
2573 var fields []reflect.StructField
2574 for _, apiScope := range allApiScopes {
2575 field := reflect.StructField{
2576 Name: apiScope.fieldName,
2577 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2578 }
2579 fields = append(fields, field)
2580 }
2581
2582 return reflect.StructOf(fields)
2583}
2584
2585// Create an instance of the scope specific structure type and return a map
2586// from apiscope to a pointer to each scope specific field.
2587func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2588 allScopePropertiesPtr := reflect.New(allScopeStructType)
2589 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2590 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2591
2592 for _, apiScope := range allApiScopes {
2593 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2594 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2595 }
2596
2597 return allScopePropertiesPtr.Interface(), scopeProperties
2598}
2599
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002600// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002601func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002602 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002603
Paul Duffin46a26a82020-04-07 19:27:04 +01002604 allScopeProperties, scopeToProperties := createPropertiesInstance()
2605 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002606 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002607
Paul Duffinc3091c82020-05-08 14:16:20 +01002608 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002609 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002610
Paul Duffin0bdcb272020-02-06 15:24:57 +00002611 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002612 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002613 InitJavaModule(module, android.HostAndDeviceSupported)
2614
Paul Duffin1b1e8062020-05-08 13:44:43 +01002615 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2616 if module.initCommonAfterDefaultsApplied(mctx) {
2617 module.createInternalModules(mctx)
2618 }
2619 })
Colin Cross79c7c262019-04-17 11:11:46 -07002620 return module
2621}
2622
Paul Duffin630b11e2021-07-15 13:35:26 +01002623var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2624
2625func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2626 return module.properties.Permitted_packages
2627}
2628
Paul Duffineedc5d52020-06-12 17:46:39 +01002629func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002630 return &module.prebuilt
2631}
2632
Paul Duffineedc5d52020-06-12 17:46:39 +01002633func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002634 return module.prebuilt.Name(module.ModuleBase.Name())
2635}
2636
Paul Duffineedc5d52020-06-12 17:46:39 +01002637func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002638
Paul Duffin50061512020-01-21 16:31:05 +00002639 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002640 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002641 module.prebuilt.ForcePrefer()
2642 }
2643
Paul Duffin46a26a82020-04-07 19:27:04 +01002644 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002645 if len(scopeProperties.Jars) == 0 {
2646 continue
2647 }
2648
Paul Duffinbbb546b2020-04-09 00:07:11 +01002649 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002650
Paul Duffin0f8faff2020-05-20 16:18:00 +01002651 if len(scopeProperties.Stub_srcs) > 0 {
2652 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2653 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002654
2655 if scopeProperties.Current_api != nil {
2656 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2657 }
Paul Duffin56d44902020-01-31 13:36:25 +00002658 }
Colin Cross79c7c262019-04-17 11:11:46 -07002659
2660 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2661 javaSdkLibrariesLock.Lock()
2662 defer javaSdkLibrariesLock.Unlock()
2663 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2664}
2665
Paul Duffineedc5d52020-06-12 17:46:39 +01002666func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002667 // Creates a java import for the jar with ".stubs" suffix
2668 props := struct {
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002669 Name *string
2670 Sdk_version *string
2671 Libs []string
2672 Jars []string
Paul Duffin1267d872021-04-16 17:21:36 +01002673 Compile_dex *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002674
2675 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002676 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002677 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Paul Duffinbbb546b2020-04-09 00:07:11 +01002678 props.Sdk_version = scopeProperties.Sdk_version
2679 // Prepend any of the libs from the legacy public properties to the libs for each of the
2680 // scopes to avoid having to duplicate them in each scope.
2681 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2682 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002683
Paul Duffin38b57852020-05-13 16:08:09 +01002684 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002685 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002686
Paul Duffin1267d872021-04-16 17:21:36 +01002687 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002688 compileDex := module.properties.Compile_dex
2689 if module.stubLibrariesCompiledForDex() {
2690 compileDex = proptools.BoolPtr(true)
2691 }
2692 props.Compile_dex = compileDex
Paul Duffin1267d872021-04-16 17:21:36 +01002693
Paul Duffin859fe962020-05-15 10:20:31 +01002694 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002695}
2696
Paul Duffineedc5d52020-06-12 17:46:39 +01002697func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002698 props := struct {
Paul Duffinbf4de042022-09-27 12:41:52 +01002699 Name *string
2700 Srcs []string
2701
2702 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002703 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002704 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Paul Duffin3d1248c2020-04-09 00:10:17 +01002705 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002706
2707 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002708 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2709
Spandan Das2cc80ba2023-10-27 17:21:52 +00002710 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002711}
2712
Jihoon Kang71c86832023-09-13 01:01:53 +00002713func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2714 api_file := scopeProperties.Current_api
2715 api_surface := &apiScope.name
2716
2717 props := struct {
2718 Name *string
2719 Api_surface *string
2720 Api_file *string
2721 Visibility []string
2722 }{}
2723
2724 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
2725 props.Api_surface = api_surface
2726 props.Api_file = api_file
2727 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2728
Spandan Das2cc80ba2023-10-27 17:21:52 +00002729 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002730}
2731
Paul Duffin44f1d842020-06-26 20:17:02 +01002732// Add the dependencies on the child module in the component deps mutator so that it
2733// creates references to the prebuilt and not the source modules.
2734func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002735 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002736 if len(scopeProperties.Jars) == 0 {
2737 continue
2738 }
2739
2740 // Add dependencies to the prebuilt stubs library
Jihoon Kangb7431552024-01-22 19:40:08 +00002741 ctx.AddVariationDependencies(nil, apiScope.prebuiltStubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002742
2743 if len(scopeProperties.Stub_srcs) > 0 {
2744 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002745 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002746 }
Paul Duffin56d44902020-01-31 13:36:25 +00002747 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002748}
2749
2750// Add other dependencies as normal.
2751func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002752
2753 implName := module.implLibraryModuleName()
2754 if ctx.OtherModuleExists(implName) {
2755 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2756
2757 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2758 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2759 // Add dependency to the rule for generating the xml permissions file
2760 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2761 }
2762 }
Colin Cross79c7c262019-04-17 11:11:46 -07002763}
2764
Jiyong Park45bf82e2020-12-15 22:29:02 +09002765var _ android.ApexModule = (*SdkLibraryImport)(nil)
2766
2767// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002768func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2769 depTag := mctx.OtherModuleDependencyTag(dep)
2770 if depTag == xmlPermissionsFileTag {
2771 return true
2772 }
2773
2774 // None of the other dependencies of the java_sdk_library_import are in the same apex
2775 // as the one that references this module.
2776 return false
2777}
2778
Jiyong Park45bf82e2020-12-15 22:29:02 +09002779// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002780func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2781 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002782 // we don't check prebuilt modules for sdk_version
2783 return nil
2784}
2785
Paul Duffinea8f8082021-06-24 13:25:57 +01002786// Implements android.ApexModule
2787func (module *SdkLibraryImport) UniqueApexVariations() bool {
2788 return module.uniqueApexVariations()
2789}
2790
Paul Duffin09817d62022-04-28 17:45:11 +01002791// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002792func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2793 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002794}
2795
2796var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2797
Paul Duffineedc5d52020-06-12 17:46:39 +01002798func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
Paul Duffin1e940d52022-04-29 14:21:25 +01002799 paths, err := module.commonOutputFiles(tag)
2800 if paths != nil || err != nil {
2801 return paths, err
2802 }
2803 if module.implLibraryModule != nil {
2804 return module.implLibraryModule.OutputFiles(tag)
2805 } else {
2806 return nil, nil
2807 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01002808}
2809
Paul Duffineedc5d52020-06-12 17:46:39 +01002810func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002811 module.generateCommonBuildActions(ctx)
2812
Jeongik Chad5fe8782021-07-08 01:13:11 +09002813 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2814 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2815
Paul Duffin0f8faff2020-05-20 16:18:00 +01002816 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002817 ctx.VisitDirectDeps(func(to android.Module) {
2818 tag := ctx.OtherModuleDependencyTag(to)
2819
Paul Duffin0f8faff2020-05-20 16:18:00 +01002820 // Extract information from any of the scope specific dependencies.
2821 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2822 apiScope := scopeTag.apiScope
2823 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2824
2825 // Extract information from the dependency. The exact information extracted
2826 // is determined by the nature of the dependency which is determined by the tag.
2827 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002828 } else if tag == implLibraryTag {
2829 if implLibrary, ok := to.(*Library); ok {
2830 module.implLibraryModule = implLibrary
2831 } else {
2832 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2833 }
2834 } else if tag == xmlPermissionsFileTag {
2835 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2836 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2837 } else {
2838 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2839 }
Colin Cross79c7c262019-04-17 11:11:46 -07002840 }
2841 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002842
2843 // Populate the scope paths with information from the properties.
2844 for apiScope, scopeProperties := range module.scopeProperties {
2845 if len(scopeProperties.Jars) == 0 {
2846 continue
2847 }
2848
2849 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002850 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002851 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2852 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2853 }
Paul Duffin39853512021-02-26 11:09:39 +00002854
2855 if ctx.Device() {
2856 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2857 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08002858 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00002859 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002860 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00002861 di, err := android.FindDeapexerProviderForModule(ctx)
2862 if err != nil {
2863 // An error was found, possibly due to multiple apexes in the tree that export this library
2864 // Defer the error till a client tries to call DexJarBuildPath
2865 module.dexJarFileErr = err
Spandan Das3a392012024-01-17 18:26:27 +00002866 module.initHiddenAPIError(err)
Spandan Dasfae468e2023-12-12 23:23:53 +00002867 return
Martin Stjernholm44825602021-09-17 01:44:12 +01002868 }
Spandan Das5be63332023-12-13 00:06:32 +00002869 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(module.BaseModuleName())
Jiakai Zhang81e46812023-02-08 21:56:07 +08002870 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002871 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2872 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002873 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002874 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002875 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002876 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002877
Jiakai Zhang204356f2021-09-09 08:12:46 +00002878 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, installPath)
2879 module.dexpreopter.isSDKLibrary = true
2880 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002881
2882 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2883 module.dexpreopter.inputProfilePathOnHost = profilePath
2884 }
2885
2886 // Dexpreopting.
Jiakai Zhang204356f2021-09-09 08:12:46 +00002887 module.dexpreopt(ctx, dexOutputPath)
Paul Duffin39853512021-02-26 11:09:39 +00002888 } else {
2889 // This should never happen as a variant for a prebuilt_apex is only created if the
2890 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002891 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002892 }
2893 }
2894 }
Colin Cross79c7c262019-04-17 11:11:46 -07002895}
2896
Jiyong Parkf1691d22021-03-29 20:11:58 +09002897func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec, headerJars bool) android.Paths {
Paul Duffineedc5d52020-06-12 17:46:39 +01002898
2899 // For consistency with SdkLibrary make the implementation jar available to libraries that
2900 // are within the same APEX.
2901 implLibraryModule := module.implLibraryModule
Colin Cross56a83212020-09-15 18:30:11 -07002902 if implLibraryModule != nil && withinSameApexesAs(ctx, module) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002903 if headerJars {
2904 return implLibraryModule.HeaderJars()
2905 } else {
2906 return implLibraryModule.ImplementationJars()
2907 }
2908 }
2909
Paul Duffin23970f42020-05-20 14:20:02 +01002910 return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
Paul Duffin56d44902020-01-31 13:36:25 +00002911}
2912
Colin Cross79c7c262019-04-17 11:11:46 -07002913// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002914func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002915 // This module is just a wrapper for the prebuilt stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002916 return module.sdkJars(ctx, sdkVersion, true)
Colin Cross79c7c262019-04-17 11:11:46 -07002917}
2918
2919// to satisfy SdkLibraryDependency interface
Jiyong Parkf1691d22021-03-29 20:11:58 +09002920func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion android.SdkSpec) android.Paths {
Colin Cross79c7c262019-04-17 11:11:46 -07002921 // This module is just a wrapper for the stubs.
Paul Duffineedc5d52020-06-12 17:46:39 +01002922 return module.sdkJars(ctx, sdkVersion, false)
2923}
2924
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002925// to satisfy UsesLibraryDependency interface
Spandan Das59a4a2b2024-01-09 21:35:56 +00002926func (module *SdkLibraryImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002927 // The dex implementation jar extracted from the .apex file should be used in preference to the
2928 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00002929 if module.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002930 ctx.ModuleErrorf(module.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00002931 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002932 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002933 return module.dexJarFile
2934 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002935 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002936 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002937 } else {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002938 return module.implLibraryModule.DexJarBuildPath(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +01002939 }
2940}
2941
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002942// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002943func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002944 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002945}
2946
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002947// to satisfy UsesLibraryDependency interface
2948func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2949 return nil
2950}
2951
Paul Duffineedc5d52020-06-12 17:46:39 +01002952// to satisfy apex.javaDependency interface
2953func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2954 if module.implLibraryModule == nil {
2955 return nil
2956 } else {
2957 return module.implLibraryModule.JacocoReportClassesFile()
2958 }
2959}
2960
2961// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002962func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2963 if module.implLibraryModule == nil {
2964 return LintDepSets{}
2965 } else {
2966 return module.implLibraryModule.LintDepSets()
2967 }
2968}
2969
Spandan Das17854f52022-01-14 21:19:14 +00002970func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002971 if module.implLibraryModule == nil {
2972 return false
2973 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002974 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002975 }
2976}
2977
Spandan Das17854f52022-01-14 21:19:14 +00002978func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002979 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002980 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002981 }
2982}
2983
Colin Cross08dca382020-07-21 20:31:17 -07002984// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002985func (module *SdkLibraryImport) Stem() string {
2986 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002987}
Jiyong Parke3833882020-02-17 17:28:10 +09002988
Paul Duffin44b481b2020-06-17 16:59:43 +01002989var _ ApexDependency = (*SdkLibraryImport)(nil)
2990
2991// to satisfy java.ApexDependency interface
2992func (module *SdkLibraryImport) HeaderJars() android.Paths {
2993 if module.implLibraryModule == nil {
2994 return nil
2995 } else {
2996 return module.implLibraryModule.HeaderJars()
2997 }
2998}
2999
3000// to satisfy java.ApexDependency interface
3001func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
3002 if module.implLibraryModule == nil {
3003 return nil
3004 } else {
3005 return module.implLibraryModule.ImplementationAndResourcesJars()
3006 }
3007}
3008
Jiakai Zhang204356f2021-09-09 08:12:46 +00003009// to satisfy java.DexpreopterInterface interface
3010func (module *SdkLibraryImport) IsInstallable() bool {
3011 return true
3012}
3013
Paul Duffinfef55002021-06-17 14:56:05 +01003014var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
3015
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01003016func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01003017 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08003018 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01003019}
3020
Jiyong Parke3833882020-02-17 17:28:10 +09003021// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09003022type sdkLibraryXml struct {
3023 android.ModuleBase
3024 android.DefaultableModuleBase
3025 android.ApexModuleBase
3026
3027 properties sdkLibraryXmlProperties
3028
3029 outputFilePath android.OutputPath
3030 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07003031
3032 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09003033}
3034
3035type sdkLibraryXmlProperties struct {
3036 // canonical name of the lib
3037 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003038
3039 // Signals that this shared library is part of the bootclasspath starting
3040 // on the version indicated in this attribute.
3041 //
3042 // This will make platforms at this level and above to ignore
3043 // <uses-library> tags with this library name because the library is already
3044 // available
3045 On_bootclasspath_since *string
3046
3047 // Signals that this shared library was part of the bootclasspath before
3048 // (but not including) the version indicated in this attribute.
3049 //
3050 // The system will automatically add a <uses-library> tag with this library to
3051 // apps that target any SDK less than the version indicated in this attribute.
3052 On_bootclasspath_before *string
3053
3054 // Indicates that PackageManager should ignore this shared library if the
3055 // platform is below the version indicated in this attribute.
3056 //
3057 // This means that the device won't recognise this library as installed.
3058 Min_device_sdk *string
3059
3060 // Indicates that PackageManager should ignore this shared library if the
3061 // platform is above the version indicated in this attribute.
3062 //
3063 // This means that the device won't recognise this library as installed.
3064 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00003065
3066 // The SdkLibrary's min api level as a string
3067 //
3068 // This value comes from the ApiLevel of the MinSdkVersion property.
3069 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00003070
3071 // Uses-libs dependencies that the shared library requires to work correctly.
3072 //
3073 // This will add dependency="foo:bar" to the <library> section.
3074 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09003075}
3076
3077// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
3078// Not to be used directly by users. java_sdk_library internally uses this.
3079func sdkLibraryXmlFactory() android.Module {
3080 module := &sdkLibraryXml{}
3081
3082 module.AddProperties(&module.properties)
3083
3084 android.InitApexModule(module)
3085 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
3086
3087 return module
3088}
3089
Colin Crossaede88c2020-08-11 12:17:01 -07003090func (module *sdkLibraryXml) UniqueApexVariations() bool {
3091 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
3092 // mounted APEX, which contains the name of the APEX.
3093 return true
3094}
3095
Jiyong Parke3833882020-02-17 17:28:10 +09003096// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09003097func (module *sdkLibraryXml) BaseDir() string {
3098 return "etc"
3099}
3100
3101// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09003102func (module *sdkLibraryXml) SubDir() string {
3103 return "permissions"
3104}
3105
3106// from android.PrebuiltEtcModule
3107func (module *sdkLibraryXml) OutputFile() android.OutputPath {
3108 return module.outputFilePath
3109}
3110
3111// from android.ApexModule
3112func (module *sdkLibraryXml) AvailableFor(what string) bool {
3113 return true
3114}
3115
3116func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
3117 // do nothing
3118}
3119
Jiyong Park45bf82e2020-12-15 22:29:02 +09003120var _ android.ApexModule = (*sdkLibraryXml)(nil)
3121
3122// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003123func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3124 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003125 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
3126 return nil
3127}
3128
Jiyong Parke3833882020-02-17 17:28:10 +09003129// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07003130func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09003131 implName := proptools.String(module.properties.Lib_name)
Colin Crossff694a82023-12-13 15:54:49 -08003132 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07003133 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09003134 // In most cases, this works fine. But when apex_name is set or override_apex is used
3135 // this can be wrong.
Colin Cross56a83212020-09-15 18:30:11 -07003136 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.ApexVariationName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09003137 }
3138 partition := "system"
3139 if module.SocSpecific() {
3140 partition = "vendor"
3141 } else if module.DeviceSpecific() {
3142 partition = "odm"
3143 } else if module.ProductSpecific() {
3144 partition = "product"
3145 } else if module.SystemExtSpecific() {
3146 partition = "system_ext"
3147 }
3148 return "/" + partition + "/framework/" + implName + ".jar"
3149}
3150
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003151func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
3152 if value == nil {
3153 return ""
3154 }
3155 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
3156 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003157 // attributes in bp files have underscores but in the xml have dashes.
3158 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003159 return ""
3160 }
Pedro Loureirob638c622021-12-22 15:28:05 +00003161 if apiLevel.IsCurrent() {
3162 // passing "current" would always mean a future release, never the current (or the current in
3163 // progress) which means some conditions would never be triggered.
3164 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3165 `"current" is not an allowed value for this attribute`)
3166 return ""
3167 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003168 // "safeValue" is safe because it translates finalized codenames to a string
3169 // with their SDK int.
3170 safeValue := apiLevel.String()
3171 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003172}
3173
3174// formats an attribute for the xml permissions file if the value is not null
3175// returns empty string otherwise
3176func formattedOptionalAttribute(attrName string, value *string) string {
3177 if value == nil {
3178 return ""
3179 }
3180 return fmt.Sprintf(` %s=\"%s\"\n`, attrName, *value)
3181}
3182
Jamie Garsidee570ace2023-11-27 12:07:36 +00003183func formattedDependenciesAttribute(dependencies []string) string {
3184 if dependencies == nil {
3185 return ""
3186 }
3187 return fmt.Sprintf(` dependency=\"%s\"\n`, strings.Join(dependencies, ":"))
3188}
3189
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003190func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3191 libName := proptools.String(module.properties.Lib_name)
3192 libNameAttr := formattedOptionalAttribute("name", &libName)
3193 filePath := module.implPath(ctx)
3194 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003195 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3196 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3197 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3198 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003199 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003200 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3201 // 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 +00003202 var libraryTag string
3203 if module.properties.Min_device_sdk != nil {
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003204 libraryTag = ` <apex-library\n`
Pedro Loureiroc3621422021-09-28 15:40:23 +00003205 } else {
3206 libraryTag = ` <library\n`
3207 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003208
3209 return strings.Join([]string{
3210 `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n`,
3211 `<!-- Copyright (C) 2018 The Android Open Source Project\n`,
3212 `\n`,
3213 ` Licensed under the Apache License, Version 2.0 (the \"License\");\n`,
3214 ` you may not use this file except in compliance with the License.\n`,
3215 ` You may obtain a copy of the License at\n`,
3216 `\n`,
3217 ` http://www.apache.org/licenses/LICENSE-2.0\n`,
3218 `\n`,
3219 ` Unless required by applicable law or agreed to in writing, software\n`,
3220 ` distributed under the License is distributed on an \"AS IS\" BASIS,\n`,
3221 ` WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n`,
3222 ` See the License for the specific language governing permissions and\n`,
3223 ` limitations under the License.\n`,
3224 `-->\n`,
3225 `<permissions>\n`,
Pedro Loureiroc3621422021-09-28 15:40:23 +00003226 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003227 libNameAttr,
3228 filePathAttr,
3229 implicitFromAttr,
3230 implicitUntilAttr,
3231 minSdkAttr,
3232 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003233 dependenciesAttr,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003234 ` />\n`,
3235 `</permissions>\n`}, "")
3236}
3237
Jiyong Parke3833882020-02-17 17:28:10 +09003238func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossff694a82023-12-13 15:54:49 -08003239 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
3240 module.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07003241
Jiyong Parke3833882020-02-17 17:28:10 +09003242 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003243 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003244 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003245
3246 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -08003247 rule := android.NewRuleBuilder(pctx, ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003248 rule.Command().
3249 Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
3250 Output(module.outputFilePath)
3251
Colin Crossf1a035e2020-11-16 17:32:30 -08003252 rule.Build("java_sdk_xml", "Permission XML")
Jiyong Parke3833882020-02-17 17:28:10 +09003253
3254 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
3255}
3256
3257func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003258 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003259 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003260 Disabled: true,
3261 }}
3262 }
3263
satayev8f088b02021-12-06 11:40:46 +00003264 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003265 Class: "ETC",
3266 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3267 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003268 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003269 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003270 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003271 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3272 },
3273 },
3274 }}
3275}
Paul Duffindd46f712020-02-10 13:37:10 +00003276
Pedro Loureiroc3621422021-09-28 15:40:23 +00003277func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3278 module.validateAtLeastTAttributes(ctx)
3279 module.validateMinAndMaxDeviceSdk(ctx)
3280 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3281 module.validateOnBootclasspathBeforeRequirements(ctx)
3282}
3283
3284func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3285 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3286 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3287 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3288 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3289 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3290}
3291
3292func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3293 if attr != nil {
3294 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3295 // we will inform the user of invalid inputs when we try to write the
3296 // permissions xml file so we don't need to do it here
3297 if t.GreaterThan(level) {
3298 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3299 }
3300 }
3301 }
3302}
3303
3304func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3305 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3306 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3307 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3308 if minErr == nil && maxErr == nil {
3309 // we will inform the user of invalid inputs when we try to write the
3310 // permissions xml file so we don't need to do it here
3311 if min.GreaterThan(max) {
3312 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3313 }
3314 }
3315 }
3316}
3317
3318func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3319 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3320 if module.properties.Min_device_sdk != nil {
3321 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3322 if err == nil {
3323 if moduleMinApi.GreaterThan(api) {
3324 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3325 }
3326 }
3327 }
3328 if module.properties.Max_device_sdk != nil {
3329 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3330 if err == nil {
3331 if moduleMinApi.GreaterThan(api) {
3332 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3333 }
3334 }
3335 }
3336}
3337
3338func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3339 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3340 if module.properties.On_bootclasspath_before != nil {
3341 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3342 // if we use the attribute, then we need to do this validation
3343 if moduleMinApi.LessThan(t) {
3344 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3345 if module.properties.Min_device_sdk == nil {
3346 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")
3347 }
3348 }
3349 }
3350}
3351
Paul Duffindd46f712020-02-10 13:37:10 +00003352type sdkLibrarySdkMemberType struct {
3353 android.SdkMemberTypeBase
3354}
3355
Paul Duffin296701e2021-07-14 10:29:36 +01003356func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3357 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003358}
3359
3360func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3361 _, ok := module.(*SdkLibrary)
3362 return ok
3363}
3364
3365func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3366 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3367}
3368
3369func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3370 return &sdkLibrarySdkMemberProperties{}
3371}
3372
Paul Duffin976b0e52021-04-27 23:20:26 +01003373var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3374 android.SdkMemberTypeBase{
3375 PropertyName: "java_sdk_libs",
3376 SupportsSdk: true,
3377 },
3378}
3379
Paul Duffindd46f712020-02-10 13:37:10 +00003380type sdkLibrarySdkMemberProperties struct {
3381 android.SdkMemberPropertiesBase
3382
Paul Duffine8409952022-09-22 16:24:46 +01003383 // Stem name for files in the sdk snapshot.
3384 //
3385 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3386 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3387 //
3388 // This property is marked as keep so that it will be kept in all instances of this struct, will
3389 // not be cleared but will be copied to common structs. That is needed because this field is used
3390 // to construct many file names for other parts of this struct and so it needs to be present in
3391 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3392 // be unavailable for generating file names if there were other properties that were still set.
3393 Stem string `sdk:"keep"`
3394
Paul Duffindd46f712020-02-10 13:37:10 +00003395 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003396 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003397
Paul Duffin3d1248c2020-04-09 00:10:17 +01003398 // The Java stubs source files.
3399 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003400
3401 // The naming scheme.
3402 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003403
3404 // True if the java_sdk_library_import is for a shared library, false
3405 // otherwise.
3406 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003407
Paul Duffin1267d872021-04-16 17:21:36 +01003408 // True if the stub imports should produce dex jars.
3409 Compile_dex *bool
3410
Paul Duffina2ae7e02020-09-11 11:55:00 +01003411 // The paths to the doctag files to add to the prebuilt.
3412 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003413
3414 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003415
3416 // Signals that this shared library is part of the bootclasspath starting
3417 // on the version indicated in this attribute.
3418 //
3419 // This will make platforms at this level and above to ignore
3420 // <uses-library> tags with this library name because the library is already
3421 // available
3422 On_bootclasspath_since *string
3423
3424 // Signals that this shared library was part of the bootclasspath before
3425 // (but not including) the version indicated in this attribute.
3426 //
3427 // The system will automatically add a <uses-library> tag with this library to
3428 // apps that target any SDK less than the version indicated in this attribute.
3429 On_bootclasspath_before *string
3430
3431 // Indicates that PackageManager should ignore this shared library if the
3432 // platform is below the version indicated in this attribute.
3433 //
3434 // This means that the device won't recognise this library as installed.
3435 Min_device_sdk *string
3436
3437 // Indicates that PackageManager should ignore this shared library if the
3438 // platform is above the version indicated in this attribute.
3439 //
3440 // This means that the device won't recognise this library as installed.
3441 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003442
3443 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003444}
3445
3446type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003447 Jars android.Paths
3448 StubsSrcJar android.Path
3449 CurrentApiFile android.Path
3450 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003451 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003452 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003453}
3454
3455func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3456 sdk := variant.(*SdkLibrary)
3457
Paul Duffine8409952022-09-22 16:24:46 +01003458 // Copy the stem name for files in the sdk snapshot.
3459 s.Stem = sdk.distStem()
3460
Paul Duffin106a3a42022-01-27 16:39:06 +00003461 s.Scopes = make(map[*apiScope]*scopeProperties)
Paul Duffindd46f712020-02-10 13:37:10 +00003462 for _, apiScope := range allApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003463 paths := sdk.findScopePaths(apiScope)
3464 if paths == nil {
3465 continue
3466 }
3467
Paul Duffindd46f712020-02-10 13:37:10 +00003468 jars := paths.stubsImplPath
3469 if len(jars) > 0 {
3470 properties := scopeProperties{}
3471 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003472 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003473 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003474 if paths.currentApiFilePath.Valid() {
3475 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3476 }
3477 if paths.removedApiFilePath.Valid() {
3478 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3479 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003480 // The annotations zip is only available for modules that set annotations_enabled: true.
3481 if paths.annotationsZip.Valid() {
3482 properties.AnnotationsZip = paths.annotationsZip.Path()
3483 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003484 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003485 }
3486 }
3487
Paul Duffindfa131e2020-05-15 20:37:11 +01003488 s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
Paul Duffind7eb1c22020-05-26 20:57:10 +01003489 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003490 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003491 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003492 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003493 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3494 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3495 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3496 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003497
3498 if sdk.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
3499 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3500 }
Paul Duffindd46f712020-02-10 13:37:10 +00003501}
3502
3503func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003504 if s.Naming_scheme != nil {
3505 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3506 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003507 if s.Shared_library != nil {
3508 propertySet.AddProperty("shared_library", *s.Shared_library)
3509 }
Paul Duffin1267d872021-04-16 17:21:36 +01003510 if s.Compile_dex != nil {
3511 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3512 }
Paul Duffin869de142021-07-15 14:14:41 +01003513 if len(s.Permitted_packages) > 0 {
3514 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3515 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003516 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3517 if s.DexPreoptProfileGuided != nil {
3518 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3519 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003520
Paul Duffine8409952022-09-22 16:24:46 +01003521 stem := s.Stem
3522
Paul Duffindd46f712020-02-10 13:37:10 +00003523 for _, apiScope := range allApiScopes {
3524 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003525 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003526
Paul Duffin958806b2022-05-16 13:10:47 +00003527 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003528
Paul Duffindd46f712020-02-10 13:37:10 +00003529 var jars []string
3530 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003531 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003532 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3533 jars = append(jars, dest)
3534 }
3535 scopeSet.AddProperty("jars", jars)
3536
Paul Duffin22628d52021-05-12 23:13:22 +01003537 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3538 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003539 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003540 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3541 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3542 } else {
3543 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3544 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003545 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003546 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3547 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3548 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003549
Paul Duffin1fd005d2020-04-09 01:08:11 +01003550 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003551 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003552 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3553 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3554 }
3555
3556 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003557 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003558 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003559 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3560 }
3561
Anton Hanssond78eb762021-09-21 15:25:12 +01003562 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003563 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003564 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3565 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3566 }
3567
Paul Duffindd46f712020-02-10 13:37:10 +00003568 if properties.SdkVersion != "" {
3569 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3570 }
3571 }
3572 }
3573
Paul Duffina2ae7e02020-09-11 11:55:00 +01003574 if len(s.Doctag_paths) > 0 {
3575 dests := []string{}
3576 for _, p := range s.Doctag_paths {
3577 dest := filepath.Join("doctags", p.Rel())
3578 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3579 dests = append(dests, dest)
3580 }
3581 propertySet.AddProperty("doctag_files", dests)
3582 }
Paul Duffindd46f712020-02-10 13:37:10 +00003583}