blob: c3ffb46d589bb7fa0314277ed561b3f294409a96 [file] [log] [blame]
Jiyong Parkc678ad32018-04-10 13:07:10 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jihoon Kangee113282024-01-23 00:16:41 +000018 "errors"
Jiyong Parkc678ad32018-04-10 13:07:10 +090019 "fmt"
20 "path"
Sundong Ahn054b19a2018-10-19 13:46:09 +090021 "path/filepath"
Paul Duffin46a26a82020-04-07 19:27:04 +010022 "reflect"
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"
ThiƩbaud Weksteen00e8b312024-03-18 14:06:00 +110032 "android/soong/etc"
Jiyong Parkc678ad32018-04-10 13:07:10 +090033)
34
Jooyung Han58f26ab2019-12-18 15:34:32 +090035const (
Pedro Loureiro9956e5e2021-09-07 17:21:59 +000036 sdkXmlFileSuffix = ".xml"
Jiyong Parkc678ad32018-04-10 13:07:10 +090037)
38
Paul Duffind1b3a922020-01-22 11:57:20 +000039// A tag to associated a dependency with a specific api scope.
40type scopeDependencyTag struct {
41 blueprint.BaseDependencyTag
42 name string
43 apiScope *apiScope
Paul Duffinc8782502020-04-29 20:45:27 +010044
45 // Function for extracting appropriate path information from the dependency.
Colin Crossdcf71b22021-02-01 13:59:03 -080046 depInfoExtractor func(paths *scopePaths, ctx android.ModuleContext, dep android.Module) error
Paul Duffinc8782502020-04-29 20:45:27 +010047}
48
49// Extract tag specific information from the dependency.
50func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
Colin Crossdcf71b22021-02-01 13:59:03 -080051 err := tag.depInfoExtractor(paths, ctx, dep)
Paul Duffinc8782502020-04-29 20:45:27 +010052 if err != nil {
53 ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
54 }
Paul Duffind1b3a922020-01-22 11:57:20 +000055}
56
Paul Duffin80342d72020-06-26 22:08:43 +010057var _ android.ReplaceSourceWithPrebuilt = (*scopeDependencyTag)(nil)
58
59func (tag scopeDependencyTag) ReplaceSourceWithPrebuilt() bool {
60 return false
61}
62
Paul Duffind1b3a922020-01-22 11:57:20 +000063// Provides information about an api scope, e.g. public, system, test.
64type apiScope struct {
65 // The name of the api scope, e.g. public, system, test
66 name string
67
Paul Duffin97b53b82020-05-05 14:40:52 +010068 // The api scope that this scope extends.
Paul Duffind0b9fca2022-09-30 18:11:41 +010069 //
70 // This organizes the scopes into an extension hierarchy.
71 //
72 // If set this means that the API provided by this scope includes the API provided by the scope
73 // set in this field.
Paul Duffin97b53b82020-05-05 14:40:52 +010074 extends *apiScope
75
Paul Duffind0b9fca2022-09-30 18:11:41 +010076 // The next api scope that a library that uses this scope can access.
77 //
78 // This organizes the scopes into an access hierarchy.
79 //
80 // If set this means that a library that can access this API can also access the API provided by
81 // the scope set in this field.
82 //
83 // A module that sets sdk_version: "<scope>_current" should have access to the <scope> API of
84 // every java_sdk_library that it depends on. If the library does not provide an API for <scope>
85 // then it will traverse up this access hierarchy to find an API that it does provide.
86 //
87 // If this is not set then it defaults to the scope set in extends.
88 canAccess *apiScope
89
Paul Duffin3375e352020-04-28 10:44:03 +010090 // The legacy enabled status for a specific scope can be dependent on other
91 // properties that have been specified on the library so it is provided by
92 // a function that can determine the status by examining those properties.
93 legacyEnabledStatus func(module *SdkLibrary) bool
94
95 // The default enabled status for non-legacy behavior, which is triggered by
96 // explicitly enabling at least one api scope.
97 defaultEnabledStatus bool
98
99 // Gets a pointer to the scope specific properties.
100 scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
101
Paul Duffin46a26a82020-04-07 19:27:04 +0100102 // The name of the field in the dynamically created structure.
103 fieldName string
104
Paul Duffin6b836ba2020-05-13 19:19:49 +0100105 // The name of the property in the java_sdk_library_import
106 propertyName string
107
Jihoon Kangb7431552024-01-22 19:40:08 +0000108 // The tag to use to depend on the prebuilt stubs library module
109 prebuiltStubsTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000110
Jihoon Kangbd093452023-12-26 19:08:01 +0000111 // The tag to use to depend on the everything stubs library module.
112 everythingStubsTag scopeDependencyTag
113
114 // The tag to use to depend on the exportable stubs library module.
115 exportableStubsTag scopeDependencyTag
116
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100117 // The tag to use to depend on the stubs source module (if separate from the API module).
118 stubsSourceTag scopeDependencyTag
119
Paul Duffinc8782502020-04-29 20:45:27 +0100120 // The tag to use to depend on the stubs source and API module.
121 stubsSourceAndApiTag scopeDependencyTag
Paul Duffind1b3a922020-01-22 11:57:20 +0000122
Paul Duffin958806b2022-05-16 13:10:47 +0000123 // The tag to use to depend on the module that provides the latest version of the API .txt file.
124 latestApiModuleTag scopeDependencyTag
125
126 // The tag to use to depend on the module that provides the latest version of the API removed.txt
127 // file.
128 latestRemovedApiModuleTag scopeDependencyTag
129
Paul Duffind1b3a922020-01-22 11:57:20 +0000130 // The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
131 apiFilePrefix string
132
Paul Duffind0b9fca2022-09-30 18:11:41 +0100133 // The scope specific suffix to add to the sdk library module name to construct a scope specific
Paul Duffind1b3a922020-01-22 11:57:20 +0000134 // module name.
135 moduleSuffix string
136
Paul Duffind1b3a922020-01-22 11:57:20 +0000137 // SDK version that the stubs library is built against. Note that this is always
138 // *current. Older stubs library built with a numbered SDK version is created from
139 // the prebuilt jar.
140 sdkVersion string
Paul Duffin1fb487d2020-04-07 18:50:10 +0100141
Paul Duffin15f34ef2020-07-20 18:04:44 +0100142 // The annotation that identifies this API level, empty for the public API scope.
143 annotation string
144
Paul Duffin1fb487d2020-04-07 18:50:10 +0100145 // Extra arguments to pass to droidstubs for this scope.
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100146 //
Paul Duffin15f34ef2020-07-20 18:04:44 +0100147 // This is not used directly but is used to construct the droidstubsArgs.
148 extraArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100149
Paul Duffin15f34ef2020-07-20 18:04:44 +0100150 // The args that must be passed to droidstubs to generate the API and stubs source
151 // for this scope, constructed dynamically by initApiScope().
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100152 //
153 // The API only includes the additional members that this scope adds over the scope
154 // that it extends.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100155 //
156 // The stubs source must include the definitions of everything that is in this
157 // api scope and all the scopes that this one extends.
158 droidstubsArgs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100159
Anton Hansson6478ac12020-05-02 11:19:36 +0100160 // Whether the api scope can be treated as unstable, and should skip compat checks.
161 unstable bool
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000162
163 // Represents the SDK kind of this scope.
164 kind android.SdkKind
Paul Duffind1b3a922020-01-22 11:57:20 +0000165}
166
167// Initialize a scope, creating and adding appropriate dependency tags
168func initApiScope(scope *apiScope) *apiScope {
Paul Duffinc8782502020-04-29 20:45:27 +0100169 name := scope.name
Paul Duffin46dc45a2020-05-14 15:39:10 +0100170 scopeByName[name] = scope
171 allScopeNames = append(allScopeNames, name)
Paul Duffin6b836ba2020-05-13 19:19:49 +0100172 scope.propertyName = strings.ReplaceAll(name, "-", "_")
173 scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
Jihoon Kangb7431552024-01-22 19:40:08 +0000174 scope.prebuiltStubsTag = scopeDependencyTag{
Paul Duffinc8782502020-04-29 20:45:27 +0100175 name: name + "-stubs",
176 apiScope: scope,
177 depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
Paul Duffind1b3a922020-01-22 11:57:20 +0000178 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000179 scope.everythingStubsTag = scopeDependencyTag{
180 name: name + "-stubs-everything",
181 apiScope: scope,
182 depInfoExtractor: (*scopePaths).extractEverythingStubsLibraryInfoFromDependency,
183 }
184 scope.exportableStubsTag = scopeDependencyTag{
185 name: name + "-stubs-exportable",
186 apiScope: scope,
187 depInfoExtractor: (*scopePaths).extractExportableStubsLibraryInfoFromDependency,
188 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100189 scope.stubsSourceTag = scopeDependencyTag{
190 name: name + "-stubs-source",
191 apiScope: scope,
192 depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
193 }
Paul Duffinc8782502020-04-29 20:45:27 +0100194 scope.stubsSourceAndApiTag = scopeDependencyTag{
195 name: name + "-stubs-source-and-api",
196 apiScope: scope,
197 depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
Paul Duffind1b3a922020-01-22 11:57:20 +0000198 }
Paul Duffin958806b2022-05-16 13:10:47 +0000199 scope.latestApiModuleTag = scopeDependencyTag{
200 name: name + "-latest-api",
201 apiScope: scope,
202 depInfoExtractor: (*scopePaths).extractLatestApiPath,
203 }
204 scope.latestRemovedApiModuleTag = scopeDependencyTag{
205 name: name + "-latest-removed-api",
206 apiScope: scope,
207 depInfoExtractor: (*scopePaths).extractLatestRemovedApiPath,
208 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100209
210 // To get the args needed to generate the stubs source append all the args from
211 // this scope and all the scopes it extends as each set of args adds additional
212 // members to the stubs.
Paul Duffin15f34ef2020-07-20 18:04:44 +0100213 var scopeSpecificArgs []string
214 if scope.annotation != "" {
215 scopeSpecificArgs = []string{"--show-annotation", scope.annotation}
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100216 }
Paul Duffin15f34ef2020-07-20 18:04:44 +0100217 for s := scope; s != nil; s = s.extends {
218 scopeSpecificArgs = append(scopeSpecificArgs, s.extraArgs...)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100219
Paul Duffin15f34ef2020-07-20 18:04:44 +0100220 // Ensure that the generated stubs includes all the API elements from the API scope
221 // that this scope extends.
222 if s != scope && s.annotation != "" {
223 scopeSpecificArgs = append(scopeSpecificArgs, "--show-for-stub-purposes-annotation", s.annotation)
224 }
225 }
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100226
Paul Duffind0b9fca2022-09-30 18:11:41 +0100227 // By default, a library that can access a scope can also access the scope it extends.
228 if scope.canAccess == nil {
229 scope.canAccess = scope.extends
230 }
231
Paul Duffin15f34ef2020-07-20 18:04:44 +0100232 // Escape any special characters in the arguments. This is needed because droidstubs
233 // passes these directly to the shell command.
234 scope.droidstubsArgs = proptools.ShellEscapeList(scopeSpecificArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100235
Paul Duffind1b3a922020-01-22 11:57:20 +0000236 return scope
237}
238
Anton Hansson08f476b2021-04-07 15:32:19 +0100239func (scope *apiScope) stubsLibraryModuleNameSuffix() string {
240 return ".stubs" + scope.moduleSuffix
241}
242
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000243func (scope *apiScope) exportableStubsLibraryModuleNameSuffix() string {
244 return ".stubs.exportable" + scope.moduleSuffix
245}
246
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000247func (scope *apiScope) apiLibraryModuleName(baseName string) string {
248 return scope.stubsLibraryModuleName(baseName) + ".from-text"
249}
250
Jihoon Kang2261a822024-09-12 00:01:54 +0000251func (scope *apiScope) sourceStubsLibraryModuleName(baseName string) string {
Jihoon Kang1147b312023-06-08 23:25:57 +0000252 return scope.stubsLibraryModuleName(baseName) + ".from-source"
253}
254
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000255func (scope *apiScope) exportableSourceStubsLibraryModuleName(baseName string) string {
256 return scope.exportableStubsLibraryModuleName(baseName) + ".from-source"
257}
258
Paul Duffinc3091c82020-05-08 14:16:20 +0100259func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
Anton Hansson08f476b2021-04-07 15:32:19 +0100260 return baseName + scope.stubsLibraryModuleNameSuffix()
Paul Duffind1b3a922020-01-22 11:57:20 +0000261}
262
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000263func (scope *apiScope) exportableStubsLibraryModuleName(baseName string) string {
264 return baseName + scope.exportableStubsLibraryModuleNameSuffix()
265}
266
Paul Duffinc8782502020-04-29 20:45:27 +0100267func (scope *apiScope) stubsSourceModuleName(baseName string) string {
Paul Duffindd9d0742020-05-08 15:52:37 +0100268 return baseName + ".stubs.source" + scope.moduleSuffix
Paul Duffind1b3a922020-01-22 11:57:20 +0000269}
270
Paul Duffin3375e352020-04-28 10:44:03 +0100271func (scope *apiScope) String() string {
272 return scope.name
273}
274
Paul Duffin958806b2022-05-16 13:10:47 +0000275// snapshotRelativeDir returns the snapshot directory into which the files related to scopes will
276// be stored.
277func (scope *apiScope) snapshotRelativeDir() string {
278 return filepath.Join("sdk_library", scope.name)
279}
280
281// snapshotRelativeCurrentApiTxtPath returns the snapshot path to the API .txt file for the named
282// library.
283func (scope *apiScope) snapshotRelativeCurrentApiTxtPath(name string) string {
284 return filepath.Join(scope.snapshotRelativeDir(), name+".txt")
285}
286
287// snapshotRelativeRemovedApiTxtPath returns the snapshot path to the removed API .txt file for the
288// named library.
289func (scope *apiScope) snapshotRelativeRemovedApiTxtPath(name string) string {
290 return filepath.Join(scope.snapshotRelativeDir(), name+"-removed.txt")
291}
292
Paul Duffind1b3a922020-01-22 11:57:20 +0000293type apiScopes []*apiScope
294
295func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
296 var list []string
297 for _, scope := range scopes {
298 list = append(list, accessor(scope))
299 }
300 return list
301}
302
Jihoon Kanga96a7b12023-09-20 23:43:32 +0000303// Method that maps the apiScopes properties to the index of each apiScopes elements.
304// apiScopes property to be used as the key can be specified with the input accessor.
305// Only a string property of apiScope can be used as the key of the map.
306func (scopes apiScopes) MapToIndex(accessor func(*apiScope) string) map[string]int {
307 ret := make(map[string]int)
308 for i, scope := range scopes {
309 ret[accessor(scope)] = i
310 }
311 return ret
312}
313
Jihoon Kang98aa8fa2024-06-07 11:06:57 +0000314func (scopes apiScopes) ConvertStubsLibraryExportableToEverything(name string) string {
315 for _, scope := range scopes {
316 if strings.HasSuffix(name, scope.exportableStubsLibraryModuleNameSuffix()) {
317 return strings.TrimSuffix(name, scope.exportableStubsLibraryModuleNameSuffix()) +
318 scope.stubsLibraryModuleNameSuffix()
319 }
320 }
321 return name
322}
323
Jiyong Parkc678ad32018-04-10 13:07:10 +0900324var (
Paul Duffin46dc45a2020-05-14 15:39:10 +0100325 scopeByName = make(map[string]*apiScope)
326 allScopeNames []string
Paul Duffind1b3a922020-01-22 11:57:20 +0000327 apiScopePublic = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100328 name: "public",
329
330 // Public scope is enabled by default for both legacy and non-legacy modes.
331 legacyEnabledStatus: func(module *SdkLibrary) bool {
332 return true
333 },
334 defaultEnabledStatus: true,
335
336 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
337 return &module.sdkLibraryProperties.Public
338 },
Paul Duffind1b3a922020-01-22 11:57:20 +0000339 sdkVersion: "current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000340 kind: android.SdkPublic,
Paul Duffind1b3a922020-01-22 11:57:20 +0000341 })
342 apiScopeSystem = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100343 name: "system",
344 extends: apiScopePublic,
345 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
346 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
347 return &module.sdkLibraryProperties.System
348 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100349 apiFilePrefix: "system-",
350 moduleSuffix: ".system",
351 sdkVersion: "system_current",
352 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000353 kind: android.SdkSystem,
Paul Duffind1b3a922020-01-22 11:57:20 +0000354 })
355 apiScopeTest = initApiScope(&apiScope{
Paul Duffin3375e352020-04-28 10:44:03 +0100356 name: "test",
Anton Hansson4fe970f2020-10-09 10:16:49 +0100357 extends: apiScopeSystem,
Paul Duffin3375e352020-04-28 10:44:03 +0100358 legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
359 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
360 return &module.sdkLibraryProperties.Test
361 },
Paul Duffin15f34ef2020-07-20 18:04:44 +0100362 apiFilePrefix: "test-",
363 moduleSuffix: ".test",
364 sdkVersion: "test_current",
365 annotation: "android.annotation.TestApi",
366 unstable: true,
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000367 kind: android.SdkTest,
Paul Duffind1b3a922020-01-22 11:57:20 +0000368 })
Paul Duffin8f265b92020-04-28 14:13:56 +0100369 apiScopeModuleLib = initApiScope(&apiScope{
Paul Duffin6b836ba2020-05-13 19:19:49 +0100370 name: "module-lib",
Paul Duffin8f265b92020-04-28 14:13:56 +0100371 extends: apiScopeSystem,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100372 // The module-lib scope is disabled by default in legacy mode.
Paul Duffin8f265b92020-04-28 14:13:56 +0100373 //
374 // Enabling this would break existing usages.
375 legacyEnabledStatus: func(module *SdkLibrary) bool {
376 return false
377 },
378 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
379 return &module.sdkLibraryProperties.Module_lib
380 },
381 apiFilePrefix: "module-lib-",
382 moduleSuffix: ".module_lib",
383 sdkVersion: "module_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100384 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES)",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000385 kind: android.SdkModule,
Paul Duffin8f265b92020-04-28 14:13:56 +0100386 })
Paul Duffin0c5bae52020-06-02 13:00:08 +0100387 apiScopeSystemServer = initApiScope(&apiScope{
388 name: "system-server",
389 extends: apiScopePublic,
Paul Duffind0b9fca2022-09-30 18:11:41 +0100390
391 // The system-server scope can access the module-lib scope.
392 //
393 // A module that provides a system-server API is appended to the standard bootclasspath that is
394 // used by the system server. So, it should be able to access module-lib APIs provided by
395 // libraries on the bootclasspath.
396 canAccess: apiScopeModuleLib,
397
Paul Duffin0c5bae52020-06-02 13:00:08 +0100398 // The system-server scope is disabled by default in legacy mode.
399 //
400 // Enabling this would break existing usages.
401 legacyEnabledStatus: func(module *SdkLibrary) bool {
402 return false
403 },
404 scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
405 return &module.sdkLibraryProperties.System_server
406 },
407 apiFilePrefix: "system-server-",
408 moduleSuffix: ".system_server",
409 sdkVersion: "system_server_current",
Paul Duffin15f34ef2020-07-20 18:04:44 +0100410 annotation: "android.annotation.SystemApi(client=android.annotation.SystemApi.Client.SYSTEM_SERVER)",
411 extraArgs: []string{
412 "--hide-annotation", "android.annotation.Hide",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100413 // com.android.* classes are okay in this interface"
Paul Duffin15f34ef2020-07-20 18:04:44 +0100414 "--hide", "InternalClasses",
Paul Duffin0c5bae52020-06-02 13:00:08 +0100415 },
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000416 kind: android.SdkSystemServer,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100417 })
Jihoon Kang98aa8fa2024-06-07 11:06:57 +0000418 AllApiScopes = apiScopes{
Paul Duffind1b3a922020-01-22 11:57:20 +0000419 apiScopePublic,
420 apiScopeSystem,
421 apiScopeTest,
Paul Duffin8f265b92020-04-28 14:13:56 +0100422 apiScopeModuleLib,
Paul Duffin0c5bae52020-06-02 13:00:08 +0100423 apiScopeSystemServer,
Paul Duffind1b3a922020-01-22 11:57:20 +0000424 }
Jihoon Kangb0f4c022024-08-06 00:15:25 +0000425 apiLibraryAdditionalProperties = map[string]string{
426 "legacy.i18n.module.platform.api": "i18n.module.public.api.stubs.source.api.contribution",
427 "stable.i18n.module.platform.api": "i18n.module.public.api.stubs.source.api.contribution",
428 "conscrypt.module.platform.api": "conscrypt.module.public.api.stubs.source.api.contribution",
Jihoon Kang0c705a42023-08-02 06:44:57 +0000429 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900430)
431
Jiyong Park82484c02018-04-23 21:41:26 +0900432var (
433 javaSdkLibrariesLock sync.Mutex
434)
435
Jiyong Parkc678ad32018-04-10 13:07:10 +0900436// TODO: these are big features that are currently missing
Jiyong Park1be96912018-05-28 18:02:19 +0900437// 1) disallowing linking to the runtime shared lib
438// 2) HTML generation
Jiyong Parkc678ad32018-04-10 13:07:10 +0900439
440func init() {
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000441 RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900442
Jiyong Park82484c02018-04-23 21:41:26 +0900443 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
444 javaSdkLibraries := javaSdkLibraries(ctx.Config())
445 sort.Strings(*javaSdkLibraries)
446 ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
447 })
Paul Duffindd46f712020-02-10 13:37:10 +0000448
449 // Register sdk member types.
Paul Duffin976b0e52021-04-27 23:20:26 +0100450 android.RegisterSdkMemberType(javaSdkLibrarySdkMemberType)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900451}
452
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000453func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
454 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
455 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
456}
457
Paul Duffin3375e352020-04-28 10:44:03 +0100458// Properties associated with each api scope.
459type ApiScopeProperties struct {
460 // Indicates whether the api surface is generated.
461 //
462 // If this is set for any scope then all scopes must explicitly specify if they
463 // are enabled. This is to prevent new usages from depending on legacy behavior.
464 //
465 // Otherwise, if this is not set for any scope then the default behavior is
466 // scope specific so please refer to the scope specific property documentation.
467 Enabled *bool
Paul Duffin87a05a32020-05-12 11:50:28 +0100468
469 // The sdk_version to use for building the stubs.
470 //
471 // If not specified then it will use an sdk_version determined as follows:
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000472 //
Paul Duffin87a05a32020-05-12 11:50:28 +0100473 // 1) If the sdk_version specified on the java_sdk_library is none then this
Trevor Radcliffedf8aa1f2021-11-04 14:25:39 +0000474 // will be none. This is used for java_sdk_library instances that are used
475 // to create stubs that contribute to the core_current sdk version.
476 // 2) Otherwise, it is assumed that this library extends but does not
477 // contribute directly to a specific sdk_version and so this uses the
478 // sdk_version appropriate for the api scope. e.g. public will use
479 // sdk_version: current, system will use sdk_version: system_current, etc.
Paul Duffin87a05a32020-05-12 11:50:28 +0100480 //
481 // This does not affect the sdk_version used for either generating the stubs source
482 // or the API file. They both have to use the same sdk_version as is used for
483 // compiling the implementation library.
484 Sdk_version *string
Mark White9421c4c2023-08-10 00:07:03 +0000485
486 // Extra libs used when compiling stubs for this scope.
487 Libs []string
Paul Duffin3375e352020-04-28 10:44:03 +0100488}
489
Jiyong Parkc678ad32018-04-10 13:07:10 +0900490type sdkLibraryProperties struct {
Anton Hanssonf8ea3722021-09-16 14:24:13 +0100491 // List of source files that are needed to compile the API, but are not part of runtime library.
492 Api_srcs []string `android:"arch_variant"`
493
Paul Duffin5df79302020-05-16 15:52:12 +0100494 // Visibility for impl library module. If not specified then defaults to the
495 // visibility property.
496 Impl_library_visibility []string
497
Paul Duffin4911a892020-04-29 23:35:13 +0100498 // Visibility for stubs library modules. If not specified then defaults to the
499 // visibility property.
500 Stubs_library_visibility []string
501
502 // Visibility for stubs source modules. If not specified then defaults to the
503 // visibility property.
504 Stubs_source_visibility []string
505
Anton Hansson7f66efa2020-10-08 14:47:23 +0100506 // List of Java libraries that will be in the classpath when building the implementation lib
507 Impl_only_libs []string `android:"arch_variant"`
508
Paul Duffin77590a82022-04-28 14:13:30 +0000509 // List of Java libraries that will included in the implementation lib.
510 Impl_only_static_libs []string `android:"arch_variant"`
511
Sundong Ahnf043cf62018-06-25 16:04:37 +0900512 // List of Java libraries that will be in the classpath when building stubs
513 Stub_only_libs []string `android:"arch_variant"`
514
Anton Hanssondae54cd2021-04-21 16:30:10 +0100515 // List of Java libraries that will included in stub libraries
516 Stub_only_static_libs []string `android:"arch_variant"`
517
Paul Duffin7a586d32019-12-30 17:09:34 +0000518 // list of package names that will be documented and publicized as API.
519 // This allows the API to be restricted to a subset of the source files provided.
520 // If this is unspecified then all the source files will be treated as being part
521 // of the API.
Jiyong Parkc678ad32018-04-10 13:07:10 +0900522 Api_packages []string
523
Paul Duffin749f98f2019-12-30 17:23:46 +0000524 // the relative path to the directory containing the api specification files.
525 // Defaults to "api".
526 Api_dir *string
527
Paul Duffindfa131e2020-05-15 20:37:11 +0100528 // Determines whether a runtime implementation library is built; defaults to false.
529 //
530 // 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 +0200531 // it is as if shared_library: false, was set.
Paul Duffin43db9be2019-12-30 17:35:49 +0000532 Api_only *bool
533
Paul Duffin11512472019-02-11 15:55:17 +0000534 // local files that are used within user customized droiddoc options.
535 Droiddoc_option_files []string
536
Spandan Das93e95992021-07-29 18:26:39 +0000537 // additional droiddoc options.
Paul Duffin11512472019-02-11 15:55:17 +0000538 // Available variables for substitution:
539 //
540 // $(location <label>): the path to the droiddoc_option_files with name <label>
Sundong Ahndd567f92018-07-31 17:19:11 +0900541 Droiddoc_options []string
542
Paul Duffine22c2ab2020-05-20 19:35:27 +0100543 // is set to true, Metalava will allow framework SDK to contain annotations.
544 Annotations_enabled *bool
545
Sundong Ahn054b19a2018-10-19 13:46:09 +0900546 // a list of top-level directories containing files to merge qualifier annotations
547 // (i.e. those intended to be included in the stubs written) from.
548 Merge_annotations_dirs []string
549
550 // a list of top-level directories containing Java stub files to merge show/hide annotations from.
551 Merge_inclusion_annotations_dirs []string
552
Paul Duffin4f5c1ef2020-11-19 14:53:43 +0000553 // If set to true then don't create dist rules.
554 No_dist *bool
Sundong Ahn80a87b32019-05-13 15:02:50 +0900555
Paul Duffin31310252020-11-20 21:26:20 +0000556 // The stem for the artifacts that are copied to the dist, if not specified
557 // then defaults to the base module name.
558 //
559 // For each scope the following artifacts are copied to the apistubs/<scope>
560 // directory in the dist.
561 // * stubs impl jar -> <dist-stem>.jar
562 // * API specification file -> api/<dist-stem>.txt
563 // * Removed API specification file -> api/<dist-stem>-removed.txt
564 //
565 // Also used to construct the name of the filegroup (created by prebuilt_apis)
566 // that references the latest released API and remove API specification files.
567 // * API specification filegroup -> <dist-stem>.api.<scope>.latest
568 // * Removed API specification filegroup -> <dist-stem>-removed.api.<scope>.latest
Jaewoong Jung1a97ee02021-03-09 13:25:02 -0800569 // * API incompatibilities baseline filegroup -> <dist-stem>-incompatibilities.api.<scope>.latest
Paul Duffin31310252020-11-20 21:26:20 +0000570 Dist_stem *string
571
Colin Cross986b69a2021-06-01 13:13:40 -0700572 // The subdirectory for the artifacts that are copied to the dist directory. If not specified
Colin Cross3dd66252021-06-01 14:05:09 -0700573 // then defaults to "unknown". Should be set to "android" for anything that should be published
Colin Cross986b69a2021-06-01 13:13:40 -0700574 // in the public Android SDK.
575 Dist_group *string
576
Anton Hanssondff2c782020-12-21 17:10:01 +0000577 // A compatibility mode that allows historical API-tracking files to not exist.
578 // Do not use.
579 Unsafe_ignore_missing_latest_api bool
580
Paul Duffin3375e352020-04-28 10:44:03 +0100581 // indicates whether system and test apis should be generated.
582 Generate_system_and_test_apis bool `blueprint:"mutated"`
583
584 // The properties specific to the public api scope
585 //
586 // Unless explicitly specified by using public.enabled the public api scope is
587 // enabled by default in both legacy and non-legacy mode.
588 Public ApiScopeProperties
589
590 // The properties specific to the system api scope
591 //
592 // In legacy mode the system api scope is enabled by default when sdk_version
593 // is set to something other than "none".
594 //
595 // In non-legacy mode the system api scope is disabled by default.
596 System ApiScopeProperties
597
598 // The properties specific to the test api scope
599 //
600 // In legacy mode the test api scope is enabled by default when sdk_version
601 // is set to something other than "none".
602 //
603 // In non-legacy mode the test api scope is disabled by default.
604 Test ApiScopeProperties
Paul Duffin37e0b772019-12-30 17:20:10 +0000605
Paul Duffin0c5bae52020-06-02 13:00:08 +0100606 // The properties specific to the module-lib api scope
Paul Duffin8f265b92020-04-28 14:13:56 +0100607 //
Zi Wangb2179e32023-01-31 15:53:30 -0800608 // Unless explicitly specified by using module_lib.enabled the module_lib api
609 // scope is disabled by default.
Paul Duffin8f265b92020-04-28 14:13:56 +0100610 Module_lib ApiScopeProperties
611
Paul Duffin0c5bae52020-06-02 13:00:08 +0100612 // The properties specific to the system-server api scope
613 //
Zi Wangb2179e32023-01-31 15:53:30 -0800614 // Unless explicitly specified by using system_server.enabled the
615 // system_server api scope is disabled by default.
Paul Duffin0c5bae52020-06-02 13:00:08 +0100616 System_server ApiScopeProperties
617
Jiyong Park932cdfe2020-05-28 00:19:53 +0900618 // Determines if the stubs are preferred over the implementation library
619 // for linking, even when the client doesn't specify sdk_version. When this
620 // is set to true, such clients are provided with the widest API surface that
621 // this lib provides. Note however that this option doesn't affect the clients
622 // that are in the same APEX as this library. In that case, the clients are
623 // always linked with the implementation library. Default is false.
624 Default_to_stubs *bool
625
Paul Duffin160fe412020-05-10 19:32:20 +0100626 // Properties related to api linting.
627 Api_lint struct {
628 // Enable api linting.
629 Enabled *bool
Anton Hanssonfd1c0d22023-11-02 15:18:09 +0000630
631 // If API lint is enabled, this flag controls whether a set of legitimate lint errors
632 // are turned off. The default is true.
633 Legacy_errors_allowed *bool
Paul Duffin160fe412020-05-10 19:32:20 +0100634 }
635
Jihoon Kang6592e872023-12-19 01:13:16 +0000636 // a list of aconfig_declarations module names that the stubs generated in this module
637 // depend on.
638 Aconfig_declarations []string
639
Jihoon Kang48e2ac92024-07-29 21:18:46 +0000640 // Determines if the module generates the stubs from the api signature files
641 // instead of the source Java files. Defaults to true.
642 Build_from_text_stub *bool
643
Jiyong Parkc678ad32018-04-10 13:07:10 +0900644 // TODO: determines whether to create HTML doc or not
Paul Duffine8409952022-09-22 16:24:46 +0100645 // Html_doc *bool
Jiyong Parkc678ad32018-04-10 13:07:10 +0900646}
647
Paul Duffin0f8faff2020-05-20 16:18:00 +0100648// Paths to outputs from java_sdk_library and java_sdk_library_import.
649//
650// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
651// OptionalPaths are always set by java_sdk_library but may not be set by
652// java_sdk_library_import as not all instances provide that information.
Paul Duffind1b3a922020-01-22 11:57:20 +0000653type scopePaths struct {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100654 // The path (represented as Paths for convenience when returning) to the stubs header jar.
655 //
656 // That is the jar that is created by turbine.
657 stubsHeaderPath android.Paths
658
659 // The path (represented as Paths for convenience when returning) to the stubs implementation jar.
660 //
661 // This is not the implementation jar, it still only contains stubs.
662 stubsImplPath android.Paths
663
Paul Duffin1267d872021-04-16 17:21:36 +0100664 // The dex jar for the stubs.
665 //
666 // This is not the implementation jar, it still only contains stubs.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100667 stubsDexJarPath OptionalDexJarPath
Paul Duffin1267d872021-04-16 17:21:36 +0100668
Jihoon Kangbd093452023-12-26 19:08:01 +0000669 // The exportable dex jar for the stubs.
670 // This is not the implementation jar, it still only contains stubs.
671 // Includes unflagged apis and flagged apis enabled by release configurations.
672 exportableStubsDexJarPath OptionalDexJarPath
673
Paul Duffin0f8faff2020-05-20 16:18:00 +0100674 // The API specification file, e.g. system_current.txt.
675 currentApiFilePath android.OptionalPath
676
677 // The specification of API elements removed since the last release.
678 removedApiFilePath android.OptionalPath
679
680 // The stubs source jar.
681 stubsSrcJar android.OptionalPath
Anton Hanssond78eb762021-09-21 15:25:12 +0100682
683 // Extracted annotations.
684 annotationsZip android.OptionalPath
Paul Duffin958806b2022-05-16 13:10:47 +0000685
686 // The path to the latest API file.
Jihoon Kang5623e542024-01-31 23:27:26 +0000687 latestApiPaths android.Paths
Paul Duffin958806b2022-05-16 13:10:47 +0000688
689 // The path to the latest removed API file.
Jihoon Kang5623e542024-01-31 23:27:26 +0000690 latestRemovedApiPaths android.Paths
Paul Duffind1b3a922020-01-22 11:57:20 +0000691}
692
Colin Crossdcf71b22021-02-01 13:59:03 -0800693func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Colin Cross313aa542023-12-13 13:47:44 -0800694 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800695 paths.stubsHeaderPath = lib.HeaderJars
696 paths.stubsImplPath = lib.ImplementationJars
Paul Duffin1267d872021-04-16 17:21:36 +0100697
698 libDep := dep.(UsesLibraryDependency)
Spandan Das59a4a2b2024-01-09 21:35:56 +0000699 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
Jihoon Kangbd093452023-12-26 19:08:01 +0000700 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
701 return nil
702 } else {
703 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
704 }
705}
706
707func (paths *scopePaths) extractEverythingStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
708 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
709 paths.stubsHeaderPath = lib.HeaderJars
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000710 if !ctx.Config().ReleaseHiddenApiExportableStubs() {
711 paths.stubsImplPath = lib.ImplementationJars
712 }
Jihoon Kangbd093452023-12-26 19:08:01 +0000713
714 libDep := dep.(UsesLibraryDependency)
715 paths.stubsDexJarPath = libDep.DexJarBuildPath(ctx)
716 return nil
717 } else {
718 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
719 }
720}
721
722func (paths *scopePaths) extractExportableStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000723 if lib, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
724 if ctx.Config().ReleaseHiddenApiExportableStubs() {
725 paths.stubsImplPath = lib.ImplementationJars
726 }
727
Jihoon Kangbd093452023-12-26 19:08:01 +0000728 libDep := dep.(UsesLibraryDependency)
729 paths.exportableStubsDexJarPath = libDep.DexJarBuildPath(ctx)
Paul Duffinc8782502020-04-29 20:45:27 +0100730 return nil
731 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -0800732 return fmt.Errorf("expected module that has JavaInfoProvider, e.g. java_library")
Paul Duffinc8782502020-04-29 20:45:27 +0100733 }
734}
735
Jihoon Kangee113282024-01-23 00:16:41 +0000736func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider) error) error {
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100737 if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000738 err := action(apiStubsProvider)
739 if err != nil {
740 return err
741 }
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000742 return nil
743 } else {
744 return fmt.Errorf("expected module that implements ExportableApiStubsSrcProvider, e.g. droidstubs")
745 }
746}
747
Jihoon Kangee113282024-01-23 00:16:41 +0000748func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider) error) error {
Paul Duffin0f8faff2020-05-20 16:18:00 +0100749 if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
Jihoon Kangee113282024-01-23 00:16:41 +0000750 err := action(apiStubsProvider)
751 if err != nil {
752 return err
753 }
Paul Duffin0f8faff2020-05-20 16:18:00 +0100754 return nil
755 } else {
756 return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
757 }
758}
759
Jihoon Kangee113282024-01-23 00:16:41 +0000760func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider, stubsType StubsType) error {
761 var annotationsZip, currentApiFilePath, removedApiFilePath android.Path
762 annotationsZip, annotationsZipErr := provider.AnnotationsZip(stubsType)
763 currentApiFilePath, currentApiFilePathErr := provider.ApiFilePath(stubsType)
764 removedApiFilePath, removedApiFilePathErr := provider.RemovedApiFilePath(stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100765
Jihoon Kangee113282024-01-23 00:16:41 +0000766 combinedError := errors.Join(annotationsZipErr, currentApiFilePathErr, removedApiFilePathErr)
767
768 if combinedError == nil {
769 paths.annotationsZip = android.OptionalPathForPath(annotationsZip)
770 paths.currentApiFilePath = android.OptionalPathForPath(currentApiFilePath)
771 paths.removedApiFilePath = android.OptionalPathForPath(removedApiFilePath)
772 }
773 return combinedError
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000774}
775
Jihoon Kangee113282024-01-23 00:16:41 +0000776func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider, stubsType StubsType) error {
777 stubsSrcJar, err := provider.StubsSrcJar(stubsType)
778 if err == nil {
779 paths.stubsSrcJar = android.OptionalPathForPath(stubsSrcJar)
780 }
781 return err
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000782}
783
Colin Crossdcf71b22021-02-01 13:59:03 -0800784func (paths *scopePaths) extractStubsSourceInfoFromDep(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000785 stubsType := Everything
786 if ctx.Config().ReleaseHiddenApiExportableStubs() {
787 stubsType = Exportable
788 }
Jihoon Kangee113282024-01-23 00:16:41 +0000789 return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000790 return paths.extractStubsSourceInfoFromApiStubsProviders(provider, stubsType)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100791 })
792}
793
Colin Crossdcf71b22021-02-01 13:59:03 -0800794func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000795 stubsType := Everything
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000796 if ctx.Config().ReleaseHiddenApiExportableStubs() {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000797 stubsType = Exportable
Jihoon Kangf55a5f72024-01-08 08:56:20 +0000798 }
Jihoon Kangee113282024-01-23 00:16:41 +0000799 return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) error {
Jihoon Kang2a26b132024-06-24 07:39:40 +0000800 extractApiInfoErr := paths.extractApiInfoFromApiStubsProvider(provider, stubsType)
801 extractStubsSourceInfoErr := paths.extractStubsSourceInfoFromApiStubsProviders(provider, stubsType)
Jihoon Kangee113282024-01-23 00:16:41 +0000802 return errors.Join(extractApiInfoErr, extractStubsSourceInfoErr)
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100803 })
804}
805
Jihoon Kang5623e542024-01-31 23:27:26 +0000806func extractOutputPaths(dep android.Module) (android.Paths, error) {
Paul Duffin958806b2022-05-16 13:10:47 +0000807 var paths android.Paths
808 if sourceFileProducer, ok := dep.(android.SourceFileProducer); ok {
809 paths = sourceFileProducer.Srcs()
Jihoon Kang5623e542024-01-31 23:27:26 +0000810 return paths, nil
Paul Duffin958806b2022-05-16 13:10:47 +0000811 } else {
Jihoon Kang5623e542024-01-31 23:27:26 +0000812 return nil, fmt.Errorf("module %q does not produce source files", dep)
Paul Duffin958806b2022-05-16 13:10:47 +0000813 }
Paul Duffin958806b2022-05-16 13:10:47 +0000814}
815
816func (paths *scopePaths) extractLatestApiPath(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang5623e542024-01-31 23:27:26 +0000817 outputPaths, err := extractOutputPaths(dep)
818 paths.latestApiPaths = outputPaths
Paul Duffin958806b2022-05-16 13:10:47 +0000819 return err
820}
821
822func (paths *scopePaths) extractLatestRemovedApiPath(ctx android.ModuleContext, dep android.Module) error {
Jihoon Kang5623e542024-01-31 23:27:26 +0000823 outputPaths, err := extractOutputPaths(dep)
824 paths.latestRemovedApiPaths = outputPaths
Paul Duffin958806b2022-05-16 13:10:47 +0000825 return err
826}
827
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100828type commonToSdkLibraryAndImportProperties struct {
Paul Duffindfa131e2020-05-15 20:37:11 +0100829 // Specifies whether this module can be used as an Android shared library; defaults
830 // to true.
831 //
832 // An Android shared library is one that can be referenced in a <uses-library> element
833 // in an AndroidManifest.xml.
834 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +0100835
836 // Files containing information about supported java doc tags.
837 Doctag_files []string `android:"path"`
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000838
839 // Signals that this shared library is part of the bootclasspath starting
840 // on the version indicated in this attribute.
841 //
842 // This will make platforms at this level and above to ignore
843 // <uses-library> tags with this library name because the library is already
844 // available
845 On_bootclasspath_since *string
846
847 // Signals that this shared library was part of the bootclasspath before
848 // (but not including) the version indicated in this attribute.
849 //
850 // The system will automatically add a <uses-library> tag with this library to
851 // apps that target any SDK less than the version indicated in this attribute.
852 On_bootclasspath_before *string
853
854 // Indicates that PackageManager should ignore this shared library if the
855 // platform is below the version indicated in this attribute.
856 //
857 // This means that the device won't recognise this library as installed.
858 Min_device_sdk *string
859
860 // Indicates that PackageManager should ignore this shared library if the
861 // platform is above the version indicated in this attribute.
862 //
863 // This means that the device won't recognise this library as installed.
864 Max_device_sdk *string
Paul Duffin0ff08bd2020-04-29 13:30:54 +0100865}
866
Paul Duffin71b33cc2021-06-23 11:39:47 +0100867// commonSdkLibraryAndImportModule defines the interface that must be provided by a module that
868// embeds the commonToSdkLibraryAndImport struct.
869type commonSdkLibraryAndImportModule interface {
Paul Duffind796f6f2022-11-23 23:06:05 +0000870 android.Module
Paul Duffin71b33cc2021-06-23 11:39:47 +0100871
Spandan Das23956d12024-01-19 00:22:22 +0000872 // Returns the name of the root java_sdk_library that creates the child stub libraries
873 // This is the `name` as it appears in Android.bp, and not the name in Soong's build graph
874 // (with the prebuilt_ prefix)
875 //
876 // e.g. in the following java_sdk_library_import
877 // java_sdk_library_import {
878 // name: "framework-foo.v1",
879 // source_module_name: "framework-foo",
880 // }
881 // the values returned by
882 // 1. Name(): prebuilt_framework-foo.v1 # unique
883 // 2. BaseModuleName(): framework-foo # the source
884 // 3. RootLibraryName: framework-foo.v1 # the undecordated `name` from Android.bp
885 RootLibraryName() string
886}
887
888func (m *SdkLibrary) RootLibraryName() string {
889 return m.BaseModuleName()
890}
891
892func (m *SdkLibraryImport) RootLibraryName() string {
893 // m.BaseModuleName refers to the source of the import
894 // use moduleBase.Name to get the name of the module as it appears in the .bp file
895 return m.ModuleBase.Name()
Paul Duffin71b33cc2021-06-23 11:39:47 +0100896}
897
Paul Duffin56d44902020-01-31 13:36:25 +0000898// Common code between sdk library and sdk library import
899type commonToSdkLibraryAndImport struct {
Paul Duffin71b33cc2021-06-23 11:39:47 +0100900 module commonSdkLibraryAndImportModule
Paul Duffinc3091c82020-05-08 14:16:20 +0100901
Paul Duffin56d44902020-01-31 13:36:25 +0000902 scopePaths map[*apiScope]*scopePaths
Paul Duffin1b1e8062020-05-08 13:44:43 +0100903
Paul Duffindfa131e2020-05-15 20:37:11 +0100904 commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
Paul Duffin859fe962020-05-15 10:20:31 +0100905
Paul Duffina2ae7e02020-09-11 11:55:00 +0100906 // Paths to commonSdkLibraryProperties.Doctag_files
907 doctagPaths android.Paths
908
Paul Duffin859fe962020-05-15 10:20:31 +0100909 // Functionality related to this being used as a component of a java_sdk_library.
910 EmbeddableSdkLibraryComponent
Jihoon Kang8479dea2024-04-04 01:19:05 +0000911
912 // Path to the header jars of the implementation library
913 // This is non-empty only when api_only is false.
914 implLibraryHeaderJars android.Paths
Jihoon Kanga3a05462024-04-05 00:36:44 +0000915
916 // The reference to the implementation library created by the source module.
917 // Is nil if the source module does not exist.
918 implLibraryModule *Library
Paul Duffin56d44902020-01-31 13:36:25 +0000919}
920
Paul Duffin71b33cc2021-06-23 11:39:47 +0100921func (c *commonToSdkLibraryAndImport) initCommon(module commonSdkLibraryAndImportModule) {
922 c.module = module
Paul Duffin1b1e8062020-05-08 13:44:43 +0100923
Paul Duffin71b33cc2021-06-23 11:39:47 +0100924 module.AddProperties(&c.commonSdkLibraryProperties)
Paul Duffin859fe962020-05-15 10:20:31 +0100925
926 // Initialize this as an sdk library component.
Paul Duffin71b33cc2021-06-23 11:39:47 +0100927 c.initSdkLibraryComponent(module)
Paul Duffin1b1e8062020-05-08 13:44:43 +0100928}
929
930func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
Spandan Das23956d12024-01-19 00:22:22 +0000931 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +0100932 c.sdkLibraryComponentProperties.SdkLibraryName = namePtr
933
Paul Duffindfa131e2020-05-15 20:37:11 +0100934 // Only track this sdk library if this can be used as a shared library.
935 if c.sharedLibrary() {
936 // Use the name specified in the module definition as the owner.
Paul Duffin3f0290e2021-06-30 18:25:36 +0100937 c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffindfa131e2020-05-15 20:37:11 +0100938 }
Paul Duffin859fe962020-05-15 10:20:31 +0100939
Paul Duffin1b1e8062020-05-08 13:44:43 +0100940 return true
Paul Duffinc3091c82020-05-08 14:16:20 +0100941}
942
Paul Duffinea8f8082021-06-24 13:25:57 +0100943// uniqueApexVariations provides common implementation of the ApexModule.UniqueApexVariations
944// method.
945func (c *commonToSdkLibraryAndImport) uniqueApexVariations() bool {
946 // A java_sdk_library that is a shared library produces an XML file that makes the shared library
947 // usable from an AndroidManifest.xml's <uses-library> entry. That XML file contains the name of
948 // the APEX and so it needs a unique variation per APEX.
949 return c.sharedLibrary()
950}
951
Paul Duffina2ae7e02020-09-11 11:55:00 +0100952func (c *commonToSdkLibraryAndImport) generateCommonBuildActions(ctx android.ModuleContext) {
953 c.doctagPaths = android.PathsForModuleSrc(ctx, c.commonSdkLibraryProperties.Doctag_files)
954}
955
Jihoon Kanga3a05462024-04-05 00:36:44 +0000956func (c *commonToSdkLibraryAndImport) getImplLibraryModule() *Library {
957 return c.implLibraryModule
958}
959
Paul Duffineedc5d52020-06-12 17:46:39 +0100960// Module name of the runtime implementation library
961func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +0000962 return c.module.RootLibraryName() + ".impl"
Paul Duffineedc5d52020-06-12 17:46:39 +0100963}
964
965// Module name of the XML file for the lib
966func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
Spandan Das23956d12024-01-19 00:22:22 +0000967 return c.module.RootLibraryName() + sdkXmlFileSuffix
Paul Duffineedc5d52020-06-12 17:46:39 +0100968}
969
Paul Duffinc3091c82020-05-08 14:16:20 +0100970// Name of the java_library module that compiles the stubs source.
971func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +0000972 baseName := c.module.RootLibraryName()
Jihoon Kang2261a822024-09-12 00:01:54 +0000973 return apiScope.stubsLibraryModuleName(baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100974}
975
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000976// Name of the java_library module that compiles the exportable stubs source.
977func (c *commonToSdkLibraryAndImport) exportableStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +0000978 baseName := c.module.RootLibraryName()
Jihoon Kang2261a822024-09-12 00:01:54 +0000979 return apiScope.exportableStubsLibraryModuleName(baseName)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000980}
981
Paul Duffinc3091c82020-05-08 14:16:20 +0100982// Name of the droidstubs module that generates the stubs source and may also
983// generate/check the API.
984func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +0000985 baseName := c.module.RootLibraryName()
Jihoon Kang2261a822024-09-12 00:01:54 +0000986 return apiScope.stubsSourceModuleName(baseName)
Paul Duffinc3091c82020-05-08 14:16:20 +0100987}
988
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000989// Name of the java_api_library module that generates the from-text stubs source
990// and compiles to a jar file.
991func (c *commonToSdkLibraryAndImport) apiLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +0000992 baseName := c.module.RootLibraryName()
Jihoon Kang2261a822024-09-12 00:01:54 +0000993 return apiScope.apiLibraryModuleName(baseName)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000994}
995
Jihoon Kang1147b312023-06-08 23:25:57 +0000996// Name of the java_library module that compiles the stubs
997// generated from source Java files.
Jihoon Kangfa4a90d2023-12-20 02:53:38 +0000998func (c *commonToSdkLibraryAndImport) sourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +0000999 baseName := c.module.RootLibraryName()
Jihoon Kang2261a822024-09-12 00:01:54 +00001000 return apiScope.sourceStubsLibraryModuleName(baseName)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001001}
1002
1003// Name of the java_library module that compiles the exportable stubs
1004// generated from source Java files.
1005func (c *commonToSdkLibraryAndImport) exportableSourceStubsLibraryModuleName(apiScope *apiScope) string {
Spandan Das23956d12024-01-19 00:22:22 +00001006 baseName := c.module.RootLibraryName()
Jihoon Kang2261a822024-09-12 00:01:54 +00001007 return apiScope.exportableSourceStubsLibraryModuleName(baseName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001008}
1009
Paul Duffin46dc45a2020-05-14 15:39:10 +01001010// The component names for different outputs of the java_sdk_library.
1011//
1012// They are similar to the names used for the child modules it creates
1013const (
1014 stubsSourceComponentName = "stubs.source"
1015
1016 apiTxtComponentName = "api.txt"
1017
1018 removedApiTxtComponentName = "removed-api.txt"
Anton Hanssond78eb762021-09-21 15:25:12 +01001019
1020 annotationsComponentName = "annotations.zip"
Paul Duffin46dc45a2020-05-14 15:39:10 +01001021)
1022
mrziwang9f7b9f42024-07-10 12:18:06 -07001023func (module *commonToSdkLibraryAndImport) setOutputFiles(ctx android.ModuleContext) {
1024 if module.doctagPaths != nil {
1025 ctx.SetOutputFiles(module.doctagPaths, ".doctags")
1026 }
1027 for _, scopeName := range android.SortedKeys(scopeByName) {
1028 paths := module.findScopePaths(scopeByName[scopeName])
1029 if paths == nil {
1030 continue
Paul Duffin46dc45a2020-05-14 15:39:10 +01001031 }
mrziwang9f7b9f42024-07-10 12:18:06 -07001032 componentToOutput := map[string]android.OptionalPath{
1033 stubsSourceComponentName: paths.stubsSrcJar,
1034 apiTxtComponentName: paths.currentApiFilePath,
1035 removedApiTxtComponentName: paths.removedApiFilePath,
1036 annotationsComponentName: paths.annotationsZip,
1037 }
1038 for _, component := range android.SortedKeys(componentToOutput) {
1039 if componentToOutput[component].Valid() {
1040 ctx.SetOutputFiles(android.Paths{componentToOutput[component].Path()}, "."+scopeName+"."+component)
Paul Duffina2ae7e02020-09-11 11:55:00 +01001041 }
1042 }
Paul Duffin46dc45a2020-05-14 15:39:10 +01001043 }
1044}
1045
Paul Duffin803a9562020-05-20 11:52:25 +01001046func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
Paul Duffin56d44902020-01-31 13:36:25 +00001047 if c.scopePaths == nil {
1048 c.scopePaths = make(map[*apiScope]*scopePaths)
1049 }
1050 paths := c.scopePaths[scope]
1051 if paths == nil {
1052 paths = &scopePaths{}
1053 c.scopePaths[scope] = paths
1054 }
1055
1056 return paths
1057}
1058
Paul Duffin803a9562020-05-20 11:52:25 +01001059func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
1060 if c.scopePaths == nil {
1061 return nil
1062 }
1063
1064 return c.scopePaths[scope]
1065}
1066
1067// If this does not support the requested api scope then find the closest available
1068// scope it does support. Returns nil if no such scope is available.
1069func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
Paul Duffind0b9fca2022-09-30 18:11:41 +01001070 for s := scope; s != nil; s = s.canAccess {
Paul Duffin803a9562020-05-20 11:52:25 +01001071 if paths := c.findScopePaths(s); paths != nil {
1072 return paths
1073 }
1074 }
1075
1076 // This should never happen outside tests as public should be the base scope for every
1077 // scope and is enabled by default.
1078 return nil
1079}
1080
Paul Duffin1267d872021-04-16 17:21:36 +01001081// selectScopePaths returns the *scopePaths appropriate for the specific kind.
1082//
1083// If the module does not support the specific kind then it will return the *scopePaths for the
1084// closest kind which is a subset of the requested kind. e.g. if requesting android.SdkModule then
1085// it will return *scopePaths for android.SdkSystem if available or android.SdkPublic of not.
1086func (c *commonToSdkLibraryAndImport) selectScopePaths(ctx android.BaseModuleContext, kind android.SdkKind) *scopePaths {
Paul Duffin32cf58a2021-05-18 16:32:50 +01001087 apiScope := sdkKindToApiScope(kind)
Paul Duffinb05d4292020-05-20 12:19:10 +01001088
Paul Duffin803a9562020-05-20 11:52:25 +01001089 paths := c.findClosestScopePath(apiScope)
1090 if paths == nil {
1091 var scopes []string
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001092 for _, s := range AllApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01001093 if c.findScopePaths(s) != nil {
1094 scopes = append(scopes, s.name)
1095 }
1096 }
Spandan Das23956d12024-01-19 00:22:22 +00001097 ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.module.RootLibraryName(), scopes)
Paul Duffin803a9562020-05-20 11:52:25 +01001098 return nil
1099 }
1100
Paul Duffin1267d872021-04-16 17:21:36 +01001101 return paths
1102}
1103
Paul Duffin32cf58a2021-05-18 16:32:50 +01001104// sdkKindToApiScope maps from android.SdkKind to apiScope.
1105func sdkKindToApiScope(kind android.SdkKind) *apiScope {
1106 var apiScope *apiScope
1107 switch kind {
1108 case android.SdkSystem:
1109 apiScope = apiScopeSystem
1110 case android.SdkModule:
1111 apiScope = apiScopeModuleLib
1112 case android.SdkTest:
1113 apiScope = apiScopeTest
1114 case android.SdkSystemServer:
1115 apiScope = apiScopeSystemServer
1116 default:
1117 apiScope = apiScopePublic
1118 }
1119 return apiScope
1120}
1121
Paul Duffin1267d872021-04-16 17:21:36 +01001122// to satisfy SdkLibraryDependency interface
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001123func (c *commonToSdkLibraryAndImport) SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
Paul Duffin1267d872021-04-16 17:21:36 +01001124 paths := c.selectScopePaths(ctx, kind)
1125 if paths == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001126 return makeUnsetDexJarPath()
Paul Duffin1267d872021-04-16 17:21:36 +01001127 }
1128
1129 return paths.stubsDexJarPath
Paul Duffinb05d4292020-05-20 12:19:10 +01001130}
1131
Paul Duffin32cf58a2021-05-18 16:32:50 +01001132// to satisfy SdkLibraryDependency interface
Jihoon Kangbd093452023-12-26 19:08:01 +00001133func (c *commonToSdkLibraryAndImport) SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath {
1134 paths := c.selectScopePaths(ctx, kind)
1135 if paths == nil {
1136 return makeUnsetDexJarPath()
1137 }
1138
1139 return paths.exportableStubsDexJarPath
1140}
1141
1142// to satisfy SdkLibraryDependency interface
Paul Duffin32cf58a2021-05-18 16:32:50 +01001143func (c *commonToSdkLibraryAndImport) SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath {
1144 apiScope := sdkKindToApiScope(kind)
1145 paths := c.findScopePaths(apiScope)
1146 if paths == nil {
1147 return android.OptionalPath{}
1148 }
1149
1150 return paths.removedApiFilePath
1151}
1152
Paul Duffin859fe962020-05-15 10:20:31 +01001153func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
1154 componentProps := &struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001155 SdkLibraryName *string
Paul Duffin859fe962020-05-15 10:20:31 +01001156 SdkLibraryToImplicitlyTrack *string
Paul Duffindfa131e2020-05-15 20:37:11 +01001157 }{}
1158
Spandan Das23956d12024-01-19 00:22:22 +00001159 namePtr := proptools.StringPtr(c.module.RootLibraryName())
Paul Duffin3f0290e2021-06-30 18:25:36 +01001160 componentProps.SdkLibraryName = namePtr
1161
Paul Duffindfa131e2020-05-15 20:37:11 +01001162 if c.sharedLibrary() {
Paul Duffin859fe962020-05-15 10:20:31 +01001163 // Mark the stubs library as being components of this java_sdk_library so that
1164 // any app that includes code which depends (directly or indirectly) on the stubs
1165 // library will have the appropriate <uses-library> invocation inserted into its
1166 // manifest if necessary.
Paul Duffin3f0290e2021-06-30 18:25:36 +01001167 componentProps.SdkLibraryToImplicitlyTrack = namePtr
Paul Duffin859fe962020-05-15 10:20:31 +01001168 }
1169
1170 return componentProps
1171}
1172
Paul Duffindfa131e2020-05-15 20:37:11 +01001173func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
1174 return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
1175}
1176
Paul Duffinf4600f62021-05-13 22:34:45 +01001177// Check if the stub libraries should be compiled for dex
1178func (c *commonToSdkLibraryAndImport) stubLibrariesCompiledForDex() bool {
1179 // Always compile the dex file files for the stub libraries if they will be used on the
1180 // bootclasspath.
1181 return !c.sharedLibrary()
1182}
1183
Paul Duffin859fe962020-05-15 10:20:31 +01001184// Properties related to the use of a module as an component of a java_sdk_library.
1185type SdkLibraryComponentProperties struct {
Paul Duffin3f0290e2021-06-30 18:25:36 +01001186 // The name of the java_sdk_library/_import module.
1187 SdkLibraryName *string `blueprint:"mutated"`
Paul Duffin859fe962020-05-15 10:20:31 +01001188
1189 // The name of the java_sdk_library/_import to add to a <uses-library> entry
1190 // in the AndroidManifest.xml of any Android app that includes code that references
1191 // this module. If not set then no java_sdk_library/_import is tracked.
1192 SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
1193}
1194
1195// Structure to be embedded in a module struct that needs to support the
1196// SdkLibraryComponentDependency interface.
1197type EmbeddableSdkLibraryComponent struct {
1198 sdkLibraryComponentProperties SdkLibraryComponentProperties
1199}
1200
Paul Duffin71b33cc2021-06-23 11:39:47 +01001201func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(module android.Module) {
1202 module.AddProperties(&e.sdkLibraryComponentProperties)
Paul Duffin859fe962020-05-15 10:20:31 +01001203}
1204
1205// to satisfy SdkLibraryComponentDependency
Paul Duffin3f0290e2021-06-30 18:25:36 +01001206func (e *EmbeddableSdkLibraryComponent) SdkLibraryName() *string {
1207 return e.sdkLibraryComponentProperties.SdkLibraryName
1208}
1209
1210// to satisfy SdkLibraryComponentDependency
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001211func (e *EmbeddableSdkLibraryComponent) OptionalSdkLibraryImplementation() *string {
Ulya Trafimovich78645fb2021-07-16 15:29:25 +01001212 // For shared libraries, this is the same as the SDK library name. If a Java library or app
1213 // depends on a component library (e.g. a stub library) it still needs to know the name of the
1214 // run-time library and the corresponding module that provides the implementation. This name is
1215 // passed to manifest_fixer (to be added to AndroidManifest.xml) and added to CLC (to be used
1216 // in dexpreopt).
1217 //
1218 // For non-shared SDK (component or not) libraries this returns `nil`, as they are not
1219 // <uses-library> and should not be added to the manifest or to CLC.
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001220 return e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack
1221}
1222
Paul Duffin859fe962020-05-15 10:20:31 +01001223// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
1224// (including the java_sdk_library) itself.
1225type SdkLibraryComponentDependency interface {
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01001226 UsesLibraryDependency
1227
Paul Duffin3f0290e2021-06-30 18:25:36 +01001228 // SdkLibraryName returns the name of the java_sdk_library/_import module.
1229 SdkLibraryName() *string
1230
Ulya Trafimovich39b437b2020-09-23 16:42:35 +01001231 // The name of the implementation library for the optional SDK library or nil, if there isn't one.
1232 OptionalSdkLibraryImplementation() *string
Paul Duffin859fe962020-05-15 10:20:31 +01001233}
1234
1235// Make sure that all the module types that are components of java_sdk_library/_import
1236// and which can be referenced (directly or indirectly) from an android app implement
1237// the SdkLibraryComponentDependency interface.
1238var _ SdkLibraryComponentDependency = (*Library)(nil)
1239var _ SdkLibraryComponentDependency = (*Import)(nil)
1240var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
Paul Duffineedc5d52020-06-12 17:46:39 +01001241var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
Paul Duffin859fe962020-05-15 10:20:31 +01001242
Paul Duffin32cf58a2021-05-18 16:32:50 +01001243// Provides access to sdk_version related files, e.g. header and implementation jars.
Paul Duffin859fe962020-05-15 10:20:31 +01001244type SdkLibraryDependency interface {
1245 SdkLibraryComponentDependency
1246
Jihoon Kangbd093452023-12-26 19:08:01 +00001247 // SdkApiStubDexJar returns the dex jar for the stubs for the prebuilt
1248 // java_sdk_library_import module. It is needed by the hiddenapi processing tool which
1249 // processes dex files.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001250 SdkApiStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
Paul Duffinf4600f62021-05-13 22:34:45 +01001251
Jihoon Kangbd093452023-12-26 19:08:01 +00001252 // SdkApiExportableStubDexJar returns the exportable dex jar for the stubs for
1253 // java_sdk_library module. It is needed by the hiddenapi processing tool which processes
1254 // dex files.
1255 SdkApiExportableStubDexJar(ctx android.BaseModuleContext, kind android.SdkKind) OptionalDexJarPath
1256
Paul Duffin32cf58a2021-05-18 16:32:50 +01001257 // SdkRemovedTxtFile returns the optional path to the removed.txt file for the specified sdk kind.
1258 SdkRemovedTxtFile(ctx android.BaseModuleContext, kind android.SdkKind) android.OptionalPath
1259
Paul Duffinf4600f62021-05-13 22:34:45 +01001260 // sharedLibrary returns true if this can be used as a shared library.
1261 sharedLibrary() bool
Jihoon Kanga3a05462024-04-05 00:36:44 +00001262
Jihoon Kang28c96572024-09-11 23:44:44 +00001263 // getImplLibraryModule returns the pointer to the implementation library submodule of this
1264 // sdk library.
Jihoon Kanga3a05462024-04-05 00:36:44 +00001265 getImplLibraryModule() *Library
Paul Duffin859fe962020-05-15 10:20:31 +01001266}
1267
Jihoon Kang28c96572024-09-11 23:44:44 +00001268type SdkLibraryInfo struct {
1269 // GeneratingLibs is the names of the library modules that this sdk library
1270 // generates. Note that this only includes the name of the modules that other modules can
1271 // depend on, and is not a holistic list of generated modules.
1272 GeneratingLibs []string
1273}
1274
1275var SdkLibraryInfoProvider = blueprint.NewProvider[SdkLibraryInfo]()
1276
1277func getGeneratingLibs(ctx android.ModuleContext, sdkVersion android.SdkSpec, sdkLibraryModuleName string, sdkInfo SdkLibraryInfo) []string {
1278 apiLevel := sdkVersion.ApiLevel
1279 if apiLevel.IsPreview() {
1280 return sdkInfo.GeneratingLibs
1281 }
1282
1283 generatingPrebuilts := []string{}
1284 for _, apiScope := range AllApiScopes {
1285 scopePrebuiltModuleName := prebuiltApiModuleName("sdk", sdkLibraryModuleName, apiScope.name, apiLevel.String())
1286 if ctx.OtherModuleExists(scopePrebuiltModuleName) {
1287 generatingPrebuilts = append(generatingPrebuilts, scopePrebuiltModuleName)
1288 }
1289 }
1290 return generatingPrebuilts
1291}
1292
Inseob Kimc0907f12019-02-08 21:00:45 +09001293type SdkLibrary struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001294 Library
Jiyong Parkc678ad32018-04-10 13:07:10 +09001295
Sundong Ahn054b19a2018-10-19 13:46:09 +09001296 sdkLibraryProperties sdkLibraryProperties
Jiyong Parkc678ad32018-04-10 13:07:10 +09001297
Paul Duffin3375e352020-04-28 10:44:03 +01001298 // Map from api scope to the scope specific property structure.
1299 scopeToProperties map[*apiScope]*ApiScopeProperties
1300
Paul Duffin56d44902020-01-31 13:36:25 +00001301 commonToSdkLibraryAndImport
Jihoon Kanga3a05462024-04-05 00:36:44 +00001302
1303 builtInstalledForApex []dexpreopterInstall
Jiyong Parkc678ad32018-04-10 13:07:10 +09001304}
1305
Inseob Kimc0907f12019-02-08 21:00:45 +09001306var _ SdkLibraryDependency = (*SdkLibrary)(nil)
Colin Cross897d2ed2019-02-11 14:03:51 -08001307
Paul Duffin3375e352020-04-28 10:44:03 +01001308func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
1309 return module.sdkLibraryProperties.Generate_system_and_test_apis
1310}
1311
Jihoon Kanga3a05462024-04-05 00:36:44 +00001312func (module *SdkLibrary) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
1313 if module.implLibraryModule != nil {
1314 return module.implLibraryModule.DexJarBuildPath(ctx)
1315 }
1316 return makeUnsetDexJarPath()
1317}
1318
1319func (module *SdkLibrary) DexJarInstallPath() android.Path {
1320 if module.implLibraryModule != nil {
1321 return module.implLibraryModule.DexJarInstallPath()
1322 }
1323 return nil
1324}
1325
Paul Duffin3375e352020-04-28 10:44:03 +01001326func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
1327 // Check to see if any scopes have been explicitly enabled. If any have then all
1328 // must be.
1329 anyScopesExplicitlyEnabled := false
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001330 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001331 scopeProperties := module.scopeToProperties[scope]
1332 if scopeProperties.Enabled != nil {
1333 anyScopesExplicitlyEnabled = true
1334 break
1335 }
Paul Duffind1b3a922020-01-22 11:57:20 +00001336 }
Paul Duffin3375e352020-04-28 10:44:03 +01001337
1338 var generatedScopes apiScopes
1339 enabledScopes := make(map[*apiScope]struct{})
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001340 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001341 scopeProperties := module.scopeToProperties[scope]
1342 // If any scopes are explicitly enabled then ignore the legacy enabled status.
1343 // This is to ensure that any new usages of this module type do not rely on legacy
1344 // behaviour.
1345 defaultEnabledStatus := false
1346 if anyScopesExplicitlyEnabled {
1347 defaultEnabledStatus = scope.defaultEnabledStatus
1348 } else {
1349 defaultEnabledStatus = scope.legacyEnabledStatus(module)
1350 }
1351 enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
1352 if enabled {
1353 enabledScopes[scope] = struct{}{}
1354 generatedScopes = append(generatedScopes, scope)
1355 }
1356 }
1357
1358 // Now check to make sure that any scope that is extended by an enabled scope is also
1359 // enabled.
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00001360 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01001361 if _, ok := enabledScopes[scope]; ok {
1362 extends := scope.extends
1363 if extends != nil {
1364 if _, ok := enabledScopes[extends]; !ok {
1365 ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
1366 }
1367 }
1368 }
1369 }
1370
1371 return generatedScopes
Paul Duffind1b3a922020-01-22 11:57:20 +00001372}
1373
satayev758968a2021-12-06 11:42:40 +00001374var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil)
1375
satayev8f088b02021-12-06 11:40:46 +00001376func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) {
Jihoon Kanga3a05462024-04-05 00:36:44 +00001377 CheckMinSdkVersion(ctx, &module.Library)
1378}
1379
1380func CheckMinSdkVersion(ctx android.ModuleContext, module *Library) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00001381 android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) {
satayev8f088b02021-12-06 11:40:46 +00001382 ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
1383 isExternal := !module.depIsInSameApex(ctx, child)
1384 if am, ok := child.(android.ApexModule); ok {
1385 if !do(ctx, parent, am, isExternal) {
1386 return false
1387 }
1388 }
1389 return !isExternal
1390 })
1391 })
1392}
1393
Paul Duffineedc5d52020-06-12 17:46:39 +01001394type sdkLibraryComponentTag struct {
1395 blueprint.BaseDependencyTag
1396 name string
1397}
1398
1399// Mark this tag so dependencies that use it are excluded from visibility enforcement.
1400func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
1401
1402var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
Paul Duffine74ac732020-02-06 13:51:46 +00001403
Jiyong Parke3833882020-02-17 17:28:10 +09001404func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
Paul Duffineedc5d52020-06-12 17:46:39 +01001405 if dt, ok := depTag.(sdkLibraryComponentTag); ok {
Jiyong Parke3833882020-02-17 17:28:10 +09001406 return dt == xmlPermissionsFileTag
1407 }
1408 return false
1409}
1410
Paul Duffineedc5d52020-06-12 17:46:39 +01001411var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
Paul Duffin5df79302020-05-16 15:52:12 +01001412
Jeongik Chaaaa6dcd2024-05-22 00:41:28 +09001413var _ android.InstallNeededDependencyTag = sdkLibraryComponentTag{}
1414
Jihoon Kang46d66de2024-05-22 22:42:39 +00001415// To satisfy the CopyDirectlyInAnyApexTag interface. Implementation library of the sdk library
1416// in an apex is considered to be directly in the apex, as if it was listed in java_libs.
1417func (t sdkLibraryComponentTag) CopyDirectlyInAnyApex() {}
1418
1419var _ android.CopyDirectlyInAnyApexTag = implLibraryTag
1420
Jeongik Chaaaa6dcd2024-05-22 00:41:28 +09001421func (t sdkLibraryComponentTag) InstallDepNeeded() bool {
1422 return t.name == "xml-permissions-file" || t.name == "impl-library"
1423}
1424
Paul Duffin44f1d842020-06-26 20:17:02 +01001425// Add the dependencies on the child modules in the component deps mutator.
1426func (module *SdkLibrary) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin3375e352020-04-28 10:44:03 +01001427 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
Paul Duffind1b3a922020-01-22 11:57:20 +00001428 // Add dependencies to the stubs library
Spandan Das877f39d2023-03-29 16:19:51 +00001429 stubModuleName := module.stubsLibraryModuleName(apiScope)
Jihoon Kangbd093452023-12-26 19:08:01 +00001430 ctx.AddVariationDependencies(nil, apiScope.everythingStubsTag, stubModuleName)
Jihoon Kang1147b312023-06-08 23:25:57 +00001431
Jihoon Kangbd093452023-12-26 19:08:01 +00001432 exportableStubModuleName := module.exportableStubsLibraryModuleName(apiScope)
1433 ctx.AddVariationDependencies(nil, apiScope.exportableStubsTag, exportableStubModuleName)
Paul Duffind1b3a922020-01-22 11:57:20 +00001434
Paul Duffin15f34ef2020-07-20 18:04:44 +01001435 // Add a dependency on the stubs source in order to access both stubs source and api information.
1436 ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
Paul Duffin958806b2022-05-16 13:10:47 +00001437
1438 if module.compareAgainstLatestApi(apiScope) {
1439 // Add dependencies on the latest finalized version of the API .txt file.
1440 latestApiModuleName := module.latestApiModuleName(apiScope)
1441 ctx.AddDependency(module, apiScope.latestApiModuleTag, latestApiModuleName)
1442
1443 // Add dependencies on the latest finalized version of the remove API .txt file.
1444 latestRemovedApiModuleName := module.latestRemovedApiModuleName(apiScope)
1445 ctx.AddDependency(module, apiScope.latestRemovedApiModuleTag, latestRemovedApiModuleName)
1446 }
Sundong Ahn054b19a2018-10-19 13:46:09 +09001447 }
1448
Paul Duffindfa131e2020-05-15 20:37:11 +01001449 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01001450 // Add dependency to the rule for generating the implementation library.
1451 ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
1452
Paul Duffindfa131e2020-05-15 20:37:11 +01001453 if module.sharedLibrary() {
1454 // Add dependency to the rule for generating the xml permissions file
Paul Duffineedc5d52020-06-12 17:46:39 +01001455 ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
Paul Duffindfa131e2020-05-15 20:37:11 +01001456 }
Paul Duffin44f1d842020-06-26 20:17:02 +01001457 }
1458}
Paul Duffine74ac732020-02-06 13:51:46 +00001459
Paul Duffin44f1d842020-06-26 20:17:02 +01001460// Add other dependencies as normal.
1461func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
Jihoon Kange4a90172024-07-18 22:49:08 +00001462 // If the module does not create an implementation library or defaults to stubs,
1463 // mark the top level sdk library as stubs module as the module will provide stubs via
1464 // "magic" when listed as a dependency in the Android.bp files.
1465 notCreateImplLib := proptools.Bool(module.sdkLibraryProperties.Api_only)
1466 preferStubs := proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1467 module.properties.Is_stubs_module = proptools.BoolPtr(notCreateImplLib || preferStubs)
1468
Anton Hanssone77fccc2021-01-20 16:52:41 +00001469 var missingApiModules []string
1470 for _, apiScope := range module.getGeneratedApiScopes(ctx) {
1471 if apiScope.unstable {
1472 continue
1473 }
Paul Duffin958806b2022-05-16 13:10:47 +00001474 if m := module.latestApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001475 missingApiModules = append(missingApiModules, m)
1476 }
Paul Duffin958806b2022-05-16 13:10:47 +00001477 if m := module.latestRemovedApiModuleName(apiScope); !ctx.OtherModuleExists(m) {
Anton Hanssone77fccc2021-01-20 16:52:41 +00001478 missingApiModules = append(missingApiModules, m)
1479 }
Paul Duffin958806b2022-05-16 13:10:47 +00001480 if m := module.latestIncompatibilitiesModuleName(apiScope); !ctx.OtherModuleExists(m) {
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001481 missingApiModules = append(missingApiModules, m)
1482 }
Anton Hanssone77fccc2021-01-20 16:52:41 +00001483 }
1484 if len(missingApiModules) != 0 && !module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api {
1485 m := module.Name() + " is missing tracking files for previously released library versions.\n"
1486 m += "You need to do one of the following:\n"
1487 m += "- Add `unsafe_ignore_missing_latest_api: true` to your blueprint (to disable compat tracking)\n"
1488 m += "- Add a set of prebuilt txt files representing the last released version of this library for compat checking.\n"
1489 m += " (the current set of API files can be used as a seed for this compatibility tracking\n"
1490 m += "\n"
1491 m += "The following filegroup modules are missing:\n "
1492 m += strings.Join(missingApiModules, "\n ") + "\n"
1493 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."
1494 ctx.ModuleErrorf(m)
1495 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001496}
1497
Inseob Kimc0907f12019-02-08 21:00:45 +09001498func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Das5ae65ee2024-04-16 22:03:26 +00001499 if disableSourceApexVariant(ctx) {
1500 // Prebuilts are active, do not create the installation rules for the source javalib.
1501 // Even though the source javalib is not used, we need to hide it to prevent duplicate installation rules.
1502 // TODO (b/331665856): Implement a principled solution for this.
1503 module.HideFromMake()
1504 }
satayev8f088b02021-12-06 11:40:46 +00001505
Paul Duffina2ae7e02020-09-11 11:55:00 +01001506 module.generateCommonBuildActions(ctx)
1507
Jihoon Kanga3a05462024-04-05 00:36:44 +00001508 module.stem = proptools.StringDefault(module.overridableProperties.Stem, ctx.ModuleName())
1509
1510 module.provideHiddenAPIPropertyInfo(ctx)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001511
Paul Duffinb97b1572021-04-29 21:50:40 +01001512 // Collate the components exported by this module. All scope specific modules are exported but
1513 // the impl and xml component modules are not.
1514 exportedComponents := map[string]struct{}{}
1515
Sundong Ahn57368eb2018-07-06 11:20:23 +09001516 // Record the paths to the header jars of the library (stubs and impl).
Paul Duffind1b3a922020-01-22 11:57:20 +00001517 // When this java_sdk_library is depended upon from others via "libs" property,
Jiyong Parkc678ad32018-04-10 13:07:10 +09001518 // the recorded paths will be returned depending on the link type of the caller.
1519 ctx.VisitDirectDeps(func(to android.Module) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001520 tag := ctx.OtherModuleDependencyTag(to)
1521
Paul Duffinc8782502020-04-29 20:45:27 +01001522 // Extract information from any of the scope specific dependencies.
1523 if scopeTag, ok := tag.(scopeDependencyTag); ok {
1524 apiScope := scopeTag.apiScope
Paul Duffin803a9562020-05-20 11:52:25 +01001525 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
Paul Duffinc8782502020-04-29 20:45:27 +01001526
1527 // Extract information from the dependency. The exact information extracted
1528 // is determined by the nature of the dependency which is determined by the tag.
1529 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffinb97b1572021-04-29 21:50:40 +01001530
1531 exportedComponents[ctx.OtherModuleName(to)] = struct{}{}
Jihoon Kang4b9220a2024-08-22 22:11:04 +00001532
1533 ctx.Phony(ctx.ModuleName(), scopePaths.stubsHeaderPath...)
Sundong Ahn20e998b2018-07-24 11:19:26 +09001534 }
Jihoon Kang8479dea2024-04-04 01:19:05 +00001535
1536 if tag == implLibraryTag {
1537 if dep, ok := android.OtherModuleProvider(ctx, to, JavaInfoProvider); ok {
1538 module.implLibraryHeaderJars = append(module.implLibraryHeaderJars, dep.HeaderJars...)
Jihoon Kanga3a05462024-04-05 00:36:44 +00001539 module.implLibraryModule = to.(*Library)
1540 android.SetProvider(ctx, JavaInfoProvider, dep)
Jihoon Kang8479dea2024-04-04 01:19:05 +00001541 }
1542 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001543 })
Paul Duffinb97b1572021-04-29 21:50:40 +01001544
Jihoon Kanga3a05462024-04-05 00:36:44 +00001545 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
1546 if !apexInfo.IsForPlatform() {
1547 module.hideApexVariantFromMake = true
1548 }
1549
1550 if module.implLibraryModule != nil {
1551 if ctx.Device() {
1552 module.classesJarPaths = android.Paths{module.implLibraryModule.implementationJarFile}
1553 module.bootDexJarPath = module.implLibraryModule.bootDexJarPath
1554 module.uncompressDexState = module.implLibraryModule.uncompressDexState
1555 module.active = module.implLibraryModule.active
1556 }
1557
1558 module.outputFile = module.implLibraryModule.outputFile
1559 module.dexJarFile = makeDexJarPathFromPath(module.implLibraryModule.dexJarFile.Path())
1560 module.headerJarFile = module.implLibraryModule.headerJarFile
1561 module.implementationAndResourcesJar = module.implLibraryModule.implementationAndResourcesJar
1562 module.builtInstalledForApex = module.implLibraryModule.builtInstalledForApex
1563 module.dexpreopter.configPath = module.implLibraryModule.dexpreopter.configPath
1564 module.dexpreopter.outputProfilePathOnHost = module.implLibraryModule.dexpreopter.outputProfilePathOnHost
1565
Jihoon Kang34155e32024-05-20 19:08:49 +00001566 // Properties required for Library.AndroidMkEntries
1567 module.logtagsSrcs = module.implLibraryModule.logtagsSrcs
1568 module.dexpreopter.builtInstalled = module.implLibraryModule.dexpreopter.builtInstalled
1569 module.jacocoReportClassesFile = module.implLibraryModule.jacocoReportClassesFile
1570 module.dexer.proguardDictionary = module.implLibraryModule.dexer.proguardDictionary
1571 module.dexer.proguardUsageZip = module.implLibraryModule.dexer.proguardUsageZip
1572 module.linter.reports = module.implLibraryModule.linter.reports
Jihoon Kang629e2a32024-06-25 20:47:49 +00001573 module.linter.outputs.depSets = module.implLibraryModule.LintDepSets()
Jihoon Kang34155e32024-05-20 19:08:49 +00001574
Jihoon Kanga3a05462024-04-05 00:36:44 +00001575 if !module.Host() {
1576 module.hostdexInstallFile = module.implLibraryModule.hostdexInstallFile
1577 }
1578
Colin Crossa6182ab2024-08-21 10:47:44 -07001579 if installFilesInfo, ok := android.OtherModuleProvider(ctx, module.implLibraryModule, android.InstallFilesProvider); ok {
1580 if installFilesInfo.CheckbuildTarget != nil {
1581 ctx.CheckbuildFile(installFilesInfo.CheckbuildTarget)
1582 }
1583 }
Jihoon Kanga3a05462024-04-05 00:36:44 +00001584 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: module.implLibraryModule.uniqueSrcFiles.Strings()})
1585 }
1586
Paul Duffinb97b1572021-04-29 21:50:40 +01001587 // Make the set of components exported by this module available for use elsewhere.
Cole Faust18994c72023-02-28 16:02:16 -08001588 exportedComponentInfo := android.ExportedComponentsInfo{Components: android.SortedKeys(exportedComponents)}
Colin Cross40213022023-12-13 15:19:49 -08001589 android.SetProvider(ctx, android.ExportedComponentsInfoProvider, exportedComponentInfo)
Paul Duffin958806b2022-05-16 13:10:47 +00001590
1591 // Provide additional information for inclusion in an sdk's generated .info file.
1592 additionalSdkInfo := map[string]interface{}{}
1593 additionalSdkInfo["dist_stem"] = module.distStem()
Paul Duffine8409952022-09-22 16:24:46 +01001594 baseModuleName := module.distStem()
Paul Duffin958806b2022-05-16 13:10:47 +00001595 scopes := map[string]interface{}{}
1596 additionalSdkInfo["scopes"] = scopes
1597 for scope, scopePaths := range module.scopePaths {
1598 scopeInfo := map[string]interface{}{}
1599 scopes[scope.name] = scopeInfo
1600 scopeInfo["current_api"] = scope.snapshotRelativeCurrentApiTxtPath(baseModuleName)
1601 scopeInfo["removed_api"] = scope.snapshotRelativeRemovedApiTxtPath(baseModuleName)
Jihoon Kang5623e542024-01-31 23:27:26 +00001602 if p := scopePaths.latestApiPaths; len(p) > 0 {
1603 // The last path in the list is the one that applies to this scope, the
1604 // preceding ones, if any, are for the scope(s) that it extends.
1605 scopeInfo["latest_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001606 }
Jihoon Kang5623e542024-01-31 23:27:26 +00001607 if p := scopePaths.latestRemovedApiPaths; len(p) > 0 {
1608 // The last path in the list is the one that applies to this scope, the
1609 // preceding ones, if any, are for the scope(s) that it extends.
1610 scopeInfo["latest_removed_api"] = p[len(p)-1].String()
Paul Duffin958806b2022-05-16 13:10:47 +00001611 }
1612 }
Colin Cross40213022023-12-13 15:19:49 -08001613 android.SetProvider(ctx, android.AdditionalSdkInfoProvider, android.AdditionalSdkInfo{additionalSdkInfo})
mrziwang9f7b9f42024-07-10 12:18:06 -07001614 module.setOutputFiles(ctx)
Jihoon Kang28c96572024-09-11 23:44:44 +00001615
1616 var generatingLibs []string
1617 for _, apiScope := range AllApiScopes {
1618 if _, ok := module.scopePaths[apiScope]; ok {
1619 generatingLibs = append(generatingLibs, module.stubsLibraryModuleName(apiScope))
1620 }
1621 }
1622
mrziwang9f7b9f42024-07-10 12:18:06 -07001623 if module.requiresRuntimeImplementationLibrary() && module.implLibraryModule != nil {
Jihoon Kang28c96572024-09-11 23:44:44 +00001624 generatingLibs = append(generatingLibs, module.implLibraryModuleName())
mrziwang9f7b9f42024-07-10 12:18:06 -07001625 setOutputFiles(ctx, module.implLibraryModule.Module)
1626 }
Jihoon Kang28c96572024-09-11 23:44:44 +00001627
1628 android.SetProvider(ctx, SdkLibraryInfoProvider, SdkLibraryInfo{
1629 GeneratingLibs: generatingLibs,
1630 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09001631}
1632
Jihoon Kanga3a05462024-04-05 00:36:44 +00001633func (module *SdkLibrary) BuiltInstalledForApex() []dexpreopterInstall {
1634 return module.builtInstalledForApex
1635}
1636
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001637func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffindfa131e2020-05-15 20:37:11 +01001638 if !module.requiresRuntimeImplementationLibrary() {
Paul Duffin43db9be2019-12-30 17:35:49 +00001639 return nil
1640 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001641 entriesList := module.Library.AndroidMkEntries()
Jihoon Kanga3a05462024-04-05 00:36:44 +00001642 entries := &entriesList[0]
1643 entries.Required = append(entries.Required, module.implLibraryModuleName())
Yo Chiang07d75072020-06-05 17:43:19 +08001644 if module.sharedLibrary() {
Yo Chiang07d75072020-06-05 17:43:19 +08001645 entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1646 }
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001647 return entriesList
Jiyong Park82484c02018-04-23 21:41:26 +09001648}
1649
Anton Hansson5fd5d242020-03-27 19:43:19 +00001650// The dist path of the stub artifacts
1651func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
Colin Crossf0eace92021-06-02 13:02:23 -07001652 return path.Join("apistubs", module.distGroup(), apiScope.name)
Anton Hansson5fd5d242020-03-27 19:43:19 +00001653}
1654
Paul Duffin12ceb462019-12-24 20:31:31 +00001655// Get the sdk version for use when compiling the stubs library.
Paul Duffin780c5f42020-05-12 15:52:55 +01001656func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
Paul Duffin87a05a32020-05-12 11:50:28 +01001657 scopeProperties := module.scopeToProperties[apiScope]
1658 if scopeProperties.Sdk_version != nil {
1659 return proptools.String(scopeProperties.Sdk_version)
1660 }
1661
Jiyong Parkf1691d22021-03-29 20:11:58 +09001662 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin12ceb462019-12-24 20:31:31 +00001663 if sdkDep.hasStandardLibs() {
1664 // If building against a standard sdk then use the sdk version appropriate for the scope.
Paul Duffind1b3a922020-01-22 11:57:20 +00001665 return apiScope.sdkVersion
Paul Duffin12ceb462019-12-24 20:31:31 +00001666 } else {
1667 // Otherwise, use no system module.
1668 return "none"
1669 }
1670}
1671
Paul Duffin31310252020-11-20 21:26:20 +00001672func (module *SdkLibrary) distStem() string {
1673 return proptools.StringDefault(module.sdkLibraryProperties.Dist_stem, module.BaseModuleName())
1674}
1675
Colin Cross986b69a2021-06-01 13:13:40 -07001676// distGroup returns the subdirectory of the dist path of the stub artifacts.
1677func (module *SdkLibrary) distGroup() string {
Colin Cross59b92bf2021-06-01 14:07:56 -07001678 return proptools.StringDefault(module.sdkLibraryProperties.Dist_group, "unknown")
Colin Cross986b69a2021-06-01 13:13:40 -07001679}
1680
Paul Duffin958806b2022-05-16 13:10:47 +00001681func latestPrebuiltApiModuleName(name string, apiScope *apiScope) string {
1682 return PrebuiltApiModuleName(name, apiScope.name, "latest")
1683}
1684
Jihoon Kang748a24d2024-03-20 21:29:39 +00001685func latestPrebuiltApiCombinedModuleName(name string, apiScope *apiScope) string {
1686 return PrebuiltApiCombinedModuleName(name, apiScope.name, "latest")
1687}
1688
Paul Duffind1b3a922020-01-22 11:57:20 +00001689func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001690 return ":" + module.latestApiModuleName(apiScope)
1691}
1692
1693func (module *SdkLibrary) latestApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001694 return latestPrebuiltApiCombinedModuleName(module.distStem(), apiScope)
Jiyong Park58c518b2018-05-12 22:29:12 +09001695}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001696
Paul Duffind1b3a922020-01-22 11:57:20 +00001697func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001698 return ":" + module.latestRemovedApiModuleName(apiScope)
1699}
1700
1701func (module *SdkLibrary) latestRemovedApiModuleName(apiScope *apiScope) string {
Jihoon Kang748a24d2024-03-20 21:29:39 +00001702 return latestPrebuiltApiCombinedModuleName(module.distStem()+"-removed", apiScope)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001703}
1704
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001705func (module *SdkLibrary) latestIncompatibilitiesFilegroupName(apiScope *apiScope) string {
Paul Duffin958806b2022-05-16 13:10:47 +00001706 return ":" + module.latestIncompatibilitiesModuleName(apiScope)
1707}
1708
1709func (module *SdkLibrary) latestIncompatibilitiesModuleName(apiScope *apiScope) string {
1710 return latestPrebuiltApiModuleName(module.distStem()+"-incompatibilities", apiScope)
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001711}
1712
Jihoon Kang0c705a42023-08-02 06:44:57 +00001713// The listed modules' stubs contents do not match the corresponding txt files,
1714// but require additional api contributions to generate the full stubs.
1715// This method returns the name of the additional api contribution module
1716// for corresponding sdk_library modules.
1717func (module *SdkLibrary) apiLibraryAdditionalApiContribution() string {
1718 if val, ok := apiLibraryAdditionalProperties[module.Name()]; ok {
Jihoon Kangb0f4c022024-08-06 00:15:25 +00001719 return val
Jihoon Kang0c705a42023-08-02 06:44:57 +00001720 }
1721 return ""
1722}
1723
Anton Hansson944e77d2020-08-19 11:40:22 +01001724func childModuleVisibility(childVisibility []string) []string {
1725 if childVisibility == nil {
1726 // No child visibility set. The child will use the visibility of the sdk_library.
1727 return nil
1728 }
1729
1730 // Prepend an override to ignore the sdk_library's visibility, and rely on the child visibility.
1731 var visibility []string
1732 visibility = append(visibility, "//visibility:override")
1733 visibility = append(visibility, childVisibility...)
1734 return visibility
1735}
1736
Paul Duffin5df79302020-05-16 15:52:12 +01001737// Creates the implementation java library
1738func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
Anton Hansson944e77d2020-08-19 11:40:22 +01001739 visibility := childModuleVisibility(module.sdkLibraryProperties.Impl_library_visibility)
1740
Cole Faustb7493472024-08-28 11:55:52 -07001741 staticLibs := module.properties.Static_libs.Clone()
1742 staticLibs.AppendSimpleValue(module.sdkLibraryProperties.Impl_only_static_libs)
Paul Duffin5df79302020-05-16 15:52:12 +01001743 props := struct {
Paul Duffin77590a82022-04-28 14:13:30 +00001744 Name *string
Cole Faust8eeae4b2024-09-12 11:51:04 -07001745 Enabled proptools.Configurable[bool]
Paul Duffin77590a82022-04-28 14:13:30 +00001746 Visibility []string
Paul Duffin77590a82022-04-28 14:13:30 +00001747 Libs []string
Cole Faustb7493472024-08-28 11:55:52 -07001748 Static_libs proptools.Configurable[[]string]
Paul Duffin77590a82022-04-28 14:13:30 +00001749 Apex_available []string
Jihoon Kanga3a05462024-04-05 00:36:44 +00001750 Stem *string
Paul Duffin5df79302020-05-16 15:52:12 +01001751 }{
1752 Name: proptools.StringPtr(module.implLibraryModuleName()),
Cole Faust8eeae4b2024-09-12 11:51:04 -07001753 Enabled: module.EnabledProperty(),
Anton Hansson944e77d2020-08-19 11:40:22 +01001754 Visibility: visibility,
Jihoon Kanga3a05462024-04-05 00:36:44 +00001755
1756 Libs: append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...),
1757
Cole Faustb7493472024-08-28 11:55:52 -07001758 Static_libs: staticLibs,
Paul Duffin77590a82022-04-28 14:13:30 +00001759 // Pass the apex_available settings down so that the impl library can be statically
1760 // embedded within a library that is added to an APEX. Needed for updatable-media.
1761 Apex_available: module.ApexAvailable(),
Jihoon Kanga3a05462024-04-05 00:36:44 +00001762
1763 Stem: proptools.StringPtr(module.Name()),
Paul Duffin5df79302020-05-16 15:52:12 +01001764 }
1765
1766 properties := []interface{}{
1767 &module.properties,
1768 &module.protoProperties,
1769 &module.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -07001770 &module.dexProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001771 &module.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -07001772 &module.linter.properties,
Spandan Dasb9c58352024-05-13 18:29:45 +00001773 &module.overridableProperties,
Paul Duffin5df79302020-05-16 15:52:12 +01001774 &props,
1775 module.sdkComponentPropertiesForChildLibrary(),
1776 }
1777 mctx.CreateModule(LibraryFactory, properties...)
1778}
1779
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001780type libraryProperties struct {
1781 Name *string
Cole Faust8eeae4b2024-09-12 11:51:04 -07001782 Enabled proptools.Configurable[bool]
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001783 Visibility []string
1784 Srcs []string
1785 Installable *bool
1786 Sdk_version *string
1787 System_modules *string
1788 Patch_module *string
1789 Libs []string
1790 Static_libs []string
1791 Compile_dex *bool
1792 Java_version *string
1793 Openjdk9 struct {
1794 Srcs []string
1795 Javacflags []string
1796 }
1797 Dist struct {
1798 Targets []string
1799 Dest *string
1800 Dir *string
1801 Tag *string
1802 }
Jihoon Kangfa3f0782024-08-21 20:42:18 +00001803 Is_stubs_module *bool
1804 Stub_contributing_api *string
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001805}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001806
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001807func (module *SdkLibrary) stubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope) libraryProperties {
1808 props := libraryProperties{}
Cole Faust8eeae4b2024-09-12 11:51:04 -07001809 props.Enabled = module.EnabledProperty()
Jihoon Kang786df932023-09-07 01:18:31 +00001810 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001811 // sources are generated from the droiddoc
Paul Duffin12ceb462019-12-24 20:31:31 +00001812 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
Paul Duffin52d398a2019-06-11 12:31:14 +01001813 props.Sdk_version = proptools.StringPtr(sdkVersion)
Paul Duffina18abc22020-05-16 18:54:24 +01001814 props.System_modules = module.deviceProperties.System_modules
1815 props.Patch_module = module.properties.Patch_module
Paul Duffin367ab912019-12-23 19:40:36 +00001816 props.Installable = proptools.BoolPtr(false)
Sundong Ahn054b19a2018-10-19 13:46:09 +09001817 props.Libs = module.sdkLibraryProperties.Stub_only_libs
Mark White9421c4c2023-08-10 00:07:03 +00001818 props.Libs = append(props.Libs, module.scopeToProperties[apiScope].Libs...)
Anton Hanssondae54cd2021-04-21 16:30:10 +01001819 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Paul Duffine22c2ab2020-05-20 19:35:27 +01001820 // The stub-annotations library contains special versions of the annotations
1821 // with CLASS retention policy, so that they're kept.
1822 if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1823 props.Libs = append(props.Libs, "stub-annotations")
1824 }
Paul Duffina18abc22020-05-16 18:54:24 +01001825 props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1826 props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
Anton Hansson83509b52020-05-21 09:21:57 +01001827 // We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1828 // interop with older developer tools that don't support 1.9.
1829 props.Java_version = proptools.StringPtr("1.8")
Jihoon Kangfe914ed2024-02-12 22:49:21 +00001830 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kangfa3f0782024-08-21 20:42:18 +00001831 props.Stub_contributing_api = proptools.StringPtr(apiScope.kind.String())
Paul Duffinf4600f62021-05-13 22:34:45 +01001832
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001833 return props
1834}
1835
1836// Creates a static java library that has API stubs
1837func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1838
1839 props := module.stubsLibraryProps(mctx, apiScope)
1840 props.Name = proptools.StringPtr(module.sourceStubsLibraryModuleName(apiScope))
1841 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
1842
1843 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1844}
1845
1846// Create a static java library that compiles the "exportable" stubs
1847func (module *SdkLibrary) createExportableStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1848 props := module.stubsLibraryProps(mctx, apiScope)
1849 props.Name = proptools.StringPtr(module.exportableSourceStubsLibraryModuleName(apiScope))
1850 props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope) + "{.exportable}"}
1851
Paul Duffin859fe962020-05-15 10:20:31 +01001852 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jiyong Parkc678ad32018-04-10 13:07:10 +09001853}
1854
Paul Duffin6d0886e2020-04-07 18:49:53 +01001855// Creates a droidstubs module that creates stubs source files from the given full source
Paul Duffinc8782502020-04-29 20:45:27 +01001856// files and also updates and checks the API specification files.
Paul Duffin15f34ef2020-07-20 18:04:44 +01001857func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, scopeSpecificDroidstubsArgs []string) {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001858 props := struct {
Sundong Ahn054b19a2018-10-19 13:46:09 +09001859 Name *string
Cole Faust8eeae4b2024-09-12 11:51:04 -07001860 Enabled proptools.Configurable[bool]
Paul Duffin4911a892020-04-29 23:35:13 +01001861 Visibility []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001862 Srcs []string
1863 Installable *bool
Paul Duffin52d398a2019-06-11 12:31:14 +01001864 Sdk_version *string
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001865 Api_surface *string
Paul Duffin12ceb462019-12-24 20:31:31 +00001866 System_modules *string
Cole Faustb7493472024-08-28 11:55:52 -07001867 Libs proptools.Configurable[[]string]
Paul Duffin6877e6d2020-09-25 19:59:14 +01001868 Output_javadoc_comments *bool
Paul Duffin11512472019-02-11 15:55:17 +00001869 Arg_files []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001870 Args *string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001871 Java_version *string
Paul Duffine22c2ab2020-05-20 19:35:27 +01001872 Annotations_enabled *bool
Sundong Ahn054b19a2018-10-19 13:46:09 +09001873 Merge_annotations_dirs []string
1874 Merge_inclusion_annotations_dirs []string
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001875 Generate_stubs *bool
Anton Hanssone87b03d2020-12-21 15:29:34 +00001876 Previous_api *string
Jihoon Kang6592e872023-12-19 01:13:16 +00001877 Aconfig_declarations []string
Sundong Ahn054b19a2018-10-19 13:46:09 +09001878 Check_api struct {
Anton Hanssone6056152020-12-31 10:37:27 +00001879 Current ApiToCheck
1880 Last_released ApiToCheck
Paul Duffin160fe412020-05-10 19:32:20 +01001881
1882 Api_lint struct {
1883 Enabled *bool
1884 New_since *string
1885 Baseline_file *string
1886 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001887 }
Sundong Ahn1b92c822018-05-29 11:35:17 +09001888 Aidl struct {
1889 Include_dirs []string
1890 Local_include_dirs []string
1891 }
Paul Duffin040e9062020-11-23 17:41:36 +00001892 Dists []android.Dist
Jiyong Parkc678ad32018-04-10 13:07:10 +09001893 }{}
1894
Paul Duffin7b78b4d2020-04-28 14:08:32 +01001895 // The stubs source processing uses the same compile time classpath when extracting the
1896 // API from the implementation library as it does when compiling it. i.e. the same
1897 // * sdk version
1898 // * system_modules
1899 // * libs (static_libs/libs)
Paul Duffin250e6192019-06-07 10:44:37 +01001900
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001901 props.Name = proptools.StringPtr(name)
Cole Faust8eeae4b2024-09-12 11:51:04 -07001902 props.Enabled = module.EnabledProperty()
Anton Hansson944e77d2020-08-19 11:40:22 +01001903 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_source_visibility)
Paul Duffina18abc22020-05-16 18:54:24 +01001904 props.Srcs = append(props.Srcs, module.properties.Srcs...)
Anton Hanssonf8ea3722021-09-16 14:24:13 +01001905 props.Srcs = append(props.Srcs, module.sdkLibraryProperties.Api_srcs...)
Paul Duffina18abc22020-05-16 18:54:24 +01001906 props.Sdk_version = module.deviceProperties.Sdk_version
Jihoon Kang3198f3c2023-01-26 08:08:52 +00001907 props.Api_surface = &apiScope.name
Paul Duffina18abc22020-05-16 18:54:24 +01001908 props.System_modules = module.deviceProperties.System_modules
Jiyong Parkc678ad32018-04-10 13:07:10 +09001909 props.Installable = proptools.BoolPtr(false)
Sundong Ahne6f0b052018-06-05 16:46:14 +09001910 // A droiddoc module has only one Libs property and doesn't distinguish between
1911 // shared libs and static libs. So we need to add both of these libs to Libs property.
Cole Faustb7493472024-08-28 11:55:52 -07001912 props.Libs = proptools.NewConfigurable[[]string](nil, nil)
1913 props.Libs.AppendSimpleValue(module.properties.Libs)
1914 props.Libs.Append(module.properties.Static_libs)
1915 props.Libs.AppendSimpleValue(module.sdkLibraryProperties.Stub_only_libs)
1916 props.Libs.AppendSimpleValue(module.scopeToProperties[apiScope].Libs)
Paul Duffina18abc22020-05-16 18:54:24 +01001917 props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1918 props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1919 props.Java_version = module.properties.Java_version
Jiyong Parkc678ad32018-04-10 13:07:10 +09001920
Paul Duffine22c2ab2020-05-20 19:35:27 +01001921 props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
Sundong Ahn054b19a2018-10-19 13:46:09 +09001922 props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1923 props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
Jihoon Kang6592e872023-12-19 01:13:16 +00001924 props.Aconfig_declarations = module.sdkLibraryProperties.Aconfig_declarations
Sundong Ahn054b19a2018-10-19 13:46:09 +09001925
Paul Duffin6d0886e2020-04-07 18:49:53 +01001926 droidstubsArgs := []string{}
Paul Duffin235ffff2019-12-24 10:41:30 +00001927 if len(module.sdkLibraryProperties.Api_packages) != 0 {
Paul Duffin6d0886e2020-04-07 18:49:53 +01001928 droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
Paul Duffin235ffff2019-12-24 10:41:30 +00001929 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001930 droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
Anton Hanssonfd1c0d22023-11-02 15:18:09 +00001931 disabledWarnings := []string{"HiddenSuperclass"}
1932 if proptools.BoolDefault(module.sdkLibraryProperties.Api_lint.Legacy_errors_allowed, true) {
1933 disabledWarnings = append(disabledWarnings,
1934 "BroadcastBehavior",
1935 "DeprecationMismatch",
1936 "MissingPermission",
1937 "SdkConstant",
1938 "Todo",
1939 )
Paul Duffin235ffff2019-12-24 10:41:30 +00001940 }
Paul Duffin6d0886e2020-04-07 18:49:53 +01001941 droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
Sundong Ahnfb2721f2018-09-17 13:23:09 +09001942
Paul Duffin6877e6d2020-09-25 19:59:14 +01001943 // Output Javadoc comments for public scope.
1944 if apiScope == apiScopePublic {
1945 props.Output_javadoc_comments = proptools.BoolPtr(true)
1946 }
1947
Paul Duffin1fb487d2020-04-07 18:50:10 +01001948 // Add in scope specific arguments.
Paul Duffin0ff08bd2020-04-29 13:30:54 +01001949 droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
Paul Duffin11512472019-02-11 15:55:17 +00001950 props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
Paul Duffin6d0886e2020-04-07 18:49:53 +01001951 props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
Jiyong Parkc678ad32018-04-10 13:07:10 +09001952
Paul Duffin15f34ef2020-07-20 18:04:44 +01001953 // List of APIs identified from the provided source files are created. They are later
1954 // compared against to the not-yet-released (a.k.a current) list of APIs and to the
1955 // last-released (a.k.a numbered) list of API.
1956 currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1957 removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1958 apiDir := module.getApiDir()
1959 currentApiFileName = path.Join(apiDir, currentApiFileName)
1960 removedApiFileName = path.Join(apiDir, removedApiFileName)
Jiyong Parkc678ad32018-04-10 13:07:10 +09001961
Paul Duffin15f34ef2020-07-20 18:04:44 +01001962 // check against the not-yet-release API
1963 props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1964 props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
Jiyong Park58c518b2018-05-12 22:29:12 +09001965
Paul Duffin958806b2022-05-16 13:10:47 +00001966 if module.compareAgainstLatestApi(apiScope) {
Paul Duffin15f34ef2020-07-20 18:04:44 +01001967 // check against the latest released API
1968 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
Anton Hanssone87b03d2020-12-21 15:29:34 +00001969 props.Previous_api = latestApiFilegroupName
Paul Duffin15f34ef2020-07-20 18:04:44 +01001970 props.Check_api.Last_released.Api_file = latestApiFilegroupName
1971 props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1972 module.latestRemovedApiFilegroupName(apiScope))
Jaewoong Jung1a97ee02021-03-09 13:25:02 -08001973 props.Check_api.Last_released.Baseline_file = proptools.StringPtr(
1974 module.latestIncompatibilitiesFilegroupName(apiScope))
Paul Duffin160fe412020-05-10 19:32:20 +01001975
Paul Duffin15f34ef2020-07-20 18:04:44 +01001976 if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1977 // Enable api lint.
1978 props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1979 props.Check_api.Api_lint.New_since = latestApiFilegroupName
Paul Duffin160fe412020-05-10 19:32:20 +01001980
Paul Duffin15f34ef2020-07-20 18:04:44 +01001981 // If it exists then pass a lint-baseline.txt through to droidstubs.
1982 baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1983 baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1984 paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1985 if err != nil {
1986 mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1987 }
1988 if len(paths) == 1 {
1989 props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1990 } else if len(paths) != 0 {
1991 mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
Paul Duffin160fe412020-05-10 19:32:20 +01001992 }
1993 }
Paul Duffin15f34ef2020-07-20 18:04:44 +01001994 }
Jiyong Park58c518b2018-05-12 22:29:12 +09001995
Paul Duffin15f34ef2020-07-20 18:04:44 +01001996 if !Bool(module.sdkLibraryProperties.No_dist) {
Paul Duffin040e9062020-11-23 17:41:36 +00001997 // Dist the api txt and removed api txt artifacts for sdk builds.
1998 distDir := proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
Jihoon Kang02168052024-03-20 00:44:54 +00001999 stubsTypeTagPrefix := ""
2000 if mctx.Config().ReleaseHiddenApiExportableStubs() {
2001 stubsTypeTagPrefix = ".exportable"
2002 }
Paul Duffin040e9062020-11-23 17:41:36 +00002003 for _, p := range []struct {
2004 tag string
2005 pattern string
2006 }{
Jihoon Kangd1799f62024-02-20 23:01:38 +00002007 // "exportable" api files are copied to the dist directory instead of the
Jihoon Kang02168052024-03-20 00:44:54 +00002008 // "everything" api files when "RELEASE_HIDDEN_API_EXPORTABLE_STUBS" build flag
2009 // is set. Otherwise, the "everything" api files are copied to the dist directory.
2010 {tag: "%s.api.txt", pattern: "%s.txt"},
2011 {tag: "%s.removed-api.txt", pattern: "%s-removed.txt"},
Paul Duffin040e9062020-11-23 17:41:36 +00002012 } {
2013 props.Dists = append(props.Dists, android.Dist{
2014 Targets: []string{"sdk", "win_sdk"},
2015 Dir: distDir,
2016 Dest: proptools.StringPtr(fmt.Sprintf(p.pattern, module.distStem())),
Jihoon Kang02168052024-03-20 00:44:54 +00002017 Tag: proptools.StringPtr(fmt.Sprintf(p.tag, stubsTypeTagPrefix)),
Paul Duffin040e9062020-11-23 17:41:36 +00002018 })
2019 }
Anton Hansson5fd5d242020-03-27 19:43:19 +00002020 }
2021
Spandan Das2cc80ba2023-10-27 17:21:52 +00002022 mctx.CreateModule(DroidstubsFactory, &props, module.sdkComponentPropertiesForChildLibrary()).(*Droidstubs).CallHookIfAvailable(mctx)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002023}
2024
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002025func (module *SdkLibrary) createApiLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002026 props := struct {
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002027 Name *string
Cole Faust8eeae4b2024-09-12 11:51:04 -07002028 Enabled proptools.Configurable[bool]
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002029 Visibility []string
2030 Api_contributions []string
Cole Faustb7493472024-08-28 11:55:52 -07002031 Libs proptools.Configurable[[]string]
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002032 Static_libs []string
2033 System_modules *string
2034 Enable_validation *bool
2035 Stubs_type *string
2036 Sdk_version *string
2037 Previous_api *string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002038 }{}
2039
2040 props.Name = proptools.StringPtr(module.apiLibraryModuleName(apiScope))
Cole Faust8eeae4b2024-09-12 11:51:04 -07002041 props.Enabled = module.EnabledProperty()
Jihoon Kang786df932023-09-07 01:18:31 +00002042 props.Visibility = []string{"//visibility:override", "//visibility:private"}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002043
2044 apiContributions := []string{}
2045
2046 // Api surfaces are not independent of each other, but have subset relationships,
2047 // and so does the api files. To generate from-text stubs for api surfaces other than public,
2048 // all subset api domains' api_contriubtions must be added as well.
2049 scope := apiScope
2050 for scope != nil {
2051 apiContributions = append(apiContributions, module.stubsSourceModuleName(scope)+".api.contribution")
2052 scope = scope.extends
2053 }
Jihoon Kang0c705a42023-08-02 06:44:57 +00002054 if apiScope == apiScopePublic {
2055 additionalApiContribution := module.apiLibraryAdditionalApiContribution()
2056 if additionalApiContribution != "" {
2057 apiContributions = append(apiContributions, additionalApiContribution)
2058 }
2059 }
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002060
2061 props.Api_contributions = apiContributions
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002062
2063 // Ensure that stub-annotations is added to the classpath before any other libs
Cole Faustb7493472024-08-28 11:55:52 -07002064 props.Libs = proptools.NewConfigurable[[]string](nil, nil)
2065 props.Libs.AppendSimpleValue([]string{"stub-annotations"})
2066 props.Libs.AppendSimpleValue(module.properties.Libs)
2067 props.Libs.Append(module.properties.Static_libs)
2068 props.Libs.AppendSimpleValue(module.sdkLibraryProperties.Stub_only_libs)
2069 props.Libs.AppendSimpleValue(module.scopeToProperties[apiScope].Libs)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002070 props.Static_libs = module.sdkLibraryProperties.Stub_only_static_libs
Jihoon Kangd30ac8a2023-10-09 18:00:17 +00002071
Jihoon Kang4ec24872023-10-05 17:26:09 +00002072 props.System_modules = module.deviceProperties.System_modules
Jihoon Kang063ec002023-06-28 01:16:23 +00002073 props.Enable_validation = proptools.BoolPtr(true)
Jihoon Kang5d701272024-02-15 21:53:49 +00002074 props.Stubs_type = proptools.StringPtr("everything")
Jihoon Kang4ec24872023-10-05 17:26:09 +00002075
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002076 if module.deviceProperties.Sdk_version != nil {
2077 props.Sdk_version = module.deviceProperties.Sdk_version
2078 }
2079
2080 if module.compareAgainstLatestApi(apiScope) {
2081 // check against the latest released API
2082 latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
2083 props.Previous_api = latestApiFilegroupName
2084 }
2085
Spandan Das2cc80ba2023-10-27 17:21:52 +00002086 mctx.CreateModule(ApiLibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002087}
2088
Jihoon Kang02168052024-03-20 00:44:54 +00002089func (module *SdkLibrary) topLevelStubsLibraryProps(mctx android.DefaultableHookContext, apiScope *apiScope, doDist bool) libraryProperties {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002090 props := libraryProperties{}
2091
Cole Faust8eeae4b2024-09-12 11:51:04 -07002092 props.Enabled = module.EnabledProperty()
Jihoon Kang1147b312023-06-08 23:25:57 +00002093 props.Visibility = childModuleVisibility(module.sdkLibraryProperties.Stubs_library_visibility)
2094 sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
2095 props.Sdk_version = proptools.StringPtr(sdkVersion)
2096
Jihoon Kang1147b312023-06-08 23:25:57 +00002097 props.System_modules = module.deviceProperties.System_modules
2098
Jihoon Kang1147b312023-06-08 23:25:57 +00002099 // The imports need to be compiled to dex if the java_sdk_library requests it.
2100 compileDex := module.dexProperties.Compile_dex
2101 if module.stubLibrariesCompiledForDex() {
2102 compileDex = proptools.BoolPtr(true)
2103 }
2104 props.Compile_dex = compileDex
2105
Jihoon Kangfa3f0782024-08-21 20:42:18 +00002106 props.Stub_contributing_api = proptools.StringPtr(apiScope.kind.String())
2107
Jihoon Kang02168052024-03-20 00:44:54 +00002108 if !Bool(module.sdkLibraryProperties.No_dist) && doDist {
2109 props.Dist.Targets = []string{"sdk", "win_sdk"}
2110 props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.distStem()))
2111 props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
2112 props.Dist.Tag = proptools.StringPtr(".jar")
2113 }
Jihoon Kang85bc1932024-07-01 17:04:46 +00002114 props.Is_stubs_module = proptools.BoolPtr(true)
Jihoon Kang02168052024-03-20 00:44:54 +00002115
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002116 return props
2117}
2118
2119func (module *SdkLibrary) createTopLevelStubsLibrary(
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002120 mctx android.DefaultableHookContext, apiScope *apiScope) {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002121
Jihoon Kang02168052024-03-20 00:44:54 +00002122 // Dist the "everything" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is false
2123 doDist := !mctx.Config().ReleaseHiddenApiExportableStubs()
2124 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002125 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
2126
2127 // Add the stub compiling java_library/java_api_library as static lib based on build config
2128 staticLib := module.sourceStubsLibraryModuleName(apiScope)
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002129 if mctx.Config().BuildFromTextStub() && module.ModuleBuildFromTextStubs() {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002130 staticLib = module.apiLibraryModuleName(apiScope)
2131 }
2132 props.Static_libs = append(props.Static_libs, staticLib)
2133
2134 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2135}
2136
2137func (module *SdkLibrary) createTopLevelExportableStubsLibrary(
2138 mctx android.DefaultableHookContext, apiScope *apiScope) {
2139
Jihoon Kang02168052024-03-20 00:44:54 +00002140 // Dist the "exportable" stubs when the RELEASE_HIDDEN_API_EXPORTABLE_STUBS build flag is true
2141 doDist := mctx.Config().ReleaseHiddenApiExportableStubs()
2142 props := module.topLevelStubsLibraryProps(mctx, apiScope, doDist)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002143 props.Name = proptools.StringPtr(module.exportableStubsLibraryModuleName(apiScope))
2144
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002145 staticLib := module.exportableSourceStubsLibraryModuleName(apiScope)
2146 props.Static_libs = append(props.Static_libs, staticLib)
2147
Jihoon Kang1147b312023-06-08 23:25:57 +00002148 mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
2149}
2150
Paul Duffin958806b2022-05-16 13:10:47 +00002151func (module *SdkLibrary) compareAgainstLatestApi(apiScope *apiScope) bool {
2152 return !(apiScope.unstable || module.sdkLibraryProperties.Unsafe_ignore_missing_latest_api)
2153}
2154
Paul Duffinea8f8082021-06-24 13:25:57 +01002155// Implements android.ApexModule
Jooyung Han5e9013b2020-03-10 06:23:13 +09002156func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2157 depTag := mctx.OtherModuleDependencyTag(dep)
2158 if depTag == xmlPermissionsFileTag {
2159 return true
2160 }
Jihoon Kanga3a05462024-04-05 00:36:44 +00002161 if dep.Name() == module.implLibraryModuleName() {
2162 return true
2163 }
Jooyung Han5e9013b2020-03-10 06:23:13 +09002164 return module.Library.DepIsInSameApex(mctx, dep)
2165}
2166
Paul Duffinea8f8082021-06-24 13:25:57 +01002167// Implements android.ApexModule
2168func (module *SdkLibrary) UniqueApexVariations() bool {
2169 return module.uniqueApexVariations()
2170}
2171
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002172func (module *SdkLibrary) ModuleBuildFromTextStubs() bool {
2173 return proptools.BoolDefault(module.sdkLibraryProperties.Build_from_text_stub, true)
Jihoon Kang80456fd2023-11-15 19:22:14 +00002174}
2175
Jiyong Parkc678ad32018-04-10 13:07:10 +09002176// Creates the xml file that publicizes the runtime library
Paul Duffinf0229202020-04-29 16:47:28 +01002177func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002178 moduleMinApiLevel := module.Library.MinSdkVersion(mctx)
Pedro Loureiroc3621422021-09-28 15:40:23 +00002179 var moduleMinApiLevelStr = moduleMinApiLevel.String()
2180 if moduleMinApiLevel == android.NoneApiLevel {
2181 moduleMinApiLevelStr = "current"
2182 }
Jiyong Parke3833882020-02-17 17:28:10 +09002183 props := struct {
Pedro Loureiroc3621422021-09-28 15:40:23 +00002184 Name *string
Cole Faust8eeae4b2024-09-12 11:51:04 -07002185 Enabled proptools.Configurable[bool]
Pedro Loureiroc3621422021-09-28 15:40:23 +00002186 Lib_name *string
2187 Apex_available []string
2188 On_bootclasspath_since *string
2189 On_bootclasspath_before *string
2190 Min_device_sdk *string
2191 Max_device_sdk *string
2192 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002193 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002194 }{
Pedro Loureiroc3621422021-09-28 15:40:23 +00002195 Name: proptools.StringPtr(module.xmlPermissionsModuleName()),
Cole Faust8eeae4b2024-09-12 11:51:04 -07002196 Enabled: module.EnabledProperty(),
Pedro Loureiroc3621422021-09-28 15:40:23 +00002197 Lib_name: proptools.StringPtr(module.BaseModuleName()),
2198 Apex_available: module.ApexProperties.Apex_available,
2199 On_bootclasspath_since: module.commonSdkLibraryProperties.On_bootclasspath_since,
2200 On_bootclasspath_before: module.commonSdkLibraryProperties.On_bootclasspath_before,
2201 Min_device_sdk: module.commonSdkLibraryProperties.Min_device_sdk,
2202 Max_device_sdk: module.commonSdkLibraryProperties.Max_device_sdk,
2203 Sdk_library_min_api_level: &moduleMinApiLevelStr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00002204 Uses_libs_dependencies: module.usesLibraryProperties.Uses_libs,
Jiyong Parkc678ad32018-04-10 13:07:10 +09002205 }
Jiyong Parke3833882020-02-17 17:28:10 +09002206
Jiyong Parke3833882020-02-17 17:28:10 +09002207 mctx.CreateModule(sdkLibraryXmlFactory, &props)
Jiyong Parkc678ad32018-04-10 13:07:10 +09002208}
2209
Colin Cross571cccf2019-02-04 11:22:08 -08002210var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
2211
Jiyong Park82484c02018-04-23 21:41:26 +09002212func javaSdkLibraries(config android.Config) *[]string {
Colin Cross571cccf2019-02-04 11:22:08 -08002213 return config.Once(javaSdkLibrariesKey, func() interface{} {
Jiyong Park82484c02018-04-23 21:41:26 +09002214 return &[]string{}
2215 }).(*[]string)
2216}
2217
Paul Duffin749f98f2019-12-30 17:23:46 +00002218func (module *SdkLibrary) getApiDir() string {
2219 return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
2220}
2221
Jiyong Parkc678ad32018-04-10 13:07:10 +09002222// For a java_sdk_library module, create internal modules for stubs, docs,
2223// runtime libs and xml file. If requested, the stubs and docs are created twice
2224// once for public API level and once for system API level
Paul Duffinf0229202020-04-29 16:47:28 +01002225func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
Paul Duffina18abc22020-05-16 18:54:24 +01002226 if len(module.properties.Srcs) == 0 {
Inseob Kimc0907f12019-02-08 21:00:45 +09002227 mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
Jooyung Han58f26ab2019-12-18 15:34:32 +09002228 return
Inseob Kimc0907f12019-02-08 21:00:45 +09002229 }
2230
Paul Duffin37e0b772019-12-30 17:20:10 +00002231 // If this builds against standard libraries (i.e. is not part of the core libraries)
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002232 // then assume it provides both system and test apis.
Jiyong Parkf1691d22021-03-29 20:11:58 +09002233 sdkDep := decodeSdkDep(mctx, android.SdkContext(&module.Library))
Paul Duffin37e0b772019-12-30 17:20:10 +00002234 hasSystemAndTestApis := sdkDep.hasStandardLibs()
Paul Duffin3375e352020-04-28 10:44:03 +01002235 module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
Paul Duffin4f5c1ef2020-11-19 14:53:43 +00002236
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002237 missingCurrentApi := false
Inseob Kim8098faa2019-03-18 10:19:51 +09002238
Paul Duffin3375e352020-04-28 10:44:03 +01002239 generatedScopes := module.getGeneratedApiScopes(mctx)
Paul Duffind1b3a922020-01-22 11:57:20 +00002240
Paul Duffin749f98f2019-12-30 17:23:46 +00002241 apiDir := module.getApiDir()
Paul Duffin3375e352020-04-28 10:44:03 +01002242 for _, scope := range generatedScopes {
Inseob Kim8098faa2019-03-18 10:19:51 +09002243 for _, api := range []string{"current.txt", "removed.txt"} {
Paul Duffind1b3a922020-01-22 11:57:20 +00002244 path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
Inseob Kim8098faa2019-03-18 10:19:51 +09002245 p := android.ExistentPathForSource(mctx, path)
2246 if !p.Valid() {
Colin Cross18f840c2021-05-20 17:56:54 -07002247 if mctx.Config().AllowMissingDependencies() {
2248 mctx.AddMissingDependencies([]string{path})
2249 } else {
2250 mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
2251 missingCurrentApi = true
2252 }
Inseob Kim8098faa2019-03-18 10:19:51 +09002253 }
2254 }
2255 }
2256
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002257 if missingCurrentApi {
Inseob Kim8098faa2019-03-18 10:19:51 +09002258 script := "build/soong/scripts/gen-java-current-api-files.sh"
2259 p := android.ExistentPathForSource(mctx, script)
2260
2261 if !p.Valid() {
2262 panic(fmt.Sprintf("script file %s doesn't exist", script))
2263 }
2264
2265 mctx.ModuleErrorf("One or more current api files are missing. "+
2266 "You can update them by:\n"+
Paul Duffin37e0b772019-12-30 17:20:10 +00002267 "%s %q %s && m update-api",
Paul Duffind1b3a922020-01-22 11:57:20 +00002268 script, filepath.Join(mctx.ModuleDir(), apiDir),
Paul Duffin3375e352020-04-28 10:44:03 +01002269 strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
Inseob Kim8098faa2019-03-18 10:19:51 +09002270 return
2271 }
2272
Paul Duffin3375e352020-04-28 10:44:03 +01002273 for _, scope := range generatedScopes {
Paul Duffin15f34ef2020-07-20 18:04:44 +01002274 // Use the stubs source name for legacy reasons.
2275 module.createStubsSourcesAndApi(mctx, scope, module.stubsSourceModuleName(scope), scope.droidstubsArgs)
Paul Duffin0ff08bd2020-04-29 13:30:54 +01002276
Paul Duffind1b3a922020-01-22 11:57:20 +00002277 module.createStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002278 module.createExportableStubsLibrary(mctx, scope)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00002279
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002280 if mctx.Config().BuildFromTextStub() && module.ModuleBuildFromTextStubs() {
2281 module.createApiLibrary(mctx, scope)
Jihoon Kang0c705a42023-08-02 06:44:57 +00002282 }
Jihoon Kangb0f4c022024-08-06 00:15:25 +00002283 module.createTopLevelStubsLibrary(mctx, scope)
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00002284 module.createTopLevelExportableStubsLibrary(mctx, scope)
Inseob Kimc0907f12019-02-08 21:00:45 +09002285 }
2286
Paul Duffindfa131e2020-05-15 20:37:11 +01002287 if module.requiresRuntimeImplementationLibrary() {
Paul Duffin5df79302020-05-16 15:52:12 +01002288 // Create child module to create an implementation library.
2289 //
2290 // This temporarily creates a second implementation library that can be explicitly
2291 // referenced.
2292 //
2293 // TODO(b/156618935) - update comment once only one implementation library is created.
2294 module.createImplLibrary(mctx)
2295
Paul Duffindfa131e2020-05-15 20:37:11 +01002296 // Only create an XML permissions file that declares the library as being usable
2297 // as a shared library if required.
2298 if module.sharedLibrary() {
2299 module.createXmlFile(mctx)
2300 }
Paul Duffin43db9be2019-12-30 17:35:49 +00002301
2302 // record java_sdk_library modules so that they are exported to make
2303 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2304 javaSdkLibrariesLock.Lock()
2305 defer javaSdkLibrariesLock.Unlock()
2306 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2307 }
Anton Hansson7f66efa2020-10-08 14:47:23 +01002308
Paul Duffin77590a82022-04-28 14:13:30 +00002309 // 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 +01002310 module.properties.Libs = append(module.properties.Libs, module.sdkLibraryProperties.Impl_only_libs...)
Cole Faustb7493472024-08-28 11:55:52 -07002311 module.properties.Static_libs.AppendSimpleValue(module.sdkLibraryProperties.Impl_only_static_libs)
Inseob Kimc0907f12019-02-08 21:00:45 +09002312}
2313
2314func (module *SdkLibrary) InitSdkLibraryProperties() {
Colin Crossce6734e2020-06-15 16:09:53 -07002315 module.addHostAndDeviceProperties()
2316 module.AddProperties(&module.sdkLibraryProperties)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002317
Paul Duffin71b33cc2021-06-23 11:39:47 +01002318 module.initSdkLibraryComponent(module)
Paul Duffin859fe962020-05-15 10:20:31 +01002319
Paul Duffina18abc22020-05-16 18:54:24 +01002320 module.properties.Installable = proptools.BoolPtr(true)
2321 module.deviceProperties.IsSDKLibrary = true
Inseob Kimc0907f12019-02-08 21:00:45 +09002322}
Sundong Ahn054b19a2018-10-19 13:46:09 +09002323
Paul Duffindfa131e2020-05-15 20:37:11 +01002324func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
2325 return !proptools.Bool(module.sdkLibraryProperties.Api_only)
2326}
2327
Jihoon Kangfa3f0782024-08-21 20:42:18 +00002328func moduleStubLinkType(j *Module) (stub bool, ret sdkLinkType) {
2329 kind := android.ToSdkKind(proptools.String(j.properties.Stub_contributing_api))
2330 switch kind {
2331 case android.SdkPublic:
Anton Hansson2d0c1942020-05-25 12:20:51 +01002332 return true, javaSdk
Jihoon Kangfa3f0782024-08-21 20:42:18 +00002333 case android.SdkSystem:
Anton Hansson2d0c1942020-05-25 12:20:51 +01002334 return true, javaSystem
Jihoon Kangfa3f0782024-08-21 20:42:18 +00002335 case android.SdkModule:
Anton Hansson2d0c1942020-05-25 12:20:51 +01002336 return true, javaModule
Jihoon Kangfa3f0782024-08-21 20:42:18 +00002337 case android.SdkTest:
Anton Hansson2d0c1942020-05-25 12:20:51 +01002338 return true, javaSystem
Jihoon Kangfa3f0782024-08-21 20:42:18 +00002339 case android.SdkSystemServer:
Jihoon Kang1147b312023-06-08 23:25:57 +00002340 return true, javaSystemServer
Jihoon Kangfa3f0782024-08-21 20:42:18 +00002341 // Default value for all modules other than java_sdk_library-generated stub submodules
2342 case android.SdkInvalid:
2343 return false, javaPlatform
2344 default:
2345 panic(fmt.Sprintf("stub_contributing_api set as an unsupported sdk kind %s", kind.String()))
Jihoon Kang1147b312023-06-08 23:25:57 +00002346 }
Anton Hansson2d0c1942020-05-25 12:20:51 +01002347}
2348
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002349// java_sdk_library is a special Java library that provides optional platform APIs to apps.
2350// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
2351// are linked against to, 2) droiddoc module that internally generates API stubs source files,
2352// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
2353// the runtime lib to the classpath at runtime if requested via <uses-library>.
Inseob Kimc0907f12019-02-08 21:00:45 +09002354func SdkLibraryFactory() android.Module {
2355 module := &SdkLibrary{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002356
2357 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002358 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002359
Inseob Kimc0907f12019-02-08 21:00:45 +09002360 module.InitSdkLibraryProperties()
Jooyung Han58f26ab2019-12-18 15:34:32 +09002361 android.InitApexModule(module)
Sundong Ahn054b19a2018-10-19 13:46:09 +09002362 InitJavaModule(module, android.HostAndDeviceSupported)
Paul Duffin3375e352020-04-28 10:44:03 +01002363
2364 // Initialize the map from scope to scope specific properties.
2365 scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002366 for _, scope := range AllApiScopes {
Paul Duffin3375e352020-04-28 10:44:03 +01002367 scopeToProperties[scope] = scope.scopeSpecificProperties(module)
2368 }
2369 module.scopeToProperties = scopeToProperties
2370
Paul Duffin4911a892020-04-29 23:35:13 +01002371 // Add the properties containing visibility rules so that they are checked.
Paul Duffin5df79302020-05-16 15:52:12 +01002372 android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
Paul Duffin4911a892020-04-29 23:35:13 +01002373 android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
2374 android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
2375
Paul Duffin1b1e8062020-05-08 13:44:43 +01002376 module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
Paul Duffindfa131e2020-05-15 20:37:11 +01002377 // If no implementation is required then it cannot be used as a shared library
2378 // either.
2379 if !module.requiresRuntimeImplementationLibrary() {
2380 // If shared_library has been explicitly set to true then it is incompatible
2381 // with api_only: true.
2382 if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
2383 ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
2384 }
2385 // Set shared_library: false.
2386 module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
2387 }
2388
Paul Duffin1b1e8062020-05-08 13:44:43 +01002389 if module.initCommonAfterDefaultsApplied(ctx) {
2390 module.CreateInternalModules(ctx)
2391 }
2392 })
Jiyong Parkc678ad32018-04-10 13:07:10 +09002393 return module
2394}
Colin Cross79c7c262019-04-17 11:11:46 -07002395
2396//
2397// SDK library prebuilts
2398//
2399
Paul Duffin56d44902020-01-31 13:36:25 +00002400// Properties associated with each api scope.
2401type sdkLibraryScopeProperties struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002402 Jars []string `android:"path"`
2403
2404 Sdk_version *string
2405
Colin Cross79c7c262019-04-17 11:11:46 -07002406 // List of shared java libs that this module has dependencies to
2407 Libs []string
Paul Duffin3d1248c2020-04-09 00:10:17 +01002408
Paul Duffinc8782502020-04-29 20:45:27 +01002409 // The stubs source.
Paul Duffin3d1248c2020-04-09 00:10:17 +01002410 Stub_srcs []string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002411
2412 // The current.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002413 Current_api *string `android:"path"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01002414
2415 // The removed.txt
Paul Duffin0f8faff2020-05-20 16:18:00 +01002416 Removed_api *string `android:"path"`
Anton Hanssond78eb762021-09-21 15:25:12 +01002417
2418 // Annotation zip
2419 Annotations *string `android:"path"`
Colin Cross79c7c262019-04-17 11:11:46 -07002420}
2421
Paul Duffin56d44902020-01-31 13:36:25 +00002422type sdkLibraryImportProperties struct {
Paul Duffinfcfd7912020-01-31 17:54:30 +00002423 // List of shared java libs, common to all scopes, that this module has
2424 // dependencies to
2425 Libs []string
Paul Duffin1267d872021-04-16 17:21:36 +01002426
2427 // If set to true, compile dex files for the stubs. Defaults to false.
2428 Compile_dex *bool
Paul Duffin869de142021-07-15 14:14:41 +01002429
2430 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01002431 Permitted_packages []string
Spandan Das23956d12024-01-19 00:22:22 +00002432
2433 // Name of the source soong module that gets shadowed by this prebuilt
2434 // If unspecified, follows the naming convention that the source module of
2435 // the prebuilt is Name() without "prebuilt_" prefix
2436 Source_module_name *string
Paul Duffin56d44902020-01-31 13:36:25 +00002437}
2438
Paul Duffineedc5d52020-06-12 17:46:39 +01002439type SdkLibraryImport struct {
Colin Cross79c7c262019-04-17 11:11:46 -07002440 android.ModuleBase
2441 android.DefaultableModuleBase
2442 prebuilt android.Prebuilt
Paul Duffindd46f712020-02-10 13:37:10 +00002443 android.ApexModuleBase
Colin Cross79c7c262019-04-17 11:11:46 -07002444
Paul Duffin37856732021-02-26 14:24:15 +00002445 hiddenAPI
Jiakai Zhang204356f2021-09-09 08:12:46 +00002446 dexpreopter
Paul Duffin37856732021-02-26 14:24:15 +00002447
Colin Cross79c7c262019-04-17 11:11:46 -07002448 properties sdkLibraryImportProperties
2449
Paul Duffin46a26a82020-04-07 19:27:04 +01002450 // Map from api scope to the scope specific property structure.
2451 scopeProperties map[*apiScope]*sdkLibraryScopeProperties
2452
Paul Duffin56d44902020-01-31 13:36:25 +00002453 commonToSdkLibraryAndImport
Paul Duffineedc5d52020-06-12 17:46:39 +01002454
Paul Duffineedc5d52020-06-12 17:46:39 +01002455 // The reference to the xml permissions module created by the source module.
2456 // Is nil if the source module does not exist.
2457 xmlPermissionsFileModule *sdkLibraryXml
Paul Duffin39853512021-02-26 11:09:39 +00002458
Jeongik Chad5fe8782021-07-08 01:13:11 +09002459 // Build path to the dex implementation jar obtained from the prebuilt_apex, if any.
Spandan Dasfae468e2023-12-12 23:23:53 +00002460 dexJarFile OptionalDexJarPath
2461 dexJarFileErr error
Jeongik Chad5fe8782021-07-08 01:13:11 +09002462
2463 // Expected install file path of the source module(sdk_library)
2464 // or dex implementation jar obtained from the prebuilt_apex, if any.
2465 installFile android.Path
Colin Cross79c7c262019-04-17 11:11:46 -07002466}
2467
Paul Duffineedc5d52020-06-12 17:46:39 +01002468var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
Colin Cross79c7c262019-04-17 11:11:46 -07002469
Paul Duffin46a26a82020-04-07 19:27:04 +01002470// The type of a structure that contains a field of type sdkLibraryScopeProperties
2471// for each apiscope in allApiScopes, e.g. something like:
Colin Crossd079e0b2022-08-16 10:27:33 -07002472//
2473// struct {
2474// Public sdkLibraryScopeProperties
2475// System sdkLibraryScopeProperties
2476// ...
2477// }
Paul Duffin46a26a82020-04-07 19:27:04 +01002478var allScopeStructType = createAllScopePropertiesStructType()
2479
2480// Dynamically create a structure type for each apiscope in allApiScopes.
2481func createAllScopePropertiesStructType() reflect.Type {
2482 var fields []reflect.StructField
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002483 for _, apiScope := range AllApiScopes {
Paul Duffin46a26a82020-04-07 19:27:04 +01002484 field := reflect.StructField{
2485 Name: apiScope.fieldName,
2486 Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
2487 }
2488 fields = append(fields, field)
2489 }
2490
2491 return reflect.StructOf(fields)
2492}
2493
2494// Create an instance of the scope specific structure type and return a map
2495// from apiscope to a pointer to each scope specific field.
2496func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
2497 allScopePropertiesPtr := reflect.New(allScopeStructType)
2498 allScopePropertiesStruct := allScopePropertiesPtr.Elem()
2499 scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
2500
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00002501 for _, apiScope := range AllApiScopes {
Paul Duffin46a26a82020-04-07 19:27:04 +01002502 field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
2503 scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
2504 }
2505
2506 return allScopePropertiesPtr.Interface(), scopeProperties
2507}
2508
Jaewoong Jung4f158ee2019-07-11 10:05:35 -07002509// java_sdk_library_import imports a prebuilt java_sdk_library.
Colin Cross79c7c262019-04-17 11:11:46 -07002510func sdkLibraryImportFactory() android.Module {
Paul Duffineedc5d52020-06-12 17:46:39 +01002511 module := &SdkLibraryImport{}
Colin Cross79c7c262019-04-17 11:11:46 -07002512
Paul Duffin46a26a82020-04-07 19:27:04 +01002513 allScopeProperties, scopeToProperties := createPropertiesInstance()
2514 module.scopeProperties = scopeToProperties
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08002515 module.AddProperties(&module.properties, allScopeProperties, &module.importDexpreoptProperties)
Colin Cross79c7c262019-04-17 11:11:46 -07002516
Paul Duffinc3091c82020-05-08 14:16:20 +01002517 // Initialize information common between source and prebuilt.
Paul Duffin71b33cc2021-06-23 11:39:47 +01002518 module.initCommon(module)
Paul Duffinc3091c82020-05-08 14:16:20 +01002519
Paul Duffin0bdcb272020-02-06 15:24:57 +00002520 android.InitPrebuiltModule(module, &[]string{""})
Paul Duffindd46f712020-02-10 13:37:10 +00002521 android.InitApexModule(module)
Colin Cross79c7c262019-04-17 11:11:46 -07002522 InitJavaModule(module, android.HostAndDeviceSupported)
2523
Paul Duffin1b1e8062020-05-08 13:44:43 +01002524 module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
2525 if module.initCommonAfterDefaultsApplied(mctx) {
2526 module.createInternalModules(mctx)
2527 }
2528 })
Colin Cross79c7c262019-04-17 11:11:46 -07002529 return module
2530}
2531
Paul Duffin630b11e2021-07-15 13:35:26 +01002532var _ PermittedPackagesForUpdatableBootJars = (*SdkLibraryImport)(nil)
2533
2534func (module *SdkLibraryImport) PermittedPackagesForUpdatableBootJars() []string {
2535 return module.properties.Permitted_packages
2536}
2537
Paul Duffineedc5d52020-06-12 17:46:39 +01002538func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
Colin Cross79c7c262019-04-17 11:11:46 -07002539 return &module.prebuilt
2540}
2541
Paul Duffineedc5d52020-06-12 17:46:39 +01002542func (module *SdkLibraryImport) Name() string {
Colin Cross79c7c262019-04-17 11:11:46 -07002543 return module.prebuilt.Name(module.ModuleBase.Name())
2544}
2545
Spandan Das23956d12024-01-19 00:22:22 +00002546func (module *SdkLibraryImport) BaseModuleName() string {
2547 return proptools.StringDefault(module.properties.Source_module_name, module.ModuleBase.Name())
2548}
2549
Paul Duffineedc5d52020-06-12 17:46:39 +01002550func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
Colin Cross79c7c262019-04-17 11:11:46 -07002551
Paul Duffin50061512020-01-21 16:31:05 +00002552 // If the build is configured to use prebuilts then force this to be preferred.
Jeongik Cha816a23a2020-07-08 01:09:23 +09002553 if mctx.Config().AlwaysUsePrebuiltSdks() {
Paul Duffin50061512020-01-21 16:31:05 +00002554 module.prebuilt.ForcePrefer()
2555 }
2556
Paul Duffin46a26a82020-04-07 19:27:04 +01002557 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002558 if len(scopeProperties.Jars) == 0 {
2559 continue
2560 }
2561
Paul Duffinbbb546b2020-04-09 00:07:11 +01002562 module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
Paul Duffin3d1248c2020-04-09 00:10:17 +01002563
Paul Duffin0f8faff2020-05-20 16:18:00 +01002564 if len(scopeProperties.Stub_srcs) > 0 {
2565 module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
2566 }
Jihoon Kang71c86832023-09-13 01:01:53 +00002567
2568 if scopeProperties.Current_api != nil {
2569 module.createPrebuiltApiContribution(mctx, apiScope, scopeProperties)
2570 }
Paul Duffin56d44902020-01-31 13:36:25 +00002571 }
Colin Cross79c7c262019-04-17 11:11:46 -07002572
2573 javaSdkLibraries := javaSdkLibraries(mctx.Config())
2574 javaSdkLibrariesLock.Lock()
2575 defer javaSdkLibrariesLock.Unlock()
2576 *javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
2577}
2578
Paul Duffineedc5d52020-06-12 17:46:39 +01002579func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffinbbb546b2020-04-09 00:07:11 +01002580 // Creates a java import for the jar with ".stubs" suffix
2581 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002582 Name *string
2583 Source_module_name *string
2584 Created_by_java_sdk_library_name *string
2585 Sdk_version *string
2586 Libs []string
2587 Jars []string
2588 Compile_dex *bool
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002589 Is_stubs_module *bool
Paul Duffinbf4de042022-09-27 12:41:52 +01002590
2591 android.UserSuppliedPrebuiltProperties
Paul Duffinbbb546b2020-04-09 00:07:11 +01002592 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002593 props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002594 props.Source_module_name = proptools.StringPtr(apiScope.stubsLibraryModuleName(module.BaseModuleName()))
2595 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002596 props.Sdk_version = scopeProperties.Sdk_version
2597 // Prepend any of the libs from the legacy public properties to the libs for each of the
2598 // scopes to avoid having to duplicate them in each scope.
2599 props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
2600 props.Jars = scopeProperties.Jars
Paul Duffin1dbe3ca2020-05-16 09:57:59 +01002601
Paul Duffin38b57852020-05-13 16:08:09 +01002602 // The imports are preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002603 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
Paul Duffin859fe962020-05-15 10:20:31 +01002604
Paul Duffin1267d872021-04-16 17:21:36 +01002605 // The imports need to be compiled to dex if the java_sdk_library_import requests it.
Paul Duffinf4600f62021-05-13 22:34:45 +01002606 compileDex := module.properties.Compile_dex
2607 if module.stubLibrariesCompiledForDex() {
2608 compileDex = proptools.BoolPtr(true)
2609 }
2610 props.Compile_dex = compileDex
Jihoon Kangfe914ed2024-02-12 22:49:21 +00002611 props.Is_stubs_module = proptools.BoolPtr(true)
Paul Duffin1267d872021-04-16 17:21:36 +01002612
Paul Duffin859fe962020-05-15 10:20:31 +01002613 mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffinbbb546b2020-04-09 00:07:11 +01002614}
2615
Paul Duffineedc5d52020-06-12 17:46:39 +01002616func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
Paul Duffin3d1248c2020-04-09 00:10:17 +01002617 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002618 Name *string
2619 Source_module_name *string
2620 Created_by_java_sdk_library_name *string
2621 Srcs []string
Paul Duffinbf4de042022-09-27 12:41:52 +01002622
2623 android.UserSuppliedPrebuiltProperties
Paul Duffin3d1248c2020-04-09 00:10:17 +01002624 }{}
Paul Duffinc3091c82020-05-08 14:16:20 +01002625 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
Spandan Das23956d12024-01-19 00:22:22 +00002626 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()))
2627 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002628 props.Srcs = scopeProperties.Stub_srcs
Paul Duffin38b57852020-05-13 16:08:09 +01002629
2630 // The stubs source is preferred if the java_sdk_library_import is preferred.
Paul Duffinbf4de042022-09-27 12:41:52 +01002631 props.CopyUserSuppliedPropertiesFromPrebuilt(&module.prebuilt)
2632
Spandan Das2cc80ba2023-10-27 17:21:52 +00002633 mctx.CreateModule(PrebuiltStubsSourcesFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Paul Duffin3d1248c2020-04-09 00:10:17 +01002634}
2635
Jihoon Kang71c86832023-09-13 01:01:53 +00002636func (module *SdkLibraryImport) createPrebuiltApiContribution(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
2637 api_file := scopeProperties.Current_api
2638 api_surface := &apiScope.name
2639
2640 props := struct {
Spandan Das23956d12024-01-19 00:22:22 +00002641 Name *string
2642 Source_module_name *string
2643 Created_by_java_sdk_library_name *string
2644 Api_surface *string
2645 Api_file *string
2646 Visibility []string
Jihoon Kang71c86832023-09-13 01:01:53 +00002647 }{}
2648
2649 props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope) + ".api.contribution")
Spandan Das23956d12024-01-19 00:22:22 +00002650 props.Source_module_name = proptools.StringPtr(apiScope.stubsSourceModuleName(module.BaseModuleName()) + ".api.contribution")
2651 props.Created_by_java_sdk_library_name = proptools.StringPtr(module.RootLibraryName())
Jihoon Kang71c86832023-09-13 01:01:53 +00002652 props.Api_surface = api_surface
2653 props.Api_file = api_file
2654 props.Visibility = []string{"//visibility:override", "//visibility:public"}
2655
Spandan Das2cc80ba2023-10-27 17:21:52 +00002656 mctx.CreateModule(ApiContributionImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
Jihoon Kang71c86832023-09-13 01:01:53 +00002657}
2658
Paul Duffin44f1d842020-06-26 20:17:02 +01002659// Add the dependencies on the child module in the component deps mutator so that it
2660// creates references to the prebuilt and not the source modules.
2661func (module *SdkLibraryImport) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin46a26a82020-04-07 19:27:04 +01002662 for apiScope, scopeProperties := range module.scopeProperties {
Paul Duffin56d44902020-01-31 13:36:25 +00002663 if len(scopeProperties.Jars) == 0 {
2664 continue
2665 }
2666
2667 // Add dependencies to the prebuilt stubs library
Jihoon Kangb7431552024-01-22 19:40:08 +00002668 ctx.AddVariationDependencies(nil, apiScope.prebuiltStubsTag, android.PrebuiltNameFromSource(module.stubsLibraryModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002669
2670 if len(scopeProperties.Stub_srcs) > 0 {
2671 // Add dependencies to the prebuilt stubs source library
Paul Duffin864116c2021-04-02 10:24:13 +01002672 ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, android.PrebuiltNameFromSource(module.stubsSourceModuleName(apiScope)))
Paul Duffin0f8faff2020-05-20 16:18:00 +01002673 }
Paul Duffin56d44902020-01-31 13:36:25 +00002674 }
Paul Duffin44f1d842020-06-26 20:17:02 +01002675}
2676
2677// Add other dependencies as normal.
2678func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffineedc5d52020-06-12 17:46:39 +01002679
2680 implName := module.implLibraryModuleName()
2681 if ctx.OtherModuleExists(implName) {
2682 ctx.AddVariationDependencies(nil, implLibraryTag, implName)
2683
2684 xmlPermissionsModuleName := module.xmlPermissionsModuleName()
2685 if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
2686 // Add dependency to the rule for generating the xml permissions file
2687 ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
2688 }
2689 }
Colin Cross79c7c262019-04-17 11:11:46 -07002690}
2691
Jiyong Park45bf82e2020-12-15 22:29:02 +09002692var _ android.ApexModule = (*SdkLibraryImport)(nil)
2693
2694// Implements android.ApexModule
Paul Duffineedc5d52020-06-12 17:46:39 +01002695func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
2696 depTag := mctx.OtherModuleDependencyTag(dep)
2697 if depTag == xmlPermissionsFileTag {
2698 return true
2699 }
2700
2701 // None of the other dependencies of the java_sdk_library_import are in the same apex
2702 // as the one that references this module.
2703 return false
2704}
2705
Jiyong Park45bf82e2020-12-15 22:29:02 +09002706// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002707func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2708 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002709 // we don't check prebuilt modules for sdk_version
2710 return nil
2711}
2712
Paul Duffinea8f8082021-06-24 13:25:57 +01002713// Implements android.ApexModule
2714func (module *SdkLibraryImport) UniqueApexVariations() bool {
2715 return module.uniqueApexVariations()
2716}
2717
Paul Duffin09817d62022-04-28 17:45:11 +01002718// MinSdkVersion - Implements hiddenAPIModule
Spandan Das8c9ae7e2023-03-03 21:20:36 +00002719func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel {
2720 return android.NoneApiLevel
Paul Duffin09817d62022-04-28 17:45:11 +01002721}
2722
2723var _ hiddenAPIModule = (*SdkLibraryImport)(nil)
2724
Paul Duffineedc5d52020-06-12 17:46:39 +01002725func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffina2ae7e02020-09-11 11:55:00 +01002726 module.generateCommonBuildActions(ctx)
2727
Jeongik Chad5fe8782021-07-08 01:13:11 +09002728 // Assume that source module(sdk_library) is installed in /<sdk_library partition>/framework
2729 module.installFile = android.PathForModuleInstall(ctx, "framework", module.Stem()+".jar")
2730
Paul Duffin0f8faff2020-05-20 16:18:00 +01002731 // Record the paths to the prebuilt stubs library and stubs source.
Colin Cross79c7c262019-04-17 11:11:46 -07002732 ctx.VisitDirectDeps(func(to android.Module) {
2733 tag := ctx.OtherModuleDependencyTag(to)
2734
Paul Duffin0f8faff2020-05-20 16:18:00 +01002735 // Extract information from any of the scope specific dependencies.
2736 if scopeTag, ok := tag.(scopeDependencyTag); ok {
2737 apiScope := scopeTag.apiScope
2738 scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
2739
2740 // Extract information from the dependency. The exact information extracted
2741 // is determined by the nature of the dependency which is determined by the tag.
2742 scopeTag.extractDepInfo(ctx, to, scopePaths)
Paul Duffineedc5d52020-06-12 17:46:39 +01002743 } else if tag == implLibraryTag {
2744 if implLibrary, ok := to.(*Library); ok {
2745 module.implLibraryModule = implLibrary
2746 } else {
2747 ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
2748 }
2749 } else if tag == xmlPermissionsFileTag {
2750 if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
2751 module.xmlPermissionsFileModule = xmlPermissionsFileModule
2752 } else {
2753 ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
2754 }
Colin Cross79c7c262019-04-17 11:11:46 -07002755 }
2756 })
Paul Duffin0f8faff2020-05-20 16:18:00 +01002757
2758 // Populate the scope paths with information from the properties.
2759 for apiScope, scopeProperties := range module.scopeProperties {
2760 if len(scopeProperties.Jars) == 0 {
2761 continue
2762 }
2763
2764 paths := module.getScopePathsCreateIfNeeded(apiScope)
Anton Hanssond78eb762021-09-21 15:25:12 +01002765 paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
Paul Duffin0f8faff2020-05-20 16:18:00 +01002766 paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
2767 paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
2768 }
Paul Duffin39853512021-02-26 11:09:39 +00002769
2770 if ctx.Device() {
2771 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2772 // obtained from the associated deapexer module.
Colin Crossff694a82023-12-13 15:54:49 -08002773 ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Paul Duffin39853512021-02-26 11:09:39 +00002774 if ai.ForPrebuiltApex {
Paul Duffin39853512021-02-26 11:09:39 +00002775 // Get the path of the dex implementation jar from the `deapexer` module.
Spandan Dasfae468e2023-12-12 23:23:53 +00002776 di, err := android.FindDeapexerProviderForModule(ctx)
2777 if err != nil {
2778 // An error was found, possibly due to multiple apexes in the tree that export this library
2779 // Defer the error till a client tries to call DexJarBuildPath
2780 module.dexJarFileErr = err
Spandan Das3a392012024-01-17 18:26:27 +00002781 module.initHiddenAPIError(err)
Spandan Dasfae468e2023-12-12 23:23:53 +00002782 return
Martin Stjernholm44825602021-09-17 01:44:12 +01002783 }
Spandan Das5be63332023-12-13 00:06:32 +00002784 dexJarFileApexRootRelative := ApexRootRelativePathToJavaLib(module.BaseModuleName())
Jiakai Zhang81e46812023-02-08 21:56:07 +08002785 if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002786 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
2787 module.dexJarFile = dexJarFile
Jiakai Zhang204356f2021-09-09 08:12:46 +00002788 installPath := android.PathForModuleInPartitionInstall(
Jiakai Zhang81e46812023-02-08 21:56:07 +08002789 ctx, "apex", ai.ApexVariationName, dexJarFileApexRootRelative)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002790 module.installFile = installPath
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002791 module.initHiddenAPI(ctx, dexJarFile, module.findScopePaths(apiScopePublic).stubsImplPath[0], nil)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002792
Spandan Dase21a8d42024-01-23 23:56:29 +00002793 module.dexpreopter.installPath = module.dexpreopter.getInstallPath(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), installPath)
Jiakai Zhang204356f2021-09-09 08:12:46 +00002794 module.dexpreopter.isSDKLibrary = true
Spandan Dase21a8d42024-01-23 23:56:29 +00002795 module.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()), &module.dexpreopter)
Jiakai Zhang81e46812023-02-08 21:56:07 +08002796
2797 if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil {
2798 module.dexpreopter.inputProfilePathOnHost = profilePath
2799 }
Paul Duffin39853512021-02-26 11:09:39 +00002800 } else {
2801 // This should never happen as a variant for a prebuilt_apex is only created if the
2802 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01002803 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin39853512021-02-26 11:09:39 +00002804 }
2805 }
2806 }
mrziwang9f7b9f42024-07-10 12:18:06 -07002807
Jihoon Kang28c96572024-09-11 23:44:44 +00002808 var generatingLibs []string
2809 for _, apiScope := range AllApiScopes {
2810 if scopeProperties, ok := module.scopeProperties[apiScope]; ok {
2811 if len(scopeProperties.Jars) == 0 {
2812 continue
2813 }
2814 generatingLibs = append(generatingLibs, module.stubsLibraryModuleName(apiScope))
2815 }
2816 }
2817
mrziwang9f7b9f42024-07-10 12:18:06 -07002818 module.setOutputFiles(ctx)
2819 if module.implLibraryModule != nil {
Jihoon Kang28c96572024-09-11 23:44:44 +00002820 generatingLibs = append(generatingLibs, module.implLibraryModuleName())
mrziwang9f7b9f42024-07-10 12:18:06 -07002821 setOutputFiles(ctx, module.implLibraryModule.Module)
2822 }
Jihoon Kang28c96572024-09-11 23:44:44 +00002823
2824 android.SetProvider(ctx, SdkLibraryInfoProvider, SdkLibraryInfo{
2825 GeneratingLibs: generatingLibs,
2826 })
Colin Cross79c7c262019-04-17 11:11:46 -07002827}
2828
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002829// to satisfy UsesLibraryDependency interface
Spandan Das59a4a2b2024-01-09 21:35:56 +00002830func (module *SdkLibraryImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {
Paul Duffin39853512021-02-26 11:09:39 +00002831 // The dex implementation jar extracted from the .apex file should be used in preference to the
2832 // source.
Spandan Dasfae468e2023-12-12 23:23:53 +00002833 if module.dexJarFileErr != nil {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002834 ctx.ModuleErrorf(module.dexJarFileErr.Error())
Spandan Dasfae468e2023-12-12 23:23:53 +00002835 }
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002836 if module.dexJarFile.IsSet() {
Paul Duffin39853512021-02-26 11:09:39 +00002837 return module.dexJarFile
2838 }
Paul Duffineedc5d52020-06-12 17:46:39 +01002839 if module.implLibraryModule == nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01002840 return makeUnsetDexJarPath()
Paul Duffineedc5d52020-06-12 17:46:39 +01002841 } else {
Spandan Das59a4a2b2024-01-09 21:35:56 +00002842 return module.implLibraryModule.DexJarBuildPath(ctx)
Paul Duffineedc5d52020-06-12 17:46:39 +01002843 }
2844}
2845
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002846// to satisfy UsesLibraryDependency interface
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002847func (module *SdkLibraryImport) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09002848 return module.installFile
Ulya Trafimovich31e444e2020-08-14 17:32:16 +01002849}
2850
Ulya Trafimovichdbf31662020-12-17 12:07:54 +00002851// to satisfy UsesLibraryDependency interface
2852func (module *SdkLibraryImport) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2853 return nil
2854}
2855
Paul Duffineedc5d52020-06-12 17:46:39 +01002856// to satisfy apex.javaDependency interface
2857func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2858 if module.implLibraryModule == nil {
2859 return nil
2860 } else {
2861 return module.implLibraryModule.JacocoReportClassesFile()
2862 }
2863}
2864
2865// to satisfy apex.javaDependency interface
Colin Cross08dca382020-07-21 20:31:17 -07002866func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2867 if module.implLibraryModule == nil {
2868 return LintDepSets{}
2869 } else {
2870 return module.implLibraryModule.LintDepSets()
2871 }
2872}
2873
Spandan Das17854f52022-01-14 21:19:14 +00002874func (module *SdkLibraryImport) GetStrictUpdatabilityLinting() bool {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002875 if module.implLibraryModule == nil {
2876 return false
2877 } else {
Spandan Das17854f52022-01-14 21:19:14 +00002878 return module.implLibraryModule.GetStrictUpdatabilityLinting()
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002879 }
2880}
2881
Spandan Das17854f52022-01-14 21:19:14 +00002882func (module *SdkLibraryImport) SetStrictUpdatabilityLinting(strictLinting bool) {
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002883 if module.implLibraryModule != nil {
Spandan Das17854f52022-01-14 21:19:14 +00002884 module.implLibraryModule.SetStrictUpdatabilityLinting(strictLinting)
Jaewoong Jung476b9d62021-05-10 15:30:00 -07002885 }
2886}
2887
Colin Cross08dca382020-07-21 20:31:17 -07002888// to satisfy apex.javaDependency interface
Paul Duffineedc5d52020-06-12 17:46:39 +01002889func (module *SdkLibraryImport) Stem() string {
2890 return module.BaseModuleName()
Colin Cross79c7c262019-04-17 11:11:46 -07002891}
Jiyong Parke3833882020-02-17 17:28:10 +09002892
Paul Duffin44b481b2020-06-17 16:59:43 +01002893var _ ApexDependency = (*SdkLibraryImport)(nil)
2894
2895// to satisfy java.ApexDependency interface
2896func (module *SdkLibraryImport) HeaderJars() android.Paths {
2897 if module.implLibraryModule == nil {
2898 return nil
2899 } else {
2900 return module.implLibraryModule.HeaderJars()
2901 }
2902}
2903
2904// to satisfy java.ApexDependency interface
2905func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2906 if module.implLibraryModule == nil {
2907 return nil
2908 } else {
2909 return module.implLibraryModule.ImplementationAndResourcesJars()
2910 }
2911}
2912
Jiakai Zhang204356f2021-09-09 08:12:46 +00002913// to satisfy java.DexpreopterInterface interface
2914func (module *SdkLibraryImport) IsInstallable() bool {
2915 return true
2916}
2917
Paul Duffinfef55002021-06-17 14:56:05 +01002918var _ android.RequiredFilesFromPrebuiltApex = (*SdkLibraryImport)(nil)
2919
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01002920func (module *SdkLibraryImport) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01002921 name := module.BaseModuleName()
Jiakai Zhang81e46812023-02-08 21:56:07 +08002922 return requiredFilesFromPrebuiltApexForImport(name, &module.dexpreopter)
Paul Duffinfef55002021-06-17 14:56:05 +01002923}
2924
Spandan Das2ea84dd2024-01-25 22:12:50 +00002925func (j *SdkLibraryImport) UseProfileGuidedDexpreopt() bool {
2926 return proptools.Bool(j.importDexpreoptProperties.Dex_preopt.Profile_guided)
2927}
2928
Jiyong Parke3833882020-02-17 17:28:10 +09002929// java_sdk_library_xml
Jiyong Parke3833882020-02-17 17:28:10 +09002930type sdkLibraryXml struct {
2931 android.ModuleBase
2932 android.DefaultableModuleBase
2933 android.ApexModuleBase
2934
2935 properties sdkLibraryXmlProperties
2936
2937 outputFilePath android.OutputPath
2938 installDirPath android.InstallPath
Colin Cross56a83212020-09-15 18:30:11 -07002939
2940 hideApexVariantFromMake bool
Jiyong Parke3833882020-02-17 17:28:10 +09002941}
2942
2943type sdkLibraryXmlProperties struct {
2944 // canonical name of the lib
2945 Lib_name *string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00002946
2947 // Signals that this shared library is part of the bootclasspath starting
2948 // on the version indicated in this attribute.
2949 //
2950 // This will make platforms at this level and above to ignore
2951 // <uses-library> tags with this library name because the library is already
2952 // available
2953 On_bootclasspath_since *string
2954
2955 // Signals that this shared library was part of the bootclasspath before
2956 // (but not including) the version indicated in this attribute.
2957 //
2958 // The system will automatically add a <uses-library> tag with this library to
2959 // apps that target any SDK less than the version indicated in this attribute.
2960 On_bootclasspath_before *string
2961
2962 // Indicates that PackageManager should ignore this shared library if the
2963 // platform is below the version indicated in this attribute.
2964 //
2965 // This means that the device won't recognise this library as installed.
2966 Min_device_sdk *string
2967
2968 // Indicates that PackageManager should ignore this shared library if the
2969 // platform is above the version indicated in this attribute.
2970 //
2971 // This means that the device won't recognise this library as installed.
2972 Max_device_sdk *string
Pedro Loureiroc3621422021-09-28 15:40:23 +00002973
2974 // The SdkLibrary's min api level as a string
2975 //
2976 // This value comes from the ApiLevel of the MinSdkVersion property.
2977 Sdk_library_min_api_level *string
Jamie Garsidee570ace2023-11-27 12:07:36 +00002978
2979 // Uses-libs dependencies that the shared library requires to work correctly.
2980 //
2981 // This will add dependency="foo:bar" to the <library> section.
2982 Uses_libs_dependencies []string
Jiyong Parke3833882020-02-17 17:28:10 +09002983}
2984
2985// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2986// Not to be used directly by users. java_sdk_library internally uses this.
2987func sdkLibraryXmlFactory() android.Module {
2988 module := &sdkLibraryXml{}
2989
2990 module.AddProperties(&module.properties)
2991
2992 android.InitApexModule(module)
2993 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2994
2995 return module
2996}
2997
Colin Crossaede88c2020-08-11 12:17:01 -07002998func (module *sdkLibraryXml) UniqueApexVariations() bool {
2999 // sdkLibraryXml needs a unique variation per APEX because the generated XML file contains the path to the
3000 // mounted APEX, which contains the name of the APEX.
3001 return true
3002}
3003
Jiyong Parke3833882020-02-17 17:28:10 +09003004// from android.PrebuiltEtcModule
Jooyung Han0703fd82020-08-26 22:11:53 +09003005func (module *sdkLibraryXml) BaseDir() string {
3006 return "etc"
3007}
3008
3009// from android.PrebuiltEtcModule
Jiyong Parke3833882020-02-17 17:28:10 +09003010func (module *sdkLibraryXml) SubDir() string {
3011 return "permissions"
3012}
3013
ThiƩbaud Weksteen00e8b312024-03-18 14:06:00 +11003014var _ etc.PrebuiltEtcModule = (*sdkLibraryXml)(nil)
3015
Jiyong Parke3833882020-02-17 17:28:10 +09003016// from android.ApexModule
3017func (module *sdkLibraryXml) AvailableFor(what string) bool {
3018 return true
3019}
3020
3021func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
3022 // do nothing
3023}
3024
Jiyong Park45bf82e2020-12-15 22:29:02 +09003025var _ android.ApexModule = (*sdkLibraryXml)(nil)
3026
3027// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003028func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3029 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003030 // sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
3031 return nil
3032}
3033
Jiyong Parke3833882020-02-17 17:28:10 +09003034// File path to the runtime implementation library
Colin Cross56a83212020-09-15 18:30:11 -07003035func (module *sdkLibraryXml) implPath(ctx android.ModuleContext) string {
Jiyong Parke3833882020-02-17 17:28:10 +09003036 implName := proptools.String(module.properties.Lib_name)
Colin Crossff694a82023-12-13 15:54:49 -08003037 if apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider); !apexInfo.IsForPlatform() {
Colin Crosse07f2312020-08-13 11:24:56 -07003038 // TODO(b/146468504): ApexVariationName() is only a soong module name, not apex name.
Jiyong Parke3833882020-02-17 17:28:10 +09003039 // In most cases, this works fine. But when apex_name is set or override_apex is used
3040 // this can be wrong.
Spandan Das33bbeb22024-06-18 23:28:25 +00003041 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexInfo.BaseApexName, implName)
Jiyong Parke3833882020-02-17 17:28:10 +09003042 }
3043 partition := "system"
3044 if module.SocSpecific() {
3045 partition = "vendor"
3046 } else if module.DeviceSpecific() {
3047 partition = "odm"
3048 } else if module.ProductSpecific() {
3049 partition = "product"
3050 } else if module.SystemExtSpecific() {
3051 partition = "system_ext"
3052 }
3053 return "/" + partition + "/framework/" + implName + ".jar"
3054}
3055
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003056func formattedOptionalSdkLevelAttribute(ctx android.ModuleContext, attrName string, value *string) string {
3057 if value == nil {
3058 return ""
3059 }
3060 apiLevel, err := android.ApiLevelFromUser(ctx, *value)
3061 if err != nil {
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003062 // attributes in bp files have underscores but in the xml have dashes.
3063 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"), err.Error())
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003064 return ""
3065 }
Pedro Loureirob638c622021-12-22 15:28:05 +00003066 if apiLevel.IsCurrent() {
3067 // passing "current" would always mean a future release, never the current (or the current in
3068 // progress) which means some conditions would never be triggered.
3069 ctx.PropertyErrorf(strings.ReplaceAll(attrName, "-", "_"),
3070 `"current" is not an allowed value for this attribute`)
3071 return ""
3072 }
Pedro Loureiro48991222022-06-17 20:01:21 +00003073 // "safeValue" is safe because it translates finalized codenames to a string
3074 // with their SDK int.
3075 safeValue := apiLevel.String()
3076 return formattedOptionalAttribute(attrName, &safeValue)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003077}
3078
3079// formats an attribute for the xml permissions file if the value is not null
3080// returns empty string otherwise
3081func formattedOptionalAttribute(attrName string, value *string) string {
3082 if value == nil {
3083 return ""
3084 }
Paul Duffin1816cde2024-04-10 10:58:21 +01003085 return fmt.Sprintf(" %s=\"%s\"\n", attrName, *value)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003086}
3087
Jamie Garsidee570ace2023-11-27 12:07:36 +00003088func formattedDependenciesAttribute(dependencies []string) string {
3089 if dependencies == nil {
3090 return ""
3091 }
Paul Duffin1816cde2024-04-10 10:58:21 +01003092 return fmt.Sprintf(" dependency=\"%s\"\n", strings.Join(dependencies, ":"))
Jamie Garsidee570ace2023-11-27 12:07:36 +00003093}
3094
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003095func (module *sdkLibraryXml) permissionsContents(ctx android.ModuleContext) string {
3096 libName := proptools.String(module.properties.Lib_name)
3097 libNameAttr := formattedOptionalAttribute("name", &libName)
3098 filePath := module.implPath(ctx)
3099 filePathAttr := formattedOptionalAttribute("file", &filePath)
Pedro Loureiroba6682f2021-10-29 09:32:32 +00003100 implicitFromAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-since", module.properties.On_bootclasspath_since)
3101 implicitUntilAttr := formattedOptionalSdkLevelAttribute(ctx, "on-bootclasspath-before", module.properties.On_bootclasspath_before)
3102 minSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "min-device-sdk", module.properties.Min_device_sdk)
3103 maxSdkAttr := formattedOptionalSdkLevelAttribute(ctx, "max-device-sdk", module.properties.Max_device_sdk)
Jamie Garsidee570ace2023-11-27 12:07:36 +00003104 dependenciesAttr := formattedDependenciesAttribute(module.properties.Uses_libs_dependencies)
Pedro Loureiro196d3e62021-12-22 19:53:01 +00003105 // <library> is understood in all android versions whereas <apex-library> is only understood from API T (and ignored before that).
3106 // 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 +00003107 var libraryTag string
3108 if module.properties.Min_device_sdk != nil {
Paul Duffin1816cde2024-04-10 10:58:21 +01003109 libraryTag = " <apex-library\n"
Pedro Loureiroc3621422021-09-28 15:40:23 +00003110 } else {
Paul Duffin1816cde2024-04-10 10:58:21 +01003111 libraryTag = " <library\n"
Pedro Loureiroc3621422021-09-28 15:40:23 +00003112 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003113
3114 return strings.Join([]string{
Paul Duffin1816cde2024-04-10 10:58:21 +01003115 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
3116 "<!-- Copyright (C) 2018 The Android Open Source Project\n",
3117 "\n",
3118 " Licensed under the Apache License, Version 2.0 (the \"License\");\n",
3119 " you may not use this file except in compliance with the License.\n",
3120 " You may obtain a copy of the License at\n",
3121 "\n",
3122 " http://www.apache.org/licenses/LICENSE-2.0\n",
3123 "\n",
3124 " Unless required by applicable law or agreed to in writing, software\n",
3125 " distributed under the License is distributed on an \"AS IS\" BASIS,\n",
3126 " WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
3127 " See the License for the specific language governing permissions and\n",
3128 " limitations under the License.\n",
3129 "-->\n",
3130 "<permissions>\n",
Pedro Loureiroc3621422021-09-28 15:40:23 +00003131 libraryTag,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003132 libNameAttr,
3133 filePathAttr,
3134 implicitFromAttr,
3135 implicitUntilAttr,
3136 minSdkAttr,
3137 maxSdkAttr,
Jamie Garsidee570ace2023-11-27 12:07:36 +00003138 dependenciesAttr,
Paul Duffin1816cde2024-04-10 10:58:21 +01003139 " />\n",
3140 "</permissions>\n",
3141 }, "")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003142}
3143
Jiyong Parke3833882020-02-17 17:28:10 +09003144func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossff694a82023-12-13 15:54:49 -08003145 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
3146 module.hideApexVariantFromMake = !apexInfo.IsForPlatform()
Colin Cross56a83212020-09-15 18:30:11 -07003147
Jiyong Parke3833882020-02-17 17:28:10 +09003148 libName := proptools.String(module.properties.Lib_name)
Pedro Loureiroc3621422021-09-28 15:40:23 +00003149 module.selfValidate(ctx)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003150 xmlContent := module.permissionsContents(ctx)
Jiyong Parke3833882020-02-17 17:28:10 +09003151
3152 module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
Paul Duffin1816cde2024-04-10 10:58:21 +01003153 android.WriteFileRuleVerbatim(ctx, module.outputFilePath, xmlContent)
Jiyong Parke3833882020-02-17 17:28:10 +09003154
3155 module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
Jeongik Cha00e09912024-04-23 05:07:13 +09003156 ctx.PackageFile(module.installDirPath, libName+".xml", module.outputFilePath)
mrziwange2346b82024-06-10 15:09:45 -07003157
3158 ctx.SetOutputFiles(android.OutputPaths{module.outputFilePath}.Paths(), "")
Jiyong Parke3833882020-02-17 17:28:10 +09003159}
3160
3161func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
Colin Cross56a83212020-09-15 18:30:11 -07003162 if module.hideApexVariantFromMake {
satayev8f088b02021-12-06 11:40:46 +00003163 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003164 Disabled: true,
3165 }}
3166 }
3167
satayev8f088b02021-12-06 11:40:46 +00003168 return []android.AndroidMkEntries{{
Jiyong Parke3833882020-02-17 17:28:10 +09003169 Class: "ETC",
3170 OutputFile: android.OptionalPathForPath(module.outputFilePath),
3171 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07003172 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Parke3833882020-02-17 17:28:10 +09003173 entries.SetString("LOCAL_MODULE_TAGS", "optional")
Colin Crossc68db4b2021-11-11 18:59:15 -08003174 entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.String())
Jiyong Parke3833882020-02-17 17:28:10 +09003175 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
3176 },
3177 },
3178 }}
3179}
Paul Duffindd46f712020-02-10 13:37:10 +00003180
Pedro Loureiroc3621422021-09-28 15:40:23 +00003181func (module *sdkLibraryXml) selfValidate(ctx android.ModuleContext) {
3182 module.validateAtLeastTAttributes(ctx)
3183 module.validateMinAndMaxDeviceSdk(ctx)
3184 module.validateMinMaxDeviceSdkAndModuleMinSdk(ctx)
3185 module.validateOnBootclasspathBeforeRequirements(ctx)
3186}
3187
3188func (module *sdkLibraryXml) validateAtLeastTAttributes(ctx android.ModuleContext) {
3189 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3190 module.attrAtLeastT(ctx, t, module.properties.Min_device_sdk, "min_device_sdk")
3191 module.attrAtLeastT(ctx, t, module.properties.Max_device_sdk, "max_device_sdk")
3192 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_before, "on_bootclasspath_before")
3193 module.attrAtLeastT(ctx, t, module.properties.On_bootclasspath_since, "on_bootclasspath_since")
3194}
3195
3196func (module *sdkLibraryXml) attrAtLeastT(ctx android.ModuleContext, t android.ApiLevel, attr *string, attrName string) {
3197 if attr != nil {
3198 if level, err := android.ApiLevelFromUser(ctx, *attr); err == nil {
3199 // we will inform the user of invalid inputs when we try to write the
3200 // permissions xml file so we don't need to do it here
3201 if t.GreaterThan(level) {
3202 ctx.PropertyErrorf(attrName, "Attribute value needs to be at least T")
3203 }
3204 }
3205 }
3206}
3207
3208func (module *sdkLibraryXml) validateMinAndMaxDeviceSdk(ctx android.ModuleContext) {
3209 if module.properties.Min_device_sdk != nil && module.properties.Max_device_sdk != nil {
3210 min, minErr := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3211 max, maxErr := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3212 if minErr == nil && maxErr == nil {
3213 // we will inform the user of invalid inputs when we try to write the
3214 // permissions xml file so we don't need to do it here
3215 if min.GreaterThan(max) {
3216 ctx.ModuleErrorf("min_device_sdk can't be greater than max_device_sdk")
3217 }
3218 }
3219 }
3220}
3221
3222func (module *sdkLibraryXml) validateMinMaxDeviceSdkAndModuleMinSdk(ctx android.ModuleContext) {
3223 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3224 if module.properties.Min_device_sdk != nil {
3225 api, err := android.ApiLevelFromUser(ctx, *module.properties.Min_device_sdk)
3226 if err == nil {
3227 if moduleMinApi.GreaterThan(api) {
3228 ctx.PropertyErrorf("min_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3229 }
3230 }
3231 }
3232 if module.properties.Max_device_sdk != nil {
3233 api, err := android.ApiLevelFromUser(ctx, *module.properties.Max_device_sdk)
3234 if err == nil {
3235 if moduleMinApi.GreaterThan(api) {
3236 ctx.PropertyErrorf("max_device_sdk", "Can't be less than module's min sdk (%s)", moduleMinApi)
3237 }
3238 }
3239 }
3240}
3241
3242func (module *sdkLibraryXml) validateOnBootclasspathBeforeRequirements(ctx android.ModuleContext) {
3243 moduleMinApi := android.ApiLevelOrPanic(ctx, *module.properties.Sdk_library_min_api_level)
3244 if module.properties.On_bootclasspath_before != nil {
3245 t := android.ApiLevelOrPanic(ctx, "Tiramisu")
3246 // if we use the attribute, then we need to do this validation
3247 if moduleMinApi.LessThan(t) {
3248 // if minAPi is < T, then we need to have min_device_sdk (which only accepts T+)
3249 if module.properties.Min_device_sdk == nil {
3250 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")
3251 }
3252 }
3253 }
3254}
3255
Paul Duffindd46f712020-02-10 13:37:10 +00003256type sdkLibrarySdkMemberType struct {
3257 android.SdkMemberTypeBase
3258}
3259
Paul Duffin296701e2021-07-14 10:29:36 +01003260func (s *sdkLibrarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
3261 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffindd46f712020-02-10 13:37:10 +00003262}
3263
3264func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
3265 _, ok := module.(*SdkLibrary)
3266 return ok
3267}
3268
3269func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
3270 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
3271}
3272
3273func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
3274 return &sdkLibrarySdkMemberProperties{}
3275}
3276
Paul Duffin976b0e52021-04-27 23:20:26 +01003277var javaSdkLibrarySdkMemberType = &sdkLibrarySdkMemberType{
3278 android.SdkMemberTypeBase{
3279 PropertyName: "java_sdk_libs",
3280 SupportsSdk: true,
3281 },
3282}
3283
Paul Duffindd46f712020-02-10 13:37:10 +00003284type sdkLibrarySdkMemberProperties struct {
3285 android.SdkMemberPropertiesBase
3286
Paul Duffine8409952022-09-22 16:24:46 +01003287 // Stem name for files in the sdk snapshot.
3288 //
3289 // This is used to construct the path names of various sdk library files in the sdk snapshot to
3290 // make sure that they match the finalized versions of those files in prebuilts/sdk.
3291 //
3292 // This property is marked as keep so that it will be kept in all instances of this struct, will
3293 // not be cleared but will be copied to common structs. That is needed because this field is used
3294 // to construct many file names for other parts of this struct and so it needs to be present in
3295 // all structs. If it was not marked as keep then it would be cleared in some structs and so would
3296 // be unavailable for generating file names if there were other properties that were still set.
3297 Stem string `sdk:"keep"`
3298
Paul Duffindd46f712020-02-10 13:37:10 +00003299 // Scope to per scope properties.
Paul Duffin106a3a42022-01-27 16:39:06 +00003300 Scopes map[*apiScope]*scopeProperties
Paul Duffindd46f712020-02-10 13:37:10 +00003301
Paul Duffin3d1248c2020-04-09 00:10:17 +01003302 // The Java stubs source files.
3303 Stub_srcs []string
Paul Duffinf7a64332020-05-13 16:54:55 +01003304
3305 // The naming scheme.
3306 Naming_scheme *string
Paul Duffind7eb1c22020-05-26 20:57:10 +01003307
3308 // True if the java_sdk_library_import is for a shared library, false
3309 // otherwise.
3310 Shared_library *bool
Paul Duffina2ae7e02020-09-11 11:55:00 +01003311
Paul Duffin1267d872021-04-16 17:21:36 +01003312 // True if the stub imports should produce dex jars.
3313 Compile_dex *bool
3314
Paul Duffina2ae7e02020-09-11 11:55:00 +01003315 // The paths to the doctag files to add to the prebuilt.
3316 Doctag_paths android.Paths
Paul Duffin869de142021-07-15 14:14:41 +01003317
3318 Permitted_packages []string
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003319
3320 // Signals that this shared library is part of the bootclasspath starting
3321 // on the version indicated in this attribute.
3322 //
3323 // This will make platforms at this level and above to ignore
3324 // <uses-library> tags with this library name because the library is already
3325 // available
3326 On_bootclasspath_since *string
3327
3328 // Signals that this shared library was part of the bootclasspath before
3329 // (but not including) the version indicated in this attribute.
3330 //
3331 // The system will automatically add a <uses-library> tag with this library to
3332 // apps that target any SDK less than the version indicated in this attribute.
3333 On_bootclasspath_before *string
3334
3335 // Indicates that PackageManager should ignore this shared library if the
3336 // platform is below the version indicated in this attribute.
3337 //
3338 // This means that the device won't recognise this library as installed.
3339 Min_device_sdk *string
3340
3341 // Indicates that PackageManager should ignore this shared library if the
3342 // platform is above the version indicated in this attribute.
3343 //
3344 // This means that the device won't recognise this library as installed.
3345 Max_device_sdk *string
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003346
3347 DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"`
Paul Duffindd46f712020-02-10 13:37:10 +00003348}
3349
3350type scopeProperties struct {
Paul Duffin1fd005d2020-04-09 01:08:11 +01003351 Jars android.Paths
3352 StubsSrcJar android.Path
3353 CurrentApiFile android.Path
3354 RemovedApiFile android.Path
Paul Duffine7babdb2022-02-10 13:06:54 +00003355 AnnotationsZip android.Path `supported_build_releases:"Tiramisu+"`
Paul Duffin1fd005d2020-04-09 01:08:11 +01003356 SdkVersion string
Paul Duffindd46f712020-02-10 13:37:10 +00003357}
3358
3359func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
3360 sdk := variant.(*SdkLibrary)
3361
Paul Duffine8409952022-09-22 16:24:46 +01003362 // Copy the stem name for files in the sdk snapshot.
3363 s.Stem = sdk.distStem()
3364
Paul Duffin106a3a42022-01-27 16:39:06 +00003365 s.Scopes = make(map[*apiScope]*scopeProperties)
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00003366 for _, apiScope := range AllApiScopes {
Paul Duffin803a9562020-05-20 11:52:25 +01003367 paths := sdk.findScopePaths(apiScope)
3368 if paths == nil {
3369 continue
3370 }
3371
Paul Duffindd46f712020-02-10 13:37:10 +00003372 jars := paths.stubsImplPath
3373 if len(jars) > 0 {
3374 properties := scopeProperties{}
3375 properties.Jars = jars
Paul Duffin780c5f42020-05-12 15:52:55 +01003376 properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
Paul Duffin0f8faff2020-05-20 16:18:00 +01003377 properties.StubsSrcJar = paths.stubsSrcJar.Path()
Paul Duffin10269f12020-06-19 18:39:55 +01003378 if paths.currentApiFilePath.Valid() {
3379 properties.CurrentApiFile = paths.currentApiFilePath.Path()
3380 }
3381 if paths.removedApiFilePath.Valid() {
3382 properties.RemovedApiFile = paths.removedApiFilePath.Path()
3383 }
Anton Hanssond78eb762021-09-21 15:25:12 +01003384 // The annotations zip is only available for modules that set annotations_enabled: true.
3385 if paths.annotationsZip.Valid() {
3386 properties.AnnotationsZip = paths.annotationsZip.Path()
3387 }
Paul Duffin106a3a42022-01-27 16:39:06 +00003388 s.Scopes[apiScope] = &properties
Paul Duffindd46f712020-02-10 13:37:10 +00003389 }
3390 }
3391
Paul Duffind7eb1c22020-05-26 20:57:10 +01003392 s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
Paul Duffin1267d872021-04-16 17:21:36 +01003393 s.Compile_dex = sdk.dexProperties.Compile_dex
Paul Duffina2ae7e02020-09-11 11:55:00 +01003394 s.Doctag_paths = sdk.doctagPaths
Paul Duffin869de142021-07-15 14:14:41 +01003395 s.Permitted_packages = sdk.PermittedPackagesForUpdatableBootJars()
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00003396 s.On_bootclasspath_since = sdk.commonSdkLibraryProperties.On_bootclasspath_since
3397 s.On_bootclasspath_before = sdk.commonSdkLibraryProperties.On_bootclasspath_before
3398 s.Min_device_sdk = sdk.commonSdkLibraryProperties.Min_device_sdk
3399 s.Max_device_sdk = sdk.commonSdkLibraryProperties.Max_device_sdk
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003400
Jihoon Kanga3a05462024-04-05 00:36:44 +00003401 implLibrary := sdk.getImplLibraryModule()
3402 if implLibrary != nil && implLibrary.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided {
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003403 s.DexPreoptProfileGuided = proptools.BoolPtr(true)
3404 }
Paul Duffindd46f712020-02-10 13:37:10 +00003405}
3406
3407func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinf7a64332020-05-13 16:54:55 +01003408 if s.Naming_scheme != nil {
3409 propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
3410 }
Paul Duffind7eb1c22020-05-26 20:57:10 +01003411 if s.Shared_library != nil {
3412 propertySet.AddProperty("shared_library", *s.Shared_library)
3413 }
Paul Duffin1267d872021-04-16 17:21:36 +01003414 if s.Compile_dex != nil {
3415 propertySet.AddProperty("compile_dex", *s.Compile_dex)
3416 }
Paul Duffin869de142021-07-15 14:14:41 +01003417 if len(s.Permitted_packages) > 0 {
3418 propertySet.AddProperty("permitted_packages", s.Permitted_packages)
3419 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +08003420 dexPreoptSet := propertySet.AddPropertySet("dex_preopt")
3421 if s.DexPreoptProfileGuided != nil {
3422 dexPreoptSet.AddProperty("profile_guided", proptools.Bool(s.DexPreoptProfileGuided))
3423 }
Paul Duffinf7a64332020-05-13 16:54:55 +01003424
Paul Duffine8409952022-09-22 16:24:46 +01003425 stem := s.Stem
3426
Jihoon Kang98aa8fa2024-06-07 11:06:57 +00003427 for _, apiScope := range AllApiScopes {
Paul Duffindd46f712020-02-10 13:37:10 +00003428 if properties, ok := s.Scopes[apiScope]; ok {
Paul Duffin6b836ba2020-05-13 19:19:49 +01003429 scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
Paul Duffindd46f712020-02-10 13:37:10 +00003430
Paul Duffin958806b2022-05-16 13:10:47 +00003431 scopeDir := apiScope.snapshotRelativeDir()
Paul Duffin3d1248c2020-04-09 00:10:17 +01003432
Paul Duffindd46f712020-02-10 13:37:10 +00003433 var jars []string
3434 for _, p := range properties.Jars {
Paul Duffine8409952022-09-22 16:24:46 +01003435 dest := filepath.Join(scopeDir, stem+"-stubs.jar")
Paul Duffindd46f712020-02-10 13:37:10 +00003436 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3437 jars = append(jars, dest)
3438 }
3439 scopeSet.AddProperty("jars", jars)
3440
Paul Duffin22628d52021-05-12 23:13:22 +01003441 if ctx.SdkModuleContext().Config().IsEnvTrue("SOONG_SDK_SNAPSHOT_USE_SRCJAR") {
3442 // Copy the stubs source jar into the snapshot zip as is.
Paul Duffine8409952022-09-22 16:24:46 +01003443 srcJarSnapshotPath := filepath.Join(scopeDir, stem+".srcjar")
Paul Duffin22628d52021-05-12 23:13:22 +01003444 ctx.SnapshotBuilder().CopyToSnapshot(properties.StubsSrcJar, srcJarSnapshotPath)
3445 scopeSet.AddProperty("stub_srcs", []string{srcJarSnapshotPath})
3446 } else {
3447 // Merge the stubs source jar into the snapshot zip so that when it is unpacked
3448 // the source files are also unpacked.
Paul Duffine8409952022-09-22 16:24:46 +01003449 snapshotRelativeDir := filepath.Join(scopeDir, stem+"_stub_sources")
Paul Duffin22628d52021-05-12 23:13:22 +01003450 ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
3451 scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
3452 }
Paul Duffin3d1248c2020-04-09 00:10:17 +01003453
Paul Duffin1fd005d2020-04-09 01:08:11 +01003454 if properties.CurrentApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003455 currentApiSnapshotPath := apiScope.snapshotRelativeCurrentApiTxtPath(stem)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003456 ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
3457 scopeSet.AddProperty("current_api", currentApiSnapshotPath)
3458 }
3459
3460 if properties.RemovedApiFile != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003461 removedApiSnapshotPath := apiScope.snapshotRelativeRemovedApiTxtPath(stem)
Paul Duffin3dbf9fd2020-06-02 13:00:02 +01003462 ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
Paul Duffin1fd005d2020-04-09 01:08:11 +01003463 scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
3464 }
3465
Anton Hanssond78eb762021-09-21 15:25:12 +01003466 if properties.AnnotationsZip != nil {
Paul Duffine8409952022-09-22 16:24:46 +01003467 annotationsSnapshotPath := filepath.Join(scopeDir, stem+"_annotations.zip")
Anton Hanssond78eb762021-09-21 15:25:12 +01003468 ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
3469 scopeSet.AddProperty("annotations", annotationsSnapshotPath)
3470 }
3471
Paul Duffindd46f712020-02-10 13:37:10 +00003472 if properties.SdkVersion != "" {
3473 scopeSet.AddProperty("sdk_version", properties.SdkVersion)
3474 }
3475 }
3476 }
3477
Paul Duffina2ae7e02020-09-11 11:55:00 +01003478 if len(s.Doctag_paths) > 0 {
3479 dests := []string{}
3480 for _, p := range s.Doctag_paths {
3481 dest := filepath.Join("doctags", p.Rel())
3482 ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
3483 dests = append(dests, dest)
3484 }
3485 propertySet.AddProperty("doctag_files", dests)
3486 }
Paul Duffindd46f712020-02-10 13:37:10 +00003487}
Spandan Dasdee1a742024-08-09 17:37:25 +00003488
3489// TODO(b/358613520): This can be removed when modules are no longer allowed to depend on the top-level library.
Cole Faustb36d31d2024-08-27 16:04:28 -07003490func (s *SdkLibrary) IDEInfo(ctx android.BaseModuleContext, dpInfo *android.IdeInfo) {
3491 s.Library.IDEInfo(ctx, dpInfo)
Spandan Dasdee1a742024-08-09 17:37:25 +00003492 if s.implLibraryModule != nil {
3493 dpInfo.Deps = append(dpInfo.Deps, s.implLibraryModule.Name())
3494 } else {
3495 // This java_sdk_library does not have an implementation (it sets `api_only` to true).
3496 // Examples of this are `art.module.intra.core.api` (IntraCore api surface).
3497 // Return the "public" stubs for these.
3498 stubPaths := s.findClosestScopePath(apiScopePublic)
3499 if len(stubPaths.stubsHeaderPath) > 0 {
3500 dpInfo.Jars = append(dpInfo.Jars, stubPaths.stubsHeaderPath[0].String())
3501 }
3502 }
3503}